Community
cancel
Showing results for 
Search instead for 
Did you mean: 
Highlighted
New Contributor II
88 Views

Fortran and Coroutines

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
Highlighted
81 Views

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
Highlighted
Black Belt Retired Employee
78 Views

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.

--
Steve (aka "Doctor Fortran") - https://stevelionel.com/drfortran
0 Kudos
Highlighted
New Contributor II
68 Views

0 Kudos