MPI_Scan(3)

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

✨ MPI_Scan(3) ✨ β€” the MPI manual, section 3, bestie edition πŸ’–

okay so real quick, this page lives in the MPI manual (section 3), and it's about one lil function that does a whole lot of heavy lifting. gather round.

🌸 NAME 🌸

MPI_Scan β€” computes the scan (partial reductions) of data on a collection of processes 🫢

like, everybody sends something in, and everybody gets back the combined result of themselves and everyone before them. it's giving relay race but the baton is math. πŸ’…


✨ SYNOPSIS ✨

int MPI_Scan(const void *sendbuf, void *recvbuf, int count,
MPI_Datatype datatype, MPI_Op op, MPI_Comm comm)
int MPI_Scan_c(const void *sendbuf, void *recvbuf, MPI_Count count,
MPI_Datatype datatype, MPI_Op op, MPI_Comm comm)

two flavors, both iconic. the _c one takes an MPI_Count instead of an int, no cap fr fr. πŸ‘‘


πŸ’– INPUT PARAMETERS πŸ’–

  • sendbuf β€” starting address of send buffer (choice). this is what you are tossing into the pot πŸ“
  • count β€” number of elements in input buffer (non-negative integer). no negative counts, we don't do that here
  • datatype β€” data type of elements of input buffer (handle)
  • op β€” operation (handle). this is the math you want done ✨
  • comm β€” communicator (handle). the group chat you're doing this in πŸ’¬

πŸŽ€ OUTPUT PARAMETERS πŸŽ€

  • recvbuf β€” starting address of receive buffer (choice). this is where your partial reduction lands, bestie

🌈 NOTES ON COLLECTIVE OPERATIONS 🌈

so the reduction functions (MPI_Op) do not return an error value. the way that they just… can't. 😭 so if these functions detect an error, literally all they can do is either call MPI_Abort or silently skip the problem. that's it. that's the whole toolkit.

so if you change the error handler from MPI_ERRORS_ARE_FATAL to something else, for example MPI_ERRORS_RETURN, then no error may be indicated. like. at all.

and why?? the reason for this is the performance problems in ensuring that all collective routines return the same error value. it's a trade-off and this is the trade. πŸ’€


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

good news: this routine is thread-safe. that means you can safely use it from multiple threads without needing any user-provided thread locks. no mutex babysitting required, we love to see it πŸ’…βœ¨

but β€” and I need you to hear me β€” the routine is not interrupt safe. typically this is due to the use of memory allocation routines such as malloc or other non-MPICH runtime routines that are themselves not interrupt-safe. so like, don't interrupt it mid-glow-up. πŸ™ˆ


🧚 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. periodt. πŸ’œ


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

the error handler may be changed with MPI_Comm_set_errhandler (for communicators), MPI_File_set_errhandler (for files), and MPI_Win_set_errhandler (for RMA windows). the MPI-1 routine MPI_Errhandler_set may be used but its use is deprecated πŸ™… (do not, 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. they're trying their best fr fr 🫢

  • MPI_SUCCESS β€” No error; MPI routine completed successfully. πŸŽ‰ slay
  • MPI_ERR_BUFFER β€” Invalid buffer pointer. Usually a null buffer where one is not valid.
  • MPI_ERR_COMM β€” Invalid communicator. A common error is to use a null communicator in a call (not even allowed in MPI_Comm_rank).
  • MPI_ERR_COUNT β€” Invalid count argument. Count arguments must be non-negative; a count of zero is often valid.
  • MPI_ERR_OP β€” Invalid operation. MPI operations (objects of type MPI_Op) must either be one of the predefined operations (e.g., MPI_SUM) or created with MPI_Op_create.
  • MPI_ERR_TYPE β€” Invalid datatype argument. Additionally, this error can occur if an uncommitted MPI_Datatype (see MPI_Type_commit) is used in a communication call.
  • MPI_ERR_OTHER β€” Other error; use MPI_Error_string to get more information about this error code.

and that's the whole page, bestie πŸŽ€ this one came from the MPI crew, dated 4/10/2026, and it's filed under MPI_Scan(3). go forth and reduce, you absolute main character. πŸ’…πŸ’–πŸŒˆ

πŸ“¦ where this page comes from