I need to compile a cuda .cu file using nvcc from command line. The file is "vectorAdd_kernel.cu" and contains the following piece of code:
extern "C" __global__ void VecAdd_kernel(const float* A, const float* B, float* C, int N)
{
int i = blockDim.x * blockIdx.x + threadIdx.x;
if (i < N)
C[i] = A[i] + B[i];
}
I used the following command (I need to get a .cubin file):
nvcc --cubin --use-local-env --cl-version 2010 -keep -I "C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include" vectorAdd_kernel.cu
The compiler creates files vectorAdd_kernel.cpp4.ii and vectorAdd_kernel.cpp1.ii then it stops with the following output:
C:\Users\Massimo\Desktop\Pluto>nvcc --cubin --use-local-env --cl-version 2010 vectorAdd_kernel.cu -keep -I "C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include"
vectorAdd_kernel.cu
vectorAdd_kernel.cu
c:\program files (x86)\microsoft visual studio 10.0\vc\include\codeanalysis\sourceannotations.h(29): error: invalid redeclaration of type name "size_t"
C:/Program Files (x86)/Microsoft Visual Studio 10.0/VC/include\new(51): error: first parameter of allocation function must be of type## Heading ## "size_t"
C:/Program Files (x86)/Microsoft Visual Studio 10.0/VC/include\new(55): error: first parameter of allocation function must be of type "size_t"
Can you please help me in solving this issue?
nvcc The main wrapper for the NVIDIA CUDA Compiler suite. Used to compile and link both host and gpu code. cuobjdump The NVIDIA CUDA equivalent to the Linux objdump tool.
NVCC is a compiler driver which works by invoking all the necessary tools and compilers like cudacc, g++, cl, etc. NVCC can output either C code (CPU Code) that must then be compiled with the rest of the application using another tool or PTX or object code directly.
nvcc is the compiler driver used to compile both . cu and . cpp files. It uses the cl.exe (on Windows) or gcc (on Linux) executable that it can find as the compiler for host code.
In order to compile CUDA code files, you have to use nvcc compiler. Cuda codes can only be compiled and executed on node that have a GPU. Heracles has 4 Nvidia Tesla P100 GPUs on node18. Cuda Compiler is installed on node 18, so you need ssh to compile cuda programs.
I just encountered this in Visual Studio 2017 and Cuda v9.0 trying to compile from the command line with nvcc
. After a lengthy session I realised my Visual Studio Command line tools were setup to use cl.exe
from the x86
director instead of the x64
. There are a number of ways to resolve it, one way is to override the directory it looks for its compiler tools with - for example as in :
nvcc -ccbin "C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\VC\Tools\MSVC\14.11.25503\bin\HostX86\x64" -o add_cuda add_cuda.cu
It then worked fine.
I'll also mention that I used the which.exe
utility from the git tools to figure out what version of cl.exe
it was accessing, but the where
command - native to windows - works as well.
Another way - probably a better way - to handle this is to just set the Visual Studio environment variables correctly to 64 bits like this for the Enterprise edition:
"C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\VC\Auxiliary\Build\vcvarsall.bat" x64
For the Community edition substitute "Community" for "Enterprise" in the path.
You can also select the toolset with (for example) --vcvars_ver=14.0
which selects the 14.0 toolset, necessary to compile CUDA 9.1 with the 15.5 version of Visual Studio.
Then you can build simply with this:
nvcc -o add_cuda add_cuda.cu
VS Community 2019:
Open a x64 Native Tools Command Prompt for VS 2019
**********************************************************************
** Visual Studio 2019 Developer Command Prompt v16.3.6
** Copyright (c) 2019 Microsoft Corporation
**********************************************************************
[vcvarsall.bat] Environment initialized for: 'x64'
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community>cd c:\Users\AFP\Downloads\cuda_by_example\chapter03
c:\Users\AFP\Downloads\cuda_by_example\chapter03>nvcc hello_world.cu
hello_world.cu
Creating library a.lib and object a.exp
c:\Users\AFP\Downloads\cuda_by_example\chapter03>
If environment is not initialized for x64
and you have opened x86 Native Tools Command Prompt for VS 2019, run:
**********************************************************************
** Visual Studio 2019 Developer Command Prompt v16.3.6
** Copyright (c) 2019 Microsoft Corporation
**********************************************************************
[vcvarsall.bat] Environment initialized for: 'x86'
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community>cd c:\Users\AFP\Downloads\cuda_by_example\chapter03
c:\Users\AFP\Downloads\cuda_by_example\chapter03>nvcc hello_world.cu
hello_world.cu
YOU GET LOT OF ERRORS ....
75 errors detected in the compilation of "C:/Users/AFP/AppData/Local/Temp/tmpxft_00004504_00000000-12_hello_world.cpp1.ii".
c:\Users\AFP\Downloads\cuda_by_example\chapter03>"c:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Auxiliary\Build\vcvarsall.bat" x64
**********************************************************************
** Visual Studio 2019 Developer Command Prompt v16.3.6
** Copyright (c) 2019 Microsoft Corporation
**********************************************************************
[vcvarsall.bat] Environment initialized for: 'x64'
c:\Users\AFP\Downloads\cuda_by_example\chapter03>nvcc hello_world.cu
hello_world.cu
Creating library a.lib and object a.exp
c:\Users\AFP\Downloads\cuda_by_example\chapter03>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With