Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/go/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Fortran OpenMP并行函数调用_Fortran_Openmp - Fatal编程技术网

Fortran OpenMP并行函数调用

Fortran OpenMP并行函数调用,fortran,openmp,Fortran,Openmp,我有一些Fortran代码,是为科学目的编写的。有两个条件函数调用是按顺序测试和运行的。对这些函数的调用我想并行化,因为它们彼此不相关。然而,我不确定最好的方法是什么 我拥有的代码示例如下: ... if ( flag1 ) then stat1 = func1( tm, dt, struct % a, struct % b ) if ( stat1 .gt. 0 ) then call die_error("error message") status = 1

我有一些Fortran代码,是为科学目的编写的。有两个条件函数调用是按顺序测试和运行的。对这些函数的调用我想并行化,因为它们彼此不相关。然而,我不确定最好的方法是什么

我拥有的代码示例如下:

...

if ( flag1 ) then
  stat1 = func1( tm, dt, struct % a, struct % b )
  if ( stat1 .gt. 0 ) then
    call die_error("error message")
    status = 1
    return
  end if
end if

if ( flag2 ) then
  stat2 = func2( tm, dt, struct % a, struct % c )
  if ( stat2 .gt. 0 ) then
    call die_error("error message 2")
    status = 1
    return
  end if
end if

...

...

!$omp parallel num_threads(2) firstprivate(tm, dt, struct % a) if(flag1 .and. flag2)
  !$omp master
    !$omp task lastprivate(stat1)
      stat1 = func1( tm, dt, struct % a, struct % b )
    !$omp end task

    !$omp task lastprivate(stat2)
      stat2 = func2( tm, dt, struct % a, struct % c )
    !$omp end task

    !$omp taskwait

    if ( (stat1 .gt. 0) .or. (stat2 .gt. 0) ) then
      call die_error("error message")
      status = 1
      return
    end if
  !$omp end master
!$omp end parallel

if ( flag1 .and. .not.flag2 ) then
  stat1 = func1( tm, dt, struct % a, struct % b )
  if ( stat1 .gt. 0 ) then
    call die_error("error message")
    status = 1
    return
  end if
end if

if ( flag2 .and. .not.flag1 ) then
  stat2 = func2( tm, dt, struct % a, struct % c )
  if ( stat2 .gt. 0 ) then
    call die_error("error message 2")
    status = 1
    return
  end if
end if

...
标志
flag1
flag2
是用户定义的,其中一个可能为真,或者两个都为真,或者两个都为假,这就是独立测试它们的原因

函数参数
tm
dt
分别是整数和双精度变量,但它们不会被
func1
func2
更改

自定义数据类型
struct
包含三个双精度数组:
a
b
c
。数组
结构%a
不会被
func1
func2
更改。但是,
struct%b
func1
更改,
struct%c
func2
更改

在特定情况下,当
flag1
flag2
均为true时,可以并行调用
func1
func2
。但是,我不确定以下几点:

  • 如何正确处理自定义数据类型;例如,我需要锁吗
  • 如何正确处理
    结构%a
    ,因为它不会被代码更改
    firstprivate(结构%a)
    例如
  • 如果
    flag1
    flag2
    都为真,如何正确实现我只希望并行化的条件
  • 我的尝试如下:

    ...
    
    if ( flag1 ) then
      stat1 = func1( tm, dt, struct % a, struct % b )
      if ( stat1 .gt. 0 ) then
        call die_error("error message")
        status = 1
        return
      end if
    end if
    
    if ( flag2 ) then
      stat2 = func2( tm, dt, struct % a, struct % c )
      if ( stat2 .gt. 0 ) then
        call die_error("error message 2")
        status = 1
        return
      end if
    end if
    
    ...
    
    
    ...
    
    !$omp parallel num_threads(2) firstprivate(tm, dt, struct % a) if(flag1 .and. flag2)
      !$omp master
        !$omp task lastprivate(stat1)
          stat1 = func1( tm, dt, struct % a, struct % b )
        !$omp end task
    
        !$omp task lastprivate(stat2)
          stat2 = func2( tm, dt, struct % a, struct % c )
        !$omp end task
    
        !$omp taskwait
    
        if ( (stat1 .gt. 0) .or. (stat2 .gt. 0) ) then
          call die_error("error message")
          status = 1
          return
        end if
      !$omp end master
    !$omp end parallel
    
    if ( flag1 .and. .not.flag2 ) then
      stat1 = func1( tm, dt, struct % a, struct % b )
      if ( stat1 .gt. 0 ) then
        call die_error("error message")
        status = 1
        return
      end if
    end if
    
    if ( flag2 .and. .not.flag1 ) then
      stat2 = func2( tm, dt, struct % a, struct % c )
      if ( stat2 .gt. 0 ) then
        call die_error("error message 2")
        status = 1
        return
      end if
    end if
    
    ...
    
    我在上面还有几个问题:

  • $是否需要omp主指令
    指令
  • 应该
    $不能使用omp节
    指令而不是
    $omp任务
  • 是我从
    返回的$如果
    func1
    func2
    失败,omp并行指令是否安全