using nvcc to compile the CUDA code +PGI to compile the C


Can I use the PGI c/c++ compiler to compile the C code part of a program that contains CUDA code. In other words, can I use the PGI C/C++ compiler explicitly with nvcc?


Hi ghandurah,

I haven’t tried this myself, but you should be able to call the CUDA C host code just like any other C object. You may need to encapsulate the CUDA C function name with “extern C” so it doesn’t use C++ name mangling and linking might be a bit tricky to get all the correct libraries.

Let me try writing up an example.

  • Mat

Hi Mat,

Thanks for your reply. did you try an example? I can’t try now because I am waiting for the license.


Hi ghandurah,

nvcc is really a C++ compiler so to call nvcc host code you would follow the normal C call C++ process (See Chapter 12 of the PGI User’s Guide).

To compile code that includes CUDA header files, you would need to use the PGI C++ compiler (pgcpp). You wont be able to compile CUDA kernels, but should be able to call nvcc compiled code. Also, you would need to add the define flag “-D__GNUC__” (i.e. pretend you’re using g++) since the CUDA header files don’t recognize pgcpp. For example, we can compile the simpleCUBLAS found in the CUDA SDK.

simpleCUBLAS% pgcpp simpleCUBLAS.c -I/opt/cuda/include -D__GNUC__ -L/opt/cuda/lib64 -lcublas
simpleCUBLAS% setenv LD_LIBRARY_PATH /opt/cuda/lib64
simpleCUBLAS% a.out
simpleCUBLAS test running..

Press ENTER to exit...

Second example building the SDK Monte Carlo code.

MonteCarlo% pgcpp MonteCarlo.cpp -I/opt/cuda/include -I/opt/cuda-3.0-beta/sdk/C/common/inc -D__GNUC__ obj/x86_64/release/MonteCarlo*.o -L/opt/cuda/lib64 -lcudart -L/opt/cuda-3.0-beta/sdk/C/common/lib/linux -L/opt/cuda-2.3/lib64 -L../../lib -L../../common/lib/linux -L../../../shared//lib -lcudart     -L/opt/cuda-2.3/lib64 -L../../lib -L../../common/lib/linux -L../../../shared//lib -lcudart -lcutil_x86_64 -lshrutil_x86_64
MonteCarlo% a.out
Generating input data...
Allocating memory...
Generating normally distributed samples...
Running GPU Monte Carlo...
Options         : 256
Simulation paths: 262144
Time (ms.)      : 3.708000
GPU options per sec.: 69039.914730
GPU Monte Carlo vs. Black-Scholes statistics
L1 norm        : 3.064244E-06
Average reserve: 371.047315
CPU Monte Carlo vs. Black-Scholes statistics...
L1 norm: 2.970427E-06
Average reserve: 386.847174
CPU vs. GPU Monte Carlo statistics...
L1 norm: 9.597225E-08
Shutting down...

Press ENTER to exit...

Hope this helps,

Thanks a lot. I will try it and let you know what happened.