MPI_COMM_SIZE(3)

πŸ’… NAME

MPI_Comm_size β€” returns the size of the group associated with a communicator. yes bestie that's literally it, it's the "how many of us are in here" function πŸ’–

(this page is from the Open MPI manual, welcome to the group chat)


✨ SYNTAX ✨

🌸 C Syntax

#include <mpi.h>

int MPI_Comm_size(MPI_Comm comm, int *size)

πŸ¦‹ Fortran Syntax

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

🧚 Fortran 2008 Syntax

USE mpi_f08
MPI_Comm_size(comm, size, ierror)
     TYPE(MPI_Comm), INTENT(IN) :: comm
     INTEGER, INTENT(OUT) :: size
     INTEGER, OPTIONAL, INTENT(OUT) :: ierror

🫢 INPUT PARAMETER

  • comm β€” the communicator (handle). that's the whole input, she's low maintenance πŸ’…

πŸ’– OUTPUT PARAMETERS

  • size β€” number of processes in the group of comm (integer). like, the headcount πŸŽ€
  • ierror β€” Fortran only: error status (integer).

🌈 DESCRIPTION

okay so this function tells you the number of processes involved in a communicator. if you pass it MPI_COMM_WORLD, it hands you the total number of processes available. periodt.

it's exactly equivalent to grabbing the communicator's group with MPI_Comm_group, computing the size using MPI_Group_size, and then freeing that temporary group via MPI_Group_free. same energy, way less typing ✨

plot twist though: if the communicator is an inter-communicator (the kind that enables communication between two groups), this function returns the size of the local group. if you want the size of the remote group, you gotta use the MPI_Comm_remote_size function instead. different function, different vibe, iykyk.

this call is often used with MPI_Comm_rank to figure out how much concurrency is actually available for a specific library or program. MPI_Comm_rank tells you the rank of the process that calls it, in the range from 0 . . πŸ”₯


πŸŽ€ NOTE

MPI_COMM_NULL is not considered a valid argument to this function. not me warning you about it, but like, i'm warning you about it πŸ™ˆ


πŸ”₯ ERRORS

almost all MPI routines return an error value β€” C routines as the return result of the function, and Fortran routines in the last argument. no cap.

before the error value is returned, the current MPI error handler associated with the communication object (e.g., communicator, window, file) is called. if no communication object is associated with the MPI call, then the call is considered attached to MPI_COMM_SELF and will call the associated MPI error handler. when MPI_COMM_SELF is not initialized (i.e., before MPI_Init/MPI_Init_thread, after MPI_Finalize, or when using the Sessions Model exclusively) the error raises the initial error handler. that initial error handler can be changed by calling MPI_Comm_set_errhandler on MPI_COMM_SELF when using the World model, or the mpi_initial_errhandler CLI argument to mpiexec or 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 is called for MPI I/O functions, and the MPI_ERRORS_ABORT error handler is called for all other MPI functions. she's thorough πŸ’€

Open MPI includes three predefined error handlers that can be used:

  • MPI_ERRORS_ARE_FATAL β€” causes the program to abort all connected MPI processes. dramatic, but make it fatal πŸ’€
  • 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. multitasking queen πŸ‘‘
  • MPI_ERRORS_RETURN β€” returns an error code to the application. the polite one 🫢

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
note that MPI does not guarantee that an MPI program can continue past an error. like, fr fr, do not assume you can just keep going 😭

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_rank
  • MPI_Comm_compare

2003-2026, The Open MPI Community


and that's the whole page bestie 🫢 brought to you by the Open MPI girlies, dated Sep 11, 2026 β€” go forth and count your processes, you're gonna slay βœ¨πŸ’–πŸŽ€

πŸ“¦ where this page comes from