Transcription of OpenMP Application Programming Interface …
1 OpenMPApplication ProgrammingInterfaceExamplesVersion November 2016 Source codes for OpenMP Examples can be downloaded from 1997-2016 OpenMP Architecture Review to copy without fee all or part of this material is granted, provided the OpenMPArchitecture Review Board copyright notice and the title of this document appear. Notice isgiven that copying is by permission of OpenMP Architecture Review page intentionally left blankContentsIntroduction1 Examples21. Parallel A Simple Parallel Loop .. TheparallelConstruct .. Controlling the Number of Threads on Multiple Nesting Levels .. Interaction Between thenum_threadsClause andomp_set_dynamic.. Fortran Restrictions on thedoConstruct .. ThenowaitClause .. ThecollapseClause.
2 In Loop Constructs .. Theparallel sectionsConstruct .. ThefirstprivateClause and thesectionsConstruct .. ThesingleConstruct .. TheworkshareConstruct .. ThemasterConstruct .. Parallel Random Access Iterator Loop .. Theomp_set_dynamicandomp_set_num_threads Routines .. Theomp_get_num_threadsRoutine .. 382. OpenMP Theproc_bindClause .. Spread Affinity Policy .. Close Affinity Policy .. Master Affinity Policy .. Affinity Query Functions .. 483. ThetaskandtaskwaitConstructs .. Task Priority .. Task Dependences .. Flow Dependence .. Anti-dependence .. Output Dependence .. Concurrent Execution with Dependences .. Matrix multiplication .. ThetaskgroupConstruct.
3 ThetaskyieldConstruct .. ThetaskloopConstruct .. 854.. onparallelConstruct .. withmapClause .. withto/frommap-types .. with Array Sections .. withifClause .. dataConstruct .. Simpletarget dataConstruct .. dataRegion Enclosing MultipletargetRegions .. dataConstruct with Orphaned Call .. dataConstruct withifClause .. enter dataandtarget exit dataConstructs .. updateConstruct .. Simpletarget dataandtarget updateConstructs .. updateConstruct withifClause .. targetConstruct .. targetandend declare targetfor a Function .. targetConstruct for Class Type .. 117iiOpenMP Examples Version - November targetandend declare targetfor Variables .. targetandend declare targetwithdeclare simd.
4 TargetDirective withlinkClause .. withomp_get_num_teamsandomp_get_team_num Routines .. ,teams, anddistributeConstructs .. teams, and Distribute Parallel Loop Constructs .. teamsand Distribute Parallel Loop Constructs with SchedulingClauses .. teamsanddistribute simdConstructs .. teamsand Distribute Parallel Loop SIMD Constructs .. AsynchronoustargetExecution and Dependences .. Asynchronoustargetwith Tasks .. ontargetConstruct .. AsynchronoustargetwithnowaitanddependCla uses .. Array Sections in Device Constructs .. Device Routines .. Target Memory and Device Pointers Routines .. 1525. simdConstructs .. Loop-Carried Lexical Forward Dependence .. 1656. ThecriticalConstruct .. Worksharing Constructs Inside acriticalConstruct.
5 Binding ofbarrierRegions .. TheatomicConstruct .. Restrictions on theatomicConstruct .. TheflushConstruct without a List .. TheorderedClause and theorderedConstruct .. Doacross Loop Nest .. Lock Routines .. Theomp_init_lockRoutine .. Theomp_init_lock_with_hintRoutine .. Ownership of Locks .. Simple Lock Routines .. Nestable Lock Routines .. 2067. Data ThethreadprivateDirective .. Thedefault(none)Clause .. TheprivateClause .. Fortran Private Loop Iteration Variables .. Fortran Restrictions onsharedandprivateClauses with Common Blocks .. Fortran Restrictions on Storage Association with theprivateClause .. C/C++ Arrays in afirstprivateClause .. ThelastprivateClause .. ThereductionClause.
6 ThecopyinClause .. ThecopyprivateClause .. C++ Reference in Data-Sharing Clauses .. FortranASSOCIATEC onstruct .. 2478. Memory The OpenMP Memory Model .. Race Conditions Caused by Implied Copies of Shared Variables in Fortran .. 2569. Program Conditional Compilation .. Internal Control Variables (ICVs) .. Placement offlush,barrier,taskwaitandtaskyieldDire ctives .. Cancellation Constructs .. 267ivOpenMP Examples Version - November Nested Loop Constructs .. Restrictions on Nesting of Regions .. 275A. Document Revision Changes from to .. Changes from to .. Changes from to .. Changes from to .. 282 ContentsvIntroduction1 This collection of Programming examples supplements the OpenMP API for Shared Memory2 Parallelization specifications, and is not part of the formal specifications.
7 It assumes familiarity3with the OpenMP specifications, and shares the typographical conventions used in that This first release of the OpenMP Examples reflects the OpenMP Version examples are being developed and will be published in future releases of this OpenMP API specification provides a model for parallel Programming that is portable across7shared memory architectures from different vendors. Compilers from numerous vendors support8the OpenMP directives, library routines, and environment variables demonstrated in this document allow10users to create and manage parallel programs while permitting portability. The directives extend the11C, C++ and Fortran base languages with single program multiple data (SPMD) constructs, tasking12constructs, device constructs, worksharing constructs, and synchronization constructs, and they13provide support for sharing and privatizing data.
8 The functionality to control the runtime14environment is provided by library routines and environment variables. Compilers that support the15 OpenMP API often include a command line option to the compiler that activates and allows16interpretation of all OpenMP latest source codes for OpenMP Examples can be downloaded from thesourcesdirectory at18 The codes for this OpenMP Examples document have19the information about the OpenMP API and a list of the compilers that support the OpenMP21 API can be found at the web site22 following are examples of the OpenMP API directives, constructs, and / C++A statement following a directive is compound only when necessary, and a non-compound3statement is indented with respect to a directive preceding / C++Each example is labeled , whereenameis the example name,seqnois the5sequence number in a section.
9 Andextis the source file extension to indicate the code type and6source one of the following:7 c C code,8 cpp C++ code,9 f Fortran code in fixed form, and10 f90 Fortran code in free 11 Parallel Execution2A single thread, theinitial thread, begins sequential execution of an OpenMP enabled program, as3if the whole program is in an implicit parallel region consisting of an implicit task executed by the4initial encloses code, forming a parallel region. Aninitial threadencountering a6parallelregion forks (creates) a team of threads at the beginning of theparallelregion, and7joins them (removes from execution) at the end of the region. The initial thread becomes the master8thread of the team in aparallelregion with athreadnumber equal to zero, the other threads are9numbered from 1 to number of threads minus 1.
10 A team may be comprised of just a single thread of a team is assigned an implicit task consisting of code within the parallel region. The11task that creates a parallel region is suspended while the tasks of the team are executed. A thread is12tied to its task; that is, only the thread assigned to the task can execute that task. After completion13of theparallelregion, the master thread resumes execution of the generating task within aparallelregion is allowed to encounter anotherparallelregion to form a15nestedparallelregion. The parallelism of a nestedparallelregion (whether it forks16additional threads, or is executed serially by the encountering task) can be controlled by the17 OMP_NESTED environment variable or theomp_set_nested()API routine with arguments18indicating true or number of threads of aparallelregion can be set by theOMP_NUM_THREADS20environment variable, theomp_set_num_threads()routine, or on theparalleldirective21with thenum_threadsclause.
