Difference between revisions of "Concurrent Programming"

From Gridkaschool
(Update for 2015 tutorial)
Line 1: Line 1:
==Under Construction==
 
 
''Caveat Emptor'' these pages are still being adapted for the 2015 school, so don't trust the exact links/content yet!
 
 
 
== Introduction ==
 
== Introduction ==
   
Line 16: Line 12:
 
=== Threaded Building Blocks ===
 
=== Threaded Building Blocks ===
   
Threaded Building Blocks is a higher level C++ that supports many common design patterns for applications to take advantage of multi-threading capabilities. Basic patterns like parallel loops and reductions are available. We'll also look at the TBB graph constructs.
+
Threaded Building Blocks is a higher level C++ template library that supports many common design patterns for applications to take advantage of multi-threading capabilities. Basic patterns like parallel loops and reductions are available. We'll also look at the TBB graph constructs.
   
 
TBB includes useful containers, which are thread safe, as well as a high performance multi-threaded implementation of malloc.
 
TBB includes useful containers, which are thread safe, as well as a high performance multi-threaded implementation of malloc.
Line 22: Line 18:
 
=== Talk ===
 
=== Talk ===
   
The introductory talk to this tutorial is [https://drive.google.com/file/d/0BxzUzuvjpkLNYjNkZHZjOWZyUms/edit?usp=sharing here].
+
The introductory talk to this tutorial is [FIXME here].
   
 
== Setup ==
 
== Setup ==
Line 28: Line 24:
 
=== General ===
 
=== General ===
   
  +
The machines we'll use in the tutorial are RedHat Enterprise 7 machines. On these the native C++ compiler is gcc4.8.3,
It's important to setup the Redhat ''devtoolset'' to have access to a
 
modern version of gcc for all of the tutorial exercises.
+
which has C++11 support and supports all the tutorial exercises fine.
   
  +
As as aside, if you want the very latest C++14 standard support, gcc5.2 is recommended, but for this tutorial C++11 contains
To do this login and execute this command:
 
  +
all we need.
 
<code>
 
$ scl enable devtoolset-2 bash
 
</code>
 
 
You can verify that all is well by asking for the gcc version, which
 
should be 4.8.2:
 
 
<code>
 
$ g++ --version
 
g++ (GCC) 4.8.2 20140120 (Red Hat 4.8.2-15)
 
</code>
 
   
  +
''If you wish to use your own machine to do the tutorial exercises then please make sure your gcc version is 4.8 at minimum!''
   
 
=== C++11 Compiler Options ===
 
=== C++11 Compiler Options ===
Line 53: Line 39:
 
* <code>-pthread</code> - Enable posix thread support, which is the underlying thread library used by libstdc++ on linux platforms
 
* <code>-pthread</code> - Enable posix thread support, which is the underlying thread library used by libstdc++ on linux platforms
   
You might well find this [https://github.com/graeme-a-stewart/cpp-concurrency/blob/gridka14/src/cpp11/Makefile Makefile]
+
You might well find this [https://github.com/graeme-a-stewart/cpp-concurrency/blob/gridka15/src/cpp11/Makefile Makefile]
 
useful. It will compile any <code>.cc</code> file into an executable with the correct
 
useful. It will compile any <code>.cc</code> file into an executable with the correct
 
compiler flags.
 
compiler flags.
Line 61: Line 47:
 
=== Threaded Building Blocks ===
 
=== Threaded Building Blocks ===
   
Intel Threaded Building Blocks has been compiled and installed into
+
Intel Threaded Building Blocks v4.4 has been compiled and installed into
 
<code>/usr/local</code> (specifically <code>/usr/local/include</code> for header files and
 
<code>/usr/local</code> (specifically <code>/usr/local/include</code> for header files and
 
<code>/usr/local/lib64</code> for libraries). Thus you shouldn't need any special
 
<code>/usr/local/lib64</code> for libraries). Thus you shouldn't need any special
extra setup to use is during the school. (Actually TBB is very easy to install from source.)
+
extra setup to use is during the school. (In fact TBB is very easy to install from source.)
   
 
Compiling against TBB should just work, but linking requires <code>-ltbb</code> and <code>-lrt</code>.
 
Compiling against TBB should just work, but linking requires <code>-ltbb</code> and <code>-lrt</code>.
Line 74: Line 60:
 
[https://www.threadingbuildingblocks.org/ Intel documentation] is
 
[https://www.threadingbuildingblocks.org/ Intel documentation] is
 
more convenient.
 
more convenient.
  +
  +
''If you have a machine or distribution on which TBB is installed, and you intend to use that, please make sure it's an up to date version, at least v4.2.''
   
 
== Writing Code ==
 
== Writing Code ==
Line 83: Line 71:
 
* vi - The other classic editor from the [http://en.wikipedia.org/wiki/Editor_war editor wars]
 
* vi - The other classic editor from the [http://en.wikipedia.org/wiki/Editor_war editor wars]
 
* geany - A nice standalone GUI editor with indentation and code highlighting, but you will need to switch on X forwarding to use it (so <code>ssh -Y -p 24 multithr@gks-XXX.scc.kit.edu</code>)
 
* geany - A nice standalone GUI editor with indentation and code highlighting, but you will need to switch on X forwarding to use it (so <code>ssh -Y -p 24 multithr@gks-XXX.scc.kit.edu</code>)
 
We have also setup working VNC on these machines, if you would prefer to use a VNC client from your laptop.
 
   
 
If you want us to really recommend something then we pick <code>geany</code> ;-)
 
If you want us to really recommend something then we pick <code>geany</code> ;-)
Line 92: Line 78:
 
Introductions to each section of the tutorial are here:
 
Introductions to each section of the tutorial are here:
   
https://github.com/graeme-a-stewart/cpp-concurrency/tree/gridka14/doc
+
https://github.com/graeme-a-stewart/cpp-concurrency/tree/gridka15/doc
   
 
and exercises are at the end of each document.
 
and exercises are at the end of each document.
Line 98: Line 84:
 
The file
 
The file
   
https://github.com/graeme-a-stewart/cpp-concurrency/blob/gridka14/doc/XX-Answers.md
+
https://github.com/graeme-a-stewart/cpp-concurrency/blob/gridka15/doc/XX-Answers.md
   
 
lists the source code that is the solution to each of the exercises. Feel free to look at that if you need a hint or want to check your answer.
 
lists the source code that is the solution to each of the exercises. Feel free to look at that if you need a hint or want to check your answer.
Line 104: Line 90:
 
You can clone the git repository like this:
 
You can clone the git repository like this:
   
<code>git clone https://github.com/graeme-a-stewart/cpp-concurrency.git -b gridka14</code>
+
<code>git clone https://github.com/graeme-a-stewart/cpp-concurrency.git -b gridka15</code>
   
N.B. This will clone the GridKA 2014 version of the repository, if you want to clone the master branch then just do
+
N.B. This will clone the GridKA 2015 version of the repository, if you want to clone the master branch then just do
   
 
<code>git clone https://github.com/graeme-a-stewart/cpp-concurrency.git</code>
 
<code>git clone https://github.com/graeme-a-stewart/cpp-concurrency.git</code>

Revision as of 21:06, 7 September 2015

Introduction

This tutorial consists of two sections:

  • C++11 Concurrency
  • Intel Threaded Building Blocks

C++11 Concurrency

C++11 introduced direct support in the language for concurrency, with the ability to launch and manage different threads. Support for mutexes and other synchronisation mechanisms are available.

Threaded Building Blocks

Threaded Building Blocks is a higher level C++ template library that supports many common design patterns for applications to take advantage of multi-threading capabilities. Basic patterns like parallel loops and reductions are available. We'll also look at the TBB graph constructs.

TBB includes useful containers, which are thread safe, as well as a high performance multi-threaded implementation of malloc.

Talk

The introductory talk to this tutorial is [FIXME here].

Setup

General

The machines we'll use in the tutorial are RedHat Enterprise 7 machines. On these the native C++ compiler is gcc4.8.3, which has C++11 support and supports all the tutorial exercises fine.

As as aside, if you want the very latest C++14 standard support, gcc5.2 is recommended, but for this tutorial C++11 contains all we need.

If you wish to use your own machine to do the tutorial exercises then please make sure your gcc version is 4.8 at minimum!

C++11 Compiler Options

To compile concurrent C++11 programs you'll need two flags for g++:

  • -std=c++11 - Use the new C++11 standards
  • -pthread - Enable posix thread support, which is the underlying thread library used by libstdc++ on linux platforms

You might well find this Makefile useful. It will compile any .cc file into an executable with the correct compiler flags.

A good reference for C++ concurrency libraries is http://www.cplusplus.com/ (use the reference section, under multi-threading).

Threaded Building Blocks

Intel Threaded Building Blocks v4.4 has been compiled and installed into /usr/local (specifically /usr/local/include for header files and /usr/local/lib64 for libraries). Thus you shouldn't need any special extra setup to use is during the school. (In fact TBB is very easy to install from source.)

Compiling against TBB should just work, but linking requires -ltbb and -lrt.

The Makefile here will add all the flags you need, and enable -std=c++11 so it's recommended.

Documentation for TBB is installed into /usr/local/share/tbb/html, but using the Intel documentation is more convenient.

If you have a machine or distribution on which TBB is installed, and you intend to use that, please make sure it's an up to date version, at least v4.2.

Writing Code

You will have been given access to a machine on which to complete the tutorial. There are a suite of editors available and you can pick whichever you like best:

  • nano - A very simple text editor (but lacks helpful things such as automatic indentation and code colourisation)
  • emacs - The classic Stallman text editor
  • vi - The other classic editor from the editor wars
  • geany - A nice standalone GUI editor with indentation and code highlighting, but you will need to switch on X forwarding to use it (so ssh -Y -p 24 multithr@gks-XXX.scc.kit.edu)

If you want us to really recommend something then we pick geany ;-)

Exercises

Introductions to each section of the tutorial are here:

https://github.com/graeme-a-stewart/cpp-concurrency/tree/gridka15/doc

and exercises are at the end of each document.

The file

https://github.com/graeme-a-stewart/cpp-concurrency/blob/gridka15/doc/XX-Answers.md

lists the source code that is the solution to each of the exercises. Feel free to look at that if you need a hint or want to check your answer.

You can clone the git repository like this:

git clone https://github.com/graeme-a-stewart/cpp-concurrency.git -b gridka15

N.B. This will clone the GridKA 2015 version of the repository, if you want to clone the master branch then just do

git clone https://github.com/graeme-a-stewart/cpp-concurrency.git

References

https://github.com/graeme-a-stewart/cpp-concurrency/blob/master/doc/00-TipsReferences.md

Is the current 'master' copy of the introduction, which provides references.