什么可能导致源自 fftw_destroy_plan 的分段错误
Posted
技术标签:
【中文标题】什么可能导致源自 fftw_destroy_plan 的分段错误【英文标题】:what could cause a segmentation fault stemming from fftw_destroy_plan 【发布时间】:2015-10-26 21:04:06 【问题描述】:我有一个使用 mpirun 运行并包含多线程 FFTW 的大型 c++ 程序。
所有 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 调用内存泄漏?
编辑2: 正如建议的那样,我附上了大部分原始代码(只是复制粘贴)
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】:这个bug的最终原因是
fftw_cleanup_threads();
当类的第一个对象被破坏时它被激活,它删除了所有 FFTW 计划使用的一些重要的全局数据(即使是在其他地方构建的)。 解决方案是删除此行,因为它并没有做太多
【讨论】:
以上是关于什么可能导致源自 fftw_destroy_plan 的分段错误的主要内容,如果未能解决你的问题,请参考以下文章