Attributes are attached to communicators.
Attributes are local to the process and specific to the communicator
to which they are attached. Attributes are not propagated by MPI from one
communicator to another except when the communicator is duplicated
using MPI_COMM_DUP (and even then the application must give
specific permission through
callback functions for the attribute to be copied).

[] Advice
to implementors.

Attributes are scalar values, equal in size to, or larger than a C-language
pointer. Attributes can always hold an MPI handle.
( End of advice to implementors.)
The caching interface defined here represents that attributes be
stored by MPI opaquely within a communicator.
Accessor functions include the following:

obtain a key value (used to identify an attribute); the user
specifies ``callback'' functions by which MPI informs the application
when the communicator is destroyed or
copied.

store and retrieve the value of an attribute;

[] Advice
to implementors.

Caching and callback functions are only called synchronously,
in response to explicit application requests. This avoid problems
that result from repeated crossings between user and system space.
(This synchronous calling rule is a general property of MPI.)

The choice of key values is under control of MPI. This allows MPI to optimize
its implementation of attribute sets. It also avoids conflict between
independent modules caching information on the same communicators.

A much smaller interface, consisting of just a callback facility, would allow
the entire caching facility to be implemented by portable code. However, with
the minimal callback interface, some form of table searching is implied by the
need to handle arbitrary communicators. In contrast, the more complete
interface defined here permits rapid access to attributes through the use of
pointers in communicators (to find the attribute table) and cleverly chosen
key values (to retrieve individual attributes). In light of the efficiency
``hit'' inherent in the minimal interface, the more complete interface defined
here is seen to be superior.
( End of advice to implementors.)
MPI provides the following services related to caching. They are
all process local.

Generates a new attribute key. Keys are locally unique in a process,
and opaque to user, though they are explicitly stored in integers.
Once allocated, the key value can be used to associate attributes
and access them on any locally defined communicator.

The copy_fn function is invoked when a communicator is
duplicated by MPI_COMM_DUP. copy_fn should be
of type MPI_Copy_function, which is defined as follows:

The copy callback function is invoked for each key value in
oldcomm in arbitrary order. Each call
to the copy callback is made with a key value and its corresponding attribute.
If it returns flag = 0, then the
attribute is deleted in the duplicated communicator. Otherwise
( flag = 1), the new attribute value is set via
attribute_val_out. The function returns MPI_SUCCESS on
success and an error code on failure (in which case
MPI_COMM_DUP will fail).

copy_fn may be specified as
MPI_NULL_FN from either C or FORTRAN, in which case
no copy callback occurs for keyval; MPI_NULL_FN
is a function that does nothing other than returning flag = 0.
In C, the NULL function pointer has the same behavior as using
MPI_NULL_FN.
As a further convenience, MPI_DUP_FN is a simple-minded
copy callback available from C and FORTRAN; it sets flag = 1,
and returns the value of
attribute_val_in in attribute_val_out.

Note that the C version of this MPI_COMM_DUP assumes that the
callback functions follow the C prototype, while the corresponding
FORTRAN version assumes the FORTRAN prototype.

[] Advice to users.

A valid copy function is one that completely duplicates the
information by making a full duplicate copy of the data structures
implied by an attribute; another might just make another reference to
that data structure, while using a reference-count mechanism. Other
types of attributes might not copy at all (they might be specific to
oldcomm only).
( End of advice to users.)
Analogous to copy_fn is a callback deletion function, defined
as follows. The delete_fn function is invoked when a communicator is
deleted by MPI_COMM_FREE or when a call is made explicitly
to MPI_ATTR_DELETE. delete_fn should be
of type MPI_Delete_function, which is defined as follows:

This function is called by MPI_COMM_FREE and
MPI_ATTR_DELETE to do whatever is needed to remove an attribute.
It may be specified as the null function pointer in C or as
MPI_NULL_FN from either C or FORTRAN, in which case no delete
callback occurs for keyval.

The special key value MPI_KEYVAL_INVALID is never returned
by MPI_KEYVAL_CREATE. Therefore, it can be used for
static initialization of key values.

Frees an extant attribute key.
This function sets the value of keyval to
MPI_KEYVAL_INVALID.
Note that it is not erroneous to free an attribute key
that is in use, because the actual free does not transpire until after all
references (in other communicators on the process) to the key have been freed.
These references need to be explictly freed by the program, either via calls
to MPI_ATTR_DELETE that free one attribute instance, or by calls
to MPI_COMM_FREE that free all attribute instances associated with
the freed communicator.

[] Advice
to implementors.
The function MPI_NULL_FN need not be
aliased to (void (*))0 in C, though this is fine.
It could be a legitimately callable function that profiles and so on.
For FORTRAN, it is most convenient to have MPI_NULL_FN
be a legitimate do-nothing function call. ( End of advice to implementors.)
MPI_ATTR_PUT(comm, keyval, attribute_val)
[ IN comm] communicator to which attribute will be attached (handle)
[ IN keyval] key value, as returned by MPI_KEYVAL_CREATE (integer)
[ IN attribute_val] attribute value

This function stores the stipulated attribute value attribute_val
for subsequent retrieval by MPI_ATTR_GET.
If the value is already present, then the outcome
is as if MPI_ATTR_DELETE
was first called to delete the previous
value (and the callback function delete_fn was executed), and a new
value was next stored. The call is erroneous if there is no key with value
keyval; in particular
MPI_KEYVAL_INVALID is an erroneous key value.

MPI_ATTR_GET(comm, keyval, attribute_val, flag)
[ IN comm] communicator to which attribute is attached (handle)
[ IN keyval] key value (integer)
[ OUT attribute_val] attribute value, unless flag = false
[ OUT flag] true if an attribute value was extracted; false if no attribute is associated with the key

Retrieves attribute value by key.
The call is erroneous if there is no key with value
keyval. On the other hand, the call is correct if the key value
exists, but no attribute is attached on comm for that key; in such case,
the call returns flag = false. In particular
MPI_KEYVAL_INVALID is an erroneous key value.

MPI_ATTR_DELETE(comm, keyval)
[ IN comm] communicator to which attribute is attached (handle)
[ IN keyval] The key value of the deleted attribute (integer)

int MPI_Attr_delete(MPI_Comm comm, int keyval)

MPI_ATTR_DELETE(COMM, KEYVAL, IERROR) INTEGER COMM, KEYVAL, IERROR

Delete attribute from cache by key. This function invokes the
attribute delete function delete_fn
specified when the keyval was created.

Whenever a communicator is replicated using the function
MPI_COMM_DUP, all call-back copy functions for attributes
that are currently set are invoked (in arbitrary order).
Whenever a communicator is deleted using the function
MPI_COMM_FREE all callback delete functions for attributes
that are currently set are invoked.