Parallelism and what is missing

The parallel package is not here

The usual way to write parallel pure code is Control.Parallel: par marks an expression as worth evaluating in another thread, and pseq orders two evaluations. The package is not a boot library of GHC, and this container has no network, so the import fails before anything else:

module Main (main) where import Control.Parallel (par, pseq) main :: IO () main = print (1 `par` (2 `pseq` (1 + 2)))

The real error

ghc -Wall -fno-code missing.hs

missing.hs:3:1: error: [GHC-87110]
    Could not find module ‘Control.Parallel’.
    Use -v to see a list of the files searched for.
  |
3 | import Control.Parallel (par, pseq)
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

On a machine with an index, the fix is cabal install --lib parallel or a line in build-depends, and the package then compiles against the same GHC. Without it there are no sparks, and a page that showed par output here would be inventing it.

What par and pseq mean

The two functions are small and the meaning is worth knowing even without the package:

The mode of the runtime is the other half: `-N` gives the capabilities, and a spark only runs on one that is free. The lesson on the threaded runtime measured that part with threads, which is the tool this container has.

The forkIO version, measured

The same idea with the tools that are installed: split the work into four chunks, one thread each, and sum the results. The chunks have different offsets, so no constant expression is shared between them.

module Main (main) where import Control.Concurrent (forkIO, newEmptyMVar, putMVar, takeMVar) import Control.Exception (evaluate) import Control.Monad (forM) chunkSum :: Int -> Int chunkSum n = sum [n * 1000 + 1 .. n * 1000 + 20000000 :: Int] main :: IO () main = do boxes <- forM [1 .. 4 :: Int] $ \n -> do box <- newEmptyMVar _ <- forkIO (evaluate (chunkSum n) >>= putMVar box) pure box values <- mapM takeMVar boxes print (sum values)

ghc -Wall -O1 -threaded -rtsopts -o chunks chunks.hs
./chunks +RTS -N1
./chunks +RTS -N4
for i in 1 2 3 4 5; do /usr/bin/time -f "%e" ./chunks +RTS -N1 >/dev/null; done
for i in 1 2 3 4 5; do /usr/bin/time -f "%e" ./chunks +RTS -N4 >/dev/null; done

800200040000000
800200040000000
0.09
0.09
0.09
0.09
0.09
0.04
0.04
0.04
0.04
0.04

The sum is the same at both settings, as it must be. The -N1 runs take 0.09 s each and the -N4 runs 0.04 s, a little over twice as fast: the runtime ran the four chunks on several capabilities, which is what the par version would do with a scheduler and a spark pool instead of four MVars.

The trade is visible in the code: the forkIO version writes the split, the boxes and the join by hand, while the par version would leave all of it to the runtime. The next lesson uses the manual version for a real design, a pool of workers with an STM queue, which is the shape a program needs when the jobs are not known in advance.

Exercises

  1. Run the chunks program with eight chunks and four capabilities and compare the timings.
  2. Explain why the same constant expression in all four threads is not parallel work.
  3. Write the chunked sum with forkIO and an STM result list instead of MVars.
  4. Read the GHC user guide section on sparks and write down two settings that change their behaviour.
  5. Explain what pseq is for and what happens without it.
  6. Decide for a program you know whether the parallel package would help or whether the work is not parallel at all.

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. What does a par b do?

    Answer

    Suggests evaluating a in parallel and returns b

    par creates a spark for a and its value is b; the runtime decides whether to use it.

  2. Why does import Control.Parallel fail here?

    Answer

    The package is not a boot library and there is no network

    GHC-87110 says the module was not found in any package of this installation.

  3. What does pseq add to a parallel expression?

    Answer

    It forces the first argument before returning the second

    The ordering keeps a value alive and evaluated in the order the program needs.

  4. What does this program print? module Main (main) where main :: IO () main = print (sum [1 .. 3 :: Int])

    Answer

    6

    The sum of 1, 2 and 3 is 6.

  5. What did the chunked program measure at -N1 and -N4?

    Answer

    0.09 s and 0.04 s

    The five runs were 0.09 each at -N1 and 0.04 each at -N4.

  6. What does a spark that the runtime ignores do?

    Answer

    The work stays on the main thread and the program is not faster

    A spark is a hint, so an ignored one leaves the computation where it was.

  7. What does this program print? module Main (main) where main :: IO () main = print (product [1 .. 4 :: Int])

    Answer

    24

    The product of 1 to 4 is 24.

  8. What is the forkIO version missing compared with par?

    Answer

    The split, the boxes and the join are written by hand

    par lets the runtime schedule the work; the manual version spells it out.

  9. What does this program print? module Main (main) where main :: IO () main = print (length [1 .. 100 :: Int])

    Answer

    100

    The range has 100 elements.

  10. Which runtime flag gives the capabilities a spark can use?

    Answer

    +RTS -N

    The number of capabilities is set with -N, and -threaded is the runtime that has them.

The code on this page was built with GHC 9.10.3 and run on Alpine Linux 3.24.3 (x86_64) in a Docker container.

Article author: Arthur Isaev