multithreading - 如何用等待功能替换 OMP BARRIER?
问题描述
我想!$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
and omp_end
as 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_start
并且omp_end
由线程设置并由1
其他线程读取。正如您目前所拥有的那样,发生这种情况的顺序是未定义的,因此可以(并且可能会)在设置之前读取它们。
然而,还有一个更根本的问题。看起来您只是希望其他线程等待线程1
完成,但没有办法提前知道这需要多长时间。因此,没有办法实现这样的等待功能。这就是首先使用屏障的全部原因。
推荐阅读
- python - 根据不同的参数在Python pandas中计算多个月的字段
- kotlin - 无法使用 picocli 和 micronaut 连接数据库
- python - 如何查找某个范围内的所有像素值
- python - checkpoint.restore() 如何在 Tensorflow 中工作,注意神经机器翻译
- r - 是否有记录 CPU 使用率的 R 函数?
- javascript - 在滚动时反应工具提示触发 componentDidupdate
- flutter - 无法执行 HTTP 请求,没有与主机名关联的地址
- cppcheck - Cppcheck 停止检查未使用的StructMember
- php - 如何在不重新加载页面的情况下显示消息?
- database - 如何在 mongoDB 中查找和分组两个集合?