What is MPI_Comm_set_attr?

MPI library

MPI (Message Passing Interface) is a library that enables you to write parallel programs in C or Fortran77. The library uses commonly available operating system services to create parallel processes and exchange information among these processes.

Parallel processing

MPI_Comm_set_attr

This method stores an attribute value associated with a key.

Syntax

int MPI_Comm_set_attr(MPI_Comm comm, int comm_keyval, void *attribute_val)

Parameters

  • comm is a communicator object describing a group of processes. In many applications, various processes work together and a communicator describes the processes that a routine initiates.

  • comm_keyval is the key value returned by the MPI_Comm_create_keyval method.

  • attribute_val is the attribute value to be stored.

Return value

  • The function returns an error if it is unsuccessful. By default, the error aborts the MPI job.

  • In case of success, it returns MPI_SUCCESS - the value returned upon successful termination of any MPI routine.

  • In case of an invalid communicator provided in the function’s argument, MPI_ERR_COM is returned.

  • MPI_ERR_KEYVAL is returned if the key value is invalid.

  • MPI_ARR_ARG is returned if an attempt was made to free a permanent key.

Example

The example below shows how we can use the MPI_set_attr function:

#include "mpi.h"
#include <stdio.h>
int main( int argc, char *argv[] )
{
int key[3], attrval[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 );
attrval[i] = 1024 * i;
}
/* Insert attribute in several orders. */
MPI_Comm_set_attr( comm, key[2], &attrval[2] );
MPI_Comm_set_attr( comm, key[1], &attrval[1] );
MPI_Comm_set_attr( comm, key[0], &attrval[0] );
}
  • We initialize key and attrval as an array of 3 integer elements.

  • Then, we initialize comm with MPI_COMM_WORLD, which describes all the processes that the current job starts with.

  • After that, we create key values using the MPI_Comm_create_keyval method and define attribute values in the for loop (lines 14 and 15).

  • Finally, we use the MPI_Comm_set_attr method to set attribute values associated with the keys (lines 19, 20, and 21).

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved