OpenCL on any Windows PC

So, you own a Windows pc, but maybe you have an old GPU? Or maybe you’re just rocking a laptop with integrated graphics, and so you think: “There’s no way I could create cool GPU applications with OpenCL”. Well, that’s where you’re wrong!

In fact, integrated graphics GPUs can be used for Open CL! Even your CPU can function as a GPU (albeit, maybe not the best GPU due to architectural constraints).

This is because Intel actually has an implementation of OpenCL for its architecture that can work with any third generation and above Core I CPU, and odds are that if you bought a pc within the last four years, you’ll have a compatible CPU!

Downloading Intel’s OpenCL implementation

So the first step in our tutorial is actually to go to intel’s website and download their OpenCL SDK, which comes complete with OpenCL’s 2.1 full profile implementation, and you can find the download links here. You will need to click register, and then download the actual SDK.

img1.png

After you’ve installed it, and if you’re running Microsoft’s Visual Studio 2015 (as of writing this guide, support for Visual Studio 2017 is still not available), you can now start a new OpenCL project and be ready to go, but this guide is to actually teach you how to use OpenCL on any Windows computer, with no specific software.

Setting up the OpenCL environment on an IDE

Now, I’m going to be using C++ to develop my OpenCL applications, but there are other options, like using Python, which we will explore in the future.

With this in mind, go ahead and download a C++ compiler. I’m using MinGW, which is a minimal GNU tools for windows compiler, and I’m using it with Codeblocks, a very simple IDE for C, C++ development on Windows, but in theory you should be able to use this guide with any compiler or IDE.

To be able to use OpenCL we are going to need to do two things:

  1. Tell the compiler to know where to find the headers for the OpenCL API
  2. Tell the compiler where the OpenCL library.

For the first step, if you are using an IDE, you should go to your build options and look for the Search Directories (or equivalent as in the screenshot) options and add the Path to your OpenCL’s SDK installation.

That path should be under: C:\Program Files (x86)\Intel\OpenCL SDK\6.3\include if you’re using a 64-bit PC or C:\Program Files\Intel\OpenCL SDK\6.3\include if you are using a 32-bit PC.

This will enable the compiler to recognize the OpenCL API calls in our program.

Then, look for the “Liker Settings” option on your IDE and add the OpenCL library’s path under “Link Libraries” so that the program knows what to call in order to execute OpenCL statements.

The path should be: C:\Windows\System32\OpenCL.dll.

This screenshot will show the two options I am talking about:

im2

You can now build your application using OpenCL on your IDE!

Building OpenCL applications on the command line

If you’re not using an IDE though, then you need to link the library and the headers when compiling in the command line.

To link the headers, when compiling you must compile it using the “-I” flag, like so:


-I"C:\Program Files (x86)\Intel\OpenCL SDK\6.3\include"

And then to link the library you have to run MinGW using only the executable you created in the previous command and the path to the library, like so:


mingw32-g++.exe  -o bin\Debug\openCL.exe obj\Debug\main.o C:\Windows\System32\OpenCL.dll

Now that we know how to build the actual program, we can now write it. A very simple program would just count the number of platforms found, using:


clGetPlatformIDs( 0, NULL, &num_platforms )

This should return 1. Since OpenCL is designed to work in heterogeneous situations, device that will run an OpenCL executable needs to have a platform with it’s specific OpenCL inplementations to fit the device. This is generally a per-vendor plaform. Meaning that all Nvidia devices will share one platoform. And all Intel devices should share a Platform.

So, since we’ve only installed Intel’s OpenCL implementations we should only see one platform. This platform can then contain many devices like CPU’s and integrated GPU’s, but they may share a platform, or not depending on the vendor’s implementations. Generally if the devices share a vendor and and the same ISA, they will have the same platform.

In this particular case though, the return will be 2, since there is an extra “Experimental platform”, but for actuall use, the CPU and GPU both share the “INTEL (R) OPENCL” platfofrm.

The code I’m using for this experiment is:


#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <cl/cl.h>

int main(){

cl_uint num_platforms = 0;
clGetPlatformIDs(0,NULL,&num_platforms);

printf("\n\nDetected %d OpenCL platform(s) \n", num_platforms);

}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s