For a while I wanted to blog about the open source
implementation of the Parallel Extensions for Mono
that Jeremie Laval
has been working on. Jeremie is one of our mentored students
in the 2008 Google Summer of Code.

Dual CPU laptops are becoming the norm; quad-core computers
are now very affordable, and eight CPU machines are routinely
purchased as developer workstations.

The Parallel Extension API makes it easy to prepare your
software to run on multi processor machines by providing
constructs that take care of distributing the work to various
CPUs based on the computer load and the number of processors
available.

There are various pieces in the Parallel Extensions
framework, the simplest use case is Parallel.For, a
loop construct that would execute the code in as optimally as
possible given the number of processors available on the
system.

Parallel.For is a simple replacement, you usually
replace for loops that look like this:

for (int i = 0; i < N; i++)
BODY ();

With the following (this is using lambda expressions):

Parallel.For (0, N, i => BODY);

The above would iterate from 0 to N calling the code in
BODY with the parameter scattered across various CPUs.

C# 3 and Parallel LINQ

Marek
Safar recently
announced that Mono C# compiler was now completely
compliant with the 3.0 language specification.

In his announcement he
used Luke
Hoban's
brutal ray tracer-in-one-LINQ statement program. This was a
hard test case for our C# compiler to pass, but we are finally
there. I
had blogged
about it in the past. Luke Hoban's
ray-tracer-in-one-linq-statement looks like this:

Jeremie
then modified
the above program to use the parallel extensions to LINQ.
He replaced Enumerable.Range with
ParallelEnumerable.Range and foreach with
the parallel ForAll method to take advantage of his
library.