CUDA with Microsoft XNA


I’ve been looking at learning CUDA as a new interest since it first came out. However, it is only in the last week or so that I have bitten the bullet and written some of my own code. I decided to produce a version of the boid flocking algorithm, using Microsoft’s XNA Game Studio for the graphics. I had some previously developed code for XNA which I used as the base for my app. The .NET interop wasn’t too difficult once I started running parts of the C# code in unsafe mode to help with the managed/unmanaged transition.

I have tuned both the CUDA and C# code to manage 10000 boids running at 30 fps. My main problem with tuning the code now seems to be the CPU rather than the GPU. The CPU is 4.5 years old (don’t laugh!) whilst the GPU is a GTX 285. However I hope to have a Core i7 with a GTX 295 delivered next week.

There is a short video of my app on YouTube. Please take a look if you’re interested and let me know what you think.


Cool video! I’m very new to CUDA so sorry if this is a dumb question, but how did you combine the c/c++ of CUDA with the C# from XNA?
(Also if you know how to install CUDA on w7 x64 I’d really love to know.)

In order to get the CUDA code to work with XNA I had to compile it as a DLL file. This can then be called from .NET using Interop Services. I also used some unsafe C# code to call the CUDA code in order to avoid the overhead of marshalling the data.

To use XNA with CUDA, you have to install the 32 bit version of CUDA, even on a 64 bit machine. This entails altering a few environment variables after CUDA has been installed to get it to work properly.

BTW, I’ve added a few more videos on my channel.

Thanks I subscribed to your channel. I ended up installing 32 bit Windows 7 just to make my life easier, perhaps when I’m done learning CUDA I’ll try to combine it with XNA.