【发布时间】: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