【发布时间】:2021-09-24 07:13:01
【问题描述】:
我有一个名为 array 的 4 个任务数组。声明如下:
type(tcb),dimension(4)::arrray
与:
type:: tcb !> new type task control block
procedure(my_interface),NOPASS,pointer:: f_ptr => null() !< the function pointer
type(variables)::variables !< the variables
integer :: state !< the task state
end type tcb
我只有 2 个线程来执行这 4 个任务。我想避免使用!$OMP TASK。
当我使用任务构造时,我得到了这样的结果:
type(tcb),dimension(4),intent(inout)::array !< the array of tasks
integer,intent(in)::ff !< the counter
type(tcb)::self !< self
type(variables),intent(inout)::var !< the variables
!OpenMP variables
integer::num_thread !< the rank of the thread
integer::nthreads !< the number of threads
integer:: OMP_GET_THREAD_NUM !< function to get the rank of the thread
integer::OMP_GET_NUM_THREADS !< function to get the number of threads
!=======================================================================================================================================================
!$OMP PARALLEL PRIVATE(num_thread,nthreads,ff) &
!$OMP SHARED(array)
num_thread=OMP_GET_THREAD_NUM() !< le rang du thread
nthreads=OMP_GET_NUM_THREADS() !< le nombre de threads
!$OMP MASTER
do ff=1,3
!$OMP TASK SHARED(array) IF ((num_thread .ne. 0) .and. (num_thread .ne. 1))
call array(ff)%f_ptr(self,var)
!$OMP END TASK
end if
end do
!$OMP TASKWAIT
!$OMP END MASTER
你有什么想法吗?我希望当线程完成运行任务时,它直接移动到下一个可用任务。它不应该等待另一个线程完成。 不使用 OMP Tasking 怎么办? 我想单独安排任务,而不是借助 OpenMP。有可能吗?
【问题讨论】:
-
您想要的正是 OpenMP 任务的工作方式。一旦一个线程完成了一项任务,它将寻找下一个准备好执行的可用任务。我不明白你为什么不想使用 OpenMP 任务。您能否更具体地说明“等待另一个线程完成”的含义?在您的代码中发生这种情况的唯一方法是所有线程都必须等到所有线程在并行区域结束时完成。但这是你无法避免的。
标签: multithreading fortran task openmp threadpool