Cabana 0.8.0-dev
 
Loading...
Searching...
No Matches
Cabana_ExecutionPolicy.hpp
Go to the documentation of this file.
1/****************************************************************************
2 * Copyright (c) 2018-2023 by the Cabana authors *
3 * All rights reserved. *
4 * *
5 * This file is part of the Cabana library. Cabana is distributed under a *
6 * BSD 3-clause license. For the licensing terms see the LICENSE file in *
7 * the top-level directory. *
8 * *
9 * SPDX-License-Identifier: BSD-3-Clause *
10 ****************************************************************************/
11
16#ifndef CABANA_EXECUTIONPOLICY_HPP
17#define CABANA_EXECUTIONPOLICY_HPP
18
19#include <impl/Cabana_Index.hpp>
20
21#include <Kokkos_Core.hpp>
22
23#include <type_traits>
24
25namespace Cabana
26{
27//---------------------------------------------------------------------------//
28namespace Impl
29{
31
38template <int VectorLength, class IndexType>
39class StructRange
40{
41 public:
42 KOKKOS_INLINE_FUNCTION
43 static constexpr IndexType structBegin( const IndexType begin )
44 {
45 return Index<VectorLength>::s( begin );
46 }
47
48 KOKKOS_INLINE_FUNCTION
49 static constexpr IndexType structEnd( const IndexType end )
50 {
51 // If the end is also at the front of an array that means the struct
52 // index of end is also the ending struct index. If not, we are not
53 // iterating all the way through the arrays of the last struct. In
54 // this case we add 1 to ensure that the loop over structs loops
55 // through all structs with data.
56 return ( 0 == Index<VectorLength>::a( end ) )
57 ? Index<VectorLength>::s( end )
58 : Index<VectorLength>::s( end ) + 1;
59 }
60
61 KOKKOS_INLINE_FUNCTION
62 static constexpr IndexType size( const IndexType begin,
63 const IndexType end )
64 {
65 return structEnd( end ) - structBegin( begin );
66 }
67};
68
70} // end namespace Impl
71
72//---------------------------------------------------------------------------//
79template <int VectorLength, class... Properties>
80class SimdPolicy : public Kokkos::TeamPolicy<Properties...,
81 Kokkos::Schedule<Kokkos::Dynamic>>
82{
83 public:
85 using base_type =
86 Kokkos::TeamPolicy<Properties..., Kokkos::Schedule<Kokkos::Dynamic>>;
88 using index_type = typename base_type::index_type;
89
97 SimdPolicy( const index_type begin, const index_type end )
98 : base_type(
99 Impl::StructRange<VectorLength, index_type>::size( begin, end ),
100 1, VectorLength )
101 , _struct_begin(
102 Impl::StructRange<VectorLength, index_type>::structBegin(
103 begin ) )
104 , _struct_end(
105 Impl::StructRange<VectorLength, index_type>::structEnd( end ) )
106 , _array_begin( Impl::Index<VectorLength>::a( begin ) )
107 , _array_end( Impl::Index<VectorLength>::a( end ) )
108 {
109 }
110
112 KOKKOS_INLINE_FUNCTION index_type structBegin() const
113 {
114 return _struct_begin;
115 }
116
118 KOKKOS_INLINE_FUNCTION index_type structEnd() const { return _struct_end; }
119
121 KOKKOS_INLINE_FUNCTION index_type arrayBegin( const index_type s ) const
122 {
123 // If the given struct index is also the index of the struct index in
124 // begin, use the starting array index. If not, that means we have
125 // passed the first struct and all subsequent structs start at array
126 // index 0.
127 return ( s == _struct_begin ) ? _array_begin : 0;
128 }
129
131 KOKKOS_INLINE_FUNCTION index_type arrayEnd( const index_type s ) const
132 {
133 // If we are in the last unfilled struct then use the array
134 // index of end. If not, we are looping through the current array all
135 // the way to the end so use the vector length.
136 return ( ( s == _struct_end - 1 ) && ( _array_end != 0 ) )
137 ? _array_end
138 : VectorLength;
139 }
140
141 private:
142 index_type _struct_begin;
143 index_type _struct_end;
144 index_type _array_begin;
145 index_type _array_end;
146};
147
148//---------------------------------------------------------------------------//
149
150} // end namespace Cabana
151
152#endif // end CABANA_EXECUTIONPOLICY_HPP
AoSoA indexing.
Kokkos::TeamPolicy< Properties..., Kokkos::Schedule< Kokkos::Dynamic > > base_type
Kokkos team policy.
Definition Cabana_ExecutionPolicy.hpp:85
SimdPolicy(const index_type begin, const index_type end)
Range constructor.
Definition Cabana_ExecutionPolicy.hpp:97
KOKKOS_INLINE_FUNCTION index_type arrayEnd(const index_type s) const
Given a struct id get the ending array index.
Definition Cabana_ExecutionPolicy.hpp:131
typename base_type::index_type index_type
Index type.
Definition Cabana_ExecutionPolicy.hpp:88
KOKKOS_INLINE_FUNCTION index_type arrayBegin(const index_type s) const
Given a struct id get the beginning array index.
Definition Cabana_ExecutionPolicy.hpp:121
KOKKOS_INLINE_FUNCTION index_type structEnd() const
Get the ending struct index.
Definition Cabana_ExecutionPolicy.hpp:118
KOKKOS_INLINE_FUNCTION index_type structBegin() const
Get the starting struct index.
Definition Cabana_ExecutionPolicy.hpp:112
Core: particle data structures and algorithms.
Definition Cabana_AoSoA.hpp:36
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:1012