OpenMP OpenMP Application Programming Interface Examples Version 4.0.2-March 2015 Source codes for OpenMP 4.0.2 Examples can be downloaded from github. Copyright 1997-2015 OpenMP Architecture Review Board. Permission to copy without fee all or part of this material is granted,provided the OpenMP
OpenMP Application Programming Interface Examples Version 4.0.2 – March 2015 Source codes for OpenMP 4.0.2 Examples can be downloaded from github. Copyright c 1997-2015 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
Contents 1 A Simple Parallel Loop 2 The OpenMP Memory Model 3 Conditional Compilation 10 4 Internal Control Variables(ICVs) 11 5 The parallel Construct 6 Controlling the Number of Threads on Multiple Nesting Levels 公 7 Interaction Between the num_threads Clause and omp_set_dynamic 20 8 The proc_bind Clause 22 8.1 Spread Affinity Policy. ·。”。·”·,··4·4”·。。·。···◆ 22 8.2 Close Affinity Policy 25 83 Master Affinity Policy.....................,...,...... 0 9 Fortran Restrictions on the do Construct 29 10 Fortran Private Loop Iteration Variables 吗 11 The nowait Clause 33 12 The collapse Clause 37 13 The parallel sections Construct 41 14 The firstprivate Clause and the sections Construct 43 15 The single Construct
Contents 1 A Simple Parallel Loop 3 2 The OpenMP Memory Model 4 3 Conditional Compilation 10 4 Internal Control Variables (ICVs) 11 5 The parallel Construct 14 6 Controlling the Number of Threads on Multiple Nesting Levels 17 7 Interaction Between the num_threads Clause and omp_set_dynamic 20 8 The proc_bind Clause 22 8.1 Spread Affinity Policy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 8.2 Close Affinity Policy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 8.3 Master Affinity Policy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 9 Fortran Restrictions on the do Construct 29 10 Fortran Private Loop Iteration Variables 31 11 The nowait Clause 33 12 The collapse Clause 37 13 The parallel sections Construct 41 14 The firstprivate Clause and the sections Construct 43 15 The single Construct 45 i
16 The task and taskwait Constructs 47 17 Task Dependences 66 17.1 Flow Dependence 66 17.2 Anti-dependence 6 17.3 Output Dependence... 68 l7.4 Concurrent Execution with Dependences.······················ 70 17.5 Matrix multiplication 71 18 The taskgroup Construct 73 19 The taskyield Construct 76 20 The workshare Construct 78 21 The master Construct 82 22 The critical Construct 84 23 Worksharing Constructs Inside a critical Construct 86 24 Binding of barrier Regions 88 25 The atomic Construct 91 26 Restrictions on the atomic Construct % 27 The flush Construct without a List 102 28 Placement of flush,barrier,taskwait and taskyield Directives 106 29 The ordered Clause and the ordered Construct 110 30 Cancellation Constructs 114 31 The threadprivate Directive 119 32 Parallel Random Access Iterator Loop 125 33 Fortran Restrictions on shared and private Clauses with Common Blocks 126 ii OpenMP Examples Version 4.0.2-March 2015
16 The task and taskwait Constructs 47 17 Task Dependences 66 17.1 Flow Dependence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66 17.2 Anti-dependence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67 17.3 Output Dependence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68 17.4 Concurrent Execution with Dependences . . . . . . . . . . . . . . . . . . . . . . . 70 17.5 Matrix multiplication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71 18 The taskgroup Construct 73 19 The taskyield Construct 76 20 The workshare Construct 78 21 The master Construct 82 22 The critical Construct 84 23 Worksharing Constructs Inside a critical Construct 86 24 Binding of barrier Regions 88 25 The atomic Construct 91 26 Restrictions on the atomic Construct 98 27 The flush Construct without a List 102 28 Placement of flush, barrier, taskwait and taskyield Directives 106 29 The ordered Clause and the ordered Construct 110 30 Cancellation Constructs 114 31 The threadprivate Directive 119 32 Parallel Random Access Iterator Loop 125 33 Fortran Restrictions on shared and private Clauses with Common Blocks 126 ii OpenMP Examples Version 4.0.2 - March 2015
34 The default(none)Clause 129 35 Race Conditions Caused by Implied Copies of Shared Variables in Fortran 131 36 The private Clause 133 37 Fortran Restrictions on Storage Association with the private Clause 137 38 C/C++Arrays in a firstprivate Clause 140 39 The lastprivate Clause 142 40 The reduction Clause 144 41 The copyin Clause 150 42 The copyprivate Clause 152 43 Nested Loop Constructs 157 44 Restrictions on Nesting of Regions 160 45 The omp_set_dynamic and omp_set_num_threads Routines 167 46 The omp_get_num_threads Routine 169 47 The omp_init_lock Routine 172 48 Ownership of Locks 174 49 Simple Lock Routines 176 50 Nestable Lock Routines 179 51 SIMD Constructs 182 52 target Construct 193 52.1 target Construct on parallel Construct 193 52.2 target Construct with map Clause .....194 Contents
34 The default(none) Clause 129 35 Race Conditions Caused by Implied Copies of Shared Variables in Fortran 131 36 The private Clause 133 37 Fortran Restrictions on Storage Association with the private Clause 137 38 C/C++ Arrays in a firstprivate Clause 140 39 The lastprivate Clause 142 40 The reduction Clause 144 41 The copyin Clause 150 42 The copyprivate Clause 152 43 Nested Loop Constructs 157 44 Restrictions on Nesting of Regions 160 45 The omp_set_dynamic and omp_set_num_threads Routines 167 46 The omp_get_num_threads Routine 169 47 The omp_init_lock Routine 172 48 Ownership of Locks 174 49 Simple Lock Routines 176 50 Nestable Lock Routines 179 51 SIMD Constructs 182 52 target Construct 193 52.1 target Construct on parallel Construct . . . . . . . . . . . . . . . . . . . . 193 52.2 target Construct with map Clause . . . . . . . . . . . . . . . . . . . . . . . . . 194 Contents iii
52.3 map Clause with to/from map-types........................195 52.4 map Clause with Array Sections .......................... 197 52.5 target Construct with if Clause 198 53 target data Construct 200 53.1 Simple target data Construct 200 53.2 target data Region Enclosing Multiple target Regions.·········· 201 53.3 target data Construct with Orphaned Call...........·.····... 204 53.4 target data Construct with if Clause 208 54 target update Construct 212 54.1 Simple target data and target update Constructs 212 54.2 target update Construct with if Clause 214 55 declare target Construct 216 55.1 declare target and end declare target for a Function 216 55.2 declare target Construct for Class Type 218 55.3 declare target and end declare target for Variables·········: 219 55.4 declare target and end declare target with declare simd 222 56 teams Constructs 224 56.1 target and teams Constructs with omp_get_num_teams and omp_get_team_num Routines·········.·····,。,.,,·.· 224 56.2 target,teams,and distribute Constructs·,··. 226 56.3 target teams,and Distribute Parallel Loop Constructs·········· 227 56.4 target teams and Distribute Parallel Loop Constructs with Scheduling Clauses 229 56.5 target teams and distribute simd Constructs............... 230 56.6 target teams and Distribute Parallel Loop SIMD Constructs.......... 232 57 Asynchronous Execution of a target Region Using Tasks 233 58 Array Sections in Device Constructs 238 59 Device Routines 243 59.I omp_is_initialdevice Routine....................... 243 59.2omp_get_num_devices Routine..··········· 245 iv OpenMP Examples Version 4.0.2-March 2015
52.3 map Clause with to/from map-types . . . . . . . . . . . . . . . . . . . . . . . . 195 52.4 map Clause with Array Sections . . . . . . . . . . . . . . . . . . . . . . . . . . . 197 52.5 target Construct with if Clause . . . . . . . . . . . . . . . . . . . . . . . . . 198 53 target data Construct 200 53.1 Simple target data Construct . . . . . . . . . . . . . . . . . . . . . . . . . . 200 53.2 target data Region Enclosing Multiple target Regions . . . . . . . . . . . . 201 53.3 target data Construct with Orphaned Call . . . . . . . . . . . . . . . . . . . . 204 53.4 target data Construct with if Clause . . . . . . . . . . . . . . . . . . . . . . 208 54 target update Construct 212 54.1 Simple target data and target update Constructs . . . . . . . . . . . . . 212 54.2 target update Construct with if Clause . . . . . . . . . . . . . . . . . . . . 214 55 declare target Construct 216 55.1 declare target and end declare target for a Function . . . . . . . . . . 216 55.2 declare target Construct for Class Type . . . . . . . . . . . . . . . . . . . . 218 55.3 declare target and end declare target for Variables . . . . . . . . . . 219 55.4 declare target and end declare target with declare simd . . . . . 222 56 teams Constructs 224 56.1 target and teams Constructs with omp_get_num_teams and omp_get_team_num Routines . . . . . . . . . . . . . . . . . . . . . . . . 224 56.2 target, teams, and distribute Constructs . . . . . . . . . . . . . . . . . . 226 56.3 target teams, and Distribute Parallel Loop Constructs . . . . . . . . . . . . . 227 56.4 target teams and Distribute Parallel Loop Constructs with Scheduling Clauses 229 56.5 target teams and distribute simd Constructs . . . . . . . . . . . . . . . 230 56.6 target teams and Distribute Parallel Loop SIMD Constructs . . . . . . . . . . 232 57 Asynchronous Execution of a target Region Using Tasks 233 58 Array Sections in Device Constructs 238 59 Device Routines 243 59.1 omp_is_initial_device Routine . . . . . . . . . . . . . . . . . . . . . . . 243 59.2 omp_get_num_devices Routine . . . . . . . . . . . . . . . . . . . . . . . . . 245 iv OpenMP Examples Version 4.0.2 - March 2015