global_index_map.hpp 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. // Copyright 2005 The Trustees of Indiana University.
  2. // Use, modification and distribution is subject to the Boost Software
  3. // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  4. // http://www.boost.org/LICENSE_1_0.txt)
  5. // Authors: Douglas Gregor
  6. // Andrew Lumsdaine
  7. #ifndef BOOST_PARALLEL_GLOBAL_INDEX_MAP_HPP
  8. #define BOOST_PARALLEL_GLOBAL_INDEX_MAP_HPP
  9. #ifndef BOOST_GRAPH_USE_MPI
  10. #error "Parallel BGL files should not be included unless <boost/graph/use_mpi.hpp> has been included"
  11. #endif
  12. #include <boost/property_map/property_map.hpp>
  13. #include <vector>
  14. #include <boost/shared_ptr.hpp>
  15. namespace boost { namespace parallel {
  16. template<typename IndexMap, typename GlobalMap>
  17. class global_index_map
  18. {
  19. public:
  20. typedef typename property_traits<IndexMap>::key_type key_type;
  21. typedef typename property_traits<IndexMap>::value_type value_type;
  22. typedef value_type reference;
  23. typedef readable_property_map_tag category;
  24. template<typename ProcessGroup>
  25. global_index_map(ProcessGroup pg, value_type num_local_indices,
  26. IndexMap index_map, GlobalMap global)
  27. : index_map(index_map), global(global)
  28. {
  29. typedef typename ProcessGroup::process_id_type process_id_type;
  30. starting_index.reset(new std::vector<value_type>(num_processes(pg) + 1));
  31. send(pg, 0, 0, num_local_indices);
  32. synchronize(pg);
  33. // Populate starting_index in all processes
  34. if (process_id(pg) == 0) {
  35. (*starting_index)[0] = 0;
  36. for (process_id_type src = 0; src < num_processes(pg); ++src) {
  37. value_type n;
  38. receive(pg, src, 0, n);
  39. (*starting_index)[src + 1] = (*starting_index)[src] + n;
  40. }
  41. for (process_id_type dest = 1; dest < num_processes(pg); ++dest)
  42. send(pg, dest, 1, &starting_index->front(), num_processes(pg));
  43. synchronize(pg);
  44. } else {
  45. synchronize(pg);
  46. receive(pg, 0, 1, &starting_index->front(), num_processes(pg));
  47. }
  48. }
  49. friend inline value_type
  50. get(const global_index_map& gim, const key_type& x)
  51. {
  52. using boost::get;
  53. return (*gim.starting_index)[get(gim.global, x).first]
  54. + get(gim.index_map, x);
  55. }
  56. private:
  57. shared_ptr<std::vector<value_type> > starting_index;
  58. IndexMap index_map;
  59. GlobalMap global;
  60. };
  61. } } // end namespace boost::parallel
  62. #endif // BOOST_PARALLEL_GLOBAL_INDEX_MAP_HPP