MULTICORE PROGRAMMING WITH OPENMP

Size: px
Start display at page:

Download "MULTICORE PROGRAMMING WITH OPENMP"

Transcription

1 MULTICORE PROGRAMMING WITH OPENMP Dott. Alessandro Capotondi

2 Outline and Goals Understand OpenMP directives usage Manage data parallelism and load balancing using OpenMP Understand Overheads Compare to Amdahl s predictions

3 Disclaimer (Unfortunally) This is not an OpenMP course, so it will not cover all the aspect related to topic. If you want to elaborate in deep your knowledge of parallel programming using OpenMP here you can find some additional material (this is not required for the successful pass of this exam) Tutorials: Reference Card Books

4 Programming model: OpenMP De-facto standard for the shared memory programming model A collection of compiler directives, library routines and environment variables Easy to specify parallel execution within a serial code Targets several types of parallelism (data, task, heterogeneous) Popular in high-end embedded Requires special support in the compiler Generates calls to threading libraries (e.g. pthreads)

5 Recap

6 Recap

7 Recap

8 Fork/Join Parallelism Sequential program Parallel program Initially only master thread is active Master thread executes sequential code Fork: Master thread creates or awakens additional threads to execute parallel code Join: At the end of parallel code created threads are suspended upon barrier synchronization

9 Pragmas Pragma: a compiler directive in C or C++ Stands for pragmatic information A way for the programmer to communicate with the compiler Compiler free to ignore pragmas: original sequential semantic is not altered Syntax: #pragma omp <rest of pragma>

10 Components of OpenMP a subset of the directives Directives Parallel regions #pragma omp parallel Work sharing #pragma omp for #pragma omp sections Synchronization #pragma omp barrier #pragma omp critical #pragma omp atomic Clauses Thread Forking/Joining omp_parallel_start() omp_parallel_end() Loop scheduling Thread IDs Data scope attributes private shared reduction Loop scheduling static dynamic Runtime Library omp_get_thread_num() omp_get_num_threads()

11

12 Outlining parallelism The parallel directive Fundamental construct to outline parallel computation within a sequential program Code within its scope is replicated among threads Defers implementation of parallel execution to the runtime (machinespecific, e.g. pthread_create) A sequential program....is easily parallelized int main() #pragma omp parallel printf ( \nhello world! ); int main() omp_parallel_start(&parfun, ); parfun(); omp_parallel_end(); int parfun( ) printf ( \nhello world! );

13 #pragma omp parallel Code originally contained within the scope of the pragma is outlined to a new function within the compiler int main() omp_parallel_start(&parfun, ); parfun(); omp_parallel_end(); int main() #pragma omp parallel printf ( \nhello world! ); int parfun( ) printf ( \nhello world! );

14 #pragma omp parallel The #pragma construct in the main function is replaced with function calls to the runtime library int main() omp_parallel_start(&parfun, ); parfun(); omp_parallel_end(); int main() #pragma omp parallel printf ( \nhello world! ); int parfun( ) printf ( \nhello world! );

15 #pragma omp parallel First we call the runtime to fork new threads, and pass them a pointer to the function to execute in parallel int main() omp_parallel_start(&parfun, ); parfun(); omp_parallel_end(); int main() #pragma omp parallel printf ( \nhello world! ); int parfun( ) printf ( \nhello world! );

16 #pragma omp parallel Then the master itself calls the parallel function int main() omp_parallel_start(&parfun, ); parfun(); omp_parallel_end(); int main() #pragma omp parallel printf ( \nhello world! ); int parfun( ) printf ( \nhello world! );

17 #pragma omp parallel Finally we call the runtime to synchronize threads with a barrier and suspend them int main() omp_parallel_start(&parfun, ); parfun(); omp_parallel_end(); int main() #pragma omp parallel printf ( \nhello world! ); int parfun( ) printf ( \nhello world! );

18 #pragma omp parallel Data scope attributes int main() int id; int a = 5; #pragma omp parallel id = omp_get_thread_num(); if (id == 0) printf ( Master: a = %d., a*2); else printf ( Slave: a = %d., a); Call runtime to get thread ID: Every thread sees a different value Master and slave threads access the same variable a A slightly more complex example

19 #pragma omp parallel Data scope attributes int main() int id; int a = 5; #pragma omp parallel id = omp_get_thread_num(); if (id == 0) printf ( Master: a = %d., a*2); else printf ( Slave: a = %d., a); Call runtime to get thread ID: Every thread sees a different value Master and slave threads access the same variable a A slightly more complex example

20 #pragma omp parallel Data scope attributes What is the view of memory among different threads in a parallel region? int main() int id; int a = 5; #pragma omp parallel shared (a) private (id) id = omp_get_thread_num(); if (id == 0) printf ( Master: a = %d., a*2); else printf ( Slave: a = %d., a); Insert code to retrieve the address of the shared object from within each parallel thread Allow symbol privatization: Each thread contains a private copy of this variable A slightly more complex example

21 #pragma omp parallel Data scope attributes int main() int id; int a = 5; #pragma omp parallel shared (a) private (id) id = omp_get_thread_num(); if (id == 0) printf ( Master: a = %d., a*2); else printf ( Slave: a = %d., a); Insert code to retrieve the address of the shared object from within each parallel thread Allow symbol privatization: Each thread contains a private copy of this variable A slightly more complex example

22 LET S TRY IT! ALESSANDRO CAPOTONDI ALESSANDRO.CAPOTONDI@UNIBO.IT GIUSEPPE TAGLIAVINI GIUSEPPE.TAGLIAVINI@UNIBO.IT

23 ODROID XU Exynos5 Octa Cortex -A15 1.4Ghz quad core and Cortex -A7 quad core CPUs PowerVR SGX544MP3 GPU (OpenGL ES 2.0, OpenGL ES 1.1 and OpenCL 1.1 EP) 2Gbyte LPDDR3 RAM USB 3.0 Host x 1, USB 3.0 OTG x 1, USB 2.0 Host x 4 HDMI 1.4a output Type-D connector emmc 4.5 Flash Storage Full Fledged Ubuntu OS support

24 DO YOU TRUST ME? Somewhere in the undergrounds of Bologna

25 SUBMIT YOUR RESULTS You could submit your results and questions at Include: Student(s) Name and ID(s) Attach the SpreadSheet! Solutions will be published on the course website in a few weeks!

26 SETUP FOR THE EXERCISES Login to Odroid ssh X stud_usr@<board-ip> Execise files are already loaded on the board cd /home/stud_usr/hsdes17 List the files using the command ls Take a look at test.c. Different exercises are #ifdef-ed gedit test.c & To compile and execute the desired one make clean all run e MYOPTS="-DEX1" (Execise 1) make clean all run e MYOPTS="-DEX2" (Execise 2)

27 EX 1 OpenMP Hello World! #pragma omp parallel num_threads (?) printf "Hello world, I m thread??" Use parallel directive to create multiple threads Each thread executes the code enclosed within the scope of the directive Use runtime library functions to determine thread ID All SPMD parallelization is based on this approach

28 More data sharing clauses firstprivate copyin, private storage lastprivate Copyout, private storage

29 Sharing work among threads The for directive The parallel pragma instructs every thread to execute all of the code inside the block If we encounter a for loop that we want to divide among threads, we use the for pragma #pragma omp for

30 #pragma omp for #pragma omp for can be placed everywhere inside a parallel construct, or combined with it, as in the example int main() #pragma omp parallel for for (i=0; i<10; i++) a[i] = i; int main() omp_parallel_start(&parfun, ); parfun(); omp_parallel_end(); int parfun( ) int LB = ; int UB = ; for (i=lb; i<ub; i++) a[i] = i;

31 #pragma omp for int main() omp_parallel_start(&parfun, ); parfun(); omp_parallel_end(); int main() #pragma omp parallel for for (i=0; i<10; i++) a[i] = i; int parfun( ) int LB = ; int UB = ; for (i=lb; i<ub; i++) a[i] = i;

32 #pragma omp for int main() omp_parallel_start(&parfun, ); parfun(); omp_parallel_end(); int main() #pragma omp parallel for for (i=0; i<10; i++) a[i] = i; int parfun( ) int LB = ; int UB = ; for (i=lb; i<ub; i++) a[i] = i;

33 The schedule clause Static Loop Partitioning #pragma omp for schedule(static) for (i=0; i<12; i++) a[i] = i; Es. 12 iterations (N), 4 threads (Nthr) DATA CHUNK C = ceil ( N ) Nthr 3 iterations thread Useful for: Simple, regular loops Iterations with equal duration Iteration space Thread ID (TID) LOWER BOUND LB = C * TID UPPER BOUND UB = min [C * ( TID + 1) ], N

34 The schedule clause Static Loop Partitioning #pragma omp for schedule(static) for (i=0; i<12; i++) a[i] = i; Es. 12 iterations (N), 4 threads (Nthr) DATA CHUNK C = ceil ( N ) Nthr 3 iterations thread Useful for: Simple, regular loops Iterations with equal duration Iteration space Thread ID (TID) LOWER BOUND LB = C * TID UPPER BOUND UB = min [C * ( TID + 1) ], N

35 The schedule clause Static Loop Partitioning #pragma omp for schedule(static) for (i=0; i<12; i++) a[i] = i; int start = rand(); int count = 0; Iteration space while (start++ < 256) count++; a[count] = foo(); UNBALANCED workloads

36 The schedule clause Dynamic Loop Partitioning #pragma omp for schedule(static) schedule(dynamic) for (i=0; i<12; i++) int start = rand(); int count = 0; Iteration space while (start++ < 256) count++; a[count] = foo();

37 The schedule clause Dynamic Loop Partitioning Runtime environment Iteration space Work queue int parfun( ) int LB, UB; GOMP_loop_dynamic_next(&LB, &UB); for (i=lb; i<ub; i++)

38 The schedule clause Dynamic Loop Partitioning Iteration space BALANCED workloads 9

39 Parallelization granularity Iteration chunking Fine-grain Parallelism Best opportunities for load balancing, but.. Small amounts of computational work between parallelism computation stages Low computation to parallelization ratio High parallelization overhead Coarse-grain Parallelism Harder to load balance efficiently, but.. Large amounts of computational work between parallelism computation stages High computation to parallelization ratio Low parallelization overhead

40 The schedule clause Dynamic Loop Partitioning #pragma omp for schedule(dynamic, 1) for (i=0; i<12; i++) int start = rand(); int count = 0; Iteration space while (start++ < 256) count++; a[count] = foo();

41 The schedule clause Dynamic Loop Partitioning Runtime environment Iteration space Work queue int parfun( ) int LB, UB; GOMP_loop_dynamic_next(&LB, &UB); for (i=lb; i<ub; i++) WORK

42 The schedule clause Dynamic Loop Partitioning Iteration space

43 The schedule clause Dynamic Loop Partitioning Iteration space chunking overhead

44 The schedule clause Dynamic Loop Partitioning #pragma omp for schedule(dynamic, 2) for (i=0; i<12; i++) int start = rand(); int count = 0; Iteration space while (start++ < 256) count++; a[count] = foo();

45 The schedule clause Dynamic Loop Partitioning Runtime environment Iteration space Work queue int parfun( ) int LB, UB; GOMP_loop_dynamic_next(&LB, &UB); for (i=lb; i<ub; i++) WORK

46 The schedule clause Dynamic Loop Partitioning Iteration space

47 The schedule clause Dynamic Loop Partitioning Iteration space smaller chunking overhead

48 More scheduling clauses 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[, chunk]) Schedule and chunk size taken from the OMP_SCHEDULE environment variable (or the runtime library for OpenMP 3.0)

49 LET S TRY IT! ALESSANDRO CAPOTONDI ALESSANDRO.CAPOTONDI@UNIBO.IT GIUSEPPE TAGLIAVINI GIUSEPPE.TAGLIAVINI@UNIBO.IT

50 EX 2A STATIC LOOP #pragma omp parallel for \ num_threads(nthreads) schedule(static) for (uint i=0; i<niters; i++) work (W); /* (implicit) SYNCH POINT */ T0 T1 T2 T With schedule(static) M iterations are statically assigned to each thread, where: M = NITERS/NTHREADS Small overhead: loop indexes are computed according to thread ID Optimal scheduling if workload is balanced

51 EX 2A STATIC LOOP #pragma omp parallel for \ num_threads(nthreads) schedule(static) for (uint i=0; i<niters; i++) work (W); /* (implicit) SYNCH POINT */ 2A Use the following configurations: NITERS = 1024 NTHREADS= 1,2,4,8,16 W = Objectives: Collect execution time in a excel sheet for the various configurations Comment on the results

52 EX 2B DYNAMIC LOOP #pragma omp parallel for \ num_threads (NTHREADS) schedule (dynamic, M) for (uint i=0; i<niters; i++) work (W); /* (implicit) SYNCH POINT */ T0 T1 T2 T OVERHEAD CHUNK Iterations are dynamically assigned to threads in groups of M = NITERS/NTHREADS Same parallelization of schedule(static) Coarse granularity Overhead only at beginning and end of loop

53 EX 2B DYNAMIC LOOP T0 T1 T2 T #pragma omp parallel for \ num_threads (NTHREADS) \ schedule(dynamic, 1) for (uint i=0; i<niters; i++) work (W); /* (implicit) SYNCH POINT */ Iterations are dynamically assigned to threads one iteration per chunk Finest granularity Overhead at every iteration OVERHEAD CHUNK (size = 1 iter)

54 EX 2B DYNAMIC LOOP #pragma omp parallel for \ num_threads (NTHREADS) schedule (dynamic, M) for (uint i=0; i<niters; i++) work (W); /* (implicit) SYNCH POINT */ 2a, 2b Use the following configurations: NITERS = 1024 NTHREADS = 1,2,4,8,16 M = NITERS/NTHREADS, 1 W = Objectives: Collect execution time in a excel sheet for the various configurations Comment on the results

55 EX 2C DYNAMIC LOOP #pragma omp parallel for \ num_threads (NTHREADS) schedule (dynamic, M) for (uint i=0; i<niters; i++) work (W); /* (implicit) SYNCH POINT */ 2c Use the following configurations: NITERS = NTHREADS = 1,2,4,8,16 M = NITERS/NTHREADS, 1 W = 10 Objectives: Collect execution time in a excel sheet for the various configurations Comment on the results

56 EX 2D DYNAMIC LOOP (HOMEWORKS) Study the impact of chunk size for varying sizes of of the workload W Use the following configurations: NITERS = 1024*256 NTHREADS = 4 M = = 32, 16, 8, 4, 1 W = 1, 10, 100, 1000 Objectives: Collect execution time in a excel sheet for the various configurations Comment on the results HINT: Plot NORMALIZED execution time to the fastest scheduling option for a given value of W

57 EX 3 - UNBALANCED LOOP T0 T1 T2 T3 #pragma omp parallel for \ num_threads (NTHREADS) \ schedule (dynamic, NITERS/NTHREADS) SYNCH POINT for (uint i=0; i<niters; i++) work((i>>2) * W); /* (implicit) SYNCH POINT */ Iterations have different duration Using coarse-grained chunks (NITERS/NTHREADS) creates unbalanced work among the threads Due to the barrier at the end of parallel region, all threads have to wait for the slowest one

58 EX 3 - UNBALANCED LOOP T0 T1 T2 T3 #pragma omp parallel for \ num_threads (NTHREADS) \ schedule (dynamic, 1) for (uint i=0; i<niters; i++) work((i>>2) * W); /* (implicit) SYNCH POINT */ Iterations have different duration SPEEDUP Using fine-grained chunks (the finest is creates balanced work among the threads The overhead for dynamic scheduling is amortized by the effect of workbalancing on the overall loop duration

59 EX 3 - UNBALANCED LOOP #pragma omp parallel for \ num_threads (NTHREADS) schedule (dynamic, M) for (uint i=0; i<niters; i++) work((i>>2) * W); /* (implicit) SYNCH POINT */ Exercise 3a, 3b Use the following configurations: NITERS = 128 NTHREADS = 4 M = 32, 16, 8, 4, 1 W = Objectives: Create a new excel sheet plotting results (execution time) for static and dynamic schedules (with chunk size M) Comment on results

60 #pragma omp barrier Most important synchronization mechanism in shared memory fork/join parallel programming All threads participating in a parallel region wait until everybody has finished before computation flows on This prevents later stages of the program to work with inconsistent shared data It is implied at the end of parallel constructs, as well as for and sections (unless a nowait clause is specified)

61 #pragma omp critical Critical Section: a portion of code that only one thread at a time may execute We denote a critical section by putting the pragma #pragma omp critical in front of a block of C code

62 π-finding code example double area, pi, x; int i, n; #pragma omp parallel for private(x) \ shared(area) for (i=0; i<n; i++) x = (i + 0.5)/n; area += 4.0/(1.0 + x*x); pi = area/n;

63 Race condition Ensure atomic updates of the shared variable area to avoid a race condition in which one process may race ahead of another and ignore changes

64 Race condition (Cont d) Thread A reads into a local register Thread B reads into a local register Thread A updates area with Thread B ignores write from thread A and updates area with time

65 π-finding code example double area, pi, x; int i, n; #pragma omp parallel for private(x) shared(area) for (i=0; i<n; i++) x = (i +0.5)/n; #pragma omp critical area += 4.0/(1.0 + x*x); pi = area/n; #pragma omp critical protects the code within its scope by acquiring a lock before entering the critical section and releasing it after execution

66 Correctness, not performance! As a matter of fact, using locks makes execution sequential To dim this effect we should try use fine grained locking (i.e. make critical sections as small as possible) A simple instruction to compute the value of area in the previous example is translated into many more simpler instructions within the compiler! The programmer is not aware of the real granularity of the critical section

67 Correctness, not performance! As a matter of fact, using locks makes execution sequential To dim this effect we should try use fine grained locking (i.e. make critical sections as small as possible) A simple instruction to compute the This value is a of dump area in of the previous example is translated into many intermediate more simpler instructions within the compiler! representation of the The programmer is not aware of the program real granularity within the of the critical section compiler

68 Correctness, not performance! As a matter of fact, using locks makes execution sequential To dim this effect we should try use fine grained locking (i.e. make critical sections as small as possible) A simple instruction to compute the value of area in the previous example is translated into many more simpler instructions within the compiler! The programmer is not aware of the real granularity of the critical section

69 Correctness, not performance! As a matter of fact, using locks makes execution sequential To dim this effect we should try use fine grained locking (i.e. make critical sections as small as possible) A simple instruction to compute the value of area in the call runtime to acquire lock previous example is translated into many more simpler instructions within the compiler! The programmer is not aware Lock-protected of the real granularity of the critical section operations (critical section) call runtime to release lock

70 π-finding code example double area, pi, x; int i, n; #pragma omp parallel for \ private(x) \ shared(area) for (i=0; i<n; i++) x = (i +0.5)/n; Parallel #pragma omp critical area += 4.0/(1.0 + x*x); Sequential pi = area/n; Waiting for lock

71 Correctness, not performance! A programming pattern such as area += 4.0/(1.0 + x*x); in which we: Fetch the value of an operand Add a value to it Store the updated value is called a reduction, and is commonly supported by parallel programming APIs OpenMP takes care of storing partial results in private variables and combining partial results after the loop

72 Correctness, not performance! double area, pi, x; int i, n; #pragma omp parallel for private(x) shared(area) for (i=0; i<n; i++) x = (i +0.5)/n; reduction(+:area) area += 4.0/(1.0 + x*x); pi = area/n; The reduction clause instructs the compiler to create private copies of the area variable for every thread. At the end of the loop partial sums are combined on the shared area variable

73 Correctness, not performance! double area, pi, x; int i, n; #pragma omp parallel for private(x) shared(area) for (i=0; i<n; i++) x = (i +0.5)/n; reduction(+:area) area += 4.0/(1.0 + x*x); pi = area/n; The reduction clause instructs the compiler to create private copies of the area variable for every thread. At the end of the loop partial sums are combined on the shared area variable

74 Correctness, not performance! double area, pi, x; int i, n; #pragma omp parallel for private(x) shared(area) for (i=0; i<n; i++) x = (i +0.5)/n; reduction(+:area) area += 4.0/(1.0 + x*x); pi = area/n; The reduction clause instructs the compiler to create private copies of the area variable for every thread. At the end of the loop partial sums are combined on the shared area variable

75 More worksharing constructs The master directive 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();

76 More worksharing constructs The single directive The single construct denotes a block of code that is executed by only one thread (not necessarily the master thread). A barrier is implied at the end of the single block (can remove the barrier with a nowait clause). #pragma omp parallel do_many_things(); #pragma omp single exchange_boundaries(); do_many_other_things();

77 Recap exercise!

78 Recap exercise! Solution (1)

79 Recap exercise! Solution (2)

80 What we learned? Create Parallelism Exploit data-parallelisms using OpenMP Correctness is not performance efficiency! How to measure parallelization effectiveness and efficiency?

81 LET S TRY IT! ALESSANDRO CAPOTONDI ALESSANDRO.CAPOTONDI@UNIBO.IT GIUSEPPE TAGLIAVINI GIUSEPPE.TAGLIAVINI@UNIBO.IT

82 EX 4 AMDAHL S EFFECT A program has a workload composed of NITERS repetitions of the function work. This function is invoked with a parameter W that specifies its duration. Of these NITERS repetitions, only P% can be executed in parallel. Use the following configurations: NITERS = NTHREADS = 4 P = 0%, 10%, 50%, 90% of NITERS W = 1, 1000 Objective Compute on a new spreadsheet the estimated speedup according to the Amdahl s law Measure the actual speedup Comment on results. What is the effect of the overheads? SPEEDUP EX3 0.00% 10.00% 50.00% 90.00% P Speedup (W=1) Speedup (W=10000) Ideal

83 SPMD VS MPMD Recall.. SPMD (single program, multiple data) Processors execute the same stream of instructions over different data #pragma omp for MPMD (multiple program, multiple data) Processors execute different streams of instructions over (possibly) different data #pragma omp sections #pragma omp task

84 Recap: TASK parallelism in OpenMP 2.5 The sections directive The for pragma allows to exploit data parallelism in loops OpenMP 2.5 also provides a directive to exploit task parallelism #pragma omp sections

85 Task Parallelism Example int main() v = alpha(); w = beta (); y = delta (); x = gamma (v, w); z = epsilon (x, y)); printf ( %f\n, z);

86 Task Parallelism Example int main() #pragma omp parallel sections v = alpha(); w = beta (); #pragma omp parallel sections y = delta (); x = gamma (v, w); z = epsilon (x, y)); printf ( %f\n, z);

87 Task Parallelism Example int main() #pragma omp parallel sections v = alpha(); w = beta (); #pragma omp parallel sections y = delta (); x = gamma (v, w); z = epsilon (x, y)); printf ( %f\n, z);

88 Task Parallelism Example int main() #pragma omp parallel sections #pragma omp section v = alpha(); #pragma omp section w = beta (); #pragma omp parallel sections #pragma omp section y = delta (); #pragma omp section x = gamma (v, w); z = epsilon (x, y)); printf ( %f\n, z);

89 Task Parallelism Example int main() v = alpha(); w = beta (); y = delta (); x = gamma (v, w); z = epsilon (x, y)); printf ( %f\n, z);

90 Task Parallelism Example int main() #pragma omp parallel sections v = alpha(); w = beta (); y = delta (); x = gamma (v, w); z = epsilon (x, y)); printf ( %f\n, z);

91 Task Parallelism Example int main() #pragma omp parallel sections v = alpha(); w = beta (); y = delta (); x = gamma (v, w); z = epsilon (x, y)); printf ( %f\n, z);

92 Task Parallelism Example int main() #pragma omp parallel sections #pragma omp section v = alpha(); #pragma omp section w = beta (); #pragma omp section y = delta (); x = gamma (v, w); z = epsilon (x, y)); printf ( %f\n, z);

93 Task parallelism The sections directive allows a very limited form of task parallelism All tasks must be statically outlined in the code What if a functional loop (while) body is identified as a task? Unrolling? Not feasible for high iteration count What if recursion is used?

94 Task parallelism Why? Example: list traversal OpenMP v2.5 EXAMPLE void traverse_list (List l) Element e ; #pragma omp parallel private ( e ) for ( e = e first; e; e = e next ) #pragma omp single nowait process ( e ) ; Awkward! Poor performance Not composable

95 Task parallelism EXAMPLE Why? Example: tree traversal void traverse_tree (Tree *tree) #pragma omp parallel sections #pragma omp section if ( tree left ) traverse_tree ( tree left ); #pragma omp section if ( tree right) traverse_tree ( tree right); process (tree); OpenMP v2.5 Too many parallel regions Extra overheads Extra synchronizations Not always well supported

96 Task parallelism Better solution for those problems Main addition to OpenMP 3.0a Allows to parallelize irregular problems unbounded loops recursive algorithms producer/consumer schemes... Ayguadé et al., The Design of OpenMP Tasks, IEEE TPDS March 2009

97 Task parallelism The OpenMP tasking model Creating tasks Data scoping Synchronizing tasks Execution model

98 What is an OpenMP task? Tasks are work units which execution may be deferred they can also be executed immediately! Tasks are composed of: code to execute data environment Initialized at creation time internal control variables (ICVs)

99 Task directive #pragma omp task [ clauses ] structured block Each encountering thread creates a task Packages code and data environment Highly composable. Can be nested inside parallel regions inside other tasks inside worksharing constructs (for, sections)

100 List traversal with tasks Why? Example: list traversal EXAMPLE void traverse_list (List l) Element e ; for ( e = e first; e; e = e next ) #pragma omp task process ( e ) ; What is the scope of e?

101 Task data scoping Data scoping clauses shared(list) private(list) firstprivate(list) data is captured at creation default(shared none)

102 Task data scoping when there are no clauses.. If no clause Implicit rules apply e.g., global variables are shared Otherwise... firstprivate shared attribute is lexically inherited

103 List traversal with tasks EXAMPLE int a ; void foo ( ) int b, c ; #pragma omp parallel shared(c) int d ; #pragma omp task int e ; a = shared b = firstprivate c = shared d = firstprivate e = private Tip default(none) is your friend Use it if you do not see it clear

104 List traversal with tasks EXAMPLE void traverse_list (List l) Element e ; for ( e = e first; e; e = e next ) #pragma omp task process ( e ) ; e is firstprivate

105 List traversal with tasks EXAMPLE void traverse_list (List l) Element e ; for ( e = e first; e; e = e next ) #pragma omp task process ( e ) ; how we can guarantee here that the traversal is finished?

106 Task synchronization Barriers (implicit or explicit) All tasks created by any thread of the current team are guaranteed to be completed at barrier exit Task barrier #pragma omp taskwait Encountering task suspends until child tasks complete Only direct childs, not descendants!

107 List traversal with tasks EXAMPLE void traverse_list (List l) Element e ; for ( e = e first; e; e = e next ) #pragma omp task process ( e ) ; #pragma omp taskwait All tasks guaranteed to be completed here

108 Task execution model Task are executed by a thread of the team that generated it Can be executed immediately by the same thread that creates it Parallel regions in 3.0 create tasks! One implicit task is created for each thread So all task-concepts have sense inside the parallel region Threads can suspend the execution of a task and start/resume another

109 Task parallelism Why? Example: list traversal EXAMPLE CAREFUL! Multiple traversal of the same list List l; #pragma omp parallel traverse_list (l); void traverse_list (List l) Element e ; for ( e = e first; e; e = e next ) #pragma omp task process ( e ) ;

110 Task parallelism Why? Example: list traversal Single traversal EXAMPLE List l; #pragma omp parallel #pragma omp single traverse_list (l); One thread enters single and creates all tasks All the team cooperates executing them void traverse_list (List l) Element e ; for ( e = e first; e; e = e next ) #pragma omp task process ( e ) ;

111 Task parallelism In case task is within a regular counted loop an alternative is to parallelize task creation among threads EXAMPLE /* A DIFFERENT EXAMPLE */ Multiple traversals Multiple threads create tasks All the team cooperates executing them #pragma omp parallel Myfunc (); void Myfunc () int i; #pragma omp for for (i=lb; i<ub; i++) #pragma omp task process ( i ) ;

112 LET S TRY IT! ALESSANDRO CAPOTONDI ALESSANDRO.CAPOTONDI@UNIBO.IT GIUSEPPE TAGLIAVINI GIUSEPPE.TAGLIAVINI@UNIBO.IT

113 EX 5 TASK PARALLELISM WITH SECTIONS void sections() work( ); printf("%hu: Done with first elaboration!\n, ); work( ); printf("%hu: Done with second elaboration!\n, ); work( ); printf("%hu: Done with third elaboration!\n", ); work( ); printf("%hu: Done with fourth elaboration!\n", ); a) Distribute workload among 4 threads using SPMD parallelization I. Get thread id II. Use if/else or switch/case to differentiate workload b) Implement the same workload partitioning with sections directive

114 EX 6 TASK PARALLELISM WITH TASK void tasks() unsigned int i; for(i=0; i<4; i++) work((i+1)* ); printf("%hu: Done with elaboration\n", ); a) Distribute workload among 4 threads using task directive I. Same program as before II. But we had to manually unroll the loop to use sections b) Compare performance and ease of coding with sections

115 EX 7 TASK PARALLELISM WITH TASK void tasks() unsigned int i; Modify the EX6 exercise code as indicated on this slide for(i=0; i<1024; i++) work( ); printf("%hu: Done with elaboration\n", ); b) Parallelize the loop with task directive Use single directive to force a single processor to create tasks c) Parallelize the loop with single directive Use nowait clause to allow for parallel execution Compare performance and ease of coding of the two solutions

116 Overview of major 4.0 additions Device constructs SIMD constructs Cancellation Task dependences and task groups Thread affinity control User-defined reductions Initial support for Fortran 2003 Support for array sections (including in C and C++) Sequentially consistent atomics Display of initial OpenMP internal control variables

117 When are tasks guaranteed to complete or at the end of taskgroup construct #pragma omp taskgroup #pragma omp task do_tasky_stuff() Might create nested tasks wait at end of construct until all tasks created in the construct, including descendants, have completed.

118 Task dependencies #pragma omp task depend(type:list) where type is in, out or inout and list is a list of variables. list may contain subarrays: OpenMP 4.0 includes a syntax for C/C++ in: the generated task will be a dependent task of all previously generated sibling tasks that reference at least one of the list items in an out or inout clause out or inout: the generated task will be a dependent task of all previously generated sibling tasks that reference at least one of the list items in an in, out or inout clause

119 Task dependencies example #pragma omp task depend (out:a)... //writes a #pragma omp task depend (out:b)... //writes b #pragma omp task depend (in:a,b)... //reads a and b The first two tasks can execute in parallel The third task cannot start until the first two are complete

120 OpenMP 4.0 provides support for a wide range of devices Use target directive to offload a region #pragma omp target [clause [[,] clause] ] Creates new data environment from enclosing device data environment Clauses support data movement and conditional offloading device supports offload to a device other than default Does not assume copies are made memory may be shared with host Does not copy if present in enclosing device data environment Does not copy if present in enclosing device data environment if supports running on host if amount of work is small Other constructs support device data environment target data places map list items in device data environment target update ensures variable is consistent in host and device

121 Several other device constructs support full-featured code Use target declare directive to create device versions #pragma omp declare target Can be applied to functions and global variables teams directive creates multiple teams in a target region #pragma omp teams [clause [[,] clause] ] Work across teams only synchronized at end of target region Useful for GPUs (corresponds to thread blocks) Use distribute directive to run loop across multiple teams #pragma omp distribute [clause [[,] clause] ] Several combined/composite constructs simplify device use

122 Example: OpenMP support for devices Jacobi iteration #pragma omp target data map(a, Anew) while (err>tol && iter < iter_max) err = 0.0; #pragma target teams #pragma omp parallel for reduction(max:err) for(int j=1; j< n-1; j++) for(int i=1; i<m-1; i++) Anew[j][i] = 0.25* (A[j][i+1] + A[j][i-1]+ A[j-1][i] + A[j+1][i]); err = max(err,abs(anew[j][i] A[j][i])); #pragma omp target teams #pragma omp parallel for for(int j=1; j< n-1; j++) for(int i=1; i<m-1; i++) A[j][i] = Anew[j]i]; iter ++; Copy A back out to host but only once Create a data region on the device. Map A and Anew onto the target device The "target teams construct tells the compiler to pick the number of teams which translates to thread blocks for CUDA.

123 OpenMP 4.0 provides portable SIMD constructs Use simd directive to indicate a loop should be SIMDized #pragma omp simd [clause [[,] clause] ] Execute iterations of following loop in SIMD chunks Region binds to the current task, so loop is not divided across threads SIMD chunk is set of iterations executed concurrently by a SIMD lanes Creates a new data environment Clauses control data environment, how loop is partitioned safelen(length) limits the number of iterations in a SIMD chunk linear lists variables with a linear relationship to the iteration space aligned specifies byte alignments of a list of variables private, lastprivate, reduction, collapse - usual meanings

124 The declare simd construct generates SIMD functions #pragma omp simd notinbranch float min (float a, float b) return a < b? a : b; #pragma omp simd notinbranch float distsq(float x, float y) return (x y) (x y); Compile library and use functions in a SIMD loop void minex (float *a, float *b, float *c, float *d) #pragma omp parallel for simd for (i = 0; i < N; i++) d[i] = min (distsq(a[i], b[i]), c[i]); Creates implicit tasks of parallel region Divides loop into SIMD chunks Schedules SIMD chunks across implicit tasks Loop is fully SIMDized by using SIMD versions of functions

125 Exercise Considering the follow pseudo code. int main() for(int i=0; i < 128; ++i) Work1(); #pragma omp parallel for schedule(static) num_threads(128) for(int i=0; i < 1024; ++i) Work2(); return 0;

126 Exercise 1. Compute the ideal speedup expected, considering: a fixed execution time of 1ms for the function Work1() and 8ms for the function Work2(), and zero overheads for the control flow of the loops and zero costs for the parallelization. 2. Write, for each thread, the set of indexes i, that they will execute on the parallel for loop, according to the schedule policy specified. (E.g. Thread 0: i=0,1...; Thread 1: i=...) 3. Compute the speedup expected considering also the following overheads: barriers take 8ms, parallel creation takes 32ms; loop overhead takes 1ms.

127 Solution (1) 1. Compute the ideal speedup expected, considering: a fixed execution time of 1ms for the function Work1() and 8ms for the function Work2(), and zero overheads for the control flow of the loops and zero costs for the parallelization. SSSSSSSSSSSSSSSSSSll tttttttt (nnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnee tttttttt + ppppppppppppppppppppppppppee tttttttt) #cccccccccc 128 1mmmm mmmm 128 1mmmm = 128 8mmmm 8320mmmm 192mmmm 43.33x

128 Solution (2) 2. Write, for each thread, the set of indexes i, that they will execute on the parallel for loop, according to the schedule policy specified. (E.g. Thread 0: i=0,1...; Thread 1: i=...) Chuck_size = 1024 / 128 = 8 iterations Thread 0 = i from 0 to 7 Thread 1 = i from 8 to 15 Thread 2 = i from 16 to 23 Thread 128 = i from 1016 to 1023

129 Solution (3) 3. Compute the speedup expected considering also the following overheads: barriers take 8ms, parallel creation takes 32ms; loop overhead takes 1ms. SSSSSSSSSSSSSSSSSSll tttttttt (nnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnee tttttttt + ppppppppppppppll ooooooooooooooooo + bbbbbbbbbbbbrr ooooooooooooooooo + ppppppppppppppppppppppppppee tttttttt #cccccccccc + iiiiiiiiiiiiiiiiiiss ooooooooooooooooo ) 128 1mmmm mmmm 128 1mmmm + 32mmmm + 8mmmm (8mmmm + 1mmmm) = 8320mmmm 240mmmm 34.66x

130 Conclusions What we learned about OpenMP: Control and dispatch parallelism using OpenMP interface Distribute the work among different threads Implement different parallelization scheme such as data-parallelism (SPMD) or task-parallelism (MPMD) Investigate performance efficiency Novel directions of OpenMP

Exercise: OpenMP Programming

Exercise: OpenMP Programming Exercise: OpenMP Programming Multicore programming with OpenMP 19.04.2016 A. Marongiu - amarongiu@iis.ee.ethz.ch D. Palossi dpalossi@iis.ee.ethz.ch ETH zürich Odroid Board Board Specs Exynos5 Octa Cortex

More information

Parallel Programming in C with MPI and OpenMP

Parallel Programming in C with MPI and OpenMP Parallel Programming in C with MPI and OpenMP Michael J. Quinn Chapter 17 Shared-memory Programming 1 Outline n OpenMP n Shared-memory model n Parallel for loops n Declaring private variables n Critical

More information

Parallel Programming in C with MPI and OpenMP

Parallel Programming in C with MPI and OpenMP Parallel Programming in C with MPI and OpenMP Michael J. Quinn Chapter 17 Shared-memory Programming 1 Outline n OpenMP n Shared-memory model n Parallel for loops n Declaring private variables n Critical

More information

Parallel Programming in C with MPI and OpenMP

Parallel Programming in C with MPI and OpenMP Parallel Programming in C with MPI and OpenMP Michael J. Quinn Chapter 17 Shared-memory Programming Outline OpenMP Shared-memory model Parallel for loops Declaring private variables Critical sections Reductions

More information

Module 10: Open Multi-Processing Lecture 19: What is Parallelization? The Lecture Contains: What is Parallelization? Perfectly Load-Balanced Program

Module 10: Open Multi-Processing Lecture 19: What is Parallelization? The Lecture Contains: What is Parallelization? Perfectly Load-Balanced Program The Lecture Contains: What is Parallelization? Perfectly Load-Balanced Program Amdahl's Law About Data What is Data Race? Overview to OpenMP Components of OpenMP OpenMP Programming Model OpenMP Directives

More information

Parallel Programming. OpenMP Parallel programming for multiprocessors for loops

Parallel Programming. OpenMP Parallel programming for multiprocessors for loops Parallel Programming OpenMP Parallel programming for multiprocessors for loops OpenMP OpenMP An application programming interface (API) for parallel programming on multiprocessors Assumes shared memory

More information

Introduction to. Slides prepared by : Farzana Rahman 1

Introduction to. Slides prepared by : Farzana Rahman 1 Introduction to OpenMP Slides prepared by : Farzana Rahman 1 Definition of OpenMP Application Program Interface (API) for Shared Memory Parallel Programming Directive based approach with library support

More information

Parallel Programming. Exploring local computational resources OpenMP Parallel programming for multiprocessors for loops

Parallel Programming. Exploring local computational resources OpenMP Parallel programming for multiprocessors for loops Parallel Programming Exploring local computational resources OpenMP Parallel programming for multiprocessors for loops Single computers nowadays Several CPUs (cores) 4 to 8 cores on a single chip Hyper-threading

More information

Introduction to OpenMP

Introduction to OpenMP Christian Terboven, Dirk Schmidl IT Center, RWTH Aachen University Member of the HPC Group terboven,schmidl@itc.rwth-aachen.de IT Center der RWTH Aachen University History De-facto standard for Shared-Memory

More information

OpenMP 4.5: Threading, vectorization & offloading

OpenMP 4.5: Threading, vectorization & offloading OpenMP 4.5: Threading, vectorization & offloading Michal Merta michal.merta@vsb.cz 2nd of March 2018 Agenda Introduction The Basics OpenMP Tasks Vectorization with OpenMP 4.x Offloading to Accelerators

More information

Advanced OpenMP. Lecture 11: OpenMP 4.0

Advanced OpenMP. Lecture 11: OpenMP 4.0 Advanced OpenMP Lecture 11: OpenMP 4.0 OpenMP 4.0 Version 4.0 was released in July 2013 Starting to make an appearance in production compilers What s new in 4.0 User defined reductions Construct cancellation

More information

Lecture 4: OpenMP Open Multi-Processing

Lecture 4: OpenMP Open Multi-Processing CS 4230: Parallel Programming Lecture 4: OpenMP Open Multi-Processing January 23, 2017 01/23/2017 CS4230 1 Outline OpenMP another approach for thread parallel programming Fork-Join execution model OpenMP

More information

OpenMP Overview. in 30 Minutes. Christian Terboven / Aachen, Germany Stand: Version 2.

OpenMP Overview. in 30 Minutes. Christian Terboven / Aachen, Germany Stand: Version 2. OpenMP Overview in 30 Minutes Christian Terboven 06.12.2010 / Aachen, Germany Stand: 03.12.2010 Version 2.3 Rechen- und Kommunikationszentrum (RZ) Agenda OpenMP: Parallel Regions,

More information

Shared Memory Parallelism - OpenMP

Shared Memory Parallelism - OpenMP Shared Memory Parallelism - OpenMP Sathish Vadhiyar Credits/Sources: OpenMP C/C++ standard (openmp.org) OpenMP tutorial (http://www.llnl.gov/computing/tutorials/openmp/#introduction) OpenMP sc99 tutorial

More information

OpenMP. A parallel language standard that support both data and functional Parallelism on a shared memory system

OpenMP. A parallel language standard that support both data and functional Parallelism on a shared memory system OpenMP A parallel language standard that support both data and functional Parallelism on a shared memory system Use by system programmers more than application programmers Considered a low level primitives

More information

OpenMP 4.0. Mark Bull, EPCC

OpenMP 4.0. Mark Bull, EPCC OpenMP 4.0 Mark Bull, EPCC OpenMP 4.0 Version 4.0 was released in July 2013 Now available in most production version compilers support for device offloading not in all compilers, and not for all devices!

More information

Introduction to OpenMP. OpenMP basics OpenMP directives, clauses, and library routines

Introduction to OpenMP. OpenMP basics OpenMP directives, clauses, and library routines Introduction to OpenMP Introduction OpenMP basics OpenMP directives, clauses, and library routines What is OpenMP? What does OpenMP stands for? What does OpenMP stands for? Open specifications for Multi

More information

OpenMP 4.0/4.5. Mark Bull, EPCC

OpenMP 4.0/4.5. Mark Bull, EPCC OpenMP 4.0/4.5 Mark Bull, EPCC OpenMP 4.0/4.5 Version 4.0 was released in July 2013 Now available in most production version compilers support for device offloading not in all compilers, and not for all

More information

ECE 574 Cluster Computing Lecture 10

ECE 574 Cluster Computing Lecture 10 ECE 574 Cluster Computing Lecture 10 Vince Weaver http://www.eece.maine.edu/~vweaver vincent.weaver@maine.edu 1 October 2015 Announcements Homework #4 will be posted eventually 1 HW#4 Notes How granular

More information

HPCSE - II. «OpenMP Programming Model - Tasks» Panos Hadjidoukas

HPCSE - II. «OpenMP Programming Model - Tasks» Panos Hadjidoukas HPCSE - II «OpenMP Programming Model - Tasks» Panos Hadjidoukas 1 Recap of OpenMP nested loop parallelism functional parallelism OpenMP tasking model how to use how it works examples Outline Nested Loop

More information

A brief introduction to OpenMP

A brief introduction to OpenMP A brief introduction to OpenMP Alejandro Duran Barcelona Supercomputing Center Outline 1 Introduction 2 Writing OpenMP programs 3 Data-sharing attributes 4 Synchronization 5 Worksharings 6 Task parallelism

More information

Parallel Programming with OpenMP. CS240A, T. Yang

Parallel Programming with OpenMP. CS240A, T. Yang Parallel Programming with OpenMP CS240A, T. Yang 1 A Programmer s View of OpenMP What is OpenMP? Open specification for Multi-Processing Standard API for defining multi-threaded shared-memory programs

More information

HPC Practical Course Part 3.1 Open Multi-Processing (OpenMP)

HPC Practical Course Part 3.1 Open Multi-Processing (OpenMP) HPC Practical Course Part 3.1 Open Multi-Processing (OpenMP) V. Akishina, I. Kisel, G. Kozlov, I. Kulakov, M. Pugach, M. Zyzak Goethe University of Frankfurt am Main 2015 Task Parallelism Parallelization

More information

Programming Shared-memory Platforms with OpenMP. Xu Liu

Programming Shared-memory Platforms with OpenMP. Xu Liu Programming Shared-memory Platforms with OpenMP Xu Liu Introduction to OpenMP OpenMP directives concurrency directives parallel regions loops, sections, tasks Topics for Today synchronization directives

More information

Introduction to OpenMP.

Introduction to OpenMP. Introduction to OpenMP www.openmp.org Motivation Parallelize the following code using threads: for (i=0; i

More information

OpenMP programming. Thomas Hauser Director Research Computing Research CU-Boulder

OpenMP programming. Thomas Hauser Director Research Computing Research CU-Boulder OpenMP programming Thomas Hauser Director Research Computing thomas.hauser@colorado.edu CU meetup 1 Outline OpenMP Shared-memory model Parallel for loops Declaring private variables Critical sections Reductions

More information

OpenMP. Dr. William McDoniel and Prof. Paolo Bientinesi WS17/18. HPAC, RWTH Aachen

OpenMP. Dr. William McDoniel and Prof. Paolo Bientinesi WS17/18. HPAC, RWTH Aachen OpenMP Dr. William McDoniel and Prof. Paolo Bientinesi HPAC, RWTH Aachen mcdoniel@aices.rwth-aachen.de WS17/18 Loop construct - Clauses #pragma omp for [clause [, clause]...] The following clauses apply:

More information

OpenMP loops. Paolo Burgio.

OpenMP loops. Paolo Burgio. OpenMP loops Paolo Burgio paolo.burgio@unimore.it Outline Expressing parallelism Understanding parallel threads Memory Data management Data clauses Synchronization Barriers, locks, critical sections Work

More information

Topics. Introduction. Shared Memory Parallelization. Example. Lecture 11. OpenMP Execution Model Fork-Join model 5/15/2012. Introduction OpenMP

Topics. Introduction. Shared Memory Parallelization. Example. Lecture 11. OpenMP Execution Model Fork-Join model 5/15/2012. Introduction OpenMP Topics Lecture 11 Introduction OpenMP Some Examples Library functions Environment variables 1 2 Introduction Shared Memory Parallelization OpenMP is: a standard for parallel programming in C, C++, and

More information

Multi-core Architecture and Programming

Multi-core Architecture and Programming Multi-core Architecture and Programming Yang Quansheng( 杨全胜 ) http://www.njyangqs.com School of Computer Science & Engineering 1 http://www.njyangqs.com Programming with OpenMP Content What is PpenMP Parallel

More information

Progress on OpenMP Specifications

Progress on OpenMP Specifications Progress on OpenMP Specifications Wednesday, November 13, 2012 Bronis R. de Supinski Chair, OpenMP Language Committee This work has been authored by Lawrence Livermore National Security, LLC under contract

More information

Review. Lecture 12 5/22/2012. Compiler Directives. Library Functions Environment Variables. Compiler directives for construct, collapse clause

Review. Lecture 12 5/22/2012. Compiler Directives. Library Functions Environment Variables. Compiler directives for construct, collapse clause Review Lecture 12 Compiler Directives Conditional compilation Parallel construct Work-sharing constructs for, section, single Synchronization Work-tasking Library Functions Environment Variables 1 2 13b.cpp

More information

EE/CSCI 451: Parallel and Distributed Computation

EE/CSCI 451: Parallel and Distributed Computation EE/CSCI 451: Parallel and Distributed Computation Lecture #7 2/5/2017 Xuehai Qian Xuehai.qian@usc.edu http://alchem.usc.edu/portal/xuehaiq.html University of Southern California 1 Outline From last class

More information

OpenMP I. Diego Fabregat-Traver and Prof. Paolo Bientinesi WS16/17. HPAC, RWTH Aachen

OpenMP I. Diego Fabregat-Traver and Prof. Paolo Bientinesi WS16/17. HPAC, RWTH Aachen OpenMP I Diego Fabregat-Traver and Prof. Paolo Bientinesi HPAC, RWTH Aachen fabregat@aices.rwth-aachen.de WS16/17 OpenMP References Using OpenMP: Portable Shared Memory Parallel Programming. The MIT Press,

More information

EE/CSCI 451 Introduction to Parallel and Distributed Computation. Discussion #4 2/3/2017 University of Southern California

EE/CSCI 451 Introduction to Parallel and Distributed Computation. Discussion #4 2/3/2017 University of Southern California EE/CSCI 451 Introduction to Parallel and Distributed Computation Discussion #4 2/3/2017 University of Southern California 1 USC HPCC Access Compile Submit job OpenMP Today s topic What is OpenMP OpenMP

More information

COSC 6374 Parallel Computation. Introduction to OpenMP(I) Some slides based on material by Barbara Chapman (UH) and Tim Mattson (Intel)

COSC 6374 Parallel Computation. Introduction to OpenMP(I) Some slides based on material by Barbara Chapman (UH) and Tim Mattson (Intel) COSC 6374 Parallel Computation Introduction to OpenMP(I) Some slides based on material by Barbara Chapman (UH) and Tim Mattson (Intel) Edgar Gabriel Fall 2014 Introduction Threads vs. processes Recap of

More information

OpenMP Programming. Prof. Thomas Sterling. High Performance Computing: Concepts, Methods & Means

OpenMP Programming. Prof. Thomas Sterling. High Performance Computing: Concepts, Methods & Means High Performance Computing: Concepts, Methods & Means OpenMP Programming Prof. Thomas Sterling Department of Computer Science Louisiana State University February 8 th, 2007 Topics Introduction Overview

More information

Shared Memory Parallelism using OpenMP

Shared Memory Parallelism using OpenMP Indian Institute of Science Bangalore, India भ रत य व ज ञ न स स थ न ब गल र, भ रत SE 292: High Performance Computing [3:0][Aug:2014] Shared Memory Parallelism using OpenMP Yogesh Simmhan Adapted from: o

More information

OpenMP 4.0/4.5: New Features and Protocols. Jemmy Hu

OpenMP 4.0/4.5: New Features and Protocols. Jemmy Hu OpenMP 4.0/4.5: New Features and Protocols Jemmy Hu SHARCNET HPC Consultant University of Waterloo May 10, 2017 General Interest Seminar Outline OpenMP overview Task constructs in OpenMP SIMP constructs

More information

ME759 High Performance Computing for Engineering Applications

ME759 High Performance Computing for Engineering Applications ME759 High Performance Computing for Engineering Applications Parallel Computing on Multicore CPUs October 25, 2013 Dan Negrut, 2013 ME964 UW-Madison A programming language is low level when its programs

More information

Introduction to OpenMP

Introduction to OpenMP Introduction to OpenMP Christian Terboven 10.04.2013 / Darmstadt, Germany Stand: 06.03.2013 Version 2.3 Rechen- und Kommunikationszentrum (RZ) History De-facto standard for

More information

Shared Memory Programming with OpenMP

Shared Memory Programming with OpenMP Shared Memory Programming with OpenMP (An UHeM Training) Süha Tuna Informatics Institute, Istanbul Technical University February 12th, 2016 2 Outline - I Shared Memory Systems Threaded Programming Model

More information

Introduction to Standard OpenMP 3.1

Introduction to Standard OpenMP 3.1 Introduction to Standard OpenMP 3.1 Massimiliano Culpo - m.culpo@cineca.it Gian Franco Marras - g.marras@cineca.it CINECA - SuperComputing Applications and Innovation Department 1 / 59 Outline 1 Introduction

More information

OpenMP. Diego Fabregat-Traver and Prof. Paolo Bientinesi WS16/17. HPAC, RWTH Aachen

OpenMP. Diego Fabregat-Traver and Prof. Paolo Bientinesi WS16/17. HPAC, RWTH Aachen OpenMP Diego Fabregat-Traver and Prof. Paolo Bientinesi HPAC, RWTH Aachen fabregat@aices.rwth-aachen.de WS16/17 Worksharing constructs To date: #pragma omp parallel created a team of threads We distributed

More information

OpenMP C and C++ Application Program Interface Version 1.0 October Document Number

OpenMP C and C++ Application Program Interface Version 1.0 October Document Number OpenMP C and C++ Application Program Interface Version 1.0 October 1998 Document Number 004 2229 001 Contents Page v Introduction [1] 1 Scope............................. 1 Definition of Terms.........................

More information

[Potentially] Your first parallel application

[Potentially] Your first parallel application [Potentially] Your first parallel application Compute the smallest element in an array as fast as possible small = array[0]; for( i = 0; i < N; i++) if( array[i] < small ) ) small = array[i] 64-bit Intel

More information

OpenMP Algoritmi e Calcolo Parallelo. Daniele Loiacono

OpenMP Algoritmi e Calcolo Parallelo. Daniele Loiacono OpenMP Algoritmi e Calcolo Parallelo References Useful references Using OpenMP: Portable Shared Memory Parallel Programming, Barbara Chapman, Gabriele Jost and Ruud van der Pas OpenMP.org http://openmp.org/

More information

CS4961 Parallel Programming. Lecture 5: More OpenMP, Introduction to Data Parallel Algorithms 9/5/12. Administrative. Mary Hall September 4, 2012

CS4961 Parallel Programming. Lecture 5: More OpenMP, Introduction to Data Parallel Algorithms 9/5/12. Administrative. Mary Hall September 4, 2012 CS4961 Parallel Programming Lecture 5: More OpenMP, Introduction to Data Parallel Algorithms Administrative Mailing list set up, everyone should be on it - You should have received a test mail last night

More information

Introduction to OpenMP

Introduction to OpenMP Introduction to OpenMP Ricardo Fonseca https://sites.google.com/view/rafonseca2017/ Outline Shared Memory Programming OpenMP Fork-Join Model Compiler Directives / Run time library routines Compiling and

More information

Multithreading in C with OpenMP

Multithreading in C with OpenMP Multithreading in C with OpenMP ICS432 - Spring 2017 Concurrent and High-Performance Programming Henri Casanova (henric@hawaii.edu) Pthreads are good and bad! Multi-threaded programming in C with Pthreads

More information

Parallel Programming

Parallel Programming Parallel Programming OpenMP Nils Moschüring PhD Student (LMU) Nils Moschüring PhD Student (LMU), OpenMP 1 1 Overview What is parallel software development Why do we need parallel computation? Problems

More information

Computational Mathematics

Computational Mathematics Computational Mathematics Hamid Sarbazi-Azad Department of Computer Engineering Sharif University of Technology e-mail: azad@sharif.edu OpenMP Work-sharing Instructor PanteA Zardoshti Department of Computer

More information

Tasking in OpenMP. Paolo Burgio.

Tasking in OpenMP. Paolo Burgio. asking in OpenMP Paolo Burgio paolo.burgio@unimore.it Outline Expressing parallelism Understanding parallel threads Memory Data management Data clauses Synchronization Barriers, locks, critical sections

More information

OpenMP - II. Diego Fabregat-Traver and Prof. Paolo Bientinesi WS15/16. HPAC, RWTH Aachen

OpenMP - II. Diego Fabregat-Traver and Prof. Paolo Bientinesi WS15/16. HPAC, RWTH Aachen OpenMP - II Diego Fabregat-Traver and Prof. Paolo Bientinesi HPAC, RWTH Aachen fabregat@aices.rwth-aachen.de WS15/16 OpenMP References Using OpenMP: Portable Shared Memory Parallel Programming. The MIT

More information

EPL372 Lab Exercise 5: Introduction to OpenMP

EPL372 Lab Exercise 5: Introduction to OpenMP EPL372 Lab Exercise 5: Introduction to OpenMP References: https://computing.llnl.gov/tutorials/openmp/ http://openmp.org/wp/openmp-specifications/ http://openmp.org/mp-documents/openmp-4.0-c.pdf http://openmp.org/mp-documents/openmp4.0.0.examples.pdf

More information

Distributed Systems + Middleware Concurrent Programming with OpenMP

Distributed Systems + Middleware Concurrent Programming with OpenMP Distributed Systems + Middleware Concurrent Programming with OpenMP Gianpaolo Cugola Dipartimento di Elettronica e Informazione Politecnico, Italy cugola@elet.polimi.it http://home.dei.polimi.it/cugola

More information

Computer Architecture

Computer Architecture Jens Teubner Computer Architecture Summer 2016 1 Computer Architecture Jens Teubner, TU Dortmund jens.teubner@cs.tu-dortmund.de Summer 2016 Jens Teubner Computer Architecture Summer 2016 2 Part I Programming

More information

Programming Shared Memory Systems with OpenMP Part I. Book

Programming Shared Memory Systems with OpenMP Part I. Book Programming Shared Memory Systems with OpenMP Part I Instructor Dr. Taufer Book Parallel Programming in OpenMP by Rohit Chandra, Leo Dagum, Dave Kohr, Dror Maydan, Jeff McDonald, Ramesh Menon 2 1 Machine

More information

Overview: The OpenMP Programming Model

Overview: The OpenMP Programming Model Overview: The OpenMP Programming Model motivation and overview the parallel directive: clauses, equivalent pthread code, examples the for directive and scheduling of loop iterations Pi example in OpenMP

More information

Introduction to OpenMP

Introduction to OpenMP Introduction to OpenMP Le Yan Scientific computing consultant User services group High Performance Computing @ LSU Goals Acquaint users with the concept of shared memory parallelism Acquaint users with

More information

COSC 6374 Parallel Computation. Introduction to OpenMP. Some slides based on material by Barbara Chapman (UH) and Tim Mattson (Intel)

COSC 6374 Parallel Computation. Introduction to OpenMP. Some slides based on material by Barbara Chapman (UH) and Tim Mattson (Intel) COSC 6374 Parallel Computation Introduction to OpenMP Some slides based on material by Barbara Chapman (UH) and Tim Mattson (Intel) Edgar Gabriel Fall 2015 OpenMP Provides thread programming model at a

More information

Parallel Programming: OpenMP

Parallel Programming: OpenMP Parallel Programming: OpenMP Xianyi Zeng xzeng@utep.edu Department of Mathematical Sciences The University of Texas at El Paso. November 10, 2016. An Overview of OpenMP OpenMP: Open Multi-Processing An

More information

Review. Tasking. 34a.cpp. Lecture 14. Work Tasking 5/31/2011. Structured block. Parallel construct. Working-Sharing contructs.

Review. Tasking. 34a.cpp. Lecture 14. Work Tasking 5/31/2011. Structured block. Parallel construct. Working-Sharing contructs. Review Lecture 14 Structured block Parallel construct clauses Working-Sharing contructs for, single, section for construct with different scheduling strategies 1 2 Tasking Work Tasking New feature in OpenMP

More information

15-418, Spring 2008 OpenMP: A Short Introduction

15-418, Spring 2008 OpenMP: A Short Introduction 15-418, Spring 2008 OpenMP: A Short Introduction This is a short introduction to OpenMP, an API (Application Program Interface) that supports multithreaded, shared address space (aka shared memory) parallelism.

More information

Programming with OpenMP*

Programming with OpenMP* Objectives At the completion of this module you will be able to Thread serial code with basic OpenMP pragmas Use OpenMP synchronization pragmas to coordinate thread execution and memory access 2 Agenda

More information

Shared Memory Parallel Programming with Pthreads An overview

Shared Memory Parallel Programming with Pthreads An overview Shared Memory Parallel Programming with Pthreads An overview Part II Ing. Andrea Marongiu (a.marongiu@unibo.it) Includes slides from ECE459: Programming for Performance course at University of Waterloo

More information

Tasking in OpenMP 4. Mirko Cestari - Marco Rorro -

Tasking in OpenMP 4. Mirko Cestari - Marco Rorro - Tasking in OpenMP 4 Mirko Cestari - m.cestari@cineca.it Marco Rorro - m.rorro@cineca.it Outline Introduction to OpenMP General characteristics of Taks Some examples Live Demo Multi-threaded process Each

More information

https://www.youtube.com/playlist?list=pllx- Q6B8xqZ8n8bwjGdzBJ25X2utwnoEG

https://www.youtube.com/playlist?list=pllx- Q6B8xqZ8n8bwjGdzBJ25X2utwnoEG https://www.youtube.com/playlist?list=pllx- Q6B8xqZ8n8bwjGdzBJ25X2utwnoEG OpenMP Basic Defs: Solution Stack HW System layer Prog. User layer Layer Directives, Compiler End User Application OpenMP library

More information

OpenMP Application Program Interface

OpenMP Application Program Interface OpenMP Application Program Interface Version.0 - RC - March 01 Public Review Release Candidate Copyright 1-01 OpenMP Architecture Review Board. Permission to copy without fee all or part of this material

More information

Allows program to be incrementally parallelized

Allows program to be incrementally parallelized Basic OpenMP What is OpenMP An open standard for shared memory programming in C/C+ + and Fortran supported by Intel, Gnu, Microsoft, Apple, IBM, HP and others Compiler directives and library support OpenMP

More information

Multicore Computing. Arash Tavakkol. Instructor: Department of Computer Engineering Sharif University of Technology Spring 2016

Multicore Computing. Arash Tavakkol. Instructor: Department of Computer Engineering Sharif University of Technology Spring 2016 Multicore Computing Instructor: Arash Tavakkol Department of Computer Engineering Sharif University of Technology Spring 2016 Shared Memory Programming Using OpenMP Some Slides come From Parallel Programmingin

More information

OpenMP threading: parallel regions. Paolo Burgio

OpenMP threading: parallel regions. Paolo Burgio OpenMP threading: parallel regions Paolo Burgio paolo.burgio@unimore.it Outline Expressing parallelism Understanding parallel threads Memory Data management Data clauses Synchronization Barriers, locks,

More information

Shared memory programming model OpenMP TMA4280 Introduction to Supercomputing

Shared memory programming model OpenMP TMA4280 Introduction to Supercomputing Shared memory programming model OpenMP TMA4280 Introduction to Supercomputing NTNU, IMF February 16. 2018 1 Recap: Distributed memory programming model Parallelism with MPI. An MPI execution is started

More information

Session 4: Parallel Programming with OpenMP

Session 4: Parallel Programming with OpenMP Session 4: Parallel Programming with OpenMP Xavier Martorell Barcelona Supercomputing Center Agenda Agenda 10:00-11:00 OpenMP fundamentals, parallel regions 11:00-11:30 Worksharing constructs 11:30-12:00

More information

1 of 6 Lecture 7: March 4. CISC 879 Software Support for Multicore Architectures Spring Lecture 7: March 4, 2008

1 of 6 Lecture 7: March 4. CISC 879 Software Support for Multicore Architectures Spring Lecture 7: March 4, 2008 1 of 6 Lecture 7: March 4 CISC 879 Software Support for Multicore Architectures Spring 2008 Lecture 7: March 4, 2008 Lecturer: Lori Pollock Scribe: Navreet Virk Open MP Programming Topics covered 1. Introduction

More information

CMSC 714 Lecture 4 OpenMP and UPC. Chau-Wen Tseng (from A. Sussman)

CMSC 714 Lecture 4 OpenMP and UPC. Chau-Wen Tseng (from A. Sussman) CMSC 714 Lecture 4 OpenMP and UPC Chau-Wen Tseng (from A. Sussman) Programming Model Overview Message passing (MPI, PVM) Separate address spaces Explicit messages to access shared data Send / receive (MPI

More information

UvA-SARA High Performance Computing Course June Clemens Grelck, University of Amsterdam. Parallel Programming with Compiler Directives: OpenMP

UvA-SARA High Performance Computing Course June Clemens Grelck, University of Amsterdam. Parallel Programming with Compiler Directives: OpenMP Parallel Programming with Compiler Directives OpenMP Clemens Grelck University of Amsterdam UvA-SARA High Performance Computing Course June 2013 OpenMP at a Glance Loop Parallelization Scheduling Parallel

More information

OpenMP Examples - Tasking

OpenMP Examples - Tasking Dipartimento di Ingegneria Industriale e dell Informazione University of Pavia December 4, 2017 Outline 1 2 Assignment 2: Quicksort Assignment 3: Jacobi Outline 1 2 Assignment 2: Quicksort Assignment 3:

More information

Introduction to OpenMP

Introduction to OpenMP Introduction to OpenMP Ekpe Okorafor School of Parallel Programming & Parallel Architecture for HPC ICTP October, 2014 A little about me! PhD Computer Engineering Texas A&M University Computer Science

More information

Data Environment: Default storage attributes

Data Environment: Default storage attributes COSC 6374 Parallel Computation Introduction to OpenMP(II) Some slides based on material by Barbara Chapman (UH) and Tim Mattson (Intel) Edgar Gabriel Fall 2014 Data Environment: Default storage attributes

More information

A Short Introduction to OpenMP. Mark Bull, EPCC, University of Edinburgh

A Short Introduction to OpenMP. Mark Bull, EPCC, University of Edinburgh A Short Introduction to OpenMP Mark Bull, EPCC, University of Edinburgh Overview Shared memory systems Basic Concepts in Threaded Programming Basics of OpenMP Parallel regions Parallel loops 2 Shared memory

More information

CS 470 Spring Mike Lam, Professor. Advanced OpenMP

CS 470 Spring Mike Lam, Professor. Advanced OpenMP CS 470 Spring 2018 Mike Lam, Professor Advanced OpenMP Atomics OpenMP provides access to highly-efficient hardware synchronization mechanisms Use the atomic pragma to annotate a single statement Statement

More information

OpenMP. Application Program Interface. CINECA, 14 May 2012 OpenMP Marco Comparato

OpenMP. Application Program Interface. CINECA, 14 May 2012 OpenMP Marco Comparato OpenMP Application Program Interface Introduction Shared-memory parallelism in C, C++ and Fortran compiler directives library routines environment variables Directives single program multiple data (SPMD)

More information

Jukka Julku Multicore programming: Low-level libraries. Outline. Processes and threads TBB MPI UPC. Examples

Jukka Julku Multicore programming: Low-level libraries. Outline. Processes and threads TBB MPI UPC. Examples Multicore Jukka Julku 19.2.2009 1 2 3 4 5 6 Disclaimer There are several low-level, languages and directive based approaches But no silver bullets This presentation only covers some examples of them is

More information

OPENMP OPEN MULTI-PROCESSING

OPENMP OPEN MULTI-PROCESSING OPENMP OPEN MULTI-PROCESSING OpenMP OpenMP is a portable directive-based API that can be used with FORTRAN, C, and C++ for programming shared address space machines. OpenMP provides the programmer with

More information

OpenMP: Open Multiprocessing

OpenMP: Open Multiprocessing OpenMP: Open Multiprocessing Erik Schnetter June 7, 2012, IHPC 2012, Iowa City Outline 1. Basic concepts, hardware architectures 2. OpenMP Programming 3. How to parallelise an existing code 4. Advanced

More information

Shared memory parallel computing

Shared memory parallel computing Shared memory parallel computing OpenMP Sean Stijven Przemyslaw Klosiewicz Shared-mem. programming API for SMP machines Introduced in 1997 by the OpenMP Architecture Review Board! More high-level than

More information

Parallel Programming with OpenMP. CS240A, T. Yang, 2013 Modified from Demmel/Yelick s and Mary Hall s Slides

Parallel Programming with OpenMP. CS240A, T. Yang, 2013 Modified from Demmel/Yelick s and Mary Hall s Slides Parallel Programming with OpenMP CS240A, T. Yang, 203 Modified from Demmel/Yelick s and Mary Hall s Slides Introduction to OpenMP What is OpenMP? Open specification for Multi-Processing Standard API for

More information

Make the Most of OpenMP Tasking. Sergi Mateo Bellido Compiler engineer

Make the Most of OpenMP Tasking. Sergi Mateo Bellido Compiler engineer Make the Most of OpenMP Tasking Sergi Mateo Bellido Compiler engineer 14/11/2017 Outline Intro Data-sharing clauses Cutoff clauses Scheduling clauses 2 Intro: what s a task? A task is a piece of code &

More information

Shared Memory Programming Paradigm!

Shared Memory Programming Paradigm! Shared Memory Programming Paradigm! Ivan Girotto igirotto@ictp.it Information & Communication Technology Section (ICTS) International Centre for Theoretical Physics (ICTP) 1 Multi-CPUs & Multi-cores NUMA

More information

OpenMP Introduction. CS 590: High Performance Computing. OpenMP. A standard for shared-memory parallel programming. MP = multiprocessing

OpenMP Introduction. CS 590: High Performance Computing. OpenMP. A standard for shared-memory parallel programming. MP = multiprocessing CS 590: High Performance Computing OpenMP Introduction Fengguang Song Department of Computer Science IUPUI OpenMP A standard for shared-memory parallel programming. MP = multiprocessing Designed for systems

More information

Introduction to OpenMP. Martin Čuma Center for High Performance Computing University of Utah

Introduction to OpenMP. Martin Čuma Center for High Performance Computing University of Utah Introduction to OpenMP Martin Čuma Center for High Performance Computing University of Utah mcuma@chpc.utah.edu Overview Quick introduction. Parallel loops. Parallel loop directives. Parallel sections.

More information

OpenMP 4. CSCI 4850/5850 High-Performance Computing Spring 2018

OpenMP 4. CSCI 4850/5850 High-Performance Computing Spring 2018 OpenMP 4 CSCI 4850/5850 High-Performance Computing Spring 2018 Tae-Hyuk (Ted) Ahn Department of Computer Science Program of Bioinformatics and Computational Biology Saint Louis University Learning Objectives

More information

An Introduction to OpenMP

An Introduction to OpenMP Dipartimento di Ingegneria Industriale e dell'informazione University of Pavia December 4, 2017 Recap Parallel machines are everywhere Many architectures, many programming model. Among them: multithreading.

More information

Synchronization. Event Synchronization

Synchronization. Event Synchronization Synchronization Synchronization: mechanisms by which a parallel program can coordinate the execution of multiple threads Implicit synchronizations Explicit synchronizations Main use of explicit synchronization

More information

OpenMP: Open Multiprocessing

OpenMP: Open Multiprocessing OpenMP: Open Multiprocessing Erik Schnetter May 20-22, 2013, IHPC 2013, Iowa City 2,500 BC: Military Invents Parallelism Outline 1. Basic concepts, hardware architectures 2. OpenMP Programming 3. How to

More information

OpenMP 2. CSCI 4850/5850 High-Performance Computing Spring 2018

OpenMP 2. CSCI 4850/5850 High-Performance Computing Spring 2018 OpenMP 2 CSCI 4850/5850 High-Performance Computing Spring 2018 Tae-Hyuk (Ted) Ahn Department of Computer Science Program of Bioinformatics and Computational Biology Saint Louis University Learning Objectives

More information

Cluster Computing 2008

Cluster Computing 2008 Objectives At the completion of this module you will be able to Thread serial code with basic OpenMP pragmas Use OpenMP synchronization pragmas to coordinate thread execution and memory access Based on

More information

New Features after OpenMP 2.5

New Features after OpenMP 2.5 New Features after OpenMP 2.5 2 Outline OpenMP Specifications Version 3.0 Task Parallelism Improvements to nested and loop parallelism Additional new Features Version 3.1 - New Features Version 4.0 simd

More information

OpenMP. OpenMP. Portable programming of shared memory systems. It is a quasi-standard. OpenMP-Forum API for Fortran and C/C++

OpenMP. OpenMP. Portable programming of shared memory systems. It is a quasi-standard. OpenMP-Forum API for Fortran and C/C++ OpenMP OpenMP Portable programming of shared memory systems. It is a quasi-standard. OpenMP-Forum 1997-2002 API for Fortran and C/C++ directives runtime routines environment variables www.openmp.org 1

More information