0

Quite new to MPI, I want every process to broadcast its array to other processes.

nproc is the number of processes in total (3), myid is the current processes' id and array has size DATASIZE.

What confuses me here is receiving an array from root may occur before root actually broadcasts its array. How does receiving even work in this case?

For example:

Process #0 receives array {1, 2} from Process #1 happens before Process #1 broadcasts its array.

for(int i = 0; i < nproc; i++){
root = i;
if(root == myid){
    printf("Process #%d broadcasts its array {", root);
    printArray(array);
    printf("}\n");
    MPI_Bcast(array, DATASIZE, MPI_INT, root, MPI_COMM_WORLD);
}
else{
    MPI_Bcast(array, DATASIZE, MPI_INT, root, MPI_COMM_WORLD);
    printf("Process #%d receives array {", myid);
    printArray(array);
    printf("} from Process #%d\n", root);
}
}
Den
  • 37
  • 5
  • `MPI_Bcast()` has a very clear semantic, and if `root` has not yet broadcasted the message, then all other task wait, so what are you confused with ? note each task needs `nproc` arrays (one to send, `nprocs-1` to receive), so you might want to adjust your example. An other approach is to call `MPI_Alltoall[v]()` once. – Gilles Gouaillardet May 11 '18 at 06:10
  • I expect "Process #0 receives array {1, 2} from Process #1" to not happen before "Process #1 broadcasts its array", it happens though. – Den May 11 '18 at 07:09
  • what makes you say that ? is it because of the order the messages are printed ? if rank 1 `printf()` before rank 0, there is no guarantee the message of rank 1 will appear first in the output of the program. Try to insert `MPI_Barrier()` after process 0 receive from 1 and before process 1 broadcast to 0, and see if a deadlock occurs. – Gilles Gouaillardet May 11 '18 at 07:33
  • Thanks for the answer. Yes MPI_Barrier() with that organization causes a deadlock. Let me rephrase my question again then, what can be done to make sure that printing receive msg happens before printing the relevant broadcast msg? – Den May 11 '18 at 08:22
  • long story short, you cannot. – Gilles Gouaillardet May 11 '18 at 08:30

0 Answers0