MPI_FETCH_AND_OP(3)

🌸 NAME 🌸

MPI_Fetch_and_op β€” bestie, it takes the contents of the origin buffer, combines them with a target buffer, and hands you back what the target buffer had before. iconic behavior honestly πŸ’…


✨ SYNTAX ✨

πŸŽ€ C Syntax

#include <mpi.h>

int MPI_Fetch_and_op(const void *origin_addr, void *result_addr,
     MPI_Datatype datatype, int target_rank, MPI_Aint target_disp,
     MPI_Op op, MPI_Win win)

πŸŽ€ Fortran Syntax

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

πŸŽ€ Fortran 2008 Syntax

USE mpi_f08
MPI_Fetch_and_op(origin_addr, result_addr, datatype, target_rank,
             target_disp, op, win, ierror)
     TYPE(*), DIMENSION(..), INTENT(IN) :: origin_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_Op), INTENET(IN) :: op
     TYPE(MPI_Win), INTENT(IN) :: win
     INTEGER, OPTIONAL, INTENT(OUT) :: ierror

🌈 INPUT PARAMETERS 🌈

  • origin_addr β€” the starting address of the buffer, no cap. (choice)
  • result_addr β€” the starting address of the result buffer, that's where the tea gets collected. (choice)
  • datatype β€” the data type of the entry living in the origin, result, and target buffers. (handle)
  • target_rank β€” rank of the target, has to be a nonnegative integer. (nonnegative integer)
  • target_disp β€” displacement from the start of the window to the beginning of the target buffer. (nonnegative integer)
  • op β€” the reduce operation. (handle)
  • win β€” the window object. (handle)

πŸ’– OUTPUT PARAMETER πŸ’–

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

πŸ¦‹ DESCRIPTION πŸ¦‹

Okay so here's the vibe: it accumulates one element of type datatype, going from the origin buffer (origin_addr) over to the buffer at offset target_disp, inside the target window that target_rank and win are pointing at. It uses operation op, and then β€” this is the fun part β€” it returns into the result buffer result_addr whatever the target buffer was holding before the accumulation happened. like, it's giving receipts πŸ“Έ

The origin and result buffers (origin_addr and result_addr) must be disjoint. Not optional, not negotiable, fr fr. Any of the predefined operations for MPI_Rreduce can go in as op, plus MPI_NO_OP or MPI_REPLACE β€” but user-defined functions? nope, can't use those. The datatype argument has to be a predefined datatype. And the whole operation is executed atomically. periodt.

There's a new predefined operation, MPI_REPLACE. It corresponds to the associative function f(a, b) =b β€” meaning the current value sitting in the target memory just gets replaced by the value the origin supplied. out with the old πŸ’€

And there's another new predefined operation, MPI_NO_OP. It corresponds to the associative function f(a, b) = a β€” so the current value in the target memory gets returned in the result buffer over at the origin, and nothing at all happens to the target buffer. zero changes, zero drama, main character energy of doing nothing 🧚


πŸ‘‘ 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_Fetch_and_op function.

that's on you bestie, not on MPI ✨


πŸ’œ ERRORS πŸ’œ

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

Before that error value comes back, the current MPI error handler associated with the communication object (like a communicator, window, or file) gets called. If there's no communication object associated with the MPI call, then the call counts 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 change that initial error handler by calling MPI_Comm_set_errhandler on MPI_COMM_SELF when you're using the World model, or with the mpi_initial_errhandler CLI argument to mpiexec, or 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, while the MPI_ERRORS_ABORT error handler gets called for all other MPI functions. 😭

Open MPI ships three predefined error handlers you can use:

  • MPI_ERRORS_ARE_FATAL β€” causes the program to abort all connected MPI processes. yeah. all of them. πŸ’€
  • 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. If it's 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 it's called on a session, it aborts only the local process.
  • MPI_ERRORS_RETURN β€” returns an error code to the application. polite of it 🫢

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. so like, be careful bestie πŸ™ˆ

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_Get_accumulate
  • MPI_Reduce

2003-2026, The Open MPI Community


and that's the whole page, bestie β€” Open MPI, page MPI_FETCH_AND_OP(3), dated Sep 10, 2026. slay, go accumulate something atomically, ily πŸ’–πŸŒΈβœ¨

πŸ“¦ where this page comes from