[MPI3 Fortran] Fortran extra_state argument to MPI attribute functions

Aleksandar Donev donev1 at llnl.gov
Tue May 26 15:43:23 CDT 2009

On Tuesday 26 May 2009 11:52, Jeff Squyres 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.

> Of course, you *could* pass an INTEGER EXTRA_STATE value that is an  
> index into a global array in the application that contains some  
> reference to an external data instance -- but that seems fairly  
> manual.  Is there a better way?
Several ways.

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

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 INTENT(IN), 
they cannot modify the value of the copy, so whether you use 2a or 2b 
cannot be seen by the user.

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 

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


Aleksandar Donev, Ph.D.
Lawrence Postdoctoral Fellow @ Lawrence Livermore National Laboratory
High Performance Computational Materials Science and Chemistry
E-mail: donev1 at llnl.gov
Phone: (925) 424-6816  Fax: (925) 423-0785
Address: P.O.Box 808, L-367, Livermore, CA 94551-9900
Web: http://cims.nyu.edu/~donev/

More information about the mpiwg-fortran mailing list