the_naive
the_naive

Reputation: 3064

Cuda error undefined reference to 'cufftPlan1d'?

I'm trying to check how to work with CUFFT and my code is the following

#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;
}

And the MAKEFILE consists of the following:

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

But I get compilation errors, the errors are as shown in the image:enter image description here

Apparently the problem is occurring only when I call the functions cufftPlan1d and cufftExecC2C. Do I have to add anything extra in the makefile to make use of these functions? My CUDA version 5.5 and I'm doing it in Ubuntu.

Thanks

Upvotes: 3

Views: 4859

Answers (1)

talonmies
talonmies

Reputation: 72349

There are two problems here

  1. The CUFFT library is not being linked. Change the compilation command to:

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

  2. Set LD_LIBRARY_PATH to include the absolute path to the CUFFT library to allow runtime loading of the shared library. The syntax for this can be found here.

[This answer has been assembled from comments and added as a community wiki entry to get this question off the unanswered queue for the CUDA tag]

Upvotes: 6

Related Questions