#include #include #include #include #define V_LEN 10 //Vector Length #define E_CNT 10 //Element count MPI_Op MPI_MySum; //Custom Sum function MPI_Datatype MPI_MyType;//We need this MPI Datatype to make MPI aware of our custom structure int i,j,true=1; int totalnodes,mynode; typedef struct CustomType_t { float feat[V_LEN]; //Some vector of float float distc; //An independant float value int number; //A counter of a different type } CustomType; CustomType *SharedStruct; void construct_MyType(void){ int i; CustomType p; int BlockLengths[3] = {V_LEN,1,1}; MPI_Aint Displacement[3]; MPI_Datatype types[3] = {MPI_FLOAT, MPI_FLOAT, MPI_INT}; /* Compute relative displacements w/r to the Type's begining address * using portable technique * */ MPI_Get_address(&p.feat[0],&Displacement[0]); MPI_Get_address(&p.distc ,&Displacement[1]); MPI_Get_address(&p.number ,&Displacement[2]); // Displacement is RELATIVE to it's first structure element! for(i=2; i >= 0; i--) Displacement[i] -= Displacement[0]; // It is good practice to include this in case // the compiler pads your data structure /* BlockLengths[3] = 1; types[3] = MPI_UB; Displacement[3] = sizeof(CustomType); */ MPI_Type_create_struct(3, BlockLengths, Displacement, types, &MPI_MyType); MPI_Type_commit(&MPI_MyType); // important!! return; } void MySum(CustomType *cin, CustomType *cinout, int *len, MPI_Datatype *dptr) { int i,j; // Some sanity check printf("\nIn MySum, Node %d with len=%d\n",mynode,*len); if(*dptr != MPI_MyType) { printf("Invalid datatype\n"); MPI_Abort(MPI_COMM_WORLD, 3); } for(i=0; i < *len; i++) { cinout[i].distc +=cin[i].distc; cinout[i].number+=cin[i].number; for(j=0; j