MPI_COMM_RANK(3)

πŸ’– MPI_Comm_rank β€” Open MPI, bestie edition πŸ’–

okay so real talk, this is MPI_Comm_rank(3) from the Open MPI manual, and it's genuinely one of the most iconic functions in the whole MPI lineup. If you've ever wondered "wait, who even am I in this communicator?" β€” this is the function that answers that. Let's get into it. 🫢


✨ NAME ✨

MPI_Comm_rank β€” figures out the rank of the process that's calling it, inside the communicator. that's it, that's the whole job, and she does it flawlessly. πŸ‘‘


🌸 SYNTAX 🌸

πŸ’… C Syntax

#include <mpi.h>

int MPI_Comm_rank(MPI_Comm comm, int *rank)

πŸ’… Fortran Syntax

USE MPI
! or the older form: INCLUDE 'mpif.h'
MPI_COMM_RANK(COMM, RANK, IERROR)
     INTEGER COMM, RANK, IERROR

πŸ’… Fortran 2008 Syntax

USE mpi_f08
MPI_Comm_rank(comm, rank, ierror)
     TYPE(MPI_Comm), INTENT(IN) :: comm
     INTEGER, INTENT(OUT) :: rank
     INTEGER, OPTIONAL, INTENT(OUT) :: ierror

πŸ“₯ INPUT PARAMETERS πŸ“₯

  • comm β€” the communicator, a handle. yes bestie, that's the only thing you hand her and she does the rest.

πŸ“€ OUTPUT PARAMETERS πŸ“€

  • rank β€” the rank of the calling process inside the group of comm. it's an integer, it's your little number, it's your whole identity lowkey.
  • ierror β€” Fortran only, babes: the error status, also an integer.

πŸ¦‹ DESCRIPTION πŸ¦‹

So basically, this function hands you the rank of your process within that particular communicator's group. πŸŽ€

The way it works under the hood is exactly equivalent to doing this whole lil routine:

  1. grab the communicator's group with MPI_Comm_group
  2. compute the rank using MPI_Group_rank
  3. then free the temporary group via MPI_Group_free

same energy, way less typing. she ate and left no crumbs. πŸ’…

ngl a LOT of programs are written using the manager-worker model, where one process (like, say, the rank-zero process β€” main character energy fr) plays the supervisory role, and all the other processes serve as compute nodes. in that setup, MPI_Comm_size and MPI_Comm_rank are the duo that tells you what role each process of a communicator is playing. know your role bestie. ✨


😭 ERRORS 😭

Okay this part is long but it's important, so pay attention, no skimming. πŸ™ˆ

Almost all MPI routines return an error value. In C routines it comes back as the return result of the function, and in Fortran routines it's the last argument.

Before that error value ever gets returned, the current MPI error handler associated with the communication object (so like, a communicator, window, or file) gets called. And if there's no communication object associated with the MPI call? Then that call is considered attached to MPI_COMM_SELF, and it will call the associated MPI error handler. πŸ’€

When MPI_COMM_SELF is not initialized β€” meaning before MPI_Init/MPI_Init_thread, after MPI_Finalize, or when you're using the Sessions Model exclusively β€” the error raises the initial error handler.

You can change that initial error handler, but only these ways:

  • calling MPI_Comm_set_errhandler on MPI_COMM_SELF when using the World model
  • the mpi_initial_errhandler CLI argument to mpiexec
  • the info key to MPI_Comm_spawn/MPI_Comm_spawn_multiple

And if no other appropriate error handler has been set, then the MPI_ERRORS_RETURN error handler gets called for MPI I/O functions, while the MPI_ERRORS_ABORT error handler gets called for all other MPI functions.

Open MPI ships with three predefined error handlers you can just use, periodt:

  • MPI_ERRORS_ARE_FATAL β€” causes the program to abort all connected MPI processes. dramatic but effective.
  • 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, it 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, it aborts only the local process.
  • MPI_ERRORS_RETURN β€” returns an error code to the application. chill and polite honestly.

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
⚠️ heads up bestie: note that MPI does not guarantee that an MPI program can continue past an error. so like... don't bank on it.

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_Comm_group
  • MPI_Comm_size
  • MPI_Comm_compare

2003-2026, The Open MPI Community


and that's the whole page, bestie!! πŸŽ€ thanks for reading all the way to the end, you're literally a scholar. this was Open MPI, brought to you on Sep 10, 2026, and you were kicking it with MPI_COMM_RANK(3). go find your rank and slay. πŸ’–βœ¨πŸ¦‹

πŸ“¦ where this page comes from