[MPI3 Fortran] Fortran extra_state argument to MPI attributefunctions

Jeff Squyres jsquyres at cisco.com
Wed May 27 14:54:40 CDT 2009

On May 26, 2009, at 4:43 PM, Aleksandar Donev wrote:

> > However, the original intent is somewhat lost here -- since the
> > MPI_COMM_CREATE_KEYVAL interface specifies that the EXTRA_STATE
> > parameter is an integer, how can you pass a reference to a larger /
> > external structure?  This seems somewhat broken / inconsistent with
> > the intent from the C interface.
> Yes, the interface that specifies "INTEGER" is broken! Integers are  
> not
> handles and should not be used as such.

All I know is the intent -- I don't know why they chose to implement  
it that way (in 1993/1994).  Perhaps F90 features were not widely  
available then...?  [shrug]

> The recommended way, if you are willing to use a Fortran 2003, is to
> make the extra parameters exactly what they are in C, a void pointer:
> USE ISO_C_BINDING ! F03 intrinsic module
> TYPE(C_PTR), VALUE :: extra_state

Is this effectively a neutral type -- such that a Fortran app can pass  
a reference/pointer/whatever to some larger structure through this  
type in the call to MPI_COMM_CREATE_KEYVAL and then "cast" it back to  
the right type in the callback function?

> If using F03 is not acceptable, you could also make it an opaque type,
> to be defined in the MPI3 module:
> TYPE(ExtraState), INTENT(IN) :: extra_state
> The implementation would be *required* to make a copy of the value so
> that changes to the original actual are not visible in subsequent
> callbacks. As long as the callbacks themselves also have an  
> they cannot modify the value of the copy, so whether you use 2a or 2b
> cannot be seen by the user.

Agreed -- if we use INTENT(in), the difference between 2a and 2b is  
irrelevant.  Making a copy of a small value (4 or 8 bytes) to inside  
MPI is no big deal.  Having to copy a value of potentially arbitrary  
size would not be desirable.

> You can also do the interface:
> TYPE(ExtraState), VALUE :: extra_state
> but then you cannot use OPTIONAL, and also this will create two  
> copies,
> one upon calling MPI, and then your implementation will again have to
> make a copy of the copy (since the first copy is on the stack and will
> disappear).

That's also ok -- if we're only copy 4 or 8 bytes (i.e., a pointer),  
no big deal.

> > How are such things normally done in Fortran?  (meaning: what
> > *should*   we do here?)
> In Fortran 2003, the "right way" (meaning what I would teach my
> students) to do that is as follows:
> TYPE, ABSTRACT :: ExtraState
> Dummy argument:
> CLASS(ExtraState), INTENT(IN) :: extra_state
> but this is probably not where you want to go since few compilers can
> correctly handle object-oriented features yet....and it is not in the
> spirit of the C interface.

Probably not.

Jeff Squyres
Cisco Systems

More information about the mpiwg-fortran mailing list