MPI_KEYVAL_CREATE(3)

πŸ’– NAME πŸ’–

MPI_Keyval_create β€” bestie, this one generates a brand new attribute key ✨ β€” but heads up, this routine is deprecated in favor of MPI_Comm_create_keyval.


🌸 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

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 πŸŽ€

  • copy_fn β€” the copy callback function for your keyval, aka the vibe you want copied over 🫢
  • delete_fn β€” the delete callback function for your keyval, aka the cleanup crew 🧹
  • extra_state β€” extra state for the callback functions. little snack you're handing them for later πŸ“

✨ OUTPUT PARAMETERS ✨

  • keyval β€” key value for future access (integer). this is your golden ticket 🎟️
  • ierror β€” Fortran only: error status (integer).

πŸ’… DESCRIPTION πŸ’…

Not to be dramatic but use of this routine is deprecated as of MPI-2. Please use MPI_Comm_create_keyval instead, okurrr? πŸ’–

So. It generates a new attribute key. Keys are locally unique in a process and opaque to the user, even though they're literally just stored in integers (iconic, honestly). Once a key is allocated, the key value can be used to associate attributes and access them on any locally defined communicator. slay πŸ’…

The copy_fn function gets invoked when a communicator is duplicated by MPI_Comm_dup. 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 (no, you don't get to pick, it's giving chaos). 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 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. lowkey the laziest girlies in the game and we respect them for it 🫢


πŸ‘‘ NOTES πŸ‘‘

Key values are global β€” available for any and all communicators. periodt.

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. so if that's you… be so fr and check yourself πŸ™ˆ

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) attribute value. The use of type void * for both is to avoid messy type casts. (the way that C just… does that πŸ’€)

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. no crumbs, no copy, no cap.

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:

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, MPI_Attr_delete, and MPI_Attr_put 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 will fail. she's on cleanup duty and she takes it seriously πŸ’…

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. queen of doing the bare minimum, honestly same 🌸

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. noted and archived πŸ“Œ


πŸ¦‹ 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. classic.

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/MPI_Init_thread, after MPI_Finalize, 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 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/MPI_Comm_spawn_multiple. 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. it's giving no survivors πŸ’€
  • 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 was called on that communicator. If called on a window or file, acts as if MPI_Abort 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 then MPI_Comm_set_errhandler
  • MPI_File_create_errhandler then MPI_File_set_errhandler
  • MPI_Session_create_errhandler then MPI_Session_set_errhandler or at MPI_Session_init
  • MPI_Win_create_errhandler then MPI_Win_set_errhandler

Note that MPI does not guarantee that an MPI program can continue past an error. (she might not make it, bestie 😭)

See the MPI man page for a full list of MPI error codes.

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

See also:

  • MPI_Keyval_free
  • MPI_Comm_create_keyval
  • MPI_Comm_free_keyval

2003-2026, The Open MPI Community


okay that's the whole page bestie, you ate that πŸŽ€ this was brought to you by the Open MPI girlies, dated Sep 10, 2026, filed under MPI_KEYVAL_CREATE(3) β€” mwah, go forth and create those keys πŸ’…βœ¨πŸ¦‹

πŸ“¦ where this page comes from