What is MPI_Comm_get_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.
MPI_Comm_get_attr
This method gets an attribute value associated with a key.
Syntax
int MPI_Comm_get_attr(MPI_Comm comm, int comm_keyval, void *attribute_val, int *flag)
Parameters
-
commis 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_keyvalis the key value returned by theMPI_Comm_create_keyvalmethod. -
attribute_valis an output parameter representing attribute value. -
flagis an output parameter that returns true if an attribute value was extracted and false otherwise.
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_COMis returned. -
MPI_ERR_KEYVALis returned if the key value is invalid.
Example
The following code snippet shows how we can use the MPI_get_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] );int i, flag, *val_p;for (i=0; i<n; i++) {MPI_Comm_get_attr( comm, key[i], &val_p, &flag );}}
-
We initialize
keyandattrvalas an array of 3 integer elements. -
Then, we initialize
commwithMPI_COMM_WORLD, which describes all the processes that the current job starts with. -
After that, we create key values using the
MPI_Comm_create_keyvalmethod and define attribute values in theforloop (lines 14 and 15). -
We use the
MPI_Comm_set_attrmethod to set attribute values associated with the keys (lines 19, 20, and 21). -
Finally, we use the
MPI_Comm_get_attrto retrieve the attribute value associated with each key (line 25).
Free Resources