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_addrandresult_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 ifMPI_Abortwas called on that communicator. If called on a window or file, acts as ifMPI_Abortwas 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_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. 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.
π Copyright π
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
- ubuntu stonking: openmpi-doc 5.0.10-6 Β· other
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms