What is MPI_Comm_free_keyval?

MPI library

MPI, short for Message Passing Interface, is a library that allows you to write parallel programs in Fortran77 or C. The library uses common operating system services to exchange information among the parallel processes it creates.

Parallel processing

MPI_Comm_free_keyval

This method deletes an attribute key for communicator objects.

Syntax

int MPI_Comm_free_keyval(
  int *comm_keyval
);

Parameters

  • comm_keyval is the key value that will be deleted.

Return value

If unsuccessful, the function returns an error. The error aborts the MPI job by default.

  • In case of success, MPI_Comm_free_keyval returns MPI_SUCCESS, which is the value returned upon successful termination of any MPI routine.

  • MPI_ARR_ARG is returned if an invalid argument is provided or an attempt is made to free one of the permanent keys.

Example

The following illustrates the use of MPI_Comm_free_keyval.

#include "mpi.h"
#include <stdio.h>

int main( int argc, char *argv[] )
{
  int key[3];
  int i;
  MPI_Comm comm;

  MPI_Init( &argc, &argv );
  comm = MPI_COMM_WORLD;
  /* Creating key values */
  for (i=0; i<3; i++) {
      MPI_Comm_create_keyval( MPI_NULL_COPY_FN, MPI_NULL_DELETE_FN, &key[i], (void *)0 );
   }

  /* Deleting key values */

    for (i=0; i<3; i++) {
        MPI_Comm_free_keyval( &key[i] );
    }
}
  • We create three key values in the for loop.

  • We use the default copy and delete functions, MPI_COMM_NULL_COPY_FN and MPI_COMM_NULL_DELETE_FN, in the function’s arguments.

  • Finally, we free the key values using the MPI_Comm_free_keyval function at the end of the above code.

Free Resources

Copyright ©2026 Educative, Inc. All rights reserved