threads in cPP

download threads in cPP

of 28

Transcript of threads in cPP

  • 7/28/2019 threads in cPP

    1/28

    Threads in C++

    Gabor CzilliUniversity of Erlangen-Nuremberg - Advanced C++ Seminar

    July 11, 2011

    Gabor Czilli (CS Masters Student) Threads in C++ July 11, 2011 1 / 26

    http://find/
  • 7/28/2019 threads in cPP

    2/28

    Table of Contents

    1 Introduction & Motivation

    2 Pthreads (Posix Threads)Pthread example

    3 Boost ThreadsThread ManagementSynchronizationThread Pool

    4 OpenMPThread ManagementSynchronization

    5 Boost vs. OpenMP

    6 Summary / Conclusion

    Gabor Czilli (CS Masters Student) Threads in C++ July 11, 2011 2 / 26

    http://find/
  • 7/28/2019 threads in cPP

    3/28

    Introduction & MotivationWhy everyone wants Threads

    Processors do not get (much) faster anymoreBut the number of processing units increase on a single chip(2,4,8,16 core machines)

    As well as the number of simultaneous operations per clock cycleWe need to solve bigger and bigger problemsIn reasonable execution times

    So...Computational work has to be done in parallel,

    to take advantage of modern processors

    Gabor Czilli (CS Masters Student) Threads in C++ July 11, 2011 3 / 26

    http://find/
  • 7/28/2019 threads in cPP

    4/28

  • 7/28/2019 threads in cPP

    5/28

    PthreadsPosix Threads

    Posix: Portable Operating System Interface for UnixStandard API for Unix systems not available on WindowsC thread library

    i n t p t h r e a d c r e a t e ( p t h r e a d t t , c o n s t p t h r e a d a t t r t a t t r , v o i d(s t a r t r o u t i n e ) ( v o i d ) , v o i d ar g ) ;

    i n t p t h r e a d j o i n ( p t h r e a d t t , v o i d r e t v a l ) ;

    i n t p t h r e a d m u t e x i n i t ( p t hr e ad m u te x t mutex , c o n s t

    p t h re a d m u t ex a t t r t a t t r ) ;

    i n t p t h r e a d m u t e x d e s t r o y ( p t h r e ad m u t e x t mutex ) ;

    i n t p t h r e a d m u t e x l o c k ( p t h r e ad m u t e x t mutex ) ; // a nd same u n l o c kf u n c t i o n

    Gabor Czilli (CS Masters Student) Threads in C++ July 11, 2011 5 / 26

    http://find/
  • 7/28/2019 threads in cPP

    6/28

    PthreadsExample

    // l i n k w i th p t h r e a d# i n c l u d e < p t h r e a d . h ># i n c l u d e < i o s t r e a m >

    # d e f i n e NTHREADS 4

    s t a t i c p t h r e a d m u t e x t m ut ex 1 ;

    i n t main () {

    p t h r e a d m u t e x i n i t (& mu te x1 , 0 ) ;pt hr ea d t th re ad [NTHREADS] ;

    f o r ( i n t i = 0 ; i < NTHREADS; ++i )p t h r e a d c r e a t e ( t h re a d + i , 0 , t h r e a d s t a r t , ( v o i d ) i ) ;

    f o r ( i n t i = 0 ; i < NTHREADS; ++i )p t h r e a d j o i n ( t h r e a d [ i ] , 0 ) ;

    p t h r e a d m u t e x d e s t r o y (& mu te x1 ) ;

    r e t u r n 0 ;}

    Gabor Czilli (CS Masters Student) Threads in C++ July 11, 2011 6 / 26

    http://find/
  • 7/28/2019 threads in cPP

    7/28

    PthreadsExample

    s t a t i c v o i d t h r e a d s t a r t ( v o i d ar g ) {i n t i n d e x = ( i n t ) ar g ;

    p t h r e a d m u t e x l o c k (& mut ex1 ) ;s t d : : cou t

  • 7/28/2019 threads in cPP

    8/28

    Boost ThreadsGeneral

    Part of the next C++ standard

    Portable threading library (all systems boost supports)Uses e.g. pthreads in Linux and Windows-Threads in WindowsC++ API with support for RAII

    Gabor Czilli (CS Masters Student) Threads in C++ July 11, 2011 8 / 26

    http://goforward/http://find/http://goback/
  • 7/28/2019 threads in cPP

    9/28

    Boost ThreadsThread Management

    c l a s s boos t : : th re ad ;

    For launching and managing threadsConstructor takes class with function call operatormember f u n c t i o n b o o s t : : t h r e a d : : j o i n ( )

    Wait for thread to nish executionb o o st : : t h i s t h r e a d : : g e t i d ( )

    Non-member function, returns unique id representing the currentlyexecuting thread# i n c l u d e < boos t / th re ad . hpp >

    s t r u c t MyThread {v o i d o p e r a t o r ( ) ( ) {

    // new t h re a d s s t a r t h er e}

    } ;

    i n t main () {boost : : thre ad thr ead (MyThread () ) ;t h re ad . j o in ( ) ;

    r e t u r n 0 ;}Gabor Czilli (CS Masters Student) Threads in C++ July 11, 2011 9 / 26

    http://find/
  • 7/28/2019 threads in cPP

    10/28

    Boost ThreadsSynchronization

    c l a s s bo os t : : mutex ;

    Implements Lockable interface ( for lock(), unlock(), try lock())Should be used with a boost::lock for RAII resource management

    c l a s s b o o s t : : l o c k g u a r d ;

    Calls lock() in its constructor and unlock in destructorc l a s s b o o s t : : u n i q u e l o c k ;

    Constructor can wait for locking or try to lock, when additionallypassing boost::try to lock functions: owns lock, timed lock, unlock,

    release more exiblec l a s s b o o s t : : s h a r e d l o c k ;

    Used when only read access is needed for a threadUnderlying mutex can be shared e.g. with a unique lock

    Gabor Czilli (CS Masters Student) Threads in C++ July 11, 2011 10 / 26

    http://find/
  • 7/28/2019 threads in cPP

    11/28

    Boost ThreadsSynchronization example

    # i n c l u d e < boos t / th rea d . hpp >

    s t a t i c boos t : : sha red mutex mutex ;

    s t a t i c v oi d read ( ) {b o o s t : : s h a r e d l o c k < boost : : shared mutex > lo ck (mutex ) ;// do r e a d o n l y o p e r a t i o n s h e r e i n p a r a l l e l

    }

    s t a t i c v oi d w r i t e ( ) {b o o s t : : u n i q u e l o c k < boost : : shared mutex > lo ck (mutex ) ;// do w r i t e o p e r a t io n s h e re

    }

    i n t main () {boos t : : t h rea d th rea d1 ( r ead ) ;boos t : : t h rea d th rea d2 ( r ead ) ;boos t : : t h rea d th rea d3 ( r ead ) ;boos t : : t h read th rea d4 ( wr i t e ) ;

    t h rea d1 . j o in ( ) ;t h rea d2 . j o in ( ) ;t h rea d3 . j o in ( ) ;t h rea d4 . j o in ( ) ;

    r e t u r n 0 ;}

    Gabor Czilli (CS Masters Student) Threads in C++ July 11, 2011 11 / 26

    http://find/http://goback/
  • 7/28/2019 threads in cPP

    12/28

    Boost ThreadsSynchronization

    boos t : : l ock ( Lockab le1 , Lockab le2 , . . . ) ,

    boos t : : l oc k ( beg in , end )

    Non-member functions, acquire multiple locks in a way that avoidsdeadlocksc l a s s b o o st : : b a r r i e r ;boos t : : b a r r i e r : : wa i t ( ) ;

    Barriers are created for n threads, asthreads reach the barrier (call wait())they must wait until all n threads have

    arrived.c l a s s b oo st : : c o n d i t i o n v a r i a b l e ;b o o st : : c o n d i t i o n v a r i a b l e : : w a i t ( l o c k ) , n o t i f y o n e , n o t i f y a l l ;

    Provide a mechanism for one thread to wait for notication from

    another thread. Atomically unlocks the lock and starts sleeping.Gabor Czilli (CS Masters Student) Threads in C++ July 11, 2011 12 / 26

    http://find/http://goback/
  • 7/28/2019 threads in cPP

    13/28

    Boost ThreadsSynchronization

    b o os t : : c o n d i t i o n v a r i a b l e c on d ;boo st : : mutex mutex ;v o l a t i l e bo ol d a t a r e a d y ;

    v o i d c r e a t e d a t a ( ) {// . . .d a ta r e ad y = t r u e ;c on d . n o t i f y o n e ( ) ;// . . .

    }

    v o i d w a i t f o r d a t a t o p r o c e s s ( ) {// . . .b o o s t : : u n i q u e l o c k < bo os t : : mutex > lo ck (mutex) ;

    w h i l e ( ! d a t a r e a d y ) { // mutex i s l o c k ed h e r econd . wai t ( loc k ) ; // a t o m i c a l l y w a it and u n lo c k// mute x l o c k e d a g a i n

    }

    p r o c e s s d a t a ( ) ;// . . .

    }

    Gabor Czilli (CS Masters Student) Threads in C++ July 11, 2011 13 / 26

    http://find/
  • 7/28/2019 threads in cPP

    14/28

    Boost ThreadsThread Pool

    Concept of a thread pool

    Gabor Czilli (CS Masters Student) Threads in C++ July 11, 2011 14 / 26

    http://find/
  • 7/28/2019 threads in cPP

    15/28

    Boost ThreadsThread Pool

    Concept of a thread pool

    There is no implementation of a thread pool in Boost, but a verygood implementation based on Boost(http://threadpool.sourceforge.net)

    Gabor Czilli (CS Masters Student) Threads in C++ July 11, 2011 14 / 26

    http://find/
  • 7/28/2019 threads in cPP

    16/28

    OpenMPGeneral

    API for multi-threaded, shared memory parallelismBased on compiler support makes heavy use of compiler directives(#pragma omp)

    Portable to all platforms with supporting compilers, even whencompiler ignores directives(among others: Intel-, Microsoft-, GNU-, IBM- and Cray-Compilers)Number of threads determined by: enviroment variable, explicitfunction call or automatically by the number of processors (atruntime)Higher level multithreaded programming

    Gabor Czilli (CS Masters Student) Threads in C++ July 11, 2011 15 / 26

    http://find/
  • 7/28/2019 threads in cPP

    17/28

    OpenMPExample

    Matrix-Matrix multiplication:f o r ( i n t i = 0 ; i < n ; ++i )

    f o r ( i n t k = 0 ; k < n ; ++k )f o r ( i n t j = 0 ; j < n ; ++j )

    out [ in+j ] += a [ i n+k ] b [ kn+j ] ;

    Gabor Czilli (CS Masters Student) Threads in C++ July 11, 2011 16 / 26

    http://find/
  • 7/28/2019 threads in cPP

    18/28

    OpenMPExample

    Matrix-Matrix multiplication:f o r ( i n t i = 0 ; i < n ; ++i )f o r ( i n t k = 0 ; k < n ; ++k )

    f o r ( i n t j = 0 ; j < n ; ++j )out [ in+j ] += a [ i n+k ] b [ kn+j ] ;

    Parallel with OpenMp:# i n c l u d e < omp.h >

    // . . .

    #pragma omp p a r a l l e l f o r

    f o r ( i n t i = 0 ; i < n ; ++i )f o r ( i n t k = 0 ; k < n ; ++k )f o r ( i n t j = 0 ; j < n ; ++j )

    out [ in+j ] += a [ i n+k ] b [ kn+j ] ;

    // . . .

    Gabor Czilli (CS Masters Student) Threads in C++ July 11, 2011 16 / 26

    http://find/
  • 7/28/2019 threads in cPP

    19/28

    OpenMPManagement Directives

    #pragma omp p a r a l l e l{ / b l o c k / }

    Start team of threads, execute the block in parallel, join threads atthe end

    #pragma omp f o r{ / b l o c k / }

    Partition loop iterations, to be executed in parallel

    #pragma omp s e c t i o n s{

    #pragma omp se c t io n{ / b l o c k / }#pragma omp se c t io n

    { / b l o c k / }}

    Execute sections in parallel, each section once, by one thread

    Gabor Czilli (CS Masters Student) Threads in C++ July 11, 2011 17 / 26

    O MP

    http://find/
  • 7/28/2019 threads in cPP

    20/28

    OpenMPSynchronization Directives

    #pragma omp c r i t i c a l ( name){ / b l o c k / }

    Critical section, cannot be executed in parallel, identied by name

    #pragma omp b a r r i e r

    A thread will wait at the barrier until all other threads have reachedthis pointData scope attributes:sh a re d ( l i s t )p r i v a t e ( l i s t )

    Declare data to be shared between threads, or to give each thread alocal copy

    Gabor Czilli (CS Masters Student) Threads in C++ July 11, 2011 18 / 26

    B O MP

    http://find/
  • 7/28/2019 threads in cPP

    21/28

    Boost vs. OpenMPPerformance comparison

    On Linux, Boost is implemented with the help of pthreads...OpenMP tooPerformance comparison:

    How well do the implementations scale on a calculation intensiveproblem? (like matrix multiplication)Synchronization is often unaviodable, which locking mechanisms are

    implemented more efficiently? (when facing high lock contention)Problem 1: multithreaded calculation of a matrix-matrixmultiplicationProblem 2: like problem 1, but locking a critical section in eachiteration of the second loopHardware: 4x Intel Xeon CPU E7340 @ 2.40GHz server 16 cores,32GB RAMSoftware: g++ 4.4.5, options: -O3 -march=native -mtune=native-mfpmath=sseBoost version 1.42 (no change since 1.41)

    Gabor Czilli (CS Masters Student) Threads in C++ July 11, 2011 19 / 26

    B t O MP

    http://find/
  • 7/28/2019 threads in cPP

    22/28

    Boost vs. OpenMPResults Problem 1

    0

    50

    100

    150

    200

    250

    300

    0 100 200 300 40 0 500 600

    t i m e

    i n m s

    matrix siz e

    OpenMPBoost

    Single threaded

    Gabor Czilli (CS Masters Student) Threads in C++ July 11, 2011 20 / 26

    B t O MP

    http://find/http://find/
  • 7/28/2019 threads in cPP

    23/28

    Boost vs. OpenMPResults Problem 1

    0

    5

    10

    15

    20

    25

    30

    0 100 200 300 40 0 500 600

    t i m e

    i n m s

    matrix size

    OpenMPBoost

    Gabor Czilli (CS Masters Student) Threads in C++ July 11, 2011 21 / 26

    Boost vs OpenMP

    http://goback/http://find/http://find/http://goback/
  • 7/28/2019 threads in cPP

    24/28

    Boost vs. OpenMPResults Problem 2

    0

    50

    100

    150

    200

    250

    300

    350

    400

    0 100 200 300 40 0 500 600

    t i m e

    i n m s

    matrix siz e

    OpenMPBoostSingle threaded

    Gabor Czilli (CS Masters Student) Threads in C++ July 11, 2011 22 / 26

    Boost vs OpenMP

    http://goback/http://find/http://find/http://goback/
  • 7/28/2019 threads in cPP

    25/28

    Boost vs. OpenMPResults Problem 2, threads: 4

    0

    50

    100

    150

    200

    250

    300

    0 100 200 300 40 0 5 00 600

    t i m e

    i n m s

    matrix siz e

    OpenMPBoostSingle threaded

    Gabor Czilli (CS Masters Student) Threads in C++ July 11, 2011 23 / 26

    Summary / Conclusion

    http://find/http://find/
  • 7/28/2019 threads in cPP

    26/28

    Summary / Conclusion

    Pthreads:

    low level, medium portable interfacecan be of high performancebut a lot of manual resource management neededtherefore error-prone

    Boost threads

    medium level, highly portable interfacemedium performance, when lockingRAII style management of resourcesstill full exibility

    OpenMP

    high level, highly portable interfacehigh perfomance, if it ts to your applicationcompletely different approach to multithreadingdoes not save the programmer from thinking about deadlocks, raceconditions, synchronization and efficient parallel algorithms

    Gabor Czilli (CS Masters Student) Threads in C++ July 11, 2011 24 / 26

    http://find/
  • 7/28/2019 threads in cPP

    27/28

    Boost.Boost C++ Libraries , July 2011.

    http://www.boost.org .Boost.Boost C++ Libraries , July 2011.http://www.boost.org/doc/libs/1_46_1/doc/html/thread.html .

    OpenMP.Open Multi-Processing , July 2011.http://openmp.org .

    OpenMP.Open Multi-Processing , July 2011.https://computing.llnl.gov/tutorials/openMP .

    Gabor Czilli (CS Masters Student) Threads in C++ July 11, 2011 25 / 26

    http://www.boost.org/http://www.boost.org/doc/libs/1_46_1/doc/html/thread.htmlhttp://www.boost.org/doc/libs/1_46_1/doc/html/thread.htmlhttp://www.boost.org/doc/libs/1_46_1/doc/html/thread.htmlhttp://openmp.org/https://computing.llnl.gov/tutorials/openMPhttps://computing.llnl.gov/tutorials/openMPhttp://openmp.org/http://www.boost.org/doc/libs/1_46_1/doc/html/thread.htmlhttp://www.boost.org/doc/libs/1_46_1/doc/html/thread.htmlhttp://www.boost.org/http://find/
  • 7/28/2019 threads in cPP

    28/28

    Thank you for your attention.

    Questions?

    Gabor Czilli (CS Masters Student) Threads in C++ July 11, 2011 26 / 26

    http://goforward/http://find/http://goback/