在CUDA FFT函数调用期间出错ERROR“code = 2(CUFFT_ALLOC_FAILED)”

时间:2013-09-25 07:05:40

标签: c cuda gpu gpgpu kingsoft

我正在编写一个程序,用于使用CUDA处理GPS信号。所以我需要使用fft,但是发生了错误。

 CUDA error at F:/clouddrive kingsoft/acc/accfinal/accfinal/acc.cu:341 code=2(CUF
FT_ALLOC_FAILED) "cufftPlan1d(&plan, new_size, CUFFT_C2C, 1)"

,代码在这里。

 double fft_Ifft_Sum(Complex *h_signal,Complex *h_filter_kernel,double* list,bool firstEnter)
{
   double max=0;
    int new_size=samplesPerCode;
    int mem_size = sizeof(Complex) * new_size;

    // Allocate device memory for signal
    Complex *d_signal;
    checkCudaErrors(cudaMalloc((void **)&d_signal, mem_size));
    // Copy host memory to device
    checkCudaErrors(cudaMemcpy(d_signal, h_signal, mem_size,cudaMemcpyHostToDevice));

    // Allocate device memory for filter kernel
   if(firstEnter)
   { 
     checkCudaErrors(cudaFree(d_filter_kernel));
     checkCudaErrors(cudaMalloc((void **)&d_filter_kernel, mem_size));

     checkCudaErrors(cudaMemcpy(d_filter_kernel, h_filter_kernel, mem_size,
                               cudaMemcpyHostToDevice));
   }

    // CUFFT plan
    cufftHandle plan;
    checkCudaErrors(cufftPlan1d(&plan, new_size, CUFFT_C2C, 1));

    // Transform signal and kernel

    checkCudaErrors(cufftExecC2C(plan, (cufftComplex *)d_signal, (cufftComplex *)d_signal, CUFFT_FORWARD));

     ComplexPointwiseMulAndScale_p<<<1024, 1024>>>(d_signal, d_filter_kernel, new_size, 1.0f / new_size);   
// Check if kernel execution generated and error
    getLastCudaError("Kernel execution failed [ ComplexPointwiseMulAndScale ]");

    // Transform signal back
    //printf("Transforming signal back cufftExecC2C\n");
    checkCudaErrors(cufftExecC2C(plan, (cufftComplex *)d_signal, (cufftComplex *)d_signal, CUFFT_INVERSE));
    cudaThreadSynchronize();

    // Copy device memory to host
     Complex *h_convolved_signal = h_signal;
    checkCudaErrors(cudaMemcpy(h_convolved_signal, d_signal, mem_size,

                      cudaMemcpyDeviceToHost));
    // list=new double[new_size];
    for(int i=0;i<new_size;i++)
    {
        list[i]=h_convolved_signal[i].x*h_convolved_signal[i].x+h_convolved_signal[i].y*h_convolved_signal[i].y;
        if(list[i]>max)max=list[i];
    }


   return max;



    //Destroy CUFFT context
    checkCudaErrors(cufftDestroy(plan));

    // cleanup memory
    free(h_signal);
    free(h_filter_kernel);


    checkCudaErrors(cudaFree(d_signal));
    checkCudaErrors(cudaFree(d_filter_kernel));
    cudaDeviceReset();
}

并且这个函数将在main中被调用大约1900次并且它可以平稳运行直到大约1440次,这里将发生错误。我无法找出原因。 感谢。

1 个答案:

答案 0 :(得分:1)

每次进入该功能时,您都会为d_signal分配设备内存,但永远不会释放它。在任何return或destroy操作之前,函数中都有free语句,所以如果你反复调用这个函数,这对我来说就像是个问题。

我认为编译器会根据你所显示的内容发出关于无法访问的代码的警告。