In-depth analysis of cufft's batch usage (cufft1d c2c example code is as follows)

完成2k到1024k点cufft完整代码 
#include"iostream"
#include"cuda_runtime_api.h"
#include"device_launch_parameters.h"
#include"cufft.h"

#include "sys/time.h"


///time
double what_time_is_it_now() {
    
    
    struct timeval time;
    if (gettimeofday(&time, NULL)) {
    
    
        return 0;
    }
    return (double)time.tv_sec + (double)time.tv_usec * .000001;
}


int main()
{
    
    
    const int Nt =1024*1024;
   const int BATCH = 2;
    double start, end;
   //BATCH用于批量处理一批一维数据,当BATCH=2时
   //则将0-1024,1024-2048作为两个一维信号做FFT处理变换
    cufftComplex* host_in, * host_out, * device_in, * device_out;
    //主机内存申请及初始化--主机锁页内存

    cudaMallocHost((void**)&host_in, Nt * sizeof(cufftComplex));
    cudaMallocHost((void**)&host_out, Nt * sizeof(cufftComplex));

    for (int i = 0; i < Nt; i++)
    {
    
    
        host_in[i].x = i + 1;
        host_in[i].y = i *3;

    }
    //设备内存申请
    cudaMalloc((void**)&device_in, Nt * sizeof(cufftComplex));
    cudaMalloc((void**)&device_out, Nt * sizeof(cufftComplex));
    //数据传输--H2D
    cudaMemcpy(device_in, host_in, Nt * sizeof(cufftComplex), cudaMemcpyHostToDevice);

    //创建cufft句柄
    cufftHandle cufftForwrdHandle;
    int j;
    int k = 0;
    int m;

    cudaDeviceSynchronize();
    for (j = 2 * 1024; j <= 1024 * 1024; j *= 2) {
    
    

        cufftPlan1d(&cufftForwrdHandle,j, CUFFT_C2C, BATCH);
        //cufftPlan1d(&cufftInverseHandle, Nt, CUFFT_C2C, BATCH);


        if (j < 128 * 1024) {
    
    
           k = 5000;

        }
        else {
    
    
           k = 1000;
       }

       // start = what_time_is_it_now();

        for (m = 0; m < k; m++) {
    
    
            //执行fft正变换
            cufftExecC2C(cufftForwrdHandle, device_in, device_out, CUFFT_FORWARD);
        }
        cudaDeviceSynchronize();
        end = what_time_is_it_now();
        printf("%dk   ", j / 1024);
        printf("\t%f   ", (end - start));
        printf("\t%f   \n ", 1000 * (end - start) / k);
   }


    return 0;
}


Guess you like

Origin blog.csdn.net/weixin_45206081/article/details/130056488