C++使用fftw3多线程计算出现异常


std::vector<double> periodogramPSE(const std::vector<double>& xn, const std::vector<double>& wn, int L)
{
    int N = xn.size();
    int M = wn.size();
    assert(M <= N);
    //if (M < N)
    //{
    //    std::cerr << "The length of window is smaller than the length of data, ";
    //    std::cerr << "the data will be trucated to the window length!" << std::endl;
    //}
    vector<double> wxn(L>=M? M:L);
    int S = 0;
    if (L >= M)
    {
        S = M;

        for (int i = 0; i < M; ++i)
            wxn[i] = xn[i] * wn[i];
    }
    else
    {
        S = L;
        //std::cerr << "The FFT points is smaller than the data points, ";
        //std::cerr << "the data will be trucated to the FFT points!" << std::endl;
        for (int i = 0; i < L; ++i)
            wxn[i] = xn[i] * wn[i];
    }
    
    fftw_complex* in, * out;
    fftw_plan p;
    in = (fftw_complex*)fftw_malloc(sizeof(fftw_complex) * S);
    out = (fftw_complex*)fftw_malloc(sizeof(fftw_complex) * S);
    for (int i = 0; i < S; i++)
    {
        in[i][0] = wxn[i];
        in[i][1] = 0;
        //std::cout << in[i][0] << "," << in[i][1] << " ";
    }
    p = fftw_plan_dft_1d(S, in, out, FFTW_FORWARD, FFTW_ESTIMATE);
    fftw_execute(p); /* repeat as needed */

    
    //complex<double> x = reinterpret_cast<complex<double>*>(out);
    //complex<double> x;
    //abs(x);
    std::vector<double> absXk;
    for (int i = 0; i < S; i++)
    {
        absXk.push_back((pow(out[i][0], 2) + pow(out[1][0], 2)) / M);
    }
    fftw_destroy_plan(p);
    fftw_free(out);
    fftw_free(in);
    //for (auto& i : absXk)
    //{
    //    i *= i;
    //    i /= M;
    //}
    return absXk;

}

出现异常的时间不确定,使用多线程同时运算6个矩阵,每个矩阵800行左右,每行3000个数据,函数输入是一个矩阵的一行,一般来说运算到第十几个矩阵“p = fftw_plan_dft_1d(S, in, out, FFTW_FORWARD, FFTW_ESTIMATE);”就出现异常。我检测fftw的输入没有问题,出现异常的矩阵重新计算也不会出现异常。到底是什么原因导致的出现异常?异常截图:

img

多线程使用的openmp。