Trouble with OpenMPI Bcast in C++

  • Thread starter FunkyDwarf
  • Start date
  • Tags
    C++
In summary, the conversation is about a user trying to use MPIBcast to distribute a user entered value across all nodes, but it seems like only the root node is working. The code being used is OpenMPI 1.5.4 and it includes a Bcast procedure within an if statement, which may be causing issues. The other node never receives a non-zero value and the user is unsure if the Bcast syntax or usage is incorrect. There is a question about how to input the value for N once without using send/receive.
  • #1
FunkyDwarf
489
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
  • #2
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?
 
  • #3
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".
 
  • #4
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)
 
  • #5


Hi FD,

It looks like you are using the correct syntax for MPI::COMM_WORLD.Bcast, so the issue may lie elsewhere. One potential problem could be the use of the "while" loop in your code. This loop may be preventing the other nodes from receiving the broadcasted value because it is continuously checking for a non-zero value of N. Instead of using a loop, you could try using the MPI::COMM_WORLD.Wait function to ensure that the broadcast has been received before proceeding with the rest of the code. Another possible solution could be to use the MPI::COMM_WORLD.Barrier function before the Bcast function to ensure that all nodes are synchronized before the broadcast takes place. I hope this helps and good luck with your project!

Best,
 

What is OpenMPI Bcast in C++?

OpenMPI Bcast is a function in the OpenMPI library that is used for broadcasting data from one process to all other processes in a parallel program. It is commonly used in C++ programs to share data among multiple processes.

Why am I having trouble with OpenMPI Bcast in C++?

There could be several reasons why you are having trouble with OpenMPI Bcast in C++. Some common issues include incorrect implementation of the function, mismatched data types, or errors in the communication between processes.

How can I debug my OpenMPI Bcast in C++ program?

To debug your OpenMPI Bcast program, you can use tools such as MPI debugger (mpirun) or GDB (GNU Debugger). These tools can help you identify and fix errors in your code, such as segmentation faults or deadlocks.

Can I use OpenMPI Bcast in C++ for large data sets?

Yes, OpenMPI Bcast can be used for large data sets in C++ programs. However, it is important to optimize the communication pattern and data structure to avoid performance issues. Additionally, using a high-speed interconnect can also improve the efficiency of data transfer.

Are there any alternatives to OpenMPI Bcast in C++?

Yes, there are alternative functions for broadcasting data in parallel programs, such as MPI_Allgather, MPI_Allreduce, and MPI_Scatter. These functions have different communication patterns and may be more suitable for certain applications. It is recommended to consult the OpenMPI documentation to determine the best function for your specific needs.

Similar threads

  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
6
Views
8K
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
2
Replies
66
Views
4K
  • Programming and Computer Science
Replies
3
Views
1K
Replies
10
Views
958
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
2
Replies
39
Views
3K
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
2
Views
873
Back
Top