Capstone C: measuring the pool

Measuring the pool

The last lesson measures the pool: eight CPU-bound jobs, a worker count from the argument, and the five-run method of the performance lesson. The work is forced in the worker with evaluate, because a thunk handed to a TVar is computed later in the main thread and the measurement would be of nothing.

value <- evaluate (work (jobCost job)) atomically (modifyTVar' results ((jobId job, value) :))

The jobs have different costs on purpose: a shared constant expression would be a CAF computed once, and the pool would look parallel while doing one job.

The program

module Main (main) where import Control.Concurrent (forkIO) import Control.Concurrent.STM import Control.Exception (evaluate) import Control.Monad (replicateM_, when) import System.Environment (getArgs) data Job = Job { jobId :: Int , jobCost :: Int } work :: Int -> Int work n = sum [1 .. n :: Int] readJob :: TVar [Job] -> STM (Maybe Job) readJob queue = do jobs <- readTVar queue case jobs of [] -> pure Nothing job : rest -> writeTVar queue rest >> pure (Just job) worker :: TVar [Job] -> TVar [(Int, Int)] -> TVar Int -> IO () worker queue results finished = loop where loop = do next <- atomically (readJob queue) case next of Nothing -> atomically (modifyTVar' finished (+ 1)) Just job -> do value <- evaluate (work (jobCost job)) atomically (modifyTVar' results ((jobId job, value) :)) loop main :: IO () main = do args <- getArgs let workers = case args of [n] -> read n _ -> 1 jobs = [Job n (n * 5000000) | n <- [1 .. 8]] queue <- newTVarIO jobs results <- newTVarIO [] finished <- newTVarIO 0 replicateM_ workers (forkIO (worker queue results finished)) atomically $ do done <- readTVar finished when (done < workers) retry values <- readTVarIO results print (workers, sum (map snd values))

ghc -Wall -O1 -threaded -rtsopts -o bench bench.hs
./bench 1 +RTS -N4
./bench 4 +RTS -N4

(1,2550000090000000)
(4,2550000090000000)

Five runs of each, after a warm-up, with /usr/bin/time -f "%e" on this container (4 CPUs, GHC 9.10.3, -O1):

1 worker: 0.21 0.20 0.20 0.20 0.21 min 0.20 median 0.20 4 workers: 0.08 0.08 0.09 0.09 0.09 min 0.08 median 0.09

Reading the numbers

This is the last lesson of the course. The article groups that follow are the reading: testing, Haskell against C++, and a modern project around a pure core.

Questions

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

  1. Why is the work forced in the worker?

    Answer

    A thunk put in a TVar would be computed later in the main thread

    Without the force the measurement would time the main thread.

  2. Why do the jobs have different costs?

    Answer

    A shared constant expression would be computed once, not per job

    A CAF is shared, so equal literal costs would be one computation.

  3. What did the median fall from and to?

    Answer

    From 0.20 s to 0.09 s

    The five runs gave 0.20 for one worker and 0.09 for four.

  4. What does this program print? module Main (main) where main :: IO () main = print (4 * 2 :: Int)

    Answer

    8

    Four times two is eight.

  5. Why is the speedup not four on four workers?

    Answer

    The last job bounds the run and the costs are unequal

    The tail of the schedule decides the total time.

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

  7. Which statistic is reported for the five runs?

    Answer

    The minimum and the median

    The same two statistics as the performance lesson.

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

    Answer

    8

    The range has eight elements.

  9. What is the result sum the same in both runs?

    Answer

    The pool computed the same jobs, only the schedule differs

    Concurrency changes the time, not the answer.

  10. What does this program print? module Main (main) where main :: IO () main = print (null [1 :: Int] :: Bool)

    Answer

    False

    The list has an element.

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

Banner image