Fortran Coder

标题: OpenMP中运行后数值问题 [打印本页]

作者: 978142355    时间: 2018-8-6 19:43
标题: OpenMP中运行后数值问题
练习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

作者: 978142355    时间: 2018-8-7 14:34
自评一下:脑子抽筋了,那里的符号是2**tid,当成了2*tid。当tid=0,2**tid结果是1,a传入的是0,所以最终值肯定是1。。。。。。。。。。。。。。。。。。




欢迎光临 Fortran Coder (http://bbs.fcode.cn/) Powered by Discuz! X3.2