As a relatively new OpenCL user I was looking online for a guide to setting up OpenCL in visual studio. I have about 9 years development experience, so I thought this wouldn’t be much of an issue but I did have a little trouble due to a few details of the setup not being common knowledge. In this article I’m going to provide step by step instructions on setting up OpenCL for both nVidia and ATI graphics cards in Visual Studio 2010. I assume that you have an OpenCL capable graphics card, if you are unsure please check your manufacturers website or leave a comment and I’ll check for you. In this tutorial I will be using an nVidia card but I will provide text instructions for AMD cards.
1. Installing OpenCL
For nVidia graphics cards you need to install the CUDA toolkit and development drivers. As of the time of writing the current CUDA toolkit version was 5.0, this version includes the development drivers in the toolkit. It can be downloaded here:
For ATI graphics cards you will need to install the AMD APP SDK (formerly the AMD Stream SDK). This can be downloaded from:
2. Setting up the Project
First thing to do is set up an empty VS project by choosing
- ‘New Project->Visual C++->Win32 Console Application'
- Enter a name for the project and choose OK
- In the application creation wizard choose ‘Next’
- Under ‘Additional options’ check the ‘Empty project’ box and click ‘Finish’
3. Including OpenCL in the Project
The first step in including OpenCL is to create a C++ file, this enables the configuration options we will need.
- Right click on the ‘Source Files’ folder in the solution explorer and select ‘Add-> New Item’
- Select C++ File and give the file a name
- Click the ‘Add’ button in the bottom right hand corner of the dialog box
- Now we are ready to point the project to the include directories for OpenCL
- Right click on the project name in the solution explorer and select ‘Properties’
- From the ‘Configuration’ drop down box choose ‘All Configurations’
- Navigate to ‘Configuration Properties-> C/C++ -> General’
- In the ‘Additional Include Directories’ field add the following information depending on your graphics card
- nVidia -“$(CUDA_INC_PATH)”
- AMD -“$(AMDAPPSDKROOT)\include”
4. Linking OpenCL
Now we are going to tell the project where to find the actual library file which contains OpenCL. This is where the actual implementation of OpenCL is contained.
Without closing the dialog box used above:
- Choose ‘Linker-> General’
- In the ‘Additional Dependencies’ field enter the following, again this is based on your card vendor
- nVidia - “$(CUDA_LIB_PATH)”
- AMD - “$(AMDAPPSDKROOT)\lib\x86” (for 64 bit users you may need to change the x86 to x86_64, I have no machine to test this on)
- Still in the ‘Linker’ submenu, select ‘Input’
- In the ‘Additional Dependencies’ field click on the arrow that appears at the end of the field and choose Edit…
- In the dialog that appears enter “OpenCL.lib” (without the quotes obviously J)
- Click OK in the dialog box
- Click OK in the Properties dialog to bring you back to the main IDE
5. A Note for 64-Bit users
If you are using a 64-bit machine then you will need to add an x64 target to your project to be able to build it.
- Again open the properties page of your project by right clicking on it and selecting ‘Properties’
- Select ‘Configuration Manager…’ from the top right hand corner of the dialog (if it is greyed out expand ‘Configuration Properties’ on the left hand side to enable it)
- Under ‘Active solution platforms’ select New...
- Under ‘Type or select the new platform’ select x64 and click OK
- Click Close
- Click OK to get back to the main IDE
Your project should now be set up for OpenCL!! Wasn’t that a breeze ;)
6. Testing your project
Below is the simplest OpenCL application to test that the install and configuration have worked. Copy this code into the C++ class that you created at the start of the tutorial:
Click the run button. If you get a console window that opens and then closes again, then congratulations!!! You have just taken the first step on your OpenCL/world domination journey. Happy coding guys! If I have made any errors please leave me a comment and I will correct it ASAP.