Want to run my CUDA C programs from a command line ... what do I need?

I want to create my CUDA C programs using a plain old text editor and run the programs from a command line. I do not want to use Visual Studio. I want to download just the stuff that I need to compile and run my CUDA C programs. I see something called the “NVIDIA Driver”. Is that what I need? Will it give me all that I need to compile and run my CUDA C programs? What do I set my PATH variable to?

The CUDA toolchain requires a supported host compiler. On Windows, that is MSVC. However, there is no need to use the MSVS IDE. For example, I write CUDA code in Emacs, then compile it with nvcc from the command line and use a simple Makefile if needed.

So simply install the standard CUDA package, then ignore everything having to do with the IDE. As I recall, the CUDA installation already sets up the PATH as needed. In my environment I see the following as part of PATH:

Program Files/NVIDIA GPU Computing Toolkit/CUDA/v9.2/bin
Program Files/NVIDIA GPU Computing Toolkit/CUDA/v9.2/libnvvp

This is for CUDA 9.2; it is possible that other CUDA versions are installing into different directories. In my case the compiler driver nvcc lives here: Program Files/NVIDIA GPU Computing Toolkit/CUDA/v9.2/bin/nvcc

Thank you! That is very helpful. I am trying to run from a command line the hello world example on page 23 of the book titled CUDA by Example. Here is what I have in my run.bat file:

%comspec% /c C:“Program Files (x86)”“Microsoft Visual Studio”\2019\Community\VC\Auxiliary\Build\vcvarsall.bat x86

nvcc -o HelloWorld hello_world.c

I get a bunch of error messages.

Here is the hello world program:

#include <stdio.h>
#include “book-examples/common/book.h”

int main( void ) {
printf( “Hello, World!\n” );
return 0;
}

Below are the error messages. Any thoughts on what is going wrong?


** Visual Studio 2019 Developer Command Prompt v16.7.5

** Copyright © 2020 Microsoft Corporation


[vcvarsall.bat] Environment initialized for: ‘x86’

hello_world.c

common/book.h(21): error C2146: syntax error: missing ‘)’ before identifier ‘err’

common/book.h(21): error C2061: syntax error: identifier ‘err’

common/book.h(21): error C2059: syntax error: ‘;’

common/book.h(21): error C2059: syntax error: ‘,’

common/book.h(23): error C2059: syntax error: ‘)’

common/book.h(38): error C2143: syntax error: missing ‘{’ before ‘<’

common/book.h(38): error C2059: syntax error: ‘<’

common/book.h(48): error C2065: ‘EXIT_FAILURE’: undeclared identifier

common/book.h(57): error C2065: ‘EXIT_FAILURE’: undeclared identifier

common/book.h(67): error C2054: expected ‘(’ to follow ‘device

common/book.h(67): error C2085: ‘value’: not in formal parameter list

common/book.h(67): error C2143: syntax error: missing ‘;’ before ‘{’

common/book.h(80): error C2054: expected ‘(’ to follow ‘global

common/book.h(81): error C2085: ‘float_to_color’: not in formal parameter list

common/book.h(81): error C2143: syntax error: missing ‘;’ before ‘{’

common/book.h(104): error C2054: expected ‘(’ to follow ‘global

common/book.h(104): error C2143: syntax error: missing ‘)’ before ‘*’

common/book.h(104): error C2085: ‘float_to_color’: not in formal parameter list

common/book.h(104): error C2143: syntax error: missing ‘;’ before ‘*’

common/book.h(105): error C2059: syntax error: ‘)’

C:\Program Files (x86)\Windows Kits\10\include\10.0.18362.0\ucrt\corecrt_malloc.h(97): error C2040: ‘malloc’: ‘void *(size_t)’ differs in levels of indirection from ‘int ()’

C:\Program Files (x86)\Windows Kits\10\include\10.0.18362.0\ucrt\stdlib.h(52): error C2371: ‘exit’: redefinition; different basic types

common/book.h(182): error C2065: ‘true’: undeclared identifier

CUDA doesn’t support any 32-bit targets at this time, so you need to configure MSVC for 64 bit by passing amd64 (and not x86) to vcvarsall.bat. I am using an older version of MSVC, so double check the batch file to make sure that is still the proper argument for a 64-bit target.

I don’t have the file book.h available. It presumably contains CUDA code, not C code. CUDA is a language in the C++ family and uses .cu at its source file suffix. So try renaming into hello_world.cu so nvcc knows it is compiling a CUDA program.

For what it is worth, it is not clear to me whether MSVS 2019 Community Edition is a host compiler supported by current CUDA versions.

Thank you, again! I sure appreciate your help. I think that I am close to getting the Hello, World program running. As you recommended, in run.bat I passed in amd64:

%comspec% /c C:“Program Files (x86)”“Microsoft Visual Studio”\2017\Community\VC\Auxiliary\Build\vcvarsall.bat amd64

And I changed .c to .cu:

nvcc -o HelloWorld hello_world.cu

That yields this error message:

nvcc fatal : Cannot find compiler ‘cl.exe’ in PATH

There are multiple cl.exe files in the Visual Studio folder. I don’t know which is the right one, so I picked one and added it to the PATH environment variable:

C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.16.27023\bin\Hostx64\x64\cl.exe

Any thoughts on why I am getting the error message? Below is the output that I am getting.

[DEBUG:vcvarsall.bat] init with arguments ‘amd64’
[DEBUG:vcvarsall.bat] Command line parse completed with values:
[DEBUG:vcvarsall.bat] __VCVARSALL_TARGET_ARCH=‘x64’
[DEBUG:vcvarsall.bat] __VCVARSALL_HOST_ARCH=‘x64’
[DEBUG:vcvarsall.bat] __VCVARSALL_WINSDK=’’
[DEBUG:vcvarsall.bat] __VCVARSALL_STORE=’’
[DEBUG:vcvarsall.bat] __VCVARSALL_HELP=’’
[DEBUG:vcvarsall.bat] __VCVARSALL_PARSE_ERROR=‘0’
[DEBUG:VsDevCmd] Writing pre-initialization environment to C:\Users\costello\AppData\Local\Temp\dd_vsdevcmd15_preinit_env.log
[DEBUG:core\vsdevcmd_start] initializing with arguments ‘’
[DEBUG:core\parse_cmd.bat] initializaing with arguments ‘’
[DEBUG:VsDevCmd.bat] Found version “15.9.28”


** Visual Studio 2017 Developer Command Prompt v15.9.28
** Copyright © 2017 Microsoft Corporation


[DEBUG:VsDevCmd.bat] calling “core\dotnet.bat”
[DEBUG:core\dotnet.bat] init:COMPLETE
[DEBUG:VsDevCmd.bat] calling “core\msbuild.bat”
[DEBUG:core\msbuild.bat] initializing…
[DEBUG:core\msbuild.bat] init:COMPLETE
[DEBUG:VsDevCmd.bat] calling “core\winsdk.bat”
[DEBUG:winsdk.bat] initializing…
[DEBUG:core\winsdk.bat] init:COMPLETE
[DEBUG:VsDevCmd.bat] calling “ext\cmake.bat”
[DEBUG:ext\cmake.bat] init:COMPLETE
[DEBUG:VsDevCmd.bat] calling “ext\netfxsdk.bat”
[DEBUG:ext\netfxsdk.bat] init:COMPLETE
[DEBUG:VsDevCmd.bat] calling “ext\perf_tools.bat”
[DEBUG:ext\perf_tools.bat] init:COMPLETE
[DEBUG:VsDevCmd.bat] calling “ext\roslyn.bat”
[DEBUG:ext\roslyn.bat] init:COMPLETE
[DEBUG:VsDevCmd.bat] calling “ext\team_explorer.bat”
[DEBUG:ext\team_explorer.bat] init:COMPLETE
[DEBUG:VsDevCmd.bat] calling “ext\testwindow.bat”
[DEBUG:ext\testwindow.bat] init:COMPLETE
[DEBUG:VsDevCmd.bat] calling “ext\vcvars.bat”
[DEBUG:ext\vcvars.bat] init:COMPLETE
[DEBUG:core\vsdevcmd_end] initializing with arguments ‘’
[DEBUG:VsDevCmd] Writing post-execution environment to AppData\Local\Temp\dd_vsdevcmd15_env.log
[vcvarsall.bat] Environment initialized for: ‘x64’
nvcc fatal : Cannot find compiler ‘cl.exe’ in PATH

There are multiple cl.exe files in the Visual Studio folder. I don’t know which is the right one, so I picked one

I set up my MSVS many years ago (in 2012!) and couldn’t possibly remember the details. I can see that I use

Program Files (x86)\Microsoft Visual Studio 10.0\VC\vcvarsall.bat

and the 64-bit compiler binary I actually invoke resides here:

Program Files (x86)/Microsoft Visual Studio 10.0/VC/BIN/amd64/cl.exe

Microsoft may have changed their directory structure over the past eight years. Check the Microsoft documentation on what environment variables are needed to point the MSVC compiler. I think PATH, INCLUDE, LIB.