Fortran Coder

查看: 6005|回复: 1
打印 上一主题 下一主题

[并行] OpenMP中运行后数值问题

[复制链接]

40

帖子

11

主题

0

精华

大宗师

F 币
4119 元
贡献
1857 点

规矩勋章

QQ
跳转到指定楼层
楼主
发表于 2018-8-6 19:43:17 | 只看该作者 |只看大图 回帖奖励 |倒序浏览 |阅读模式
练习OpenMP在Fortran中的应用,主要是代码中Single那一部分,传入的a值应该是0,为什么运行后a值变成1了?谢谢各位。
[Fortran] 纯文本查看 复制代码
    program test17 
    use omp_lib
    implicit none
    integer tid,cpus
    integer a

    call omp_set_num_threads(3)

    !$omp parallel private(a,tid)
    tid=omp_get_thread_num()
    a=tid
    write(*,*) 'a=',a,'tid=',tid,' before single'
    !$omp single
    write(*,*)
    cpus=omp_get_num_threads()
    tid=omp_get_thread_num()

    a=a+2**tid

    write(*,*) 'single threads:', cpus
    write(*,*) 'a=',a,'tid=',tid,' during single'
    write(*,*)
    !$omp end single copyprivate(a)
    tid=omp_get_thread_num()
    write(*,*) 'a=',a,'tid=',tid,'after single'
    !$omp end parallel
    read(*,*)
    end program test17

1.png (43 KB, 下载次数: 184)

1.png
分享到:  微信微信
收藏收藏 点赞点赞 点踩点踩
where is dream,where is home

40

帖子

11

主题

0

精华

大宗师

F 币
4119 元
贡献
1857 点

规矩勋章

QQ
沙发
 楼主| 发表于 2018-8-7 14:34:26 | 只看该作者
自评一下:脑子抽筋了,那里的符号是2**tid,当成了2*tid。当tid=0,2**tid结果是1,a传入的是0,所以最终值肯定是1。。。。。。。。。。。。。。。。。。
where is dream,where is home
您需要登录后才可以回帖 登录 | 极速注册

本版积分规则

捐赠本站|Archiver|关于我们 About Us|小黑屋|Fcode ( 京ICP备18005632-2号 )

GMT+8, 2024-4-26 00:22

Powered by Tencent X3.4

© 2013-2024 Tencent

快速回复 返回顶部 返回列表