/
Cabana_Grid_SparseLocalGrid.hpp
322 lines (278 loc) · 14.1 KB
/
Cabana_Grid_SparseLocalGrid.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
/****************************************************************************
* Copyright (c) 2018-2023 by the Cabana authors *
* All rights reserved. *
* *
* This file is part of the Cabana library. Cabana is distributed under a *
* BSD 3-clause license. For the licensing terms see the LICENSE file in *
* the top-level directory. *
* *
* SPDX-License-Identifier: BSD-3-Clause *
****************************************************************************/
/*!
\file Cabana_Grid_SparseLocalGrid.hpp
\brief Sparse Local grid and related implementations
*/
#ifndef CABANA_GRID_LOCALGRID_SPARSE_HPP
#define CABANA_GRID_LOCALGRID_SPARSE_HPP
#include <Cabana_Grid_GlobalGrid.hpp>
#include <Cabana_Grid_IndexSpace.hpp>
#include <Cabana_Grid_SparseIndexSpace.hpp>
#include <Cabana_Grid_Types.hpp>
#include <Cabana_Utils.hpp> // FIXME: remove after next release.
#include <array>
#include <vector>
namespace Cabana
{
namespace Grid
{
namespace Experimental
{
//---------------------------------------------------------------------------//
template <class MeshType>
class LocalGrid;
//---------------------------------------------------------------------------//
/*!
\brief Local logical grid specialized for sparse grid.
\tparam MeshType Mesh type: SparseMesh
\tparam NumSpaceDim Space Dimension, needed by SparseMesh's template
*/
template <class Scalar, std::size_t NumSpaceDim>
class LocalGrid<SparseMesh<Scalar, NumSpaceDim>>
{
public:
//! Mesh type.
using mesh_type = SparseMesh<Scalar, NumSpaceDim>;
//! Spatial dimension.
static constexpr std::size_t num_space_dim = mesh_type::num_space_dim;
/*!
\brief Constructor.
\param global_grid The global grid from which the local grid will be
constructed.
\param halo_cell_width The number of halo cells surrounding the locally
owned cells.
\param cell_num_per_tile_dim The number of local cells in each tile in
each dimension.
*/
LocalGrid( const std::shared_ptr<GlobalGrid<mesh_type>>& global_grid,
const int halo_cell_width, const long cell_num_per_tile_dim );
//! \brief Get the global grid that owns the local grid.
const GlobalGrid<mesh_type>& globalGrid() const;
//! \brief Get a mutable version of the global grid that own the local grid
GlobalGrid<mesh_type>& globalGrid();
//! \brief Get the number of cells in the halo.
int haloCellWidth() const;
//! \brief Get the number of tiles in the halo.
int haloTileWidth() const;
//! \brief Get the total number of local cells per dimension (owned + halo).
//! \param d Spatial dimension.
int totalNumCell( const int d ) const;
//! \brief Get the total number of local cells (owned + halo) on this MPI
//! rank
int totalNumCell() const;
/*!
\brief Get the global index of a neighbor given neighbor rank offsets
relative to this local grid.
\param off_ijk %Array of neighbor offset indices.
If the neighbor rank is out of bounds return -1. Note that in the case of
periodic boundaries out of bounds indices are allowed as the indices will
be wrapped around the periodic boundary.
*/
int neighborRank( const std::array<int, num_space_dim>& off_ijk ) const;
/*!
\brief Get the global index of a neighbor given neighbor rank offsets
relative to this local grid.
\param off_i, off_j, off_k Neighbor offset index in a given dimension.
If the neighbor rank is out of bounds return -1. Note that in the case of
periodic boundaries out of bounds indices are allowed as the indices will
be wrapped around the periodic boundary.
*/
int neighborRank( const int off_i, const int off_j, const int off_k ) const;
/*!
\brief Given a decomposition type, entity type, and index type, get the
contiguous set of indices that span the space of those entities in the
local domain (return the general cell index space)
\param t1 Decomposition type: Own or Ghost
\param t2 Entity type: Cell, Node, Edge, or Face
\param t3 Index type: Local or Global
*/
template <class DecompositionTag, class EntityType, class IndexType>
IndexSpace<num_space_dim> indexSpace( DecompositionTag t1, EntityType t2,
IndexType t3 ) const;
/*!
\brief Given the relative offsets of a neighbor rank, get the set of
global entity indices shared with that neighbor in the given
decomposition.
\param t1 Decomposition type: Own or Ghost
\param t2 Entity: Cell or Node; TODO: Edge or Face
\param off_ijk Array of neighbor offset indices.
\param halo_width Optional depth of shared indices within the halo. Must
be less than or equal to the halo width of the local grid. Default is to
use the halo width of the local grid.
*/
template <unsigned long long cellBitsPerTileDim, class DecompositionTag,
class EntityType>
TileIndexSpace<num_space_dim, cellBitsPerTileDim>
sharedTileIndexSpace( DecompositionTag t1, EntityType t2,
const std::array<int, num_space_dim>& off_ijk,
const int halo_width = -1 ) const;
/*!
\brief Given the relative offsets of a neighbor rank, get the set of
global entity indices shared with that neighbor in the given
decomposition.
\tparam DecompositionTag Decomposition type: Own or Ghost
\tparam EntityType Entity: Cell, Node, Edge, or Face
\param t1 Decomposition type: Own or Ghost
\param t2 Entity: Cell or Node; TODO: Edge or Face
\param off_i, off_j, off_k Neighbor offset index in a given dimension.
\param halo_width Optional depth of shared indices within the halo. Must
be less than or equal to the halo width of the local grid. Default is to
use the halo width of the local grid.
*/
template <unsigned long long cellBitsPerTileDim, class DecompositionTag,
class EntityType, std::size_t NSD = num_space_dim>
std::enable_if_t<3 == NSD, TileIndexSpace<3, cellBitsPerTileDim>>
sharedTileIndexSpace( DecompositionTag t1, EntityType t2, const int off_i,
const int off_j, const int off_k,
const int halo_width = -1 ) const;
private:
// Node related index space implementations
template <class DecompositionTag, class IndexType>
IndexSpace<num_space_dim> indexSpaceImpl( DecompositionTag t1, Node,
IndexType t3 ) const;
template <unsigned long long cellBitsPerTileDim, class DecompositionTag>
TileIndexSpace<num_space_dim, cellBitsPerTileDim>
sharedTileIndexSpaceImpl( DecompositionTag t1, Node,
const std::array<int, num_space_dim>& off_ijk,
const int halo_width ) const;
// Cell related index space implementations
template <class DecompositionTag, class IndexType>
IndexSpace<num_space_dim> indexSpaceImpl( DecompositionTag t1, Cell,
IndexType t3 ) const;
template <unsigned long long cellBitsPerTileDim, class DecompositionTag>
TileIndexSpace<num_space_dim, cellBitsPerTileDim>
sharedTileIndexSpaceImpl( DecompositionTag t1, Cell,
const std::array<int, num_space_dim>& off_ijk,
const int halo_width ) const;
// Face related index space implementations
template <class DecompositionTag, class IndexType>
IndexSpace<num_space_dim> indexSpaceImpl( DecompositionTag t1, Face<Dim::I>,
IndexType t3 ) const;
template <class DecompositionTag, class IndexType>
IndexSpace<num_space_dim> indexSpaceImpl( DecompositionTag t1, Face<Dim::J>,
IndexType t3 ) const;
template <class DecompositionTag, class IndexType>
IndexSpace<num_space_dim> indexSpaceImpl( DecompositionTag t1, Face<Dim::K>,
IndexType t3 ) const;
template <unsigned long long cellBitsPerTileDim, class DecompositionTag>
TileIndexSpace<num_space_dim, cellBitsPerTileDim>
sharedTileIndexSpaceImpl( DecompositionTag t1, Face<Dim::I>,
const std::array<int, num_space_dim>& off_ijk,
const int halo_width ) const;
template <unsigned long long cellBitsPerTileDim, class DecompositionTag>
TileIndexSpace<num_space_dim, cellBitsPerTileDim>
sharedTileIndexSpaceImpl( DecompositionTag t1, Face<Dim::J>,
const std::array<int, num_space_dim>& off_ijk,
const int halo_width ) const;
template <unsigned long long cellBitsPerTileDim, class DecompositionTag>
TileIndexSpace<num_space_dim, cellBitsPerTileDim>
sharedTileIndexSpaceImpl( DecompositionTag t1, Face<Dim::K>,
const std::array<int, num_space_dim>& off_ijk,
const int halo_width ) const;
// Edge related index space implementations1
template <class DecompositionTag, class IndexType>
IndexSpace<num_space_dim> indexSpaceImpl( DecompositionTag t1, Edge<Dim::I>,
IndexType t3 ) const;
template <class DecompositionTag, class IndexType>
IndexSpace<num_space_dim> indexSpaceImpl( DecompositionTag t1, Edge<Dim::J>,
IndexType t3 ) const;
template <class DecompositionTag, class IndexType>
IndexSpace<num_space_dim> indexSpaceImpl( DecompositionTag t1, Edge<Dim::K>,
IndexType t3 ) const;
template <unsigned long long cellBitsPerTileDim, class DecompositionTag>
TileIndexSpace<num_space_dim, cellBitsPerTileDim>
sharedTileIndexSpaceImpl( DecompositionTag t1, Edge<Dim::I>,
const std::array<int, num_space_dim>& off_ijk,
const int halo_width ) const;
template <unsigned long long cellBitsPerTileDim, class DecompositionTag>
TileIndexSpace<num_space_dim, cellBitsPerTileDim>
sharedTileIndexSpaceImpl( DecompositionTag t1, Edge<Dim::J>,
const std::array<int, num_space_dim>& off_ijk,
const int halo_width ) const;
template <unsigned long long cellBitsPerTileDim, class DecompositionTag>
TileIndexSpace<num_space_dim, cellBitsPerTileDim>
sharedTileIndexSpaceImpl( DecompositionTag t1, Edge<Dim::K>,
const std::array<int, num_space_dim>& off_ijk,
const int halo_width ) const;
// More general implementations for node and cell center indices
// Note: In Sparse grid, each cell considers the left-most node and its cell
// center as belongings, i.e., Node and Cell share the same element-to-cell
// mapping, thus sharse the same interface.
IndexSpace<num_space_dim> indexSpaceImpl( Own, Local ) const;
IndexSpace<num_space_dim> indexSpaceImpl( Ghost, Local ) const;
IndexSpace<num_space_dim> indexSpaceImpl( Own, Global ) const;
template <unsigned long long cellBitsPerTileDim>
TileIndexSpace<num_space_dim, cellBitsPerTileDim>
sharedTileIndexSpaceImpl( Own,
const std::array<int, num_space_dim>& off_ijk,
const int halo_width ) const;
template <unsigned long long cellBitsPerTileDim>
TileIndexSpace<num_space_dim, cellBitsPerTileDim>
sharedTileIndexSpaceImpl( Ghost,
const std::array<int, num_space_dim>& off_ijk,
const int halo_width ) const;
private:
std::shared_ptr<GlobalGrid<mesh_type>> _global_grid;
int _halo_cell_width;
const long _cell_num_per_tile_dim;
}; // end LocalGrid<SparseMesh<Scalar, NumSpaceDim>>
//---------------------------------------------------------------------------//
// Creation function.
//---------------------------------------------------------------------------//
/*!
\brief Create a local grid.
\param global_grid The global grid from which the local grid will be
constructed.
\param halo_cell_width The number of halo cells surrounding the locally
owned cells.
\param cell_num_per_tile_dim The number of local cells in each tile in each
dimension.
\return Shared pointer to a LocalGrid.
*/
template <class MeshType>
std::shared_ptr<LocalGrid<MeshType>>
createSparseLocalGrid( const std::shared_ptr<GlobalGrid<MeshType>>& global_grid,
const int halo_cell_width,
const int cell_num_per_tile_dim )
{
static_assert( isSparseMesh<MeshType>::value,
"createSparseLocalGrid supports only SparseMesh" );
return std::make_shared<LocalGrid<MeshType>>( global_grid, halo_cell_width,
cell_num_per_tile_dim );
}
} // namespace Experimental
} // namespace Grid
} // namespace Cabana
//---------------------------------------------------------------------------//
namespace Cajita
{
namespace Experimental
{
//! \cond Deprecated
template <class MeshType>
using LocalGrid CAJITA_DEPRECATED =
Cabana::Grid::Experimental::LocalGrid<MeshType>;
template <class... Args>
CAJITA_DEPRECATED auto createSparseLocalGrid( Args&&... args )
{
return Cabana::Grid::Experimental::createSparseLocalGrid(
std::forward<Args>( args )... );
}
//! \endcond
} // namespace Experimental
} // namespace Cajita
//---------------------------------------------------------------------------//
// Template implementations
//---------------------------------------------------------------------------//
#include <Cabana_Grid_SparseLocalGrid_impl.hpp>
//---------------------------------------------------------------------------//
#endif // end CABANA_GRID_LOCALGRID_SPARSE_HPP