16#ifndef CABANA_GRID_SPARSE_ARRAY_HPP
17#define CABANA_GRID_SPARSE_ARRAY_HPP
26#include <Kokkos_Core.hpp>
51template <
class DataTypes,
class EntityType,
class MeshType,
62 "[SparesArrayLayout] Support only SparseMesh" );
74 using key_type =
typename sparse_map_type::key_type;
78 sparse_map_type::cell_bits_per_tile_dim;
81 sparse_map_type::cell_num_per_tile_dim;
95 SparseMapType& sparse_map,
const float over_allocation )
96 : _over_allocation( over_allocation )
97 , _local_grid( local_grid )
100 auto sparse_mesh = _local_grid->globalGrid().globalMesh();
101 _cell_size[0] = sparse_mesh.cellSize( 0 );
102 _cell_size[1] = sparse_mesh.cellSize( 1 );
103 _cell_size[2] = sparse_mesh.cellSize( 2 );
104 _global_low_corner[0] = sparse_mesh.lowCorner( 0 );
105 _global_low_corner[1] = sparse_mesh.lowCorner( 1 );
106 _global_low_corner[2] = sparse_mesh.lowCorner( 2 );
113 const std::shared_ptr<LocalGrid<MeshType>>
localGrid()
const
119 inline uint64_t
sizeCell()
const {
return _map.sizeCell(); }
122 inline uint64_t
sizeTile()
const {
return _map.sizeTile(); }
130 return _map.reservedCellSize( factor );
137 inline void clear() { _map.clear(); }
147 template <
class ExecSpace,
class PositionSliceType>
149 const int particle_num,
const int p2g_radius = 1 )
152 Kokkos::Array<scalar_type, 3> dx_inv = {
156 auto& low_corner = _global_low_corner;
158 Kokkos::parallel_for(
159 "Cabana::Grid::Experimental::SparseArrayLayout::registerSparseMap",
160 Kokkos::RangePolicy<ExecSpace>( 0, particle_num ),
161 KOKKOS_LAMBDA(
const int pid ) {
162 scalar_type pos[3] = { positions( pid, 0 ) - low_corner[0],
163 positions( pid, 1 ) - low_corner[1],
164 positions( pid, 2 ) - low_corner[2] };
166 static_cast<int>( std::lround( pos[0] * dx_inv[0] ) -
168 static_cast<int>( std::lround( pos[1] * dx_inv[1] ) -
170 static_cast<int>( std::lround( pos[2] * dx_inv[2] ) -
173 const int p2g_size = p2g_radius * 2;
174 for (
int i = 0; i <= p2g_size; ++i )
175 for (
int j = 0; j <= p2g_size; ++j )
176 for (
int k = 0; k <= p2g_size; ++k )
178 int cell_id[3] = { grid_base[0] + i,
181 map.insertCell( cell_id[0], cell_id[1],
191 KOKKOS_FORCEINLINE_FUNCTION
193 const int cell_k )
const
195 return _map.queryCell( cell_i, cell_j, cell_k );
202 KOKKOS_FORCEINLINE_FUNCTION
204 const int cell_k )
const
206 return _map.queryTile( cell_i, cell_j, cell_k );
213 KOKKOS_FORCEINLINE_FUNCTION
215 const int tile_k )
const
217 return _map.queryTileFromTileId( tile_i, tile_j, tile_k );
224 KOKKOS_FORCEINLINE_FUNCTION
227 return _map.queryTileFromTileKey( tile_key );
235 KOKKOS_FORCEINLINE_FUNCTION
237 const int cell_k )
const
239 return _map.cell_local_id( cell_i, cell_j, cell_k );
244 float _over_allocation;
246 Kokkos::Array<scalar_type, 3> _cell_size;
248 Kokkos::Array<scalar_type, 3> _global_low_corner;
250 std::shared_ptr<LocalGrid<MeshType>> _local_grid;
262template <
class DataTypes,
class EntityType,
class MeshType,
266 :
public std::true_type
271template <
class DataTypes,
class EntityType,
class MeshType,
275 :
public std::true_type
291template <
class DataTypes,
class EntityType,
class MeshType,
295 SparseMapType& sparse_map, EntityType,
const float over_allocation = 1.01f )
297 return std::make_shared<
299 local_grid, sparse_map, over_allocation );
313template <
class DataTypes,
class MemorySpace,
class EntityType,
class MeshType,
320 MeshType, SparseMapType>;
323 static_assert( Kokkos::is_memory_space<MemorySpace>() );
338 sparse_map_type::cell_bits_per_tile;
341 sparse_map_type::cell_mask_per_tile;
377 std::string
label()
const {
return _data.label(); }
406 reserve( _layout.reservedCellSize( factor ) );
429 KOKKOS_FORCEINLINE_FUNCTION
432 KOKKOS_FORCEINLINE_FUNCTION
435 return _data.arraySize( s );
446 template <
class PositionSliceType>
448 const int p2g_radius = 1 )
450 _layout.template registerSparseMap<execution_space, PositionSliceType>(
451 positions, particle_num, p2g_radius );
452 this->
resize( _layout.sparseMap().sizeCell() );
460 KOKKOS_FORCEINLINE_FUNCTION
462 const int tile_k )
const
464 auto tile_id = _layout.queryTileFromTileId( tile_i, tile_j, tile_k );
465 return _data.access( tile_id );
473 template <
typename Value>
474 KOKKOS_FORCEINLINE_FUNCTION
soa_type&
477 return _data.access( tile_id );
484 KOKKOS_FORCEINLINE_FUNCTION
486 const int cell_k )
const
488 auto tile_id = _layout.queryTile( cell_i, cell_j, cell_k );
489 return _data.access( tile_id );
498 template <
typename Key>
500 getTuple(
const Key tile_key,
const int cell_local_id )
const
502 auto tile_id = _layout.queryTileFromTileKey( tile_key );
514 template <std::size_t M,
typename... Indices>
515 KOKKOS_FORCEINLINE_FUNCTION
516 typename soa_type::template member_reference_type<M>
517 get(
const Kokkos::Array<int, 3> cell_ijk, Indices&&... ids )
const
521 _layout.cellLocalId( cell_ijk[0], cell_ijk[1], cell_ijk[2] );
529 template <std::
size_t M>
530 KOKKOS_FORCEINLINE_FUNCTION
531 typename soa_type::template member_reference_type<M>
532 get(
const Kokkos::Array<int, 3> cell_ijk )
const
536 _layout.cellLocalId( cell_ijk[0], cell_ijk[1], cell_ijk[2] );
547 template <std::size_t M,
typename... Indices>
548 KOKKOS_FORCEINLINE_FUNCTION
549 typename soa_type::template member_reference_type<M>
550 get(
const Kokkos::Array<int, 3> tile_ijk,
551 const Kokkos::Array<int, 3> local_cell_ijk,
552 Indices&&... ids )
const
554 auto& soa =
accessTile( tile_ijk[0], tile_ijk[1], tile_ijk[2] );
555 auto array_index = _layout.cellLocalId(
556 local_cell_ijk[0], local_cell_ijk[1], local_cell_ijk[2] );
566 template <std::
size_t M>
567 KOKKOS_FORCEINLINE_FUNCTION
568 typename soa_type::template member_reference_type<M>
569 get(
const Kokkos::Array<int, 3> tile_ijk,
570 const Kokkos::Array<int, 3> local_cell_ijk )
const
572 auto& soa =
accessTile( tile_ijk[0], tile_ijk[1], tile_ijk[2] );
573 auto array_index = _layout.cellLocalId(
574 local_cell_ijk[0], local_cell_ijk[1], local_cell_ijk[2] );
585 template <std::size_t M,
typename... Indices>
586 KOKKOS_FORCEINLINE_FUNCTION
587 typename soa_type::template member_reference_type<M>
588 get(
const int tile_id,
const Kokkos::Array<int, 3> local_cell_ijk,
589 Indices&&... ids )
const
591 auto& soa = _data.access( tile_id );
592 auto array_index = _layout.cellLocalId(
593 local_cell_ijk[0], local_cell_ijk[1], local_cell_ijk[2] );
603 template <std::
size_t M>
604 KOKKOS_FORCEINLINE_FUNCTION
605 typename soa_type::template member_reference_type<M>
607 const Kokkos::Array<int, 3> local_cell_ijk )
const
609 auto& soa = _data.access( tile_id );
610 auto array_index = _layout.cellLocalId(
611 local_cell_ijk[0], local_cell_ijk[1], local_cell_ijk[2] );
623 template <std::size_t M,
typename... Indices>
624 KOKKOS_FORCEINLINE_FUNCTION
625 typename soa_type::template member_reference_type<M>
626 get(
const int tile_id,
const int cell_id, Indices&&... ids )
const
628 auto& soa = _data.access( tile_id );
638 template <std::
size_t M>
639 KOKKOS_FORCEINLINE_FUNCTION
640 typename soa_type::template member_reference_type<M>
641 get(
const int tile_id,
const int cell_id )
const
643 auto& soa = _data.access( tile_id );
665template <
class DataTypes,
class MemorySpace,
class EntityType,
class MeshType,
668 SparseArray<DataTypes, MemorySpace, EntityType, MeshType, SparseMapType>>
669 :
public std::true_type
674template <
class DataTypes,
class MemorySpace,
class EntityType,
class MeshType,
677 MeshType, SparseMapType>>
678 :
public std::true_type
691template <
class MemorySpace,
class DataTypes,
class EntityType,
class MeshType,
694 const std::string label,
697 return std::make_shared<
SparseArray<DataTypes, MemorySpace, EntityType,
698 MeshType, SparseMapType>>( label,
Array-of-Struct-of-Arrays particle data structure.
auto createSparseArrayLayout(const std::shared_ptr< LocalGrid< MeshType > > &local_grid, SparseMapType &sparse_map, EntityType, const float over_allocation=1.01f)
Create sparse array layout over the entities of a sparse local grid.
Definition Cabana_Grid_SparseArray.hpp:293
auto createSparseArray(const std::string label, SparseArrayLayout< DataTypes, EntityType, MeshType, SparseMapType > &layout)
Create sparse array based on the sparse array layout.
Definition Cabana_Grid_SparseArray.hpp:693
Sparse Local grid and related implementations.
Array-of-Struct-of-Arrays.
Definition Cabana_AoSoA.hpp:123
Definition Cabana_Grid_SparseLocalGrid.hpp:35
Entity layout for sparse array data on the local sparse mesh.
Definition Cabana_Grid_SparseArray.hpp:54
DataTypes member_types
Array member types, such as Cabana::MemberTypes<double, float[3]>
Definition Cabana_Grid_SparseArray.hpp:67
uint64_t arraySize() const
Array size in cell (default size measurse: cell)
Definition Cabana_Grid_SparseArray.hpp:134
KOKKOS_FORCEINLINE_FUNCTION value_type queryCell(const int cell_i, const int cell_j, const int cell_k) const
(Device) Query the 1D cell ID from the 3D cell ijk
Definition Cabana_Grid_SparseArray.hpp:192
KOKKOS_FORCEINLINE_FUNCTION value_type queryTileFromTileKey(const key_type tile_key) const
(Device) Query the 1D tile key from the 1D tile key
Definition Cabana_Grid_SparseArray.hpp:225
EntityType entity_type
Entity Type.
Definition Cabana_Grid_SparseArray.hpp:65
static constexpr std::size_t num_space_dim
Definition Cabana_Grid_SparseArray.hpp:83
SparseMapType sparse_map_type
Abbreviation for Sparse Map Type.
Definition Cabana_Grid_SparseArray.hpp:70
SparseArrayLayout(const std::shared_ptr< LocalGrid< MeshType > > &local_grid, SparseMapType &sparse_map, const float over_allocation)
(Host) Constructor
Definition Cabana_Grid_SparseArray.hpp:94
MeshType mesh_type
Mesh Type, should be SparseMesh.
Definition Cabana_Grid_SparseArray.hpp:57
void clear()
clear valid info inside array layout; i.e. clear sparse map
Definition Cabana_Grid_SparseArray.hpp:137
static constexpr unsigned long long cell_bits_per_tile_dim
Definition Cabana_Grid_SparseArray.hpp:77
SparseMapType & sparseMap()
get reference of sparse map
Definition Cabana_Grid_SparseArray.hpp:110
typename sparse_map_type::key_type key_type
key type in sparse map, i.e., the tile key type
Definition Cabana_Grid_SparseArray.hpp:74
KOKKOS_FORCEINLINE_FUNCTION value_type queryTileFromTileId(const int tile_i, const int tile_j, const int tile_k) const
(Device) Query the 1D tile key from the 3D tile ijk
Definition Cabana_Grid_SparseArray.hpp:214
const std::shared_ptr< LocalGrid< MeshType > > localGrid() const
Get the local grid over which this layout is defined.
Definition Cabana_Grid_SparseArray.hpp:113
void registerSparseMap(PositionSliceType &positions, const int particle_num, const int p2g_radius=1)
Register valid grids in sparse map according to input particle positions.
Definition Cabana_Grid_SparseArray.hpp:148
static constexpr unsigned long long cell_num_per_tile_dim
Definition Cabana_Grid_SparseArray.hpp:80
KOKKOS_FORCEINLINE_FUNCTION value_type queryTile(const int cell_i, const int cell_j, const int cell_k) const
(Device) Query the 1D tile ID from the 3D tile ijk
Definition Cabana_Grid_SparseArray.hpp:203
uint64_t sizeTile() const
array size in tile
Definition Cabana_Grid_SparseArray.hpp:122
KOKKOS_FORCEINLINE_FUNCTION value_type cellLocalId(const int cell_i, const int cell_j, const int cell_k) const
(Device) Get local cell ID from cell IJK
Definition Cabana_Grid_SparseArray.hpp:236
uint64_t sizeCell() const
array size in cell
Definition Cabana_Grid_SparseArray.hpp:119
typename mesh_type::scalar_type scalar_type
Scalar Type.
Definition Cabana_Grid_SparseArray.hpp:59
typename sparse_map_type::value_type value_type
value type in sparse map, i.e., the tile ID (array ID) type
Definition Cabana_Grid_SparseArray.hpp:72
uint64_t reservedCellSize(float factor) const
array reservation size in cell
Definition Cabana_Grid_SparseArray.hpp:128
typename sparse_map_type::memory_space memory_space
Memory space, the same memory space in sparse map.
Definition Cabana_Grid_SparseArray.hpp:85
Sparse array of field data on the local sparse mesh; Array data is stored in AoSoA manner,...
Definition Cabana_Grid_SparseArray.hpp:316
static constexpr int vector_length
AoSoA vector length = cell number in each tile.
Definition Cabana_Grid_SparseArray.hpp:347
const array_layout & layout() const
Get const reference to the array layout (device accessible)
Definition Cabana_Grid_SparseArray.hpp:381
KOKKOS_FORCEINLINE_FUNCTION soa_type::template member_reference_type< M > get(const Kokkos::Array< int, 3 > cell_ijk) const
Access element from cell IJK.
Definition Cabana_Grid_SparseArray.hpp:532
SparseMapType sparse_map_type
Sparse map type.
Definition Cabana_Grid_SparseArray.hpp:333
EntityType entity_type
Array entity type (node, cell, face, edge).
Definition Cabana_Grid_SparseArray.hpp:329
KOKKOS_FORCEINLINE_FUNCTION soa_type::template member_reference_type< M > get(const Kokkos::Array< int, 3 > cell_ijk, Indices &&... ids) const
Access element from cell IJK, access corresponding element's channels with extra indices.
Definition Cabana_Grid_SparseArray.hpp:517
KOKKOS_FORCEINLINE_FUNCTION soa_type::template member_reference_type< M > get(const int tile_id, const int cell_id, Indices &&... ids) const
Access element in a hierarchical manner, from 1D tile ID and then 1D local cell ID,...
Definition Cabana_Grid_SparseArray.hpp:626
static constexpr unsigned long long cell_bits_per_tile
Least bits required to represent all cells inside a tile.
Definition Cabana_Grid_SparseArray.hpp:337
KOKKOS_FORCEINLINE_FUNCTION size_type arraySize(const size_type s) const
Get data array size at a given struct member index.
Definition Cabana_Grid_SparseArray.hpp:433
aosoa_type & aosoa()
Get AoSoA reference.
Definition Cabana_Grid_SparseArray.hpp:372
Cabana::Tuple< member_types > tuple_type
AoSoA tuple type.
Definition Cabana_Grid_SparseArray.hpp:353
MeshType mesh_type
Mesh type.
Definition Cabana_Grid_SparseArray.hpp:331
KOKKOS_FORCEINLINE_FUNCTION size_type numSoA() const
Get the number of SoA inside an AoSoA structure.
Definition Cabana_Grid_SparseArray.hpp:430
KOKKOS_FORCEINLINE_FUNCTION soa_type & accessTile(const Value tile_id) const
(Device) Access tile SoA from tile-related information
Definition Cabana_Grid_SparseArray.hpp:475
SparseArray(const std::string label, array_layout layout)
(Host) Constructor
Definition Cabana_Grid_SparseArray.hpp:363
KOKKOS_FUNCTION bool empty() const
Test if the AoSoA array is empty.
Definition Cabana_Grid_SparseArray.hpp:427
MemorySpace memory_space
Kokkos memory space.
Definition Cabana_Grid_SparseArray.hpp:322
void shrinkToFit()
Shrink allocation to fit the valid size.
Definition Cabana_Grid_SparseArray.hpp:410
void reserve(const size_type n)
Reserve the AoSoA array according to the input.
Definition Cabana_Grid_SparseArray.hpp:398
std::string label() const
Get array label (description)
Definition Cabana_Grid_SparseArray.hpp:377
static constexpr std::size_t num_space_dim
Dimension number.
Definition Cabana_Grid_SparseArray.hpp:335
void reserveFromMap(const double factor=1.2)
Reserve the AoSoA array according to the sparse map info in layout.
Definition Cabana_Grid_SparseArray.hpp:404
KOKKOS_FORCEINLINE_FUNCTION soa_type::template member_reference_type< M > get(const int tile_id, const int cell_id) const
Access element in a hierarchical manner, from 1D tile ID and then 1D local cell ID.
Definition Cabana_Grid_SparseArray.hpp:641
array_layout & layout()
Get reference to the array layout (device accessible)
Definition Cabana_Grid_SparseArray.hpp:379
void resize(const size_type n)
Resize the AoSoA array according to the input.
Definition Cabana_Grid_SparseArray.hpp:387
KOKKOS_FUNCTION size_type capacity()
Get AoSoA capacity.
Definition Cabana_Grid_SparseArray.hpp:421
typename memory_space::execution_space execution_space
Default execution space.
Definition Cabana_Grid_SparseArray.hpp:325
KOKKOS_FORCEINLINE_FUNCTION soa_type::template member_reference_type< M > get(const int tile_id, const Kokkos::Array< int, 3 > local_cell_ijk, Indices &&... ids) const
Access element in a hierarchical manner, from 1D tile ID and then local cell IJK, access correspondin...
Definition Cabana_Grid_SparseArray.hpp:588
void clear()
Definition Cabana_Grid_SparseArray.hpp:413
KOKKOS_FORCEINLINE_FUNCTION soa_type::template member_reference_type< M > get(const Kokkos::Array< int, 3 > tile_ijk, const Kokkos::Array< int, 3 > local_cell_ijk, Indices &&... ids) const
Access element in a hierarchical manner, from tile IJK and then local cell IJK, access corresponding ...
Definition Cabana_Grid_SparseArray.hpp:550
Cabana::AoSoA< member_types, memory_space, vector_length > aosoa_type
AosoA type.
Definition Cabana_Grid_SparseArray.hpp:349
void resize()
Reserve the AoSoA array according to the sparse map info in layout.
Definition Cabana_Grid_SparseArray.hpp:392
KOKKOS_FORCEINLINE_FUNCTION soa_type::template member_reference_type< M > get(const int tile_id, const Kokkos::Array< int, 3 > local_cell_ijk) const
Access element in a hierarchical manner, from 1D tile ID and then local cell IJK.
Definition Cabana_Grid_SparseArray.hpp:606
static constexpr unsigned long long cell_mask_per_tile
Cell ID mask inside a tile.
Definition Cabana_Grid_SparseArray.hpp:340
KOKKOS_FORCEINLINE_FUNCTION soa_type::template member_reference_type< M > get(const Kokkos::Array< int, 3 > tile_ijk, const Kokkos::Array< int, 3 > local_cell_ijk) const
Access element in a hierarchical manner, from tile IJK and then local cell IJK.
Definition Cabana_Grid_SparseArray.hpp:569
KOKKOS_FORCEINLINE_FUNCTION soa_type & accessTile(const int tile_i, const int tile_j, const int tile_k) const
(Device) Access tile SoA from tile-related information
Definition Cabana_Grid_SparseArray.hpp:461
void registerSparseGrid(PositionSliceType &positions, int particle_num, const int p2g_radius=1)
Register valid grids in sparse map according to input particle positions.
Definition Cabana_Grid_SparseArray.hpp:447
KOKKOS_FORCEINLINE_FUNCTION soa_type & accessTileFromCell(const int cell_i, const int cell_j, const int cell_k) const
(Device) Access tile SoA from cell-related information
Definition Cabana_Grid_SparseArray.hpp:485
SparseArray< DataTypes, MemorySpace, EntityType, MeshType, SparseMapType > sparse_array_type
self type
Definition Cabana_Grid_SparseArray.hpp:319
DataTypes member_types
DataTypes Data types (Cabana::MemberTypes).
Definition Cabana_Grid_SparseArray.hpp:345
KOKKOS_FORCEINLINE_FUNCTION tuple_type getTuple(const Key tile_key, const int cell_local_id) const
Access AoSoA tuple from tile key and local cell id.
Definition Cabana_Grid_SparseArray.hpp:500
Cabana::SoA< member_types, vector_length > soa_type
SoA Type.
Definition Cabana_Grid_SparseArray.hpp:351
typename memory_space::size_type size_type
Memory space size type.
Definition Cabana_Grid_SparseArray.hpp:327
KOKKOS_FUNCTION size_type size() const
Get AoSoA size (valid number of elements)
Definition Cabana_Grid_SparseArray.hpp:424
SparseArrayLayout< member_types, entity_type, mesh_type, sparse_map_type > array_layout
Sparse array layout type.
Definition Cabana_Grid_SparseArray.hpp:356
Core: particle data structures and algorithms.
Definition Cabana_AoSoA.hpp:36
KOKKOS_FORCEINLINE_FUNCTION std::enable_if< is_parameter_pack< ParameterPack_t >::value, typenameParameterPack_t::templatevalue_type< N > & >::type get(ParameterPack_t &pp)
Get an element from a parameter pack.
Definition Cabana_ParameterPack.hpp:129
Sparse array layout static type checker.
Definition Cabana_Grid_SparseArray.hpp:258
Sparse array static type checker.
Definition Cabana_Grid_SparseArray.hpp:661
Definition Cabana_Grid_Types.hpp:337
Definition Cabana_SoA.hpp:32
Definition Cabana_Tuple.hpp:32