OpenCL (Open Computing Language) is a new framework for writing programs that execute in parallel on different compute devices (such as CPUs and GPUs) from different vendors (AMD, Intel, ATI, Nvidia etc.). The framework defines a language to write “kernels” in. These kernels are the functions which are to run on the different compute devices. In this post I explain how to get started with OpenCL and how to make a small OpenCL program that will compute the sum of two vectors in parallel.
1. Installing OpenCL for ATI graphic cards
You will need to install the AMD APP SDK (formerly the AMD Stream SDK). This can be downloaded from:
Select the apt version and 32/64 bit system. Download to hard disk and install it with the Express option. Once installed on to the hard disk, you can see “AMDAPPROOT” set in the environmental variables (Right click on My computer-> Properties -> Advanced System Settings ->Environmental Variables). If you can see the environmental variable set, then it implies SDK has been successfully installed.
2. Setting up a Visual Studio (VS) 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’
Including OpenCL in the Project
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 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
4. Linking OpenCL
Now we are going to locate 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 :
“$(AMDAPPSDKROOT)\lib\x86” (for 64 bit users you may need to change the x86 to x86_64)
– 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 :
– Click OK in the dialog box
– Click OK in the Properties dialog to bring you back to the main IDE
NOTE: If you are a 32-bit user then your project is now set up for OpenCL (go to the” Test Your Project” section), if you’re a 64-bit user then continue on to the next section.
5. Adding x64 target
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!!
6.Testing your project with OpenCL program – Vector Addition
Copy the code of opencl vector addition into the C++ class that you created at the start of the installation.
The source code for the same can be downloaded from : https://github.com/smistad/OpenCL-Getting-Started/
Click the run button. You will get a console window that will ask for dataset for summation of two vectors in parallel.
Enter the dataset and get the desired output of addition.