Search code examples
fortran

What is the difference between STOP and ERROR STOP?


Fortran has stop and error stop, both of which exit the program prematurely and can return an error code.

What is the difference between the two, and when should either be used? (Also, are there implementation/compiler concerns here?)

I understand from Intel's documentation that stop terminates the program, whereas error stop appears to do a little more. That page is useful but does not entirely answer my question, because 1) it's a little too technical for me, and 2) I know that the Fortran standard(s) leave room for "artistic expression" among implementations, so I don't trust that page to perfectly reflect the standard.

I note that this question is related to What is the difference between "stop" and "exit" in Fortran? — in fact, my question was inspired by this answer to that question. Whereas that question asks the difference between stop and exit, I'm specifically looking at the nitty-gritty differences between stop and error stop. My question here would have been a useful addition to that question in retrospect, but since it wasn't, this is a separate question.


Solution

  • 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, a stop statement is an image control statement; an error stop statement is not. Image control statements affect ordering of images, and are restricted in where they appear. For example, stop statements may not appear in a pure subprogram1 or in a do concurrent block.


    1 Whatever one thinks of the use of stop and error stop inside functions, it's fair to say that stop in particular should generally be avoided completely inside functions.