1. Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
引言
1.1 Scope . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.1 范围
1.2 Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.2 词汇
1.2.1 Threading Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.2.1 线程概念
1.2.2 OpenMP Language Terminology . . . . . . . . . . . . . . . . . . . . . 2
1.2.2 OpenMP语言术语
1.2.3 Tasking Terminology . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
1.2.3 任务术语
1.2.4 Data Terminology . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
1.2.4 数据术语
1.2.5 Implementation Terminology . . . . . . . . . . . . . . . . . . . . . . . . 10
1.2.5 实现术语
1.3 Execution Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
1.3 执行模型
1.4 Memory Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
1.4 内存模型
1.4.1 Structure of the OpenMP Memory Model . . . . . . . . . . . . . . . 13
1.4.1 OpenMP内存模型的结构
1.4.2 The Flush Operation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
1.4.2 刷出操作
1.4.3 OpenMP Memory Consistency . . . . . . . . . . . . . . . . . . . . . . 16
1.4.3 OpenMP内存一致性
1.5 OpenMP Compliance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
1.5 OpenMP的柔韧性
1.6 Normative References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
1.6 被提名的引用
1.7 Organization of this document . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
1.7 这份文档的组织
2. Directives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
2.命令
2.1 Directive Format . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
2.1 命令格式
2.1.1 Fixed Source Form Directives . . . . . . . . . . . . . . . . . . . . . . . 23
2.1.1 固定的源形式定义
2.1.2 Free Source Form Directives . . . . . . . . . . . . . . . . . . . . . . . . 24
2.1.2 自由的源形式定义
2.2 Conditional Compilation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
2.2 条件编译
2.2.1 Fixed Source Form Conditional Compilation Sentinels . . . . 26
2.2.1 固定源形式条件定义的哨兵
2.2.2 Free Source Form Conditional Compilation Sentinel . . . . . . 27
2.2.2 自由源形式条件定义的哨兵
2.3 Internal Control Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
2.3 内部控制变量
2.3.1 ICV Descriptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
2.3.1 ICV描述
2.3.2 Modifying and Retrieving ICV Values . . . . . . . . . . . . . . . . . . 29
2.3.2 改变和取得ICV数值
2.3.3 How the Per-Data Environment ICVs Work . . . . . . . . . . . . . 30
2.3.3 每个数据环境ICV如何起作用
2.3.4 ICV Override Relationships . . . . . . . . . . . . . . . . . . . . . . . . . 31
2.3.4 ICV重载关系
2.4 parallel Construct . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
2.4 parallel结构
2.4.1 Determining the Number of Threads for a parallel Region 36
2.4.1 决定并行区域的线程数量
2.5 Worksharing Constructs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
2.5 worksharing结构
2.5.1 Loop Construct . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
2.5.1 loop结构
2.5.2 sections Construct . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
2.5.2 section结构
2.5.3 single Construct . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
2.5.3 single 结构
2.5.4 workshare Construct . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
2.5.4 workshare结构
2.6 Combined Parallel Worksharing Constructs . . . . . . . . . . . . . . . . . . . 55
2.6 混合parallel worksharing结构
2.6.1 Parallel Loop Construct . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
2.6.1 parallel loop construct结构
2.6.2 parallel sections Construct . . . . . . . . . . . . . . . . . . . . . 57
2.6.2 parallel section结构
2.6.3 parallel workshare Construct . . . . . . . . . . . . . . . . . . . . 59
2.6.3 parallel workshare 结构
2.7 Tasking Constructs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
2.7 Tasking结构.
2.7.1 task Construct . . . . . . . . . . . . . .. . . . . . . . . . . . . . . . . . . . . 61
2.7.1 task结构
2.7.2 taskyield Construct . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
2.7.2 taskyield 结构
2.7.3 Task Scheduling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
2.7.3 Task调度
2.8 Master and Synchronization Constructs . . . . . . . . . . . . . . . . . . . . . . 67
2.8 Master和Synchronization结构
2.8.1 master Construct . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
2.8.1 master结构
2.8.2 critical Construct . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68
2.8.2 critical结构
2.8.3 barrier Construct . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
2.8.3 barrier结构
2.8.4 taskwait Construct . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72
2.8.4 taskwait结构
2.8.5 atomic Construct . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
2.8.5 atomic结构
2.8.6 flush Construct . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78
2.8.6 flush结构
2.8.7 ordered Construct . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82
2.8.7 ordered结构
2.9 Data Environment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84
2.9 数据环境
2.9.1 Data-sharing Attribute Rules . . . . . . . . . . . . . . . . . . . . . . . . 84
2.9.1 数据分享属性规则
2.9.2 threadprivate Directive . . . . . . . . . . . . . . . . . . . . . . . . . 88
2.9.2 threadprivate 命令
2.9.3 Data-Sharing Attribute Clauses . . . . . . . . . . . . . . . . . . . . . . 92 iii
2.9.3 数据分享属性子句
2.9.4 Data Copying Clauses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107
2.9.4 数据拷贝子句
2.10 Nesting of Regions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111
2.10 区域的嵌套
3. Runtime Library Routines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113
3. 运行时库函数
3.1 Runtime Library Definitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114
3.1 运行时库定义
3.2 Execution Environment Routines . . . . . . . . . . . . . . . . . . . . . . . . . . . 115
3.2 执行环境函数
3.2.1 omp_set_num_threads . . . . . . . . . . . . . . . . . . . . . . . . . . 116
3.2.2 omp_get_num_threads . . . . . . . . . . . . . . . . . . . . . . . . . . 117
3.2.3 omp_get_max_threads . . . . . . . . . . . . . . . . . . . . . . . . . . 118
3.2.4 omp_get_thread_num . . . . . . . . . . . . . . . . . . . . . . . . . . . 119
3.2.5 omp_get_num_procs . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121
3.2.6 omp_in_parallel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122
3.2.7 omp_set_dynamic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123
3.2.8 omp_get_dynamic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124
3.2.9 omp_set_nested . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125
3.2.10 omp_get_nested . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126
3.2.11 omp_set_schedule . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128
3.2.12 omp_get_schedule . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130
3.2.13 omp_get_thread_limit . . . . . . . . . . . . . . . . . . . . . . . . . 131
3.2.14 omp_set_max_active_levels . . . . . . . . . . . . . . . . . . . . 132
3.2.15 omp_get_max_active_levels . . . . . . . . . . . . . . . . . . . . 134
3.2.16 omp_get_level . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135
3.2.17 omp_get_ancestor_thread_num . . . . . . . . . . . . . . . . . . 136
3.2.18 omp_get_team_size . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137
3.2.19 omp_get_active_level . . . . . . . . . . . . . . . . . . . . . . . . . 139
3.2.20 omp_in_final . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140
3.3 Lock Routines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141
3.3 锁函数
3.3.1 omp_init_lock and omp_init_nest_lock . . . . . . . . . 143
3.3.2 omp_destroy_lock and omp_destroy_nest_lock . . . 144
3.3.3 omp_set_lock and omp_set_nest_lock . . . . . . . . . . . .
3.3.4 omp_unset_lock and omp_unset_nest_lock . . . . . . . . 146
3.3.5 omp_test_lock and omp_test_nest_lock . . . . . . . . . . 147
3.4 Timing Routines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148
3.4.1 omp_get_wtime . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148
3.4.2 omp_get_wtick . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150
4. Environment Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153
4. 环境变量
4.1 OMP_SCHEDULE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154
4.2 OMP_NUM_THREADS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155
4.3 OMP_DYNAMIC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156
4.4 OMP_PROC_BIND . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156
4.5 OMP_NESTED . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157
4.6 OMP_STACKSIZE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157
4.7 OMP_WAIT_POLICY . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158
4.8 OMP_MAX_ACTIVE_LEVELS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159
4.9 OMP_THREAD_LIMIT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160
A. Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161
A. 例子
A.1 A Simple Parallel Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161
A.2 The OpenMP Memory Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162
A.3 Conditional Compilation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169
A.4 Internal Control Variables (ICVs) . . . . . . . . . . . . . . . . . . . . . . . . . . . 170
A.5 The parallel Construct . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172
A.6 Controlling the Number of Threads on Multiple Nesting Levels . . . . 175
A.7 Interaction Between the num_threads Clause and omp_set_dynamic 177
A.8 Fortran Restrictions on the do Construct . . . . . . . . . . . . . . . . . . . . . 179
A.9 Fortran Private Loop Iteration Variables . . . . . . . . . . . . . . . . . . . . . . 181
A.10 The nowait clause . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182
A.11 The collapse clause . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185
A.12 The parallel sections Construct . . . . . . . . . . . . . . . . . . . . . . . . 189 v
A.13 The firstprivate Clause and the sections Construct . . . . . . 190
A.14 The single Construct . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192
A.15 Tasking Constructs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193
A.16 The taskyield Directive . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212
A.17 The workshare Construct . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213
A.18 The master Construct . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217
A.19 The critical Construct . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219
A.20 worksharing Constructs Inside a critical Construct . . . . . . . . . . 221
A.21 Binding of barrier Regions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222
A.22 The atomic Construct . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 224
A.23 Restrictions on the atomic Construct . . . . . . . . . . . . . . . . . . . . . . . 230
A.24 The flush Construct without a List . . . . . . . . . . . . . . . . . . . . . . . . . 233
A.25 Placement of flush, barrier, taskwait and taskyield Directives 236
A.26 The ordered Clause and the ordered Construct . . . . . . . . . . . . . 239
A.27 The threadprivate Directive . . . . . . . . . . . . . . . . . . . . . . . . . . . . 244
A.28 Parallel Random Access Iterator Loop . . . . . . . . . . . . . . . . . . . . . . . 250
A.29 Fortran Restrictions on shared and private Clauses with Common
Blocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251
A.30 The default(none) Clause . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 253
A.31 Race Conditions Caused by Implied Copies of Shared Variables in
Fortran . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 255
A.32 The private Clause . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 256
A.33 Fortran Restrictions on Storage Association with the private Clause 260
A.34 C/C++ Arrays in a firstprivate Clause . . . . . . . . . . . . . . . . . . . 263
A.35 The lastprivate Clause . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 264
A.36 The reduction Clause . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 266
A.37 The copyin Clause . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 271
A.38 The copyprivate Clause . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 273
A.39 Nested Loop Constructs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 278
A.40 Restrictions on Nesting of Regions . . . . . . . . . . . . . . . . . . . . . . . . . . 281
A.41 The omp_set_dynamic and omp_set_num_threads Routines . . 288
A.42 The omp_get_num_threads Routine . . . . . . . . . . . . . . . . . . . . . . 289
A.43 The omp_init_lock Routine . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 292
A.44 Ownership of Locks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 293
A.45 Simple Lock Routines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 294
A.46 Nestable Lock Routines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 297
B. Stubs for Runtime Library Routines . . . . . . . . . . . . . . . . . . . . . . . . . . . . 301
B.运行时库函数的桩
B.1 C/C++ Stub Routines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 302
B.1 C/C++ 桩函数
B.2 Fortran Stub Routines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 309
B.2 fortran桩函数
C. OpenMP C and C++ Grammar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 315
C. OpenMP C和C++语法
C.1 Notation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 315
C.1 注释
C.2 Rules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 316
C.2 规则
D. Interface Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 325
D. 接口解释
D.1 Example of the omp.h Header File . . . . . . . . . . . . . . . . . . . . . . . . . 326
D.1 omp.h头文件的例子
D.2 Example of an Interface Declaration include File . . . . . . . . . . . . . 328
D.2 包括了文件的接口定义的例子
D.3 Example of a Fortran Interface Declaration module . . . . . . . . . . . . 330
D.3 fortran接口定义模块的例子
D.4 Example of a Generic Interface for a Library Routine . . . . . . . . . . . . 334
D.4 库函数的通用接口的例子
E. OpenMP Implementation-Defined Behaviors . . . . . . . . . . . . . . . . . . . . . 335
E. OpenMP 实现的行为
F. Features History . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 339
F. 特性的历史
F.1 Version 3.0 to 3.1 Differences . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 339
F.1 3.0与3.1的区别
F.2 Version 2.5 to 3.0 Differences . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 340
F.2 2.5和3.0的区别
