DOLFIN
DOLFIN C++ interface
IndexMap.h
1 // Copyright (C) 2015 Chris Richardson
2 //
3 // This file is part of DOLFIN.
4 //
5 // DOLFIN is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU Lesser General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // DOLFIN is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public License
16 // along with DOLFIN. If not, see <http://www.gnu.org/licenses/>.
17 
18 #ifndef __INDEX_MAP_H
19 #define __INDEX_MAP_H
20 
21 #include <utility>
22 #include <vector>
23 #include <dolfin/common/MPI.h>
24 
25 namespace dolfin
26 {
27 
34 
35  class IndexMap
36  {
37  public:
38 
42  enum class MapSize : int32_t { ALL = 0,
43  OWNED = 1,
44  UNOWNED = 2,
45  GLOBAL = 3 };
46 
48  IndexMap();
49 
51  explicit IndexMap(MPI_Comm mpi_comm);
52 
55  IndexMap(MPI_Comm mpi_comm, std::size_t local_size, std::size_t block_size);
56 
58  ~IndexMap();
59 
62  void init(std::size_t local_size, std::size_t block_size);
63 
65  std::pair<std::size_t, std::size_t> local_range() const;
66 
69  std::size_t size(MapSize type) const;
70 
73  const std::vector<std::size_t>& local_to_global_unowned() const;
74 
76  std::size_t local_to_global(std::size_t i) const;
77 
80  void set_local_to_global(const std::vector<std::size_t>& indices);
81 
83  const std::vector<int>& off_process_owner() const;
84 
86  int global_index_owner(std::size_t index) const;
87 
89  int block_size() const;
90 
92  MPI_Comm mpi_comm() const;
93 
94  private:
95 
96  // MPI Communicator
97  dolfin::MPI::Comm _mpi_comm;
98 
99  // Cache rank of mpi_comm (otherwise calls to MPI_Comm_rank can be
100  // excessive)
101  unsigned int _rank;
102 
103  // Range of ownership of index for all processes
104  std::vector<std::size_t> _all_ranges;
105 
106  // Local to global map for off-process entries
107  std::vector<std::size_t> _local_to_global;
108 
109  // Off process owner cache
110  std::vector<int> _off_process_owner;
111 
112  // Block size
113  int _block_size;
114 
115  };
116 
117  // Function which may appear in a hot loop
118  inline std::size_t IndexMap::local_to_global(std::size_t i) const
119  {
120  // These two calls get hopefully optimized out of hot loops due to
121  // inlining
122  const std::size_t local_size = size(IndexMap::MapSize::OWNED);
123  const std::size_t global_offset = local_range().first;
124 
125  if (i < local_size)
126  return (i + global_offset);
127  else
128  {
129  const std::div_t div = std::div((i - local_size), _block_size);
130  const int component = div.rem;
131  const int index = div.quot;
132  dolfin_assert((std::size_t) index < _local_to_global.size());
133  return _block_size*_local_to_global[index] + component;
134  }
135  }
136 
137 }
138 
139 #endif
const std::vector< std::size_t > & local_to_global_unowned() const
Definition: IndexMap.cpp:107
int block_size() const
Get block size.
Definition: IndexMap.cpp:137
void set_local_to_global(const std::vector< std::size_t > &indices)
Definition: IndexMap.cpp:112
Definition: IndexMap.h:35
Definition: adapt.h:29
MPI_Comm mpi_comm() const
Return MPI communicator.
Definition: IndexMap.cpp:142
int global_index_owner(std::size_t index) const
Get process owner of any global index.
Definition: IndexMap.cpp:124
const std::vector< int > & off_process_owner() const
Get off process owner for unowned indices.
Definition: IndexMap.cpp:132
~IndexMap()
Destructor.
Definition: IndexMap.cpp:44
std::size_t local_to_global(std::size_t i) const
Get global index of local index i.
Definition: IndexMap.h:118
void init(std::size_t local_size, std::size_t block_size)
Definition: IndexMap.cpp:49
std::size_t size(MapSize type) const
Definition: IndexMap.cpp:77
MapSize
Definition: IndexMap.h:42
Definition: MPI.h:76
IndexMap()
Constructor.
Definition: IndexMap.cpp:25
std::pair< std::size_t, std::size_t > local_range() const
Local range of indices.
Definition: IndexMap.cpp:63