LINQ to Silliness: Generating a Mandelbrot with parallel potential

I've been writing about LINQ recently, and in particular I've written a small amount about Parallel LINQ. (Don't get excited - it's only about a page, just to mention it as a sort of "meta-provider" for LINQ.) I was wondering what to use to demonstrate it - what general task can we perform which could take a lot of CPU?

Well, I used to be quite into fractals, and I've written Mandelbrot set generators in various languages. I hadn't done it in C# before now, however. Calculating the colour of each pixel is completely independent of all the other pixels - it's an "embarrassingly parallelizable" task. So, a great task for PLINQ. Here's the "normal LINQ" code:

 

var query = from row in Enumerable.Range(0, ImageHeight)
from col in Enumerable.Range(0, ImageWidth)
select ComputeMandelbrotIndex(row, col);

byte[] data = query.ToArray();

Changing this into a parallel query is really simple - although we do need to preserve the ordering of the results:

var query = from row in Enumerable.Range(0, ImageHeight).AsParallel(QueryOptions.PreserveOrdering)
from col in Enumerable.Range(0, ImageWidth)
select ComputeMandelbrotIndex(row, col);

byte[] data = query.ToArray();

Without being able to actually use PLINQ yet, I can't tell how awful the order preservation is - Joe warns that it's costly, but we'll see. This is on a pretty giant sequence of data, of course... An alternative would be to parallelize a row at a time, but that loses some of the purity of the solution. This is a very, very silly way of parallelizing the task, but it's got a certain quirky appeal.

Of course, there's then the code for ComputeMandelBrotIndex and displaying a bitmap from it - the full code is available for download (it's a single C# file - just compile and run). Enjoy.

Update!

This blog post has been picked up by Nick Palladinos, who has written his own Parallel LINQ provider (much kudos for that - unfortunately for me the blog is in Greek, which I don't understand). Apparently on a dual core processor the parallelised version of the Mandelbrot generator is indeed about twice as fast - it works! Unfortunately I can't tell as my laptop only has a single core... it's very exciting though :)

Published Wednesday, October 03, 2007 9:12 PM by skeet
Filed under: , , ,

Comments

# A cautionary parallel tale: ordering isn't simple

A little while ago, I wrote about my silly project to test Parallel LINQ - a Mandelbrot generator . In

Tuesday, December 04, 2007 5:08 PM by Jon Skeet's Coding Blog

# Visualising the Mandelbrot set with LINQ - yet again

I've been thinking about ranges again, particularly after catching a book error just in time, and

Tuesday, February 26, 2008 2:16 PM by Jon Skeet: Coding Blog

Leave a Comment

(required) 
(required) 
(optional)
(required)