1 // Copyright 2005 The Trustees of Indiana University.
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)
7 // Authors: Douglas Gregor
9 #ifndef BOOST_PARALLEL_GLOBAL_INDEX_MAP_HPP
10 #define BOOST_PARALLEL_GLOBAL_INDEX_MAP_HPP
12 #include <boost/property_map/property_map.hpp>
14 #include <boost/shared_ptr.hpp>
16 namespace boost { namespace parallel {
18 template<typename IndexMap, typename GlobalMap>
19 class global_index_map
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;
27 template<typename ProcessGroup>
28 global_index_map(ProcessGroup pg, value_type num_local_indices,
29 IndexMap index_map, GlobalMap global)
30 : index_map(index_map), global(global)
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);
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) {
42 receive(pg, src, 0, n);
43 (*starting_index)[src + 1] = (*starting_index)[src] + n;
45 for (process_id_type dest = 1; dest < num_processes(pg); ++dest)
46 send(pg, dest, 1, &starting_index->front(), num_processes(pg));
50 receive(pg, 0, 1, &starting_index->front(), num_processes(pg));
54 friend inline value_type
55 get(const global_index_map& gim, const key_type& x)
58 return (*gim.starting_index)[get(gim.global, x).first]
59 + get(gim.index_map, x);
63 shared_ptr<std::vector<value_type> > starting_index;
68 } } // end namespace boost::parallel
70 #endif // BOOST_PARALLEL_GLOBAL_INDEX_MAP_HPP