博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
6、OpenMP归约操作reduction、shared、simd
阅读量:4170 次
发布时间:2019-05-26

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

基本思想:OpenMP进行加减乘除运算的规约处理 reduction 可以支持的运算符号 +、-、*、/、&&、|、^、|| 符号

#pragma omp parallel for reduction(+:sum)    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"<

测试结果i 可以看出 在增加归约之后,结果和串行程序保持一致,随着叠加次数增加,其时间明显比串行时间的少

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.0376821 secondsi=1 sum=1 the current thread id: 1i=8 sum=8 the current thread id: 8i=9 sum=9 the current thread id: 9i=4 sum=4 the current thread id: 4i=2 sum=2 the current thread id: 2i=7 sum=7 the current thread id: 7i=10 sum=10 the current thread id: 10i=6 sum=6 the current thread id: 6i=5 sum=5 the current thread id: 5i=0 sum=0 the current thread id: 0i=3 sum=3 the current thread id: 3i=11 sum=11 the current thread id: 11 sum=66parallelProgram elapse time: 0.0163177 secondsProcess finished with exit code 0

同样的逻辑代码

shared 对一个变量的同一个内存进行读写

#pragma omp parallel for shared(sum)    for(int i=0;i

simd 编译器可以忽略矢量依赖关系,使循环尽可能实现矢量友好,并尊重用户对同时执行多个循环迭代的意图。

#pragma omp simdfor(int i=0;i

测试代码

#include 
#include
#include
using namespace std;using namespace chrono;static omp_lock_t m_lock;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=66sequentialProgram elapse time: 0.0011415 seconds sum=66 sum=66 sum=66 sum=66 sum=66 sum=66parallelProgram elapse time: 0.0028554 secondsProcess finished with exit code 0

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

你可能感兴趣的文章
What is the difference between a pack slip and a pick slip?
查看>>
CSS基础
查看>>
How to Reset or Change Microsoft Office 2007 Product License Key or Volume License Key (VLK)
查看>>
使用java concurrent调用xmlp api生成pdf
查看>>
这个sql咋写?
查看>>
SQL to inspect form personalization
查看>>
A Simple Example of Weak Ref Cursor
查看>>
trap or bug when using CONTINUE in Oracle 11g
查看>>
Oracle之AUTHID CURRENT_USER
查看>>
Oracle之NULL IS NULL
查看>>
Oracle日期计算之INTERVAL
查看>>
Oracle PL/SQL之EXCEPTION
查看>>
Oracle PL/SQL之EXCEPTION -- WHEN OTHERS THEN
查看>>
Oracle PL/SQL之VARCHAR2 QUALIFIER
查看>>
Oracle PL/SQL之处理index不连续的table类型变量
查看>>
Oracle PL/SQL之嵌套表(Nested Table)
查看>>
Oracle PL/SQL之令人不解的提示(nls_date_format)
查看>>
Oracle PL/SQL之GROUP BY ROLLUP
查看>>
Oracle PL/SQL之GROUP BY CUBE
查看>>
Oracle PL/SQL之GROUPING 函数
查看>>