Brainlings.com
  • Living Forever / Health
  • Treadmill Desk Diary
  • Making Money / Finance
  • Technology and Gadgets
  • Science!!!
  • Photography
  • Contact Us

“Hello World!” in CUDA

Nov02
2011
7 Comments Written by brains

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:

  1. Developers versions of the graphics card drivers: These are special drivers that allow you to access the hardware in the appropriate way.
  2. The CUDA toolkit: The compiler and some other things required for development.
  3. 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”

  • Check off the “CUDA 4.0(.targets, .props)”  value and press “OK”

  • Next, right click on your project name again and go to “Properties”

  • Go to “Configuration Properties” -> “Linker” -> “General” -> “Additional Library Directories” and click on the value and go to “Edit”

  • Add the directory: “$(CUDA_PATH)\lib\$(PlatformName)” and press “OK”

  • Then go to “Configuration Properties” -> “Linker” -> “Input” -> “Additional Dependencies”

  • Add “cuda.lib ” and “cudart.lib”

  • 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”
  • Then add the “.cu” extention in the “Extensions To Include” Field.

  • 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://www.ademiller.com/blogs/tech/2010/10/visual-studio-2010-adding-intellisense-support-for-cuda-c/

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.)

Posted in Technology and Gadgets
SHARE THIS Twitter Facebook Delicious StumbleUpon E-mail
← Caloric Restriction Diet
Rezound vs Nexus Prime →
  • Pingback: apparently under construction » Visual Studio 2010 + CUDA 4.0 空プロジェクト

  • Hesham Zeid

    Thanks Thanks Thanks ………….

    Really you are the best , you are really a King
    Thank u so much for this marvelous explanation
    It’s works I’m so so so happy really thank you :)   

  • Admin

    you are the best, great effort!!!!!!

  • http://www.facebook.com/profile.php?id=100003979310656 Don Ls

    Thank you so much. It was very help full and detailed. Best Posting I have ever found.

  • Junaid Aslam

    This is by far the best ever explanation of any tool i have seen in my life. And i desperately wanted to figure it out, but really think that it isn`t so easy to settle things for CUDA with VS2010 as told by people. In the End i would like to say THANKYOU Dear Admin who posted it.!

  • surya

    I have got the above errors-

    1>Hello_World_Man.cu.obj : error LNK2001: unresolved external symbol ___cudaUnregisterFatBinary@4

    1>Hello_World_Man.cu.obj : error LNK2001: unresolved external symbol _cudaLaunch@4

    1>Hello_World_Man.cu.obj : error LNK2001: unresolved external symbol _cudaSetupArgument@12

    1>Hello_World_Man.cu.obj : error LNK2001: unresolved external symbol ___cudaRegisterFunction@40

    1>Hello_World_Man.cu.obj : error LNK2001: unresolved external symbol ___cudaRegisterFatBinary@4

    1>Hello_World_Man.cu.obj : error LNK2001: unresolved external symbol _cudaFree@4

    1>Hello_World_Man.cu.obj : error LNK2001: unresolved external symbol _cudaConfigureCall@32

    1>Hello_World_Man.cu.obj : error LNK2001: unresolved external symbol _cudaMemcpy@16

    1>Hello_World_Man.cu.obj : error LNK2001: unresolved external symbol _cudaMalloc@8

    1>E:AcademicsPRACTICEHello_World_ManReleaseHello_World_Man.exe : fatal error LNK1120: 9 unresolved externals

    1>

    1>Build FAILED.

    Please help me, what needs to be done next

  • surya

    I have compiled the cuda hello world code. But I have a command window, which opens and displays nothing, and stays that way. Please help me where I am going wrong

Recent Posts

  • How to Prevent Mold Growth
  • Best Compact Camera for December 2012
  • Best Android Tablet (November-December 2012)
  • Samsung 830 256GB vs Crucial C300 128GB vs 3xRaid0 WD Caviar Blacks Benchmarks
  • Best Wireless Router, October 2012

Recent Comments

  • Dave Derderian on How to Setup AndEngine for Android Game Development
  • surya on “Hello World!” in CUDA
  • surya on “Hello World!” in CUDA
  • Junaid Aslam on “Hello World!” in CUDA
  • Ronaldihno on Best Wireless Router, October 2012

EvoLve theme by Theme4Press  •  Powered by WordPress Brainlings.com

Back to Top