代码实现
主函数:
int N=2048;
float *in=(float *)malloc(sizeof(float)*N*N);
fftwf_complex *outfftw3=(fftwf_complex *)fftwf_malloc(sizeof(fftwf_complex)*N*(N/2+1));
MKL_Complex8 *outmkl=(MKL_Complex8 *)malloc(sizeof(MKL_Complex8)*N*(N/2+1));
Rand(in,N);
onemkl(in,outmkl,N);
fftw3(in,outfftw3,N);
match(outfftw3,outmkl,N);
各个定义的函数:
void Rand(float *in,int N)
{
VSLStreamStatePtr stream;
vslNewStream(&stream,VSL_BRNG_MT19937,1);
vsRngUniform(VSL_RNG_METHOD_UNIFORM_STD,stream,N*N,in,0.0f,1.0f);
}
void onemkl(float *in,MKL_Complex8 *out,int N)
{
int time;
MKL_LONG sizeN[2]={N,N};
MKL_LONG rs[3]={0,N,1};
MKL_LONG cs[3]={0,N/2+1,1};
DFTI_DESCRIPTOR_HANDLE handle=NULL;
DftiCreateDescriptor(&handle,DFTI_SINGLE,DFTI_REAL,2,sizeN);
DftiSetValue(handle,DFTI_PLACEMENT,DFTI_NOT_INPLACE);
DftiSetValue(handle,DFTI_CONJUGATE_EVEN_STORAGE,DFTI_COMPLEX_COMPLEX);
DftiSetValue(handle,DFTI_INPUT_STRIDES,rs);
DftiSetValue(handle,DFTI_OUTPUT_STRIDES,cs);
DftiCommitDescriptor(handle);
clock_t start,end;
start=clock();
DftiComputeForward(handle,in,out);
end=clock();
printf("1 oneMKL use %f\n",(double)(end-start)/CLOCKS_PER_SEC);
start=clock();
for(time=0;time<1000;time++) DftiComputeForward(handle,in,out);
end=clock();
printf("1000 oneMKL Average use %f\n",(double)(end-start)/CLOCKS_PER_SEC/1000);
}
void fftw3(float *in,fftwf_complex *out,int N)
{
int time;
fftw_plan flag;
clock_t start,end;
flag=fftwf_plan_dft_r2c_2d(N,N,in,out,FFTW_MEASURE);
start=clock();
fftwf_execute(flag);
end=clock();
printf("1 FFTW3 use %f\n",(double)(end-start)/CLOCKS_PER_SEC);
start=clock();
for(time=0;time<1000;time++) fftwf_execute(flag);
end=clock();
printf("1000 FFTW3 Average use %f\n",(double)(end-start)/CLOCKS_PER_SEC/1000);
}
void match(fftwf_complex *array_f,MKL_Complex8 *array_m,int N)
{
float diff=0.000001;
float R=0;
float I=0;
int a=0;
for(int i=0;i<(N/2+1)*N;i++)
{
R=fabs(array_m[i].real-array_f[i][0]);
I=fabs(array_m[i].imag-array_f[i][1]);
if(R>diff || I>diff) a=1;
}
if(a==0) printf("oneMKL with FFTW3 is Match\n");
else printf("oneMKL with FFTW3 is not Match\n");
}