【问题标题】:Using global variables in a function called by threaded subroutines在线程子程序调用的函数中使用全局变量
【发布时间】:2014-03-01 05:45:34
【问题描述】:

我正在学习如何将我的 FORTRAN 源代码移植到 Openmp,并遇到了一个基本问题,即如何处理包含在调用子例程所在的并行区域之前定义的全局变量的线程子例程。

以下是我为该问题编写的示例代码。我将一系列从 1 到 1000000 的值乘以一个名为“a”的全局变量“p1”。乘法由一个并行执行,该子程序调用一个函数“afun”来执行逐元素乘法 a(i)*p1 并将相应的结果保存到相应的 b(i)。 “p1”的值是在并行区域之外分配的,因此是子程序“asub”及其称为函数“afun”的全局变量。

请问我需要哪些额外的 Openmp 配置才能成功运行程序?

program common_test
    use omp_lib
    implicit none
    integer :: a(100000),b(100000)
    integer :: p1,i
    common /p_com/ p1
    !$omp threadprivate(/p_com/)
    do i=1,100000
        a(i)=i
    enddo
    p1=2
    !$omp parallel do shared(a,b) private(i) copyin(p1)
    do i=1,100000
        call asub(afun,i,b(i))
    enddo
    !$omp end parallel do
contains
    subroutine asub(func,x,y)
        implicit none
        interface
           function func(x,p)
           implicit none
           integer :: x,p,func
           end function func    
        end interface
        integer, intent(in) :: x
        integer, intent(out) :: y
        integer :: p1
        common /p_com/ p1
        y=func(x,p1)    
    end subroutine asub

    function afun(x,p)
        implicit none
        integer, intent(in) :: x
        integer :: p,afun
        afun=x*p
    end function afun
end program common_test

【问题讨论】:

    标签: multithreading parallel-processing openmp fortran90


    【解决方案1】:

    我终于在没有收到英特尔编译器投诉的情况下运行了示例代码。我所做的更正是在主程序中第二个 Openmp 指令的行尾将 copyin(p1) 替换为 copyin(/p_com/)。也就是说,

    program common_test
        use omp_lib
        implicit none
        integer :: a(100000),b(100000)
        integer :: p1,i
        common /p_com/ p1
    
        !$omp threadprivate(/p_com/)
        do i=1,100000
            a(i)=i
        enddo
    
        p1=2
        !$omp parallel do shared(a,b) private(i) copyin(/p_com/)
        do i=1,100000
            call asub(afun,i,b(i))
        enddo
        !$omp end parallel do
    contains
        subroutine asub(func,x,y)
            implicit none
            interface
            function func(x,p)
            implicit none
            integer :: x,p,func
            end function func    
            end interface
            integer, intent(in) :: x
            integer, intent(out) :: y
            integer :: p1
            common /p_com/ p1
            y=func(x,p1)    
        end subroutine asub
    
        function afun(x,p)
            implicit none
            integer, intent(in) :: x
            integer :: p,afun
            afun=x*p
        end function afun
    end program common_test
    

    【讨论】:

      猜你喜欢
      • 2013-11-16
      • 1970-01-01
      • 1970-01-01
      • 2013-12-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-17
      相关资源
      最近更新 更多