C 创建多个计划和执行计划时出现的问题

C 创建多个计划和执行计划时出现的问题,c,fftw,C,Fftw,通过调用fftwf_plan_many_dft_r2c()并使用OpenMP执行它,我对创建多个计划感到有点困惑。我在这里试图实现的是,看看显式使用OpenMP和组织FFTW数据是否可以协同工作。(我知道我“应该”使用多线程版本的fftw,但我没有从中获得预期的加速) 我的代码如下所示: /* I ignore some helper APIs */ #define N 1024*1024 //N is the total size of 1d fft fftwf_plan p; float

通过调用fftwf_plan_many_dft_r2c()并使用OpenMP执行它,我对创建多个计划感到有点困惑。我在这里试图实现的是,看看显式使用OpenMP和组织FFTW数据是否可以协同工作。(我知道我“应该”使用多线程版本的fftw,但我没有从中获得预期的加速)

我的代码如下所示:

/* I ignore some helper APIs */
#define N 1024*1024 //N is the total size of 1d fft 
fftwf_plan p;
float * in;
fftwf_complex *out;

omp_set_num_threads(threadNum); // Suppose threadNum is 2 here
in = fftwf_alloc_real(2*(N/2+1));
std::fill(in,in+2*(N/2+1),1.1f); // just try with a random real floating numbers
out = (fftwf_complex *)&in[0];  // for in-place transformation
/* Problems start from here */
int n[] = {N/threadNum}; // according to the manual, n is the size of each "howmany" transformation
p = fftwf_plan_many_dft_r2c(1, n, threadNum, in, NULL,1 ,1, out, NULL, 1, 1, FFTW_ESTIMATE);

#pragma omp parallel for
for (int i = 0; i < threadNum; i ++)
{
    fftwf_execute(p);
    // fftwf_execute_dft_r2c(p,in+i*N/threadNum,out+i*N/threadNum);
}
/*我忽略了一些助手API*/
#定义N 1024*1024//N是1d fft的总大小
fftwf_计划p;
浮入;
fftwf_复合体*out;
omp_set_num_threads(threadNum);//假设threadNum在这里是2
in=fftwf_alloc_real(2*(N/2+1));
标准::填充(in,in+2*(N/2+1),1.1f);//试着用一个随机的实数浮点数
out=(fftwf_复数*)&in[0];//用于就地转换
/*问题从这里开始*/
int n[]={n/threadNum};//根据手册,n是每个“多少”转换的大小
p=fftwf\u plan\u many\u dft\u r2c(1,n,threadNum,in,NULL,1,1,out,NULL,1,1,FFTW\u估计);
#pragma-omp并行
对于(int i=0;i
我得到的是这样的:

/* I ignore some helper APIs */
#define N 1024*1024 //N is the total size of 1d fft 
fftwf_plan p;
float * in;
fftwf_complex *out;

omp_set_num_threads(threadNum); // Suppose threadNum is 2 here
in = fftwf_alloc_real(2*(N/2+1));
std::fill(in,in+2*(N/2+1),1.1f); // just try with a random real floating numbers
out = (fftwf_complex *)&in[0];  // for in-place transformation
/* Problems start from here */
int n[] = {N/threadNum}; // according to the manual, n is the size of each "howmany" transformation
p = fftwf_plan_many_dft_r2c(1, n, threadNum, in, NULL,1 ,1, out, NULL, 1, 1, FFTW_ESTIMATE);

#pragma omp parallel for
for (int i = 0; i < threadNum; i ++)
{
    fftwf_execute(p);
    // fftwf_execute_dft_r2c(p,in+i*N/threadNum,out+i*N/threadNum);
}
如果使用fftwf_execute(p),程序将成功执行,但结果似乎不正确。(我将结果与不使用many_plan和openmp的版本进行比较)

如果我使用fftwf_execute_dft_r2c(),我得到了分段错误

有人能帮我吗?我应该如何跨多个线程划分数据?或者一开始就不正确

先谢谢你

flyree

  • 您是否为out正确分配内存?这是否:
执行与此相同的操作:

out = (fftw_complex*)fftw_malloc(sizeof(fftw_complex)*numberOfOutputColumns);
  • 您正在尝试访问并行块中的“p”,而没有明确告诉openMP如何使用它。应该是:
用于共享的pragma omp并行(p)

  • 如果要将工作分成n个线程,我认为您应该明确地告诉omp使用n个线程:
用于共享(p)num_线程(n)的pragma omp并行

  • 这段代码在没有多线程的情况下工作吗?如果您删除for循环和openMP调用并执行fftwf_execute(p)一次,它会工作吗

  • 我对FFTW的很多计划不太了解,但似乎p是很多计划,而不是一个单一的计划。所以,当你“执行”p时,你同时执行所有的计划,对吗?您实际上不需要迭代执行p

我仍在学习OpenMP+FFTW,因此我可能在这些方面出错。StackOverflow不喜欢我在pragma前面放一个#,但你需要一个。

  • 您是否为out正确分配内存?这是否:
执行与此相同的操作:

out = (fftw_complex*)fftw_malloc(sizeof(fftw_complex)*numberOfOutputColumns);
  • 您正在尝试访问并行块中的“p”,而没有明确告诉openMP如何使用它。应该是:
用于共享的pragma omp并行(p)

  • 如果要将工作分成n个线程,我认为您应该明确地告诉omp使用n个线程:
用于共享(p)num_线程(n)的pragma omp并行

  • 这段代码在没有多线程的情况下工作吗?如果您删除for循环和openMP调用并执行fftwf_execute(p)一次,它会工作吗

  • 我对FFTW的很多计划不太了解,但似乎p是很多计划,而不是一个单一的计划。所以,当你“执行”p时,你同时执行所有的计划,对吗?您实际上不需要迭代执行p

我仍在学习OpenMP+FFTW,因此我可能在这些方面出错。StackOverflow不喜欢我在pragma前面放一个#,但你需要一个