1

我对通过调用 fftwf_plan_many_dft_r2c() 并使用 OpenMP 执行它来创建 many_plan 有点困惑。我在这里想要实现的是看看是否明确使用 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);
}

我得到的是这样的:

如果我使用 fftwf_execute(p),程序执行成功,但结果似乎不正确。(我将结果与不使用 many_plan 和 openmp 的版本进行比较)

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

有人可以在这里帮助我吗?我应该如何跨多个线程分区数据?或者它首先是不正确的。

先感谢您。

飞人

4

1 回答 1

1
  • 您是否为out正确分配内存?做这个:
out = (fftwf_complex *)&in[0];  // for in-place transformation

做同样的事情:

out = (fftw_complex*)fftw_malloc(sizeof(fftw_complex)*numberOfOutputColumns);
  • 您正在尝试访问并行块内的“p”,而没有具体告诉 openMP 如何使用它。它应该是:

用于共享的 pragma omp 并行(p)

  • 如果您要将工作拆分为 n 个线程,我认为您会明确地想要告诉 omp 使用 n 个线程:

pragma omp parallel for shared(p) num_threads(n)

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

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

我仍在学习 OpenMP + FFTW,所以我可能在这些方面错了。StackOverflow 不喜欢我在编译指示前面加上 #,但你需要一个。

于 2013-02-21T20:36:15.917 回答