Goal: To go from nothing to running “Hello World” on nVidia graphics cards using CUDA without any prior experience in programming.
Motivation: I’m trying to learn how to get started with GPGPU using CUDA, so I figured I’d go ahead and write up a little tutorial while I figure things out for myself. I’m hoping to learn how to accelerate regular programs by using my graphics card.
I’m not sure if this works on both the Professional and Express versions, perhaps someone can give it a go and let me know.
First steps, you’ll need to download a few things for CUDA programming:
- Developers versions of the graphics card drivers: These are special drivers that allow you to access the hardware in the appropriate way.
- The CUDA toolkit: The compiler and some other things required for development.
- The CUDA SDK (Software development kit) Basically has a collection of examples and documentation.
You can download all of these things on the nvidia developer zone website.
So go ahead and install Visual Studio C++ 2010 and the three downloads mentioned above.
- Open Visual Studio C++ 2010 and start a new empty project from “File” -> “New” -> “Project”, just name it “HelloWorld” for now.
- Next, right click on your project’s name in the solution explorer and go to “properties”.
- Under the “General” tab, under the “Platform Toolset” tab, change the value to V100.
- Press “OK”.
- Right click on your project name again in the solution explorer and go to “Build Customizations”
- Go to “Configuration Properties” -> “Linker” -> “General” -> “Additional Library Directories” and click on the value and go to “Edit”
- Press “OK” and go back to the main screen.
- Add a “.cu” file to your project. Let’s just make a new “cpp” file under “source” named “HelloWorld.cu”
- You will now notice that if you right click on your project’s name in the solution explorer and go to “Properties”, you will see the “CUDA C/C++” tab under the “Configuration Properties” tab.
- Now to actually try and compile/run something.
- Go to: http://forums.nvidia.com/index.php?showtopic=90044 and grab a copy of bgalbraith’s version of “HelloWorld” that uses CUDA code.
- Cut and paste it into your HelloWorld.cu file.
- You’ll notice that Visual Studio doesn’t recognize the CUDA syntax. For example, we have red wavy lines under “__global__” and “cudaMalloc”.
- To fix this, go to “Tools” -> “Options”.
- Then “Projects and Solutions” -> “VC++ Project Settings”
- Now go back to the main screen by pressing “OK” and right click on your project’s name and go to “Properties”
- Go to the “Configuration Properties” tab and go to “VC++ Directories”
- Add the directory: “$(IncludePath);$(CUDA_INC_PATH)”
- Press “OK” to go back to the main menu.
- You will now need to locate a few files on your computer:
- Find the equivalent directory to: “C:\ProgramData\NVIDIA Corporation\NVIDIA GPU Computing SDK 4.0\C\doc\syntax_highlighting\visual_studio_8″ on your computer. (This directory is installed with the SDK)
- In this directory, you will find the file “usertype.dat”
- Copy that “usertype.dat” file and copy it into the same directory as the file devenv.exe
- On my system, that file is located in “C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE”
- But the “Program Files” directory might be named simply “Program Files” on a 32-bit version of windows.
- If you already made a “usertype.dat” file previously in that directory, just copy the contents of the file into the older one.
- Go back to your HelloWorld script in Visual Studio and include the following headers:
#include “cuda.h”
#include “cuda_runtime.h”
#include “device_launch_parameters.h”
- Now Visual Studio should be able to recognize the CUDA syntax. (If not, restart Visual Studio)
- Now you can right click on your “HelloWorld” file and click on “Compile” and it should successfully compile.
- You may want to add a pause in the execution so that you can actually see the output. (In case your command windows close once execution is completed.)
- Now try and compile a release build of the project.
- We have a hello world running using CUDA.
A little snag I ran into when trying to figure all this out was that I had my directories that pointed to my cuda.lib and cudart.lib files incorrectly identified. When I had that problem, I was getting an error like this when I compiled:
1>HelloWorld.cu.obj : error LNK2001: unresolved external symbol _cudaFree@4
1>HelloWorld.cu.obj : error LNK2001: unresolved external symbol _cudaConfigureCall@32
1>HelloWorld.cu.obj : error LNK2001: unresolved external symbol _cudaMemcpy@16
1>HelloWorld.cu.obj : error LNK2001: unresolved external symbol _cudaMalloc@8
1>HelloWorld.cu.obj : error LNK2001: unresolved external symbol _cudaSetupArgument@12
1>HelloWorld.cu.obj : error LNK2001: unresolved external symbol ___cudaRegisterFunction@40
1>HelloWorld.cu.obj : error LNK2001: unresolved external symbol ___cudaRegisterFatBinary@4
1>HelloWorld.cu.obj : error LNK2001: unresolved external symbol ___cudaUnregisterFatBinary@4
1>HelloWorld.cu.obj : error LNK2001: unresolved external symbol _cudaLaunch@4
- So if you get these, go back to the steps where we added the cuda.lib and cudrt.lib references, and also make sure you’ve added the directory as well above.
- Make sure that you’ve configured the libs and directories for the mode that you are running (debug vs release). Visual studio can treat them differently.
- Of course I might have skimmed over some important details and this may not be applicable to everyone’s system (and it may not be the best/most efficient way to do this), but I started out trying to learn for myself how to compile something using CUDA and I think I did it successfully. So anyone in the same situation as me might find this useful.
Useful References :
http://forums.nvidia.com/index.php?showtopic=90044
http://forums.nvidia.com/index.php?showtopic=184539
http://blog.cuvilib.com/2011/02/24/how-to-run-cuda-in-visual-studio-2010/
You may want to add a pause in the execution so that you can actually see the output. (In case your command windows close once execution is completed.)





























Pingback: apparently under construction » Visual Studio 2010 + CUDA 4.0 空プロジェクト