Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between STOP and ERROR STOP?

Tags:

fortran

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.

like image 721
jvriesem Avatar asked Mar 28 '18 21:03

jvriesem


3 Answers

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.

like image 125
francescalus Avatar answered Nov 01 '22 03:11

francescalus


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).

like image 7
Vladimir F Героям слава Avatar answered Nov 01 '22 02:11

Vladimir F Героям слава


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.

like image 4
Scientist Avatar answered Nov 01 '22 02:11

Scientist