Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
29010 Discussions

Exiting an OMP DO parallel loop due to detection of error condition in one thread

OP1
New Contributor III
1,186 Views

IVF will not accept having a RETURN or EXITstatement inside a parallel DO OMP loop (it's not legal). I want to do the following:

!$OMP DO
... threads do some work in parallel
... if one thread detects an error condition (say, my algorithm does not converge fast enough), a signal should be sent to all threads to stop their calculations, and all should exit the loop.
!$OMP END DO

How can I achieve this?

Thanks!!

Olivier

0 Kudos
3 Replies
jimdempseyatthecove
Honored Contributor III
1,186 Views

Oliver,

use a shared variable

logical :: ExitLoop
...
ExitLoop = .false.
!$OMP DO
...
IF(SomeLogicalErrorExpression) then
ExitLoop = .true.
exit
ENDIF
...
!$OMP END DO

Caution, to not use

ExitLoop = SomeLogicalErrorExpression

as this will/may unset one threads exit condition.

Jim Dempsey
0 Kudos
OP1
New Contributor III
1,186 Views

Oliver,

use a shared variable

logical :: ExitLoop
...
ExitLoop = .false.
!$OMP DO
...
IF(SomeLogicalErrorExpression) then
ExitLoop = .true.
exit
ENDIF
...
!$OMP END DO

Caution, to not use

ExitLoop = SomeLogicalErrorExpression

as this will/may unset one threads exit condition.

Jim Dempsey

Jim,

You cannot have an EXIT statement inside a parallelized DO loop...
I am looking for a wayof signaling the threads to stop the (possibly) length calculations they're doing when one of them has encountered a condition that renders any further work useless.

Thanks,

Olivier
0 Kudos
jimdempseyatthecove
Honored Contributor III
1,186 Views

Sorry about that, then plan B

[cpp]    logical :: DoExit
    integer :: i

    print *, 'Begin'
    DoExit = .false.
!$omp parallel do
    do i=1,1000
        if(DoExit) cycle
        print *, i
        if(iand(i, 15) .eq. 0) DoExit = .true.
    end do
!$omp end parallel do
    print *, 'End'
------------------------
 Begin
           1
           2
           3
           4
           5
           6
           7
           8
           9
         501
          10
         502
          11
         503
          12
         504
          13
         505
          14
         506
          15
         507
          16
         508
 End
[/cpp]

I can see why GOTO out of the loop should be inhibited. EXIT should not be a problem excepting for potential BARRIER problems. The code above using CYCLE would have the same symptoms should barrier follow the CYCLE.

The BARRIER could be corrected (in implementation) where the EXIT implicitly decrements the number of threads in the team (seperated from the variable held for omp_get_num_threads()). Then the barrier can wait for the remaining number of threads.

Jim Dempsey
0 Kudos
Reply