its.unc.edu 1 derived datatypes research computing unc - chapel hill instructor: mark reed email:...
Post on 21-Dec-2015
216 views
TRANSCRIPT
![Page 1: Its.unc.edu 1 Derived Datatypes Research Computing UNC - Chapel Hill Instructor: Mark Reed Email: markreed@unc.edu](https://reader035.vdocument.in/reader035/viewer/2022062516/56649d645503460f94a46f0f/html5/thumbnails/1.jpg)
its.unc.edu 1
Derived DatatypesDerived
Datatypes
Research Computing UNC - Chapel Hill
Instructor: Mark ReedEmail: [email protected]
![Page 2: Its.unc.edu 1 Derived Datatypes Research Computing UNC - Chapel Hill Instructor: Mark Reed Email: markreed@unc.edu](https://reader035.vdocument.in/reader035/viewer/2022062516/56649d645503460f94a46f0f/html5/thumbnails/2.jpg)
its.unc.edu 2
MPI DatatypesMPI Datatypes
MPI Primitive Datatypes•MPI_Int, MPI_Float, MPI_INTEGER,
etc. Derived Datatypes - can be
constructed by four methods:•contiguous
•vector
•indexed
•struct
![Page 3: Its.unc.edu 1 Derived Datatypes Research Computing UNC - Chapel Hill Instructor: Mark Reed Email: markreed@unc.edu](https://reader035.vdocument.in/reader035/viewer/2022062516/56649d645503460f94a46f0f/html5/thumbnails/3.jpg)
its.unc.edu 3
Derived DatatypesDerived Datatypes
Roll your own …
Create your own types to suit your application.
• convenience
• efficiency
![Page 4: Its.unc.edu 1 Derived Datatypes Research Computing UNC - Chapel Hill Instructor: Mark Reed Email: markreed@unc.edu](https://reader035.vdocument.in/reader035/viewer/2022062516/56649d645503460f94a46f0f/html5/thumbnails/4.jpg)
its.unc.edu 4
Derived datatypesDerived datatypes
You can define new data structures based upon sequences of the MPI primitive data types, these are 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.
Derived datatypes can be used in all send and receive operations including collective.
![Page 5: Its.unc.edu 1 Derived Datatypes Research Computing UNC - Chapel Hill Instructor: Mark Reed Email: markreed@unc.edu](https://reader035.vdocument.in/reader035/viewer/2022062516/56649d645503460f94a46f0f/html5/thumbnails/5.jpg)
its.unc.edu 5
Type MapsType Maps
A derived datatype specifies two things: •A sequence of primitive datatypes
•A sequence of integer (byte) displacements, measured from the beginning of the buffer.
Displacements are not required to be positive, distinct, or in increasing order. (however, negative displacements will precede the buffer)
Order of items need not coincide with their order in memory, and an item may appear more than once.
![Page 6: Its.unc.edu 1 Derived Datatypes Research Computing UNC - Chapel Hill Instructor: Mark Reed Email: markreed@unc.edu](https://reader035.vdocument.in/reader035/viewer/2022062516/56649d645503460f94a46f0f/html5/thumbnails/6.jpg)
its.unc.edu 6
Type Map Type Map
Primitive datatype 0 Displacement of 0
Primitive datatype 1 Displacement of 1
... ...
Primitve datatype n-1 Displacement of n-1
![Page 7: Its.unc.edu 1 Derived Datatypes Research Computing UNC - Chapel Hill Instructor: Mark Reed Email: markreed@unc.edu](https://reader035.vdocument.in/reader035/viewer/2022062516/56649d645503460f94a46f0f/html5/thumbnails/7.jpg)
its.unc.edu 7
Type SignatureType Signature
The sequence of primitive datatypes (i.e. displacements ignored) is the type signature of the datatype.
So a type map of
•{(double,0),(int,8),(char, 12)}
has a type signature of
•{double, int, char}
![Page 8: Its.unc.edu 1 Derived Datatypes Research Computing UNC - Chapel Hill Instructor: Mark Reed Email: markreed@unc.edu](https://reader035.vdocument.in/reader035/viewer/2022062516/56649d645503460f94a46f0f/html5/thumbnails/8.jpg)
its.unc.edu 8
ExtentExtent
The extent of a datatype is defined as:
•the span from the first byte to the last byte occupied by entries in this datatype (rounded up to satisfy alignment requirements)
Example: Type={(double,0),(char,8)} i.e. offsets of 0 and 8 respectively. Now assume that doubles are aligned strictly at addresses that are multiples of 8
•extent = 16 (9 rounds to next multiple of 8)
![Page 9: Its.unc.edu 1 Derived Datatypes Research Computing UNC - Chapel Hill Instructor: Mark Reed Email: markreed@unc.edu](https://reader035.vdocument.in/reader035/viewer/2022062516/56649d645503460f94a46f0f/html5/thumbnails/9.jpg)
its.unc.edu 9MPI Derived Datatypes
Datatype Interrogators
Datatype Interrogators
MPI_Type_extent (MPI_Datatype datatype, MPI_Aint *extent)
• datatype - primitive or derived datatype
• extent - returns extent of datatype in bytes
MPI_Type_size (MPI_Datatype datatype, int *size)
• datatype - primitive or derived datatype
• size - returns size in bytes of the entries in the type signature of datatype. i.e. total size of a message with this datatype, thus gaps
don’t contribute to size
![Page 10: Its.unc.edu 1 Derived Datatypes Research Computing UNC - Chapel Hill Instructor: Mark Reed Email: markreed@unc.edu](https://reader035.vdocument.in/reader035/viewer/2022062516/56649d645503460f94a46f0f/html5/thumbnails/10.jpg)
its.unc.edu 10
Committing datatypes
Committing datatypes
MPI_Type_commit (MPI_Datatype *datatype)
Required for all user defined datatypes before it can be used in communication
Subsequently can use in any function call where an MPI_Datatype is specified
![Page 11: Its.unc.edu 1 Derived Datatypes Research Computing UNC - Chapel Hill Instructor: Mark Reed Email: markreed@unc.edu](https://reader035.vdocument.in/reader035/viewer/2022062516/56649d645503460f94a46f0f/html5/thumbnails/11.jpg)
its.unc.edu 11
Datatype Constructors
Datatype Constructors
Contiguous
Vector
Indexed
Struct
![Page 12: Its.unc.edu 1 Derived Datatypes Research Computing UNC - Chapel Hill Instructor: Mark Reed Email: markreed@unc.edu](https://reader035.vdocument.in/reader035/viewer/2022062516/56649d645503460f94a46f0f/html5/thumbnails/12.jpg)
its.unc.edu 12
ContiguousContiguous
MPI_Type_contiguous (int count, MPI_Datatype oldtype, MPI_Datatype *newtype)
Contiguous is the simplest constructor.
newtype is the datatype obtained by concatenating count copies of oldtype into contiguous locations.
Concatenation is defined using extent (oldtype) as the size of the concatenated copies
![Page 13: Its.unc.edu 1 Derived Datatypes Research Computing UNC - Chapel Hill Instructor: Mark Reed Email: markreed@unc.edu](https://reader035.vdocument.in/reader035/viewer/2022062516/56649d645503460f94a46f0f/html5/thumbnails/13.jpg)
its.unc.edu 13
VectorVector
MPI_Type_vector (int count, int blocklength, int stride,
MPI_Datatype oldtype, MPI_Datatype *newtype)
•count - number of blocks
•blocklength - number of elements in each block
•stride - spacing between start of each block, measured as number of elements
![Page 14: Its.unc.edu 1 Derived Datatypes Research Computing UNC - Chapel Hill Instructor: Mark Reed Email: markreed@unc.edu](https://reader035.vdocument.in/reader035/viewer/2022062516/56649d645503460f94a46f0f/html5/thumbnails/14.jpg)
its.unc.edu 14
VectorVector
Vector is a constructor that allows replication of a datatype into locations that consist of equally spaced blocks.
Each block is obtained by concatenating the same number of copies of the old datatype.
The spacing between blocks is a multiple of the extent of the old datatype.
![Page 15: Its.unc.edu 1 Derived Datatypes Research Computing UNC - Chapel Hill Instructor: Mark Reed Email: markreed@unc.edu](https://reader035.vdocument.in/reader035/viewer/2022062516/56649d645503460f94a46f0f/html5/thumbnails/15.jpg)
its.unc.edu 15
VectorVector
oldtype
Count = 3, Blocklength=2, Stride=3
newtype
![Page 16: Its.unc.edu 1 Derived Datatypes Research Computing UNC - Chapel Hill Instructor: Mark Reed Email: markreed@unc.edu](https://reader035.vdocument.in/reader035/viewer/2022062516/56649d645503460f94a46f0f/html5/thumbnails/16.jpg)
its.unc.edu 16
HvectorHvector
MPI_Type_hvector (int count, int blocklength, MPI_Aint stride, MPI_Datatype oldtype, MPI_Datatype *newtype)
Same as vector except stride is measured in bytes rather than as a multiple of the oldtype extent.
H is for heterogeneous
![Page 17: Its.unc.edu 1 Derived Datatypes Research Computing UNC - Chapel Hill Instructor: Mark Reed Email: markreed@unc.edu](https://reader035.vdocument.in/reader035/viewer/2022062516/56649d645503460f94a46f0f/html5/thumbnails/17.jpg)
its.unc.edu 17
Example: section of 2D arrayExample: section of 2D array
Send the gold blocks
![Page 18: Its.unc.edu 1 Derived Datatypes Research Computing UNC - Chapel Hill Instructor: Mark Reed Email: markreed@unc.edu](https://reader035.vdocument.in/reader035/viewer/2022062516/56649d645503460f94a46f0f/html5/thumbnails/18.jpg)
its.unc.edu 18
Example CodeExample Code
Real a(6,5), e(3,3)
INTEGER oneslice, twoslice, sizeofreal, myrank, ierr
INTEGER status(MPI_STATUS_SIZE)
c extract the section a(1:6:2,1:5:2) and store in e
…
call MPI_TYPE_EXTENT(MPI_REAL, sizeofreal, ierr)
c create datatype for a 1D section
CALL MPI_TYPE_VECTOR (3,1,2,MPI_REAL,oneslice,ierr)
c create dataype for a 2D section
c Note: oneslice extent=5 blocks, and twoslice=29
call MPI_TYPE_HVECTOR (3,1,12*sizeofreal,oneslice,twoslice,ierr)
call MPI_TYPE_COMMIT(twoslice, ierr)
c send and receive on the same process
call MPI_SENDRECV(a(1,1),1,twoslice,myrank,0,e,9, MPI_REAL,myrank,0,MPI_COMM_WORLD,status,ierr)
![Page 19: Its.unc.edu 1 Derived Datatypes Research Computing UNC - Chapel Hill Instructor: Mark Reed Email: markreed@unc.edu](https://reader035.vdocument.in/reader035/viewer/2022062516/56649d645503460f94a46f0f/html5/thumbnails/19.jpg)
its.unc.edu 19
IndexedIndexed
The Indexed constructor allows one to specify a noncontiguous data layout where displacements between successive blocks need not be equal.
This allows one to gather arbitrary entries from an array and send them in one message, or receive one message and scatter the received entries into arbitrary locations in an array.
Hindexed version is available as well
![Page 20: Its.unc.edu 1 Derived Datatypes Research Computing UNC - Chapel Hill Instructor: Mark Reed Email: markreed@unc.edu](https://reader035.vdocument.in/reader035/viewer/2022062516/56649d645503460f94a46f0f/html5/thumbnails/20.jpg)
its.unc.edu 20
StructStruct
MPI_TYPE_STRUCT is the most general type constructor.
Generalizes MPI_TYPE_HINDEXED in that it allows each block to consist of replications of different datatypes.
Intent is to allow descriptions of arrays of structures, as a single datatype.
![Page 21: Its.unc.edu 1 Derived Datatypes Research Computing UNC - Chapel Hill Instructor: Mark Reed Email: markreed@unc.edu](https://reader035.vdocument.in/reader035/viewer/2022062516/56649d645503460f94a46f0f/html5/thumbnails/21.jpg)
its.unc.edu 21
DeallocationDeallocation
MPI_Type_free (MPI_Datatype *datatype)
MPI_TYPE_FREE marks the datatype object for deallocation and sets datatype to MPI_DATATYPE_NULL.
Any communication that is currently using this datatype will complete normally.
Derived datatypes that were defined from the freed datatype are not affected
![Page 22: Its.unc.edu 1 Derived Datatypes Research Computing UNC - Chapel Hill Instructor: Mark Reed Email: markreed@unc.edu](https://reader035.vdocument.in/reader035/viewer/2022062516/56649d645503460f94a46f0f/html5/thumbnails/22.jpg)
its.unc.edu 22
Pack and UnpackPack and Unpack
Can usually be avoided by using derived datatypes
pack/unpack routines are provided for compatibility with previous libraries, e.g. PVM and Parmacs
Provide some functionality that is not otherwise available in MPI. •For instance, a message can be received in
several parts, where the receive operation done on a later part may depend on the content of a former part
![Page 23: Its.unc.edu 1 Derived Datatypes Research Computing UNC - Chapel Hill Instructor: Mark Reed Email: markreed@unc.edu](https://reader035.vdocument.in/reader035/viewer/2022062516/56649d645503460f94a46f0f/html5/thumbnails/23.jpg)
its.unc.edu 23
Packing vs Derived Datatypes
Packing vs Derived Datatypes
Use of derived datatypes is generally recommended
The use of derived datatypes will often lead to improved performance: data copying can be avoided, and information on data layout can be reused, when the same communication buffer is reused.
Packing may result in more efficient code in situations where the sender has to communicate to the receiver information that affects the layout of the receive buffer
![Page 24: Its.unc.edu 1 Derived Datatypes Research Computing UNC - Chapel Hill Instructor: Mark Reed Email: markreed@unc.edu](https://reader035.vdocument.in/reader035/viewer/2022062516/56649d645503460f94a46f0f/html5/thumbnails/24.jpg)
its.unc.edu 24
MPI_PackMPI_Pack
int MPI_Pack(void* inbuf, int incount, MPI_Datatype datatype, void *outbuf, int outcount, int *position, MPI_Comm comm)
•inbuf - input buffer start
•incount - number of input data items
•datatype - datatype of each input data item
•outbuf - output buffer start
•outcount - output buffer size, in bytes
•position - current position in buffer, in bytes
•comm - communicator for packed message
![Page 25: Its.unc.edu 1 Derived Datatypes Research Computing UNC - Chapel Hill Instructor: Mark Reed Email: markreed@unc.edu](https://reader035.vdocument.in/reader035/viewer/2022062516/56649d645503460f94a46f0f/html5/thumbnails/25.jpg)
its.unc.edu 25
MPI_UnpackMPI_Unpack
int MPI_Unpack(void* inbuf, int insize, int *position, void *outbuf, int outcount, MPI_Datatype datatype, MPI_Comm comm)
•inbuf - input buffer start
•insize - size of input buffer, in bytes
•position - current position in buffer, in bytes
•outbuf - output buffer start
•outcount - number of items to be unpacked
•datatype - datatype of each output data item
•comm - communicator for packed message
![Page 26: Its.unc.edu 1 Derived Datatypes Research Computing UNC - Chapel Hill Instructor: Mark Reed Email: markreed@unc.edu](https://reader035.vdocument.in/reader035/viewer/2022062516/56649d645503460f94a46f0f/html5/thumbnails/26.jpg)
its.unc.edu 26
Example: Mandelbrot Set
Example: Mandelbrot Set
zn = zn-1*zn-1 + c For Mandelbrot set z0 = 0;
•thus z1 = c; z2 = c*c + c If |zn| converges for c (i.e. < 2), then c
is in the set and color it black, else color is chosen based on the value of n for which it diverges (exceeds 2)
“naturally” or “embarrassingly” parallel app since calculations are independent but there is a problem … what is it?
![Page 27: Its.unc.edu 1 Derived Datatypes Research Computing UNC - Chapel Hill Instructor: Mark Reed Email: markreed@unc.edu](https://reader035.vdocument.in/reader035/viewer/2022062516/56649d645503460f94a46f0f/html5/thumbnails/27.jpg)
its.unc.edu 27
Mandlebrot Problem?Mandlebrot Problem?
Load-balancing! Solution: partition the data set into a large number of squares and denote one processor (master) to parcel them out
View of Entire Mandlebrot set
See pmandel.c by Ed Karrels in the mpich distribution