Skip to content

Instantly share code, notes, and snippets.

Last active Feb 16, 2019
What would you like to do?


Cosmos is an OpenCL accelerated n-body simulator. It can be used to render n-body simulations at a high speed.


Using Cosmos is a two step process. First, you must output binary data of the results of a n-body simulation. Second, you render this to a high-resolution image sequence. Cosmos provides two tools for these purposes, cosmos_simulate and cosmos_render.

Unfortunately, the Cosmos toolchain does not provide an easy way to tweak initial conditions or parameters. However, it does come with a great set of pre-defined conditions and parameters. If you want to modify these, you will have to edit the source code before compiling. If you want to find places where you can edit parameters, search for the text PARAM in all source files.


To compile, use the command

clang++ cosmos_simulate.cpp -o cosmos_simulate -std=c++11 -lOpenCL

To run, use the command

./cosmos_simulate <frames>

This will cause cosmos_simulate to output a bunch of frames to the directory that it was started in. These frames will be binary files, copied directly from OpenCL output.


To compile, use the command

clang++ cosmos_render.cpp -o cosmos_render -std=c++11 -lOpenCL

To run, use the command

./cosmos_render <frames>

This will cause cosmos_render to output a bunch of rendered frames to the directory that it was started in. These rendered frames will be image files, produced by rendering the corresponding simulation frames generated by running cosmos_simulate.

Even more speed

To speed up simulations and renders even more, compile with

clang++ cosmos_tool.cpp -o cosmos_tool -std=c++11 -lOpenCL -Ofast -march=native

Credits and licensing

Cosmos is licensed under the MIT License. Thanks to Sean Barrett for creating stb_image_write. Thanks to the Khronos Group for creating OpenCL, which in my experience was a very nice API to work with. Thanks to Stéfan van der Walt and Nathaniel Smith for creating the palette used in thermal_colormap.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment