C/C++ Trouble with OpenMPI Bcast in C++

  • Thread starter Thread starter FunkyDwarf
  • Start date Start date
  • Tags Tags
    C++
AI Thread Summary
The issue with the MPI_Bcast implementation lies in its placement within the code, as it is currently executed only by the root node (mynode 0), preventing other nodes from receiving the broadcasted value. Collective communication routines like Bcast must be called by all nodes in the communicator to ensure synchronization. To avoid all nodes attempting to read input, the input should be handled separately, with the Bcast call placed outside the conditional statement. This adjustment allows all nodes to receive the value properly without requiring a loop to check for updates. Properly structuring the Bcast call is essential for effective communication in MPI.
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)
 
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.
Thread 'Project Documentation'
Trying to package up a small bank account manager project that I have been tempering on for a while. One that is certainly worth something to me. Although I have created methods to whip up quick documents with all fields and properties. I would like something better to reference in order to express the mechanical functions. It is unclear to me about any standardized format for code documentation that exists. I have tried object orientated diagrams with shapes to try and express the...
Back
Top