What is the difference between STOP and ERROR STOP?
Adding to @Vladimir F's great answer, the statement
error stop [ stop-code ] ! stop-code is an integer or default character constant expression and has the same meaning as for the stop statement.
has more severe consequences in parallel programs than a simple stop
. When executed on one coarray image (processor, thread,...), it initiates error termination there and hence causes all other images that have not already initiated error termination to initiate error termination. It causes the whole calculation to stop as soon as is possible. Therefore, error stop
should be used in parallel programming when a severe error in one of the images makes the entire simulation useless for the rest of images.
To the contrary, a single stop
statement stops only those images that have reached it, as far as I am aware and used it in Coarray Fortran. Other images can check the status of a specific image or images that have failed (stopped) using
sync images([image numbers],stat=syncstat)
if (syncstat==stat_stopped_image) then
! do something appropriate
end if
The flag stat_stopped_image
is a constant that is available from iso_fortran_env
intrinsic module in Fortran 2008 onward.
Error stop
is (obviously?) meant to be used in case of error termination of the program while stop
is used terminate the program even in normal circumstances. Some even used to write stop
before each end
.
error stop
was introduced in Fortran 2008 together with coarrays and is useful for stopping all running images (processes) while stop
just stops execution of the current image.
stop
cannot be used in pure
procedures while error stop
can (new feature of Fortran 2018).
Fortran has two types of termination of execution: normal termination and error termination.
Normal termination commences when a stop
statement or an end program
statement is reached. Error termination comes about with an error stop
statement or other forms of errors (such as with in input/output, allocation, or other such forms).
So, in the simplest manner a stop
statement initiates normal termination and an error stop
statement initiates error termination.
As noted, there is indeed more to "termination".
For normal termination (whether through a stop
or not) there are three phases: initiation, synchronization, and completion. In a single-image (non-coarray) program these things flow from one to the other. With more than one image, images synchronize after initiation until all have commenced normal termination. After synchronization the termination process undergoes completion. There is no "cascading" of normal termination.
For error termination, once one image initiates this all other images which haven't already started terminating undergo (error) termination "as quickly as possible". That is, the entire program comes to a crashing halt once one image kicks it off.
These aspects are not "processor-dependent", to the extent that if a compiler is a Fortran compiler it follows these things. However, not all (versions of all) compilers are full Fortran compilers of the current standard. In particular, some compilers do not support error stop
. The distinction of the two forms is less important without coarray implementation.
There is (at least) one aspect where compilers are allowed to vary in detail. If no integer stop code is given, on stop
the standard recommends a zero value be passed as the exit code of the process (where such a concept is supported). If no integer stop code is given on error stop
, then Fortran 2018 recommends a non-zero value be passed as the exit code.
From the Intel documentation linked, ifort follows this first recommendation.
Finally, an error stop
statement may appear in a pure subprogram whereas a stop
statement (being an image control statement) is prohibited.