Safe Haskell | None |
---|---|
Language | Haskell98 |
The monad-par
package provides a family of Par
monads, for
speeding up pure computations using parallel processors. (for a similar
programming model for use with IO
, see Control.Monad.Par.IO.)
The result of a given Par
computation is always the same - i.e. it
is deterministic, but the computation may be performed more quickly
if there are processors available to share the work.
For example, the following program fragment computes the values of
(f x)
and (g x)
in parallel, and returns a pair of their results:
runPar $ do fx <- spawnP (f x) -- start evaluating (f x) gx <- spawnP (g x) -- start evaluating (g x) a <- get fx -- wait for fx b <- get gx -- wait for gx return (a,b) -- return results
Par
can be used for specifying pure parallel computations in
which the order of the computation is not known beforehand.
The programmer specifies how information flows from one
part of the computation to another, but not the order in which
computations will be evaluated at runtime. Information flow is
described using "variables" called IVar
s, which support put
and
get
operations. For example, suppose you have a problem that
can be expressed as a network with four nodes, where b
and c
require the value of a
, and d
requires the value of b
and c
:
a / \ b c \ / d
Then you could express this in the Par
monad like this:
runPar $ do [a,b,c,d] <- sequence [new,new,new,new] fork $ do x <- get a; put b (x+1) fork $ do x <- get a; put c (x+2) fork $ do x <- get b; y <- get c; put d (x+y) fork $ do put a (3 :: Int) get d
The result of the above computation is always 9. The get
operation
waits until its input is available; multiple put
s to the same
IVar
are not allowed, and result in a runtime error. Values
stored in IVar
s are usually fully evaluated (although there are
ways provided to pass lazy values if necessary).
In the above example, b
and c
will be evaluated in parallel.
In practice the work involved at each node is too small here to see
the benefits of parallelism though: typically each node should
involve much more work. The granularity is completely under your
control - too small and the overhead of the Par
monad will
outweigh any parallelism benefits, whereas if the nodes are too
large then there might not be enough parallelism to use all the
available processors.
Unlike Control.Parallel
, in Control.Monad.Par
parallelism is
not combined with laziness, so sharing and granularity are
completely under the control of the programmer. New units of
parallel work are only created by fork
and a few other
combinators.
The default implementation is based on a work-stealing scheduler that divides the work as evenly as possible between the available processors at runtime. Other schedulers are available that are based on different policies and have different performance characteristics. To use one of these other schedulers, just import its module instead of Control.Monad.Par:
For more information on the programming model, please see these sources:
- The wiki/tutorial (http://www.haskell.org/haskellwiki/Par_Monad:_A_Parallelism_Tutorial)
- The original paper (http://www.cs.indiana.edu/~rrnewton/papers/haskell2011_monad-par.pdf)
- Tutorial slides (http://community.haskell.org/~simonmar/slides/CUFP.pdf)
- Other slides: (http://www.cs.ox.ac.uk/ralf.hinze/WG2.8/28/slides/simon.pdf, http://www.cs.indiana.edu/~rrnewton/talks/2011_HaskellSymposium_ParMonad.pdf)
Synopsis
- data Par a
- runPar :: Par a -> a
- runParIO :: Par a -> IO a
- fork :: Par () -> Par ()
- data IVar a
- new :: Par (IVar a)
- newFull :: NFData a => a -> Par (IVar a)
- newFull_ :: a -> Par (IVar a)
- get :: IVar a -> Par a
- put :: NFData a => IVar a -> a -> Par ()
- put_ :: IVar a -> a -> Par ()
- spawn :: NFData a => Par a -> Par (IVar a)
- spawn_ :: Par a -> Par (IVar a)
- spawnP :: NFData a => a -> Par (IVar a)
- module Control.Monad.Par.Combinator
- class NFData a
The Par Monad
Run a parallel, deterministic computation and return its result.
Note: you must NOT return an IVar in the output of the parallel
computation. This is unfortunately not enforced, as it is with
runST
or with newer libraries that export a Par monad, such as
lvish
.
runParIO :: Par a -> IO a Source #
A version that avoids an internal unsafePerformIO
for calling
contexts that are already in the IO
monad.
Returning any value containing IVar is still disallowed, as it can compromise type safety.
Forks a computation to happen in parallel. The forked
computation may exchange values with other computations using
IVar
s.
Communication: IVars
get :: IVar a -> Par a Source #
Read the value in an IVar
. The get
operation can only return when the
value has been written by a prior or parallel put
to the same
IVar
.
put :: NFData a => IVar a -> a -> Par () Source #
Put a value into an IVar
. Multiple put
s to the same IVar
are not allowed, and result in a runtime error.
put
fully evaluates its argument, which therefore must be an
instance of NFData
. The idea is that this forces the work to
happen when we expect it, rather than being passed to the consumer
of the IVar
and performed later, which often results in less
parallelism than expected.
Sometimes partial strictness is more appropriate: see put_
.
Operations
Like fork
, but returns an IVar
that can be used to query the
result of the forked computataion. Therefore spawn
provides futures or promises.
spawn p = do r <- new fork (p >>= put r) return r
Like spawn
, but the result is only head-strict, not fully-strict.
Spawn a pure (rather than monadic) computation. Fully-strict.
spawnP = spawn . return
module Control.Monad.Par.Combinator
This module also reexports the Combinator library for backwards compatibility with version 0.1.
A class of types that can be fully evaluated.
Since: deepseq-1.1.0.0
Instances
NFData Bool | |
Defined in Control.DeepSeq | |
NFData Char | |
Defined in Control.DeepSeq | |
NFData Double | |
Defined in Control.DeepSeq | |
NFData Float | |
Defined in Control.DeepSeq | |
NFData Int | |
Defined in Control.DeepSeq | |
NFData Int8 | |
Defined in Control.DeepSeq | |
NFData Int16 | |
Defined in Control.DeepSeq | |
NFData Int32 | |
Defined in Control.DeepSeq | |
NFData Int64 | |
Defined in Control.DeepSeq | |
NFData Integer | |
Defined in Control.DeepSeq | |
NFData Natural | Since: deepseq-1.4.0.0 |
Defined in Control.DeepSeq | |
NFData Ordering | |
Defined in Control.DeepSeq | |
NFData Word | |
Defined in Control.DeepSeq | |
NFData Word8 | |
Defined in Control.DeepSeq | |
NFData Word16 | |
Defined in Control.DeepSeq | |
NFData Word32 | |
Defined in Control.DeepSeq | |
NFData Word64 | |
Defined in Control.DeepSeq | |
NFData CallStack | Since: deepseq-1.4.2.0 |
Defined in Control.DeepSeq | |
NFData () | |
Defined in Control.DeepSeq | |
NFData TyCon | NOTE: Prior to Since: deepseq-1.4.0.0 |
Defined in Control.DeepSeq | |
NFData Void | Since: deepseq-1.4.0.0 |
Defined in Control.DeepSeq | |
NFData Unique | Since: deepseq-1.4.0.0 |
Defined in Control.DeepSeq | |
NFData Version | Since: deepseq-1.3.0.0 |
Defined in Control.DeepSeq | |
NFData ThreadId | Since: deepseq-1.4.0.0 |
Defined in Control.DeepSeq | |
NFData ExitCode | Since: deepseq-1.4.2.0 |
Defined in Control.DeepSeq | |
NFData MaskingState | Since: deepseq-1.4.4.0 |
Defined in Control.DeepSeq rnf :: MaskingState -> () # | |
NFData TypeRep | NOTE: Prior to Since: deepseq-1.4.0.0 |
Defined in Control.DeepSeq | |
NFData All | Since: deepseq-1.4.0.0 |
Defined in Control.DeepSeq | |
NFData Any | Since: deepseq-1.4.0.0 |
Defined in Control.DeepSeq | |
NFData CChar | Since: deepseq-1.4.0.0 |
Defined in Control.DeepSeq | |
NFData CSChar | Since: deepseq-1.4.0.0 |
Defined in Control.DeepSeq | |
NFData CUChar | Since: deepseq-1.4.0.0 |
Defined in Control.DeepSeq | |
NFData CShort | Since: deepseq-1.4.0.0 |
Defined in Control.DeepSeq | |
NFData CUShort | Since: deepseq-1.4.0.0 |
Defined in Control.DeepSeq | |
NFData CInt | Since: deepseq-1.4.0.0 |
Defined in Control.DeepSeq | |
NFData CUInt | Since: deepseq-1.4.0.0 |
Defined in Control.DeepSeq | |
NFData CLong | Since: deepseq-1.4.0.0 |
Defined in Control.DeepSeq | |
NFData CULong | Since: deepseq-1.4.0.0 |
Defined in Control.DeepSeq | |
NFData CLLong | Since: deepseq-1.4.0.0 |
Defined in Control.DeepSeq | |
NFData CULLong | Since: deepseq-1.4.0.0 |
Defined in Control.DeepSeq | |
NFData CBool | Since: deepseq-1.4.3.0 |
Defined in Control.DeepSeq | |
NFData CFloat | Since: deepseq-1.4.0.0 |
Defined in Control.DeepSeq | |
NFData CDouble | Since: deepseq-1.4.0.0 |
Defined in Control.DeepSeq | |
NFData CPtrdiff | Since: deepseq-1.4.0.0 |
Defined in Control.DeepSeq | |
NFData CSize | Since: deepseq-1.4.0.0 |
Defined in Control.DeepSeq | |
NFData CWchar | Since: deepseq-1.4.0.0 |
Defined in Control.DeepSeq | |
NFData CSigAtomic | Since: deepseq-1.4.0.0 |
Defined in Control.DeepSeq rnf :: CSigAtomic -> () # | |
NFData CClock | Since: deepseq-1.4.0.0 |
Defined in Control.DeepSeq | |
NFData CTime | Since: deepseq-1.4.0.0 |
Defined in Control.DeepSeq | |
NFData CUSeconds | Since: deepseq-1.4.0.0 |
Defined in Control.DeepSeq | |
NFData CSUSeconds | Since: deepseq-1.4.0.0 |
Defined in Control.DeepSeq rnf :: CSUSeconds -> () # | |
NFData CFile | Since: deepseq-1.4.0.0 |
Defined in Control.DeepSeq | |
NFData CFpos | Since: deepseq-1.4.0.0 |
Defined in Control.DeepSeq | |
NFData CJmpBuf | Since: deepseq-1.4.0.0 |
Defined in Control.DeepSeq | |
NFData CIntPtr | Since: deepseq-1.4.0.0 |
Defined in Control.DeepSeq | |
NFData CUIntPtr | Since: deepseq-1.4.0.0 |
Defined in Control.DeepSeq | |
NFData CIntMax | Since: deepseq-1.4.0.0 |
Defined in Control.DeepSeq | |
NFData CUIntMax | Since: deepseq-1.4.0.0 |
Defined in Control.DeepSeq | |
NFData Fingerprint | Since: deepseq-1.4.0.0 |
Defined in Control.DeepSeq rnf :: Fingerprint -> () # | |
NFData SrcLoc | Since: deepseq-1.4.2.0 |
Defined in Control.DeepSeq | |
NFData a => NFData [a] | |
Defined in Control.DeepSeq | |
NFData a => NFData (Maybe a) | |
Defined in Control.DeepSeq | |
NFData a => NFData (Ratio a) | |
Defined in Control.DeepSeq | |
NFData (Ptr a) | Since: deepseq-1.4.2.0 |
Defined in Control.DeepSeq | |
NFData (FunPtr a) | Since: deepseq-1.4.2.0 |
Defined in Control.DeepSeq | |
NFData a => NFData (Complex a) | |
Defined in Control.DeepSeq | |
NFData (Fixed a) | Since: deepseq-1.3.0.0 |
Defined in Control.DeepSeq | |
NFData a => NFData (Min a) | Since: deepseq-1.4.2.0 |
Defined in Control.DeepSeq | |
NFData a => NFData (Max a) | Since: deepseq-1.4.2.0 |
Defined in Control.DeepSeq | |
NFData a => NFData (First a) | Since: deepseq-1.4.2.0 |
Defined in Control.DeepSeq | |
NFData a => NFData (Last a) | Since: deepseq-1.4.2.0 |
Defined in Control.DeepSeq | |
NFData m => NFData (WrappedMonoid m) | Since: deepseq-1.4.2.0 |
Defined in Control.DeepSeq rnf :: WrappedMonoid m -> () # | |
NFData a => NFData (Option a) | Since: deepseq-1.4.2.0 |
Defined in Control.DeepSeq | |
NFData (StableName a) | Since: deepseq-1.4.0.0 |
Defined in Control.DeepSeq rnf :: StableName a -> () # | |
NFData a => NFData (ZipList a) | Since: deepseq-1.4.0.0 |
Defined in Control.DeepSeq | |
NFData a => NFData (Identity a) | Since: deepseq-1.4.0.0 |
Defined in Control.DeepSeq | |
NFData (IORef a) | NOTE: Only strict in the reference and not the referenced value. Since: deepseq-1.4.2.0 |
Defined in Control.DeepSeq | |
NFData a => NFData (First a) | Since: deepseq-1.4.0.0 |
Defined in Control.DeepSeq | |
NFData a => NFData (Last a) | Since: deepseq-1.4.0.0 |
Defined in Control.DeepSeq | |
NFData a => NFData (Dual a) | Since: deepseq-1.4.0.0 |
Defined in Control.DeepSeq | |
NFData a => NFData (Sum a) | Since: deepseq-1.4.0.0 |
Defined in Control.DeepSeq | |
NFData a => NFData (Product a) | Since: deepseq-1.4.0.0 |
Defined in Control.DeepSeq | |
NFData a => NFData (Down a) | Since: deepseq-1.4.0.0 |
Defined in Control.DeepSeq | |
NFData (MVar a) | NOTE: Only strict in the reference and not the referenced value. Since: deepseq-1.4.2.0 |
Defined in Control.DeepSeq | |
NFData a => NFData (NonEmpty a) | Since: deepseq-1.4.2.0 |
Defined in Control.DeepSeq | |
NFData a => NFData (Seq a) | |
Defined in Data.Sequence.Internal | |
NFData a => NFData (FingerTree a) | |
Defined in Data.Sequence.Internal rnf :: FingerTree a -> () # | |
NFData a => NFData (Digit a) | |
Defined in Data.Sequence.Internal | |
NFData a => NFData (Node a) | |
Defined in Data.Sequence.Internal | |
NFData a => NFData (Elem a) | |
Defined in Data.Sequence.Internal | |
NFData a => NFData (Set a) | |
Defined in Data.Set.Internal | |
NFData (Vector a) | |
Defined in Data.Vector.Unboxed.Base | |
NFData (Vector a) | |
Defined in Data.Vector.Primitive | |
NFData (IVar a) Source # | |
Defined in Control.Monad.Par.Scheds.Direct | |
NFData (IVar a) Source # | |
Defined in Control.Monad.Par.Scheds.TraceInternal | |
NFData (a -> b) | This instance is for convenience and consistency with Since: deepseq-1.3.0.0 |
Defined in Control.DeepSeq | |
(NFData a, NFData b) => NFData (Either a b) | |
Defined in Control.DeepSeq | |
(NFData a, NFData b) => NFData (a, b) | |
Defined in Control.DeepSeq | |
(NFData a, NFData b) => NFData (Array a b) | |
Defined in Control.DeepSeq | |
(NFData a, NFData b) => NFData (Arg a b) | Since: deepseq-1.4.2.0 |
Defined in Control.DeepSeq | |
NFData (Proxy a) | Since: deepseq-1.4.0.0 |
Defined in Control.DeepSeq | |
NFData (STRef s a) | NOTE: Only strict in the reference and not the referenced value. Since: deepseq-1.4.2.0 |
Defined in Control.DeepSeq | |
NFData (MVector s a) | |
Defined in Data.Vector.Unboxed.Base | |
NFData (MVector s a) | |
Defined in Data.Vector.Primitive.Mutable | |
(NFData a1, NFData a2, NFData a3) => NFData (a1, a2, a3) | |
Defined in Control.DeepSeq | |
NFData a => NFData (Const a b) | Since: deepseq-1.4.0.0 |
Defined in Control.DeepSeq | |
NFData (a :~: b) | Since: deepseq-1.4.3.0 |
Defined in Control.DeepSeq | |
(NFData a1, NFData a2, NFData a3, NFData a4) => NFData (a1, a2, a3, a4) | |
Defined in Control.DeepSeq | |
(NFData1 f, NFData1 g, NFData a) => NFData (Product f g a) | Since: deepseq-1.4.3.0 |
Defined in Control.DeepSeq | |
(NFData1 f, NFData1 g, NFData a) => NFData (Sum f g a) | Since: deepseq-1.4.3.0 |
Defined in Control.DeepSeq | |
NFData (a :~~: b) | Since: deepseq-1.4.3.0 |
Defined in Control.DeepSeq | |
(NFData a1, NFData a2, NFData a3, NFData a4, NFData a5) => NFData (a1, a2, a3, a4, a5) | |
Defined in Control.DeepSeq | |
(NFData1 f, NFData1 g, NFData a) => NFData (Compose f g a) | Since: deepseq-1.4.3.0 |
Defined in Control.DeepSeq | |
(NFData a1, NFData a2, NFData a3, NFData a4, NFData a5, NFData a6) => NFData (a1, a2, a3, a4, a5, a6) | |
Defined in Control.DeepSeq | |
(NFData a1, NFData a2, NFData a3, NFData a4, NFData a5, NFData a6, NFData a7) => NFData (a1, a2, a3, a4, a5, a6, a7) | |
Defined in Control.DeepSeq | |
(NFData a1, NFData a2, NFData a3, NFData a4, NFData a5, NFData a6, NFData a7, NFData a8) => NFData (a1, a2, a3, a4, a5, a6, a7, a8) | |
Defined in Control.DeepSeq | |
(NFData a1, NFData a2, NFData a3, NFData a4, NFData a5, NFData a6, NFData a7, NFData a8, NFData a9) => NFData (a1, a2, a3, a4, a5, a6, a7, a8, a9) | |
Defined in Control.DeepSeq |
(0.3) Reexport NFData
for fully-strict operators.