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 "register sparse map in sparse array layout",
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>;
327 Cabana::Impl::deprecated( Kokkos::is_device<MemorySpace>() ) );
330 using device_type [[deprecated]] =
typename memory_space::device_type;
345 sparse_map_type::cell_bits_per_tile;
348 sparse_map_type::cell_mask_per_tile;
384 std::string
label()
const {
return _data.label(); }
413 reserve( _layout.reservedCellSize( factor ) );
436 KOKKOS_FORCEINLINE_FUNCTION
439 KOKKOS_FORCEINLINE_FUNCTION
442 return _data.arraySize( s );
453 template <
class PositionSliceType>
455 const int p2g_radius = 1 )
457 _layout.template registerSparseMap<execution_space, PositionSliceType>(
458 positions, particle_num, p2g_radius );
459 this->
resize( _layout.sparseMap().sizeCell() );
467 KOKKOS_FORCEINLINE_FUNCTION
469 const int tile_k )
const
471 auto tile_id = _layout.queryTileFromTileId( tile_i, tile_j, tile_k );
472 return _data.access( tile_id );
480 template <
typename Value>
481 KOKKOS_FORCEINLINE_FUNCTION
soa_type&
484 return _data.access( tile_id );
491 KOKKOS_FORCEINLINE_FUNCTION
493 const int cell_k )
const
495 auto tile_id = _layout.queryTile( cell_i, cell_j, cell_k );
496 return _data.access( tile_id );
505 template <
typename Key>
507 getTuple(
const Key tile_key,
const int cell_local_id )
const
509 auto tile_id = _layout.queryTileFromTileKey( tile_key );
521 template <std::size_t M,
typename... Indices>
522 KOKKOS_FORCEINLINE_FUNCTION
523 typename soa_type::template member_reference_type<M>
524 get(
const Kokkos::Array<int, 3> cell_ijk, Indices&&... ids )
const
528 _layout.cellLocalId( cell_ijk[0], cell_ijk[1], cell_ijk[2] );
536 template <std::
size_t M>
537 KOKKOS_FORCEINLINE_FUNCTION
538 typename soa_type::template member_reference_type<M>
539 get(
const Kokkos::Array<int, 3> cell_ijk )
const
543 _layout.cellLocalId( cell_ijk[0], cell_ijk[1], cell_ijk[2] );
554 template <std::size_t M,
typename... Indices>
555 KOKKOS_FORCEINLINE_FUNCTION
556 typename soa_type::template member_reference_type<M>
557 get(
const Kokkos::Array<int, 3> tile_ijk,
558 const Kokkos::Array<int, 3> local_cell_ijk,
559 Indices&&... ids )
const
561 auto& soa =
accessTile( tile_ijk[0], tile_ijk[1], tile_ijk[2] );
562 auto array_index = _layout.cellLocalId(
563 local_cell_ijk[0], local_cell_ijk[1], local_cell_ijk[2] );
573 template <std::
size_t M>
574 KOKKOS_FORCEINLINE_FUNCTION
575 typename soa_type::template member_reference_type<M>
576 get(
const Kokkos::Array<int, 3> tile_ijk,
577 const Kokkos::Array<int, 3> local_cell_ijk )
const
579 auto& soa =
accessTile( tile_ijk[0], tile_ijk[1], tile_ijk[2] );
580 auto array_index = _layout.cellLocalId(
581 local_cell_ijk[0], local_cell_ijk[1], local_cell_ijk[2] );
592 template <std::size_t M,
typename... Indices>
593 KOKKOS_FORCEINLINE_FUNCTION
594 typename soa_type::template member_reference_type<M>
595 get(
const int tile_id,
const Kokkos::Array<int, 3> local_cell_ijk,
596 Indices&&... ids )
const
598 auto& soa = _data.access( tile_id );
599 auto array_index = _layout.cellLocalId(
600 local_cell_ijk[0], local_cell_ijk[1], local_cell_ijk[2] );
610 template <std::
size_t M>
611 KOKKOS_FORCEINLINE_FUNCTION
612 typename soa_type::template member_reference_type<M>
614 const Kokkos::Array<int, 3> local_cell_ijk )
const
616 auto& soa = _data.access( tile_id );
617 auto array_index = _layout.cellLocalId(
618 local_cell_ijk[0], local_cell_ijk[1], local_cell_ijk[2] );
630 template <std::size_t M,
typename... Indices>
631 KOKKOS_FORCEINLINE_FUNCTION
632 typename soa_type::template member_reference_type<M>
633 get(
const int tile_id,
const int cell_id, Indices&&... ids )
const
635 auto& soa = _data.access( tile_id );
645 template <std::
size_t M>
646 KOKKOS_FORCEINLINE_FUNCTION
647 typename soa_type::template member_reference_type<M>
648 get(
const int tile_id,
const int cell_id )
const
650 auto& soa = _data.access( tile_id );
672template <
class DataTypes,
class MemorySpace,
class EntityType,
class MeshType,
675 SparseArray<DataTypes, MemorySpace, EntityType, MeshType, SparseMapType>>
676 :
public std::true_type
681template <
class DataTypes,
class MemorySpace,
class EntityType,
class MeshType,
684 MeshType, SparseMapType>>
685 :
public std::true_type
698template <
class MemorySpace,
class DataTypes,
class EntityType,
class MeshType,
701 const std::string label,
704 return std::make_shared<
SparseArray<DataTypes, MemorySpace, EntityType,
705 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:700
Sparse Local grid and related implementations.
Array-of-Struct-of-Arrays.
Definition Cabana_AoSoA.hpp:121
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:354
const array_layout & layout() const
Get const reference to the array layout (device accessible)
Definition Cabana_Grid_SparseArray.hpp:388
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:539
SparseMapType sparse_map_type
Sparse map type.
Definition Cabana_Grid_SparseArray.hpp:340
EntityType entity_type
Array entity type (node, cell, face, edge).
Definition Cabana_Grid_SparseArray.hpp:336
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:524
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:633
static constexpr unsigned long long cell_bits_per_tile
Least bits required to represent all cells inside a tile.
Definition Cabana_Grid_SparseArray.hpp:344
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:440
aosoa_type & aosoa()
Get AoSoA reference.
Definition Cabana_Grid_SparseArray.hpp:379
Cabana::Tuple< member_types > tuple_type
AoSoA tuple type.
Definition Cabana_Grid_SparseArray.hpp:360
MeshType mesh_type
Mesh type.
Definition Cabana_Grid_SparseArray.hpp:338
KOKKOS_FORCEINLINE_FUNCTION size_type numSoA() const
Get the number of SoA inside an AoSoA structure.
Definition Cabana_Grid_SparseArray.hpp:437
KOKKOS_FORCEINLINE_FUNCTION soa_type & accessTile(const Value tile_id) const
(Device) Access tile SoA from tile-related information
Definition Cabana_Grid_SparseArray.hpp:482
SparseArray(const std::string label, array_layout layout)
(Host) Constructor
Definition Cabana_Grid_SparseArray.hpp:370
KOKKOS_FUNCTION bool empty() const
Test if the AoSoA array is empty.
Definition Cabana_Grid_SparseArray.hpp:434
void shrinkToFit()
Shrink allocation to fit the valid size.
Definition Cabana_Grid_SparseArray.hpp:417
void reserve(const size_type n)
Reserve the AoSoA array according to the input.
Definition Cabana_Grid_SparseArray.hpp:405
std::string label() const
Get array label (description)
Definition Cabana_Grid_SparseArray.hpp:384
static constexpr std::size_t num_space_dim
Dimension number.
Definition Cabana_Grid_SparseArray.hpp:342
void reserveFromMap(const double factor=1.2)
Reserve the AoSoA array according to the sparse map info in layout.
Definition Cabana_Grid_SparseArray.hpp:411
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:648
array_layout & layout()
Get reference to the array layout (device accessible)
Definition Cabana_Grid_SparseArray.hpp:386
void resize(const size_type n)
Resize the AoSoA array according to the input.
Definition Cabana_Grid_SparseArray.hpp:394
KOKKOS_FUNCTION size_type capacity()
Get AoSoA capacity.
Definition Cabana_Grid_SparseArray.hpp:428
typename memory_space::execution_space execution_space
Default execution space.
Definition Cabana_Grid_SparseArray.hpp:332
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:595
void clear()
Definition Cabana_Grid_SparseArray.hpp:420
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:557
Cabana::AoSoA< member_types, memory_space, vector_length > aosoa_type
AosoA type.
Definition Cabana_Grid_SparseArray.hpp:356
void resize()
Reserve the AoSoA array according to the sparse map info in layout.
Definition Cabana_Grid_SparseArray.hpp:399
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:613
static constexpr unsigned long long cell_mask_per_tile
Cell ID mask inside a tile.
Definition Cabana_Grid_SparseArray.hpp:347
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:576
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:468
typename MemorySpace::memory_space memory_space
Memory space.
Definition Cabana_Grid_SparseArray.hpp:324
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:454
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:492
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:352
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:507
Cabana::SoA< member_types, vector_length > soa_type
SoA Type.
Definition Cabana_Grid_SparseArray.hpp:358
typename memory_space::size_type size_type
Memory space size type.
Definition Cabana_Grid_SparseArray.hpp:334
KOKKOS_FUNCTION size_type size() const
Get AoSoA size (valid number of elements)
Definition Cabana_Grid_SparseArray.hpp:431
SparseArrayLayout< member_types, entity_type, mesh_type, sparse_map_type > array_layout
Sparse array layout type.
Definition Cabana_Grid_SparseArray.hpp:363
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:668
Definition Cabana_Grid_Types.hpp:337
Definition Cabana_SoA.hpp:32
Definition Cabana_Tuple.hpp:32