中国高校课件下载中心 》 教学资源 》 大学文库

华东师范大学:《高等数值分析(高性能计算/并行计算)》课程教学资源(讲义)04 OpenMP并行编程(二)工作共享结构、同步与数据环境

文档信息
资源类别:文库
文档格式:PDF
文档页数:26
文件大小:682.51KB
团购合买:点击进入团购
内容简介
编译制导:工作共享结构(续) 编译制导:同步指令 编译制导:数据环境指令
刷新页面文档预览

0 華柬师免大学|数学科学学院 chool of Ma sEast China Normal University OpenMP并行编程 (二) 工作共享结构 同步与数据环境

OpenMP 并行编程 (二) —— 工作共享结构 —— 同步与数据环境

华东师范大学数学科学学院 目录页 School of Mathematical Sciences,ECNU 案 Contents 编译制导:工作共享结构(续) 2 编译制导:同步指令 编译制导:数据环境指令 http://math.ecnu.edu.cn/~jypan

http://math.ecnu.edu.cn/~jypan 目录页 Contents 华东师范大学 数学科学学院 School of Mathematical Sciences, ECNU http://math.ecnu.edu.cn/~jypan 1 2 编译制导:工作共享结构(续) 编译制导:同步指令 3 编译制导:数据环境指令

华东师范大学数学科学学院 目录页 School of Mathematical Sciences,ECNU 案 Contents 工作共享结构(续) sections section 1 工作共享结构 single master 2 同步结构 ■ task(略) 3 数据环境结构 http://math.ecnu.edu.cn/-jypan

http://math.ecnu.edu.cn/~jypan 目录页 Contents 华东师范大学 数学科学学院 School of Mathematical Sciences, ECNU http://math.ecnu.edu.cn/~jypan  sections / section  single  master  task(略) 1 2 工作共享结构 同步结构 3 数据环境结构 1 工作共享结构(续)

内容提要 OpenMP编译制导 ●工作共享结构: for,sections,single,master,task,workshare ●同步指令: critical,barrier,atomic,flush,ordered 。数据环境指令 threadprivate ·子句: private,firstprivate,..... http://math.ecnu.edu.cn/-jypan 4

http://math.ecnu.edu.cn/~jypan 4 内容提要  OpenMP 编译制导  工作共享结构: for,sections,single,master,task,workshare  同步指令: critical,barrier,atomic,flush,ordered  数据环境指令 threadprivate  子句: private,firstprivate,… …

SECTIONS结构 !Somp sections [clause clause ... !Somp section structured-block Fortran !Somp section structured-block !Somp end sections [nowait] #pragma omp sections [clause clause...] { #pragma omp section C/C++ structured-block #pragma omp section structured-block ●sections也可以与parallel合并,即#pragma omp parallel sections http://math.ecnu.edu.cn/-jypan 5

http://math.ecnu.edu.cn/~jypan 5 SECTIONS 结构 Fortran !$omp sections [clause clause ...] !$omp section structured-block !$omp section structured-block !$omp end sections [nowait] C/C++ #pragma omp sections [clause clause ...] { #pragma omp section structured-block #pragma omp section structured-block . . . . . . . . . . . . }  sections 也可以与 parallel 合并,即 #pragma omp parallel sections

SECTIONS结构 ●指令sections创建一个工作共享域 ·域中的子任务由指令section创建,必须是一个相对独立的完整代码块 ·每个子任务都将只被一个线程执行 ●结束处隐含障碍同步,除非显式指明nowait ●sections可用的字句有 private(list) firstprivate(list) lastprivate(list) reduction(op:list) nowait http://math.ecnu.edu.cn/-jypan 6

http://math.ecnu.edu.cn/~jypan 6 SECTIONS 结构  指令 sections 创建一个工作共享域  域中的子任务由指令 section 创建,必须是一个相对独立的完整代码块  每个子任务都将只被一个线程执行  结束处隐含障碍同步,除非显式指明 nowait  sections 可用的字句有 private(list) firstprivate(list) lastprivate(list) reduction(op : list) nowait

SECTIONS示例 #pragma omp parallel num_threads(3) #pragma omp sections #pragma omp section printf("Hello world!\n"); #pragma omp section printf("Hello Math!\n"); #pragma omp section printf("Hello OpenMP!\n"); OMP sections 01.c OMP_sections_02.c ●注意sections和section的区别 ●#pragma omp section必须在sections域中 ·共享域中的每个子任务必须由section指令创建 ●第一个子任务前面的section指令可以省略 ●子任务个数小于线程个数时,多余的线程空闲等待 ·子任务个数大于线程个数时,任务分配由编译器指定,尽量负载平衡 http://math.ecnu.edu.cn/-jypan

http://math.ecnu.edu.cn/~jypan 7 SECTIONS 示例  注意 sections 和 section 的区别  #pragma omp section 必须在 sections 域中  共享域中的每个子任务必须由 section 指令创建  第一个子任务前面的 section 指令可以省略  子任务个数小于线程个数时,多余的线程空闲等待  子任务个数大于线程个数时,任务分配由编译器指定,尽量负载平衡 #pragma omp parallel num_threads(3) #pragma omp sections { #pragma omp section printf("Hello world!\n"); #pragma omp section printf("Hello Math!\n"); #pragma omp section printf("Hello OpenMP!\n"); } OMP_sections_01.c OMP_sections_02.c

SINGLE结构 !Somp single [clause clause ... Fortran structured-block !Somp end single [nowait copyprivate(list)] #pragma omp single [clause clause ... C/C++ structured-block ●用在并行域中,指定代码块只能由一个线程执行(不一定是主线程) ●第一个遇到single指令的线程执行相应的代码,其它线程则在single 结尾处等待,除非显式指明nowait ·可用的字句有 private(list) firstprivate(list) copyprivate(Iist)//将串行计算的值广播给并行域中的同名变量 nowait http://math.ecnu.edu.cn/-jypan 8

http://math.ecnu.edu.cn/~jypan 8 SINGLE 结构 Fortran !$omp single [clause clause ...] structured-block !$omp end single [nowait copyprivate(list)] C/C++ #pragma omp single [clause clause ...] { structured-block }  用在并行域中,指定代码块只能由一个线程执行(不一定是主线程)  第一个遇到 single 指令的线程执行相应的代码,其它线程则在 single 结尾处等待,除非显式指明 nowait  可用的字句有 private(list) firstprivate(list) copyprivate(list) // 将串行计算的值广播给并行域中的同名变量 nowait

MASTER结构 !Somp master Fortran structured-block !Somp end master #pragma omp master C/C++ structured-block ●master块仅由线程组中的主线程执行 ·其它线程跳过并继续执行下面的代码,即结尾处没有隐式同步 ●通常用于I/O ●与single [nowait]的区别: master指定由主线程执行,而single由最先到达的线程执行 http://math.ecnu.edu.cn/~jypan 9

http://math.ecnu.edu.cn/~jypan 9 MASTER 结构 Fortran !$omp master structured-block !$omp end master C/C++ #pragma omp master { structured-block }  master 块仅由线程组中的主线程执行  其它线程跳过并继续执行下面的代码,即结尾处没有隐式同步  通常用于 I/O  与 single [nowait] 的区别: master 指定由主线程执行,而 single 由最先到达的线程执行

华东师范大学数学科学学院 目录页 School of Mathematical Sciences,ECNU 案 Contents 同步结构 Synchronization Constructs critical ■ barrier 工作共享结构 ■ atomic 2 同步结构 flush ordered 3 数据环境结构 taskwait/taskyield http://math.ecnu.edu.cn/~jypan

http://math.ecnu.edu.cn/~jypan 目录页 Contents 华东师范大学 数学科学学院 School of Mathematical Sciences, ECNU http://math.ecnu.edu.cn/~jypan  critical  barrier  atomic  flush  ordered  taskwait/taskyield(略) 1 2 工作共享结构 同步结构 3 数据环境结构 2 同步结构 Synchronization Constructs

共26页,试读已结束,阅读完整版请下载
刷新页面下载完整文档
VIP每日下载上限内不扣除下载券和下载次数;
按次数下载不扣除下载券;
注册用户24小时内重复下载只扣除一次;
顺序:VIP每日次数-->可用次数-->下载券;
相关文档