MPI_COMPARE_AND_SWAP(3)

πŸ’– MPI_COMPARE_AND_SWAP(3) πŸ’–

okay bestie, so we're in the Open MPI man pages and this one is literally called MPI_Compare_and_swap β€” and yeah, it does exactly what the name says: it performs an RMA compare-and-swap. main character energy fr. 🫢


✨ SYNTAX ✨

🌸 C Syntax

#include <mpi.h>

int MPI_Compare_and_swap(const void *origin_addr, const void *compare_addr,
     void *result_addr, MPI_Datatype datatype, int target_rank,
     MPI_Aint target_disp, MPI_Win win)

🌸 Fortran Syntax

USE MPI
! or the older form: INCLUDE 'mpif.h'
MPI_COMPARE_AND_SWAP(ORIGIN_ADDR, COMPARE_ADDR, RESULT_ADDR, DATATYPE, TARGET_RANK,
     TARGET_DISP, WIN, IERROR)
     <type> ORIGIN_ADDR, COMPARE_ADDR, RESULT_ADDR(*)
     INTEGER(KIND=MPI_ADDRESS_KIND) TARGET_DISP
     INTEGER DATATYPE, TARGET_RANK, WIN, IERROR

🌸 Fortran 2008 Syntax

USE mpi_f08
MPI_Compare_and_swap(origin_addr, compare_addr, result_addr, datatype,
             target_rank, target_disp, win, ierror)
     TYPE(*), DIMENSION(..), INTENT(IN) :: origin_addr, compare_addr
     TYPE(*), DIMENSION(..) :: result_addr
     TYPE(MPI_Datatype), INTENT(IN) :: datatype
     INTEGER, INTENT(IN) :: target_rank
     INTEGER(KIND=MPI_ADDRESS_KIND), INTENT(IN) :: target_disp
     TYPE(MPI_Win), INTENT(IN) :: win
     INTEGER, OPTIONAL, INTENT(OUT) :: ierror

πŸŽ€ INPUT PARAMETERS πŸŽ€

  • origin_addr β€” the initial address of the buffer, bestie (choice).
  • compare_addr β€” initial address of the compare buffer (choice).
  • result_addr β€” initial address of the result buffer (choice).
  • datatype β€” the data type of the entry living in the origin, result, and target buffers (handle).
  • target_rank β€” rank of the target (nonnegative integer).
  • target_disp β€” displacement from the start of the window to the beginning of the target buffer (nonnegative integer).
  • win β€” the window object (handle).

πŸ’… OUTPUT PARAMETER πŸ’…

  • ierror β€” Fortran only, smh: error status (integer).

πŸ¦‹ DESCRIPTION πŸ¦‹

okay so here's the tea β˜•: this function takes one element of type datatype in the compare buffer compare_addr and compares it against the buffer sitting at offset target_disp in the target window that you specified with target_rank and win. and if β€” and only if β€” the compare buffer and the target buffer are literally identical, it replaces the value at the target with the value from the origin buffer origin_addr. so iconic. the original value that was chillin' at the target gets returned in the buffer result_addr. ✨

and listen, the datatype parameter has to belong to one of these categories of predefined datatypes, no exceptions:

  • C integer
  • Fortran integer
  • Logical
  • Multi-language types
  • Byte

...as specified in MPI-3 section 5.9.2 on page 176. periodt. πŸ“–

⚠️ also!! the origin and result buffers (origin_addr and result_addr) must be disjoint. not optional. don't overlap them, i'm begging. πŸ™ˆ

πŸ“ NOTES πŸ“

ngl this part is on you, bestie: it is the user's responsibility to guarantee that, when using the accumulate functions, the target displacement argument is such that accesses to the window are properly aligned according to the data type arguments in the call to the MPI_Compare_and_swap function. get those alignments right or suffer πŸ’€


🌈 ERRORS 🌈

Almost all MPI routines return an error value β€” C routines hand it back as the return result of the function, and Fortran routines put it in the last argument. classic.

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. The 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. 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. 😭

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

  • MPI_ERRORS_ARE_FATAL β€” causes the program to abort all connected MPI processes. yeah. everyone. πŸ’€
  • 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, 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, aborts only the local process.
  • MPI_ERRORS_RETURN β€” returns an error code to the application. 🫢

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. fr fr, no cap.

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.


2003-2026, The Open MPI Community


and that's the whole vibe, bestie β€” go forth and compare-and-swap responsibly βœ¨πŸ’– this masterpiece of a page was served up by Open MPI on the very iconic date of Sep 10, 2026, and it's tagged MPI_COMPARE_AND_SWAP(3). slay. πŸ’…πŸŒˆ

πŸ“¦ where this page comes from