Modelling ecological self-organization

Simulating spatial models using GPUs

Follow me on GitHub

From CUDA to OpenCL

With the help of Rohit Gupta and Quan-Xing Liu I have in the past years been implementing my models on the graphics processors (GPUs) that sit in many computers. These processors are much better suited to run spatially-explicit models than is the computer's main processor (CPU). I have been using NVidia's CUDA in the past, as I explained in the paper:

Johan van de Koppel, Rohit Gupta, and Cornelis Vuik. "Scaling-up spatially-explicit ecological models using graphics processors." Ecological Modelling 222.17 (2011): 3011-3019.

In the past year, I have been switching from CUDA to OpenCL. The reason is that OpenCL can more generally be used on many graphics cards, while CUDA is proprietary and works only on (expensive) NVidia cards.

On my GitHub website, I provide OpenCL codes for the models that I or my students have published in the past years.

Implementing the models

Running the codes requires an OpenCL-compatible graphics card and OpenCL drivers installed. On a Mac, this is natively done when Xcode has been installed, and the current code has been developed in Xcode using a Macbook pro 2012 edition. You can also compile and run the codes from the command line on a Mac. I run it using the cmake package ( that handles the generation of a suitable makefile. See further explanation below.

If you run under Windows with a computer that has an Nvidia card, you have to install CUDA (which has OpenCL integrated) including SDK and the OpenCL samples within Visual Studio, and integrate the code files within a Visual studio project. The simplest way to do that is to copy one of the code samples into a new folder within the samples folder, and use an existing sample project file by replacing the code files. I cannot help you with this as I am a Mac user (I actually switched to Mac to avoid the visual studio headache, so be warned).

For Mac or Linux users: Presuming that cmake is installed, you have to download the FindOpenCL.cmake file and put it two folders down from the current code (e.g., in a general, common folder to be used for all repository codes). If you don't want to put it in that location, than change the following line in each of the CMakeLists.txt files:


So that it points to the right folder. The FindOpenCL.cmake file can be downloaded from the "General instructions repository".

Then, unzip the file somewhere, and open a command window in the build folder within the unzipped code folder. Now, run:

$ mkdir build
$ cd build
$ cmake ..
$ make
$ make run

If you want to clear the build, type

make destroy

which cleans the build folder.

The code itself does not show anything except for a counter. To view the results, you have to use the R-project file Plot*.r (using R and RStudio I advice) or Plot*.m within Matlab. Note to set the folder settings correctly and to install the “fields” package within R (type install.package(“fields”) in R or RStudio)

Both scripts can make a movie. The R script is best for that, but you have to install the ffmpeg program, which you can find here:

And finally

I hope the code it useful for you. Please drop me an email if you like to tell whether it worked for you or not.