Introductions to Parallel Programming Using OpenMP.ppt
《Introductions to Parallel Programming Using OpenMP.ppt》由会员分享,可在线阅读,更多相关《Introductions to Parallel Programming Using OpenMP.ppt(62页珍藏版)》请在麦多课文档分享上搜索。
1、Introductions to Parallel Programming Using OpenMP,Zhenying Liu, Dr. Barbara Chapman High Performance Computing and Tools group Computer Science Department University of Houston,April 7, 2005,Content,Overview of OpenMP Acknowledgement OpenMP constructs (5 categories) OpenMP exercises References,Over
2、view of OpenMP,OpenMP is a set of extensions to Fortran/C/C+ OpenMP contains compiler directives, library routines and environment variables. Available on most single address space machines. shared memory systems, including cc-NUMA Chip MultiThreading: Chip MultiProcessing (Sun UltraSPARC IV), Simul
3、taneous Multithreading (Intel Xeon) not on distributed memory systems, classic MPPs, or PC clusters (yet!),Shared Memory Architecture,All processors have access to one global memory All processors share the same address space The system runs a single copy of the OS Processors communicate by reading/
4、writing to the global memory Examples: multiprocessor PCs (Intel P4), Sun Fire 15K, NEC SX-7, Fujitsu PrimePower, IBM p690, SGI Origin 3000.,Shared Memory Systems (cont),OpenMP Pthreads,Distributed Memory Systems,MPI HPF,Clustered of SMPs,MPIhybrid MPI + OpenMP,OpenMP Usage,Applications Applications
5、 with intense computational needs From video games to big science & engineering Programmer Accessibility From very early programmers in school to scientists to parallel computing experts Available to millions of programmers In every major (Fortran & C/C+) compiler,OpenMP Syntax,Most of the construct
6、s in OpenMP are compiler directives or pragmas. For C and C+, the pragmas take the form: #pragma omp construct clause clause For Fortran, the directives take one of the forms: C$OMP construct clause clause !$OMP construct clause clause *$OMP construct clause clause Since the constructs are directive
7、s, an OpenMP program can be compiled by compilers that dont support OpenMP.,OpenMP: Programming Model,Fork-Join Parallelism: Master thread spawns a team of threads as needed. Parallelism is added incrementally: i.e. the sequential program evolves into a parallel program.,OpenMP: How is OpenMP Typica
8、lly Used?,OpenMP is usually used to parallelize loops:Find your most time consuming loops.Split them up between threads.,void main() double Res1000;#pragma omp parallel forfor(int i=0;i1000;i+) do_huge_comp(Resi); ,void main() double Res1000;for(int i=0;i1000;i+) do_huge_comp(Resi); ,Split-up this l
9、oop between multiple threads,Sequential program,Parallel program,OpenMP: How do Threads Interact?,OpenMP is a shared memory model. Threads communicate by sharing variables. Unintended sharing of data can lead to race conditions: race condition: when the programs outcome changes as the threads are sc
10、heduled differently. To control race conditions: Use synchronization to protect data conflicts. Synchronization is expensive so: Change how data is stored to minimize the need for synchronization.,OpenMP vs. POSIX Threads,POSIX threads is the other widely used shared programming API. Fairly widely a
11、vailable, usually quite simple to implement on top of OS kernel threads. Lower level of abstraction than OpenMP library routines only, no directives more flexible, but harder to implement and maintain OpenMP can be implemented on top of POSIX threads Not much difference in availability not that many
12、 OpenMP C+ implementations no standard Fortran interface for POSIX threads,Content,Overview of OpenMP Acknowledgement OpenMP constructs (5 categories) OpenMP exercises References,Acknowledgement,Slides provided by Tim Mattson and Rudolf Eigenmann, SC 99 Mark Bull from EPCC OpenMP program examples La
13、wrence Livermore National Lab NAS FT parallelization from PGI tutorial Dr. Garbey provided us serial codes of Naiver-Stokes,Content,Overview of OpenMP Acknowledgement OpenMP constructs (5 categories) OpenMP exercises References,OpenMP Constructs,OpenMPs constructs fall into 5 categories: Parallel Re
14、gions Worksharing Data Environment Synchronization Runtime functions/environment variables OpenMP is basically the same between Fortran and C/C+,OpenMP: Parallel Regions,You create threads in OpenMP with the “omp parallel” pragma. For example, To create a 4-thread Parallel region:Each thread calls p
15、ooh(ID,A) for ID = 0 to 3,double A1000; omp_set_num_threads(4); #pragma omp parallel int ID =omp_get_thread_num();pooh(ID,A); ,Each thread redundantly executes the code within the structured block,OpenMP: Work-Sharing Constructs,The “for” Work-Sharing construct splits up loop iterations among the th
16、reads in a team,#pragma omp parallel #pragma omp for for (I=0;IN;I+)NEAT_STUFF(I); ,By default, there is a barrier at the end of the “omp for”. Use the “nowait” clause to turn off the barrier.,Work Sharing Constructs A motivating example,for(i=0;IN;i+) ai = ai + bi;,#pragma omp parallel int id, i, N
17、thrds, istart, iend;id = omp_get_thread_num();Nthrds = omp_get_num_threads();istart = id * N / Nthrds;iend = (id+1) * N / Nthrds;for(i=istart;Iiend;i+) ai=ai+bi; ,#pragma omp parallel #pragma omp for schedule(static) for(i=0;IN;i+) ai=ai+bi;,OpenMP parallel region and a work-sharing for construct,Se
18、quential code,OpenMP Parallel Region,OpenMP Parallel Region and a work-sharing for construct,OpenMP For Construct: The Schedule Clause,The schedule clause effects how loop iterations are mapped onto threadsuschedule(static ,chunk)Deal-out blocks of iterations of size “chunk” to each thread.uschedule
19、(dynamic,chunk)Each thread grabs “chunk” iterations off a queue until all iterations have been handled.uschedule(guided,chunk) Threads dynamically grab blocks of iterations. The size of the block starts large and shrinks down to size “chunk” as the calculation proceeds.uschedule(runtime) Schedule an
20、d chunk size taken from the OMP_SCHEDULE environment variable.,OpenMP: Work-Sharing Constructs,The Sections work-sharing construct gives a different structured block to each thread.,#pragma omp parallel #pragma omp sections X_calculation(); #pragma omp sectiony_calculation(); #pragma omp sectionz_ca
21、lculation(); ,By default, there is a barrier at the end of the “omp sections”. Use the “nowait” clause to turn off the barrier.,Data Environment: Changing Storage Attributes,One can selectively change storage attributes constructs using the following clauses* SHARED PRIVATE FIRSTPRIVATE THREADPRIVAT
22、E The value of a private inside a parallel loop can be transmitted to a global value outside the loop with: LASTPRIVATE The default status can be modified with: DEFAULT (PRIVATE | SHARED | NONE),* All data clauses apply to parallel regions and worksharing constructs except “shared” which only applie
23、s to parallel regions.,Data Environment: Default Storage Attributes,Shared Memory programming model: Most variables are shared by default Global variables are SHARED among threads Fortran: COMMON blocks, SAVE variables, MODULE variables C: File scope variables, static But not everything is shared. S
24、tack variables in sub-programs called from parallel regions are PRIVATE Automatic variables within a statement block are PRIVATE.,Private Clause,private(var) creates a local copy of var for each thread. The value is uninitialized Private copy is not storage associated with the original,void wrong()
25、int IS = 0; #pragma parallel for private(IS) for(int J=1;J1000;J+) IS = IS + J; printf(“%i”, IS); ,OpenMP: Reduction,Another clause that effects the way variables are shared:reduction (op : list) The variables in “list” must be shared in the enclosing parallel region. Inside a parallel or a workshar
- 1.请仔细阅读文档,确保文档完整性,对于不预览、不比对内容而直接下载带来的问题本站不予受理。
- 2.下载的文档,不会出现我们的网址水印。
- 3、该文档所得收入(下载+内容+预览)归上传者、原创作者;如果您是本文档原作者,请点此认领!既往收益都归您。
下载文档到电脑,查找使用更方便
2000 积分 0人已下载
下载 | 加入VIP,交流精品资源 |
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- INTRODUCTIONSTOPARALLELPROGRAMMINGUSINGOPENMPPPT

链接地址:http://www.mydoc123.com/p-372941.html