【发布时间】:2021-10-31 19:04:20
【问题描述】:
我想停用以下代码中的!$OMP BARRIER,所以我想用wait 函数替换它。
与!$OMP BARRIER:
if (num_thread==1) then
do i_task=first_task,last_task
tasklist_GRAD(i_task)%state=STATE_READY
call queue_enqueue_data(master_queue,tasklist_GRAD(i_task)) !< add the list elements to the queue (full queue)
end do
end if
!$OMP BARRIER ! barrier to retire
call master_worker_execution(self,var,master_queue,worker_queue,first_task,last_task,nthreads,num_thread,lck)
没有!$OMP BARRIER:
if (num_thread==1) then
omp_start=omp_get_wtime() !start
do i_task=first_task,last_task
tasklist_GRAD(i_task)%state=STATE_READY
call queue_enqueue_data(master_queue,tasklist_GRAD(i_task)) !< add the list elements to the queue (full queue)
end do
omp_end=omp_get_wtime() !end
end if
if (num_thread .ne. 1) then
call wait(int(omp_end-omp_start)*1000)
end if
call master_worker_execution(self,var,master_queue,worker_queue,first_task,last_task,nthreads,num_thread,lck)
wait子程序的定义:
subroutine wait(omp_start,omp_end)
real(kind=REAL64),intent(in)::omp_start,omp_end
real(kind=REAL64)::time
time=omp_end-omp_start
call sleep(int(time))
end subroutine wait
屏障应该让线程(不是线程号 1)等待线程号 1 完成对 master_queue 的排队。这就是为什么我想用 wait 函数替换它。
执行时,由于线程安全(我猜),我得到一个段错误。我对使用INT 函数有疑问,因为我将omp_start 和omp_end 声明为real(kind=REAL64)。
编辑:
我根据得到的答案修改了wait 子程序并做了以下操作:
subroutine wait(master_queue)
type(QUEUE_STRUCT),pointer::master_queue !< the master queue of tasks
do while (.not. queue_full(master_queue))
call sleep(1)
end do
end subroutine wait
很遗憾,我没有像 OMP_BARRIER 那样得到结果。
logical function queue_full( queue )
type(QUEUE_STRUCT), intent(in) :: queue
queue_full = (queue%size == queue%capacity)
end function queue_full
【问题讨论】:
-
了解您为什么不想使用
OMP_BARRIER会有所帮助。 -
@veryreverie 我正试图摆脱
OMP_BARRIER,因为我必须从单节点切换到多节点。OMP_BARRIER在您有多个节点时不起作用,所以我正在尝试准备代码,以便它也可以在多节点中工作。我通过从头开始实现OMP_TASK摆脱了它,但我仍然有OMP_BARRIERs 需要替换。还有其他方法吗? -
啊,在这种情况下,您需要将
OMP_BARRIER替换为您正在使用的任何多节点框架(例如 MPI)中的等效项。 -
看来您遇到了新问题。反正不知道
queue_full是什么,没人能诊断出来。 -
它告诉变量可以被程序的其他部分以异步方式修改 - 例如由其他线程。对于
queue_full = (queue%size == queue%capacity)行,您可能还需要一些原子或关键指令。
标签: multithreading fortran openmp gfortran barrier