ok so I am trying to send a structure like so over MPI
struct BColumns {
double **B;
int offset;
};
And if I just do some BS allocation of data like so
bSet.offset = myRank;
bSet.B = (double **) calloc(2, sizeof(double *));
bSet.B[0] = (double *) calloc(1, sizeof(double));
bSet.B[1] = (double *) calloc(1, sizeof(double));
bSet.B[0][0] = 1;
bSet.B[1][0] = 2;
if(myRank == 0){
MPI_Send(&bSet,sizeof(struct BColumns), MPI_BYTE, 1, 1, MPI_COMM_WORLD);
}else{
MPI_Recv(&recvBuf, sizeof(struct BColumns), MPI_BYTE, MPI_ANY_SOURCE, MPI_ANY_TAG, MPI_COMM_WORLD, &status );
}
And I am assuming that its not going to work well because if I send this structure as is it will just send the pointer in B and that pointer doesn’t point to anything on the other processor, so how would I go about sending data like this in MPI.
As suszterpatt points out, you really want to allocate your block of
Bin one big chunk; that’s probably better for performance anyway but it’s really required for any communications so you’re not chasing pointers everywhere. And I think one way or another you’re probably going to have to do it in different sends — sending size information, then the data in one chunk — although you could probably create and delete a different MPI_Type_struct for every one of these you send. But using multiple sends per object isn’t very hard:And then running it:
You could marshall that data into one message if you wanted to avoid the latency of several sends (and if you knew before hand a maximum size for the B array) either by hand or using MPI function calls or data types, but you’d still have to do it in a similar way.