NimbleSM
NimbleSM is a solid mechanics simulation code for dynamic systems
Loading...
Searching...
No Matches
nimble.quanta.arrayview.h
Go to the documentation of this file.
1/*
2//@HEADER
3// ************************************************************************
4//
5// NimbleSM
6// Copyright 2018
7// National Technology & Engineering Solutions of Sandia, LLC (NTESS)
8//
9// Under the terms of Contract DE-NA0003525 with NTESS, the U.S. Government
10// retains certain rights in this software.
11//
12// Redistribution and use in source and binary forms, with or without
13// modification, are permitted provided that the following conditions are
14// met:
15//
16// 1. Redistributions of source code must retain the above copyright
17// notice, this list of conditions and the following disclaimer.
18//
19// 2. Redistributions in binary form must reproduce the above copyright
20// notice, this list of conditions and the following disclaimer in the
21// documentation and/or other materials provided with the distribution.
22//
23// 3. Neither the name of the Corporation nor the names of the
24// contributors may be used to endorse or promote products derived from
25// this software without specific prior written permission.
26//
27// THIS SOFTWARE IS PROVIDED BY NTESS "AS IS" AND ANY EXPRESS OR IMPLIED
28// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
29// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
30// NO EVENT SHALL NTESS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
31// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
32// TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37//
38// Questions? Contact David Littlewood (djlittl@sandia.gov)
39//
40// ************************************************************************
41//@HEADER
42*/
43
44#ifndef NIMBLE_ARRAYVIEW_H
45#define NIMBLE_ARRAYVIEW_H
46
47#include <memory>
48#include <numeric>
49#include <utility>
50#include <vector>
51
52namespace nimble {
53namespace quanta {
54template <class T>
56{
57 private:
58 T* addr;
59 size_t count;
60
61 public:
62 arrayview_t() : addr(nullptr), count(0) {}
63 arrayview_t(T* addr, size_t start, size_t count) : addr(addr + start), count(count) {}
64 arrayview_t(T* addr, size_t count) : addr(addr), count(count) {}
65 arrayview_t(T* begin, T* end) : addr(begin), count((size_t)end - (size_t)begin) {}
66 arrayview_t(const arrayview_t& s) : addr(s.addr), count(s.count) {}
67 arrayview_t(arrayview_t&& s) : addr(s.addr), count(s.count) {}
68 arrayview_t(std::vector<T>& vect) : addr(vect.data()), count(vect.size()) {}
69 arrayview_t(std::vector<T>& vect, size_t start, size_t count) : addr(vect.data() + start), count(count) {}
70
71 T*
72 begin() const
73 {
74 return addr;
75 }
76 T*
77 end() const
78 {
79 return addr + count;
80 }
81
82 size_t
83 size() const
84 {
85 return count;
86 }
87 T*
89 {
90 return addr;
91 }
92 template <class int_t>
93 T&
94 operator[](int_t index) const
95 {
96 return addr[index];
97 }
98
100 sub(size_t start, size_t count) const
101 {
102 return {addr + start, count};
103 }
105 const_sub(size_t start, size_t count) const
106 {
107 return {addr + start, count};
108 }
111 {
112 // arrayview_t<T> and arrayview_t<const T> have the same internal memory
113 // layout So this code is chill
114 return (arrayview_t<const T>&)*this;
115 }
118 {
119 // Same here
120 return (const arrayview_t<const T>&)*this;
121 }
124 {
125 return (arrayview_t<const T>*)this;
126 }
129 {
130 return (const arrayview_t<const T>*)this;
131 }
132
133 template <class iter_t>
134 void
135 copy_from(iter_t source) const
136 {
137 for (T& value : *this) {
138 value = *source;
139 ++source;
140 }
141 }
142
143 template <class iter_t>
144 void
145 copy_to(iter_t dest) const
146 {
147 for (T& value : *this) {
148 *dest = value;
149 ++dest;
150 }
151 }
152
153 template <class iter_t>
154 void
155 swap_with(iter_t other) const
156 {
157 for (T& value : *this) {
158 std::swap(value, *other);
159 ++other;
160 }
161 }
162 template <class U>
163 friend void
165};
166template <class T>
167void
169{
170 std::swap(A.addr, B.addr);
171 std::swap(A.count, B.count);
172}
173template <class T, class list_t>
174std::vector<arrayview_t<T>>
175partition_into_arrayviews(std::vector<T>& values, const list_t& counts)
176{
177 std::vector<arrayview_t<T>> views;
178 views.reserve(counts.size());
179 size_t displacement = 0;
180 for (auto& count : counts) {
181 views.emplace_back(values, displacement, count);
182 displacement += count;
183 }
184 return views;
185}
186
187template <class T>
189{
190 private:
191 std::unique_ptr<T[]> arr;
192 std::unique_ptr<arrayview_t<T>[]> sections;
193 size_t elem_count;
194 size_t section_count;
195
196 public:
197 spanarray() = default;
198 spanarray(spanarray&&) = default;
199
200 spanarray(size_t elem_count, size_t section_count)
201 : arr{new T[elem_count]},
202 sections{new arrayview_t<T>[section_count]},
203 elem_count{elem_count},
204 section_count{section_count}
205 {
206 }
207 template <class int_t>
208 spanarray(const std::vector<int_t>& sect_sizes)
209 : spanarray{(size_t)std::accumulate(sect_sizes.begin(), sect_sizes.end(), 0), sect_sizes.size()}
210 {
211 size_t displacement = 0;
212 T* scan0 = data();
213 for (size_t i = 0; i < section_count; ++i) {
214 size_t section_size = (size_t)sect_sizes[i];
215 sections[i] = {scan0, displacement, section_size};
216 displacement += section_size;
217 }
218 }
219
220 size_t
221 size() const
222 {
223 return section_count;
224 }
227 {
228 return sections.get();
229 }
232 {
233 return sections.get();
234 }
237 {
238 return begin() + section_count;
239 }
241 begin() const
242 {
243 return sections.get()->as_span_of_const_ptr();
244 }
246 end() const
247 {
248 return begin() + section_count;
249 }
250
251 template <class int_t>
253 operator[](int_t index)
254 {
255 return sections[index];
256 }
257 template <class int_t>
259 operator[](int_t index) const
260 {
261 return sections[index].as_span_of_const();
262 }
263 template <class int_t, class int2_t>
264 T&
265 operator()(int_t section_index, int2_t element_index)
266 {
267 return sections[section_index][element_index];
268 }
269 template <class int_t, class int2_t>
270 const T&
271 operator()(int_t section_index, int2_t element_index) const
272 {
273 sections[0].swap(sections[1], sections[2]);
274 return sections[section_index][element_index];
275 }
276};
277} // namespace quanta
278} // namespace nimble
279#endif // NIMBLE_ARRAYVIEW_H
Definition nimble.quanta.arrayview.h:56
arrayview_t sub(size_t start, size_t count) const
Definition nimble.quanta.arrayview.h:100
T * begin() const
Definition nimble.quanta.arrayview.h:72
arrayview_t(T *begin, T *end)
Definition nimble.quanta.arrayview.h:65
arrayview_t(const arrayview_t &s)
Definition nimble.quanta.arrayview.h:66
arrayview_t(T *addr, size_t count)
Definition nimble.quanta.arrayview.h:64
T * data()
Definition nimble.quanta.arrayview.h:88
arrayview_t(T *addr, size_t start, size_t count)
Definition nimble.quanta.arrayview.h:63
void copy_to(iter_t dest) const
Definition nimble.quanta.arrayview.h:145
T & operator[](int_t index) const
Definition nimble.quanta.arrayview.h:94
arrayview_t(arrayview_t &&s)
Definition nimble.quanta.arrayview.h:67
const arrayview_t< const T > & as_span_of_const() const
Definition nimble.quanta.arrayview.h:117
void swap_with(iter_t other) const
Definition nimble.quanta.arrayview.h:155
const arrayview_t< const T > * as_span_of_const_ptr() const
Definition nimble.quanta.arrayview.h:128
T * end() const
Definition nimble.quanta.arrayview.h:77
void copy_from(iter_t source) const
Definition nimble.quanta.arrayview.h:135
arrayview_t()
Definition nimble.quanta.arrayview.h:62
arrayview_t< const T > const_sub(size_t start, size_t count) const
Definition nimble.quanta.arrayview.h:105
arrayview_t< const T > * as_span_of_const_ptr()
Definition nimble.quanta.arrayview.h:123
arrayview_t(std::vector< T > &vect)
Definition nimble.quanta.arrayview.h:68
size_t size() const
Definition nimble.quanta.arrayview.h:83
friend void swap(arrayview_t< U > &A, arrayview_t< U > &B)
arrayview_t(std::vector< T > &vect, size_t start, size_t count)
Definition nimble.quanta.arrayview.h:69
arrayview_t< const T > & as_span_of_const()
Definition nimble.quanta.arrayview.h:110
arrayview_t< T > * end()
Definition nimble.quanta.arrayview.h:236
const arrayview_t< const T > * end() const
Definition nimble.quanta.arrayview.h:246
arrayview_t< T > & operator[](int_t index)
Definition nimble.quanta.arrayview.h:253
const arrayview_t< const T > & operator[](int_t index) const
Definition nimble.quanta.arrayview.h:259
const arrayview_t< const T > * begin() const
Definition nimble.quanta.arrayview.h:241
spanarray(const std::vector< int_t > &sect_sizes)
Definition nimble.quanta.arrayview.h:208
spanarray(spanarray &&)=default
arrayview_t< T > * begin()
Definition nimble.quanta.arrayview.h:231
spanarray(size_t elem_count, size_t section_count)
Definition nimble.quanta.arrayview.h:200
T & operator()(int_t section_index, int2_t element_index)
Definition nimble.quanta.arrayview.h:265
arrayview_t< T > * data()
Definition nimble.quanta.arrayview.h:226
const T & operator()(int_t section_index, int2_t element_index) const
Definition nimble.quanta.arrayview.h:271
size_t size() const
Definition nimble.quanta.arrayview.h:221
Definition nimble.quanta.arrayview.h:53
void swap(arrayview_t< T > &A, arrayview_t< T > &B)
Definition nimble.quanta.arrayview.h:168
std::vector< arrayview_t< T > > partition_into_arrayviews(std::vector< T > &values, const list_t &counts)
Definition nimble.quanta.arrayview.h:175
Definition kokkos_contact_manager.h:49