Difference between revisions of "Concurrency"

From HaskellWiki
Jump to navigation Jump to search
m (>_<)
 
(25 intermediate revisions by 11 users not shown)
Line 1: Line 1:
  +
[[Category:GHC|Concurrency]]
= Concurrent programming in GHC =
 
  +
[[Category:Parallel]]
   
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 Haskell. If you're more interested in performance than non-determinism, learn about [[parallelism]] first.
   
  +
For practicality, the content is GHC-centric at the moment, although this may change as Haskell evolves.
Please feel free to add stuff here (Edit page link at the bottom).
 
   
== Starting points ==
+
== Overview ==
   
  +
GHC provides multi-scale support for parallel and concurrent programming, from very fine-grained, small "sparks", to coarse-grained explicit threads and locks, along with other models of concurrent and parallel programming, including actors, CSP-style concurrency, nested data parallelism and Intel Concurrent Collections. Synchronization between tasks is possible via messages, regular Haskell variables, MVar shared state or transactional memory.
* '''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]
 
</p>
 
* '''Software Transactional Memory''' (STM) is a new way to coordinate concurrent threads. There's a separate [[Software transactional memory|Wiki page devoted to STM]].
 
: STM was added to GHC 6.4, and is described in the paper [http://research.microsoft.com/~simonpj/papers/stm/index.htm Composable memory transactions]. The paper [http://research.microsoft.com/~simonpj/papers/stm/lock-free.htm Lock-free data structures using Software Transactional Memory in Haskell] gives further examples of concurrent programming using STM.
 
   
  +
== Getting started ==
* '''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, [http://research.microsoft.com/~simonpj/Papers/conc-ffi/index.htm Extending the Haskell Foreign Function Interface with Concurrency]. The GHC Commentary [http://www.cse.unsw.edu.au/~chak/haskell/ghc/comm/rts-libs/multi-thread.html Supporting multi-threaded interoperation] contains more detailed explanation of cooperation between FFI calls and multi-threaded runtime.
 
   
  +
The most important (as of 2010) to get to know are the basic "concurrent Haskell" model of threads using forkIO and MVars, the use of transactional memory via STM.
* '''Nested Data Parallelism'''. For an approach to exploiting the implicit parallelism in array programs for multiprocessors, see [[GHC/Data Parallel Haskell|Data Parallel Haskell]] (work in progress).
 
   
  +
See "[https://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.13.9123&rep=rep1&type=pdf Tackling the Awkward Squad]" to get started.
   
  +
From there, try the [[Parallel/Reading|reading list for parallelism in Haskell]].
== Using concurrency in GHC ==
 
   
  +
== Digging deeper ==
* 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].
 
   
 
* '''Software Transactional Memory''' (STM) is a newer way to coordinate concurrent threads. There's a separate [[Software transactional memory|Wiki page devoted to STM]].
* The GHC manual gives a few useful flags that control scheduling (not usually necessary) [http://www.haskell.org/ghc/docs/latest/html/users_guide/sec-using-parallel.html#parallel-rts-opts RTS options].
 
 
: STM was added to GHC 6.4, and is described in the paper [https://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.67.3686&rep=rep1&type=pdf Composable memory transactions]. The paper [https://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.64.1678&rep=rep1&type=pdf Lock-free data structures using STM 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, [https://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.80.4811&rep=rep1&type=pdf Extending the Haskell Foreign Function Interface with Concurrency]. The GHC Commentary [http://darcs.haskell.org/ghc/docs/comm/rts-libs/multi-thread.html Supporting multi-threaded interoperation] contains more detailed explanation of cooperation between FFI calls and multi-threaded runtime.
   
 
== GHC concurrency specifics ==
== Multiprocessor 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].
As of version 6.5, GHC supports running programs in parallel on an SMP or multi-core machine. How to do it:
 
   
  +
{{GHC/Multicore}}
* You'll need to get a version of GHC that supports SMP. Either download a [http://www.haskell.org/ghc/dist/current/dist nightly snapshot distribution], or [http://hackage.haskell.org/trac/ghc/wiki/GhcDarcs get the sources] from darcs and build it yourself.
 
   
  +
Support for low-level parallelism features of modern processors is slowly coming along. As of version 7.8, GHC includes the ability to [https://ghc.haskell.org/trac/ghc/wiki/SIMD emit SIMD instructions], and also has a rudimentary ability to use [[AtomicMemoryOps|atomic memory operations]].
* You need to link your program using the <tt>-threaded</tt> switch. (NOTE: previously it was necessary to compile all code, including libraries, with the <tt>-smp</tt> switch, this is no longer the case. The <tt>-smp</tt> flag is now a synonym for <tt>-threaded</tt>).
 
   
  +
== Alternative approaches ==
* Run the program with <tt>+RTS -N2</tt> to use 2 threads, for example. You should use a <tt>-N</tt> value equal to the number of CPU cores on your machine (not including Hyper-threading cores).
 
   
  +
* [http://www.cs.kent.ac.uk/projects/ofa/chp/ CHP]: CSP-style concurrency for Haskell.
* Concurrent threads (<tt>forkIO</tt> and <tt>forkOS</tt>) will run in parallel, and you can also use the <tt>par</tt> combinator and Strategies from the [http://www.haskell.org/ghc/docs/latest/html/libraries/base/Control-Parallel-Strategies.html Control.Parallel.Strategies] module to create parallelism.
 
   
  +
== See also ==
* Use <tt>+RTS -sstderr</tt> for timing stats.
 
   
  +
* [[Parallel]] portal
== Links to related work on parallel and distributed Haskell (many based on GHC) ==
 
  +
* Parallelism and concurrency [[Parallel/Research|research]]
 
* [http://www.macs.hw.ac.uk/~dsg/gph/ Glasgow Parallel Haskell]
 
* [http://www.macs.hw.ac.uk/~dsg/gdh/ Glasgow Distributed Haskell]
 
* http://www-i2.informatik.rwth-aachen.de/~stolz/dhs/
 
* http://www.informatik.uni-kiel.de/~fhu/PUBLICATIONS/1999/ifl.html
 
* [http://www.mathematik.uni-marburg.de/~eden Eden]
 
 
----
 

Latest revision as of 06:21, 12 June 2023


This page contains notes and information about how to write concurrent programs in Haskell. If you're more interested in performance than non-determinism, learn about parallelism first.

For practicality, the content is GHC-centric at the moment, although this may change as Haskell evolves.

Overview

GHC provides multi-scale support for parallel and concurrent programming, from very fine-grained, small "sparks", to coarse-grained explicit threads and locks, along with other models of concurrent and parallel programming, including actors, CSP-style concurrency, nested data parallelism and Intel Concurrent Collections. Synchronization between tasks is possible via messages, regular Haskell variables, MVar shared state or transactional memory.

Getting started

The most important (as of 2010) to get to know are the basic "concurrent Haskell" model of threads using forkIO and MVars, the use of transactional memory via STM.

See "Tackling the Awkward Squad" to get started.

From there, try the reading list for parallelism in Haskell.

Digging deeper

  • Software Transactional Memory (STM) is a newer 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 STM in Haskell gives further examples of concurrent programming using STM.

GHC concurrency specifics

You get access to concurrency operations by importing the library Control.Concurrent.

Since 2004, GHC supports running programs in parallel on an SMP or multi-core machine. How to do it:

  • Compile your program using the -threaded switch.
  • Run the program with +RTS -N2 to use 2 threads, for example (RTS stands for runtime system; see the GHC users' guide). You should use a -N value equal to the number of CPU cores on your machine (not including Hyper-threading cores). As of GHC v6.12, you can leave off the number of cores and all available cores will be used (you still need to pass -N however, like so: +RTS -N).
  • Concurrent threads (forkIO) 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.
  • To debug parallel program performance, use ThreadScope.

Support for low-level parallelism features of modern processors is slowly coming along. As of version 7.8, GHC includes the ability to emit SIMD instructions, and also has a rudimentary ability to use atomic memory operations.

Alternative approaches

  • CHP: CSP-style concurrency for Haskell.

See also