什么可能导致源自fftw_destroy_plan

时间:2015-10-26 21:04:06

标签: c++ segmentation-fault valgrind fftw

我有一个使用mpirun运行的大型c ++程序,包括多线程FFTW。

所有FFTW操作都使用包装类完成。我不会发布整个类,因为它包含不同的结构和类,但构造函数的相关部分是:

int N_threads;
   fftw_complex *work;
   fftw_plan forward,backward;
   ...
   ...
   if(!fftw_init_threads()) error("Failed to initialize multitread fftw at nfft.h");
    int max_thread=omp_get_max_threads();
    fftw_plan_with_nthreads(N_threads); 
    if((!slave)&(max_thread<N_threads)) printf("A request to create an fftw with %i threads, the maximum available thread number is %i \n",N_threads,max_thread);
    for (int i=0; i < N.Dimension(); ++i) siz *= N(i);
    work = (fftw_complex*) fftw_malloc(sizeof(fftw_complex)*siz);
    int sign = FFTW_FORWARD;
    forward = fftw_plan_dft(N.Dimension(),&N(0), work, work,sign,Flags);
    sign = FFTW_BACKWARD;
    backward = fftw_plan_dft(N.Dimension(),&N(0), work, work, sign,Flags);

,析构函数包含以下命令:

cfftw_free(work);
if (forward)  fftw_destroy_plan(forward);
if (backward) fftw_destroy_plan(backward);
fftw_cleanup_threads();

在某些mpirun节点的某些运行被破坏期间,我得到了一个分段错误,并带有以下消息

[nina14:13154] [ 0] /lib/x86_64-linux-gnu/libpthread.so.0(+0x10340) [0x7ff474164340]
[nina14:13154] [ 1] /usr/lib/x86_64-linux-gnu/libfftw3.so.3(+0x2a36c) [0x7ff47550636c]
[nina14:13154] [ 2] /usr/lib/x86_64-linux-gnu/libfftw3.so.3(fftw_plan_awake+0x16) [0x7ff4754ff626]
[nina14:13154] [ 3] /usr/lib/x86_64-linux-gnu/libfftw3_threads.so.3(+0x31d0) [0x7ff4752d81d0]
[nina14:13154] [ 4] /usr/lib/x86_64-linux-gnu/libfftw3.so.3(fftw_plan_awake+0x16) [0x7ff4754ff626]
[nina14:13154] [ 5] /usr/lib/x86_64-linux-gnu/libfftw3.so.3(fftw_destroy_plan+0x13) [0x7ff4755cf723]

我听说内存泄漏很难找到,但我可以查看所有数据但仍无法找到来源。

valgrind似乎在跟踪mpirun使用的符号时遇到了问题,即使在-v --leak-check=full mpirun --trace-children=yes运行时,在mpirun开始使用Syscall param writev(vector[...]) points to uninitialised byte(s)之后停止监控,我也无法获得任何有价值的信息(对我而言) )信息

我正在寻找线索如何解决这个错误以及有关使用valgrind或其他程序来找到它的提示

编辑: 最终我能够直接在可执行文件上运行valgrind(没有mpirun参数,它指向同一区域:

==21869== Invalid read of size 8
==21869==    at 0x5BFB36C: ??? (in /usr/lib/x86_64-linux-gnu/libfftw3.so.3.3.2)
==21869==    by 0x5BF4625: fftw_plan_awake (in /usr/lib/x86_64-linux-gnu/libfftw3.so.3.3.2)
==21869==    by 0x5BF4625: fftw_plan_awake (in /usr/lib/x86_64-linux-gnu/libfftw3.so.3.3.2)
==21869==    by 0x5CC4722: fftw_destroy_plan (in /usr/lib/x86_64-linux-gnu/libfftw3.so.3.3.2)
==21869==    by 0x4BAB29: CartesianInterpreter::~CartesianInterpreter() (nfft.h:216)

==21869== Process terminating with default action of signal 11 (SIGSEGV)
==21869==  Access not within mapped region at address 0x790
==21869==    at 0x5BFB36C: ??? (in /usr/lib/x86_64-linux-gnu/libfftw3.so.3.3.2)
==21869==    by 0x5BF4625: fftw_plan_awake (in /usr/lib/x86_64-linux-gnu/libfftw3.so.3.3.2)
==21869==    by 0x5BF4625: fftw_plan_awake (in /usr/lib/x86_64-linux-gnu/libfftw3.so.3.3.2)
==21869==    by 0x5CC4722: fftw_destroy_plan (in /usr/lib/x86_64-linux-gnu/libfftw3.so.3.3.2)
==21869==    by 0x4BAB29: CartesianInterpreter::~CartesianInterpreter() (nfft.h:216)

如何让fftw_plan_awake调用内存泄漏?

EDIT2: 正如我所建议的那样,我附加了原始代码的大部分内容(只是复制粘贴)

class fftwizers {
public:
  int N_threads;
  IVector N;
  CVector Work;
  fftw_complex *work;
  fftw_plan forward,backward;
  fftwizers(){}
 fftwizers(IVector& iN,int n_threads=default_threads,unsigned Flags=FFTW_MEASURE):N(iN),N_threads(n_threads)
  {
    cfftwizers(Flags);
  }
 fftwizers(IVector& iN,CVector &input,int n_threads=default_threads,unsigned Flags=FFTW_MEASURE):N(iN),N_threads(n_threads)
  {
    cfftwizers(Flags);
//    input.ReDimension(Work.Dimension(),(complex*) &work[0]);
    input.ReDimension(Work);
  }

 fftwizers(int n,int n_threads=default_threads,unsigned Flags=FFTW_MEASURE):N(1),N_threads(n_threads)
    {
      N(0) = n;
      cfftwizers(Flags);
    }

 void ReDimension(IVector& iN,int n_threads=default_threads,unsigned Flags=FFTW_MEASURE)
  {
    N.ReDimension(iN);
    N_threads=n_threads;
    cfftwizers(Flags);
  }
 void ReDimension(int n,int n_threads=default_threads,unsigned Flags=FFTW_MEASURE)
    {
      N.ReDimension(1);
      N(0) = n;
      N_threads=n_threads;
      cfftwizers(Flags);
    }

  void cfftwizers(unsigned Flags)
  {
    int siz = 1;
    if(!fftw_init_threads()) error("Failed to initialize multitread fftw at nfft.h");
    int max_thread=omp_get_max_threads();
    fftw_plan_with_nthreads(N_threads); // every palns created after this line will use ## threads
    if((!slave)&(max_thread<N_threads)) printf("A request to create an fftw with %i threads, the maximum available thread number is %i \n",N_threads,max_thread);
    for (int i=0; i < N.Dimension(); ++i) siz *= N(i);
    //omp_set_num_threads(N_threads);
    //omp_set_dynamic(false);
    //work = fftw_alloc_complex(siz);
    //fftw_complex *work = (fftw_complex*) fftw_malloc(sizeof(fftw_complex)*siz);
    work = (fftw_complex*) fftw_malloc(sizeof(fftw_complex)*siz);
    Work.ReDimension(siz,(complex*) &work[0]);
//    if(!slave) printf("\nBuilding fftwizer with %i as a flag",Flags);
    int sign = FFTW_FORWARD;
    forward = fftw_plan_dft(N.Dimension(),&N(0), work, work,sign,Flags);
    sign = FFTW_BACKWARD;
    backward = fftw_plan_dft(N.Dimension(),&N(0), work, work, sign,Flags);
  }
  ~fftwizers()
    {
      fftw_free(work);
      if (forward)  fftw_destroy_plan(forward);
      if (backward) fftw_destroy_plan(backward);
      fftw_cleanup_threads();
    }
  void go(int sign,CVector& Arr)
  {
    Work = Arr;
    if (sign==FFTW_FORWARD)
      fftw_execute(forward);
    else

...    ...

1 个答案:

答案 0 :(得分:1)

最终导致此错误的原因是

fftw_cleanup_threads();

当类的第一个对象被破坏时它被激活,它删除了所有FFTW计划使用的一些重要的全局数据(甚至是在别处构建的那些)。 解决方案是删除这一行,因为它无论如何都没有做多少