This is a read only copy of the old FEniCS QA forum. Please visit the new QA forum to ask questions

dolfin::MPI::broadcast()

0 votes

Hello,

I wonder if I could get some help on this problem.

I have a std::map which I'd like to broadcast to all processes as they need to look up some values but using dolfin::MPI::broadcast( name_of_map ) gives errors.

I'm aware that with boost::mpi, I can do:

world.send(1, 0, my_map);

but I can't seem to figure out how to use boost::mpi in FEniCS.

Thnaks in advance.

asked Nov 26, 2013 by gennadiy FEniCS Novice (590 points)

2 Answers

+1 vote
 
Best answer

Hi, could you please describe the errors you get when using dolfin::MPI::broadcast? Meanwhile, the send/recv with boost can be used like this

#include <dolfin.h>                                                              

using namespace dolfin;                                                          

int main()                                                                       
{                                                                                
  if(MPI::num_processes() == 2)                                                  
  {                                                                              
    std::map<int, int> map;                                                      
    MPICommunicator mpi_comm;                                                    
    boost::mpi::communicator comm(*mpi_comm, boost::mpi::comm_attach);           

    if(MPI::process_number() == 0)                                               
    {                                                                            
      map[0] = 10;                                                               
      map[1] = 100;                                                              
      comm.send(1, 0, &map, 1);                                                  
    }                                                                            
    else                                                                         
    {                                                                            
      comm.recv(0, 0, &map, 1);                                                  
      for(std::size_t i = 0; i < map.size(); i++)                                
        info("map[%d] = %d", i, map[i]);                                         
    }                                                                            
  }                                                                              
  return 0;                                                                      
} 
answered Nov 26, 2013 by MiroK FEniCS Expert (80,920 points)
selected Nov 27, 2013 by gennadiy

Thanks Mirok, this shows me how to use boost::mpi in FeniCS.

The errors with dolfin::MPI::broadcast are probably because the types I'm using with the map are user-defined abstract objects.

0 votes

My experience is that it is considerably more efficient to pack the map into std::vector(s) for communication rather than rely on Boost.MPI to communicate more abstract objects.

answered Nov 27, 2013 by Garth N. Wells FEniCS Expert (35,930 points)

Thanks Garth, I believe this is the actual problem I'm having as I'm trying to communicate more abstract objects with boost::mpi. As you suggested, I'll pack the map into std::vector(s) instead.

It's a shame I can't simultaneously select two 'best answers'.

...