global_index_map.hpp 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. #include <boost/property_map/property_map.hpp>
  10. #include <vector>
  11. #include <boost/shared_ptr.hpp>
  12. namespace boost { namespace parallel {
  13. template<typename IndexMap, typename GlobalMap>
  14. class global_index_map
  15. {
  16. public:
  17. typedef typename property_traits<IndexMap>::key_type key_type;
  18. typedef typename property_traits<IndexMap>::value_type value_type;
  19. typedef value_type reference;
  20. typedef readable_property_map_tag category;
  21. template<typename ProcessGroup>
  22. global_index_map(ProcessGroup pg, value_type num_local_indices,
  23. IndexMap index_map, GlobalMap global)
  24. : index_map(index_map), global(global)
  25. {
  26. typedef typename ProcessGroup::process_id_type process_id_type;
  27. starting_index.reset(new std::vector<value_type>(num_processes(pg) + 1));
  28. send(pg, 0, 0, num_local_indices);
  29. synchronize(pg);
  30. // Populate starting_index in all processes
  31. if (process_id(pg) == 0) {
  32. (*starting_index)[0] = 0;
  33. for (process_id_type src = 0; src < num_processes(pg); ++src) {
  34. value_type n;
  35. receive(pg, src, 0, n);
  36. (*starting_index)[src + 1] = (*starting_index)[src] + n;
  37. }
  38. for (process_id_type dest = 1; dest < num_processes(pg); ++dest)
  39. send(pg, dest, 1, &starting_index->front(), num_processes(pg));
  40. synchronize(pg);
  41. } else {
  42. synchronize(pg);
  43. receive(pg, 0, 1, &starting_index->front(), num_processes(pg));
  44. }
  45. }
  46. friend inline value_type
  47. get(const global_index_map& gim, const key_type& x)
  48. {
  49. using boost::get;
  50. return (*gim.starting_index)[get(gim.global, x).first]
  51. + get(gim.index_map, x);
  52. }
  53. private:
  54. shared_ptr<std::vector<value_type> > starting_index;
  55. IndexMap index_map;
  56. GlobalMap global;
  57. };
  58. } } // end namespace boost::parallel
  59. #endif // BOOST_PARALLEL_GLOBAL_INDEX_MAP_HPP