MPI\_KEYVAL\_CREATE(3) Open MPI MPI\_KEYVAL\_CREATE(3)

MPI\_Keyval\_create — Generates a new attribute key — this routine is deprecated in favor of MPI\_Comm\_create\_keyval &lt;#mpi-comm-create-keyval&gt;.

# [SYNTAX](#SYNTAX)

## [C Syntax](#C_Syntax)

```
#include <mpi.h>
int MPI_Keyval_create(MPI_Copy_function *copy_fn,


     MPI_Delete_function *delete_fn, int *keyval, void *extra_state)
```

## [Fortran Syntax](#Fortran_Syntax)

```
USE MPI
! or the older form: INCLUDE 'mpif.h'
MPI_KEYVAL_CREATE(COPY_FN, DELETE_FN, KEYVAL, EXTRA_STATE, IERROR)


     EXTERNAL        COPY_FN, DELETE_FN


     INTEGER KEYVAL, EXTRA_STATE, IERROR
```

# [INPUT PARAMETERS](#INPUT_PARAMETERS)

- **copy\_fn**: Copy callback function for keyval.
- **delete\_fn**: Delete callback function for keyval.
- **extra\_state**: Extra state for callback functions.

# [OUTPUT PARAMETERS](#OUTPUT_PARAMETERS)

- **keyval**: Key value for future access (integer).
- **ierror**: Fortran only: Error status (integer).

# [DESCRIPTION](#DESCRIPTION)

Note that use of this routine is *deprecated* as of MPI-2. Please use MPI\_Comm\_create\_keyval &lt;#mpi-comm-create-keyval&gt; instead.

Generates a new attribute key. Keys are locally unique in a process and opaque to the 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 &lt;#mpi-comm-dup&gt;. copy\_fn should be of type MPI\_Copy\_function, which is defined as follows:

```
typedef int MPI_Copy_function(MPI_Comm oldcomm, int keyval,


                              void *extra_state, void *attribute_val_in,


                              void *attribute_val_out, int *flag)
```

A Fortran declaration for such a function is as follows:

```
SUBROUTINE COPY_FUNCTION(OLDCOMM, KEYVAL, EXTRA_STATE, ATTRIBUTE_VAL_IN,


            ATTRIBUTE_VAL_OUT, FLAG, IERR)
INTEGER OLDCOMM, KEYVAL, EXTRA_STATE,
ATTRIBUTE_VAL_IN, ATTRIBUTE_VAL_OUT, IERR
LOGICAL FLAG
```

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 to the value returned in attribute\_val\_out. The function returns MPI\_SUCCESS on success and an error code on failure (in which case MPI\_Comm\_dup &lt;#mpi-comm-dup&gt; will fail).

copy\_fn may be specified as MPI\_NULL\_COPY\_FN or MPI\_DUP\_FN from either C or Fortran; MPI\_NULL\_COPY\_FN is a function that does nothing other than return flag = 0, and MPI\_SUCCESS. MPI\_DUP\_FN is a simple-minded copy function that sets flag = 1, returns the value of attribute\_val\_in in attribute\_val\_out, and returns MPI\_SUCCESS.

# [NOTES](#NOTES)

Key values are global (available for any and all communicators).

There are subtle differences between C and Fortran that require that the copy\_fn be written in the same language that MPI\_Keyval\_create is called from. This should not be a problem for most users; only programmers using both Fortran and C in the same program need to be sure that they follow this rule.

Even though both formal arguments attribute\_val\_in and attribute\_val\_out are of type void\*, their usage differs. The C copy function is passed by MPI in attribute\_val\_in the value of the attribute, and in attribute\_val\_out the address of the attribute, so as to allow the function to return [the (new)](/man/os/openmpi/the.new) attribute value. The use of type **void \*** for both is to avoid messy type casts.

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

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 &lt;#mpi-comm-free&gt; or when a call is made explicitly to MPI\_Attr\_delete &lt;#mpi-attr-delete&gt;. delete\_fn should be of type MPI\_Delete\_function, which is defined as follows:

```
typedef int MPI_Delete_function(MPI_Comm comm, int keyval,


    void *attribute_val, void *extra_state);
```

A Fortran declaration for such a function is as follows:

```
SUBROUTINE DELETE_FUNCTION(COMM, KEYVAL,ATTRIBUTE_VAL, EXTRA_STATE, IERR)


    INTEGER COMM, KEYVAL, ATTRIBUTE_VAL, EXTRA_STATE, IERR
```

This function is called by MPI\_Comm\_free &lt;#mpi-comm-free&gt;, MPI\_Attr\_delete &lt;#mpi-attr-delete&gt;, and MPI\_Attr\_put &lt;#mpi-attr-put&gt; to do whatever is needed to remove an attribute. The function returns MPI\_SUCCESS on success and an error code on failure (in which case MPI\_Comm\_free &lt;#mpi-comm-free&gt; will fail).

delete\_fn may be specified as MPI\_NULL\_DELETE\_FN from either C or FORTRAN; MPI\_NULL\_DELETE\_FN is a function that does nothing, other than returning MPI\_SUCCESS.

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.

# [ERRORS](#ERRORS)

Almost all MPI routines return an error value; C routines as the return result of the function and Fortran routines in the last argument.

Before the error value is returned, the current MPI error handler associated with the communication object (e.g., communicator, window, file) is called. If no communication object is associated with the MPI call, then the call is considered attached to MPI\_COMM\_SELF and will call the associated MPI error handler. When MPI\_COMM\_SELF is not initialized (i.e., before MPI\_Init &lt;#mpi-init&gt;/MPI\_Init\_thread &lt;#mpi-init-thread&gt;, after MPI\_Finalize &lt;#mpi-finalize&gt;, or when using the Sessions Model exclusively) the error raises the initial error handler. The initial error handler can be changed by calling MPI\_Comm\_set\_errhandler &lt;#mpi-comm-set-errhandler&gt; on MPI\_COMM\_SELF when using the World model, or the mpi\_initial\_errhandler CLI argument to mpiexec or info key to MPI\_Comm\_spawn &lt;#mpi-comm-spawn&gt;/MPI\_Comm\_spawn\_multiple &lt;#mpi-comm-spawn-multiple&gt;. If no other appropriate error handler has been set, then the MPI\_ERRORS\_RETURN error handler is called for MPI I/O functions and the MPI\_ERRORS\_ABORT error handler is called for all other MPI functions.

Open MPI includes three predefined error handlers that can be used:

- **MPI\_ERRORS\_ARE\_FATAL** Causes the program to abort all connected MPI processes.
- **MPI\_ERRORS\_ABORT** An error handler that can be invoked on a communicator, window, file, or session. When called on a communicator, it acts as if MPI\_Abort &lt;#mpi-abort&gt; was called on that communicator. If called on a window or file, acts as if MPI\_Abort &lt;#mpi-abort&gt; was called on a communicator containing the group of processes in the corresponding window or file. If called on a session, aborts only the local process.
- **MPI\_ERRORS\_RETURN** Returns an error code to the application.

MPI applications can also implement their own error handlers by calling:

- MPI\_Comm\_create\_errhandler &lt;#mpi-comm-create-errhandler&gt; then MPI\_Comm\_set\_errhandler &lt;#mpi-comm-set-errhandler&gt;
- MPI\_File\_create\_errhandler &lt;#mpi-file-create-errhandler&gt; then MPI\_File\_set\_errhandler &lt;#mpi-file-set-errhandler&gt;
- MPI\_Session\_create\_errhandler &lt;#mpi-session-create-errhandler&gt; then MPI\_Session\_set\_errhandler &lt;#mpi-session-set-errhandler&gt; or at MPI\_Session\_init &lt;#mpi-session-init&gt;
- MPI\_Win\_create\_errhandler &lt;#mpi-win-create-errhandler&gt; then MPI\_Win\_set\_errhandler &lt;#mpi-win-set-errhandler&gt;

Note that MPI does not guarantee that an MPI program can continue past an error.

See the MPI man page &lt;#open-mpi&gt; for a full list of MPI error codes &lt;#open-mpi-errors&gt;.

See the Error Handling section of the MPI-3.1 standard for more information.

**See also:**

- MPI\_Keyval\_free &lt;#mpi-keyval-free&gt;
- MPI\_Comm\_create\_keyval &lt;#mpi-comm-create-keyval&gt;
- MPI\_Comm\_free\_keyval &lt;#mpi-comm-free-keyval&gt;

# [Copyright](#Copyright)

2003-2026, The Open MPI Community

September 16, 2026

---

Document: MPI_Keyval_create(3) from openmpi 5.0.11-r1 (Chainguard OS)
Source: https://github.com/open-mpi/ompi
