A C#/Cuda C project that simulates a simple galaxy
This project is a test of ManagedCuda and graphics interop to OpenTK to simulate a simple galaxy on the GPU.

This project was to test the ManagedCuda library. I also used OpenTK as the opengl/drawing/window library.

This is the general idea of how it works. On every OpenTK UpdateFrame, I run the "kernel" cuda method. This is the actual gravity simulation. On every DrawFrame, I copy the contents of the position array to an OpenGL arraybuffer, using cuda's graphics interop (only the x and y coordinates are transferred). I then call glDrawArrays on that array, using GL_POINTS (GL.DrawArrays and BeginMode.Points in the OpenTK api).

There are four constants in both the C# file and Cuda C that have a large effect on the simulation. GalaxyMass is the mass of the black hole in the middle of the galaxy (the position of this is fixed to 0,0,0). ParticleMass is the mass of each star. GravParticles is the ratio of stars that have nonzero mass (n / GravParticles = number of stars that have mass) (the reason for this is to speed up the simulation- if a star does not have mass, then the simulator does not have to calculate attraction to it, reducing the number of attractions to (n ^ 2) / GravParticles). Finally, the "constant" n is the number of stars. These numbers must be the same across the C# and Cuda C file, otherwise the initial velocity generation will be off.

A Cuda-capable graphics card and all three downloads here are needed to compile the project. Some path editing may be unnecessary too, as I use a Visual Studio post-compile nvcc call to compile the Cuda C file, and sometimes it is not set up properly to run. In addition, the OpenTK and ManagedCuda dlls are required.

Last edited Oct 7, 2012 at 4:40 PM by khyperia, version 2