Dear All,
I have ran a cufft on the ubuntu platform, but some errors happened.
The detail code shown below:
cufft.cu
#include "cuda_runtime.h"
#include "device_launch_parameters.h"
#include "cufft.h"
#include <iostream>
#include <stdio.h>
#include "cufft.h"
#include <stdlib.h>
__global__ void MultiplyKernel(cufftComplex *data, cufftReal *data1,cufftComplex *data2, unsigned vectorSize) {
unsigned idx = blockIdx.x*blockDim.x+threadIdx.x;
if (idx < vectorSize){
data[idx].x = data2[idx].x*data1[idx];
data[idx].y = data2[idx].y*data1[idx];
}
}
//cufftPlan3d(&plan, NZ, NY, NX, CUFFT_R2C);
extern "C"
void ApplyKernel(float *ImageBuffer, float *KernelBuffer, cufftComplex *data2, unsigned int NX, unsigned int NY,unsigned int NZ)
{
cufftReal *data_dev, *Akernel;
cufftComplex *data_dev1, *data_dev2;
cudaMalloc((void **)&Akernel, NX * NY * NZ * sizeof(cufftReal));
cudaMalloc((void**)&data_dev, NX * NY * NZ * sizeof(cufftReal));
cudaMalloc((void**)&data_dev1, NX * NY * NZ * sizeof(cufftComplex));
cudaMalloc((void**)&data_dev2, NX * NY * NZ * sizeof(cufftComplex));
cudaMemset(data_dev, 0, NX * NY * NZ * sizeof(cufftReal));
cudaMemset(data_dev1, 0, NX * NY * NZ * sizeof(cufftComplex));
cudaMemset(data_dev2, 0, NX * NY * NZ * sizeof(cufftComplex));
//cufftComplex *resultFFT = (cufftComplex*)malloc(NX * NY * NZ * sizeof(cufftComplex));
//cufftReal *resultIFFT = (cufftReal*)malloc(NX * NY * NZ * sizeof(cufftReal));
cufftHandle plan;
cufftPlan3d(&plan, NZ, NY, NX, CUFFT_R2C);
cudaMemcpy(data_dev, ImageBuffer, NX * NY * NZ * sizeof(cufftReal), cudaMemcpyHostToDevice);
cufftExecR2C(plan, data_dev, data_dev1);
//Multiply kernel
cudaMemcpy(Akernel, KernelBuffer, NX * NY * NZ * sizeof(cufftReal), cudaMemcpyHostToDevice);
static const int BLOCK_SIZE = 256;
const int blockCount = (NX*NY*NZ+BLOCK_SIZE-1)/BLOCK_SIZE;
MultiplyKernel <<<blockCount, BLOCK_SIZE>>> (data_dev2, Akernel, data_dev1, NX*NY*NZ);
cudaMemcpy(data2, data_dev1, NX * NY * NZ * sizeof(cufftComplex), cudaMemcpyDeviceToHost);
}
main.cpp
#include <iostream>
#include <stdio.h>
#include "cufft.h"
#include <stdlib.h>
using std::cout;
using std::endl;
extern "C"
void ApplyKernel(float *ImageBuffer, float *KernelBuffer, cufftComplex *data2, unsigned int NX, unsigned int NY,unsigned int NZ);
int main()
{
unsigned int NX = 180;
unsigned int NY = 230;
unsigned int NZ = 190;
cufftReal *Image = (cufftReal*)malloc(NZ * NY * NX * sizeof(cufftReal));
cufftReal *Kernel = (cufftReal*)malloc(NZ * NY * NX * sizeof(cufftReal));
cufftComplex *resultFFT = (cufftComplex*)malloc(NZ * NY * NX * sizeof(cufftComplex));
for (int i = 0; i < NZ*NY * NX; i++)
{
Image[i] = float((rand() * rand()) % NX) / NX;
Kernel[i] = float((rand() * rand()) % NX) / NY;
}
for (int i = 0; i < 3 ; i++)
{
cout<<Image[i]<<endl;
cout<<Kernel[i]<<endl;
}
ApplyKernel(Image, Kernel, resultFFT, NX, NY, NZ);
return 0;
}
CMakeLists.txt
CMAKE_MINIMUM_REQUIRED(VERSION 2.8)
PROJECT(cufft)
INCLUDE(/usr/share/cmake-3.5/Modules/FindCUDA.cmake)
CUDA_ADD_EXECUTABLE(cufft main.cpp cufft.cu)
The errors showed below:
CMakeFiles/cufft.dir/add_generated_cufft.cu.o: In function ApplyKernel': cufft.cu:37: undefined reference to
cufftPlan3d’
cufft.cu:39: undefined reference to `cufftExecR2C’
collect2: error: ld returned 1 exit status
CMakeFiles/cufft.dir/build.make:290: recipe for target ‘cufft’ failed
make[2]: *** [cufft] Error 1
CMakeFiles/Makefile2:67: recipe for target ‘CMakeFiles/cufft.dir/all’ failed
make[1]: *** [CMakeFiles/cufft.dir/all] Error 2
Makefile:83: recipe for target ‘all’ failed
make: *** [all] Error 2
I don’t know how to solve them.