hip c2c_fft

hip c2c_fft


// An highlighted block
#include"iostream"
#include"hip/hip_runtime_api.h"
//#include"device_launch_parameters.h"
#include"hipfft.h"
#include"stdio.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 = 1;
    double start, end;
   //BATCH用于批量处理一批一维数据,当BATCH=2时
   //则将0-1024,1024-2048作为两个一维信号做FFT处理变换
    hipfftComplex* host_in, * host_out, * device_in, * device_out;
    //主机内存申请及初始化--主机锁页内存

   hipHostMalloc((void**)&host_in, Nt * sizeof(hipfftComplex));
   hipHostMalloc((void**)&host_out, Nt * sizeof(hipfftComplex));

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

}

for(int i=0;i<8;i++){
    
    
//printf("%d + %d\n", host_in[i].x,host_in[i].y);
std::cout<<host_in[i].x<<"+";
std::cout<<host_in[i].y<<std::endl;
}
//设备内存申请
hipMalloc((void**)&device_in, Nt * sizeof(hipfftComplex));
hipMalloc((void**)&device_out, Nt * sizeof(hipfftComplex));
//数据传输--H2D
hipMemcpy(device_in, host_in, Nt * sizeof(hipfftComplex), hipMemcpyHostToDevice);

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



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

    hipfftPlan1d(&cufftForwrdHandle,8, HIPFFT_C2C, BATCH);
    //hipfftPlan1d(&cufftInverseHandle, Nt, HIPFFT_C2C, BATCH);


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

    }
    else {
    
    
       k = 1;
   }

    start = what_time_is_it_now();
    for (m = 0; m < k; m++) {
    
    
        //执行fft正变换
        hipfftExecC2C(cufftForwrdHandle, device_in, device_out, HIPFFT_FORWARD);
    }
    hipDeviceSynchronize();
    end = what_time_is_it_now();
    printf("%fk   ", j / 1024);
    printf("\t%f   ", (end - start));
    printf("\t%f   \n ", 1000 * (end - start) / k);
//}
hipMemcpy(host_out, device_out, Nt * sizeof(hipfftComplex), hipMemcpyDeviceToHost);

for(int i=0;i<8;i++){
    
    
printf("%f + %f\n", host_out[i].x,host_out[i].y);
}

for(int i=0;i<8;i++){
    
    
//printf("%d + %d\n", host_in[i].x,host_in[i].y);
std::cout<<host_out[i].x<<"+";
std::cout<<host_out[i].y<<std::endl;
}
return 0;
}

Guess you like

Origin blog.csdn.net/weixin_45206081/article/details/130064857
FFT