Evaluating a procedure in parallel
In this recipe, we will conduct two time-consuming tasks in parallel. We will use the rpar
function provided by the parallel
package from hackage. The rpar
function annotates its argument to be evaluated in parallel. Then, we call
runEval
to actually perform the computation.
Getting ready
Install the parallel
package using cabal as follows:
$ cabal install parallel
How to do it…
- Import the parallel package as follows:
import Control.Parallel.Strategies (runEval, rpar)
- Evaluate two tasks in parallel, and wait for both tasks to finish before returning as seen in the following code snippet:
main = do print $ runEval $ do a <- rpar task1 b <- rpar task2 return (a, b)
- A time-consuming task can be created as follows:
task1 = 8^8^9 :: Integer
- Another time-consuming task can be created as follows:
task2 = 8^8^8 :: Integer
- Compile the code with the
threaded
andrtsopts
flags enabled, as follows:$ ghc -O2 --make Main.hs -threaded –rtsopts...