50
国国国国国国国国国 国国国 () 国国国国 国国国国国 国国

第十三章 共享存储系统编程

Embed Size (px)

DESCRIPTION

第十三章 共享存储系统编程. 共享存储系统编程. 13.1 ANSI X3H5 共享存储模型 13.2 POSIX 线程模型 13.3 OpenMP 模型. 编程标准的作用. 规定程序的执行模型 SPMD, SMP 等 如何表达并行性 DOACROSS, FORALL, PARALLEL,INDEPENDENT 如何表达同步 Lock, Barrier, Semaphore, Condition Variables 如何获得运行时的环境变量 threadid, num of processes. ANSI X3H5 共享存储器模型. - PowerPoint PPT Presentation

Citation preview

Page 1: 第十三章 共享存储系统编程

国家高性能计算中心(合肥)

第十三章 共享存储系统编程

Page 2: 第十三章 共享存储系统编程

国家高性能计算中心(合肥)

共享存储系统编程 13.1 ANSI X3H513.1 ANSI X3H5 共享存储模型共享存储模型 13.2 POSIX 13.2 POSIX 线程模型线程模型 13.3 OpenMP13.3 OpenMP 模型模型

Page 3: 第十三章 共享存储系统编程

国家高性能计算中心(合肥)

编程标准的作用

规定程序的执行模型规定程序的执行模型 SPMD, SMP SPMD, SMP 等等

如何表达并行性如何表达并行性 DOACROSS, FORALL, PARALLEL,INDEPENDENTDOACROSS, FORALL, PARALLEL,INDEPENDENT

如何表达同步如何表达同步 Lock, Barrier, Semaphore, Condition VariablesLock, Barrier, Semaphore, Condition Variables

如何获得运行时的环境变量如何获得运行时的环境变量 threadid, num of processesthreadid, num of processes

Page 4: 第十三章 共享存储系统编程

国家高性能计算中心(合肥)

ANSI X3H5 共享存储器模型 Started in the mid-80’s with the emergence of shared memory parallel Started in the mid-80’s with the emergence of shared memory parallel

computers with proprietary directive driven programming environmentscomputers with proprietary directive driven programming environments

更早的标准化结果更早的标准化结果—— PCFPCF 共享存储器并行共享存储器并行 FortranFortran 19931993 年制定的概念性编程模型年制定的概念性编程模型 Language BindingLanguage Binding

CC Fortran 77Fortran 77 Fortran 90Fortran 90

Page 5: 第十三章 共享存储系统编程

国家高性能计算中心(合肥)

并行块(工作共享构造)并行块(工作共享构造) 并行块并行块 (psections ... end psections)(psections ... end psections) 并行循环并行循环 (pdo ... Endo pdo)(pdo ... Endo pdo) 单进程单进程 (psingle ... End psingle)(psingle ... End psingle) 可嵌套可嵌套

非共享块重复执行非共享块重复执行 隐式路障隐式路障 (nowait)(nowait) ,显式路障和阻挡操作,显式路障和阻挡操作 共享共享 // 私有变量私有变量 线程同步线程同步

门插销门插销 (latch)(latch) :临界区:临界区 锁:锁: test,lock,unlocktest,lock,unlock 事件事件 :wait,post,clear:wait,post,clear 序数序数 (ordinal):(ordinal): 顺序顺序

Page 6: 第十三章 共享存储系统编程

国家高性能计算中心(合肥)

X3H5: 并行性构造Program mainProgram main !! 程序以顺序模式开始程序以顺序模式开始 ,, 此时只有一个此时只有一个

AA !A!A 只由基本线程执行,称为主线程只由基本线程执行,称为主线程parallelparallel !! 转换为并行模式,派生出多个子线程(一个组)转换为并行模式,派生出多个子线程(一个组)

BB !B!B 为每个组员所复制为每个组员所复制psectionspsections !! 并行块开始并行块开始sectionsection

CC !! 一个组员执行一个组员执行 CC

sectionsection

DD !! 一个组员执行一个组员执行 DD

end psectionsend psections !! 等待等待 CC 和和 DD 都结束都结束psinglepsingle !! 暂时转换成顺序模式暂时转换成顺序模式EE !! 已由一个组员执行已由一个组员执行end psingleend psingle !! 转回并行模式转回并行模式pdo i=1,6pdo i=1,6 !pdo!pdo 构造开始构造开始

F(i)F(i) !! 组员共享组员共享 FF 的六次迭代的六次迭代end pdo no waitend pdo no wait !! 无隐式路障同步无隐式路障同步GG !! 更多的复制代码更多的复制代码

end parallelend parallel !! 转为顺序模式转为顺序模式HH !! 初始化进程单独执行初始化进程单独执行 HH

...... !! 可能有更多的并行构造可能有更多的并行构造EndEnd

Page 7: 第十三章 共享存储系统编程

国家高性能计算中心(合肥)

线程

隐式路障同步

P Q RA

B

C

E

F(1:2)

G

H

G G

F(3:4) F(5:6)

D

B B

隐式路障同步

隐式路障同步

无隐式路障同步

隐式路障同步

Page 8: 第十三章 共享存储系统编程

国家高性能计算中心(合肥)

共享存储系统编程 13.1 ANSI X3H513.1 ANSI X3H5 共享存储模型共享存储模型 13.2 POSIX 13.2 POSIX 线程模型线程模型 13.3 OpenMP13.3 OpenMP 模型模型

Page 9: 第十三章 共享存储系统编程

国家高性能计算中心(合肥)

POSIX 线程模型 IEEE/ANSIIEEE/ANSI 标准标准—— IEEE POSIX 1003.1c-1995IEEE POSIX 1003.1c-1995 线程标准线程标准

—— Unix/NTUnix/NT 操作系统层上的,操作系统层上的, SMPSMP Chorus, Topaz, Mach CthreadsChorus, Topaz, Mach Cthreads Win32 ThreadWin32 Thread

GetThreadHandle,SetThreadPriority,SuspendThread,ResumeThreadGetThreadHandle,SetThreadPriority,SuspendThread,ResumeThread TLS(TLS( 线程局部存储线程局部存储 )—TlsAlloc, TlsSetValue)—TlsAlloc, TlsSetValue

LinuxThreads:__clone and sys_cloneLinuxThreads:__clone and sys_clone 用户线程和内核线程用户线程和内核线程 ((LWP)(LWP)( 一到一,一到多,多到多一到一,一到多,多到多 ))

Page 10: 第十三章 共享存储系统编程

国家高性能计算中心(合肥)

What Are Threads?

General-purpose solution for managing concurrency.General-purpose solution for managing concurrency. Multiple independent execution streams.Multiple independent execution streams. Shared state.Shared state. Preemptive scheduling.Preemptive scheduling. Synchronization (e.g. locks, conditions).Synchronization (e.g. locks, conditions).

Shared state(memory, files, etc.)

Threads

Page 11: 第十三章 共享存储系统编程

国家高性能计算中心(合肥)

线程共享相同的内存空间。线程共享相同的内存空间。 与标准 与标准 fork() fork() 相比,线程带来的开销很小。内核无需单独复制进程的内相比,线程带来的开销很小。内核无需单独复制进程的内

存空间或文件描述符等等。这就节省了大量的 存空间或文件描述符等等。这就节省了大量的 CPU CPU 时间。时间。 和进程一样,线程将利用多 和进程一样,线程将利用多 CPUCPU 。如果软件是针对多处理器系统设计的,。如果软件是针对多处理器系统设计的,

计算密集型应用。计算密集型应用。 支持内存共享无需使用繁琐的 支持内存共享无需使用繁琐的 IPC IPC 和其它复杂的通信机制。和其它复杂的通信机制。 Linux __cloneLinux __clone 不可移植,不可移植, PthreadPthread 可移植。可移植。 POSIX POSIX 线程标准不记录任何“家族”信息。无父无子。如果要等待一个线程标准不记录任何“家族”信息。无父无子。如果要等待一个

线程终止,就必须将线程的 线程终止,就必须将线程的 tid tid 传递给 传递给 pthread_join()pthread_join() 。线程库无法。线程库无法为您断定 为您断定 tidtid 。 。

Page 12: 第十三章 共享存储系统编程

国家高性能计算中心(合肥)

POSIX Threads: Basics and Examples by Uday Kamathhttp://www.coe.uncc.edu/~abw/parallel/pthreads/pthreads.htmlPOSIX 线程详解 : 一种支持内存共享的简单和快捷的工具by Daniel Robbinshttp://www.cn.ibm.com/developerWorks/linux/thread/posix_thread1/index.shtml

Page 13: 第十三章 共享存储系统编程

国家高性能计算中心(合肥)

Page 14: 第十三章 共享存储系统编程

国家高性能计算中心(合肥)

线程调用—线程管理POSIXPOSIX Solaris 2Solaris 2

pthread_createpthread_create thr_createthr_create

pthread_exitpthread_exit thr_exitthr_exit

pthread_killpthread_kill thr_killthr_kill

pthread_joinpthread_join thr_jointhr_join

pthread_selfpthread_self thr_selfthr_self

Page 15: 第十三章 共享存储系统编程

国家高性能计算中心(合肥)

线程调用—线程同步和互斥POSIXPOSIX Solaris 2Solaris 2

pthread_mutex_initpthread_mutex_init mutex_initmutex_init

pthread_ mutex_destroy pthread_ mutex_destroy mutex_destroymutex_destroy

pthread_ mutex_lock pthread_ mutex_lock mutex_lockmutex_lock

pthread_ mutex_trylock pthread_ mutex_trylock mutex_trylockmutex_trylock

pthread_ mutex_unlock pthread_ mutex_unlock mutex_unlockmutex_unlock

pthread_cond_initpthread_cond_init

pthread_cond_destroypthread_cond_destroy

pthread_cond_waitpthread_cond_wait

pthread_cond_timedwaitpthread_cond_timedwait

pthread_cond_signalpthread_cond_signal

pthread_cond_broadcastpthread_cond_broadcast

Page 16: 第十三章 共享存储系统编程

国家高性能计算中心(合肥)

Pthreads实现计算的实例 1

Page 17: 第十三章 共享存储系统编程

国家高性能计算中心(合肥)

Pthreads实现计算的实例 2

Page 18: 第十三章 共享存储系统编程

国家高性能计算中心(合肥)

对生产者驱动的有界缓冲区问题的 Pthread 条件变量解void *producer(void *arg1){void *producer(void *arg1){

int i;int i;

for (i=1;i<=SUMSIZE;i++){for (i=1;i<=SUMSIZE;i++){

pthread_mutex_lock(&slot_lock); pthread_mutex_lock(&slot_lock);

while(nslots<=0)while(nslots<=0)

pthread_cond_wait(&slots,&slot_lock); pthread_cond_wait(&slots,&slot_lock);

nslots--;nslots--;

pthread_mutex_unlock(&slot_lock);pthread_mutex_unlock(&slot_lock);

put_item(i*i);put_item(i*i);

pthread_mutex_lock(&item_lock);pthread_mutex_lock(&item_lock);

nitems++;nitems++;

pthread_cond_signal(&items);pthread_cond_signal(&items);

pthread_mutex_unlock(&item_lock);pthread_mutex_unlock(&item_lock);

}}

pthread_mutex_lock(&item_lock);pthread_mutex_lock(&item_lock);

producer_done=1;producer_done=1;

pthread_cond_broadcast(&items); pthread_cond_broadcast(&items); pthread_mutex_unlock(&item_lock);pthread_mutex_unlock(&item_lock);

return NULL;}return NULL;}

void *consumer(void *arg2){void *consumer(void *arg2){

int i,myitem;int i,myitem;

for (;;){for (;;){

pthread_mutex_lock(&item_lock); pthread_mutex_lock(&item_lock);

while ((nitems<=0)&&!producer_done)while ((nitems<=0)&&!producer_done)

pthread_cond_wait(&items,&item_lock);pthread_cond_wait(&items,&item_lock);

if ((nitems<=0)&&producer_done){if ((nitems<=0)&&producer_done){

ptherad_mutex_unlock(&item_lock);ptherad_mutex_unlock(&item_lock);

break;break;

}}

nitems--;nitems--;

pthread_mutex_unlock(&item_lock);pthread_mutex_unlock(&item_lock);

get_item(&myitem);get_item(&myitem);

sum+=myitem;sum+=myitem;

pthread_mutex_lock(&slot_lockpthread_mutex_lock(&slot_lock

nslots++;nslots++;

cond_signal(&slots);cond_signal(&slots);

pthread_mutex_unlock(&slot_lock); pthread_mutex_unlock(&slot_lock);

}}

return NULL; }return NULL; }

Page 19: 第十三章 共享存储系统编程

国家高性能计算中心(合肥)

共享存储系统编程 13.1 ANSI X3H513.1 ANSI X3H5 共享存储模型共享存储模型 13.2 POSIX 13.2 POSIX 线程模型线程模型 13.3 OpenMP13.3 OpenMP 模型模型

Page 20: 第十三章 共享存储系统编程

国家高性能计算中心(合肥)

The History of OpenMPThe History of OpenMP What is directive/pragma?What is directive/pragma?

Directive-based general purpose parallel programming API with emphasis Directive-based general purpose parallel programming API with emphasis on the ability to parallelize existing serial programson the ability to parallelize existing serial programs

Why a new standard?Why a new standard? Who’s Involved?Who’s Involved?

Parallelism model and basic directivesParallelism model and basic directives Fortran77Fortran77, Fortran90, Fortran90 C, C++C, C++

OpenMP 标准

Page 21: 第十三章 共享存储系统编程

国家高性能计算中心(合肥)

The History of OpenMP A key intermediate step was X3H5 in the late 80’s.A key intermediate step was X3H5 in the late 80’s.

An official standards effort to agree on a parallel dialect of Fortran for An official standards effort to agree on a parallel dialect of Fortran for shared memory computers.shared memory computers.

The X3H5 effort failed. It was too big and too late.The X3H5 effort failed. It was too big and too late. OpenMP is born:OpenMP is born:

In 1996 a group formed to create an industry standard set of directives for In 1996 a group formed to create an industry standard set of directives for SMP programmingSMP programming

This group called itself the OpenMP Architecture Review Board(the This group called itself the OpenMP Architecture Review Board(the ARB) who takes care of OpenMPARB) who takes care of OpenMP

Page 22: 第十三章 共享存储系统编程

国家高性能计算中心(合肥)

The History of OpenMP(cont.) The ARB has released the following specifications:The ARB has released the following specifications:

OpenMP 1.0 for Fortran, Nov. 1997OpenMP 1.0 for Fortran, Nov. 1997 OpenMP 1.0 for C/C++, Nov. 1998OpenMP 1.0 for C/C++, Nov. 1998 OpenMP Fortran Interpretations, Spring 1999OpenMP Fortran Interpretations, Spring 1999 OpenMP 2.0(soon)OpenMP 2.0(soon)

OpenMP is an evolving standard. Send comments over the OpenMP is an evolving standard. Send comments over the feedback link on the OpenMP web site(feedback link on the OpenMP web site(http://www.http://www.openmpopenmp.org.org))

Page 23: 第十三章 共享存储系统编程

国家高性能计算中心(合肥)

为什么要建立新标准 ? ANSI X3H5, 1994ANSI X3H5, 1994

时机不好时机不好 , , 分布式机器流行分布式机器流行 只支持循环级并行性,粒度太细只支持循环级并行性,粒度太细

Pthreads(IEEE Posix 1003.4a )Pthreads(IEEE Posix 1003.4a ) 是为低端是为低端 (low end)(low end) 的共享机器的共享机器 (( 如如 SMP)SMP) 的标准的标准 对对 FORTRANFORTRAN 的支持不够的支持不够 适合任务并行适合任务并行 , , 而不适合数据并行而不适合数据并行

MPI MPI 消息传递的编程标准消息传递的编程标准 , , 对程序员要求高对程序员要求高 HPF HPF 主要用于主要用于分布式存储机器分布式存储机器 大量已有的科学应用程序需要很好地被继承和移植大量已有的科学应用程序需要很好地被继承和移植

Page 24: 第十三章 共享存储系统编程

国家高性能计算中心(合肥)

In a Nutshell A set of directives(library routines, and environment variables) used to annotate a A set of directives(library routines, and environment variables) used to annotate a

sequential program to indicate how it should be executed in parallel—sequential program to indicate how it should be executed in parallel— 继承继承 X3H5X3H5的许多概念的许多概念

Portable, Simple and Scalable Shared Memory Multiprocessing APIPortable, Simple and Scalable Shared Memory Multiprocessing API not a new languagenot a new language not automatic parallelization not automatic parallelization extend base languages: Fortran77, Fortran90, C and C++extend base languages: Fortran77, Fortran90, C and C++ Multi-vendor Support, for both UNIX and NTMulti-vendor Support, for both UNIX and NT

Standardizes Fine Grained(Loop) Parallelism, also Supports Coarse Grained Standardizes Fine Grained(Loop) Parallelism, also Supports Coarse Grained AlgorithmsAlgorithms

Page 25: 第十三章 共享存储系统编程

国家高性能计算中心(合肥)

OpenMP是什么? 一组编译制导语句和可调用的运行一组编译制导语句和可调用的运行 (run-time)(run-time) 库函数库函数 , , 扩充到基本语扩充到基本语

言中用来表达程序中的并行性言中用来表达程序中的并行性 编译制导语句包括编译制导语句包括 : : 在串行程序中加入下列结构在串行程序中加入下列结构

SPMD(Single Program Multiple Data) constructsSPMD(Single Program Multiple Data) constructs work-sharing constructswork-sharing constructs synchronization constructssynchronization constructs

data environment constructsdata environment constructs 运行库函数包括运行库函数包括 ::

execution environment routinesexecution environment routines lock routines lock routines

另外另外 , , 在在 FORTRANFORTRAN 标准中标准中 , , 还包括对环境变量的描述还包括对环境变量的描述

Page 26: 第十三章 共享存储系统编程

国家高性能计算中心(合肥)

Page 27: 第十三章 共享存储系统编程

国家高性能计算中心(合肥)

OpenMP当前的状况 19971997 年年 1010 月月 2828 日日 , DEC, IBM, Intel, SGI, , DEC, IBM, Intel, SGI, 和 和 Kuch & Associates Kuch & Associates 等等

公司的代表们决定制定一种适用于多种硬件平台的共享存储编程的新的工业公司的代表们决定制定一种适用于多种硬件平台的共享存储编程的新的工业应用标准应用标准

接着接着 , , 全球很多的组织和全球很多的组织和 ISVISV 决定支持这一标准决定支持这一标准 , , 如如 DOE/ASCI, DOE/ASCI, Livermore Software Technology Corp., Fluent Inc., Absoft Corp. , Livermore Software Technology Corp., Fluent Inc., Absoft Corp. , Ansys Inc. Etc. Ansys Inc. Etc.

目前支持目前支持 FORTRANFORTRAN 语言语言 , C , C 和和 C, C, 并建有专门的网址 并建有专门的网址 http://www.openmp.orghttp://www.openmp.org

在科研机构中在科研机构中 , , 也引起了足够的重视也引起了足够的重视 , , 被认为是被认为是 2121 世纪最受欢迎的并行编世纪最受欢迎的并行编程标准程标准

OpenMP on NOWs (SC98, Nov. 1998)OpenMP on NOWs (SC98, Nov. 1998) Integrated OpenMP and MPI on ClustersIntegrated OpenMP and MPI on Clusters

Page 28: 第十三章 共享存储系统编程

国家高性能计算中心(合肥)

Page 29: 第十三章 共享存储系统编程

国家高性能计算中心(合肥)

SPMD的程序执行模型 P0 P1 P2 . . . Pn

Page 30: 第十三章 共享存储系统编程

国家高性能计算中心(合肥)

SMP的程序执行模型

P0 P1 P2 . . . Pn

Page 31: 第十三章 共享存储系统编程

国家高性能计算中心(合肥)

OpenMP的程序执行模型

P0 P1 P2 . . . Pn

Page 32: 第十三章 共享存储系统编程

国家高性能计算中心(合肥)

•Parallel and work sharing directives

•data environment directives

•synchronization directives

Page 33: 第十三章 共享存储系统编程

国家高性能计算中心(合肥)

编译制导语句 (1)

Work-sharing constructsWork-sharing constructs 将结构内的任务分配到处理机中将结构内的任务分配到处理机中 , , 必须动态地放在必须动态地放在 Parallel region Parallel region

construct construct 中中 , , 进入这种结构之前并不隐含进入这种结构之前并不隐含 BARRIERBARRIER 操作操作 DO(DO( 最常用最常用 ))

有有 SCHEDULESCHEDULE 选项选项 , , 可以指定采用什么调度算法可以指定采用什么调度算法 SECTIONS(SECTIONS( 可以流水线执行之可以流水线执行之 )) SINGLE(SINGLE( 只有一个处理机执行之只有一个处理机执行之 ))

Page 34: 第十三章 共享存储系统编程

国家高性能计算中心(合肥)

•Parallel Region: parallel, end parallel

•Work Sharing: do, sections, single(parallel do, nowait)

•Fork-Join model of parallel execution(static,dynamic,orphaned)

Parallel Region and Work Sharing Directives

Page 35: 第十三章 共享存储系统编程

国家高性能计算中心(合肥)

编译制导语句 (2)

指令格式指令格式 固定形式 固定形式 !$OMP !$OMP 自由形式 自由形式 !$OMP, *$OMP, C$OMP!$OMP, *$OMP, C$OMP

Parallel Region ConstructParallel Region Construct !$OMP Parallel [clause[[,] clause] . . .]!$OMP Parallel [clause[[,] clause] . . .] Do I = 1, 20 Do I = 1, 20 A(I) = A(I) + B(I)A(I) = A(I) + B(I) !$OMP End Parallel !$OMP End Parallel (( 隐含隐含 BARRIERBARRIER 操作操作 )) 其中其中 ClauseClause 可以为可以为 ::PRIVATEPRIVATE(list), (list), SHAREDSHARED(list),(list),COPYINCOPYIN(list), (list),

FIRSTPRIVATEFIRSTPRIVATE(list), (list), DEFAULTDEFAULT(PRIVATE|SHARED|NONE), (PRIVATE|SHARED|NONE), REDUCTIONREDUCTION({operation|intrinsic}:list), ({operation|intrinsic}:list), IFIF(logical_expression)(logical_expression)

Page 36: 第十三章 共享存储系统编程

国家高性能计算中心(合肥)

DO编译制导语句 !$OMP DO [clause[[,] clause] . . .]!$OMP DO [clause[[,] clause] . . .] do_loopdo_loop [!$OMP END DO [NOWAIT]][!$OMP END DO [NOWAIT]] 例子例子 :: !$OMP PARALLEL DO !$OMP PARALLEL DO DO I = 2, NDO I = 2, N B(I) = ( A(I) + A(I-1)) /2.0B(I) = ( A(I) + A(I-1)) /2.0 ENDDOENDDO !$OMP END DO NOWAIT!$OMP END DO NOWAIT !$OMP END PARALLEL !$OMP END PARALLEL

Page 37: 第十三章 共享存储系统编程

国家高性能计算中心(合肥)

SECTIONS 编译制导语句 !$OMP SECTIONS!$OMP SECTIONS !$OMP SECTION !$OMP SECTION block1block1 !$OMP SECTION!$OMP SECTION block2block2 !$OMP SECTION !$OMP SECTION block3block3 !$OMP END SECTIONS!$OMP END SECTIONS

Page 38: 第十三章 共享存储系统编程

国家高性能计算中心(合肥)

编译制导语句 (3)

Data environment constructsData environment constructs THREADPRIVATETHREADPRIVATE

Data scope attribute clausesData scope attribute clauses PRIVATEPRIVATE SHAREDSHARED DEFAULTDEFAULT FIRSTPRIVATEFIRSTPRIVATE LASTPRIVATELASTPRIVATE REDUCTIONREDUCTION COPYINCOPYIN

Page 39: 第十三章 共享存储系统编程

国家高性能计算中心(合肥)

•Data Scope attribute clauses: Private, Shared, Default, Firstprivate, Lastprivate, Reduction and Copyin/Copyout(value undefined entering/exiting parallel region)

•Threadprivate directives:Private to a thread but global within the thread(SMP) Fortran:COMMON blocks/ C:file scope and static variables

Data Environment Directives

Page 40: 第十三章 共享存储系统编程

国家高性能计算中心(合肥)

编译制导语句 (4)

Synchronization constructsSynchronization constructs MASTERMASTER CRITICALCRITICAL BARRIERBARRIER ATOMICATOMIC FLUSHFLUSH ORDEREDORDERED

Page 41: 第十三章 共享存储系统编程

国家高性能计算中心(合肥)

例子 (ORDERED)

规定了各个线程执行的顺序规定了各个线程执行的顺序 !$OMP PARALLEL!$OMP PARALLEL !$OMP DO !$OMP DO ORDEREDORDERED SCHEDULE(DYNAMIC) SCHEDULE(DYNAMIC) DO I = LowBound, UpBound, StepDO I = LowBound, UpBound, Step CALL WORK(I)CALL WORK(I) END DOEND DO !$OMP END PARALLEL!$OMP END PARALLEL

SUBROUTINE WORK(K)SUBROUTINE WORK(K) !$OMP !$OMP ORDEREDORDERED WRITE(*,*) KWRITE(*,*) K !$OMP END !$OMP END ORDERED ORDERED END END

Page 42: 第十三章 共享存储系统编程

国家高性能计算中心(合肥)

Synchronization Directives

master, barrier, critical, atomic, flush, ordered

Page 43: 第十三章 共享存储系统编程

国家高性能计算中心(合肥)

OpenMP的 Orphan新特性 1 为了便于支持粗粒度的任务级并行为了便于支持粗粒度的任务级并行 , OpenMP , OpenMP 提供了提供了 OrphanOrphan

制导语句制导语句 OrphanOrphan 制导语句是指那些在并行区域制导语句是指那些在并行区域 (Parallel Region, (Parallel Region, 如如

PARALLEL)PARALLEL) 之外的制导语句 之外的制导语句 在在 OpenMPOpenMP 中提供了一种绑定规则使得这些中提供了一种绑定规则使得这些 OrphanOrphan 制导语句制导语句

与调用它们的并行区域产生联系与调用它们的并行区域产生联系 , , 这样大大地增加了程序的模块这样大大地增加了程序的模块性。性。 X3H5 X3H5 中不支持这一特点中不支持这一特点 , , 所有的同步和控制语句都必须依所有的同步和控制语句都必须依次出现在并行区域内次出现在并行区域内 ,, 无模块性。无模块性。

Page 44: 第十三章 共享存储系统编程

国家高性能计算中心(合肥)

OpenMP 的 Orphan 特性 2

Page 45: 第十三章 共享存储系统编程

国家高性能计算中心(合肥)

运行库函数 (1)

Execution Environment RoutinesExecution Environment Routines OMP_SET_NUM_THREADSOMP_SET_NUM_THREADS OMP_GET_NUM_THREADSOMP_GET_NUM_THREADS OMP_GET_MAX_THREADSOMP_GET_MAX_THREADS OMP_GET_THREAD_NUMOMP_GET_THREAD_NUM OMP_GET_NUM_PROCSOMP_GET_NUM_PROCS OMP_IN_PARALLELOMP_IN_PARALLEL OMP_SET_DYNAMICOMP_SET_DYNAMIC OMP_GET_DYNAMICOMP_GET_DYNAMIC OMP_SET _NESTED OMP_SET _NESTED OMP_GET_NESTEDOMP_GET_NESTED

Page 46: 第十三章 共享存储系统编程

国家高性能计算中心(合肥)

运行库函数 (2)

Lock RoutinesLock Routines OMP_INIT_LOCKOMP_INIT_LOCK OMP_DESTROY_LOCKOMP_DESTROY_LOCK OMP_SET_LOCKOMP_SET_LOCK OMP_UNSET_LOCKOMP_UNSET_LOCK OMP_TEST_LOCKOMP_TEST_LOCK

Page 47: 第十三章 共享存储系统编程

国家高性能计算中心(合肥)

OpenMP 计算的实例

Page 48: 第十三章 共享存储系统编程

国家高性能计算中心(合肥)

MPI 计算的实例

Page 49: 第十三章 共享存储系统编程

国家高性能计算中心(合肥)

OpenMP与其他标准的比较

Page 50: 第十三章 共享存储系统编程

国家高性能计算中心(合肥)

OpenMP的优点与缺点 优点优点

提供了一个可用的编程标准提供了一个可用的编程标准 可移植性可移植性 , , 简单简单 , , 可扩展性可扩展性 灵活支持多线程灵活支持多线程 , , 具有负载平衡的潜在能力具有负载平衡的潜在能力 支持支持 Orphan ScopeOrphan Scope, , 使程序更具有模块化使程序更具有模块化

缺点缺点 只适用于硬件共享存储型的机器只适用于硬件共享存储型的机器 动态可变的线程数使得支持起来困难动态可变的线程数使得支持起来困难