Mixed Languages: The Right Tool for The Right Job

Tuesday, June 16, 2009 – 11:23 PM

NBody.Viewer, click for full size image.The N-body modeling code and visualization I’ve been working on finally got off the ground in some sort of reasonable form last night. Here you can see it running with 5000 stars at over 30 frames a second.

It’s been working from the start (agile/TDD isn’t something you just write about) but I hit a serious performance bottleneck with WPF’s Viewport3D class. Viewport3D is slow and only supports triangle rendering which makes it next to useless, <DELETE>. Make way for a native Direct 3D surface and better performance and improved graphical choices.

Some fun facts:

  • The UI is built with Prism 1.0 and uses Unity for its DI container and EntLib 4.1 to provide validation and logging.
  • The physics calculations are implemented in a variety of languages including C#, C++ and CUDA.
  • The parallel implementations of the physics calculations use Task Parallel Library (TPL) and OpenMP.
  • To get reasonable rendering performance the UI uses a Direct 3D surface within a Prism WPF module.

It’s roughly 6k lines of C# and 1k of C++/CLI with a further 3.5k lines of C# unit/acceptance test code (roughly 350 tests).

I’m not close to finished; the UI needs more work as to the initialization codes and there’s more work to do on optimizing the integration algorithms. I’m even thinking of using F# to implement an Octree and further parallelize the core engine using MPI.

The thing I really learnt from this is mixed language programming really isn’t that hard. Writing a shim between C# and managed to unmanaged C++/CLI isn’t that tricky and once you have it figured out the first time it’s pretty simple to reuse the same pattern, especially if you can limit yourself to use blittable types. This isn’t a new approach. Piet Hut and Jun Makino’s The Art of Computational Science uses a similar tactic but with Ruby and C.

Sorry, comments for this entry are closed at this time.