Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

c

What is MPI_Comm_get_attr?

Adnan Abbas

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

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_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

  • 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 an output parameter representing attribute value.

  • flag is 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_COM is returned.

  • MPI_ERR_KEYVAL is 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 );
}
}
MPI_Comm_get_attr
  • 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).

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

  • Finally, we use the MPI_Comm_get_attr to retrieve the attribute value associated with each key (line 25).

RELATED TAGS

c

CONTRIBUTOR

Adnan Abbas
Copyright ©2022 Educative, Inc. All rights reserved

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring