FFTW导出智慧文件,但无法加载

FFTW exports wisdom file but cannot load it

本文关键字:加载 文件 智慧 FFTW      更新时间:2023-10-16

我正在努力为FFTW操作实现更好的性能。出于这个原因,我决定使用智慧文件创建计划,但问题是它无法从智慧文件加载计划(导出智慧很好)。我试图将智慧导出到一个文件中,并在下一个程序运行时将其加载回智慧文件,但该函数总是为任何文件名返回0(即使是不存在的文件名)。我也尝试过从字符串加载,但也不起作用。

这里似乎也有类似的问题,但都没有得到回答,或者问题出在其他地方。那么这是库中的错误,还是我做错了什么?

编辑仅用于演示和查看导出功能是否真的有效:下面的代码显示了使用用于内容导入的文件从字符串加载智慧的操作(仅用于显示文件中的内容可用):

FILE * pfile;
vector<char> buffer;
pfile=fopen("WisdomFile.txt","r");
if(pfile==0)
    cout<<"Could not open file"<<endl;
else
    cout<<"Could open file successfully"<<endl;
long length;
if(pfile)
{
    fseek(pfile,0,SEEK_END);
    length=ftell(pfile);
    fseek(pfile,0,SEEK_SET);
    buffer.assign(length+1,''); //allocate space with the same length as the file
    int n=fread(&buffer[0],1,length,pfile); //read whole file to the buffer
    assert(n==length);
    fclose(pfile);
}
string show_wisdom(buffer);
cout<<show_wisdom<<endl;    //content could be read
int ret=fftwf_import_wisdom_from_string(reinterpret_cast<const char*>(&buffer[0])); // returns 0 for every filename
buffer.clear();
//... further FFTW code trying to use `FFTW_PATIENT | FFTW_USE_WISDOM_ONLY` ->but never uses the wisdom file

输出如下:

    (fftw-3.3.4 fftwf_wisdom #xbedb7e38 #x1ac524dc #x7a69378e #x21629161
  (fftwf_codelet_t2_8 3 #x11048 #x11048 #x0 #xa75017ef #xb6eb4747 #x4bef8a59 #xb03d9427)
  (fftwf_dft_thr_vrank_geq1_register 1 #x11048 #x11048 #x0 #x27a0c32d #x4e3441f9 #xb3fb3f2d #x90ae8374)
  (fftwf_dft_vrank_geq1_register 0 #x11048 #x11048 #x0 #x9be02645 #x53c7643d #xf6cf9608 #xed5460b7)
  (fftwf_dft_r2hc_register 0 #x11048 #x11048 #x0 #x52a71bc4 #x3c83e70d #x942dd977 #xf047f7e9)
  (fftwf_codelet_n1_64 0 #x11448 #x11448 #x0 #x11559ac4 #xea86db86 #xad6ae8e4 #x97f477c6)
  (fftwf_codelet_t1_16 0 #x11048 #x11048 #x0 #x8811820f #xea00b698 #x861ae7ed #x109ec45a)
  (fftwf_rdft_rank0_register 2 #x11048 #x11048 #x0 #x0095ff64 #x86e47338 #x76e9cf55 #x6cde6434)
  (fftwf_codelet_t1_16 1 #x11048 #x11048 #x0 #x29eda2bf #x97038fb2 #x0eddb089 #xafc2b57e)
  (fftwf_dft_indirect_register 0 #x11048 #x11048 #x0 #x1bea55f5 #x48417896 #x04bc4c58 #x571ce0b9)
  (fftwf_dft_thr_vrank_geq1_register 0 #x11048 #x11048 #x0 #x7b53c8cd #xda17faa2 #x220c1322 #x7c207bbd)
)

因此,如上所示,导出函数似乎可以工作,但如FFTW教程中所示导入它似乎没有效果(该程序仍然试图使用FFTW_PATIENT选项创建一个新计划,这大约需要5分钟。

我一直有这个问题,直到我注意到我使用函数void fftw_set_timelimit(double seconds)来限制总计划创建时间。无法加载在此时间限制下生成的智慧,而可以加载禁用时间限制后生成的智慧。即使没有超过时间限制,情况似乎也是如此。

由于您的代码已经过编辑,我不确定这是否适用于您的特定问题,但其他人可能会觉得这很有用。

根据FFTW的文件:

智慧会自动用于任何适用的尺寸,只要规划者标志不比创造智慧的标志更"耐心"。

换句话说,如果要为使用FFTW_PATIENT标志创建的计划导入智慧,则必须为使用相同大小和FFTW_PATIENTFFTW_EXHAUSTIVE标志创建的规划导出智慧。

然后,顺序将是首先提前制定计划,并用输出智慧

// Might as well use FFTW_EXHAUSTIVE if we can afford it ahead of time
fftw_plan plan = fftw_plan_dft_r2c_1d(N, in, out, FFTW_EXHAUSTIVE);
fftw_export_wisdom_to_filename("WisdomFile.txt");

最后,导出的智慧可以导入并使用:

if (!fftw_import_wisdom_from_filename("WisdomFile.txt"))
{
  std::cout << "Warning: could not import wisdom file" << std::endl;
}
// plan can now be created with any flags less or equal to 
// FFTW_EXHAUSTIVE used for the exported wisdom.
fftw_plan plan = fftw_plan_dft_r2c_1d(N, in, out, FFTW_PATIENT);