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

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.

8 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: Usually people use extra files for that.

Does that answer your question in any way?

#7 by

This was super helpful, thanks! I was building xmrig-amd for a few desktops, but since I was doing it on my laptop (which has an nVidia GPU), I had to temporarily replace my system's nVidia OpenCL.dll (in system32) with the version from the AMD desktop I was building for to avoid "undefined reference to `clCreateCommandQueueWithProperties'" when linking. After that, your instructions worked perfectly! Thanks again!

#8 by

clCreateCommandQueueWithProperties() is an OpenCL 2.0 function and nVidia only supports OpenCL 1.1. That's why nVidias OpenCL.dll doesn't define those symbols. I'm actually quite surprised that you can just take AMDs DLL and use it on a system with nVidia hardware. Thanks for posting that!

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 -

Just to keep your skill sharp and my comments clean.