Download MPI Derived Datatypes-Message Passing Interface Library-Lecture Slides and more Slides Microprocessor and Interfacing in PDF only on Docsity!
Dr. Hanif Durad
Lecture Outline
^
MPI Datatypes
^
Procedure
^
Datatype Construction
^
Type Maps
^
Contiguous Datatype
^
Vector Datatype
^
Extent of a Datatype
^
Structure Datatype
^
Committing a Datatype
IntroMPI.ppt
MPI Datatypes (1/2)
^
Basic types
^
Derived types^ ^
Constructed from existing types (basic and derived)
^
Used in MPI communication routines to transfer high-level,extensive data entities
^
Examples:^
Sub-arrays or “unnatural” array memory striding C structures and Fortran common blocks Large set of general variables Alternative to repeated sends of varied basic types Slow, clumsy, and error prone
Datatype Construction
^
Datatype specified by its type map^
Stencil laid over memory
^
Displacements are offsets (in bytes) from thestarting memory address of the desired data^
MPI_Type_extent function can be used to get size (inbytes)of datatypes
Dr. Hanif Durad
Type Maps
Dr. Hanif Durad
Sample C Program
#include #include /* Run with four processes */void main(int argc, char *argv[]) {int rank;MPI_Status status;struct {int x; int y; int z;} point;MPI_Datatype ptype;MPI_Init(&argc,&argv);MPI_Comm_rank(MPI_COMM_WORLD,&rank);MPI_Type_contiguous(3,MPI_INT,&ptype);MPI_Type_commit(&ptype);if(rank==3){point.x=15; point.y=23; point.z=6;MPI_Send(&point,1,ptype,1,52,MPI_COMM_WORLD);} else if(rank==1) {MPI_Recv(&point,1,ptype,3,52,MPI_COMM_WORLD,&status);printf("P:%d received coords are (%d,%d,%d) \n",rank,point.x,point.y,point.z);} MPI_Finalize();}
Dr. Hanif Durad
contig.c
Sample FORTRANProgram
Dr. Hanif Durad
Vector Datatype Example
Dr. Hanif Durad
Vector Datatype^ int MPI_Type_hvector(int count, int blocklength, MPI_Aint stride,
MPI_Datatype oldtype, MPI_Datatype *newtype)
^
similar to vector, except
stride
is now
calculated in byte-multiples instead of oldtype
-multiples.
eg: oldtype=[xxx], count=3, blocklength=2, stride=7,
newtype=[xxx][xxx]x[xxx][xxx]x[xxx]xxx
MPI_Presentation.ppt H =stands for heterogeneous
Sample C Program
#include void main(int argc, char *argv[]) {int rank,i,j;MPI_Status status;double x[4][8];MPI_Datatype coltype;MPI_Init(&argc, &argv);MPI_Comm_rank(MPI_COMM_WORLD,&rank);MPI_Type_vector(4,1,8,MPI_DOUBLE,&coltype);MPI_Type_commit(&coltype);if(rank==3){ for(i=0;i<4;++i) for(j=0;j<8;++j) x[i][j]=pow(10.0,i+1)+j;MPI_Send(&x[0][7],1,coltype,1,52,MPI_COMM_WORLD); } else if(rank==1) {MPI_Recv(&x[0][2],1,coltype,3,52,MPI_COMM_WORLD,&status);for(i=0;i<4;++i)printf("P:%d my x[%d][2]=%1f\n",rank,i,x[i][2]);} MPI_Finalize();}
Dr. Hanif Durad
vector.c
Sample FORTRANProgram
Dr. Hanif Durad
Structure Datatype (1/2)
^
Use for variables comprised of heterogeneous datatypes^ ^
C structures
^
Fortran common blocks
^
This is the most general derived data type
^
C:^ ^
int MPI_Type_struct (int count, int array_of_blocklengths, MPI_Aintarray_of_displacements, MPI_Datatype array_of_types, MPI_Datatypenewtype)
^
Fortran:^ ^
CALL MPI_TYPE_STRUCT(COUNT,ARRAY_OF_BLOCKLENGTHS,ARRAY_OF_DISPLACEMENTS, ARRAY_OF_TYPES,NEWTYPE,IERROR)
Structure Datatype (2/2)
^
newtype consists of count blocks where the i
th^
block is
array_of_blocklengths[i] copies of the typearray_of_types[i]. The displacement of the i
th^ block (in
bytes) is given by array_of_displacements[i].
Sample C Program (1/2)
#include #includevoid main(int argc, char *argv[]) {int rank,i;MPI_Status status;struct {
int num;float x;double data[4];
} a; int blocklengths[3]={1,1,4};MPI_Datatype types[3]={MPI_INT,MPI_FLOAT,MPI_DOUBLE};MPI_Aint displacements[3];MPI_Datatype restype;MPI_Aint intex,floatex;MPI_Init(&argc,&argv);MPI_Comm_rank(MPI_COMM_WORLD,&rank);
Dr. Hanif Durad
struct.c
Sample C Program (2/2)
MPI_Type_extent(MPI_INT,&intex);MPI_Type_extent(MPI_FLOAT,&floatex);displacements[0]= (MPI_Aint)0; displacements[1]=intex;displacements[2]=intex+floatex;MPI_Type_struct(3,blocklengths,displacements,types,&restype);MPI_Type_commit(&restype);if (rank==3){
a.num=6; a.x=3.14;
for(i=0;i<4;++i) a.data[i]=(double)i;
MPI_Send(&a,1,restype,1,52,MPI_COMM_WORLD);
} else if(rank==1) {
MPI_Recv(&a,1,restype,3,52,MPI_COMM_WORLD,&status);printf("P:%d my a is %d %f %lf %lf %lf %lf\n", rank,a.num,a.x,a.data[0],a.data[1],a.data[2],a.data[3]);
} MPI_Finalize();
Dr. Hanif Durad