Cuda 错误未定义对“cufftPlan1d”的引用?

the*_*ive 2 c++ linux cuda makefile

我正在尝试检查如何使用 CUFFT,我的代码如下

#include <iostream>


//For FFT
#include <cufft.h>

using namespace std;

typedef enum signaltype {REAL, COMPLEX} signal;


//Function to fill the buffer with random real values
void randomFill(cufftComplex *h_signal, int size, int flag) {

    // Real signal.
    if (flag == REAL) {
        for (int i = 0; i < size; i++) {
            h_signal[i].x = rand() / (float) RAND_MAX;
            h_signal[i].y = 0;
        }
    }
}

//Printing the random data in the buffer
void printData(cufftComplex *a, int size, char *msg) {

    if (strcmp(msg,"")==0) printf("\n");
    else printf("%s\n", msg);

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

// FFT a signal that's on the _DEVICE_.
// Doing FFT
void signalFFT(cufftComplex *d_signal, int signal_size)
{

    cufftHandle plan;
    if (cufftPlan1d(&plan, signal_size, CUFFT_C2C, 1) != CUFFT_SUCCESS)
    {
        printf("Failed to plan FFT\n");
        exit(0);
    }

    // Execute the plan.
    if (cufftExecC2C(plan, d_signal, d_signal, CUFFT_FORWARD) != CUFFT_SUCCESS)
    {
        printf ("Failed Executing FFT\n");
        exit(0);
    }

}

// Doing IFFT
void signalIFFT(cufftComplex *d_signal, int signal_size)
{
    cufftHandle plan;
    if (cufftPlan1d(&plan, signal_size, CUFFT_C2C, 1) != CUFFT_SUCCESS)
    {
        printf("Failed to plan IFFT\n");
        exit(0);
    }

    // Execute the plan
    if (cufftExecC2C(plan, d_signal, d_signal, CUFFT_INVERSE) != CUFFT_SUCCESS)
    {
        printf ("Failed Executing IFFT\n");
        exit(0);
    }

}

int main(int argc, char **argv)
{

    cudaDeviceSynchronize();

    //Declaring two complex type variables;
    cufftComplex *h_signal, *d_signal1;

    //Declaring the size variable
    int alloc_size;

    alloc_size = 16;

    //Allocating the memory for CPU version complex variable
    h_signal = (cufftComplex *) malloc(sizeof(cufftComplex) * alloc_size);

    //Allocating the memory for GPU version complex variable
    cudaMalloc(&d_signal1, sizeof(cufftComplex) * alloc_size);

    // Add random data to signal.
    randomFill(h_signal, alloc_size, REAL);
    printData(h_signal, alloc_size, "Random H1");

    // Copying the data the data to CUDA
    cudaMemcpy(d_signal1, h_signal, sizeof(cufftComplex) * alloc_size, cudaMemcpyHostToDevice);

    //Applying FFT
    signalFFT(d_signal1, alloc_size);

    //Doing IFFT
    signalIFFT(d_signal1, alloc_size);

    cudaMemcpy(h_signal, d_signal1, sizeof(cufftComplex) * alloc_size, cudaMemcpyDeviceToHost);

    printData(h_signal, alloc_size, "IFFT");
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

MAKEFILE 由以下内容组成:

main: main.cu Makefile nvcc -o main main.cu --ptxas-options=-v --use_fast_math
Run Code Online (Sandbox Code Playgroud)

但我收到编译错误,错误如图所示:在此输入图像描述

cufftPlan1d显然,只有当我调用函数和时才会出现问题cufftExecC2C。我是否需要在 makefile 中添加任何额外内容才能使用这些函数?我的 CUDA 版本是 5.5,我在 Ubuntu 中进行。

谢谢

tal*_*ies 6

这里有两个问题

  1. CUFFT 库未链接。将编译命令更改为:

    nvcc -o main main.cu --ptxas-options=-v --use_fast_math -lcufft

  2. 设置LD_LIBRARY_PATH为包含 CUFFT 库的绝对路径,以允许运行时加载共享库。可以在此处找到其语法。

[这个答案是根据评论汇总的,并添加为社区 wiki 条目,以便将此问题从 CUDA 标签的未回答队列中删除]