博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
2、OpenMP的任务调度schedule(static|dynamic|guided|runtime[size])
阅读量:4171 次
发布时间:2019-05-26

本文共 10576 字,大约阅读时间需要 35 分钟。

基本思想:对于for的任务分担 schedule(static|dynamic|guided|runtime[size])

(1)for的任务分担

#pragma omp parallel{#pragma omp forfor(int i=0;i

测试代码

#include 
#include
#include
#include
#include
using namespace std;using namespace chrono;void sequentialProgram(int num){ for(int i=0;i
(end_time-start_time).count()<<" seconds"<
(end_time-start_time).count()<<" seconds"<

测试结果,在一个并行域中,对多个for进行制导指令处理,可以使用调度指令简化完成这一操作

F:\OpenMP\cmake-build-debug\OpenMP.exehello world the current thread id: 0hello world the current thread id: 0hello world the current thread id: 0hello world the current thread id: 0hello world the current thread id: 0hello world the current thread id: 0hello world the current thread id: 0hello world the current thread id: 0hello world the current thread id: 0hello world the current thread id: 0hello world the current thread id: 0hello world the current thread id: 0hello world the current thread id: 0hello world the current thread id: 0hello world the current thread id: 0hello world the current thread id: 0hello world the current thread id: 0hello world the current thread id: 0hello world the current thread id: 0hello world the current thread id: 0hello world the current thread id: 0hello world the current thread id: 0hello world the current thread id: 0hello world the current thread id: 0sequentialProgram elapse time: 0.0776085 secondsA hello world the current thread id: 1A hello world the current thread id: 0A hello world the current thread id: 3A hello world the current thread id: 5A hello world the current thread id: 7A hello world the current thread id: 10A hello world the current thread id: 9A hello world the current thread id: 8A hello world the current thread id: 2A hello world the current thread id: 4A hello world the current thread id: 6A hello world the current thread id: 11B hello world the current thread id: 1B hello world the current thread id: 0B hello world the current thread id: 7B hello world the current thread id: 9B hello world the current thread id: 2B hello world the current thread id: 6B hello world the current thread id: 4B hello world the current thread id: 10B hello world the current thread id: 3B hello world the current thread id: 8B hello world the current thread id: 5B hello world the current thread id: 11parallelProgram elapse time: 0.0527985 secondsProcess finished with exit code 0

(2)使用for的调度指令schedule

#pragma omp parallel for schedule(static|dynamic}guided|runtime[size]) for (int i = 0; i < num; i++) {       .......    }

 当写成

#pragma omp parallel for等价#pragma omp parallel for schedule(static)等价#pragma omp parallel for schedule(static,num/omp_get_num_procs()) //  num=omp_get_num_procs()*2;

其中static 设置为多少线程来处理迭代计算任务

其中size 为可选项,当不设置size参数时,默认for循环的线程以num/omp_get_num_procs()来分配

测试代码

#include 
#include
#include
#include
#include
using namespace std;using namespace chrono;void sequentialProgram(int num){ for(int i=0;i
(end_time-start_time).count()<<" seconds"<
(end_time-start_time).count()<<" seconds"<

测试结果是相同的

F:\OpenMP\cmake-build-debug\OpenMP.exei=0 the current thread id: 0i=1 the current thread id: 0i=2 the current thread id: 0i=3 the current thread id: 0i=4 the current thread id: 0i=5 the current thread id: 0i=6 the current thread id: 0i=7 the current thread id: 0i=8 the current thread id: 0i=9 the current thread id: 0i=10 the current thread id: 0i=11 the current thread id: 0i=12 the current thread id: 0i=13 the current thread id: 0i=14 the current thread id: 0i=15 the current thread id: 0i=16 the current thread id: 0i=17 the current thread id: 0i=18 the current thread id: 0i=19 the current thread id: 0i=20 the current thread id: 0i=21 the current thread id: 0i=22 the current thread id: 0i=23 the current thread id: 0sequentialProgram elapse time: 0.0422739 secondsi=0 the current thread id: 0i=1 the current thread id: 0i=4 the current thread id: 2i=5 the current thread id: 2i=14 the current thread id: 7i=15 the current thread id: 7i=18 the current thread id: 9i=19 the current thread id: 9i=16 the current thread id: 8i=17 the current thread id: 8i=12 the current thread id: 6i=13 the current thread id: 6i=2 the current thread id: 1i=3 the current thread id: 1i=10 the current thread id: 5i=11 the current thread id: 5i=6 the current thread id: 3i=7 the current thread id: 3i=8 the current thread id: 4i=9 the current thread id: 4i=22 the current thread id: 11i=23 the current thread id: 11i=20 the current thread id: 10i=21 the current thread id: 10parallelProgram elapse time: 0.0412098 secondsProcess finished with exit code 0

(3)虽然参数static均衡的分担任务,但是存在某些线程处理速度上的差异,因此引入dynamic

#pragma omp parallel for schedule(dynamic)     for (int i = 0; i < num; i++) {      ......    }}

测试代码

#include 
#include
#include
#include
#include
using namespace std;using namespace chrono;void sequentialProgram(int num){ for(int i=0;i
(end_time-start_time).count()<<" seconds"<
(end_time-start_time).count()<<" seconds"<

测试结果可以看出,线程id=9处理速度较快,因此承担了更多的任务,当然也可以使用size进行限制线程处理任务的数量~

F:\OpenMP\cmake-build-debug\OpenMP.exei=0 the current thread id: 0i=1 the current thread id: 0i=2 the current thread id: 0i=3 the current thread id: 0i=4 the current thread id: 0i=5 the current thread id: 0i=6 the current thread id: 0i=7 the current thread id: 0i=8 the current thread id: 0i=9 the current thread id: 0i=10 the current thread id: 0i=11 the current thread id: 0i=12 the current thread id: 0i=13 the current thread id: 0i=14 the current thread id: 0i=15 the current thread id: 0i=16 the current thread id: 0i=17 the current thread id: 0i=18 the current thread id: 0i=19 the current thread id: 0i=20 the current thread id: 0i=21 the current thread id: 0i=22 the current thread id: 0i=23 the current thread id: 0sequentialProgram elapse time: 0.041236 secondsi=0 the current thread id: 2i=6 the current thread id: 9i=13 the current thread id: 9i=14 the current thread id: 9i=15 the current thread id: 9i=16 the current thread id: 9i=17 the current thread id: 9i=18 the current thread id: 9i=19 the current thread id: 9i=20 the current thread id: 9i=21 the current thread id: 9i=22 the current thread id: 9i=23 the current thread id: 9i=5 the current thread id: 11i=3 the current thread id: 1i=4 the current thread id: 8i=7 the current thread id: 4i=1 the current thread id: 10i=2 the current thread id: 3i=8 the current thread id: 0i=9 the current thread id: 6i=10 the current thread id: 7i=11 the current thread id: 5i=12 the current thread id: 2parallelProgram elapse time: 0.0399313 secondsProcess finished with exit code 0

(4)guided 采用启发式调度算法,开始分配较大的块,然后逐渐变小,最后分配给每个线程的任务为size数量,如果没设置size,将在最后分配给每个任务量为1

#pragma omp parallel for schedule(guided)     for (int i = 0; i < num; i++) {      .....    }}

测试代码

#include 
#include
#include
#include
#include
using namespace std;using namespace chrono;void sequentialProgram(int num){ for(int i=0;i
(end_time-start_time).count()<<" seconds"<
(end_time-start_time).count()<<" seconds"<

测试结果,第一次先为每个线程分配两个任务,然后最后变成每个线程只能承担一个任务 执行

F:\OpenMP\cmake-build-debug\OpenMP.exei=0 the current thread id: 0i=1 the current thread id: 0i=2 the current thread id: 0i=3 the current thread id: 0i=4 the current thread id: 0i=5 the current thread id: 0i=6 the current thread id: 0i=7 the current thread id: 0i=8 the current thread id: 0i=9 the current thread id: 0i=10 the current thread id: 0i=11 the current thread id: 0i=12 the current thread id: 0i=13 the current thread id: 0i=14 the current thread id: 0i=15 the current thread id: 0i=16 the current thread id: 0i=17 the current thread id: 0i=18 the current thread id: 0sequentialProgram elapse time: 0.033042 secondsi=0 the current thread id: 0i=1 the current thread id: 0i=16 the current thread id: 0i=17 the current thread id: 0i=18 the current thread id: 0i=6 the current thread id: 5i=7 the current thread id: 5i=2 the current thread id: 3i=3 the current thread id: 3i=13 the current thread id: 6i=14 the current thread id: 2i=15 the current thread id: 11i=10 the current thread id: 8i=12 the current thread id: 9i=8 the current thread id: 7i=9 the current thread id: 1i=11 the current thread id: 10i=4 the current thread id: 4i=5 the current thread id: 4parallelProgram elapse time: 0.0334159 secondsProcess finished with exit code 0

(5)runtime 设置之后,将获取系统的任务属性来来调用上述三种中的一种方法,我测试一下,好像每次都是以dynamic 的方式调用~~

#pragma omp parallel for schedule(runtime)    for (int i = 0; i < num; i++) {       ......    }}

测试代码

#include 
#include
#include
#include
#include
using namespace std;using namespace chrono;void sequentialProgram(int num){ for(int i=0;i
(end_time-start_time).count()<<" seconds"<
(end_time-start_time).count()<<" seconds"<

测试结果

F:\OpenMP\cmake-build-debug\OpenMP.exei=0 the current thread id: 0i=1 the current thread id: 0i=2 the current thread id: 0i=3 the current thread id: 0i=4 the current thread id: 0i=5 the current thread id: 0i=6 the current thread id: 0i=7 the current thread id: 0i=8 the current thread id: 0i=9 the current thread id: 0i=10 the current thread id: 0i=11 the current thread id: 0i=12 the current thread id: 0i=13 the current thread id: 0i=14 the current thread id: 0i=15 the current thread id: 0i=16 the current thread id: 0i=17 the current thread id: 0i=18 the current thread id: 0i=19 the current thread id: 0i=20 the current thread id: 0i=21 the current thread id: 0i=22 the current thread id: 0i=23 the current thread id: 0sequentialProgram elapse time: 0.0410057 secondsi=0 the current thread id: 1i=8 the current thread id: 9i=13 the current thread id: 9i=14 the current thread id: 9i=15 the current thread id: 9i=16 the current thread id: 9i=17 the current thread id: 9i=18 the current thread id: 9i=19 the current thread id: 9i=20 the current thread id: 9i=21 the current thread id: 9i=22 the current thread id: 9i=23 the current thread id: 9i=6 the current thread id: 2i=5 the current thread id: 8i=7 the current thread id: 11i=3 the current thread id: 10i=4 the current thread id: 3i=2 the current thread id: 4i=1 the current thread id: 7i=9 the current thread id: 0i=10 the current thread id: 6i=11 the current thread id: 5i=12 the current thread id: 1parallelProgram elapse time: 0.042588 secondsProcess finished with exit code 0

转载地址:http://btyai.baihongyu.com/

你可能感兴趣的文章
基础编程题目集 - 7-1 厘米换算英尺英寸(15 分)
查看>>
关于转义序列
查看>>
C++ Template 基础篇(一):函数模板
查看>>
C++ Template 基础篇(二):类模板
查看>>
内存泄漏和内存溢出的区别和联系
查看>>
C/C++读写文本文件、二进制文件
查看>>
Dangling pointer(悬垂指针、迷途指针)和 Wild pointer(野指针)
查看>>
#pragma once用法总结
查看>>
斐波那契数列的三种解法及时间复杂度
查看>>
避免if语句的深层次嵌套
查看>>
C++中类的声明和类的实现分开
查看>>
VS中常用的快捷键
查看>>
UML学习入门就这一篇文章
查看>>
【20181209】C++求最大公约数
查看>>
为何程序员喜欢将INF设置为0x3f3f3f3f?
查看>>
ios::sync_with_stdio(false);
查看>>
20190120 OJ 回文序列的判断
查看>>
20190120 OJ递归
查看>>
leetcode notes:longest substring without repeating charactors
查看>>
蓝桥杯2016省赛 - A1网友年龄
查看>>