Difference between revisions of "Concurrency"
(Move problems to sub-page) |
BrettGiles (talk | contribs) m (Heading levels as per guidelines) |
||
Line 1: | Line 1: | ||
− | = Concurrent programming in GHC = |
+ | == Concurrent programming in GHC == |
This page contains notes and information about how to write concurrent programs in GHC. |
This page contains notes and information about how to write concurrent programs in GHC. |
||
Line 5: | Line 5: | ||
Please feel free to add stuff here (Edit page link at the bottom). |
Please feel free to add stuff here (Edit page link at the bottom). |
||
− | == Starting points == |
+ | === Starting points === |
* '''Basic concurrency: forkIO and MVars'''. Read [http://research.microsoft.com/Users/simonpj/papers/marktoberdorf/marktoberdorf.ps.gz Tackling the awkward squad: monadic input/output, concurrency, exceptions, and foreign-language calls in Haskell].<p>The [http://www.haskell.org/ghc/docs/papers/concurrent-haskell.ps.gz original paper about Concurrent Haskell] contains quite a few examples about how to write concurrent programs. A larger example is [http://www.haskell.org/~simonmar/papers/web-server.ps.gz Writing High-Performance Server Applications in Haskell, Case Study: A Haskell Web Server] |
* '''Basic concurrency: forkIO and MVars'''. Read [http://research.microsoft.com/Users/simonpj/papers/marktoberdorf/marktoberdorf.ps.gz Tackling the awkward squad: monadic input/output, concurrency, exceptions, and foreign-language calls in Haskell].<p>The [http://www.haskell.org/ghc/docs/papers/concurrent-haskell.ps.gz original paper about Concurrent Haskell] contains quite a few examples about how to write concurrent programs. A larger example is [http://www.haskell.org/~simonmar/papers/web-server.ps.gz Writing High-Performance Server Applications in Haskell, Case Study: A Haskell Web Server] |
||
Line 17: | Line 17: | ||
− | == Using concurrency in GHC == |
+ | === Using concurrency in GHC === |
* You get access to concurrency operations by importing the library [http://www.haskell.org/ghc/docs/latest/html/libraries/base/Control-Concurrent.html Control.Concurrent]. |
* You get access to concurrency operations by importing the library [http://www.haskell.org/ghc/docs/latest/html/libraries/base/Control-Concurrent.html Control.Concurrent]. |
||
Line 24: | Line 24: | ||
− | == Multiprocessor GHC == |
+ | === Multiprocessor GHC === |
As of version 6.5, GHC supports running programs in parallel on an SMP or multi-core machine. How to do it: |
As of version 6.5, GHC supports running programs in parallel on an SMP or multi-core machine. How to do it: |
||
Line 38: | Line 38: | ||
* Use <tt>+RTS -sstderr</tt> for timing stats. |
* Use <tt>+RTS -sstderr</tt> for timing stats. |
||
− | == Links to related work on parallel and distributed Haskell (many based on GHC) == |
+ | === Links to related work on parallel and distributed Haskell (many based on GHC) === |
* [http://www.macs.hw.ac.uk/~dsg/gph/ Glasgow Parallel Haskell] |
* [http://www.macs.hw.ac.uk/~dsg/gph/ Glasgow Parallel Haskell] |
||
Line 46: | Line 46: | ||
* [http://www.mathematik.uni-marburg.de/~eden Eden] |
* [http://www.mathematik.uni-marburg.de/~eden Eden] |
||
− | = Problems with GHC implementation = |
+ | == Problems with GHC implementation == |
There are ciritical differences between the description in the paper |
There are ciritical differences between the description in the paper |
Revision as of 20:04, 8 December 2006
Concurrent programming in GHC
This page contains notes and information about how to write concurrent programs in GHC.
Please feel free to add stuff here (Edit page link at the bottom).
Starting points
- Basic concurrency: forkIO and MVars. Read Tackling the awkward squad: monadic input/output, concurrency, exceptions, and foreign-language calls in Haskell.
The original paper about Concurrent Haskell contains quite a few examples about how to write concurrent programs. A larger example is Writing High-Performance Server Applications in Haskell, Case Study: A Haskell Web Server
- Software Transactional Memory (STM) is a new way to coordinate concurrent threads. There's a separate Wiki page devoted to STM.
- STM was added to GHC 6.4, and is described in the paper Composable memory transactions. The paper Lock-free data structures using Software Transactional Memory in Haskell gives further examples of concurrent programming using STM.
- Foreign function interface. If you are calling foreign functions in a concurrent program, you need to know about bound threads. They are described in a Haskell workshop paper, Extending the Haskell Foreign Function Interface with Concurrency. The GHC Commentary Supporting multi-threaded interoperation contains more detailed explanation of cooperation between FFI calls and multi-threaded runtime.
- Nested Data Parallelism. For an approach to exploiting the implicit parallelism in array programs for multiprocessors, see Data Parallel Haskell (work in progress).
Using concurrency in GHC
- You get access to concurrency operations by importing the library Control.Concurrent.
- The GHC manual gives a few useful flags that control scheduling (not usually necessary) RTS options.
Multiprocessor GHC
As of version 6.5, GHC supports running programs in parallel on an SMP or multi-core machine. How to do it:
- You'll need to get a version of GHC that supports SMP. Either download a nightly snapshot distribution, or get the sources from darcs and build it yourself.
- You need to link your program using the -threaded switch. (NOTE: previously it was necessary to compile all code, including libraries, with the -smp switch, this is no longer the case. The -smp flag is now a synonym for -threaded).
- Run the program with +RTS -N2 to use 2 threads, for example. You should use a -N value equal to the number of CPU cores on your machine (not including Hyper-threading cores).
- Concurrent threads (forkIO and forkOS) will run in parallel, and you can also use the par combinator and Strategies from the Control.Parallel.Strategies module to create parallelism.
- Use +RTS -sstderr for timing stats.
- Glasgow Parallel Haskell
- Glasgow Distributed Haskell
- http://www-i2.informatik.rwth-aachen.de/~stolz/dhs/
- http://www.informatik.uni-kiel.de/~fhu/PUBLICATIONS/1999/ifl.html
- Eden
Problems with GHC implementation
There are ciritical differences between the description in the paper "Asynchronous exceptions in Haskell by Simon Marlow, Simon Peyton Jones, Andy Moran and John Reppy, PLDI'01." the implementation in GHC 6.4 and GHC 6.6 today.
Some of the bad effects are described here under throwTo & block statements considered harmful.