【问题标题】:Getting error when using FFTW plan as threadprivate将 FFTW 计划用作线程私有时出错
【发布时间】:2020-08-03 09:52:09
【问题描述】:

我正在尝试在使用 FFTW 操作的代码上使用 OpenMP 来执行一些复杂的代数。每个线程都需要使用自己的工作数组单独工作。因此,我遵循了这个答案(Reusable private dynamically allocated arrays in OpenMP)并尝试使用threadprivate 功能。 FFTW 计划和工作区变量需要在多个并行段的代码中多次使用。

以下是测试代码。但是,它会在p1=fftw_plan_ ... 行给出各种错误(分段错误等)。知道我做错了什么吗?这似乎只适用于数组,但不适用于FFTW 计划。 FFTW 计划是否可以由线程共享但处理它们单独的数据段 inpdoutc1outc2

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
...
#include <math.h>
#include <complex.h>
#include <omp.h>
#include <fftw3.h>

int main(int argc, char *argv[])
{
    int npts2, id;
    
    static double *inpd;
    static fftw_complex *outc1, *outc2;
    static fftw_plan p1, p2;
    static int npts2stat;
    #pragma omp threadprivate(inpd,outc1,outc2,p1,p2,npts2stat)
    
    npts2=1000;
    
    #pragma omp parallel private(id) shared(npts2)
    {
        id=omp_get_thread_num();
        printf("Thread %d: Allocating threadprivate memory \n",id);
        
        npts2stat=npts2;
        
        printf("step1 \n");
        inpd=malloc(sizeof(double)*npts2stat);
        outc1=fftw_malloc(sizeof(fftw_complex)*npts2stat);
        outc2=fftw_malloc(sizeof(fftw_complex)*npts2stat);
        printf("step2 \n");
        // CODE COMPILES FINE BUT STOPS HERE WITH ERROR
        p1=fftw_plan_dft_r2c_1d(npts2stat,inpd,outc1,FFTW_ESTIMATE);
        p2=fftw_plan_dft_1d(npts2stat,outc1,outc2,FFTW_BACKWARD,FFTW_ESTIMATE);
        printf("step3 \n");
    }
    
    // multiple omp parallel segments with different threads doing some calculations on complex numbers using FFTW
    
    #pragma omp parallel private(id)
    {
        id=omp_get_thread_num();
        printf("Thread %d: Deallocating threadprivate memory \n",id);
        free(inpd);
        fftw_free(outc1);
        fftw_free(outc2);
        fftw_destroy_plan(p1);
        fftw_destroy_plan(p2);
    }
}

EDIT1:我似乎已经解决了下面的分段错误问题。但是,如果您有更好的方法来执行此操作,那将很有帮助。例如,我不知道所有线程的单个计划是否足够,因为它作用于不同的数据inpd 私有到不同的线程。

【问题讨论】:

    标签: c static openmp fftw


    【解决方案1】:

    抱歉,FFTW 手册中关于线程安全的部分清楚地表明,一次只能调用一个线程。这似乎解决了分段错误问题。

    #pragma omp critical
            {
            p1=fftw_plan_dft_r2c_1d(npts2stat,inpd,outc1,FFTW_ESTIMATE);
            p2=fftw_plan_dft_1d(npts2stat,outc1,outc2,FFTW_BACKWARD,FFTW_ESTIMATE);
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-02-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多