MPI_IS_THREAD_MAIN(3)

πŸ’– MPI_IS_THREAD_MAIN πŸ’–

okay bestie, so this one lives in Open MPI section 3, and it's literally the "am I the main character?" detector for threads. No cap. Let's get into it 🫢


✨ SYNTAX ✨

🌸 C Syntax

#include <mpi.h>

int MPI_Is_thread_main(int *flag)

🌸 Fortran Syntax

USE MPI
! or the older form: INCLUDE 'mpif.h'
MPI_IS_THREAD_MAIN(FLAG, IERROR)
     LOGICAL FLAG
     INTEGER IERROR

🌸 Fortran 2008 Syntax

USE mpi_f08
MPI_Is_thread_main(flag, ierror)
     LOGICAL, INTENT(OUT) :: flag
     INTEGER, OPTIONAL, INTENT(OUT) :: ierror

πŸŽ€ OUTPUT PARAMETERS πŸŽ€

  • flag β€” the moment of truth: it's true when the calling thread is the main thread (a boolean, so literally just yes/no energy).
  • ierror β€” Fortran only, bestie: the error status (integer).

πŸ’… DESCRIPTION πŸ’…

So MPI_Is_thread_main gets called by a thread to figure out whether the caller is the main thread β€” and by "main thread" we mean the thread that called MPI_Init or MPI_Init_thread. It's giving "am I the one who started all of this, or am I just a side character?" energy, and honestly, valid question. πŸ¦‹


😭 ERRORS 😭

Almost every MPI routine hands back an error value β€” C routines give it to you as the return result of the function, and Fortran routines put it in the last argument. Set your expectations accordingly, bestie.

Before that error value even makes it back to you, the current MPI error handler associated with the communication object (think: communicator, window, file) gets called. If there's no communication object attached to the MPI call, then the call is treated as attached to MPI_COMM_SELF and it'll call the associated MPI error handler. And when MPI_COMM_SELF isn't initialized β€” i.e., 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 swap that initial error handler out by calling MPI_Comm_set_errhandler on MPI_COMM_SELF when you're on the World model, or via the mpi_initial_errhandler CLI argument to mpiexec, or via 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, and the MPI_ERRORS_ABORT error handler gets called for all other MPI functions. Periodt.

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

  • MPI_ERRORS_ARE_FATAL β€” causes the program to abort all connected MPI processes. dramatic much? yes. but that's the point. πŸ’€
  • MPI_ERRORS_ABORT β€” an error handler that can be invoked on a communicator, window, file, or session. When it's called on a communicator, it acts as if MPI_Abort was called on that communicator. 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. Called on a session? It aborts only the local process.
  • MPI_ERRORS_RETURN β€” returns an error code back to the application. polite king behavior πŸ‘‘

MPI applications can also cook up their own error handlers by calling:

  • MPI_Comm_create_errhandler and then MPI_Comm_set_errhandler
  • MPI_File_create_errhandler and then MPI_File_set_errhandler
  • MPI_Session_create_errhandler and then MPI_Session_set_errhandler, or at MPI_Session_init
  • MPI_Win_create_errhandler and then MPI_Win_set_errhandler
⚠️ heads up bestie: MPI does not guarantee that an MPI program can continue past an error. so like… don't count on it. πŸ™ˆ

See the MPI man page for a full list of MPI error codes. And see the Error Handling section of the MPI-3.1 standard for more info.

See also:

  • MPI_Init
  • MPI_Init_thread

2003-2026, The Open MPI Community


okay that's the whole page, bestie β€” the Open MPI crew gave us this one on Sep 10, 2026, and it's literally filed under MPI_IS_THREAD_MAIN(3). go find out if you're the main thread, you probably are πŸ’–βœ¨πŸŽ€

πŸ“¦ where this page comes from