user1738687
user1738687

Reputation: 477

Problems when running nvcc from command line

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?

Upvotes: 18

Views: 10335

Answers (3)

shashashamti2008
shashashamti2008

Reputation: 2347

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>

Upvotes: 3

Mike Wise
Mike Wise

Reputation: 22847

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.

Update:

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

Upvotes: 21

Yevgen Nabokov
Yevgen Nabokov

Reputation: 19

I've had similar problem.

The code where build breaks in SourceAnnotations.h :

#ifdef  _WIN64
typedef unsigned __int64    size_t;
#else
typedef _W64 unsigned int   size_t;
#endif

I've added _WIN64 compiler symbol with this --compiler-options "-D _WIN64". My nvcc build string looked like this:

nvcc kernel.cu --cubin --compiler-options "-D _WIN64"

Upvotes: 1

Related Questions