并行程序设计06OpenMP多线程程序设计

上传人:w****i 文档编号:92843653 上传时间:2019-07-13 格式:PPT 页数:46 大小:795.50KB
返回 下载 相关 举报
并行程序设计06OpenMP多线程程序设计_第1页
第1页 / 共46页
并行程序设计06OpenMP多线程程序设计_第2页
第2页 / 共46页
并行程序设计06OpenMP多线程程序设计_第3页
第3页 / 共46页
并行程序设计06OpenMP多线程程序设计_第4页
第4页 / 共46页
并行程序设计06OpenMP多线程程序设计_第5页
第5页 / 共46页
点击查看更多>>
资源描述

《并行程序设计06OpenMP多线程程序设计》由会员分享,可在线阅读,更多相关《并行程序设计06OpenMP多线程程序设计(46页珍藏版)》请在金锄头文库上搜索。

1、Parallel Programming,Instructor: Zhang Weizhe (张伟哲) Computer Network and Information Security Technique Research Center , School of Computer Science and Technology, Harbin Institute of Technology,2,Programming with OpenMP,Outline,3,What Is OpenMP*?,OpenMP is a set of extensions to Fortran/C/C+ OpenM

2、P 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), Simultaneous Multithreading (Intel Xeon) not on distributed memory syst

3、ems, classic MPPs, or PC clusters (yet!),4,What Is OpenMP*?,Compiler directives for multithreaded programming Easy to create threaded Fortran and C/C+ codes Supports data parallelism model Incremental parallelism Combines serial and parallel code in single source,5,What Is OpenMP*?,omp_set_lock(lck)

4、,#pragma omp parallel for private(A, B),#pragma omp critical,C$OMP parallel do shared(a, b, c),C$OMP PARALLEL REDUCTION (+: A, B),call OMP_INIT_LOCK (ilok),call omp_test_lock(jlok),setenv OMP_SCHEDULE “dynamic”,CALL OMP_SET_NUM_THREADS(10),C$OMP DO lastprivate(XX),C$OMP ORDERED,C$OMP SINGLE PRIVATE(

5、X),C$OMP SECTIONS,C$OMP MASTER,C$OMP ATOMIC,C$OMP FLUSH,C$OMP PARALLEL DO ORDERED PRIVATE (A, B, C),C$OMP THREADPRIVATE(/ABC/),C$OMP PARALLEL COPYIN(/blk/),Nthrds = OMP_GET_NUM_PROCS(),!$OMP BARRIER,http:/www.openmp.org Current spec is OpenMP 2.5 250 Pages (combined C/C+ and Fortran),6,OpenMP Syntax

6、,Most of the constructs 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 co

7、nstructs are directives, an OpenMP program can be compiled by compilers that dont support OpenMP.,7,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.,8,Ope

8、nMP:How is OpenMP Typically 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 for for(int i=0;i1000;i+) do_huge_comp(Resi); ,void main() double Res1000; for(int i=0;i1000;i+) do_huge

9、_comp(Resi); ,Split-up this loop between multiple threads,Sequential program,Parallel program,9,OpenMP vs. POSIX Threads,POSIX threads is the other widely used shared programming API. Fairly widely available, usually quite simple to implement on top of OS kernel threads. Lower level of abstraction t

10、han 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 OpenMP C+ implementations no standard Fortran interface for POSIX threads,10,OpenMP Constructs,OpenM

11、Ps constructs fall into 5 categories: Parallel Regions Worksharing Data Environment Synchronization Runtime functions/environment variables OpenMP is basically the same between Fortran and C/C+,11,OpenMP: Parallel Regions,You create threads in OpenMP with the “omp parallel” pragma. For example, To c

12、reate a 4-thread Parallel region: Each thread calls pooh(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,12,How Many Threads?,Set environment variable for

13、 number of threads set OMP_NUM_THREADS=4 There is no standard default for this variable Many systems: # of threads = # of processors Intel compilers use this default,13,14,OpenMP: Work-Sharing Constructs,Splits loop iterations into threads Must be in the parallel region Must precede the loop,#pragma

14、 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.,15,Work-sharing Construct,Threads are assigned an independent set of iterations Threads must wait at the end of work-sharing cons

15、truct,#pragma omp parallel #pragma omp for for(i = 1, i 13, i+) ci = ai + bi,16,Work Sharing Constructs A motivating example,for(i=0;IN;i+) ai = ai + bi;,#pragma omp parallel int id, i, Nthrds, istart, iend; id = omp_get_thread_num(); Nthrds = omp_get_num_threads(); istart = id * N / Nthrds; iend =

16、(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,Sequential code,OpenMP Parallel Region,OpenMP Parallel Region and a work-sharing for construct,17,Assigning Iterations to Threads,The schedule clause of the for directive deals with the assignment of itera

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 高等教育 > 其它相关文档

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号