MPI_COMPARE_AND_SWAP(3)
π MPI_COMPARE_AND_SWAP(3) β Open MPI, bestie edition
Okay so picture this: it's an Open MPI man page, it's called MPI_Compare_and_swap, and it's here to Perform RMA compare-and-swap. That's the whole name section, we ate that. π
β¨ 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β initial address of the buffer, bestie (choice). Your value lives here.compare_addrβ initial address of the compare buffer (choice). This is the one getting vibe-checked.result_addrβ initial address of the result buffer (choice). Whatever was chillin' at the target before lands here.datatypeβ the data type of the entry in the origin, result, and target buffers (handle). No cap, this matters.target_rankβ rank of the target (nonnegative integer). Like, who are we visiting.target_dispβ displacement from the start of the window to the beginning of the target buffer (nonnegative integer).winβ window object (handle). The whole little neighborhood we're poking at.
πΈ OUTPUT PARAMETER πΈ
ierrorβ Fortran only: error status (integer). Yes, only Fortran, don't come for me.
π¦ DESCRIPTION π¦
okay so here's the tea, and it's actually kind of iconic. β
This function compares one element of type datatype in the compare buffer compare_addr with the buffer at offset target_disp in the target window specified by target_rank and win β and if the compare buffer and the target buffer are identical, it replaces the value at the target with the value in the origin buffer origin_addr. The original value at the target gets returned in the buffer result_addr. Literal atomic swap energy, main character behavior, the way that it just does that.
The parameter datatype must belong to one of these categories of predefined datatypes, and I'm listing all of them because I love you:
- C integer
- Fortran integer
- Logical
- Multi-language types
- Byte
β¦as specified in MPI-3 section 5.9.2 on page 176. π
π¨ Bestie, important: the origin and result buffers (origin_addrandresult_addr) must be disjoint. Like, no overlapping, no "it's probably fine." It's not fine. Do not do it.
π NOTES π
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. That's on you, not me. I'm just the messenger. π
π₯ 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.
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.
β¨ The three predefined error handlers Open MPI ships with β¨
MPI_ERRORS_ARE_FATALβ causes the program to abort all connected MPI processes. π Like, everybody. Gone.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 ifMPI_Abortwas called on that communicator. If called on a window or file, it acts as ifMPI_Abortwas 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. The polite one. π«Ά
π DIY error handlers, for the overachievers
MPI applications can also implement their own error handlers by calling:
MPI_Comm_create_errhandlerthenMPI_Comm_set_errhandlerMPI_File_create_errhandlerthenMPI_File_set_errhandlerMPI_Session_create_errhandlerthenMPI_Session_set_errhandleror atMPI_Session_initMPI_Win_create_errhandlerthenMPI_Win_set_errhandler
β οΈ Note that MPI does not guarantee that an MPI program can continue past an error. Just so we're clear. That's not me being dramatic, that's the standard.
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.
π COPYRIGHT π
2003-2026, The Open MPI Community
okay that's the whole page, you're so welcome π β this masterpiece dropped on Sep 11, 2026, courtesy of Open MPI (the MPI_COMPARE_AND_SWAP(3) era, we're living in it). go forth and compare-and-swap responsibly, bestie πβ¨π
π¦ where this page comes from
- arch rolling: openmpi 5.0.11-1 Β· BSD-3-Clause AND LicenseRef-MPICH
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms