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

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…

  1. 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)
  2. Perform a time-consuming task as follows:

    task1 = 8^8^9 :: Integer
  3. Perform another time-consuming task as follows:

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

    $ ghc...