Byte Nibble: Joe's Random Musings

Beware of the Byte Nibblin' Mice!

Parallel Programming at Microsoft for .NET

As I have made clear in my some of my previous posts I’m very interested in concurrent and parallel programming. There are many problems in achieving good parallel code which I discussed in my post on Concurrency in the Games Industry.

Recently I have be scouring resources all over the internet and at work to see what technologies actually exist to help programmers write scalable parallel code for .NET. Over the last few years various teams at Microsoft have been working on technology for parallel code. I have picked three projects that really stood out and I thought I’d share them. I’ll keep the summaries brief as there are very good resources already in existence. You can find out more about the various teams and technologies for concurrent programming at Microsoft at The Parallel Computing Developer Centre.

If you’re looking for Native Libraries I would strong advise looking at the Parallel Patterns Library, you can find out more information at the blog Parallel Programming in Native Code.

 

Task Parallel Library (TPL) and Parallel LINQ (PLINQ)

Version 4.0 of the .NET Framework ships with several new pieces of technology, namely library improvements, to make parallel programming simpler. The two additions that have made me most excited are the Task Parallel Library (TPL) and Parallel LINQ (PLINQ). You can find out more about both libraries in the Parallel Programming in the .NET Framework docs on MSDN.

I have been playing with both technologies ever since I first got hold of the .NET Framework 4. It makes writing parallel code ridiculously easy and straightforward. I was able to convert a C# test game server which made heavy use of threads to use the new Task system very quickly. Overall I gained an average 15% performance improvement, which is certainly impressive. I don’t claim the server was particularly well written, but the simple task of making use of TPL and PLINQ made significant performance gains, time is always an issue for programmers and these libraries certainly help.
As I don’t work with .NET in my job, I haven’t had the chance to apply these technologies in a large real-world application but it certainly shows promise and once .NET 4.0 comes out of beta I will revisit the technology.

F#

F# is a functional programming language for the .NET Framework. It combines the succinct, expressive, and compositional style of functional programming with the runtime, libraries, interoperability, and object model of .NET.”

Functional programming is not something I have ever done in any great depth. Although F# is not designed specifically for concurrent programming, when I started my research several friends suggested that I look at functional languages for concurrent development. As I was looking for Microsoft technologies and I have heard so much about F# recently, it seemed like a good place to start.

After playing around with the Visual Studio 2010 Beta which includes F# and reading various tutorials and examples, I wrote a test game server based on my C# version. The code was certainly a lot more simplistic than an equivalent implementation in C# or C++.
Without any experience in functional programming the learning curve was definitely quite steep and I doubt I could implement a very large project in any great speed. Having discussed F# with a functional mad friend who is very excited about it, it definitely seems to offer some great advantages for functional programmers. Naturally the disadvantage of the F# technology is that programmers like me need to make a big investment to learn both the paradigm and the language. It’s certainly something I hope to be able to do though!

Axum

Axum is a language that builds upon the architecture of the Web and principles of isolation, actors, and message-passing to increase application safety, responsiveness, scalability, and developer productivity.”

Now for my personal favourite. Axum is an incubation project from the Parallel Computing Platform team, it’s a new language derived from C# which is designed to offer an easy and productive programming model for parallel development.

I stumbled across Axum when searching around a few weeks ago. I downloaded it and after reading the Axum Programmers Guide, I gave it a try. Using my C# test game server as a base, I implemented it in Axum.

The paradigm felt a little strange at first and there was certainly a learning curve. I was able to pick up Axum far more rapidly than I did F#, as someone who is used to writing concurrent code in C# and C++ the code felt logical and I was able to very easily understand the flow of the application.

For me, Axum offers the perfect balance between F# with a very steep learning curve and TPL with a very shallow one. I will be following the progress of the project very closely and hope that the team continue to develop it to a fully supported product.

Summary

I should point out that it is possible to use a combination of the above options and they are not mutually exclusive, there are also numerous other technologies that you can investigate.

If you’re looking to quickly parallelise an existing .NET application, TPL and PLINQ certainly looks like an attractive option. It’s very quick to convert existing threading logic, ultimately very cheap to implement and offers some significant performance and scalability gains.

As I mentioned in my Axum section, if you’re looking for a language which feels familiar but offers excellent concurrency functionality and has a reasonable learning curve, you should do some research and testing with Axum and you won’t regret it.

F# certainly seems to be the option which allows you to clearly express what you want to happen and when. My problem with F# is that without any significant experience with functional programming, I found the learning curve very steep. I believe that most programmers have experience in similar languages to me, and that could be barrier to this option.

Ultimately the correct decision will be different for every project and the key is to do the correct research.

That’s all folks.