Using CUDA 8 with Visual Studio Express 2015

Somehow I’ve never had any real exposure to CUDA. I’d quite like to experiment with CUDA a little and so would like to get my local environment setup to build CUDA programs.

There is an official CUDA installation guide here. On reading that I’m immediately concerned that support for the free versions of Visual Studio seems a little limited. I have Visual Studio 2015 Express Installed, which I use more as a text editor than IDE, though I occasionally use the C++ debugger, but the installer claims support for Visual Studio Community 2015 and above only, which I don’t have!

I did spend some time searching for Visual Studio Community 2015 but Microsoft make it very hard to discover and install old versions of Visual Studio. All the links on Google and MSDN point to 2017 which has no CUDA support at all at this time, and when you do finally get through to a page for downloading old versions it’s implied you need an MSDN subscription. I’m just going to go ahead and assume I can make it work. I only need the IDE for editing the source anyway. I’m happy to build and run the apps from the command line if I have to, and that approach probably results in more portable code anyway, for example if I ever want to push code into the cloud.

Next I install the toolkit. The installer makes changing the installation folders very difficult. There is no copy/paste for the default folders it displays, then the path selection dialog doesn’t allow you to type a path, and doesn’t allow you to create folders so you then have to go into Windows Explorer, create the full paths to the target folders, and go back to the installer and browse to them. Or you can just accept a big lost of space on your SSD!

During installation I am told that no valid version of Visual Studio was installed and forced to confirm I was OK with this. Again I assume I’m going to make it work somehow right?!

Once installed we need a program to build. The following seems to be a fairly standard definition of a CUDA hello world app, perfect for testing that I can compile a source file. I stick this code into a file called main.cu.

#include <stdio.h>
#include <stdint.h>

__global__ void kernel(void)
{
	printf("Hello, world, from the GPU\n");
}

int main(void)
{
	printf("Hello, world, from the CPU\n");
	kernel << <1, 1 >> >();
	cudaDeviceSynchronize();
	return EXIT_SUCCESS;
}

Now from the Windows Command Line I run the following to setup the Visual Studio environment. Note that my installation is on D rather than C here.

call "D:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\vcvarsall.bat" x86_amd64 

After a bit of experimentation I found that the following command line should compile the source, though as shown here it just gives me an error message relating to vcvars64.bat,

nvcc main.cu -o main.exe 
  --gpu-architecture=compute_50 
  --gpu-code=sm_50,sm_52 
  --machine 64 
  --cl-version 2015 
  -ccbin "D:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\bin\x86_amd64"

nvcc fatal : Microsoft Visual Studio configuration file 'vcvars64.bat' could not be found for
 installation at 'D:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/BIN/x86_amd64/../../..'

I’m going to assume the vcvars64.bat issue is some difference between the Express and standard versions of Visual Studio. Whatever the cause a bit of googling told me I should just create the file, which I do, putting the new file in “D:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\bin\amd64”, and containing the string “CALL setenv /x64”. After doing that the above command line completes without error, printing the following as it does so.

main.cu
   Creating library main.lib and object main.exp

As well as the lib and exp file it also made an exe! When I run that exe from the command line I get the expected “Hello World” response!

Leave a Reply

Your email address will not be published. Required fields are marked *