Trouble with OpenMPI Bcast in C++

  • Context: C/C++ 
  • Thread starter Thread starter FunkyDwarf
  • Start date Start date
  • Tags Tags
    C++
Click For Summary

Discussion Overview

The discussion revolves around the use of MPI_Bcast in OpenMPI for distributing a user-entered value across multiple nodes in a C++ program. Participants explore issues related to the broadcast functionality, synchronization among nodes, and the appropriate structure of the code to achieve the desired communication.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant describes their implementation of MPI_Bcast and notes that only the root node seems to be functioning correctly, leading to a situation where other nodes do not receive the intended value.
  • Another participant points out that the Bcast call is placed within an if statement that checks if the node is the root, suggesting that it should be executed by all nodes to function properly.
  • A third participant emphasizes that collective communication routines, like Bcast, must be called by all nodes in the communicator and that synchronization occurs automatically, negating the need for a loop to wait for the broadcast.
  • A participant raises a concern about the need to input the value for N only once, questioning whether they must implement additional send/receive logic if the Bcast call is moved outside the if statement.

Areas of Agreement / Disagreement

Participants express differing views on the correct placement of the Bcast call and the necessity of additional synchronization mechanisms. There is no consensus on the best approach to handle user input across multiple nodes.

Contextual Notes

Participants have not fully resolved the implications of moving the Bcast call outside the if statement, particularly regarding user input and the behavior of non-root nodes.

Who May Find This Useful

Individuals working with MPI in C++, particularly those interested in collective communication and synchronization issues in parallel programming.

FunkyDwarf
Messages
481
Reaction score
0
Hey gang,

I'm trying to use MPIBcast to distribute a user entered value across all nodes, but it seems like only the root node is working (i.e. no broadcast).

I'm using OpenMPI 1.5.4 with the following code:

Code:
  A=0;
  std::clock_t start;
  double duration;
  start = std::clock();
  MPI::Status status;
  MPI::Init(argc, argv);
  N=0;
  numnodes = MPI::COMM_WORLD.Get_size();
  mynode = MPI::COMM_WORLD.Get_rank();
  
  if(mynode==0){
    cout<<"Enter number of sections: "<<endl;
    cin>>N;
    MPI::COMM_WORLD.Bcast(&N, 1, MPI::INT, 0);
  }
  MPI::COMM_WORLD.Barrier();
  
  if(mynode!=0) {
    
    while(N==0) {
     
     //Loop until Bcast received
  
    }
    
  }
  
 if(mynode==0) {
  cout<<"root node: "<<N<<endl;
 } else {
   cout<<"other nodes: "<<N<<endl;
   
}

Unfortunately the other node never gets a non-zero value of N and just keeps looping :\ I have a feeling my Bcast syntax or usage is wrong but I don't know why/how. Any help would be great! (I know I could use the wait function instead of a loop but this was quicker :) )

Cheers,
-FD
 
Technology news on Phys.org
Hey FunkyDwarf.

I noticed that your Bcast procedure is in the if statement and only gets executed if mynodes is 0. Is it meant to be outside the if block just below it?
 
colective communication routines must be called by all nodes on your commutator. Also theese routines sync up all nodes so you don't need any kind of "Loop until Bcast received".
 
What if I only want to input the value for N once? If it is outside the if statement the cin will be called by all nodes. Am i forced to do some send/receive stuff?

(thanks for replys btw)
 

Similar threads

  • · Replies 5 ·
Replies
5
Views
3K
Replies
12
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 4 ·
Replies
4
Views
6K
  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 6 ·
Replies
6
Views
12K
  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K