16#ifndef CABANA_SLICE_HPP
17#define CABANA_SLICE_HPP
22#include <impl/Cabana_TypeTraits.hpp>
24#include <Kokkos_Core.hpp>
44template <
typename T, std::
size_t Rank,
int VectorLength,
int Str
ide>
45struct KokkosDataTypeImpl;
48template <
typename T,
int VectorLength,
int Str
ide>
49struct KokkosDataTypeImpl<T, 0, VectorLength, Stride>
51 using value_type =
typename std::remove_all_extents<T>::type;
52 using data_type = value_type* [VectorLength];
53 using array_layout = Kokkos::LayoutStride;
55 inline static array_layout createLayout(
const std::size_t num_soa )
57 return array_layout( num_soa, Stride, VectorLength, 1 );
62template <
typename T,
int VectorLength,
int Str
ide>
63struct KokkosDataTypeImpl<T, 1, VectorLength, Stride>
65 using value_type =
typename std::remove_all_extents<T>::type;
66 static constexpr std::size_t D0 = std::extent<T, 0>::value;
67 using data_type = value_type* [VectorLength][D0];
68 using array_layout = Kokkos::LayoutStride;
70 inline static array_layout createLayout(
const std::size_t num_soa )
72 return array_layout( num_soa, Stride, VectorLength, 1, D0,
78template <
typename T,
int VectorLength,
int Str
ide>
79struct KokkosDataTypeImpl<T, 2, VectorLength, Stride>
81 using value_type =
typename std::remove_all_extents<T>::type;
82 static constexpr std::size_t D0 = std::extent<T, 0>::value;
83 static constexpr std::size_t D1 = std::extent<T, 1>::value;
84 using data_type = value_type* [VectorLength][D0][D1];
85 using array_layout = Kokkos::LayoutStride;
87 inline static array_layout createLayout(
const std::size_t num_soa )
89 return array_layout( num_soa, Stride, VectorLength, 1, D0,
90 VectorLength * D1, D1, VectorLength );
95template <
typename T,
int VectorLength,
int Str
ide>
96struct KokkosDataTypeImpl<T, 3, VectorLength, Stride>
98 using value_type =
typename std::remove_all_extents<T>::type;
99 static constexpr std::size_t D0 = std::extent<T, 0>::value;
100 static constexpr std::size_t D1 = std::extent<T, 1>::value;
101 static constexpr std::size_t D2 = std::extent<T, 2>::value;
102 using data_type = value_type* [VectorLength][D0][D1][D2];
103 using array_layout = Kokkos::LayoutStride;
105 inline static array_layout createLayout(
const std::size_t num_soa )
107 return array_layout( num_soa, Stride, VectorLength, 1, D0,
108 VectorLength * D1 * D2, D1, VectorLength * D2, D2,
114template <
typename T,
int VectorLength,
int Str
ide>
117 using kokkos_data_type =
118 KokkosDataTypeImpl<T, std::rank<T>::value, VectorLength, Stride>;
119 using data_type =
typename kokkos_data_type::data_type;
120 using array_layout =
typename kokkos_data_type::array_layout;
122 inline static array_layout createLayout(
const std::size_t num_soa )
124 return kokkos_data_type::createLayout( num_soa );
130template <
typename T,
int VectorLength,
int Stride,
131 typename std::enable_if<
132 Impl::IsVectorLengthValid<VectorLength>::value,
int>::type = 0>
133struct KokkosViewWrapper
136 typename KokkosDataType<T, VectorLength, Stride>::data_type;
139 typename KokkosDataType<T, VectorLength, Stride>::array_layout;
141 inline static array_layout createLayout(
const std::size_t num_soa )
143 return KokkosDataType<T, VectorLength, Stride>::createLayout( num_soa );
158template <
typename DataType,
typename MemorySpace,
typename MemoryAccessType,
159 int VectorLength,
int Stride>
164 static_assert( Impl::IsVectorLengthValid<VectorLength>::value,
165 "Vector length must be valid" );
173 static_assert( Kokkos::is_memory_space<MemorySpace>() );
179 "Slice memory access type must be a Cabana access type" );
200 Impl::KokkosViewWrapper<DataType, vector_length, soa_stride>;
204 Kokkos::View<
typename view_wrapper::data_type,
205 typename view_wrapper::array_layout, MemorySpace,
206 typename MemoryAccessType::kokkos_memory_traits>;
221 static constexpr std::size_t
data_rank = std::rank<DataType>::value;
274 std::strcpy( _label,
label.c_str() );
289 std::strcpy( _label, rhs._label );
307 std::strcpy( _label, rhs._label );
316 std::string
label()
const {
return std::string( _label ); }
322 KOKKOS_INLINE_FUNCTION
329 KOKKOS_INLINE_FUNCTION
337 KOKKOS_INLINE_FUNCTION
342 if (
static_cast<size_type>( s ) < _view.extent( 0 ) - 1 )
361 template <
typename U = DataType>
362 KOKKOS_FORCEINLINE_FUNCTION
363 typename std::enable_if<( 0 == std::rank<U>::value &&
364 std::is_same<U, DataType>::value ),
368 return _view( s, a );
372 template <
typename U = DataType>
373 KOKKOS_FORCEINLINE_FUNCTION
374 typename std::enable_if<( 1 == std::rank<U>::value &&
375 std::is_same<U, DataType>::value ),
379 return _view( s, a, d0 );
383 template <
typename U = DataType>
384 KOKKOS_FORCEINLINE_FUNCTION
385 typename std::enable_if<( 2 == std::rank<U>::value &&
386 std::is_same<U, DataType>::value ),
391 return _view( s, a, d0, d1 );
395 template <
typename U = DataType>
396 KOKKOS_FORCEINLINE_FUNCTION
397 typename std::enable_if<( 3 == std::rank<U>::value &&
398 std::is_same<U, DataType>::value ),
403 return _view( s, a, d0, d1, d2 );
410 template <
typename U = DataType>
411 KOKKOS_FORCEINLINE_FUNCTION
412 typename std::enable_if<( 0 == std::rank<U>::value &&
413 std::is_same<U, DataType>::value ),
421 template <
typename U = DataType>
422 KOKKOS_FORCEINLINE_FUNCTION
423 typename std::enable_if<( 1 == std::rank<U>::value &&
424 std::is_same<U, DataType>::value ),
432 template <
typename U = DataType>
433 KOKKOS_FORCEINLINE_FUNCTION
434 typename std::enable_if<( 2 == std::rank<U>::value &&
435 std::is_same<U, DataType>::value ),
444 template <
typename U = DataType>
445 KOKKOS_FORCEINLINE_FUNCTION
446 typename std::enable_if<( 3 == std::rank<U>::value &&
447 std::is_same<U, DataType>::value ),
462 KOKKOS_INLINE_FUNCTION
471 KOKKOS_INLINE_FUNCTION
481 KOKKOS_INLINE_FUNCTION
490 KOKKOS_INLINE_FUNCTION
496 KOKKOS_INLINE_FUNCTION
514struct is_slice_impl :
public std::false_type
520template <
typename DataType,
typename MemorySpace,
typename MemoryAccessType,
521 int VectorLength,
int Stride>
523 Slice<DataType, MemorySpace, MemoryAccessType, VectorLength, Stride>>
524 :
public std::true_type
531struct is_slice :
public is_slice_impl<typename std::remove_cv<T>::type>::type
540template <
class ExecutionSpace,
class ViewType,
class SliceType>
542 ExecutionSpace exec_space, ViewType& view,
const SliceType&
slice,
543 const std::size_t begin,
const std::size_t end,
544 typename std::enable_if<( SliceType::view_rank == 2 ),
int*>::type = 0 )
546 Kokkos::parallel_for(
547 "Cabana::copySliceToView::Rank0",
548 Kokkos::RangePolicy<ExecutionSpace>( exec_space, begin, end ),
549 KOKKOS_LAMBDA(
const int i ) { view( i - begin ) =
slice( i ); } );
553template <
class ExecutionSpace,
class ViewType,
class SliceType>
555 ExecutionSpace exec_space, ViewType& view,
const SliceType&
slice,
556 const std::size_t begin,
const std::size_t end,
557 typename std::enable_if<( SliceType::view_rank == 3 ),
int*>::type = 0 )
559 Kokkos::parallel_for(
560 "Cabana::copySliceToView::Rank1",
561 Kokkos::RangePolicy<ExecutionSpace>( exec_space, begin, end ),
562 KOKKOS_LAMBDA(
const int i ) {
563 for ( std::size_t d0 = 0; d0 <
slice.extent( 2 ); ++d0 )
564 view( i - begin, d0 ) =
slice( i, d0 );
569template <
class ExecutionSpace,
class ViewType,
class SliceType>
571 ExecutionSpace exec_space, ViewType& view,
const SliceType&
slice,
572 const std::size_t begin,
const std::size_t end,
573 typename std::enable_if<( SliceType::view_rank == 4 ),
int*>::type = 0 )
575 Kokkos::parallel_for(
576 "Cabana::copySliceToView::Rank2",
577 Kokkos::RangePolicy<ExecutionSpace>( exec_space, begin, end ),
578 KOKKOS_LAMBDA(
const int i ) {
579 for ( std::size_t d0 = 0; d0 <
slice.extent( 2 ); ++d0 )
580 for ( std::size_t d1 = 0; d1 <
slice.extent( 3 ); ++d1 )
581 view( i - begin, d0, d1 ) =
slice( i, d0, d1 );
586template <
class ViewType,
class SliceType>
588 const std::size_t begin,
const std::size_t end )
590 using exec_space =
typename SliceType::execution_space;
599template <
class ExecutionSpace,
class SliceType,
class ViewType>
601 ExecutionSpace exec_space, SliceType&
slice,
const ViewType& view,
602 const std::size_t begin,
const std::size_t end,
603 typename std::enable_if<( SliceType::view_rank == 2 ),
int*>::type = 0 )
605 Kokkos::parallel_for(
606 "Cabana::copyViewToSlice::Rank0",
607 Kokkos::RangePolicy<ExecutionSpace>( exec_space, begin, end ),
608 KOKKOS_LAMBDA(
const int i ) {
slice( i - begin ) = view( i ); } );
612template <
class ExecutionSpace,
class SliceType,
class ViewType>
614 ExecutionSpace exec_space, SliceType&
slice,
const ViewType& view,
615 const std::size_t begin,
const std::size_t end,
616 typename std::enable_if<( SliceType::view_rank == 3 ),
int*>::type = 0 )
618 Kokkos::parallel_for(
619 "Cabana::copyViewToSlice::Rank1",
620 Kokkos::RangePolicy<ExecutionSpace>( exec_space, begin, end ),
621 KOKKOS_LAMBDA(
const int i ) {
622 for ( std::size_t d0 = 0; d0 <
slice.extent( 2 ); ++d0 )
623 slice( i - begin, d0 ) = view( i, d0 );
628template <
class ExecutionSpace,
class SliceType,
class ViewType>
630 ExecutionSpace exec_space, SliceType&
slice,
const ViewType& view,
631 const std::size_t begin,
const std::size_t end,
632 typename std::enable_if<( SliceType::view_rank == 4 ),
int*>::type = 0 )
634 Kokkos::parallel_for(
635 "Cabana::copyViewToSlice::Rank2",
636 Kokkos::RangePolicy<ExecutionSpace>( exec_space, begin, end ),
637 KOKKOS_LAMBDA(
const int i ) {
638 for ( std::size_t d0 = 0; d0 <
slice.extent( 2 ); ++d0 )
639 for ( std::size_t d1 = 0; d1 <
slice.extent( 3 ); ++d1 )
640 slice( i - begin, d0, d1 ) = view( i, d0, d1 );
645template <
class ViewType,
class SliceType>
647 const std::size_t begin,
const std::size_t end )
649 using exec_space =
typename SliceType::execution_space;
654template <
class SliceType>
656 [[maybe_unused]] SliceType
slice, [[maybe_unused]]
const std::size_t
size,
664template <
class ViewType>
666 [[maybe_unused]] ViewType view, [[maybe_unused]]
const std::size_t
size,
667 typename std::enable_if<Kokkos::is_view<ViewType>::value,
int>::type* = 0 )
670 assert( view.extent( 0 ) ==
size );
676template <
class SliceType>
677KOKKOS_INLINE_FUNCTION
auto
685template <
class ViewType>
686KOKKOS_INLINE_FUNCTION
auto size(
688 typename std::enable_if<Kokkos::is_view<ViewType>::value,
int>::type* = 0 )
690 return view.extent( 0 );
Memory access type checking.
Class for converting between 1d and 2d aosoa indices.
Definition Cabana_Index.hpp:38
static KOKKOS_FORCEINLINE_FUNCTION constexpr std::size_t a(const std::size_t i)
Definition Cabana_Index.hpp:77
static KOKKOS_FORCEINLINE_FUNCTION constexpr std::size_t s(const std::size_t i)
Definition Cabana_Index.hpp:62
A slice of an array-of-structs-of-arrays with data access to a single multidimensional member.
Definition Cabana_Slice.hpp:161
KOKKOS_FORCEINLINE_FUNCTION std::enable_if<(3==std::rank< U >::value &&std::is_same< U, DataType >::value), reference_type >::type access(const size_type s, const size_type a, const size_type d0, const size_type d1, const size_type d2) const
2d access for Rank 3
Definition Cabana_Slice.hpp:400
typename kokkos_view::array_layout view_layout
Definition Cabana_Slice.hpp:218
KOKKOS_INLINE_FUNCTION pointer_type data() const
Definition Cabana_Slice.hpp:463
KOKKOS_INLINE_FUNCTION size_type size() const
Definition Cabana_Slice.hpp:323
typename kokkos_view::pointer_type pointer_type
Definition Cabana_Slice.hpp:216
typename kokkos_view::value_type value_type
Definition Cabana_Slice.hpp:214
static constexpr std::size_t data_rank
Definition Cabana_Slice.hpp:221
Slice & operator=(const Slice< DataType, MemorySpace, MAT, VectorLength, Stride > &rhs)
Assignment operator for different memory spaces for assigning new memory access traits to the view.
Definition Cabana_Slice.hpp:302
Slice< member_data_type< M >, memory_space, RandomAccessMemory, VectorLength, Stride > random_access_slice
Definition Cabana_Slice.hpp:232
static constexpr std::size_t view_rank
Definition Cabana_Slice.hpp:223
std::string label() const
Definition Cabana_Slice.hpp:316
Kokkos::View< typename view_wrapper::data_type, typename view_wrapper::array_layout, memory_space, typename DefaultAccessMemory::kokkos_memory_traits > kokkos_view
Definition Cabana_Slice.hpp:203
KOKKOS_FORCEINLINE_FUNCTION std::enable_if<(0==std::rank< U >::value &&std::is_same< U, DataType >::value), reference_type >::type access(const size_type s, const size_type a) const
2d access for Rank 0
Definition Cabana_Slice.hpp:366
Impl::Index< vector_length > index_type
Definition Cabana_Slice.hpp:190
KOKKOS_INLINE_FUNCTION size_type stride(const size_type d) const
Get the stride of a given raw slice dimension. This includes the struct dimension,...
Definition Cabana_Slice.hpp:491
static constexpr std::size_t max_supported_rank
Definition Cabana_Slice.hpp:193
Slice(const pointer_type data, const size_type size, const size_type num_soa, const std::string &label="")
Constructor.
Definition Cabana_Slice.hpp:269
KOKKOS_INLINE_FUNCTION size_type arraySize(const size_type s) const
Get the size of the data array at a given struct member index.
Definition Cabana_Slice.hpp:338
static constexpr int vector_length
Definition Cabana_Slice.hpp:184
typename kokkos_view::reference_type reference_type
Definition Cabana_Slice.hpp:212
KOKKOS_INLINE_FUNCTION constexpr size_type viewRank() const
Get the rank of the raw data for this slice. This includes the struct dimension, array dimension,...
Definition Cabana_Slice.hpp:472
typename kokkos_view::size_type size_type
Definition Cabana_Slice.hpp:209
KOKKOS_FORCEINLINE_FUNCTION std::enable_if<(1==std::rank< U >::value &&std::is_same< U, DataType >::value), reference_type >::type access(const size_type s, const size_type a, const size_type d0) const
2d access for Rank 1
Definition Cabana_Slice.hpp:377
static constexpr int soa_stride
Definition Cabana_Slice.hpp:187
Slice< member_data_type< M >, memory_space, DefaultAccessMemory, VectorLength, Stride > default_access_slice
Definition Cabana_Slice.hpp:226
Slice< member_data_type< M >, memory_space, AtomicAccessMemory, VectorLength, Stride > atomic_access_slice
Definition Cabana_Slice.hpp:229
Slice()
Default constructor.
Definition Cabana_Slice.hpp:257
Slice(const Slice< DataType, MemorySpace, MAT, VectorLength, Stride > &rhs)
Shallow copy constructor for different memory spaces for assigning new memory access traits to the vi...
Definition Cabana_Slice.hpp:285
memory_space memory_space
Definition Cabana_Slice.hpp:172
KOKKOS_FORCEINLINE_FUNCTION std::enable_if<(2==std::rank< U >::value &&std::is_same< U, DataType >::value), reference_type >::type access(const size_type s, const size_type a, const size_type d0, const size_type d1) const
2d access for Rank 2
Definition Cabana_Slice.hpp:388
DefaultAccessMemory memory_access_type
Definition Cabana_Slice.hpp:181
KOKKOS_INLINE_FUNCTION size_type extent(const size_type d) const
Get the extent of a given raw slice data dimension. This includes the struct dimension,...
Definition Cabana_Slice.hpp:482
typename memory_space::execution_space execution_space
Definition Cabana_Slice.hpp:176
KOKKOS_INLINE_FUNCTION kokkos_view view() const
Get the underlying Kokkos View managing the slice data.
Definition Cabana_Slice.hpp:497
KOKKOS_INLINE_FUNCTION size_type numSoA() const
Get the number of structs-of-arrays in the container.
Definition Cabana_Slice.hpp:330
static constexpr std::size_t max_label_length
Definition Cabana_Slice.hpp:196
Slice< member_data_type< M >, memory_space, DefaultAccessMemory, VectorLength, Stride > slice_type
Definition Cabana_Slice.hpp:168
Impl::KokkosViewWrapper< member_data_type< M >, vector_length, soa_stride > view_wrapper
Definition Cabana_Slice.hpp:199
Core: particle data structures and algorithms.
Definition Cabana_AoSoA.hpp:36
void copySliceToView(ExecutionSpace exec_space, ViewType &view, const SliceType &slice, const std::size_t begin, const std::size_t end, typename std::enable_if<(SliceType::view_rank==2), int * >::type=0)
Copy from slice to View. Rank-0.
Definition Cabana_Slice.hpp:541
AoSoA_t::template member_slice_type< M > slice(const AoSoA_t &aosoa, const std::string &slice_label="")
Create a slice from an AoSoA.
Definition Cabana_AoSoA.hpp:77
void copyViewToSlice(ExecutionSpace exec_space, SliceType &slice, const ViewType &view, const std::size_t begin, const std::size_t end, typename std::enable_if<(SliceType::view_rank==2), int * >::type=0)
Copy from View to slice. Rank-0.
Definition Cabana_Slice.hpp:600
KOKKOS_INLINE_FUNCTION auto size(SliceType slice, typename std::enable_if< is_slice< SliceType >::value, int >::type *=0)
Check slice size (differs from Kokkos View).
Definition Cabana_Slice.hpp:678
void checkSize(SliceType slice, const std::size_t size, typename std::enable_if< is_slice< SliceType >::value, int >::type *=0)
Check slice size (differs from Kokkos View).
Definition Cabana_Slice.hpp:655
Atomic memory access. All reads and writes are atomic.
Definition Cabana_Types.hpp:70
Definition Cabana_Types.hpp:37
Random access memory. Read-only and const with limited spatial locality.
Definition Cabana_Types.hpp:54
Memory access type checker.
Definition Cabana_Types.hpp:31
Slice static type checker.
Definition Cabana_Slice.hpp:532