HORIZON HASKELLDocslts/ghc-9.10.xc74966e2026-09-27Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · c74966e · 2026-09-27

Modulemonad-par-0.3.6Haskell98

Control.Monad.Par

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 IVars, 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 puts to the same IVar are not allowed, and result in a runtime error. Values stored in IVars 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:

  • 3 types
  • 1 class
  • 17 values
  • Packagemonad-par-0.3.6
  • Exports21
  • LanguageHaskell98
  • LicenceBSD-3-Clause
  • SourcePar.hs

The Par Monad

4 declarations
newtypenewtype Par a
#
Instances6Monad, Functor, MonadFix, Applicative, ParFuture, ParIVar
  • Monad ParDefined in monad-par-0.3.6 · Control.Monad.Par.Scheds.TraceInternal
  • Functor ParDefined in monad-par-0.3.6 · Control.Monad.Par.Scheds.TraceInternal
  • MonadFix ParDefined in monad-par-0.3.6 · Control.Monad.Par.Scheds.TraceInternal
  • Applicative ParDefined in monad-par-0.3.6 · Control.Monad.Par.Scheds.TraceInternal
  • ParFuture IVar ParDefined in monad-par-0.3.6 · Control.Monad.Par.Scheds.Trace · orphan
  • ParIVar IVar ParDefined in monad-par-0.3.6 · Control.Monad.Par.Scheds.Trace · orphan
valuerunPar :: Par a -> a
#

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.

valuerunParIO :: Par a -> IO a
#

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 IVars.

Communication: IVars

7 declarations
newtypenewtype IVar a
#
Instances6ParFuture, ParIVar, Eq, NFData
  • ParFuture IVar ParIODefined in monad-par-0.3.6 · Control.Monad.Par.IO
  • ParFuture IVar ParDefined in monad-par-0.3.6 · Control.Monad.Par.Scheds.Trace · orphan
  • ParIVar IVar ParIODefined in monad-par-0.3.6 · Control.Monad.Par.IO
  • ParIVar IVar ParDefined in monad-par-0.3.6 · Control.Monad.Par.Scheds.Trace · orphan
  • Eq (IVar a)Defined in monad-par-0.3.6 · Control.Monad.Par.Scheds.TraceInternal

    Equality for IVars is physical equality, as with other reference types.

  • NFData (IVar a)Defined in monad-par-0.3.6 · Control.Monad.Par.Scheds.TraceInternal
valuenewFull_ :: a -> Par (IVar a)
#

Creates a new IVar that contains a value (head-strict only)

valueget :: IVar a -> Par a
#

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.

valueput :: NFData a => IVar a -> a -> Par ()
#

Put a value into an IVar. Multiple puts 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_.

valueput_ :: IVar a -> a -> Par ()
#

Like put, but only head-strict rather than fully-strict.

Operations

10 declarations

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
valueparFor :: ParFuture iv p => InclusiveRange -> (Int -> p ()) -> p ()
#

Parallel for-loop over an inclusive range. Semantically equivalent to

parFor (InclusiveRange n m) f = forM_ [n..m] f

except that the implementation will split the work into an unspecified number of subtasks in an attempt to gain parallelism. The exact number of subtasks is chosen at runtime, and is probably a small multiple of the available number of processors.

Strictly speaking the semantics of parFor depends on the number of processors, and its behaviour is therefore not deterministic. However, a good rule of thumb is to not have any interdependencies between the elements; if this rule is followed then parFor has deterministic semantics. One easy way to follow this rule is to only use put or put_ in f, never get.

valueparMap
  1. :: (Traversable t, NFData b, ParFuture iv p)
  2. => a -> b
  3. -> t a
  4. -> p (t b)
#

Applies the given function to each element of a data structure in parallel (fully evaluating the results), and returns a new data structure containing the results.

parMap f xs = mapM (spawnP . f) xs >>= mapM get

parMap is commonly used for lists, where it has this specialised type:

parMap :: NFData b => (a -> b) -> [a] -> Par [b]
valueparMapReduceRangeThresh
  1. :: (NFData a, ParFuture iv p)
  2. => Int

    threshold

  3. -> InclusiveRange

    range over which to calculate

  4. -> (Int -> p a)

    compute one result

  5. -> (a -> a -> p a)

    combine two results (associative)

  6. -> a

    initial result

  7. -> p a
#

Computes a binary map/reduce over a finite range. The range is recursively split into two, the result for each half is computed in parallel, and then the two results are combined. When the range reaches the threshold size, the remaining elements of the range are computed sequentially.

For example, the following is a parallel implementation of

 foldl (+) 0 (map (^2) [1..10^6])
parMapReduceRangeThresh 100 (InclusiveRange 1 (10^6))
       (\x -> return (x^2))
       (\x y -> return (x+y))
       0

This module also reexports the Combinator library for backwards compatibility with version 0.1.

classclass NFData a where
#

A class of types that can be fully evaluated.

Instances175NFData, …

(0.3) Reexport NFData for fully-strict operators.