Controlling parallel algorithms in sequence
In this recipe, we will conduct two time-consuming tasks in parallel. We will use the rpar
function and the rseq
function provided by the parallel
package from hackage. The rpar
function annotates its argument to be evaluated in parallel. The other function, rseq
, forces sequential evaluations in what is called the
weak head normal form.
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 import Control.Parallel.Strategies Evaluate two tasks in parallel, and wait for both tasks to finish before returning. main = do print $ runEval $ do a <- rpar task1 b <- rpar task2 rseq a rseq b return (a, b)
Perform a time-consuming task as follows:
task1 = 8^8^9 :: Integer
Perform another time-consuming task as follows:
task2 = 8^8^8 :: Integer
Compile the code with the
threaded
andrtsopts
flags enabled as follows:$ ghc...