Intel® oneAPI Math Kernel Library
Ask questions and share information with other developers who use Intel® Math Kernel Library.

Problem with mkl fft dynamic array

Semen_K_
Beginner
388 Views
 
Hi, I want to use the mklfft with multithreading, but I faced the problem. I got an error - Segmentation fault (core dumped) even when I just created the simple 2D dimension dynamic array. Also, I have checked the multithread example with static array, it worked perfectly, but when I created dynamic array, it gave me an error. Could you tell me what is wrong I am doing, please? 
 
This is my code:
#include "mkl_dfti.h"
#include <omp.h>
#include <math.h>
#include <complex.h>
#include <float.h>
#include <sys/time.h>
 
int main(int argc, char *argv[])
{
    int k, j, N = atoi(argv[1]);
    MKL_Complex8 **data = malloc(N*sizeof(MKL_Complex8*));
    for(k=0; k<N; k++){
       data = malloc(N*sizeof(MKL_Complex8));
    }
 
    printf(
      "Before FFT: N %d.\n",
      N);
 
    MKL_LONG len[2] = {N, N};
    DFTI_DESCRIPTOR_HANDLE FFT;
    int th;
    printf("Create...\n");
    DftiCreateDescriptor (&FFT, DFTI_SINGLE, DFTI_COMPLEX, 2, len);
    DftiCommitDescriptor (FFT);
 
    printf("Execute...\n");
    struct timeval start, end;
    gettimeofday(&start, NULL);
 
    DftiComputeForward (FFT, data);
 
    DftiFreeDescriptor (&FFT);
 
    gettimeofday(&end, NULL);
 
    double tstart = start.tv_sec + start.tv_usec/1000000.;
    double tend = end.tv_sec + end.tv_usec/1000000.;
    double t_sec = (tend - tstart);
    int S = N*N;
    double speed = (double) 5*S*log2(S)/t_sec*1e-6;
 
    printf("Time: %0.6f, Speed: %0.6f\n", t_sec, speed);
 
    for(j=0; j<N; j++) {
       free(data);
    }
    free(data);
 
    return 0;
}
 
0 Kudos
3 Replies
Zhen_Z_Intel
Employee
388 Views

Dear customer,

The MKL FFT function for execution's required input datatype is pointer for complex/floating array, not address of pointer. You probably need define the data as MKL_Complex8 * which size is N*N. Please do following work:

MKL_Complex8 *data = (MKL_Complex8*)malloc(N*N*sizeof(MKL_Complex8));
/*MKL_Complex8 **data =(MKL_Complex8 **) malloc(N*sizeof(MKL_Complex8*));
    for(k=0; k<N*N; k++){
        data = (MKL_Complex8*)malloc(N*sizeof(MKL_Complex8));
    }*/
....

/*for(j=0; j<N; j++) {
    free(data);
}*/
free(data);

You could refer FFT sample to write.

Best regards,
Fiona

0 Kudos
Semen_K_
Beginner
388 Views

Dear Fiona,

Thanks for your reply. I have tried it, and it works, but I need to do my computations in parallel, so, I need to each thread has it's own data. I got it that I have to pass array (not multidim array) to the function, but I need to find the way to parallelize it. For instance,

for (int i=0; i<numthreads; i++)
       DftiComputeForward (FFT, x);

Now, with one pointer, if I do like &x[i*N/numthreads*N] it is not working.

Thanks,
Sem

0 Kudos
Zhen_Z_Intel
Employee
388 Views

Hi Sem,

Please refer this link of sample for parallel computing with one common descriptor/multiple descriptor. Thanks.

Best regards,
Fiona

0 Kudos
Reply