Book Image

Haskell Data Analysis Cookbook

By : Nishant Shukla
Book Image

Haskell Data Analysis Cookbook

By: Nishant Shukla

Overview of this book

Table of Contents (19 chapters)
Haskell Data Analysis Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

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…

  1. Import the parallel package as follows:

    import Control.Parallel.Strategies (runEval, rpar)
  2. 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)
  3. A time-consuming task can be created as follows:

    task1 = 8^8^9 :: Integer
  4. Another time-consuming task can be created as follows:

    task2 = 8^8^8 :: Integer
  5. Compile the code with the threaded and rtsopts flags enabled, as follows:

    $ ghc -O2 --make Main.hs -threaded –rtsopts
    
  6. Run it by specifying...