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 'Star maps using Blender'
Blender just recently dropped a new version, 4.5(with 5.0 on the horizon), and within it was a new feature for which I immediately thought of a use for. The new feature was a .csv importer for Geometry nodes. Geometry nodes are a method of modelling that uses a node tree to create 3D models which offers more flexibility than straight modeling does. The .csv importer node allows you to bring in a .csv file and use the data in it to control aspects of your model. So for example, if you...
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...
Back
Top