gpt4 book ai didi

c++ - Cuda 错误未定义对 'cufftPlan1d' 的引用?

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:20:34 25 4
gpt4 key购买 nike

我正在尝试检查如何使用 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;
}

MAKEFILE 包含以下内容:

main: main.cu Makefile nvcc -o main main.cu --ptxas-options=-v --use_fast_math

但我得到编译错误,错误如图所示:enter image description here

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

谢谢

最佳答案

这里有两个问题

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

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

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

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

关于c++ - Cuda 错误未定义对 'cufftPlan1d' 的引用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22426214/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com