1 / 19

OpenMP Parallel Directives for Fortran Scientific Computing Department SDSC

OpenMP Parallel Directives for Fortran Scientific Computing Department SDSC. OpenMP and Directives. OpenMP is a parallel programming system based on Fortran directives

brit
Download Presentation

OpenMP Parallel Directives for Fortran Scientific Computing Department SDSC

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. OpenMP Parallel Directives for FortranScientific Computing Department SDSC

  2. OpenMP and Directives OpenMP is a parallel programming system based on Fortran directives Directives are special comments that are inserted into the source code to control parallel execution on a shared-memory machine. All directives begin with the !$OMP, C$OMP or *$OMP sentinel. To simplify things and avoid problems associated with the Fortran free and fixed source forms, use the !$OMP sentinel starting in column 1. Example directives include: !$OMP parallel !$OMP do parallel !$OMP end parallel

  3. A Simple Example - Parallel Loop !$OMP PARALLEL DO do i=1,128 b(i) = a(i) + c(i) enddo !$OMP END PARALLEL DO (note - END PARALLEL DO opt) The first directive specifies that the loop immediately following should be executed in parallel. The second directive specifies the end of the parallel section. For codes that spend the majority of their time executing the content of simple loops, the PARALLEL DO directive can result in significant parallel performance.

  4. Distribution of work - SCHEDULE Clause The division of work among CPUs can be controlled with the SCHEDULE clause. For example !$OMP PARALLEL DO SCHEDULE(STATIC) Iterations divided among the CPUs in contiguous chunks !$OMP PARALLEL DO SCHEDULE(STATIC, N) Iterations divided round-robin fashion in chunks of size N !$OMP PARALLEL DO SCHEDULE(DYNAMIC,N) Iterations handed out in chunks of size N as CPUs become available

  5. Example - SCHEDULE(STATIC) CPU2: do i=65,96 a(i)=b(i)+c(i) enddo CPU3: do i=97,128 a(i)=b(i)+c(i) enddo CPU0: do i=1,32 a(i)=b(i)+c(i) enddo CPU1: do i=33,64 a(i)=b(i)+ c(i) enddo

  6. Example - SCHEDULE(STATIC,16) CPU0: do i=1,16 a(i)=b(i)+c(i) enddo do i=65,80 a(i)=b(i)+c(i) enddo CPU1: do i=17,32 a(i)=b(i)+c(i) enddo do i=81,96 a(i)=b(i)+c(i) enddo CPU2: do i=33, 48 a(i)=b(i)+c(i) enddo do i=97,112 a(i)=b(i)+c(i) enddo CPU3: do i=49,64 a(i)=b(i)+c(i) enddo do i=113,128 a(i)=b(i)+c(i) enddo

  7. PRIVATE and SHARED Data SHARED - variable is shared by all processors PRIVATE - each processor has a private copy of a variable In the previous example of a simple parallel loop, we relied on the OpenMP defaults. Explicitly, the loop would be written as !$OMP PARALLEL DO SHARED(A,B,C,N) PRIVATE(I) do I=1,N B(I) = A(I) + C(I) enddo !$OMP END PARALLEL DO All CPUs have access to the same storage area for A, B, C and N, but each loop needs its own private value of the loop index I.

  8. PRIVATE Data Example In the following loop, each processor needs its own private copy of the variable TEMP. If TEMP were shared, the result would be unpredictable since multiple processors would be writing to the same memory location. !$OMP PARALLEL DO SHARED(A,B,C,N) PRIVATE(I,TEMP) do I=1,N TEMP = A(I)/B(I) C(I) = TEMP + SQRT(TEMP) enddo !$OMP END PARALLEL DO

  9. FIRSTPRIVATE / LASTPRIVATE FIRSTPRIVATE: Private copies of the variables are initialized from the original objects. Use can lead to better performing code LASTPRIVATE: On exiting the parallel region or loop, variable has the value that it would have had in the case of serial execution A = 2.0 c On entry, each thread has A equal to 2.0 !$OMP PARALLEL DO FIRSTPRIVATE(A),LASTPRIVATE(I) DO I=1,N Z(I) = A*X(I) + Y(I) ENDDO C On exit, I is set to N

  10. REDUCTION variables Variables that are used in collective operations over the elements of an array can be labeled as REDUCTION variables. ASUM = 0.0 APROD = 1.0 !$OMP PARALLEL DO REDUCTION(+:ASUM) REDUCTION(*:APROD) do I=1,n ASUM = ASUM + A(I) APROD = APROD * A(I) enddo !$OMP END PARALLEL DO Each processor has its own copy of ASUM and APROD. After the parallel work is finished, the master processor collects the values generated by each processor and performs global reduction.

  11. Parallel regions The !$OMP PARALLEL directive can be used to mark entire regions as parallel. The following two examples are equivalent !$OMP PARALLEL !$OMP DO do i=1,n a(i)=b(i)+c(i) enddo !$OMP DO do i=1,n x(i)=y(i)+z(i) enddo !$OMP END PARALLEL !$OMP PARALLEL DO do i=1,n a(i)=b(i)+c(i) enddo !$OMP PARALLEL DO do i=1,n x(i)=y(i)+z(i) enddo

  12. A more practical example using !$OMP PARALLEL !$OMP PARALLEL !$OMP DO do i=1,n a(i)=b(i)+c(i) enddo !$OMP END DO NOWAIT !$OMP DO do i=1,n x(i)=y(i)+z(i) enddo !$OMP END DO NOWAIT !$OMP END PARALLEL When a parallel region is exited, a barrier is implied - all threads must reach the barrier before any can proceed. By using the NOWAIT clause at the end of each loop inside the parallel region, an unnecessary synchronization of threads can be avoided.

  13. A note on OpenMP parallel loop directives OpenMP provides two sets of directives for specifying a parallel loop. Their appropriate use is detailed below !$OMP DO / !$OMP END DO Used inside parallel regions marked with the PARALLEL/END PARALLEL directives !$OMP PARALLEL DO / !$OMP END PARALLEL DO Used to mark isolated loops outside of parallel regions

  14. Critical Regions Certain parallel programs may require that each processor execute a section of code, where it is critical that only one processor execute the code section at a time. These regions can be marked with the CRITICAL / END CRITICAL directives. !$OMP PARALLEL SHARED(X,Y) !$OMP CRITICAL (SECTION1) call subroutine update(x) !$OMP END CRITICAL (SECTION1) !$OMP CRITICAL (SECTION2) call subroutine update(y) !$OMP END CRITICAL (SECTION2) !$OMP END PARALLEL

  15. More about OpenMP • Other important features of Open MP • BARRIER - all threads must synchronize at barrier • ORDERED - order of execution matches serial code • OpenMP runtime library - returns information on threads For more complete details see the OpenMP web site at http://www.openmp.org

  16. OpenMP Runtimes 2d optics program kernel (20 * 1024x1024 ffts with convolution) Run on 4 processors of Cray T90 with compiler version 3.1.0.0 Run with and without OpenMP directives

  17. OpenMP on the IBM SP • The IBM XL Fortran compiler supports a subset of OpenMP • Does not provide the END PARALLEL DO directive. Not serious since parallel loops are assumed to end following ENDDO • Does not currently support BARRIER directive • Does not recognize OpenMP conditional compilation • Does not support OpenMP run-time library routines, but does provide some of the functionality through NUM_PARTHDS() and NUM_USRTHDS() • See RS/6000 Scientific and technical Computing: POWER3 Introduction and Tuning Guide for more information (http://www.redbooks.ibm.com)

  18. OpenMP and parallelizing compilers Many compilers are able to parallelize simple loops. The main utility of OpenMP is that the programmer can provide more information to the compiler to aid in parallelization. Directive lets the compiler know that there are no repeated values of INDEX on lhs !$OMP PERMUTATION(INDEX) DO I=1,N A(INDEX(I)) = A(INDEX(I) + B(I) ENDDO !$OMP PARALLEL DO DO I=1,N A(I) = FUNC(A,B,I) ENDDO Without directive, compiler cannot be sure that function or subroutine calls have no unintended side effects

  19. OpenMP Partners • The OpenMP Architecture Review Board is comprised of the following organizations. • Compaq (Digital) • Hewlett-Packard Company • Intel Corporation • International Business Machines (IBM) • Kuck & Associates, Inc. (KAI) • Silicon Graphics, Inc. • Sun Microsystems, Inc. • U.S. Department of Energy ASCI program • The following software vendors also endorse the OpenMP API: • Absoft Corporation • Edinburgh Portable Compilers • Etnus, Inc. • GENIAS Software GmBH • Myrias Computer Technologies, Inc. • The Portland Group, Inc. (PGI)

More Related