How to use CUDA to implement the Fourier transform on the “device” instead of “host”

hello,I am new to CUDA. And i met a problem, and i want to figure out how to use CUDA to implement the Fourier transform on the device side, I report an error "calling a host
function(“cufftPlan1d”) from a global function(“performFFT”) is not allowed"when I use the following code:
include <stdio.h>
include <cuda_runtime.h>
include <cufft.h>

global void performFFT(cufftComplex* input, int N) {
//
cufftHandle plan;
cufftResult result = cufftPlan1d(&plan, N, CUFFT_C2C, 1);
if (result != CUFFT_SUCCESS) {
printf(“cuFFT Plan creation failed.\n”);
return;
}

result = cufftExecC2C(plan, input, input, CUFFT_FORWARD);
if (result != CUFFT_SUCCESS) {
    printf("cuFFT execution failed.\n");
    return;
}

// 
cufftDestroy(plan);

}

int main() {
int N = 1024;
cufftComplex* data;
cudaMalloc((void**)&data, N * sizeof(cufftComplex));

// 
for (int i = 0; i < N; ++i) {
    data[i].x = static_cast<float>(i);
    data[i].y = 0.0f;
}

// 
performFFT<<<1, 1>>>(data, N);

// 
cudaDeviceSynchronize();

// 
cudaFree(data);

return 0;

}
Can you help me, thanks a lot!

That’s correct, you cannot use the ordinary CUFFT library directly in CUDA device code.

Instead the cufftDx library can do that. You can find various questions here already on the library forum about cufftDx usage.

1 Like

OK. Thanks, i will take a look at it!