Thank you @ivanpribec . Yeah the example I showed is not too good.
The real code contains a little bit MPI, and it is more like
subroutine A(...)
...
do i = 1, n
if ( rank ==0 ) then
! rank 0 cpu do the job.
! However at some point if rank 0 find error,
! it wants to inform all the cores to return
... long god damn code ...
if (detected some error) then
quit= .true.
goto 100
else
quit = .false.
endif
... long god damn code ...
endif
100 continue
call broadcast(quit)
if (quit) then return
! all core will wait here for rank 0 to broadcast if quit or not.
! if quit is true then all the cores return.
enddo
...
end subroutine A
-
It looks like in this case, if rank 0 detects some error, and it wants all the cores to return, I can think of the not-so-adorable
goto
statement is convenient. All the cores will run at100
, and wait for thebroadcast
, whenquit
is true, all the cores return subroutineA
.
Is there some other way can achieve this?
I mean, if rank 0 detects something wrong, if inform all the cores to stop or return or whatever. -
if I want to make
... long god damn code ...
if (detected some error) then
quit= .true.
goto 100
else
quit = .false.
endif
... long god damn code ...
into a sub subroutine inside subroutine A
, call it longcode
, like
subroutine A(...)
...
do i = 1, n
if ( rank ==0 ) then
! rank 0 cpu do the job.
! However at some point if rank 0 find error,
! it wants to inform all the cores to return
call longcode(...)
endif
100 continue
call broadcast(quit)
if (quit) then return
! all core will wait here for rank 0 to broadcast if quit or not.
! if quit is true then all the cores return.
enddo
...
contains
subroutine longcode(...)
! seems need to use alternative return,
! so that it return to 100 in the main subroutine.
end subroutine longcode
end subroutine A
It seems the alternative return
will be needed in sub subroutine longcode
.
Is there some more modern way that can let sub subroutine longcode
jump to some places in the main subroutine A
?
Sorry it is a little bit chaotic, lol.