MPI_TYPE_CREATE_SUBARRAY(3)

πŸ¦‹ MPI_Type_create_subarray πŸ’…

okay so bestie, we're in the Open MPI manual, section 3 β€” that's the MPI_TYPE_CREATE_SUBARRAY(3) energy β€” and this page is about one absolutely iconic constructor. lowkey it does exactly what it says on the tin: it creates a data type describing an n-dimensional subarray of an n-dimensional array. no cap. 🫢


✨ SYNTAX ✨

πŸ’» C Syntax

#include <mpi.h>

int MPI_Type_create_subarray(int ndims, const int array_of_sizes[],
    const int array_of_subsizes[], const int array_of_starts[],
    int order, MPI_Datatype oldtype, MPI_Datatype *newtype)

🧚 Fortran Syntax

USE MPI
! or the older form: INCLUDE 'mpif.h'
MPI_TYPE_CREATE_SUBARRAY(NDIMS, ARRAY_OF_SIZES, ARRAY_OF_SUBSIZES,
     ARRAY_OF_STARTS, ORDER, OLDTYPE, NEWTYPE, IERROR)

     INTEGER NDIMS, ARRAY_OF_SIZES(*), ARRAY_OF_SUBSIZES(*),
     ARRAY_OF_STARTS(*), ORDER, OLDTYPE, NEWTYPE, IERROR

🌈 Fortran 2008 Syntax

USE mpi_f08
MPI_Type_create_subarray(ndims, array_of_sizes, array_of_subsizes,
             array_of_starts, order, oldtype, newtype, ierror)
     INTEGER, INTENT(IN) :: ndims, array_of_sizes(ndims),
     array_of_subsizes(ndims), array_of_starts(ndims), order
     TYPE(MPI_Datatype), INTENT(IN) :: oldtype
     TYPE(MPI_Datatype), INTENT(OUT) :: newtype
     INTEGER, OPTIONAL, INTENT(OUT) :: ierror

πŸŽ€ INPUT PARAMETERS πŸŽ€

  • ndims β€” the number of array dimensions. it's a positive integer, so no 0, okay? be so serious. πŸ’€
  • array_of_sizes β€” how many elements of type oldtype live in each dimension of the full array (it's an array of positive integers).
  • array_of_subsizes β€” how many elements of type oldtype live in each dimension of the subarray (also an array of positive integers).
  • array_of_starts β€” the starting coordinates of the subarray in each dimension (an array of nonnegative integers, please and thank you).
  • order β€” array storage order flag (state).
  • oldtype β€” the array element data type (handle).

πŸ’– OUTPUT PARAMETERS πŸ’–

  • newtype β€” the new data type (handle). the glow-up result. ✨
  • ierror β€” Fortran only: error status (integer).

πŸ‘‘ DESCRIPTION πŸ‘‘

the subarray type constructor creates an MPI data type describing an n-dimensional subarray of an n-dimensional array. the subarray can be situated anywhere within the full array, and it can be of any nonzero size up to the size of the larger array β€” as long as it stays confined inside that array, bestie. no escaping. this type constructor makes it easy to create file types to access arrays that are distributed in blocks among processes into a single file holding the global array. it's giving glow-up for I/O. πŸ”₯

this type constructor can handle arrays with an arbitrary number of dimensions and works for both C- and Fortran-ordered matrices (that is, row-major or column-major). and note that a C program may use Fortran order and a Fortran program may use C order πŸ‘€ β€” mix and match, it's your world.

the ndims parameter specifies the number of dimensions in the full data array and gives the number of elements in array_of_sizes, array_of_subsizes, and array_of_starts.

the number of elements of type oldtype in each dimension of the n-dimensional array and the requested subarray are specified by array_of_sizes and array_of_subsizes, respectively. for any dimension i, it is erroneous to specify array_of_subsizes[i] < 1 or array_of_subsizes[i] > array of sizes[i].

the array_of_starts contains the starting coordinates of each dimension of the subarray. arrays are assumed to be indexed starting from zero. for any dimension i, it is erroneous to specify

array_of_starts[i] < 0

or

array_of_starts[i] > (array_of_sizes[i] - array_of_subsizes[i])

the order argument specifies the storage order for the subarray as well as the full array. it must be set to one of the following:

  • MPI_ORDER_C β€” the ordering used by C arrays, (that is, row-major order).
  • MPI_ORDER_FORTRAN β€” the ordering used by Fortran arrays, (that is, column-major order).

a ndims-dimensional subarray (newtype) with no extra padding can be defined by the function Subarray() as follows:

newtype = Subarray(ndims, {size_0, size_1,..., size_ndims-1},
          {subsize_0, subsize_1, ..., subsize_ndims-1},
          {start_0, start_1, ..., start_ndims-1}, oldtype)

let the typemap of oldtype have the form:

{(type_0, disp_0), (type_1, disp_1), ..., (type_n-1, disp_n-1)}

where typei is a predefined MPI data type, and let ex be the extent of oldtype.

the Subarray() function is defined recursively in three equations on page 72 of the MPI-2 standard. (iykyk, go read it.)

for an example use of MPI_Type_create_subarray in the context of I/O, see Section 9.9.2 of the MPI-2 standard. ✨


πŸ“ NOTES πŸ“

in a Fortran program with arrays indexed starting from 1, if the starting coordinate of a particular dimension of the subarray is n, then the entry in array of starts for that dimension is n-1. yes this is the classic off-by-one lifestyle, stay woke. πŸ¦‹


😭 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.

Open MPI includes three predefined error handlers that can be used:

  • MPI_ERRORS_ARE_FATAL β€” causes the program to abort all connected MPI processes.
  • 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 if MPI_Abort was called on that communicator. if called on a window or file, acts as if MPI_Abort was 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_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… don't count on it. πŸ’€

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.


2003-2026, The Open MPI Community


okay diva, that's the whole page πŸ’œ β€” the footer says it all: this one's dated Sep 10, 2026, it's MPI_TYPE_CREATE_SUBARRAY(3) in the Open MPI manual, and it's copyright 2003-2026, The Open MPI Community. go build that subarray, bestie, you're the main character πŸŒΈπŸ‘‘

πŸ“¦ where this page comes from