/mobile Handheld Friendly website

 performance measurements

Each table row shows performance measurements for this Haskell GHC program with a particular command-line input value N.

 N  CPU secs Elapsed secs Memory KB Code B ≈ CPU Load
500,0000.080.08?306  0% 13% 33% 100%
5,000,0000.720.723,204306  0% 0% 1% 100%
50,000,0007.137.143,220306  0% 1% 1% 100%

Read the ↓ make, command line, and program output logs to see how this program was run.

Read thread-ring benchmark to see what this program should do.

 notes

The Glorious Glasgow Haskell Compilation System, version 7.8.2

503 threads are created with forkIO, with each thread creating one synchronised mutable variable (MVar) shared with the next thread in the ring. The last thread created returns an MVar to share with the first thread. Each thread reads from the MVar to its left, and writes to the MVar to its right.

Each thread then waits on a token to be passed from its neighbour. Tokens are then passed around the threads via the MVar chain N times, and the thread id of the final thread to receive a token is printed.

More information on Haskell concurrency and parallelism.

 thread-ring Haskell GHC program source code

-- The Computer Language Benchmarks Game
-- http://benchmarksgame.alioth.debian.org/
-- Contributed by Jed Brown with improvements by Spencer Janssen, Don Stewart and Alex Mason
--
-- Compile with: <ghc> --make -O2 -threaded threadring.ghc-4.hs -o threadring.ghc-4.ghc_run

import Control.Monad
import Control.Concurrent
import System.Environment
import GHC.Conc

ring = 503

new ret l i = do
  r <- newEmptyMVar
  forkOn numCapabilities (thread ret i l r)
  return r


thread :: MVar () -> Int -> MVar Int -> MVar Int -> IO ()
thread ret i l r = go
  where go = do
          m <- takeMVar l
          if m > 1
              then (putMVar r $! m - 1) >> go
              else print i >> putMVar ret ()

main = do
  a <- newMVar . read . head =<< getArgs
  ret <- newEmptyMVar
  z <- foldM (new ret) a [2..ring]
  forkOn numCapabilities (thread ret 1 z a)
  takeMVar ret

 make, command-line, and program output logs

Wed, 16 Apr 2014 21:51:21 GMT

MAKE:
mv threadring.ghc threadring.hs
/usr/local/src/ghc-7.8.2/bin/ghc --make -fllvm -O2 -XBangPatterns -rtsopts -threaded threadring.hs -o threadring.ghc_run
[1 of 1] Compiling Main             ( threadring.hs, threadring.o )
Linking threadring.ghc_run ...
rm threadring.hs
1.02s to complete and log all make actions

COMMAND LINE:
./threadring.ghc_run  50000000

PROGRAM OUTPUT:
292

Revised BSD license

  Home   Conclusions   License   Play