2

I have some data of Complex Values of size N, and would like to compute the FFT of this data using the Intel OneAPI. Here is my code:

# Connectivity
#include <bits/stdc++.h>
#include "mkl_dfti.h"
#include <complex.h>

using namespace std;
float pi = 2*acos(0.0);

int main(){
        long long int N; cin >> N
        float _Complex c2c_data[N];
        DFTI_DESCRIPTOR_HANDLE my_desc1_handle = NULL;
        DFTI_DESCRIPTOR_HANDLE my_desc2_handle = NULL;
        MKL_LONG status;
// data is inserted here
        status = DftiCreateDescriptor(&my_desc1_handle, DFTI_SINGLE, DFTI_COMPLEX, 1, N);
        status = DftiCommitDescriptor(my_desc1_handle);
        status = DftiComputeForward(my_desc1_handle, c2c_data);
        status = DftiFreeDescriptor(&my_desc1_handle);
        cout << round(cabs(c2c_data[s]) / N) << "\n";
        return 0;
}

This works for smaller cases of N, but for larger cases (around 2^21), I get a segmentation fault and for even larger cases, I get a Bus error. I have checked that this happens at the DftiComputeForward function. The length of the data is specified in DftiCreateDescriptor which is indeed N in this case, so I am not sure why I am getting this error.

Here is here how I compile my code:

dpcpp test.cpp -lmkl_intel_lp64 -lmkl_core -lmkl_intel_thread -liomp5 -ldl -lpthread -o test

It would be great if someone could help. Thank you!

tadman
  • 208,517
  • 23
  • 234
  • 262
rawnyogn
  • 21
  • 1
  • 2
    You might be blowing your stack space. On C++ consider using `std::vector`. – tadman Apr 21 '21 at 07:06
  • I don't think this is the case because it crashes only during the DftiComputeForward function and it is fine even after I put the data in. I only used an array because the example provided by Intel did it with an array. I will try it though, thank you! – rawnyogn Apr 21 '21 at 07:16
  • `float _Complex c2c_data[N];` is a VLA (which is a non-standard extension) so it is good to "unlearn" that by using a `vector` anyway. – Ted Lyngmo Apr 21 '21 at 07:19
  • The [DftiComputeForward](https://software.intel.com/content/www/us/en/develop/documentation/onemkl-developer-reference-fortran/top/fourier-transform-functions/fft-functions/fft-computation-functions/dfticomputeforward.html) documentation has stated that it only works on an array so I cannot use a vector in this case. I've already tried changing it to a vector and it does not compile, as expected. – rawnyogn Apr 21 '21 at 07:23
  • Did you try `std::vector c2c_data(N);` and `status = DftiComputeForward(my_desc1_handle, c2c_data.data());`? – Ted Lyngmo Apr 21 '21 at 07:25
  • 1
    Okay I've tried and it works for now. I'm still running on larger test cases. Could I have some idea on the difference in the stack space used? Am I not using the same amount of space? – rawnyogn Apr 21 '21 at 07:40
  • Yes, the same amount of space - but the stack space is usually very small in comparison to the heap space. Btw, where is `s` defined? – Ted Lyngmo Apr 21 '21 at 08:00
  • I removed the part of the code about `s` because I thought it was irrelevant. I'm just reading it from input. Thank you! – rawnyogn May 09 '21 at 07:38

1 Answers1

1

you might be exhausting your stack memory space.

Please consider using a vector instead.

Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
  • I have not checked for extremely large test cases because the runtime is too long but it works now for the larger test cases which it failed previously! – rawnyogn May 09 '21 at 07:39