FreeBSD and OpenCL

Chris Benesch
5 min readApr 17, 2021

FreeBSD and OpenCL are the best of both worlds. FreeBSD is a light, capable, stable operating system. OpenCL opens up the world of parallel processing and ultra fast number crunching. Believe it or not the two can coexist and you can run OpenCL capable devices on FreeBSD, but it is a little different set up than the same with Linux where everything comes pre packaged.

To start with, I will assume you have a recently installed FreeBSD system that is up and running and that you know basic unix commands and package management tools. But first, a little explanation about how OpenCL works.

To start with, most video drivers on FreeBSD (and Linux) are a generic form. They do very well for basic graphics, and a lot even offer some degree of OpenGL implementation. They as Apple says, “Just work”. It is an easy path to install a working X server. However to get to the nuts and bolts of the graphics hardware and utilize OpenCL, you must have more intrusive access to the hardware, and that is where the kernel modules comes in. The system I will be demonstrating is FreeBSD 13.0, newly released, however the stable workhorse 12.2 should be similar. Once those kernel modules are in place, there are libraries which provide generic access using a system known as icd. These libraries are located in /usr/local/etc/OpenCL/vendors. There are four main classes of OpenCL implementations in FreeBSD (and universally it is almost the same). NVidia, Intel, PoCL, and Clover. Nvidia and Intel are self explanatory, PoCL allows you to use your CPU(s) as OpenCL devices so that you can write applications that implement OpenCL, but if the customer does not have suitable graphics hardware it will still run. Clover covers Radeon based cards and a variety of others, and is basically OpenGL on steroids. These libraries if able to be successfully loaded provide an interface to the the card to the OpenCL engine which consists of C library access functions and the LLVM engine to build “kernels” to run inside OpenCL. Often the version of LLVM is behind the system compiler, so you get an install of a different LLVM.

So, on the configuration. All of these libraries depend on Linux emulation, so before installing issue “kldload linux64”. You might want to also add that to /boot/loader.conf as well. The OpenCL drivers themselves shouldnt be loaded at boot time, but included in /etc/rc.conf after boot up in the kld_list variable.

The packages to install first (on 13.0): pkg install drm-fbsd13-kmod drm-kmod drm_info libdrm linux-c7-libdrm ocl-icd pocl clinfo

At this point it is probably best to reboot and let everything settle out, those packages bring a lot of dependencies with them. Once back in a root shell, install the vendor for your chipset. On mine I had a Radeon R9 series which is identified as a “Radeon 8800 Series”, which is enabled by pkg install clover . Now give the drivers a try. First try the newer version by kldload amdgpu . If that is successful, you should see a list of outputs and the display should switch to a higher resolution (assuming still text mode). If the card is older, you can try kldload radeonkms

Whichever one works, put that in /etc/rc.conf under kld_list so it will load at boot time. Now on to clinfo, which will show you what is working with OpenCL and what isnt. In my case the output looks like the following:

clinfo output showing Clover working

If all went well, you should see details about both the graphics card and CPU. But hey, a text display isnt any fun, is it? Let’s put some real work to it. A while ago I wrote a good little example program that uses OpenCL and also straight CPU calls for comparison creating the iconic Mandelbrot set. You will need to install qt5 to build it. pkg install qt5

The source is located at https://github.com/beneschtech/OpenCLMandelbrot and can be clones with git. Just go in and run qmake cudafractal.pro and then make. If all went okay, you should have an executable named cudafractal that you can run either locally on an X display or over ssh using the -X option if your host computer is running something capable of X commands, such as Linux or even Mac supports it (it did a few years ago, who knows now).

I have vnc server on mine and use the minimalist window manager twm on it. The buttons on top say what to use to compute with. This particular box has two Opteron 16 core CPUs giving a total of 32 cores, but they only run at 2.1 Ghz due to the heat they would generate. Another thing to notice is that it gets the GPU type and name for the right hand button. Here is single threaded:

Single thread CPU compute taking 1.97s

That took almost 2 seconds. I’m old enough to remember when that was an overnight process. Now, using all the CPU cores:

Multi thread creating image in 133ms

That brought us down to 133ms. Not too bad. But now what we came here to do, running on the Radeon GPU:

23ms using GPU cores

23ms. Almost another order of magnitude faster. So, assuming this all worked, you now can write OpenCL applications on FreeBSD and with a supported card, be able to take advantage of the GPU for number crunching. Installing POCL is important. OpenCL will “work” no matter what, but will scream when a video card is set up like above.

I hope this article was useful to people wanting to set up OpenCL on FreeBSD.

References:

--

--

Chris Benesch

Professional software engineer. Math enthusiast. Entrepreneur at heart. http://www.beneschtech.com