Programming Multicores
with
Pthreads and OpenMP
Nikos P. Pitsianis
nikos@cs.duke.edu
Xiaobai Sun
Bo Zhang
Duke University
Outline
Programming with Threads
Embarrassingly Parallel (Pleasantly Parallel)
Critical Sections (Mutual Exclusion)
Data Dependent Task Parallelism (Condition Variables & Signals)
Duke University
What is a thread?
Process:
a program that is running
an address space with 1 or more threads executing within the same
address space, and the required system resources for those threads
Thread:
a sequence of control within a process
shares the resources in that process
Duke University
Process
Process: a program in running
a single address space
one or more threads executing
within that address space
Duke University
Threads
Thread:
a sequence of control
within a process
Duke University
Disadvantages :
writing multithreaded programs is harder
more difficult to debug than single threaded programs
Duke University
Outline
Programming with Threads
Embarrassingly Parallel (Pleasantly Parallel)
Critical Sections (Mutual Exclusion)
Data Dependent Task Parallelism (Condition Variables & Signals)
Duke University
Source:
www.cs.duke.edu/~nikos/mpw/dp0.c
Compile:
gcc D N=1024 O4 dp0.c o dp0
Run:
./dp0
Duke University
Source:
www.cs.duke.edu/~nikos/mpw/dp1.c
Compile:
gcc pthread D N=1024 O4 dp1.c o dp1
Run:
./dp1
Duke University
Source:
www.cs.duke.edu/~nikos/mpw/dp1.c
Compile:
gcc pthread D N=1024 O4 dp1.c o dp1
Run:
./dp1
10
Duke University
pthread_create(
pthread_t * tid,
const pthread_attr_t * attr,
void *(*func)(void *),
void * arg);
func is the function to be called.
When func() returns the thread is terminated
11
Duke University
a*r,
12
Duke University
Thread Lifespan
Once a thread is created
it starts executing the function func()
func)) is an argument passed to pthread_create()
13
Duke University
Detached:
on its termination all resources used by the thread are released
A detached thread cannot be joined
pthread_join
14
Duke University
15
Duke University
Source:
www.cs.duke.edu/~nikos/mpw/dp2.c
Compile:
gccD NTHREADS=8 D N=1024 \
pthread O4 dp2.c o dp2
Run:
./dp2
16
Duke University
Outline
Programming with Threads
Embarrassingly Parallel (Pleasantly Parallel)
Critical Sections (Mutual Exclusion)
Data Dependent Task Parallelism (Condition Variables & Signals)
17
Duke University
Mutual Exclusion
Mutual Exclusion primitives protect against races
Read-Update-Write
pthread_mutex_t mux;
pthread_mutex_init (&mux, NULL);
pthread_mutex_lock (&mux);
pthread_mutex_unlock (&mux);
18
Duke University
To lock :
pthread_mutex_lock(pthread_mutex_t &);
To unlock :
pthread_mutex_unlock(pthread_mutex_t &);
Both functions are blocking
19
Duke University
Source:
www.cs.duke.edu/~nikos/mpw/dp3.c
Compile:
gccD NTHREADS=8 D N=1024 \
pthread O4 dp3.c o dp3
Run:
./dp3
20
Duke University
Source:
www.cs.duke.edu/~nikos/mpw/dp3.c
Compile:
gccD NTHREADS=8 D N=1024 \
pthread O4 dp3.c o dp3
Run:
./dp3
21
Duke University
Outline
Programming with Threads
Embarrassingly Parallel (Pleasantly Parallel)
Critical Sections (Mutual Exclusion)
Data Dependent Task Parallelism (Condition Variables & Signals)
22
Duke University
Condition Variables
Condition variables allow one thread to
wait for (sleep until) an event generated by any other thread
23
Duke University
Condition Variables
Condition variables are used with a mutex
pthread_cond_wait(pthread_cond_t *cptr,
pthread_mutex_t *mptr);
pthread_cond_signal(pthread_cond_t *cptr);
24
Duke University
Source:
www.cs.duke.edu/~nikos/mpw/dp4.c
Compile:
gccD NTHREADS=8 D N=1024 \
pthread O4 dp4.c o dp4
Run:
./dp4
25
Duke University
Source:
www.cs.duke.edu/~nikos/mpw/dp4.c
Compile:
gccD NTHREADS=8 D N=1024 \
pthread O4 dp4.c o dp4
Run:
./dp4
26
Duke University
Outline
Programming with Threads
Embarrassingly Parallel (Pleasantly Parallel)
Critical Sections (Mutual Exclusion)
Data Dependent Task Parallelism (Condition Variables & Signals)
27
Duke University
OpenMP
A set of compiler directives and library routines for parallel
application programmers
OMP simplifies writing multi-threaded programs in Fortran, C
and C++
Most of the constructs in OpenMP are compiler directives
#pragma omp construct [clause [clause]]
#pragma omp parallel num_threads(4)
28
Duke University
Example in OpenMP
#include <omp.h>!
#include <stdlib.h>!
#include <stdio.h>!
!
void getvec (double *a);!
!
double dotprod (double *a, double *b, int n) {!
int i;!
double s = 0.0;!
!
#pragma omp parallel for reduction(+:s)!
for ( i = 0; i < n; i++ ) !
s += a[i]*b[i];!
return s;!
}!
!
int main () {!
double *a, *b;!
!
a = (double *) malloc(sizeof(double)*N);!
b = (double *) malloc(sizeof(double)*N);!
!
getvec(a); getvec(b);!
!
omp_set_num_threads(NTHREADS);!
double dp = dotprod(a,b,n);!
printf("%f\n", dp);!
}!
!
!
Source:
www.cs.duke.edu/~nikos/mpw/dp0-omp.c
Compile:
gcc D NTHREADS=8 D N=1024 \
fopenmp O4 dp0-omp.c o dp0-omp
Run:
./dp0-omp
29
Duke University
structured_block!
30
Duke University
31
Duke University
Directive Responsibility
Work-sharing
Data scoping
Synchronization
Scheduling
32
Duke University
Directive Responsibility
Work-sharing
Data scoping
Synchronization
Scheduling
33
Duke University
Directive Responsibility
Work-sharing
Data scoping
Synchronization
Scheduling
Signaling
Conditional Wait waits for
some event; signals when it
occurs
Broadcasting signals a
group of waiting threads
Multicore Programming Workshop
34
Duke University
Directive Responsibility
Work-sharing
Data scoping
Synchronization
Scheduling
35
Duke University
References
D. Butenhof, Programming with POSIX threads, Addison Wesley
(1997)
Online Tutorials from LLNL
https://computing.llnl.gov/tutorials/pthreads/
https://computing.llnl.gov/tutorials/openMP/
36