博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
5、OpenMP的变量私有化private、firstprivate、lastprivate
阅读量:4170 次
发布时间:2019-05-26

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

基本思想:OpenMP的变量私有化使用

(1)private子句将一个或多个变量变成私有化变量,每个线程都有独立的私有化同名变量副本,并不受外部同名变量的影响

它只可以用在迭代变量设置

#pragma omp parallel for private(i) for( i=0;i

或者

#pragma omp parallel  private(i) {       ..... }

测试代码

#include 
#include
#include
using namespace std;using namespace chrono;void sequentialProgram(int num){ int i=0; for( 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=12sequentialProgram elapse time: 0.0345098 secondsi=1 the current thread id: 1i=2 the current thread id: 2i=3 the current thread id: 3i=4 the current thread id: 4i=5 the current thread id: 5i=7 the current thread id: 7i=6 the current thread id: 6i=10 the current thread id: 10i=0 the current thread id: 0i=8 the current thread id: 8i=11 the current thread id: 11i=9 the current thread id: 9i=110i=100 the current thread id: 1i=100 the current thread id: 3i=100 the current thread id: 2i=100 the current thread id: 7i=100 the current thread id: 6i=100 the current thread id: 5i=100 the current thread id: 4i=100 the current thread id: 9i=100 the current thread id: 8i=100 the current thread id: 10i=100 the current thread id: 0i=100 the current thread id: 11i=110parallelProgram elapse time: 0.058988 secondsProcess finished with exit code 0

(2)firstprivate 作用和private很类似 只能用于非迭代变量

int sum=0;    int t=0;#pragma omp parallel for firstprivate(sum,t)    for(int i=0;i

测试代码

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

测试结果

F:\OpenMP\cmake-build-debug\OpenMP.exesum=0 the current thread id: 0sum=1 the current thread id: 0sum=3 the current thread id: 0sum=6 the current thread id: 0sum=10 the current thread id: 0sum=15 the current thread id: 0sum=21 the current thread id: 0sum=28 the current thread id: 0sum=36 the current thread id: 0sum=45 the current thread id: 0sum=55 the current thread id: 0sum=66 the current thread id: 0sum=66sequentialProgram elapse time: 0.0289404 secondssum=2 t=-2 the current thread id: 2sum=6 t=-6 the current thread id: 6sum=3 t=-3 the current thread id: 3sum=4 t=-4 the current thread id: 4sum=5 t=-5 the current thread id: 5sum=1 t=-1 the current thread id: 1sum=8 t=-8 the current thread id: 8sum=10 t=-10 the current thread id: 10sum=7 t=-7 the current thread id: 7sum=9 t=-9 the current thread id: 9sum=0 t=0 the current thread id: 0sum=11 t=-11 the current thread id: 11sum=1111 t=0sum=1211 the current thread id: 2sum=1211 the current thread id: 6sum=1211 the current thread id: 4sum=1211 the current thread id: 5sum=1211 the current thread id: 1sum=1211 the current thread id: 10sum=1211 the current thread id: 7sum=1211 the current thread id: 9sum=1211 the current thread id: 8sum=1211 the current thread id: 0sum=1211 the current thread id: 11sum=1211 the current thread id: 3sum=1111parallelProgram elapse time: 0.0741342 secondsProcess finished with exit code 0

(3)lastprivate  只能用在for循环的私有变量中,最终结果会发生变化

#pragma omp parallel for lastprivate(i)    for( i=0;i

测试代码

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

测试结果

F:\OpenMP\cmake-build-debug\OpenMP.exesum=0 the current thread id: 0sum=1 the current thread id: 0sum=3 the current thread id: 0sum=6 the current thread id: 0sum=10 the current thread id: 0sum=15 the current thread id: 0sum=21 the current thread id: 0sum=28 the current thread id: 0sum=36 the current thread id: 0sum=45 the current thread id: 0sum=55 the current thread id: 0sum=66 the current thread id: 0sum=66sequentialProgram elapse time: 0.028188 secondsi=1 sum=1 t=-1 the current thread id: 1i=3 sum=10 t=-10 the current thread id: 3i=4 sum=5 t=-5 the current thread id: 4i=2 sum=7 t=-7 the current thread id: 2i=5 sum=15 t=-15 the current thread id: 5i=6 sum=21 t=-21 the current thread id: 6i=7 sum=28 t=-28 the current thread id: 7i=8 sum=36 t=-36 the current thread id: 8i=10 sum=46 t=-46 the current thread id: 10i=9 sum=55 t=-55 the current thread id: 9i=0 sum=55 t=-55 the current thread id: 0i=11 sum=66 t=-66 the current thread id: 11i=12 sum=66 t=-66sum=166 the current thread id: 0sum=166parallelProgram elapse time: 0.0447262 secondsProcess finished with exit code 0

总结一下,

制导指令 for 非for 多参数 参数类型 是否修改同名变量 初始值
private 支持 支持 支持(不建议使用) 迭代参数 使用同名变量值,省略初始值
firstprivate 支持 支持 支持 不限 忽视掉同名变量值,使用初始值
lastprivate 支持 不支持 不支持 迭代参数 忽视掉同名变量值,使用初始值

三者可以结合使用~

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

测试结果

F:\OpenMP\cmake-build-debug\OpenMP.exesum=0 the current thread id: 0sum=1 the current thread id: 0sum=3 the current thread id: 0sum=6 the current thread id: 0sum=10 the current thread id: 0sum=15 the current thread id: 0sum=21 the current thread id: 0sum=28 the current thread id: 0sum=36 the current thread id: 0sum=45 the current thread id: 0sum=55 the current thread id: 0sum=66 the current thread id: 0sum=66sequentialProgram elapse time: 0.0221205 secondsi=1 sum=1 t=-1 the current thread id: 1i=3 sum=3 t=-4 the current thread id: 3i=5 sum=5 t=-9 the current thread id: 5i=0 sum=0 t=-9 the current thread id: 0i=4 sum=4 t=-21 the current thread id: 4i=8 sum=8 t=-17 the current thread id: 8i=2 sum=2 t=-29 the current thread id: 2i=6 sum=6 t=-27 the current thread id: 6i=10 sum=10 t=-39 the current thread id: 10i=11 sum=11 t=-50 the current thread id: 11i=9 sum=9 t=-59 the current thread id: 9i=7 sum=7 t=-66 the current thread id: 7i=10 sum=11 t=-66sum=111 the current thread id: 0sum=111parallelProgram elapse time: 0.0313515 secondsProcess finished with exit code 0

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

你可能感兴趣的文章
PostgreSQL数据库管理第十章Repmgr
查看>>
PostgreSQL数据库管理 第八章日常运维
查看>>
MySQL数据库管理-体系结构
查看>>
软考UML
查看>>
信息系统的生命周期各阶段及说明
查看>>
Ubuntulinux离线安装ClamTk杀毒软件步骤和使用方法
查看>>
摆脱贫穷2021V1
查看>>
Android.Could not find *.apk
查看>>
JNI
查看>>
Android基于TranslateAnimation的动画动态菜单
查看>>
android NDK中的GCC编译器
查看>>
Android NOtification 使用
查看>>
Android的SharedPreferences保存与删除数据简单实例
查看>>
android 如何从sqlite读取数据到spinner下拉中显示
查看>>
Android实现开机自动运行程序
查看>>
最近几天搭建MySql且连接问题总结
查看>>
搭建Tomcat
查看>>
在MyEclipse中运行tomcat出现Error initializing endpoint错误
查看>>
JSP文件中的上传功能(JSP中的相对路径)------JSP
查看>>
jsp中上传文件的源代码
查看>>