I keep getting bugs in the program that I'm working on with MPI Derived types, so I made a small program to demonstrate the error and hopefully someone can help me.
Code:
typedef struct{
double a[3];
double b[3];
int index;
int tag;
} NewType;
int main(int argc, char** argv){
int nprocs, myrank;
MPI_Datatype MPI_NewType;
MPI_Setup(&myrank, &nprocs, &MPI_NewType, &MPI_Neighbor);
int s = 20;
if (myrank == 0){
int i;
NewType p[s];
for (i = 0; i < s; ++i){
p[i].index = 111111;
}
MPI_Send(p, s, MPI_NewType, 1, 0, MPI_COMM_WORLD);
}
else {
int i;
NewType p[s];
for (i = 0; i < s; ++i){
p[i].index = -1;
}
MPI_Recv(p, s, MPI_NewType, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
for (i = 0; i < s; ++i) {
printf("NewType %d: %d\n", i, p[i].index);
}
}
MPI_Cleanup(&MPI_NewType);
}
In summary, node 0 creates an array of NewTypes, initializes all their index fields to 111111 and sends them to node 1, which receives and prints all the indexes out.
MPI_Setup and MPI_Cleanup are where the init/finalize stuff is handled as well as the creation of the types. Here is the method that creates and commits the MPI_NewType type. Its where I suspect the problem is.
Code:
void create_type_MPI_NewType(MPI_Datatype* datatype){
int array_of_block_lengths[4] = {3, 3, 1, 1};
MPI_Aint a_addr, b_addr, c_addr, d_addr;
NewType p;
MPI_Get_address(&p.a, &a_addr);
MPI_Get_address(&p.b, &b_addr);
MPI_Get_address(&p.index, &c_addr);
MPI_Get_address(&p.tag, &d_addr);
MPI_Aint array_of_displacements[4] = { 0, b_addr - a_addr, c_addr - b_addr, d_addr - c_addr};
MPI_Datatype array_of_types[4] = {MPI_DOUBLE, MPI_DOUBLE, MPI_INT, MPI_INT};
MPI_Type_create_struct(4, array_of_block_lengths, array_of_displacements, array_of_types, datatype);
MPI_Type_commit(datatype);
}
Anyway, the output from the above program:
NewType 0: 111111
NewType 1: 111111
NewType 2: 111111
NewType 3: 111111
NewType 4: 111111
NewType 5: 111111
NewType 6: 111111
NewType 7: 111111
NewType 8: 111111
NewType 9: 111111
NewType 10: 111111
NewType 11: 111111
NewType 12: 111111
NewType 13: 111111
NewType 14: 111111
NewType 15: 111111
NewType 16: 111111
NewType 17: -1
NewType 18: -1
NewType 19: -1
If I increase the size of the array, the number of fields that aren't filled in at the end increases (if I increase to 40, I get 6 -1's at the end). Makes me think there is something wrong with the length or displacement when I'm creating the type. Anyway, I've been staring at this for hours and can't catch the error. I could use a fresh eye.