5

I have the following code which I am trying to compile using nvcc.

Code:

#include <stdio.h>
#include <stdlib.h>
#include <cuda.h>
#include <curand.h>

int main(void)
{
    size_t n = 100;
    size_t i;
    int *hostData;
    unsigned int *devData;
    hostData = (int *)calloc(n, sizeof(int));
    curandGenerator_t gen;
    curandCreateGenerator(&gen, CURAND_RNG_PSEUDO_MRG32K3A);
    curandSetPseudoRandomGeneratorSeed(gen, 12345);
    cudaMalloc((void **)&devData, n * sizeof(int));
    curandGenerate(gen, devData, n);
    cudaMemcpy(hostData, devData, n * sizeof(int), cudaMemcpyDeviceToHost);
    for(i = 0; i < n; i++)
    {
        printf("%d ", hostData[i]);
    }
    printf("\n");
    curandDestroyGenerator (gen);
    cudaFree ( devData );
    free ( hostData );
    return 0;
}

This is the output I receive:

$ nvcc -o RNG7 RNG7.cu
/tmp/tmpxft_00005da4_00000000-13_RNG7.o: In function `main':
tmpxft_00005da4_00000000-1_RNG7.cudafe1.cpp:(.text+0x6c): undefined reference to `curandCreateGenerator'
tmpxft_00005da4_00000000-1_RNG7.cudafe1.cpp:(.text+0x7a): undefined reference to `curandSetPseudoRandomGeneratorSeed'
tmpxft_00005da4_00000000-1_RNG7.cudafe1.cpp:(.text+0xa0): undefined reference to `curandGenerate'
tmpxft_00005da4_00000000-1_RNG7.cudafe1.cpp:(.text+0x107): undefined reference to `curandDestroyGenerator'
collect2: ld returned 1 exit status

My initial guess is that for some reason the CURAND Library is not properly installed or that it cannot find the curand.h header file.

Please let me know what I should look for or how to solve my problem.

Thanks!

Wilo Maldonado
  • 551
  • 2
  • 11
  • 22

2 Answers2

13

@Wilo Maldonado: just use a linker flag -lcurand and additionally -L/path/to/cuda/libs if you do not have it already

0

The problem is not the header file, otherwise you would have got a compile error. You have a linker error. You will need to tell your linker where to find the object or library file that contains those functions.

Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285
  • I am using CentOS Operating System and running the code in terminal. How would I go to tell the linker where to find the library file? – Wilo Maldonado Jul 31 '12 at 07:10
  • I'm not familiar with the nvcc toolchain specifically, but if it's designed to imitate gcc then you would add one or more `-l` options on the compiler command line to tell the linker to include those libraries. – Greg Hewgill Jul 31 '12 at 07:54
  • It's very similar to gcc, which libraries should I tell the linker to include? – Wilo Maldonado Aug 01 '12 at 00:22
  • Whatever library contains implementations of the functions you want to call. The documentation should tell you what you need, in the same way it tells you what header file(s) you need to include. – Greg Hewgill Aug 01 '12 at 00:28