While I was learning the usage of Forpy (a very nice library to call Python directly from Fortran!), I found that the user is advised to attach the asynchronous
attribute when making a Numpy ndarray
wrapper to an existing Fortran array. For example, a code sample contains this line:
real, asynchronous :: matrix(NROWS, NCOLS)
where an ndarray
wrapper is created for matrix
. The manual page says:
Since the Fortran array is used as underlying buffer for the ndarray, it can be indirectly modified by changing the ndarray. To avoid bugs related to certain compiler optimizations, declare the Fortran array with the ‘asynchronous’ attribute.
Because I had never used the asynchronous
attribute before, I checked Modern Fortran Explained (Sec.17.4, see the bottom of this question), but I could not understand the description very well…
So I am wondering:
- Is this attribute possibly designed for some parallel calculations in mind
(based on the attribute name)? - Or, was it introduced as a hint to suppress some compiler optimization or even bugs?
- Most importantly, when are we users expected to use this keyword in actual programming? Is it used mainly for interactions with foreign languages (as in Forpy)?
I would really appreciate any advice / comments, and thanks in advance!
PS. The above question is a crosspost to comp.lang.fortran. Because the community could be somewhat different, I thought it would be useful to post the same question for both. But if this is not very good, please let me know so that I can delete one of them.
Below is some excerpts from MFE, Sec.17.4:
The asynchronous attribute for a variable has been introduced to warn the compiler that optimizations involving movement of code across wait statements (or other statements that cause wait operations) might lead to incorrect results. If a variable appears in an executable statement or a specification expression in a scoping unit and any statement of the scoping unit is executed while the variable is an affector, it must have the asynchronous attribute in the scoping unit.
There are restrictions that avoid any copying of an actual argument when the corresponding dummy argument has the asynchronous attribute: the actual argument must not be an array section with a vector subscript; if the actual argument is an array section or an assumed-shape array, the dummy argument must be an assumed-shape array; and if the actual argument is a pointer array, the dummy argument must be an assumed-shape or pointer array.