【问题标题】:Why FFTW on Windows is faster than on Linux?为什么 Windows 上的 FFTW 比 Linux 上的快?
【发布时间】:2011-12-31 08:21:50
【问题描述】:

我使用 fftw 库(fftw3.afftw3.lib)在 Linux 和 Windows 中编写了两个相同的程序,并计算了 fftwf_execute(m_wfpFFTplan) 语句的持续时间(16-fft)。

对于 10000 次运行:

  • 在 Linux 上:平均时间为 0.9
  • 在 Windows 上:平均时间为 0.12

我很困惑为什么这在 Windows 上比在 Linux 上快 9 倍。

处理器:Intel(R) Core(TM) i7 CPU 870 @ 2.93GHz

每个操作系统(Windows XP 32 位和 Linux OpenSUSE 11.4 32 位)都安装在同一台机器上。

我从 Internet 下载了 fftw.lib(适用于 Windows),但不知道该配置。一旦我使用此配置构建 FFTW:

/configure --enable-float  --enable-threads --with-combined-threads  --disable-fortran  --with-slow-timer  --enable-sse  --enable-sse2  --enable-avx   

在 Linux 中,它生成的库比默认配置 (0.4 毫秒) 快四倍。

【问题讨论】:

  • fftw 库是否使用相同的编译器(和版本)编译?它们是用相同的标志编译的吗?是为相同的架构编译的吗?也许 Windows 版本正在更好地利用 CPU 功能。可能是编译器不同,所以优化的也不同。
  • 请发布您在两个平台上使用的编译器的详细信息,以及用于您的代码和构建该库的编译选项。
  • 位数是否相同(32 位与 64 位)?相同数量的 RAM 可用?还有哪些其他进程并行运行?任何虚拟化处于活动状态?
  • 如果不研究这两个程序的来源并检查进行测试的系统的硬件、软件和软件配置,就无法真正回答这个问题。 :)
  • 我刚刚查看了我在 Pentium4 单核 3GHz(早在 2005 年)上使用 FFTW 计算的 FFT 的一些时序,并且在 2us 内计算了 16 长度的 FFT,即:0.002ms。时间没有超过 1ms,直到长度为 16384。一个 16 长度的 FFT 应该是非常轻量级的。您是否在基准计时器之外执行计划阶段?您在计划时是否使用 FFTW_MEASURE?必须使用 SSE/SSE2 和全面优化,因为 Windows(尤其是 Visual Studio)将在选择发布模式时默认启用这些

标签: linux windows performance fft fftw


【解决方案1】:

16 FFT 非常小。您会发现小于 64 的 FFT 将是硬编码的汇编程序,没有循环以获得尽可能高的性能。这意味着它们很容易受到指令集、编译器优化甚至 64 位或 32 位字的变化的影响。

当您以 2 的幂对 16 -> 1048576 的 FFT 大小进行测试时会发生什么?我说这是因为 Linux 上特定的硬编码 asm 例程可能不是最适合您的机器的优化程序,而您可能很幸运地在 Windows 上实现了该特定大小。比较此范围内的所有大小,您可以更好地了解 Linux 与 Windows 的性能。

您是否校准过 FFTW?当第一次运行时,FFTW 猜测每台机器最快的实现,但是如果你有特殊的指令集,或者特定大小的缓存或其他处理器特性,那么这些会对执行速度产生巨大的影响。因此,执行校准将测试各种 FFT 例程的速度,并为您的特定硬件选择最快的每个大小。校准涉及重复计算计划并保存生成的 FFTW“智慧”文件。然后可以重新使用保存的校准数据(这是一个漫长的过程)。我建议在您的软件启动时执行一次并每次重新使用该文件。我注意到校准后某些尺寸的性能提高了 4-10 倍!

下面是我用来为某些尺寸校准 FFTW 的代码的 sn-p。请注意,此代码是从我工作的 DSP 库中逐字粘贴的,因此某些函数调用特定于我的库。我希望 FFTW 特定的电话会有所帮助。

// Calibration FFTW
void DSP::forceCalibration(void)
{
// Try to import FFTw Wisdom for fast plan creation
FILE *fftw_wisdom = fopen("DSPDLL.ftw", "r");

// If wisdom does not exist, ask user to calibrate
if (fftw_wisdom == 0)
{
    int iStatus2 = AfxMessageBox("FFTw not calibrated on this machine."\
        "Would you like to perform a one-time calibration?\n\n"\
        "Note:\tMay take 40 minutes (on P4 3GHz), but speeds all subsequent FFT-based filtering & convolution by up to 100%.\n"\
        "\tResults are saved to disk (DSPDLL.ftw) and need only be performed once per machine.\n\n"\
        "\tMAKE SURE YOU REALLY WANT TO DO THIS, THERE IS NO WAY TO CANCEL CALIBRATION PART-WAY!", 
        MB_YESNO | MB_ICONSTOP, 0);

    if (iStatus2 == IDYES)
    {
        // Perform calibration for all powers of 2 from 8 to 4194304
        // (most heavily used FFTs - for signal processing)
        AfxMessageBox("About to perform calibration.\n"\
            "Close all programs, turn off your screensaver and do not move the mouse in this time!\n"\
            "Note:\tThis program will appear to be unresponsive until the calibration ends.\n\n"
            "\tA MESSAGEBOX WILL BE SHOWN ONCE THE CALIBRATION IS COMPLETE.\n");
        startTimer();

        // Create a whole load of FFTw Plans (wisdom accumulates automatically)
        for (int i = 8; i <= 4194304; i *= 2)
        {
            // Create new buffers and fill
            DSP::cFFTin = new fftw_complex[i];
            DSP::cFFTout = new fftw_complex[i];
            DSP::fconv_FULL_Real_FFT_rdat = new double[i];
            DSP::fconv_FULL_Real_FFT_cdat = new fftw_complex[(i/2)+1];
            for(int j = 0; j < i; j++)
            {
                DSP::fconv_FULL_Real_FFT_rdat[j] = j;
                DSP::cFFTin[j][0] = j;
                DSP::cFFTin[j][1] = j;
                DSP::cFFTout[j][0] = 0.0;
                DSP::cFFTout[j][1] = 0.0;
            }

            // Create a plan for complex FFT.
            // Use the measure flag to get the best possible FFT for this size
            // FFTw "remembers" which FFTs were the fastest during this test. 
            // at the end of the test, the results are saved to disk and re-used
            // upon every initialisation of the DSP Library
            DSP::pCF = fftw_plan_dft_1d
                (i, DSP::cFFTin, DSP::cFFTout, FFTW_FORWARD, FFTW_MEASURE);

            // Destroy the plan
            fftw_destroy_plan(DSP::pCF);

            // Create a plan for real forward FFT
            DSP::pCF = fftw_plan_dft_r2c_1d
                (i, fconv_FULL_Real_FFT_rdat, fconv_FULL_Real_FFT_cdat, FFTW_MEASURE);

            // Destroy the plan
            fftw_destroy_plan(DSP::pCF);

            // Create a plan for real inverse FFT
            DSP::pCF = fftw_plan_dft_c2r_1d
                (i, fconv_FULL_Real_FFT_cdat, fconv_FULL_Real_FFT_rdat, FFTW_MEASURE);

            // Destroy the plan
            fftw_destroy_plan(DSP::pCF);

            // Destroy the buffers. Repeat for each size
            delete [] DSP::cFFTin;
            delete [] DSP::cFFTout;
            delete [] DSP::fconv_FULL_Real_FFT_rdat;
            delete [] DSP::fconv_FULL_Real_FFT_cdat;
        }

        double time = stopTimer();

        char * strOutput;
        strOutput = (char*) malloc (100);
        sprintf(strOutput, "DSP.DLL Calibration complete in %d minutes, %d seconds\n"\
            "Please keep a copy of the DSPDLL.ftw file in the root directory of your application\n"\
            "to avoid re-calibration in the future\n", (int)time/(int)60, (int)time%(int)60);
        AfxMessageBox(strOutput);

        isCalibrated = 1;

        // Save accumulated wisdom
        char * strWisdom = fftw_export_wisdom_to_string();  
        FILE *fftw_wisdomsave = fopen("DSPDLL.ftw", "w");
        fprintf(fftw_wisdomsave, "%s", strWisdom);

        fclose(fftw_wisdomsave);
        DSP::pCF = NULL;
        DSP::cFFTin = NULL;
        DSP::cFFTout = NULL;
        fconv_FULL_Real_FFT_cdat = NULL;
        fconv_FULL_Real_FFT_rdat = NULL;
        free(strOutput);
    }
}
else 
{
    // obtain file size.
    fseek (fftw_wisdom , 0 , SEEK_END);
    long lSize = ftell (fftw_wisdom);
    rewind (fftw_wisdom);

    // allocate memory to contain the whole file.
    char * strWisdom = (char*) malloc (lSize);

    // copy the file into the buffer.
    fread (strWisdom,1,lSize,fftw_wisdom);

    // import the buffer to fftw wisdom
    fftw_import_wisdom_from_string(strWisdom);

    fclose(fftw_wisdom);
    free(strWisdom);

    isCalibrated = 1;

    return;
}
}

秘诀是使用 FFTW_MEASURE 标志创建计划,该标志专门测量数百个例程,以便为您的特定 FFT 类型(真实、复杂、1D、2D)和大小找到最快的:

DSP::pCF = fftw_plan_dft_1d (i, DSP::cFFTin, DSP::cFFTout, 
   FFTW_FORWARD, FFTW_MEASURE);

最后,所有基准测试还应在执行之外的单个 FFT 计划阶段执行,从在发布模式下编译的代码中调用,并优化并与调试器分离。基准测试应该在具有数千(甚至数百万)次迭代的循环中执行,然后取平均运行时间来计算结果。正如您可能知道的那样,计划阶段需要大量时间,并且执行旨在通过一个计划执行多次。

【讨论】:

    猜你喜欢
    • 2011-12-25
    • 1970-01-01
    • 2014-10-16
    • 2021-12-22
    • 2014-08-02
    • 2011-02-06
    • 1970-01-01
    • 2010-09-08
    • 1970-01-01
    相关资源
    最近更新 更多