-threaded and the RTS
-threaded and -N
The runtime that GHC links by default runs the green threads on one capability: the program can wait on many things at once and compute on one core. -threaded links the runtime that can run several capabilities on several operating-system threads, and +RTS -N chooses how many:
getNumCapabilities :: IO Int
./app +RTS -N1 -- one capability
./app +RTS -N4 -- four
./app +RTS -N -- one per CPU
getNumCapabilities reads the number at run time, so a program can split its work by the number of capabilities instead of a constant. The binary needs -rtsopts for the program to accept +RTS -N4.
Four chunks of work
The work is split into as many chunks as there are capabilities, each chunk sums a different range, and a thread per chunk computes it. The distinct ranges matter: a shared constant expression would be a CAF computed once and given to all four threads, which is not parallel work at all.
module Main (main) where
import Control.Concurrent (forkIO, getNumCapabilities, newEmptyMVar, putMVar, takeMVar)
import Control.Exception (evaluate)
import Control.Monad (forM)
chunk :: Int -> Int -> Int
chunk offset size = sum [offset + 1 .. offset + size :: Int]
main :: IO ()
main = do
caps <- getNumCapabilities
let total = 400000000
size = total `div` caps
dones <-
forM [0 .. caps - 1] $ \i -> do
box <- newEmptyMVar
_ <- forkIO (evaluate (chunk (i * size) size) >>= putMVar box)
pure box
results <- mapM takeMVar dones
print (caps, sum results)
ghc -Wall -O1 -threaded -rtsopts -o threads threads.hs
./threads +RTS -N1
./threads +RTS -N4
(1,80000000200000000)
(4,80000000200000000)
The two runs do the same total work and print the same sum, which is the first thing a parallel program has to get right. The (1, and (4, are the capability counts, read by the program, and they are the proof that -N reached this run.
The timings on this machine
Five runs each, after a warm-up, with , on the container this page was written in (4 CPUs, GHC 9.10.3, the binary at -O1):
-N1: 0.45 0.46 0.65 0.49 0.64 min 0.45 median 0.49
-N4: 0.22 0.23 0.23 0.27 0.22 min 0.22 median 0.23
The median falls from 0.49 s to 0.23 s, a little more than twice as fast on four capabilities. The spread is the shared machine: the -N1 runs vary from 0.45 to 0.65 and the -N4 runs from 0.22 to 0.27, which is why both statistics are reported and why a single run would be worthless. A speedup of two on four cores is a normal result for a program with a fixed cost of starting threads and a runtime that has other work to do.
Percent of CPU this job got: 289%, which is the proof that more than one core was used, and the RTS report below says the same from the other side.
+RTS -s of the parallel run
./threads +RTS -N4 -s
TASKS: 10 (1 bound, 9 peak workers (9 total), using -N4)
MUT time 0.784s ( 0.244s elapsed)
Total time 0.788s ( 0.247s elapsed)
The line that matters is the pair of times: MUT is the CPU time summed over the threads, elapsed is the wall time, and 0.784 against 0.244 is about 3.2 cores of work done in a quarter of a second. The TASKS line confirms using -N4. A run that shows MUT equal to elapsed is using one core, whatever -N says, and that is the first check when a parallel program is not faster.
What the numbers do and do not prove
- They prove that this program on this machine used more than one core and ran faster at -N4. They do not prove that every Haskell program will.
- A program that blocks on MVars gains nothing from -N; a program that computes in parallel gains up to the number of cores.
- The chunk size matters: four chunks for four capabilities is the easy case, and a chunk list of a thousand small sums would spend its time in the runtime.
- A shared thunk defeats the whole exercise, as the note on the program says; evaluate in the worker is what makes each chunk compute there and not in the main thread at the end.
The next lesson is about the package that would make this easier, parallel, and the real error this container gives when it is imported.
Exercises
- Run the five-run loop on your machine and compare the medians with the ones here.
- Change the total to 800000000 and see whether the ratio stays.
- Remove evaluate and explain the result.
- Use +RTS -N2 and compare with -N1 and -N4.
- Split the work into eight chunks with four capabilities and explain the effect.
- Read the MUT and elapsed of a single-threaded program of yours.
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What does
-threadedchange?Answer
It links a runtime that can run green threads on several OS threads
Without it the green threads share one capability; with it -N chooses the number.
-
What does
+RTS -N4set?Answer
The number of capabilities
Four capabilities, which the runtime schedules on the available cores.
-
What does
getNumCapabilitiesreturn?Answer
The number of capabilities of this run
It is what the program sees after -N, which is why it printed 1 and 4.
-
What does this program print?
module Main (main) where main :: IO () main = print (sum [1 .. 4 :: Int])Answer
10
The sum of 1 to 4 is 10.
-
Why do the four chunks use different ranges?
Answer
A shared constant expression would be computed once, not in parallel
A CAF is evaluated once and shared, so identical chunks are one chunk of work.
-
What do MUT and elapsed show in the RTS report?
Answer
CPU time summed over threads, and wall time
MUT greater than elapsed means several cores were used.
-
What does the measured median say?
Answer
About 0.49 s at -N1 and 0.23 s at -N4
The parallel run is a little more than twice as fast at the median.
-
What does this program print?
module Main (main) where main :: IO () main = print (length [1 .. 10 :: Int])Answer
10
The range has ten elements.
-
Which flag is needed for the binary to accept
+RTS -N4?Answer
-rtsoptsWithout -rtsopts the RTS options are restricted at run time.
-
What does a program that only blocks on MVars gain from -N4?
Answer
Nothing, it has to wait anyway
Parallelism helps computation, not waiting.
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