The functional programming language Haskell provides a very easy way of parallelization. Consider the following naive implementation of the
fib 0 = 0
fib 1 = 1
fib n = fib (n-1) + fib (n-2)
This implementation has a bad expontential time complexity, so it should be improved, for example with caching. But this is beyond the scope of this article. We just need a function that takes a while to finish.
In Haskell there are two operators that have to be used for parallelization:
par a b is some kind of a “fork” operation:
a is started in parallel and
b is returned. Keep in mind that Haskell is has a lazy evaluation strategy.
a is only evaluated if it is needed The function
pseq a b evaluates first
Equipped with this two operations it is very easy to parallelize
| n < 11 = fib n -- For small values of n we use the sequential version
| otherwise = f1 `par` (f2 `pseq` (f1+f2)) -- calculate f1 and f2 in parallel, return the sum as the result
f1 = parfib (n-1)
f2 = parfib (n-2)
The code has to be compiled with the
ghc -O3 -threaded --make -o parfib ParFib.hs
The number of threads is specified at runtime with the
-N command line option.
./parfib +RTS -N7 -RTS
On an Intel Core i7 920 this resulted in a speedup of 4.13 for
n=38. This processor has four physical cores.
So this is efficient. Haskell is still one of the best programming languages.