我有一组severak for-loops,其中只有在满足某个条件时才会执行。
如何确保只有一个线程(可能使用SINGLE
)执行if语句,但所有线程都可用于DO
。
!$omp parallel
!$omp do
do i=0,512
something to do
end do
!$omp end do nowait
if (condition_var) then
!$omp do
do i=0,512
only do sometimes
end do
!$omp end do
fi
!$omp end parallel
答案 0 :(得分:2)
只要条件在团队中的线程中提供相同的结果,您就不必确保只有一个线程评估if条件。更明确一点:
!$omp parallel
!$omp do
do i=0,512
something to do
end do
!$omp end do
! Synchronize here to ensure shared variables
! will not be changed while evaluating the if condition
if (condition_var) then
! All threads evaluate the if condition and either enter
! the block or skip it
!$omp do
do i=0,512
only do sometimes
end do
!$omp end do
fi
!$omp end parallel