Presentation is loading. Please wait.

Presentation is loading. Please wait.

OpenMP – Part 2 * *UHEM yaz çalıştayı notlarından derlenmiştir. (uhem.itu.edu.tr)

Similar presentations


Presentation on theme: "OpenMP – Part 2 * *UHEM yaz çalıştayı notlarından derlenmiştir. (uhem.itu.edu.tr)"— Presentation transcript:

1 OpenMP – Part 2 * *UHEM yaz çalıştayı notlarından derlenmiştir. (uhem.itu.edu.tr)

2 SECTIONS construct: –Easiest way to get different threads to carry out different kinds of work –Each section must be a structured block of code that is independent of the other sections –If there are fewer code blocks than threads, the remaining threads will be idle –If there are fewer threads than code blocks, some or all of the threads execute multiple code blocks –Depending on the type of work, this construct might lead to a load- balancing problem Work-Sharing Lab 2

3 SECTIONS construct for 2 functions (or threads) #pragma omp parallel { #pragma omp sections { #pragma omp section { FUNCTION_1(MAX) } #pragma omp section { FUNCTION_2(MIN) } } // Sections Ends Here } // Parallel Ends Here Work-Sharing Lab 2

4 This example demonstrates use of the OpenMP SECTIONS worksharing construct Note how the PARALLEL region is divided into separate sections, each of which will be executed by one thread. Run the program several times and observe any differences in output. Because there are only two sections, you should notice that some threads do not do any work. You may/may not notice that the threads doing work can vary. For example, the first time thread 0 and thread 1 may do the work, and the next time it may be thread 0 and thread 3. Work-Sharing Lab 2 bash: $ icc -openmp omp_workshare2.c -o omp_workshare2 bash: $./omp_workshare2

5 Work-Sharing Constructs SINGLE Constructs: –It specifies that the enclosed code is to be executed by only one thread in the team. –The thread chosen could vary from one run to another. –Threads that are not executing in the SINGLE directive wait at the END OF SINGLE unless NOWAIT is specified. #pragma omp single [clause...] structured_block C/C++

6 SINGLE Constructs: Only one thread initializes the shared variable a Work-Sharing Constructs

7 Synchronization (BARRIER) BARRIER Directive: –Synchronizes all threads in the team. –When a BARRIER directive is reached, a thread will wait at that point until all other threads have reached that barrier. –All threads then resume executing in parallel the code that follows the barrier. C/C++ #pragma omp barrier structured_block

8 MASTER Directive: –Specifies a region that is to be executed only by the master thread of the team. –All other threads on the team skip this section of code –It is similar to the SINGLE construct Synchronization (MASTER) C/C++ #pragma omp master Statement_or_expression

9 A private copy for each list variable is created for each thread. At the end of the reduction, the reduction variable is applied to all private copies of the shared variable, and the final result is written to the global shared variable. Reduction C/C++ #pragma omp … reduction(operator:list) Statement_or_expression

10 Reduction The syntax of the clause is: reduction(operator:list) where list is the list of variables where the operator will be applied to, and operator is one of these:

11 Reduction by multiplication This example calculates factorial using threads: At the beginning of the parallel block, a private copy is made of the variable and preinitialized to a certain value. At the end of the parallel block, the private copy is atomically merged into the shared variable using the defined operator.

12 Reduction by sum

13 Reduction by max /min


Download ppt "OpenMP – Part 2 * *UHEM yaz çalıştayı notlarından derlenmiştir. (uhem.itu.edu.tr)"

Similar presentations


Ads by Google