【教学课件】第十三章共享存储系统编程.ppt
《【教学课件】第十三章共享存储系统编程.ppt》由会员分享,可在线阅读,更多相关《【教学课件】第十三章共享存储系统编程.ppt(50页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、第十三章 共享存储系统编程共享存储系统编程 13.1 ANSI X3H513.1 ANSI X3H5共享存储模型共享存储模型 13.2 POSIX 13.2 POSIX 线程模型线程模型 13.3 OpenMP13.3 OpenMP模型模型编程标准的作用编程标准的作用 规定程序的执行模型规定程序的执行模型规定程序的执行模型规定程序的执行模型 SPMD,SMP SPMD,SMP 等等等等 如何表达并行性如何表达并行性如何表达并行性如何表达并行性 DOACROSS,FORALL,PARALLEL,INDEPENDENTDOACROSS,FORALL,PARALLEL,INDEPENDENT 如何表
2、达同步如何表达同步如何表达同步如何表达同步 Lock,Barrier,Semaphore,Condition VariablesLock,Barrier,Semaphore,Condition Variables 如何获得运行时的环境变量如何获得运行时的环境变量如何获得运行时的环境变量如何获得运行时的环境变量 threadid,num of processesthreadid,num of processesANSI X3H5共享存储器模型 Started in the mid-80s with the emergence of shared memory parallel Started i
3、n the mid-80s with the emergence of shared memory parallel computers with proprietary directive driven programming environmentscomputers with proprietary directive driven programming environments 更早的标准化结果更早的标准化结果PCFPCF共享存储器并行共享存储器并行FortranFortran 19931993年制定的概念性编程模型年制定的概念性编程模型 Language BindingLangua
4、ge Binding C C Fortran 77Fortran 77 Fortran 90Fortran 90 并行块(工作共享构造)并行块(工作共享构造)并行块并行块(psections.end psections)(psections.end psections)并行循环并行循环(pdo.Endo pdo)(pdo.Endo pdo)单进程单进程(psingle.End psingle)(psingle.End psingle)可嵌套可嵌套 非共享块重复执行非共享块重复执行 隐式路障隐式路障(nowait)(nowait),显式路障和阻挡操作,显式路障和阻挡操作 共享共享/私有变量私有变
5、量 线程同步线程同步 门插销门插销(latch)(latch):临界区:临界区 锁:锁:test,lock,unlocktest,lock,unlock 事件事件:wait,post,clear:wait,post,clear 序数序数(ordinal):(ordinal):顺序顺序X3H5:并行性构造Program mainProgram main!程序以顺序模式开始程序以顺序模式开始程序以顺序模式开始程序以顺序模式开始,此时只有一个此时只有一个此时只有一个此时只有一个A A!A!A只由基本线程执行,称为主线程只由基本线程执行,称为主线程只由基本线程执行,称为主线程只由基本线程执行,称为主线
6、程parallelparallel!转换为并行模式,派生出多个子线程(一个组)转换为并行模式,派生出多个子线程(一个组)转换为并行模式,派生出多个子线程(一个组)转换为并行模式,派生出多个子线程(一个组)B B!B!B为每个组员所复制为每个组员所复制为每个组员所复制为每个组员所复制psectionspsections!并行块开始并行块开始并行块开始并行块开始sectionsectionC C!一个组员执行一个组员执行一个组员执行一个组员执行C CsectionsectionD D!一个组员执行一个组员执行一个组员执行一个组员执行D Dend psectionsend psections!等待等
7、待等待等待C C和和和和D D都结束都结束都结束都结束psinglepsingle!暂时转换成顺序模式暂时转换成顺序模式暂时转换成顺序模式暂时转换成顺序模式E E!已由一个组员执行已由一个组员执行已由一个组员执行已由一个组员执行end psingleend psingle!转回并行模式转回并行模式转回并行模式转回并行模式pdo i=1,6pdo i=1,6!pdo!pdo构造开始构造开始构造开始构造开始F(i)F(i)!组员共享组员共享组员共享组员共享F F的六次迭代的六次迭代的六次迭代的六次迭代end pdo no waitend pdo no wait!无隐式路障同步无隐式路障同步无隐式路
8、障同步无隐式路障同步G G!更多的复制代码更多的复制代码更多的复制代码更多的复制代码end parallelend parallel!转为顺序模式转为顺序模式转为顺序模式转为顺序模式HH!初始化进程单独执行初始化进程单独执行初始化进程单独执行初始化进程单独执行HH.!可能有更多的并行构造可能有更多的并行构造可能有更多的并行构造可能有更多的并行构造EndEnd线程隐式路障同步PQRABCEF(1:2)GHGGF(3:4)F(5:6)DBB隐式路障同步隐式路障同步无隐式路障同步隐式路障同步共享存储系统编程 13.1 ANSI X3H513.1 ANSI X3H5共享存储模型共享存储模型 13.2
9、POSIX 13.2 POSIX 线程模型线程模型 13.3 OpenMP13.3 OpenMP模型模型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
10、,ResumeThreadGetThreadHandle,SetThreadPriority,SuspendThread,ResumeThread TLS(TLS(线程局部存储线程局部存储)TlsAlloc,TlsSetValue)TlsAlloc,TlsSetValue LinuxThreads:_clone and sys_cloneLinuxThreads:_clone and sys_clone 用户线程和内核线程用户线程和内核线程(LWP)(LWP)(一到一,一一到一,一到多,多到多到多,多到多)What Are Threads?General-purpose solution fo
11、r 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).Sha
12、red state(memory,files,etc.)Threads 线程共享相同的内存空间。线程共享相同的内存空间。与标准与标准 fork()fork()相比,线程带来的开销很小。内核无需单独复制进程的内相比,线程带来的开销很小。内核无需单独复制进程的内存空间或文件描述符等等。这就节省了大量的存空间或文件描述符等等。这就节省了大量的 CPU CPU 时间。时间。和进程一样,线程将利用多和进程一样,线程将利用多 CPUCPU。如果软件是针对多处理器系统设计的,。如果软件是针对多处理器系统设计的,计算密集型应用。计算密集型应用。支持内存共享无需使用繁琐的支持内存共享无需使用繁琐的 IPC IP
13、C 和其它复杂的通信机制。和其它复杂的通信机制。Linux _cloneLinux _clone不可移植,不可移植,PthreadPthread可移植可移植。POSIX POSIX 线程标准不记录任何线程标准不记录任何“家族家族”信息。无父无子。如果要等待一个信息。无父无子。如果要等待一个线程终止,就必须将线程的线程终止,就必须将线程的 tid tid 传递给传递给 pthread_join()pthread_join()。线程库无法为。线程库无法为您断定您断定 tidtid。POSIX Threads:Basics and Examples by Uday Kamathhttp:/www.c
14、oe.uncc.edu/abw/parallel/pthreads/pthreads.htmlPOSIX 线程详解线程详解:一种支持内存共一种支持内存共享的简单和快捷的工具享的简单和快捷的工具by Daniel Robbinshttp:/ 2Solaris 2pthread_createpthread_createthr_createthr_createpthread_exitpthread_exitthr_exitthr_exitpthread_killpthread_killthr_killthr_killpthread_joinpthread_jointhr_jointhr_joinpt
15、hread_selfpthread_selfthr_selfthr_self线程调用线程同步和互斥POSIXPOSIXSolaris 2Solaris 2pthread_mutex_initpthread_mutex_initmutex_initmutex_initpthread_ mutex_destroy pthread_ mutex_destroy mutex_destroymutex_destroypthread_ mutex_lock pthread_ mutex_lock mutex_lockmutex_lockpthread_ mutex_trylock pthread_ mut
16、ex_trylock mutex_trylockmutex_trylockpthread_ mutex_unlock pthread_ mutex_unlock mutex_unlockmutex_unlockpthread_cond_initpthread_cond_initpthread_cond_destroypthread_cond_destroypthread_cond_waitpthread_cond_waitpthread_cond_timedwaitpthread_cond_timedwaitpthread_cond_signalpthread_cond_signalpthre
17、ad_cond_broadcastpthread_cond_broadcastPthreadsPthreads实现计算实现计算 的实例的实例 1 1PthreadsPthreads实现计算实现计算 的实例的实例 2 2对生产者驱动的有界缓冲区问题的对生产者驱动的有界缓冲区问题的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_mut
18、ex_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);nite
19、ms+;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_mu
20、tex_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_co
21、nd_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_
22、item(&myitem);sum+=myitem;sum+=myitem;pthread_mutex_lock(&slot_lock pthread_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;共享存储系统编程 13.1 ANSI X3H513.1 ANSI X3H5共享存储模型共享存储模型 13.2 P
23、OSIX 13.2 POSIX 线程模型线程模型 13.3 OpenMP13.3 OpenMP模型模型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
24、 to parallelize existing serial programson the ability to parallelize existing serial programs Why a new standard?Why a new standard?Whos Involved?Whos Involved?Parallelism model and basic directivesParallelism model and basic directives Fortran77Fortran77,Fortran90,Fortran90 C,C+C,C+OpenMP标准The His
25、tory of OpenMP A key intermediate step was X3H5 in the late 80s.A key intermediate step was X3H5 in the late 80s.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
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 教学课件 教学 课件 第十三 共享 存储系统 编程
限制150内