Docsity

OpenMP: A Specification for Shared Memory Parallel Programming with Examples, Study notes of Computer Science

Toggle sidebar
bg1
1. Basics of Shared Memory Programming with OpenMP
Material taken from sections in Chapter 7:
•OpenMP Parallelization basics
•Loop-specific OpenMP parallelization
•General OpenMP parallelization method
•Shared memory and race conditions
2. What is OpenMP?
OpenMP is a specification of a set of compiler directives, library rou-
tines, and environment variables designed to enable shared memory pro-
gramming for Fortran and C/C++ programs.
•Originally developed by SGI
•Now a standard: www.openmp.org
•Supposed to be easier to program than pthreads (also have better
Fortran support [stop laughing])
•Requires explicit compiler support: Intel’s icc, Solaris cc, SGI cc,
IBM xlc, Portland Group pgcc, HP cc, etc.
–Supposedly fully supported as of gcc 4.2
⇒Not sure of details, but clearly OpenMP is easiest threading platform
for simple applications
3. Stupendously Basic Basics
Almost all parallel programs need to answer the following questions:
•How do I set nthreads to use?
→setenv OMP NUM THREADS # /omp set num threads(#)
•How many threads are running?
→int omp get num threads()
•Which thread am I?
→int omp get thread num()
•How do I start parallel threads?
→#pragma omp [parallel /parallel for]
•How do I join threads & their results back to serial?
→End of pragma scope / reduction clause
•How can I synchronize threads?
→omp set lock(lock)
→#pragma omp critical [<name>]
4. Parallelizing Simple Dot Product (Intro)
Will use simple dot product to illustrate how OpenMP is used. There
are two approaches:
1. Simple loop-oriented approach
2. More complex general approach
Here’s the simple serial code:
d o u b l e dd ot ( i n t N , d o u b l e ∗X ,
double ∗Y)
{
d o u b l e do t ;
i n t i ;
do t = X[ 0 ] ∗Y [ 0 ] ;
f o r ( i =1 ; i <N; i ++)
do t += X[ i ] ∗Y [ i ];
r e t ur n ( do t ) ;
}
•Note there are no loop carried
dependencies other than dot:
→We can run all iterations at
same time
→Can use specialized loop in-
struction
pf3

Partial preview of the text

Download OpenMP: A Specification for Shared Memory Parallel Programming with Examples and more Study notes Computer Science in PDF only on Docsity!

Basics of Shared Memory Programming with OpenMP

Material taken from sections in Chapter 7:

OpenMP Parallelization basics

Loop-specific OpenMP parallelization

General OpenMP parallelization method

Shared memory and race conditions

What is OpenMP?

OpenMP

is a specification of a set of compiler directives, library rou-

gramming for Fortran and C/C++ programs.tines, and environment variables designed to enable shared memory pro-

Originally developed by SGI

Now a standard:

www.openmp.org

Supposed to be easier to program than

pthreads

(also have better

Fortran support [stop laughing])

Requires explicit compiler support:

Intel’s icc, Solaris cc

, SGI cc,

IBM xlc, Portland Group pgcc, HP cc, etc.

Supposedly fully supported as of gcc 4.

for simple applicationsNot sure of details, but clearly OpenMP is easiest threading platform

Stupendously Basic Basics

Almost all parallel programs need to answer the following questions:

→How do I set nthreads to use?

setenv OMP

NUM

THREADS

omp

set

num

threads(#)

→How many threads are running?

int omp

get

num

threads()

→Which thread am I?

int omp

get

thread

num()

→How do I start parallel threads?

#pragma omp

[

parallel

parallel for

]

→How do I join threads & their results back to serial?

End of pragma scope / reduction clause

→How can I synchronize threads?

omp

set

lock(lock)

#pragma omp critical [

name

]

Parallelizing Simple Dot Product (Intro)

Will use simple dot product to illustrate how OpenMP is used.

There

are two approaches:

  1. More complex general approach1. Simple loop-oriented approach

d o u b l eHere’s the simple serial code:

d d o t ( i n t

N ,

d o u b l e

∗ X ,

d o u b l e

∗ Y )

{

d o u b l e

d o t ;

i n t

i ;

d o t

=

X [ 0 ]

∗

Y [ 0 ] ;

f o r

( i =1;

i

<

N ;

i ++)

d o t

+= X [ i ]

∗

Y [ i ] ;

r e t u r n ( d o t ) ;

}

dependencies other thanNote there are no loop carried

dot

same timeWe can run all iterations at

structionCan use specialized loop in-

Simple OpenMP Loop Parallelization

Using

parallel for

(ln 8), OpenMP splits loop iterations amongst

threads

Reduce

the

partial

sums

created

by

all

threads

down

to

master

thread, using

reduction

clause (ln 9)

(other thanCan do this because we have loop wt no loop-carried dependencies

dot

, which we fix with

reduction

serial code

1 d o u b l e

d d o t ( i n t

N ,

d o u b l e

∗ X ,

2

d o u b l e

∗ Y )

3 (^) {

4

d o u b l e

d o t ;

5

i n t

i ;

76

d o t

=

X [ 0 ]

∗

Y [ 0 ] ;

8

f o r

( i =1;

i

<

N ;

i ++)

9

d o t

+= X [ i ]

∗

Y [ i ] ;

0

r e t u r n ( d o t ) ;

1 (^) }

threaded code

1 d o u b l e

d d o t ( i n t

N ,

d o u b l e

∗ X ,

2

d o u b l e

∗ Y )

3 (^) {

4

d o u b l e

d o t ;

5

i n t

i ;

76

d o t

=

X [ 0 ]

∗

Y [ 0 ] ;

8

#pragma

omp

p a r a l l e l

f o r

\

9

r e d u c t i o n ( + : d o t )

10

f o r

( i =1;

i

<

N ;

i ++)

11

d o t

+= X [ i ]

∗

Y [ i ] ;

12

r e t u r n ( d o t ) ;

13

(^) }

+Red vars init to 0, then added with serial val on end of

region

For more complex codes, may need a more general approach:

Explicit Parallelization Example

Can use

parallel

region to explicitly thread code, and indicate ex-

plicitly which items are private, shared, or reduced

Book is in error: no such thing as

default(private)

in C!

1 d o u b l e

d d o t ( i n t

N ,

d o u b l e

∗ X ,

d o u b l e

∗ Y )

2 (^) {

3

d o u b l e

d o t = 0. 0 ,

∗ (^) x ,

∗ (^) y ;

4

i n t

n ,

i ,

n t h r ,

m y t h r ;

65

#pragma

omp

p a r a l l e l

d e f a u l t ( n o n e )

r e d u c t i o n ( + : d o t )

s h a r e d ( N , X , Y )

\

7

p r i v a t e ( n , i , n t h r , m y t h r , x , y )

8

{

9

n t h r

=

o m p

g e t

n u m

t h r e a d s ( ) ;

10

m y t h r

=

o m p

g e t

t h r e a d

n u m ( ) ;

11

n

=

N

/

n t h r ;

//

Min

i t e r

f o r a l l

t h r e a d s

12

x = X + n ∗

m y t h r ;

//

P t r s

t o

t h i s

t h r e a d s

13

y = Y + n ∗

m y t h r ;

//

c h u n k s

o f

X

&

Y

14

i f

( m y t h r

==

n t h r

(^) −

//

L a s t

t h r e a d

g e t s

r e m a i n d e r

15

n

+= N

−

n (^) ∗ (^) n t h r ;

16

d o t

=

x [ 0 ]

∗

y [ 0 ] ;

17

f o r

( i =1;

i

<

n ;

i ++)

18

d o t

+=

x [ i ]

∗

y [ i ] ;

19

}

20

r e t u r n ( d o t ) ;

21

(^) }

W/o

default(none)

all vars are

shared

Shared Memory and Race Conditions

Global & heap shared amongst threads (unlike processes):

Imagine two threads both doing the following:

loc = glob++;

glob

  1. Thread 0 & 1 read at same time, have value of 10, both incre- is equal to 10. Different outcomes may occur:

ment, both write. Both threads get

loc

glob = 11

  1. Thread 0 reads, increments, writes, and then thread 1 does the

same.

Thread 0 has

loc = 10

, thread 1 has

loc = 11

, and

glob

for both (after both completed:

will be 11 for a while for

thread 0).

  1. Swap thread 0 for 1 in above for different outcome.

This is known as a

Race condition

, which may occur whenever a

thread updates memory that another thread may read or write.

execution is calledA code which is guaranteed to behave correctly during threaded

thread safe

A routine is that is thread-safe is said to be

reentrant

Thread Safety

There are various ways to achieve thread safety:

  1. Duplicate any resource that must be written so that each thread

has private copy (copy to stack or malloc area for each thread)

  1. Place locks around all updates2. Synchronize threads so they execute section in known order

This includes the Operating System itself!combo of above methods to achieve thread safety in complex routines.For many problems, will have to insert extra code and use one or a

Too many locks/syncs can make threaded slower than serial!

Must link in thread-safe system routines (malloc,rand,math,etc)

the TCP/IP stack got locked to guarantee thread safetyslower under Linux than FreeBSD, due to where and how longMultithreaded networking applications used to be tremendously

sion (eg,sions; other times, must explicitly call thread-safe/reentrant ver-Many times standard sys names replaced with thread-safe ver-

rand

r

rather than

rand