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)
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...
Back
Top