Академический Документы
Профессиональный Документы
Культура Документы
OpenMP*
Tim Mattson
Intel Corp.
timothy.g.mattson@intel.com
* The name OpenMP is the property of the OpenMP Architecture Review Board.
Introduction
OpenMP is one of the most common parallel
programming models in use today.
It is relatively easy to use which makes a great
language to start with when learning to write
parallel software.
Assumptions:
We assume you know C. OpenMP supports Fortran
and C++, but we will restrict ourselves to C.
We assume you are new to parallel programming.
We assume you have access to a compiler that
supports OpenMP (more on that later).
2
Acknowledgements
This course is based on a long series of tutorials presented at
Supercomputing conferences. The following people helped
prepare this content:
J. Mark Bull (the University of Edinburgh)
Rudi Eigenmann (Purdue University)
Barbara Chapman (University of Houston)
Larry Meadows, Sanjiv Shah, and Clay Breshears (Intel Corp).
Preliminaries:
Our plan ... Active learning!
We will mix short lectures with short exercises.
Outline
Unit 1: Getting started with OpenMP
Mod1: Introduction to parallel programming
Mod 2: The boring bits: Using an OpenMP compiler (hello world)
Disc 1: Hello world and how threads work
Unit 5: Recapitulation
Outline
Unit 1: Getting started with OpenMP
Mod1: Introduction to parallel programming
Mod 2: The boring bits: Using an OpenMP compiler (hello world)
Disc 1: Hello world and how threads work
Unit 5: Recapitulation
Moore's Law
Moores Law
25
Pentium 4 (Wmt)
Power
20
power = perf ^ 1.74
15
10
Pentium Pro
5
i486
0
0
Growth in power
is unsustainable
Pentium M
Pentium
4
6
Scalar Performance
8
10
30
Pentium 4 (Psc)
25
Pentium 4 (Wmt)
Power
20
Mobile CPUs
with shallow
pipelines use less
power
15
10
Pentium Pro
5
i486
0
0
Pentium M
Pentium
4
6
Scalar Performance
8
11
f
Capacitance = C
Voltage = V
Frequency = f
Power = CV2f
Output
q = CV
q = CV so
W = CV2
power is work over time or how many
times in a second we oscillate the circuit
Power = W* F
Power = CV2f
Output
Processor
Output
f
Capacitance = C
Voltage = V
Frequency = f
Power = CV2f
Input
f/2
Processor
f/2
Capacitance = 2.2C
Voltage = 0.6V
Frequency = 0.5f
Power = 0.396CV2f
Chandrakasan, A.P.; Potkonjak, M.; Mehra, R.; Rabaey, J.; Brodersen, R.W.,
"Optimizing power using transformations," IEEE Transactions on Computer-Aided
Design of Integrated Circuits and Systems,, vol.14, no.1, pp.12-31, Jan 1995
Source:
Vishwani Agrawal
Microprocessor trends
Individual processors are many core (and often heterogeneous) processors.
48 cores
80 cores
10 cores
16 wide SIMD
30 cores
8 wide SIMD
NVIDIA Tesla
C1060
1 CPU + 6 cores
4 cores
Intel Xeon processor
3rd
IBM Cell
4 cores
ARM MPCORE
The result
+
15
Concurrent
Programs
Parallel
Programs
Figure from An Introduction to Concurrency in Programming Languages by J. Sottile, Timothy G. Mattson, and Craig E Rasmussen, 2010
Original Problem
Implementation
strategy
Program SPMD_Emb_Par ()
{
Program SPMD_Emb_Par
()
TYPE
*tmp,SPMD_Emb_Par
{*func();
Program
()
global_array
Data(TYPE);
TYPE
*tmp,
{*func();
Program
SPMD_Emb_Par
()
global_array
Res(TYPE);
global_array
Data(TYPE);
TYPE
*tmp,
{*func();
int global_array
N =global_array
get_num_procs();
Res(TYPE);
Data(TYPE);
TYPE
*tmp,
*func();
intint
idglobal_array
N= =get_proc_id();
get_num_procs();
Res(TYPE);
global_array
Data(TYPE);
if (id==0) setup_problem(N,DATA);
intint
idglobal_array
N= =get_proc_id();
get_num_procs();
Res(TYPE);
for
(int I=setup_problem(N,DATA);
0;
if
(id==0)
int
id
= get_proc_id();
intI<N;I=I+Num){
Num
= get_num_procs();
tmp
=int
for
(int
I=setup_problem(N,DATA);
0;func(I);
I<N;I=I+Num){
if
(id==0)
id = get_proc_id();
Res.accumulate(
tmp);
tmp
for
(int
I==setup_problem(N,
0;func(I);
I<N;I=I+Num){
if (id==0)
Data);
}
Res.accumulate(
tmp);
= func(I);
for (inttmp
I= ID;
I<N;I=I+Num){
} tmp
} = func(I,tmp);
Res.accumulate(
Data);
} }
Res.accumulate(
tmp);
} }
}
Corresponding source
code
OpenMP* Overview:
C$OMP FLUSH
C$OMP THREADPRIVATE(/ABC/)
An API for
Writing
Multithreaded
C$OMP OpenMP:
parallel do shared(a,
b, c)
call omp_test_lock(jlok)
Applications
C$OMP
C$OMP ATOMIC
C$OMP MASTER
SINGLE
PRIVATE(X)
A set
of compiler
directives and library
setenv OMP_SCHEDULE dynamic
C$OMP DO lastprivate(XX)
Nthrds = OMP_GET_NUM_PROCS()
omp_set_lock(lck)
* The name OpenMP is the property of the OpenMP Architecture Review Board.
20
OpenMP library
Environment
variables
Proc2
Proc3
ProcN
Outline
Unit 1: Getting started with OpenMP
Mod1: Introduction to parallel programming
Mod 2: The boring bits: Using an OpenMP compiler (hello world)
Disc 1: Hello world and how threads work
Unit 5: Recapitulation
23
24
26
int main()
{
int ID = 0;
27
Linux and OS X
gcc -fopenmp
PGI Linux
pgcc -mp
Intel windows
icl /Qopenmp
icpc openmp
{
int ID = 0;
}
}
Outline
Unit 1: Getting started with OpenMP
Mod1: Introduction to parallel programming
Mod 2: The boring bits: Using an OpenMP compiler (hello world)
Disc 1: Hello world and how threads work
Unit 5: Recapitulation
29
Exercise 1: Solution
int ID = omp_get_thread_num();
printf( hello(%d) , ID);
printf( world(%d) \n, ID);
}
}
Proc1
Proc2
Proc3
ProcN
32/36
L2
L2
L2
L2
L2
L2
L3
33/36
Proc1
Proc2
Proc3
ProcN
funcA() var1
var2
text
main()
funcA()
funcB()
.....
data
heap
array1
array2
Stack Pointer
Program Counter
Registers
Process
An instance of a
program execution.
The execution
context of a running
program i.e. the
resources associated
with a programs
execution.
Process ID
User ID
Group ID
Files
Locks
Sockets
35/36
funcA() var1
var2
Stack Pointer
Program Counter
Registers
Thread 1
Stack
funcB() var1
var2
var3
Stack Pointer
Program Counter
Registers
text
main()
funcA()
funcB()
.....
data
heap
array1
array2
Threads:
Threads are "light
weight processes
Threads share
Process state among
multiple threads
this greatly reduces
the cost of switching
context.
Process ID
User ID
Group ID
Files
Locks
Sockets
36/36
37/36
Exercise 1: Solution
world(0)
int ID = omp_get_thread_num();
printf( hello(%d) , ID);
printf( world(%d) \n, ID);
}
}
OpenMP Overview:
Outline
Unit 1: Getting started with OpenMP
Mod1: Introduction to parallel programming
Mod 2: The boring bits: Using an OpenMP compiler (hello world)
Disc 1: Hello world and how threads work
Unit 5: Recapitulation
40
Outline
Unit 1: Getting started with OpenMP
Mod1: Introduction to parallel programming
Mod 2: The boring bits: Using an OpenMP compiler (hello world)
Disc 1: Hello world and how threads work
41
Parallel Regions
Sequential Parts
A Nested
Parallel
region
42
Runtime function to
double A[1000];
request a certain
omp_set_num_threads(4);
number of threads
#pragma omp parallel
{
int ID = omp_get_thread_num();
pooh(ID,A);
Runtime function
}
returning a thread ID
43
double A[1000];
44
pooh(0,A)
pooh(1,A)
printf(all done\n);
pooh(2,A)
pooh(3,A)
* The name OpenMP is the property of the OpenMP Architecture Review Board
void thunk ()
{
foobar ();
}
pthread_t tid[4];
for (int i = 1; i < 4; ++i)
pthread_create (
&tid[i],0,thunk, 0);
thunk();
for (int i = 1; i < 4; ++i)
pthread_join (tid[i]);
Exercises 2 to 4:
Numerical Integration
Mathematically, we know that:
1
4.0
4.0
(1+x2)
dx =
2.0
F(xi) x
i=0
0.0
1.0
Exercise 2
Create a parallel version of the pi program
using a parallel construct.
Pay close attention to shared versus private
variables.
In addition to a parallel construct, you will need
the runtime library routines
int omp_get_num_threads();
int omp_get_thread_num();
double omp_get_wtime();
49
Outline
Unit 1: Getting started with OpenMP
Mod1: Introduction to parallel programming
Mod 2: The boring bits: Using an OpenMP compiler (hello world)
Disc 1: Hello world and how threads work
Unit 5: Recapitulation
50
Serial PI Program
static long num_steps = 100000;
double step;
int main ()
{
int i; double x, pi, sum = 0.0;
step = 1.0/(double) num_steps;
#include <omp.h>
Promote scalar to an
static long num_steps = 100000;
double step;
array dimensioned by
number of threads to
#define NUM_THREADS 2
avoid race condition.
void main ()
{
int i, nthreads; double pi, sum[NUM_THREADS];
step = 1.0/(double) num_steps;
omp_set_num_threads(NUM_THREADS);
#pragma omp parallel
{
Only one thread should copy
int i, id,nthrds;
the number of threads to the
double x;
global value to make sure
id = omp_get_thread_num();
multiple threads writing to the
nthrds = omp_get_num_threads();
same address dont conflict.
if (id == 0) nthreads = nthrds;
for (i=id, sum[id]=0.0;i< num_steps; i=i+nthrds) {
This is a common
x = (i+0.5)*step;
trick in SPMD
sum[id] += 4.0/(1.0+x*x);
programs to create
a cyclic distribution
}
of loop iterations
}
for(i=0, pi=0.0;i<nthreads;i++)pi += sum[i] * step;
}
52
Algorithm strategy:
The SPMD (Single Program Multiple Data) design pattern
Results*
Original Serial pi program with 100000000 steps ran in 1.83 seconds.
threads
1st
SPMD
1.86
1.03
1.08
0.97
*Intel compiler (icpc) with no optimization on Apple OS X 10.7.3 with a dual core (four HW
thread) Intel CoreTM i5 processor at 1.7 Ghz and 4 Gbyte DDR3 memory at 1.333 Ghz.
54
False sharing
If independent data elements happen to sit on the same cache line, each
update will cause the cache lines to slosh back and forth between threads
This is called false sharing.
HW thrd. 0
HW thrd. 1
HW thrd. 2
L1 $ lines
L1 $ lines
Core 0
Sum[0]
HW thrd. 3
Core 1
Sum[0]
#include <omp.h>
static long num_steps = 100000;
double step;
#define PAD
8
// assume 64 byte L1 cache line size
#define NUM_THREADS 2
void main ()
{
int i, nthreads; double pi, sum[NUM_THREADS][PAD];
step = 1.0/(double) num_steps;
omp_set_num_threads(NUM_THREADS);
#pragma omp parallel
Pad the array
so each sum
{
int i, id,nthrds;
value is in a
double x;
different
id = omp_get_thread_num();
cache line
nthrds = omp_get_num_threads();
if (id == 0) nthreads = nthrds;
for (i=id, sum[id]=0.0;i< num_steps; i=i+nthrds) {
x = (i+0.5)*step;
sum[id][0] += 4.0/(1.0+x*x);
}
}
for(i=0, pi=0.0;i<nthreads;i++)pi += sum[i][0] * step;
}
56
threads
1st
SPMD
1st
SPMD
padded
1.86
1.86
1.03
1.01
1.08
0.69
0.97
0.53
*Intel compiler (icpc) with no optimization on Apple OS X 10.7.3 with a dual core (four HW
thread) Intel CoreTM i5 processor at 1.7 Ghz and 4 Gbyte DDR3 memory at 1.333 Ghz.
57
58
Outline
Unit 1: Getting started with OpenMP
Mod1: Introduction to parallel programming
Mod 2: The boring bits: Using an OpenMP compiler (hello world)
Disc 1: Hello world and how threads work
Unit 5: Recapitulation
59
OpenMP Overview:
Synchronization:
Synchronization: bringing one or more threads to a well defined and
known point in their execution.
The two most common forms of synchronization are:
Synchronization
High level synchronization:
critical
atomic
barrier
ordered
Synchronization is used
to impose order
constraints and to
protect access to shared
data
Discussed
later
62
Synchronization: Barrier
Barrier: Each thread waits until all threads arrive.
#pragma omp parallel
{
int id=omp_get_thread_num();
A[id] = big_calc1(id);
#pragma omp barrier
B[id] = big_calc2(id, A);
}
63
Synchronization: critical
Mutual exclusion: Only one thread at a time
can enter a critical region.
float res;
#pragma omp parallel
Threads wait
their turn
only one at a
time calls
consume()
nthrds = omp_get_num_threads();
for(i=id;i<niters;i+=nthrds){
B = big_job(i);
#pragma omp critical
res += consume (B);
}
}
64
{
double tmp, B;
B = DOIT();
tmp = big_ugly(B);
#pragma omp atomic
X += tmp;
}
Additional forms of atomic were added in OpenMP 3.1.
We will discuss these later.
65
Exercise 3
In exercise 2, you probably used an array to
create space for each thread to store its partial
sum.
If array elements happen to share a cache line,
this leads to false sharing.
Non-shared data in the same cache line so each
update invalidates the cache line in essence
sloshing independent data back and forth
between threads.
Outline
Unit 1: Getting started with OpenMP
Mod1: Introduction to parallel programming
Mod 2: The boring bits: Using an OpenMP compiler (hello world)
Disc 1: Hello world and how threads work
Unit 5: Recapitulation
67
1st
SPMD
1.86
1.03
1.08
0.97
*Intel compiler (icpc) with no optimization on Apple OS X 10.7.3 with a dual core (four HW
thread) Intel CoreTM i5 processor at 1.7 Ghz and 4 Gbyte DDR3 memory at 1.333 Ghz.
68
Example: Using a
#include <omp.h>
static long num_steps = 100000;
double step;
#define NUM_THREADS 2
void main ()
{
double pi;
step = 1.0/(double) num_steps;
omp_set_num_threads(NUM_THREADS);
#pragma omp parallel
Create a scalar local to
{
each thread to
accumulate partial
int i, id,nthrds; double x, sum;
sums.
id = omp_get_thread_num();
nthrds = omp_get_num_threads();
if (id == 0) nthreads = nthrds;
id = omp_get_thread_num();
nthrds = omp_get_num_threads();
for (i=id, sum=0.0;i< num_steps; i=i+nthreads){
No array, so
x = (i+0.5)*step;
no false
sharing.
sum += 4.0/(1.0+x*x);
}
Sum goes out of scope beyond the parallel
#pragma omp critical
region so you must sum it in here. Must
pi += sum * step;
protect summation into pi in a critical region
}
so updates dont conflict
}
69
threads
1st
SPMD
1st
SPMD
padded
SPMD
critical
1.86
1.86
1.87
1.03
1.01
1.00
1.08
0.69
0.68
0.97
0.53
0.53
*Intel compiler (icpc) with no optimization on Apple OS X 10.7.3 with a dual core (four HW
thread) Intel CoreTM i5 processor at 1.7 Ghz and 4 Gbyte DDR3 memory at 1.333 Ghz.
70
Example: Using a
#include <omp.h>
static long num_steps = 100000;
double step;
#define NUM_THREADS 2
void main ()
{
double pi;
step = 1.0/(double) num_steps;
omp_set_num_threads(NUM_THREADS);
#pragma omp parallel
Be careful
{
int i, id,nthrds; double x;
where you put
id = omp_get_thread_num();
a critical
nthrds = omp_get_num_threads();
section
if (id == 0) nthreads = nthrds;
id = omp_get_thread_num();
nthrds = omp_get_num_threads();
for (i=id, sum=0.0;i< num_steps; i=i+nthreads){ What would happen if
x = (i+0.5)*step;
you put the critical
section inside the loop?
#pragma omp critical
pi += 4.0/(1.0+x*x);
}
}
pi *= step;
}
71
72
Outline
Unit 1: Getting started with OpenMP
Mod1: Introduction to parallel programming
Mod 2: The boring bits: Using an OpenMP compiler (hello world)
Disc 1: Hello world and how threads work
Unit 5: Recapitulation
73
Discussed later
74
Loop construct
name:
C/C++: for
Fortran: do
OpenMP parallel
region
OpenMP parallel
region and a
worksharing for
construct
76
The schedule clause affects how loop iterations are mapped onto
threads
schedule(static [,chunk])
Deal-out blocks of iterations of size chunk to each thread.
schedule(dynamic[,chunk])
Each thread grabs chunk iterations off a queue until all iterations
have been handled.
schedule(guided[,chunk])
Threads dynamically grab blocks of iterations. The size of the block
starts large and shrinks down to size chunk as the calculation
proceeds.
schedule(runtime)
Schedule and chunk size taken from the OMP_SCHEDULE
environment variable (or the runtime library).
schedule(auto)
Schedule is left up to the runtime to choose (does not have to be any
of the above).
77
When To Use
STATIC
Pre-determined and
predictable by the
programmer
DYNAMIC
Unpredictable, highly
variable work per
iteration
GUIDED
AUTO
Least work at
runtime :
scheduling
done at
compile-time
Most work at
runtime :
complex
scheduling
logic used at
run-time
78
Remove loop
carried
dependence
int i, A[MAX];
#pragma omp parallel for
for (i=0;i< MAX; i++) {
int j = 5 + 2*(i+1);
A[i] = big(j);
}
80
Nested loops
For perfectly nested rectangular loops we can parallelize
multiple loops in the nest with the collapse clause:
#pragma omp parallel for collapse(2)
for (int i=0; i<N; i++) {
Number of
for (int j=0; j<M; j++) {
loops to be
.....
parallelized,
counting from
}
the outside
}
Will form a single loop of length NxM and then
parallelize that.
Useful if N is O(no. of threads) so parallelizing the
outer loop makes balancing the load difficult.
81
Reduction
How do we handle this case?
double ave=0.0, A[MAX]; int i;
for (i=0;i< MAX; i++) {
ave + = A[i];
}
ave = ave/MAX;
Reduction
OpenMP reduction clause:
reduction (op : list)
Inside a parallel or a work-sharing construct:
A local copy of each list variable is made and initialized
depending on the op (e.g. 0 for +).
Updates occur on the local copy.
Local copies are reduced into a single value and
combined with the original global value.
Operator
+
*
min
max
Initial value
0
1
0
Largest pos. number
Most neg. number
C/C++ only
~0
&&
||
Fortran Only
Operator
.AND.
.OR.
.NEQV.
.IEOR.
.IOR.
.IAND.
.EQV.
Initial value
.true.
.false.
.false.
0
0
All bits on
.true.
84
85
Outline
Unit 1: Getting started with OpenMP
Mod1: Introduction to parallel programming
Mod 2: The boring bits: Using an OpenMP compiler (hello world)
Disc 1: Hello world and how threads work
Unit 5: Recapitulation
86
Serial PI Program
static long num_steps = 100000;
double step;
int main ()
{
int i; double x, pi, sum = 0.0;
step = 1.0/(double) num_steps;
1st
SPMD
1st
SPMD
padded
SPMD
critical
PI Loop
1.86
1.86
1.87
1.91
1.03
1.01
1.00
1.02
1.08
0.69
0.68
0.80
0.97
0.53
0.53
0.68
*Intel compiler (icpc) with no optimization on Apple OS X 10.7.3 with a dual core (four HW
thread) Intel CoreTM i5 processor at 1.7 Ghz and 4 Gbyte DDR3 memory at 1.333 Ghz.
89
Parallel loops
OpenMP 3.0 guarantees that this works i.e. that the same
schedule is used in the two loops:
!$omp do schedule(static)
do i=1,n
a(i) = ....
end do
!$omp end do nowait
!$omp do schedule(static)
do i=1,n
.... = a(i)
end do
90
Loops (cont.)
Made schedule(runtime) more useful
can get/set it with library routines
omp_set_schedule()
omp_get_schedule()
91
Outline
Unit 1: Getting started with OpenMP
Mod1: Introduction to parallel programming
Mod 2: The boring bits: Using an OpenMP compiler (hello world)
Disc 1: Hello world and how threads work
Unit 5: Recapitulation
92
Outline
Unit 1: Getting started with OpenMP
Mod1: Introduction to parallel programming
Mod 2: The boring bits: Using an OpenMP compiler (hello world)
Disc 1: Hello world and how threads work
Unit 5: Recapitulation
93
Synchronization: Barrier
Barrier: Each thread waits until all threads arrive.
#pragma omp parallel shared (A, B, C) private(id)
{
id=omp_get_thread_num();
A[id] = big_calc1(id);
implicit barrier at the end of a
#pragma omp barrier
for worksharing construct
#pragma omp for
for(i=0;i<N;i++){C[i]=big_calc3(i,A);}
#pragma omp for nowait
for(i=0;i<N;i++){ B[i]=big_calc2(C, i); }
A[id] = big_calc4(id);
}
no implicit barrier
implicit barrier at the end
due to nowait
of a parallel region
94
Master Construct
The master construct denotes a structured
block that is only executed by the master thread.
The other threads just skip it (no
synchronization is implied).
#pragma omp parallel
{
do_many_things();
#pragma omp master
{ exchange_boundaries(); }
#pragma omp barrier
do_many_other_things();
}
95
97
omp_init_lock(), omp_set_lock(),
omp_unset_lock(), omp_test_lock(),
omp_destroy_lock()
Nested Locks
A lock implies a
memory fence (a
flush) of all
thread visible
variables
omp_init_nest_lock(), omp_set_nest_lock(),
omp_unset_nest_lock(), omp_test_nest_lock(),
omp_destroy_nest_lock()
Note: a thread always accesses the most recent copy of the
lock, so you dont need to use a flush on the lock variable.
98
omp_set_num_threads(), omp_get_num_threads(),
omp_get_thread_num(), omp_get_max_threads()
Are we in an active parallel region?
omp_in_parallel()
Do you want the system to dynamically vary the number of
threads from one parallel construct to another?
omp_set_dynamic, omp_get_dynamic();
How many processors in the system?
omp_num_procs()
101
Environment Variables
Set the default number of threads to use.
OMP_NUM_THREADS int_literal
OpenMP added an environment variable to control the size of
child threads stack
OMP_STACKSIZE
Also added an environment variable to hint to runtime how to
treat idle threads
OMP_WAIT_POLICY
ACTIVE keep threads alive at barriers/locks
PASSIVE try to release processor at barriers/locks
Outline
Unit 1: Getting started with OpenMP
Mod1: Introduction to parallel programming
Mod 2: The boring bits: Using an OpenMP compiler (hello world)
Disc 1: Hello world and how threads work
Unit 5: Recapitulation
103
Data environment:
double A[10];
int main() {
int index[10];
#pragma omp parallel
work(index);
printf(%d\n, index[0]);
}
A, index, count
A, index and count are
shared by all threads.
temp is local to each
thread
temp
temp
temp
A, index, count
105
Data sharing:
106
tmp is 0 here
107
unspecified which
copy of tmp
108
Firstprivate Clause
Variables initialized from shared variable
C++ objects are copy-constructed
incr = 0;
#pragma omp parallel for firstprivate(incr)
for (i = 0; i <= MAX; i++) {
if ((i%2)==0) incr++;
A[i] = incr;
}
Each thread gets its own copy
of incr with an initial value of 0
109
Lastprivate Clause
Variables update shared variable using value
from last iteration
C++ objects are updated as if by assignment
void sq2(int n, double *lastterm)
{
double x; int i;
#pragma omp parallel for lastprivate(x)
for (i = 0; i < n; i++){
x = a[i]*a[i] + b[i]*b[i];
b[i] = sqrt(x);
x has the value it held
}
for the last sequential
*lastterm = x;
iteration (i.e., for i=(n-1))
}
110
Data Sharing:
variables: A = 1,B = 1, C = 1
#pragma omp parallel private(B) firstprivate(C)
Are A,B,C local to each thread or shared inside the parallel region?
What are their initial values inside and values after the parallel region?
itotal = 1000
C$OMP PARALLEL DEFAULT(PRIVATE) SHARED(itotal)
np = omp_get_num_threads()
each = itotal/np
These two
code
fragments are
equivalent
113
114
Exercise 5 (cont.)
Once you have a working version, try to
optimize the program?
Try different schedules on the parallel loop.
Try different mechanisms to support mutual
exclusion.
115
Outline
Unit 1: Getting started with OpenMP
Mod1: Introduction to parallel programming
Mod 2: The boring bits: Using an OpenMP compiler (hello world)
Disc 1: Hello world and how threads work
Unit 5: Recapitulation
116
void testpoint(void){
struct d_complex z;
int iter;
double temp;
z=c;
for (iter=0; iter<MXITR; iter++){
temp = (z.r*z.r)-(z.i*z.i)+c.r;
z.i = z.r*z.i*2+c.i;
z.r = temp;
if ((z.r*z.r+z.i*z.i)>4.0) {
numoutside++;
break;
}
}
}
Using
default(none)
generates a
compiler
error that j is
unspecified.
118
int main(){
int i, j;
double area, error, eps = 1.0e-5;
#pragma omp parallel for default(shared) private(c, j) \
firstpriivate(eps)
for (i=0; i<NPOINTS; i++) {
for (j=0; j<NPOINTS; j++) {
c.r = -2.0+2.5*(double)(i)/(double)(NPOINTS)+eps;
}
c.i = 1.125*(double)(j)/(double)(NPOINTS)+eps;
Other errors found using a
testpoint(c);
debugger or by inspection:
}
eps was not initialized
}
Protect updates of numoutside
area=2.0*2.5*1.125*(double)(NPOINTS*NPOINTS Which value of c die testpoint()
numoutside)/(double)(NPOINTS*NPOINTS);
error=area/(double)NPOINTS;
see? Global or private?
119
Serial PI Program
What is the
minimum change I
can make to this
code to parallelize
it?
}
120
double step;
For good OpenMP
implementations,
reduction is more
scalable than critical.
void main ()
{
int i; double x, pi, sum = 0.0;
step = 1.0/(double) num_steps;
#pragma omp parallel for private(x) reduction(+:sum)
for (i=0;i< num_steps; i++){
x = (i+0.5)*step;
i private by
sum = sum + 4.0/(1.0+x*x);
default
}
Note: we created a
pi = step * sum;
parallel program without
changing any executable
}
code and by adding 2
simple lines of text!
121
Outline
Unit 1: Getting started with OpenMP
Mod1: Introduction to parallel programming
Mod 2: The boring bits: Using an OpenMP compiler (hello world)
Disc 1: Hello world and how threads work
Unit 5: Recapitulation
122
Where variable_list is a
comma separated list of
variables
Print the value of the macro
_OPENMP
And its value will be
yyyymm
123
124
125
Outline
Unit 1: Getting started with OpenMP
Mod1: Introduction to parallel programming
Mod 2: The boring bits: Using an OpenMP compiler (hello world)
Disc 1: Hello world and how threads work
Unit 5: Recapitulation
126
list traversal
When we first created OpenMP, we focused on common use
cases in HPC Fortran arrays processed over regular
loops.
Recursion and pointer chasing were so far removed from
our Fortan focus that we didnt even consider more general
structures.
Hence, even a simple list traversal is exceedingly difficult
with the original versions of OpenMP.
p=head;
while (p) {
process(p);
p = p->next;
}
127
One Thread
Two Threads
Results on an Intel dual core 1.83 GHz CPU, Intel IA-32 compiler 10.1 build 2
45 seconds
28 seconds
128
One Thread
Two Threads
C++, (static,1)
C, (static,1)
37 seconds
47 seconds
49 seconds
32 seconds
45 seconds
28 seconds
Results on an Intel dual core 1.83 GHz CPU, Intel IA-32 compiler 10.1 build 2
129
Conclusion
We were able to parallelize the linked list
traversal but it was ugly and required
multiple passes over the data.
To move beyond its roots in the array based
world of scientific computing, we needed to
support more general data structures and
loops beyond basic for loops.
To do this, we added tasks in OpenMP 3.0
130
Outline
Unit 1: Getting started with OpenMP
Mod1: Introduction to parallel programming
Mod 2: The boring bits: Using an OpenMP compiler (hello world)
Disc 1: Hello world and how threads work
Unit 5: Recapitulation
131
Outline
Unit 1: Getting started with OpenMP
Mod1: Introduction to parallel programming
Mod 2: The boring bits: Using an OpenMP compiler (hello world)
Disc 1: Hello world and how threads work
Unit 5: Recapitulation
132
OpenMP Tasks
Tasks are independent units of work.
Tasks are composed of:
code to execute
data environment
internal control variables (ICV)
Serial
Parallel
Definitions
Task construct task directive plus structured
block
Task the package of code and instructions
for allocating data created when a thread
encounters a task construct
Task region the dynamic sequence of
instructions produced by the execution of a
task by a thread
134
or task barriers
#pragma omp taskwait
135
x is a private variable
y is a private variable
140
Outline
Unit 1: Getting started with OpenMP
Mod1: Introduction to parallel programming
Mod 2: The boring bits: Using an OpenMP compiler (hello world)
Disc 1: Hello world and how threads work
Unit 5: Recapitulation
141
142
Execution of tasks
Have potential to parallelize irregular patterns and recursive function calls
Single
Threaded
Thr1
Block 1
Block 1
Block 2
Task 2
Idle
Block 2
Task 3
Idle
Block 3
Block 3
Thr3
Thr4
Block 2
Task 1
Block 2
Task 1
Block 2
Task 2
Thr2
Time
Time
Saved
Block 2
Task 3
C11
C10
+=
A0
A1
AT0 AT1
AT
Note: the iteration sweeps through C and A, creating a new block of rows to be
updated with new parts of A. These updates are completely independent.
Tze Meng Low, Kent Milfeld, Robert van de Geijn, and Field Van Zee. Parallelizing FLAME
Code with OpenMP Task Queues. TOMS , submitted.
145
146
} // end of task-queue
} // end of parallel region
147
syrk_ln (var2)
x 10
Reference
FLAME
OpenFLAME_nth1
OpenFLAME_nth2
OpenFLAME_nth3
OpenFLAME_nth4
MFLOPS/sec.
1.5
0.5
200
400
600
800
1000
1200
matrix dimension n
1400
1600
1800
2000
Note: the above graphs is for the most nave way of marching through the matrices.
By picking blocks dynamically, much faster ramp-up can be achieved.
148
Outline
Unit 1: Getting started with OpenMP
Mod1: Introduction to parallel programming
Mod 2: The boring bits: Using an OpenMP compiler (hello world)
Disc 1: Hello world and how threads work
Unit 5: Recapitulation
149
Shared memory
a
cache1
cache2
cache3
proc1
proc2
proc3
...
cacheN
procN
a
A memory model is defined in terms of:
Coherence: Behavior of the memory system when a single
address is accessed by multiple threads.
Consistency: Orderings of reads, writes, or synchronizations
(RWS) with various addresses and by multiple threads.
150
Wa Wb Ra Rb . . .
compiler
Code order
Executable code
Wb Rb Wa Ra . . .
thread
thread
private view
RWs in any
semantically
equivalent order
private view
threadprivate
b
Commit order
b a
threadprivate
memory
151
Consistency
Sequential Consistency:
In a multi-processor, ops (R, W, S) are sequentially
consistent if:
They remain in program order for each
processor.
They are seen to be in the same overall order by
each of the other processors.
Program order = code order = commit order
Relaxed consistency:
Remove some of the ordering constraints for
memory ops (R, W, S).
153
154
Flush
Defines a sequence point at which a thread is
guaranteed to see a consistent view of memory with
respect to the flush set.
The flush set is:
all thread visible variables for a flush construct without an
argument list.
a list of variables when the flush(list) construct is used.
156
157
159
Outline
Unit 1: Getting started with OpenMP
Mod1: Introduction to parallel programming
Mod 2: The boring bits: Using an OpenMP compiler (hello world)
Disc 1: Hello world and how threads work
Unit 5: Recapitulation
160
Example: prod_cons.c
Parallelize a producer consumer program
One thread produces values that another thread consumes.
int main()
{
double *A, sum, runtime;
int flag = 0;
A = (double *)malloc(N*sizeof(double));
runtime = omp_get_wtime();
fill_rand(N, A);
162
164
{x binop = expr;
{v=x;
{v = x; x++;}
{v=x;
{++x;
v=x:}
{x++;
v = x;}
{v = x;
x--;}
{v= x;
--x;}
{--x;
v = x;}
{x--;
v = x;}
v = x;}
++x:}
The capture semantics in atomic were added to map onto common hardware
supported atomic ops and to support modern lock free algorithms.
165
166
Outline
Unit 1: Getting started with OpenMP
Mod1: Introduction to parallel programming
Mod 2: The boring bits: Using an OpenMP compiler (hello world)
Disc 1: Hello world and how threads work
Unit 5: Recapitulation
167
168
169
170
void main()
{
int Nsize, choice;
#pragma omp parallel private (Nsize, choice)
{
#pragma omp single copyprivate (Nsize, choice)
input_parameters (Nsize, choice);
do_work(Nsize, choice);
}
}
171
= 2.8
N=100
= 3.16
N= 1000
= 3.148
172
Exercise 9
We provide three files for this exercise
pi_mc.c: the monte carlo method pi program
random.c: a simple random number generator
random.h: include file for random number generator
Extra Credit:
Make your random number generator numerically correct (nonoverlapping sequences of pseudo-random numbers).
173
Outline
Unit 1: Getting started with OpenMP
Mod1: Introduction to parallel programming
Mod 2: The boring bits: Using an OpenMP compiler (hello world)
Disc 1: Hello world and how threads work
Unit 5: Recapitulation
174
= 2.8
N=100
= 3.16
N= 1000
= 3.148
176
#include omp.h
static long num_trials = 10000;
int main ()
{
long i;
long Ncirc = 0;
double pi, x, y;
double r = 1.0; // radius of circle. Side of squrare is 2*r
seed(0,-r, r); // The circle and square are centered at the origin
#pragma omp parallel for private (x, y) reduction (+:Ncirc)
for(i=0;i<num_trials; i++)
{
x = random();
y = random();
if ( x*x + y*y) <= r*r) Ncirc++;
}
pi = 4.0 * ((double)Ncirc/(double)num_trials);
printf("\n %d trials, pi is %f \n",num_trials, pi);
}
177
178
LCG code
static long MULTIPLIER = 1366;
static long ADDEND
= 150889;
static long PMOD
= 714025;
long random_last = 0;
double random ()
{
long random_next;
return ((double)random_next/(double)PMOD);
}
179
1
1
0.1
LCG - one thread
0.01
0.001
0.0001
0.00001
LCG, 4 threads,
trail 1
LCG 4 threads,
trial 2
LCG, 4 threads,
trial 3
Program written using the Intel C/C++ compiler (10.0.659.2005) in Microsoft Visual studio 2005 (8.0.50727.42) and running on a dual-core
laptop (Intel T2400 @ 1.83 Ghz with 2 GB RAM) running Microsoft Windows XP.
180
random_last carries
state between random
number computations,
To make the generator
threadsafe, make
random_last
threadprivate so each
thread has its own copy.
181
1
0.1
0.01
LCG - one
thread
LCG 4 threads,
trial 1
LCT 4 threads,
trial 2
0.001
LCG 4 threads,
trial 3
0.0001
LCG 4 threads,
thread safe
Thread safe
version gives the
same answer
each time you
run the program.
0.00001
182
183
184
One thread
2 threads
4 threads
1000
3.156
3.156
3.156
10000
3.1168
3.1168
3.1168
100000
3.13964
3.13964
3.13964
1000000
3.140348
3.140348
3.140348
10000000
3.141658
3.141658
3.141658
186
Outline
Unit 1: Getting started with OpenMP
Mod1: Introduction to parallel programming
Mod 2: The boring bits: Using an OpenMP compiler (hello world)
Disc 1: Hello world and how threads work
Unit 5: Recapitulation
187
Summary
We have now covered the most commonly used features of
OpenMP.
To close, lets consider some of the key parallel design
patterns weve discussed..
188
190
Loop parallelism
Collections of tasks are defined as iterations of one or more
loops.
Loop iterations are divided between a collection of
processing elements to compute tasks in parallel.
OpenMP PI Program:
pi = sum * step;
192
int main ()
{
int i;
double step, pi, sum;
step = 1.0/(double) num_steps;
#pragma omp parallel
{
#pragma omp single
sum = pi_comp(0,num_steps,step);
}
pi = step * sum;
}
else{
iblk = Nfinish-Nstart;
#pragma omp task shared(sum1)
sum1 = pi_comp(Nstart,
Nfinish-iblk/2,step);
#pragma omp task shared(sum2)
sum2 = pi_comp(Nfinish-iblk/2, Nfinish,
step);
#pragma omp taskwait
sum = sum1 + sum2;
}return sum;
}
19
threads
1st
SPMD
SPMD
critical
PI Loop
Pi tasks
1.86
1.87
1.91
1.87
1.03
1.00
1.02
1.00
1.08
0.68
0.80
0.76
0.97
0.53
0.68
0.52
*Intel compiler (icpc) with no optimization on Apple OS X 10.7.3 with a dual core (four HW
thread) Intel CoreTM i5 processor at 1.7 Ghz and 4 Gbyte DDR3 memory at 1.333 Ghz.
196
Background references
http://openmp.org/mp-documents/OpenMP3.1-CCard.pdf