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