Andy's observations as he continues to attempt to know all that is .NET...

Sunday, July 20, 2008

Parallel Boids

The last two weeks Ive found myself immersed in looking at the new Pfx CTP release.  I must say it has come on loads since the last drop in December.  Some of the issues I spoke about in a previous blog post have now been fixed, things like better algorithms for Parallel.For.  Perhaps some of the best addition is the set of concurrent data structures ( cds ). 

The main reason for digging into Pfx over the last two weeks was to write an article for the August edition of  Developmentor's "development's" monthly news letter, this article attempts to cover the highlights of the recent CTP, so after completing that part of it I had to find some fun use of Pfx, so I ported a developmentor favorite "Boids".  Anyone attending a Developmentor Esessntial .NET or Guerilla .NET will have probably encountered this app, but for those not in the know it is a simulation of flocking behaviour.

I took the initial code written by Jason Whittington and started tuning it for performance initially just using one core.  The main task was to reduce the number of loops, so instead of having two loops one after each other that iterate over the same set of data I simply combined them,  and the second optimisation was to replace foreach with a regular for loop.  After that I replaced the main outer for loop with Parallel.For , and viola a reasonable speed up...

I've run the code  on my dual core and on an eight way and in both cases observed a reasonable speed up...Not bad considering not a massive amount of effort.

 

image

You can download the code from here

No comments:

About Me

My photo
Im a freelance consultant for .NET based technology. My last real job, was at Cisco System were I was a lead architect for Cisco's identity solutions. I arrived at Cisco via aquisition and prior to that worked in small startups. The startup culture is what appeals to me, and thats why I finally left Cisco after seven years.....I now filll my time through a combination of consultancy and teaching for Developmentor...and working on insane startups that nobody with an ounce of sense would look twice at...