MPI_Buffer_detach(3)

πŸ“¦
version 5.0.1 Β· 5 versions
, in nixos 26.05, ubuntu stonking

πŸ’– MPI_Buffer_detach β€” bestie, we're un-attaching the buffer πŸ’–

okay so this is a page from the MPI manual, and honestly? it's giving "please put the furniture back exactly how you found it." let's get into it 🫢


✨ NAME ✨

MPI_Buffer_detach β€” removes an existing buffer (for use in MPI_Bsend etc). no cap, that's the whole job. it detaches. bye buffer πŸ‘‹


🌸 SYNOPSIS 🌸

int MPI_Buffer_detach(void *buffer_addr, int *size)

int MPI_Buffer_detach_c(void *buffer_addr, MPI_Count *size)

two flavors, bestie β€” the classic and the _c one. slay πŸ’…


πŸŽ€ OUTPUT PARAMETERS πŸŽ€

  • buffer_addr β€” the initial buffer address (it's a choice, which is MPI-speak for "this arg's type flexes depending on which binding you're in" β€” lowkey iconic)
  • size β€” the buffer size, in bytes (an integer)
these are output params, so they're writing stuff back to you, not the other way around. you're receiving, not giving. it's giving main character energy πŸ’–

🌈 NOTES 🌈

okay so here's the why, and it's actually kind of genius. the reason MPI_Buffer_detach hands you back the address and the size of the buffer it just detached is so that nested libraries can swap the buffer out and then put it back. like borrowing your friend's hoodie, wearing it, and returning it clean. 🧚

check it:

int size, mysize, idummy;
void *ptr, *myptr, *dummy;
MPI_Buffer_detach(&ptr, &size);
MPI_Buffer_attach(myptr, mysize);
.
.
.
.
.
.
library code ...
.
.
.
MPI_Buffer_detach(&dummy, &idummy);
MPI_Buffer_attach(ptr, size);

see the plot? grab the current one, stash it in ptr/size, install your own, do your thing, then yeet yours and restore the original. the original buffer survives untouched, as it should. πŸ‘‘

this is very much like the action of the Unix signal routine β€” same strengths (it is simple) and same weaknesses (it only works for nested usages). no notes, honestly.

Important lore: for this whole swap-and-restore trick to work, MPI_Buffer_detach must return MPI_SUCCESS even when there's no buffer to detach. in that case, it returns a size of zero. 🫑

and the MPI 1.1 standard for MPI_BUFFER_DETACH contains this text, verbatim:

The statements made in this section describe the behavior of MPI for buffered-mode sends. When no buffer is currently associated, MPI behaves as if a zero-sized buffer is associated with the process.

now, you could read that as applying only to the various Bsend routines. but this implementation takes the position that it applies to MPI_BUFFER_DETACH as well. and there you have it. πŸ’…


πŸ¦‹ THREAD AND INTERRUPT SAFETY πŸ¦‹

listen up, because this part matters:

the user is responsible for ensuring that multiple threads do not try to update the same MPI object from different threads. and this routine should not be used from within a signal handler. periodt.

the MPI standard defined a thread-safe interface, but bestie, that does not mean all routines may be called without any thread locks. for example: two threads must not attempt to change the contents of the same MPI_Info object concurrently. in that case, the user is responsible for using some mechanism β€” like thread locks β€” to ensure only one thread at a time makes use of this routine.

and here's the kicker: because the buffer for buffered sends (e.g., MPI_Bsend) is shared by all threads in a process, the user is responsible for ensuring that only one thread at a time calls this routine or MPI_Buffer_attach. one at a time, bestie. please. πŸ™ˆ


πŸ’œ NOTES FOR FORTRAN πŸ’œ

all MPI routines in Fortran (except for MPI_WTIME and MPI_WTICK) have an additional argument ierr at the end of the argument list. ierr is an integer and has the same meaning as the return value of the routine in C. in Fortran, MPI routines are subroutines, and they're invoked with the call statement. πŸ“ž

all MPI objects (e.g., MPI_Datatype, MPI_Comm) are of type INTEGER in Fortran. yes, even the ones you'd think aren't. all of them.

and this one's special: the Fortran binding for this routine is different. because Fortran does not have pointers, it is impossible to provide a way to use the output of this routine to exchange buffers. in this case, only the size field is set. so no buffer swap trick for the Fortran girlies β€” just the size. 😭


⭐ NOTES FOR C ⭐

even though the bufferptr argument is declared as void *, it is really the address of a void pointer. sneaky. see the rationale in the standard for more details. iykyk 🫢


πŸ”₯ ERRORS πŸ”₯

all MPI routines (except MPI_Wtime and MPI_Wtick) return an error value β€” C routines as the value of the function, and Fortran routines in the last argument. before the value is returned, the current MPI error handler is called. by default, this error handler aborts the MPI job. (yes, dramatically, like a season finale πŸ’€)

the error handler may be changed with:

  • MPI_Comm_set_errhandler (for communicators)
  • MPI_File_set_errhandler (for files)
  • MPI_Win_set_errhandler (for RMA windows)

the MPI-1 routine MPI_Errhandler_set may be used, but its use is deprecated 🚫 β€” don't do it, bestie.

the predefined error handler MPI_ERRORS_RETURN may be used to cause error values to be returned. note that MPI does not guarantee that an MPI program can continue past an error; however, MPI implementations will attempt to continue whenever possible.

the error codes, alphabetically served:

  • MPI_SUCCESS β€” no error; MPI routine completed successfully. we love a clean run ✨
  • MPI_ERR_ARG β€” invalid argument. some argument is invalid and is not identified by a specific error class (e.g., MPI_ERR_RANK). one of your args is sus 🚩
  • MPI_ERR_OTHER β€” other error; use MPI_Error_string to get more information about this error code. the mystery box of errors πŸ“¦

πŸ‘‘ SEE ALSO πŸ‘‘

  • MPI_Buffer_attach β€” the other half of this duo, fr fr

and that's the whole page, bestie! πŸ’– this one came from the MPI manual, dated 4/10/2026 β€” screenshot it, cite it, live by it. attaching and detaching buffers responsibly since forever. mwah πŸŽ€πŸ’…βœ¨

πŸ“¦ where this page comes from