IPPL API Reference
Independent Parallel Particle Layer C++ API
Loading...
Searching...
No Matches
NDIndex.hpp
1//
2// Class NDIndex
3// This is a simple wrapper around Index that just keeps track of
4// N of them and passes along requests for intersect, etc.
5//
6#include <iostream>
7
8namespace ippl {
9 template <unsigned Dim>
10 template <class... Args, typename std::enable_if<sizeof...(Args) == Dim, bool>::type>
11 KOKKOS_FUNCTION NDIndex<Dim>::NDIndex(const Args&... args)
12 : indices_m{args...} {}
13
14 template <unsigned Dim>
15 KOKKOS_FUNCTION NDIndex<Dim>::NDIndex(const Vector<unsigned, Dim>& sizes) {
16 for (unsigned int d = 0; d < Dim; ++d) {
17 indices_m[d] = Index(sizes[d]);
18 }
19 }
20
21 template <unsigned Dim>
22 KOKKOS_INLINE_FUNCTION const Index& NDIndex<Dim>::operator[](unsigned d) const noexcept {
23 return indices_m[d];
24 }
25
26 template <unsigned Dim>
27 KOKKOS_INLINE_FUNCTION Index& NDIndex<Dim>::operator[](unsigned d) noexcept {
28 return indices_m[d];
29 }
30
31 template <unsigned Dim>
32 KOKKOS_INLINE_FUNCTION unsigned NDIndex<Dim>::size() const noexcept {
33 unsigned s = indices_m[0].length();
34 for (unsigned int d = 1; d < Dim; ++d) {
35 s *= indices_m[d].length();
36 }
37 return s;
38 }
39
40 template <unsigned Dim>
41 KOKKOS_INLINE_FUNCTION bool NDIndex<Dim>::empty() const noexcept {
42 bool r = false;
43 for (unsigned d = 0; d < Dim; ++d) {
44 r = r || indices_m[d].empty();
45 }
46 return r;
47 }
48
49 template <unsigned Dim>
50 inline std::ostream& operator<<(std::ostream& out, const NDIndex<Dim>& idx) {
51 out << '{';
52 for (unsigned d = 0; d < Dim; ++d) {
53 out << idx[d] << ((d == Dim - 1) ? '}' : ',');
54 }
55 return out;
56 }
57
58 template <unsigned Dim>
59 KOKKOS_INLINE_FUNCTION NDIndex<Dim> NDIndex<Dim>::intersect(const NDIndex<Dim>& ndi) const {
61 for (unsigned d = 0; d < Dim; ++d) {
62 r[d] = indices_m[d].intersect(ndi[d]);
63 }
64 return r;
65 }
66
67 template <unsigned Dim>
68 KOKKOS_INLINE_FUNCTION NDIndex<Dim> NDIndex<Dim>::grow(int ncells) const {
70 for (unsigned d = 0; d < Dim; ++d) {
71 r[d] = indices_m[d].grow(ncells);
72 }
73 return r;
74 }
75
76 template <unsigned Dim>
77 KOKKOS_INLINE_FUNCTION NDIndex<Dim> NDIndex<Dim>::grow(int ncells, unsigned int dim) const {
78 NDIndex<Dim> r = *this;
79 r[dim] = indices_m[dim].grow(ncells);
80 return r;
81 }
82
83 template <unsigned Dim>
84 KOKKOS_INLINE_FUNCTION bool NDIndex<Dim>::touches(const NDIndex<Dim>& a) const {
85 bool touch = true;
86 for (unsigned int d = 0; (d < Dim) && touch; ++d) {
87 touch = touch && indices_m[d].touches(a.indices_m[d]);
88 }
89 return touch;
90 }
91
92 template <unsigned Dim>
93 KOKKOS_INLINE_FUNCTION bool NDIndex<Dim>::contains(const NDIndex<Dim>& a) const {
94 bool cont = true;
95 for (unsigned int d = 0; (d < Dim) && cont; ++d) {
96 cont = cont && indices_m[d].contains(a.indices_m[d]);
97 }
98 return cont;
99 }
100
101 template <unsigned Dim>
102 KOKKOS_INLINE_FUNCTION bool NDIndex<Dim>::split(NDIndex<Dim>& l, NDIndex<Dim>& r, unsigned d,
103 int i) const {
104 if (&l != this) {
105 l = *this;
106 }
107 if (&r != this) {
108 r = *this;
109 }
110 return indices_m[d].split(l[d], r[d], i);
111 }
112
113 template <unsigned Dim>
114 KOKKOS_INLINE_FUNCTION bool NDIndex<Dim>::split(NDIndex<Dim>& l, NDIndex<Dim>& r, unsigned d,
115 double a) const {
116 if (&l != this) {
117 l = *this;
118 }
119 if (&r != this) {
120 r = *this;
121 }
122 return indices_m[d].split(l[d], r[d], a);
123 }
124
125 template <unsigned Dim>
126 KOKKOS_INLINE_FUNCTION bool NDIndex<Dim>::split(NDIndex<Dim>& l, NDIndex<Dim>& r,
127 unsigned d) const {
128 if (&l != this) {
129 l = *this;
130 }
131 if (&r != this) {
132 r = *this;
133 }
134 return indices_m[d].split(l[d], r[d]);
135 }
136
137 template <unsigned Dim>
138 KOKKOS_INLINE_FUNCTION bool NDIndex<Dim>::split(NDIndex<Dim>& l, NDIndex<Dim>& r) const {
139 unsigned int max_dim = 0;
140 unsigned int max_length = 0;
141 for (unsigned int d = 0; d < Dim; ++d) {
142 if (indices_m[d].length() > max_length) {
143 max_dim = d;
144 max_length = indices_m[d].length();
145 }
146 }
147 return split(l, r, max_dim);
148 }
149
150 template <unsigned Dim>
151 KOKKOS_INLINE_FUNCTION Vector<size_t, Dim> NDIndex<Dim>::length() const {
152 Vector<size_t, Dim> result;
153 for (unsigned d = 0; d < Dim; ++d) {
154 result[d] = indices_m[d].length();
155 }
156 return result;
157 }
158
159 template <unsigned Dim>
160 KOKKOS_INLINE_FUNCTION Vector<int, Dim> NDIndex<Dim>::first() const {
161 Vector<int, Dim> result;
162 for (unsigned d = 0; d < Dim; ++d) {
163 result[d] = indices_m[d].first();
164 }
165 return result;
166 }
167
168 template <unsigned Dim>
169 KOKKOS_INLINE_FUNCTION Vector<int, Dim> NDIndex<Dim>::last() const {
170 Vector<int, Dim> result;
171 for (unsigned d = 0; d < Dim; ++d) {
172 result[d] = indices_m[d].last();
173 }
174 return result;
175 }
176
177 template <unsigned Dim>
178 KOKKOS_INLINE_FUNCTION constexpr typename NDIndex<Dim>::iterator NDIndex<Dim>::begin() {
179 return indices_m;
180 }
181
182 template <unsigned Dim>
183 KOKKOS_INLINE_FUNCTION constexpr typename NDIndex<Dim>::iterator NDIndex<Dim>::end() {
184 return indices_m + Dim;
185 }
186
187 template <unsigned Dim>
188 KOKKOS_INLINE_FUNCTION constexpr typename NDIndex<Dim>::const_iterator NDIndex<Dim>::begin()
189 const {
190 return indices_m;
191 }
192
193 template <unsigned Dim>
194 KOKKOS_INLINE_FUNCTION constexpr typename NDIndex<Dim>::const_iterator NDIndex<Dim>::end()
195 const {
196 return indices_m + Dim;
197 }
198
199 template <unsigned Dim>
200 bool operator==(const NDIndex<Dim>& nd1, const NDIndex<Dim>& nd2) {
201 for (unsigned d = 0; d < Dim; d++) {
202 if (nd1[d] != nd2[d]) {
203 return false;
204 }
205 }
206 return true;
207 }
208
209 template <unsigned Dim>
210 bool operator!=(const NDIndex<Dim>& nd1, const NDIndex<Dim>& nd2) {
211 return !(nd1 == nd2);
212 }
213} // namespace ippl
Definition Index.h:40
KOKKOS_INLINE_FUNCTION size_t length() const noexcept
Definition Index.hpp:88
Definition NDIndex.h:21
KOKKOS_INLINE_FUNCTION bool empty() const noexcept
Definition NDIndex.hpp:41
KOKKOS_INLINE_FUNCTION unsigned size() const noexcept
Definition NDIndex.hpp:32
KOKKOS_INLINE_FUNCTION const ippl::Index & operator[](unsigned d) const noexcept
Definition NDIndex.hpp:22
KOKKOS_INLINE_FUNCTION NDIndex< Dim > intersect(const NDIndex< Dim > &) const
Definition NDIndex.hpp:59
KOKKOS_INLINE_FUNCTION NDIndex< Dim > grow(int ncells) const
Definition NDIndex.hpp:68
Definition Vector.h:23
Definition Archive.h:20