Derived Data Types
As previously mentioned, MPI predefines its primitive data types:
MPI also provides facilities for you to define your own data structures based upon sequences of the MPI primitive data types. Such user defined structures are called derived data types.
Primitive data types are contiguous. Derived data types allow you to specify non-contiguous data in a convenient manner and to treat it as though it was contiguous.
MPI provides several methods for constructing derived data types:
- Contiguous
- Vector
- Indexed
- Struct
Derived Data Type Routines
The simplest constructor. Produces a new data type by making count copies of an existing data type.
MPI_Type_contiguous (count,oldtype,&newtype)
MPI_TYPE_CONTIGUOUS (count,oldtype,newtype,ierr)
Similar to contiguous, but allows for regular gaps (stride) in the displacements. MPI_Type_hvector is identical to MPI_Type_vector except that stride is specified in bytes.
MPI_Type_vector (count,blocklength,stride,oldtype,&newtype)
MPI_TYPE_VECTOR (count,blocklength,stride,oldtype,newtype,ierr)
An array of displacements of the input data type is provided as the map for the new data type. MPI_Type_hindexed is identical to MPI_Type_indexed except that offsets are specified in bytes.
MPI_Type_indexed (count,blocklens[],offsets[],old_type,&newtype)
MPI_TYPE_INDEXED (count,blocklens(),offsets(),old_type,newtype,ierr)
The new data type is formed according to completely defined map of the component data types.
NOTE: This function is deprecated in MPI-2.0 and replaced by MPI_Type_create_struct in MPI-3.0
MPI_Type_struct (count,blocklens[],offsets[],old_types,&newtype)
MPI_TYPE_STRUCT (count,blocklens(),offsets(),old_types,newtype,ierr)
Returns the size in bytes of the specified data type. Useful for the MPI subroutines that require specification of offsets in bytes.
NOTE: This function is deprecated in MPI-2.0 and replaced by MPI_Type_get_extent in MPI-3.0
MPI_Type_extent (datatype,&extent)
MPI_TYPE_EXTENT (datatype,extent,ierr)
Commits new datatype to the system. Required for all user constructed (derived) datatypes.
MPI_Type_commit (&datatype)
MPI_TYPE_COMMIT (datatype,ierr)
Deallocates the specified datatype object. Use of this routine is especially important to prevent memory exhaustion if many datatype objects are created, as in a loop.
MPI_Type_free (&datatype)
MPI_TYPE_FREE (datatype,ierr)
Examples
Examples: Contiguous Derived Data Type
Create a data type representing a row of an array and distribute a different row to all processes.

C and Fortran code examples here.
Examples: Vector Derived Data Type
Create a data type representing a column of an array and distribute different columns to all processes.

C and Fortran code examples here.
Examples: Indexed Derived Data Type
Create a datatype by extracting variable portions of an array and distribute to all tasks.

C and Fortran code examples here.
Examples: Struct Derived Data Type
Create a data type that represents a particle and distribute an array of such particles to all processes.

C and Fortran code examples here.




