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

Accessing tuple elements in parallel


In this recipe, we will cover how to access elements of a tuple in parallel.

How to do it…

  1. Import the built-in package as follows:

    import Control.Parallel.Strategies
  2. Evaluate the expression in a tuple in parallel. We perform this task twice with different strategies to demonstrate how strategies are easily swapped to change the parallel nature of the code as follows:

    main = do
      let (a, b) = withStrategy (parTuple2 rseq rseq) (task1, task2)
      print $ seq (a+b) "done 1"
      let (a, bs) = withStrategy (parTuple2 rseq rdeepseq) (task1, tasks)
      print $ seq (a + sum bs) "done 2"
  3. Define time-consuming tasks as follows:

    task1 = 8^8^8 :: Integer
    task2 = 8^8^8 :: Integer
    tasks = [10^10..10^10+10000] :: [Integer]
  4. Compile the code with the threaded and rtsopts flags enabled, as follows:

    $ ghc -O2 --make Main.hs -threaded -rtsopts
    
  5. Run it by specifying the number of cores as follows:

    $ ./Main +RTS -N2
    

There's more…

When dealing with tuples of more than two elements, other helper...