Description

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 <- spawn (return (f x))  -- start evaluating (f x)
gx <- spawn (return (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 granulairty 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:

Synopsis

data Par a Source

Instances

 Monad Par Functor Par Applicative Par ParFuture IVar Par ParIVar IVar Par

runPar :: Par a -> aSource

runParIO :: Par a -> IO aSource

A version that avoids an internal `unsafePerformIO` for calling contexts that are already in the `IO` monad.

fork :: Par () -> Par ()Source

forks a computation to happen in parallel. The forked computation may exchange values with other computations using `IVar`s.

# Communication: IVars

data IVar a Source

Instances

 ParFuture IVar Par ParFuture IVar ParIO ParIVar IVar Par ParIVar IVar ParIO NFData (IVar a)

new :: Par (IVar a)Source

creates a new `IVar`

creates a new `IVar`

newFull :: NFData a => a -> Par (IVar a)Source

creates a new `IVar` that contains a value

creates a new `IVar` that contains a value

newFull_ :: a -> Par (IVar a)Source

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

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

get :: IVar a -> Par aSource

read the value in a `IVar`. The `get` can only return when the value has been written by a prior or parallel `put` to the same `IVar`.

read the value in an `IVar`. `get` 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 a `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_`.

put a value into a `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_`.

put_ :: IVar a -> a -> Par ()Source

like `put`, but only head-strict rather than fully-strict.

like `put`, but only head-strict rather than fully-strict.

# Operations

spawn :: NFData a => Par a -> Par (IVar a)Source

Like `fork`, but returns a `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
```

spawn_ :: Par a -> Par (IVar a)Source

Like `spawn`, but the result is only head-strict, not fully-strict.

spawnP :: NFData a => a -> Par (IVar a)Source

Spawn a pure (rather than monadic) computation. Fully-strict.

```  spawnP = spawn . return
```

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

class NFData a

A class of types that can be fully evaluated.

Instances

 NFData Bool NFData Char NFData Double NFData Float NFData Int NFData Int8 NFData Int16 NFData Int32 NFData Int64 NFData Integer NFData Word NFData Word8 NFData Word16 NFData Word32 NFData Word64 NFData () NFData Version NFData a => NFData [a] (Integral a, NFData a) => NFData (Ratio a) NFData (Fixed a) (RealFloat a, NFData a) => NFData (Complex a) NFData a => NFData (Maybe a) NFData a => NFData (Set a) NFData (Vector a) NFData (Vector a) NFData (IVar a) NFData (IVar a) NFData (a -> b) This instance is for convenience and consistency with `seq`. This assumes that WHNF is equivalent to NF for functions. (NFData a, NFData b) => NFData (Either a b) (NFData a, NFData b) => NFData (a, b) (Ix a, NFData a, NFData b) => NFData (Array a b) (NFData k, NFData a) => NFData (Map k a) NFData (MVector s a) NFData (MVector s a) (NFData a, NFData b, NFData c) => NFData (a, b, c) (NFData a, NFData b, NFData c, NFData d) => NFData (a, b, c, d) (NFData a1, NFData a2, NFData a3, NFData a4, NFData a5) => NFData (a1, a2, a3, a4, a5) (NFData a1, NFData a2, NFData a3, NFData a4, NFData a5, NFData a6) => NFData (a1, a2, a3, a4, a5, a6) (NFData a1, NFData a2, NFData a3, NFData a4, NFData a5, NFData a6, NFData a7) => NFData (a1, a2, a3, a4, a5, a6, a7) (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) (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)

(0.3) Reexport `NFData` for fully-strict operators.
{"ft_lang_label":"__label__en","ft_lang_prob":0.71568835,"math_prob":0.95637184,"size":7668,"snap":"2021-21-2021-25","text_gpt3_token_len":2253,"char_repetition_ratio":0.16257828,"word_repetition_ratio":0.24908425,"special_character_ratio":0.27490872,"punctuation_ratio":0.15732484,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9785178,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-05-08T02:48:26Z\",\"WARC-Record-ID\":\"<urn:uuid:d00eb3e9-e7da-4694-9266-b13258a328a5>\",\"Content-Length\":\"33143\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:c10350bd-37f6-4149-a51a-ed9182a4634e>\",\"WARC-Concurrent-To\":\"<urn:uuid:ce9e1636-f687-4404-a1fa-eefe433d113e>\",\"WARC-IP-Address\":\"151.101.248.68\",\"WARC-Target-URI\":\"https://hackage.haskell.org/package/monad-par-0.3.4.4/docs/Control-Monad-Par.html\",\"WARC-Payload-Digest\":\"sha1:BZVIXK3A3H2EA65OP7X5EZAHU62N3RST\",\"WARC-Block-Digest\":\"sha1:7EYJCBEB4PREJFU43N4UFHIYGD4NSOCG\",\"WARC-Identified-Payload-Type\":\"application/xhtml+xml\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-21/CC-MAIN-2021-21_segments_1620243988831.77_warc_CC-MAIN-20210508001259-20210508031259-00532.warc.gz\"}"}