Documente Academic
Documente Profesional
Documente Cultură
Arash Bakhtiari
bakhtiar@in.tum.de
2012-12-18 Tue
Introduction
I
What is OpenMP?
I
I
I
I
Fork-Join Model
I
I
OpenMP API
Primary API components:
I Compiler Directives:
#pragma omp p a r a l l e l
i n t omp_get_num_threads ( v o i d ) ;
Environment Variables
e x p o r t OMP_NUM_THREADS=2
Example
Listing 1: OpenMP Hello World!
#i n c l u d e <i o s t r e a m >
#i n c l u d e <omp . h>
int
{
main ( i n t
argc ,
char argv [ ] )
#pragma omp p a r a l l e l
{
s t d : : c o u t << "THREAD : " << omp_get_thread_num ( ) << " \ t H e l l o , World ! \ n " ;
}
return 0;
}
Listing 2: Compiling
g++ o h e l l o
h e l l o . c fopenmp
Classification of Variables
private(var-list):
I
shared(var-list):
I
Example
Listing 3: OpenMP Private Variable
#i n c l u d e <i o s t r e a m >
#i n c l u d e <omp . h>
i n t main ( i n t a r g c , c h a r a r g v [ ] )
{
int i , j ;
i = 1;
j = 2;
s t d : : c o u t << "BEFORE : i , j= "<< i << " , " << j << s t d : : e n d l ;
#pragma omp p a r a l l e l p r i v a t e ( i )
{
i = 3;
j = 5;
s t d : : c o u t << " INLOOP : i , j= "<< i << " , " << j << s t d : : e n d l ;
}
s t d : : c o u t << "AFTER :
return 0;
Work-Sharing Constructs
Parallel loop
Parallel section
Master region
Single region
Parallel Loop
Syntax:
#pragma omp f o r
I
I
[ clause
...]
Scheduling Strategies
I
Schedule clause:
schedule ( type
[ , size ])
Example
Listing 4: OpenMP Private Variable
#i n c l u d e <i o s t r e a m >
#i n c l u d e <omp . h>
#d e f i n e CHUNKSIZE 100
#d e f i n e N
1000
i n t main ( )
{
i n t i , chunk ;
d o u b l e a [N] , b [N] , c [N ] ;
s r a n d ( t i m e ( NULL ) ) ;
f o r ( i =0; i < N ; i ++) {
a [ i ] = generate_random_double ( 0 . 0 ,
b [ i ] = generate_random_double ( 0 . 0 ,
}
c h u n k = CHUNKSIZE ;
#pragma omp p a r a l l e l
{
10.0);
10.0);
s h a r e d ( a , b , c , chunk )
private ( i )
0;
References