Parallelization in F#

In my last post I was writing about parallelizing loops with Parallel.For in C#. Today I though it would be nice to try that in F#. So, here is the benchmarking of the matrix multiplication and the bubblesort algorithm in F4.

Matrices Multiplication

I started with a create_matrix function that creates and randomly initializes a matrix of doubles.

Sequential multiplication could look like this:

Parallelizing it only implies replacing the outer loop with Parallel.For.

We can test those function and get the same output as I had in my previous post with this:

Before running, you have to make sure you add System.Threading.dll to the referred assemblies. And since this one depends on System.Core.dll, you also have to add this one. In you are using Visual Studio and a F# project, you can add the two references from the project properties.

-r c:\Windows\assembly\GAC_MSIL\System.Core\\System.Core.dll -r “C:\Program Files\Microsoft Parallel Extensions Dec07 CTP\System.Threading.dll”

The results are shown below:

Array Sorting

First, I created a function, create_array, that creates and randomly initializes an array of doubles.

The sequential bubblesort implementation is quite straight forward, of course.

Parallelizing it, again, only means replacing the outer for loop with Parallel.For.

And this is how the two functions were used:

The output for the program is:

If you compare the output with the one from C#, you’ll notice that the times are smaller. It looks like F# is faster than C#. Of course it can get faster if I replace the call to CompareTo() with operator <.

In this case the results look like this:

2 Replies to “Parallelization in F#”

  1. Hi,

    I have a inquiry for the webmaster/admin here at

    Can I use part of the information from your blog post right above if I provide a link back to this site?


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.