logo资料库

openmp官方文档.pdf

第1页 / 共290页
第2页 / 共290页
第3页 / 共290页
第4页 / 共290页
第5页 / 共290页
第6页 / 共290页
第7页 / 共290页
第8页 / 共290页
资料共290页,剩余部分请下载后查看
Introduction
Examples
1 Parallel Execution
1.1 A Simple Parallel Loop
1.2 The parallel Construct
1.3 Controlling the Number of Threads on Multiple Nesting Levels
1.4 Interaction Between the num_threads Clause and omp_set_dynamic
1.5 Fortran Restrictions on the do Construct
1.6 The nowait Clause
1.7 The collapse Clause
1.8 linear Clause in Loop Constructs
1.9 The parallel sections Construct
1.10 The firstprivate Clause and the sections Construct
1.11 The single Construct
1.12 The workshare Construct
1.13 The master Construct
1.14 Parallel Random Access Iterator Loop
1.15 The omp_set_dynamic and omp_set_num_threads Routines
1.16 The omp_get_num_threads Routine
2 OpenMP Affinity
2.1 The proc_bind Clause
2.1.1 Spread Affinity Policy
2.1.2 Close Affinity Policy
2.1.3 Master Affinity Policy
2.2 Affinity Query Functions
3 Tasking
3.1 The task and taskwait Constructs
3.2 Task Priority
3.3 Task Dependences
3.3.1 Flow Dependence
3.3.2 Anti-dependence
3.3.3 Output Dependence
3.3.4 Concurrent Execution with Dependences
3.3.5 Matrix multiplication
3.4 The taskgroup Construct
3.5 The taskyield Construct
3.6 The taskloop Construct
4 Devices
4.1 target Construct
4.1.1 target Construct on parallel Construct
4.1.2 target Construct with map Clause
4.1.3 map Clause with to/from map-types
4.1.4 map Clause with Array Sections
4.1.5 target Construct with if Clause
4.2 target data Construct
4.2.1 Simple target data Construct
4.2.2 target data Region Enclosing Multiple target Regions
4.2.3 target data Construct with Orphaned Call
4.2.4 target data Construct with if Clause
4.3 target enter data and target exit data Constructs
4.4 target update Construct
4.4.1 Simple target data and target update Constructs
4.4.2 target update Construct with if Clause
4.5 declare target Construct
4.5.1 declare target and end declare target for a Function
4.5.2 declare target Construct for Class Type
4.5.3 declare target and end declare target for Variables
4.5.4 declare target and end declare target with declare simd
4.5.5 declare target Directive with link Clause
4.6 teams Constructs
4.6.1 target and teams Constructs with omp_get_num_teams and omp_get_team_num Routines
4.6.2 target, teams, and distribute Constructs
4.6.3 target teams, and Distribute Parallel Loop Constructs
4.6.4 target teams and Distribute Parallel Loop Constructs with Scheduling Clauses
4.6.5 target teams and distribute simd Constructs
4.6.6 target teams and Distribute Parallel Loop SIMD Constructs
4.7 Asynchronous target Execution and Dependences
4.7.1 Asynchronous target with Tasks
4.7.2 nowait Clause on target Construct
4.7.3 Asynchronous target with nowait and depend Clauses
4.8 Array Sections in Device Constructs
4.9 Device Routines
4.9.1 omp_is_initial_device Routine
4.9.2 omp_get_num_devices Routine
4.9.3 omp_set_default_device and omp_get_default_device Routines
4.9.4 Target Memory and Device Pointers Routines
5 SIMD
5.1 simd and declare simd Constructs
5.2 inbranch and notinbranch Clauses
5.3 Loop-Carried Lexical Forward Dependence
6 Synchronization
6.1 The critical Construct
6.2 Worksharing Constructs Inside a critical Construct
6.3 Binding of barrier Regions
6.4 The atomic Construct
6.5 Restrictions on the atomic Construct
6.6 The flush Construct without a List
6.7 The ordered Clause and the ordered Construct
6.8 Doacross Loop Nest
6.9 Lock Routines
6.9.1 The omp_init_lock Routine
6.9.2 The omp_init_lock_with_hint Routine
6.9.3 Ownership of Locks
6.9.4 Simple Lock Routines
6.9.5 Nestable Lock Routines
7 Data Environment
7.1 The threadprivate Directive
7.2 The default(none) Clause
7.3 The private Clause
7.4 Fortran Private Loop Iteration Variables
7.5 Fortran Restrictions on shared and private Clauses with Common Blocks
7.6 Fortran Restrictions on Storage Association with the private Clause
7.7 C/C++ Arrays in a firstprivate Clause
7.8 The lastprivate Clause
7.9 The reduction Clause
7.10 The copyin Clause
7.11 The copyprivate Clause
7.12 C++ Reference in Data-Sharing Clauses
7.13 Fortran ASSOCIATE Construct
8 Memory Model
8.1 The OpenMP Memory Model
8.2 Race Conditions Caused by Implied Copies of Shared Variables in Fortran
9 Program Control
9.1 Conditional Compilation
9.2 Internal Control Variables (ICVs)
9.3 Placement of flush, barrier, taskwait and taskyield Directives
9.4 Cancellation Constructs
9.5 Nested Loop Constructs
9.6 Restrictions on Nesting of Regions
A Document Revision History
A.1 Changes from 4.0.2 to 4.5.0
A.2 Changes from 4.0.1 to 4.0.2
A.3 Changes from 4.0 to 4.0.1
A.4 Changes from 3.1 to 4.0
Application Programming OpenMP Interface Examples Version 4.5.0 – November 2016 Source codes for OpenMP 4.5.0 Examples can be downloaded from github. Copyright c 1997-2016 OpenMP Architecture Review Board. Permission to copy without fee all or part of this material is granted, provided the OpenMP Architecture Review Board copyright notice and the title of this document appear. Notice is given that copying is by permission of OpenMP Architecture Review Board.
This page intentionally left blank
Contents Introduction Examples 1. Parallel Execution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1.1. A Simple Parallel Loop . 1.2. The parallel Construct . . . . . . . . . . . . . . . . . . . . . . . 1.3. Controlling the Number of Threads on Multiple Nesting Levels . . . . . . . . . . . 1.4. Interaction Between the num_threads Clause and omp_set_dynamic . . . . . . . . . . 1.5. Fortran Restrictions on the do Construct . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1.6. The nowait Clause . . . . . 1.7. The collapse Clause . . . . . . . . . . . . . . . . . . . . 1.8. linear Clause in Loop Constructs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1.9. The parallel sections Construct 1.10. The firstprivate Clause and the sections Construct . . . . . . . . . . . . . . . . . . . . . . . . . 1.11. The single Construct . . . . . . . . . . . . . . . . . . . . . . . . . 1.12. The workshare Construct 1.13. The master Construct . . . . . . . . . . . . . . . . . . . . . . . . . 1.14. Parallel Random Access Iterator Loop . . . . . . . . . . . . . . . . . . . . . . . . 1.15. The omp_set_dynamic and . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . omp_set_num_threads Routines . . . . . . . . 1.16. The omp_get_num_threads Routine . . . . . . . 2. OpenMP Affinity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.1. The proc_bind Clause . . . . . . . . . . . . . . . . . . . . . . . 2.1.1. Spread Affinity Policy . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.1.2. Close Affinity Policy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.1.3. Master Affinity Policy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 2 3 5 6 8 11 13 15 18 22 24 25 27 29 33 35 36 38 40 42 42 44 47 i
2.2. Affinity Query Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3. Tasking . . . . . . . . . . . . . . . . . . . . 3.1. The task and taskwait Constructs . . . 3.2. Task Priority . . 3.3. Task Dependences . . . . . 3.3.1. Flow Dependence . 3.3.2. Anti-dependence . . 3.3.3. Output Dependence . 3.3.4. Concurrent Execution with Dependences 3.3.5. Matrix multiplication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3.4. The taskgroup Construct 3.5. The taskyield Construct 3.6. The taskloop Construct . . . . . . . . . . . . . . . . . . . . 48 51 52 71 73 73 74 75 76 78 80 83 85 4. Devices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4.1. target Construct 4.2. target data Construct . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4.1.1. target Construct on parallel Construct . . . . . . . . . . . . . . . 4.1.2. target Construct with map Clause 4.1.3. map Clause with to/from map-types . . . . . . . . . . . . . . . 4.1.4. map Clause with Array Sections . . . . . . . . . . . . . . . . . . . . . . . 4.1.5. target Construct with if Clause . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4.2.1. Simple target data Construct 4.2.2. target data Region Enclosing Multiple target Regions 4.2.3. target data Construct with Orphaned Call 4.2.4. target data Construct with if Clause . . . . . . 4.3. target enter data and target exit data Constructs 4.4. target update Construct 4.4.1. Simple target data and target update Constructs . . . . 4.4.2. target update Construct with if Clause . . 4.5. declare target Construct . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4.5.1. declare target and end declare target for a Function . . . 4.5.2. declare target Construct for Class Type . . . . . . . . . . . . . . . . . . . 87 88 88 89 90 91 93 96 96 97 101 104 108 111 111 113 115 . . . 115 117 . . . ii OpenMP Examples Version 4.5.0 - November 2016
4.5.3. declare target and end declare target for Variables . . . . 117 4.5.4. declare target and end declare target with declare simd . 120 4.5.5. declare target Directive with link Clause . . 123 126 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4.6. teams Constructs . . 4.6.1. target and teams Constructs with omp_get_num_teams . . . . . . Clauses . and omp_get_team_num Routines . . . . . . . . . . . . . . . . . 4.6.2. target, teams, and distribute Constructs . . . . . . . . . . . 4.6.3. target teams, and Distribute Parallel Loop Constructs . . . . . . . . . 4.6.4. target teams and Distribute Parallel Loop Constructs with Scheduling . . . . . . . . . . . . . . . . . . . . . . . . 4.6.5. target teams and distribute simd Constructs . . . . . 4.6.6. target teams and Distribute Parallel Loop SIMD Constructs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4.7.1. Asynchronous target with Tasks 4.7.2. nowait Clause on target Construct 4.7.3. Asynchronous target with nowait and depend Clauses . . . . 4.7. Asynchronous target Execution and Dependences . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4.8. Array Sections in Device Constructs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4.9. Device Routines . . . . . . . . . . . . . . . . . 4.9.1. omp_is_initial_device Routine . . . . 4.9.2. omp_get_num_devices Routine 4.9.3. omp_set_default_device and . . . . . . . . . . . . . . . . . . . . . . . 126 128 129 131 132 134 135 135 139 . 141 144 148 148 150 omp_get_default_device Routines . . . . . . . . . . . . . . 4.9.4. Target Memory and Device Pointers Routines . . . . . . . . . . . . . . . . . . . . 5. SIMD . . . . . . . . . . . . . . 5.1. simd and declare simd Constructs 5.2. inbranch and notinbranch Clauses . . . . . . . . . . . . . . . . 5.3. Loop-Carried Lexical Forward Dependence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6. Synchronization . 6.1. The critical Construct 6.2. Worksharing Constructs Inside a critical Construct . . . . 6.3. Binding of barrier Regions . . . . . . 6.4. The atomic Construct . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151 152 154 155 161 165 169 171 174 176 178 Contents iii
. . . 6.5. Restrictions on the atomic Construct . . 6.6. The flush Construct without a List . . . 6.7. The ordered Clause and the ordered Construct . . 6.8. Doacross Loop Nest . 6.9. Lock Routines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6.9.1. The omp_init_lock Routine . . . . . . . 6.9.2. The omp_init_lock_with_hint Routine . . . . . . . . 6.9.3. Ownership of Locks 6.9.4. Simple Lock Routines 6.9.5. Nestable Lock Routines . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184 187 190 194 200 200 201 202 203 206 7. Data Environment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7.1. The threadprivate Directive . . . . . . . . . . . . . . . 7.2. The default(none) Clause . . . . 7.3. The private Clause . . . . . . . . . . . . . . . . . . . . . . 7.4. Fortran Private Loop Iteration Variables . . . . . . . . . . . . . . . . . . . . . . . 7.5. Fortran Restrictions on shared and private Clauses with Common Blocks . . 7.6. Fortran Restrictions on Storage Association with the private Clause . . . . . . . . . . . . . . . . . 7.7. C/C++ Arrays in a firstprivate Clause . . . . . . . . . . . . . . . . . . . . . . . . . 7.8. The lastprivate Clause . . . . . . . . . . . . . . . . . . . . . . . . . 7.9. The reduction Clause . . . . . . . . . . . . . . . . . . . . . . . . . 7.10. The copyin Clause . . . 7.11. The copyprivate Clause . . . . . . . . . . . . . . . . . . . 7.12. C++ Reference in Data-Sharing Clauses . . . . . . . . . . . . . . . . . . . . . . . 7.13. Fortran ASSOCIATE Construct . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 209 211 217 219 223 225 . 227 230 232 233 240 242 246 247 8. Memory Model 8.1. The OpenMP Memory Model . . . . . . . . . . . . . . . . . . . . . . . . . . . 8.2. Race Conditions Caused by Implied Copies of Shared Variables in Fortran . . . . . . 249 250 256 9. Program Control . . Internal Control Variables (ICVs) 9.1. Conditional Compilation . 9.2. 9.3. Placement of flush, barrier, taskwait and taskyield Directives . 9.4. Cancellation Constructs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 257 259 260 . . . 263 267 iv OpenMP Examples Version 4.5.0 - November 2016
9.5. Nested Loop Constructs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9.6. Restrictions on Nesting of Regions . . . . . . . . . . . . . . . . . . . . . . . . . . A. Document Revision History A.1. Changes from 4.0.2 to 4.5.0 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . A.2. Changes from 4.0.1 to 4.0.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . A.3. Changes from 4.0 to 4.0.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . A.4. Changes from 3.1 to 4.0 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 272 275 281 281 282 282 282 Contents v
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 Introduction This collection of programming examples supplements the OpenMP API for Shared Memory Parallelization specifications, and is not part of the formal specifications. It assumes familiarity with the OpenMP specifications, and shares the typographical conventions used in that document. Note – This first release of the OpenMP Examples reflects the OpenMP Version 4.5 specifications. Additional examples are being developed and will be published in future releases of this document. The OpenMP API specification provides a model for parallel programming that is portable across shared memory architectures from different vendors. Compilers from numerous vendors support the OpenMP API. The directives, library routines, and environment variables demonstrated in this document allow users to create and manage parallel programs while permitting portability. The directives extend the C, C++ and Fortran base languages with single program multiple data (SPMD) constructs, tasking constructs, device constructs, worksharing constructs, and synchronization constructs, and they provide support for sharing and privatizing data. The functionality to control the runtime environment is provided by library routines and environment variables. Compilers that support the OpenMP API often include a command line option to the compiler that activates and allows interpretation of all OpenMP directives. The latest source codes for OpenMP Examples can be downloaded from the sources directory at https://github.com/OpenMP/Examples. The codes for this OpenMP 4.5.0 Examples document have the tag v4.5.0. Complete information about the OpenMP API and a list of the compilers that support the OpenMP API can be found at the OpenMP.org web site http://www.openmp.org 1
分享到:
收藏