Welcome to Arkanis Development

Minimal OpenCL development on Windows


I've been doing some Windows OpenCL stuff recently. One of the things that kind of annoyed me a lot was all the time spend to set things up. Installing Visual Studio (which takes quite some time), finding the proper SDK and praying that everything kind of works. Not realy my kind of fun. And all I wanted to do was to create a program that uses the systems opencl.dll.

So how hard can that be? What do I really need for that? Common sense linking would suggest you only need the header files. With that the compiler knows how to generate code to call the DLL functions. So I gave it a try and it actually worked! (Which honestly surprises me most of the time.)

  • I downloaded MinGW-w64 as standalone compiler. You can use the installer but a 50 MiB archive with a small .bat or .cmd script to add it's bin directory to the %PATH% is just my type of thing. No external dependencies or funny setup stuff. Extract and go. As long as Microsoft doesn't break backward compatibility (unlikely) it will continue to work out of the box. No trouble if I want to pick it up again later on.
  • All I really wanted from the OpenCL SDKs were the OpenCL header files. Well, you can get those directly from the Kronos Groups website. I actually only needed cl.h and cl_platform.h. The others (e.g. cl_egl.h) are optional addons and are not included by cl_platform.h.
  • The OpenCL runtime (opencl.dll) is provided by recent AMD or nVidia drivers. No need for any SDK.

All that remains is to add the OpenCL headers to the compilers include path, compile an OpenCL program and link it against the systems opencl.dll:

gcc -I. main.c C:\Windows\System32\OpenCL.dll -o main.exe

I've put the OpenCL header files into an cl subdirectory. Then added the current directory to the include path. So the compiler will find #include <cl/cl.h>.

And that's it! MinGW-w64, two header files and a recent graphics driver is all you need. :)

I've tested this with OpenCL 1.1 on nVidia (one notebook) and AMD drivers (two different computers). There's so much interesting stuff in OpenCL 1.1. alone (e.g. combining out-of-order GPU and CPU command queues) that I haven't tested newer versions yet.

If you're curious you can find the minimal setup on GitHub. It's a small OpenCL application that takes the first GPU and transforms a hand full of characters on the GPU. Total waste of any processing time but enough to see if it works.

6 comments for this post

leave a new one

#1 by
Joe Dude

Thanks for the post. I was on track to begin the massive amount of learning it would take to use OpenGL compute shaders to do GPGPU computing. That definitely wasn't optimal

I was under the impression that one of Intel, AMD, or NVIDIA's OpenCL SDK was needed. That actually means all 3 are needed if I want to set things up to be as portable as possible for other people.

Finally, I got OpenCL to compile with MinGW-w64 without an SDK, per your instructions. What problems lie ahead, I don't know. Thanks again.

#2 by

was hoping for IDE style compiler instructions :(

#3 by

It's meant to be the exact opposite. ;) Just the bare minimum needed. You could tell your IDE to pass those compiler flags to your built tools and it would do the job.

The IDE focused articles for OpenCL actually annoyed me quite a bit because I got the impression that using OpenCL itself is way more complicated than it actually is. Especially considering deployment which really is nothing more than just linking against a DLL.

#4 by

Thanks for the solution. I realised most of errors people are getting, are trying to run OpenCL is by including -lOpenCL in their gcc compilation, instead of directly linking OpenCL.dll as you did here.

#5 by

Hi! Thanks! Do you know how to run an OpenCL program via command line? If I just use main.exe then only the c code is ran.

#6 by

Well, I'm not really sure what you mean by running an OpenCL program via the command line. Usually OpenCL programs are just little code snippets that describe one specific processing step done on the GPU (e.g. calculating a sum of an array).

A useful computer program has to combine many such OpenCL programs in a command queue to solve real problems. And that's what the C code does: It tells the computer how all those OpenCL programs have to be glued together to actually work. It also allocates and manages the resources (memory, images, etc.) needed by the OpenCL programs.

So at least as far as I'm aware of it it doesn't make sense to run an OpenCL program by itself. It can't do anything by itself since it describes only a small processing step (and then even without the used resources) not a whole program.

In the minimal example the OpenCL program (or kernel) is embedded into the normal C program as a string: https://github.com/arkanis/minimal-opencl-on-windows/blob/master/main.c#L40 Usually people use extra files for that.

Does that answer your question in any way?

Leave a new comment

Having thoughts on your mind about this stuff here? Want to tell me and the rest of the world your opinion? Write and post it right here. Be sure to check out the format help (focus the large text field) and give the preview button a try.

Format help

Please us the following stuff to spice up your comment.

An empty line starts a new paragraph. ---- print "---- lines start/end code" ---- * List items start with a * or -