I'm learning MPI code. I am trying to do a pipelined ring broadcast using different sized chunks. However, when I run my code, it reaches a deadlock while Process 0 attempts to send the second chunk of data, and I have no idea why. Any help would be appreciated.
NOTE: this is part of a much larger code. It fills a buffer with chars on Process 0. After some simple debugging using print statements, I believe there is something wrong with line 9 (marked with an ***) because that's where the program stalls. The second chunk of data is never sent from Process 0.
int offset;
MPI_Status status;
if (rank == 0) {
offset = 0;
while (offset < NUM_BYTES) {
MPI_Send(&chunk_size, 1, MPI_INT, rank + 1, 3, MPI_COMM_WORLD);
MPI_Send(&offset, 1, MPI_INT, rank + 1, 2, MPI_COMM_WORLD);
MPI_Send(&buffer[offset], chunk_size, MPI_BYTE, rank + 1, 1, MPI_COMM_WORLD); ***
offset = offset + chunk_size;
if ((offset + chunk_size) >= NUM_BYTES) {
chunk_size = (NUM_BYTES - offset);
}
}
}
else {
MPI_Recv(&chunk_size, 1, MPI_INT, rank - 1, 3, MPI_COMM_WORLD, &status);
MPI_Recv(&offset, 1, MPI_INT, rank - 1, 2, MPI_COMM_WORLD, &status);
MPI_Recv(&buffer[offset], chunk_size, MPI_BYTE, rank - 1, 1, MPI_COMM_WORLD, &status);
if (rank != num_procs - 1) {
MPI_Send(&chunk_size, 1, MPI_INT, rank + 1, 3, MPI_COMM_WORLD);
MPI_Send(&offset, 1, MPI_INT, rank + 1, 2, MPI_COMM_WORLD);
MPI_Send(&buffer[offset], chunk_size, MPI_BYTE, rank + 1, 1, MPI_COMM_WORLD);
}
}