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

Fortran and Coroutines

Valued Contributor III

A coroutine is a function that can suspend execution to be resumed later. Coroutines are stackless: they suspend execution by returning to the caller and the data that is required to resume execution is stored separately from the stack. This allows for sequential code that executes asynchronously (e.g. to handle non-blocking I/O without explicit callbacks), and also supports algorithms on lazy-computed infinite sequences and other uses.



Any chance we will see coroutines in Fortran

Labels (1)
0 Kudos
3 Replies
Honored Contributor III

You can (somewhat) accomplish this with DO WHILE(.not.done) and select case with a state variable where the Done flag and state variable are SAVE.

In a design similar to OpenMP, you would have a master procedure containing the DO WHILE loop and cooperative select case proceture(s) that progress step-by-step.

An alternative method, would be to construct a ring buffer of procedure pointers where the "master" procedure is a simple loop that calls the next procedure pointer in the ring buffer.

The first method would permit you to split a single procedure into multiple parts, whereas the second method requires each fragment to be its own procedure. (collectively the fragments of a "collective"-procedure would use the same module for save states/common variables of the "collective"-procedure.

Jim Dempsey

0 Kudos
Honored Contributor III

The topic has been raised among the Fortran committee - it did not get a warm reception. Fortran already supports asynchronous I/O. 

Fortran has two parallel models, coarrays and DO CONCURRENT. There are existing mechanisms in the language to handle most of what coroutines do.

0 Kudos