ESPResSo
Extensible Simulation Package for Research on Soft Matter Systems
Loading...
Searching...
No Matches
Array.hpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2010-2022 The ESPResSo project
3 *
4 * This file is part of ESPResSo.
5 *
6 * ESPResSo is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * ESPResSo is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19#ifndef SRC_UTILS_INCLUDE_UTILS_ARRAY_HPP
20#define SRC_UTILS_INCLUDE_UTILS_ARRAY_HPP
21
22/**
23 * @file
24 *
25 * @brief Array implementation with CUDA support.
26 */
27
28#include "device_qualifier.hpp"
29#include "get.hpp"
31
32#include <boost/serialization/access.hpp>
33
34#include <cassert>
35#include <cstddef>
36#include <iterator>
37#include <ostream>
38#include <stdexcept>
39
40namespace Utils {
41namespace detail {
42
43template <typename T, std::size_t N> struct Storage {
44 T m_data[N];
45
46private:
47 friend boost::serialization::access;
48 template <typename Archive>
49 void serialize(Archive &ar, const unsigned int /* version */) {
50 ar & m_data;
51 }
52};
53
54template <typename T> struct Storage<T, 0> {};
55
56struct ArrayFormatterStream {
57 std::ostream &stream;
58 char const *separator;
59 ArrayFormatterStream(std::ostream &s, char const *sep)
60 : stream(s), separator(sep) {}
61};
62
63struct ArrayFormatter {
64 char const *separator;
65 friend ArrayFormatterStream operator<<(std::ostream &os,
66 ArrayFormatter const &fmt) {
67 return {os, fmt.separator};
68 }
69};
70
71/**
72 * @brief Alias to create prvalue C-style arrays.
73 *
74 * This type is necessary when converting arrays from one type to another type
75 * within functions marked as @c noexcept. The @c std::initializer_list<T>
76 * type doesn't include the backing array length as part of the type,
77 * and is therefore not strictly equivalent.
78 */
79template <typename T, std::size_t N> using carray_alias = T const[N];
80
81} // namespace detail
82
83template <typename T, std::size_t N> struct Array {
84 using value_type = T;
85 using size_type = std::size_t;
86 using difference_type = std::ptrdiff_t;
88 using const_reference = const value_type &;
90 using const_iterator = const value_type *;
92 using const_pointer = const value_type *;
93
94 detail::Storage<T, N> m_storage;
95
97 if (i >= N) {
98 DEVICE_THROW(std::out_of_range("Array access out of bounds."));
99 }
100 return m_storage.m_data[i];
101 }
102
104 if (i >= N) {
105 DEVICE_THROW(std::out_of_range("Array access out of bounds."));
106 }
107 return m_storage.m_data[i];
108 }
109
111 DEVICE_ASSERT(i < N);
112 return m_storage.m_data[i];
113 }
114
116 DEVICE_ASSERT(i < N);
117 return m_storage.m_data[i];
118 }
119
120 DEVICE_QUALIFIER constexpr reference front() { return *begin(); }
121
122 DEVICE_QUALIFIER constexpr const_reference front() const { return *cbegin(); }
123
124 DEVICE_QUALIFIER constexpr reference back() { return *(end() - 1); }
125
127 return *(cend() - 1);
128 }
129
130 DEVICE_QUALIFIER constexpr pointer data() noexcept {
131 return &m_storage.m_data[0];
132 }
133
134 DEVICE_QUALIFIER constexpr const_pointer data() const noexcept {
135 return &m_storage.m_data[0];
136 }
137
138 DEVICE_QUALIFIER constexpr iterator begin() noexcept {
139 return &m_storage.m_data[0];
140 }
141
142 DEVICE_QUALIFIER constexpr const_iterator begin() const noexcept {
143 return &m_storage.m_data[0];
144 }
145
146 DEVICE_QUALIFIER constexpr const_iterator cbegin() const noexcept {
147 return &m_storage.m_data[0];
148 }
149
150 DEVICE_QUALIFIER constexpr iterator end() noexcept {
151 return &m_storage.m_data[N];
152 }
153
154 DEVICE_QUALIFIER constexpr const_iterator end() const noexcept {
155 return &m_storage.m_data[N];
156 }
157
158 DEVICE_QUALIFIER constexpr const_iterator cend() const noexcept {
159 return &m_storage.m_data[N];
160 }
161
162 DEVICE_QUALIFIER constexpr bool empty() const noexcept { return size() == 0; }
163
164 DEVICE_QUALIFIER constexpr size_type size() const noexcept { return N; }
165
166 DEVICE_QUALIFIER constexpr size_type max_size() const noexcept { return N; }
167
168 DEVICE_QUALIFIER void fill(const value_type &value) {
169 for (size_type i = 0; i != size(); ++i) {
170 m_storage.m_data[i] = value;
171 }
172 }
173
174 DEVICE_QUALIFIER static constexpr Array<T, N>
175 broadcast(const value_type &value) {
176 Array<T, N> ret{};
177 for (size_type i = 0; i != N; ++i) {
178 ret[i] = value;
179 }
180 return ret;
181 }
182
183 static constexpr detail::ArrayFormatter formatter(char const *sep = " ") {
184 return {sep};
185 }
186
187private:
188 friend boost::serialization::access;
189 template <typename Archive>
190 void serialize(Archive &ar, const unsigned int /* version */) {
191 ar & m_storage;
192 }
193
194 static std::ostream &format(std::ostream &out, Array const &a,
195 char const *const sep) {
196 if (not a.empty())
197 out << a[0];
198
199 for (auto it = std::next(a.begin()); it != a.end(); ++it)
200 out << sep << *it;
201
202 return out;
203 }
204
205 friend std::ostream &operator<<(std::ostream &out, Array const &a) {
206 return format(out, a, ", ");
207 }
208
209 friend std::ostream &operator<<(detail::ArrayFormatterStream const &fmt,
210 Array const &a) {
211 return format(fmt.stream, a, fmt.separator);
212 }
213};
214
215template <std::size_t I, class T, std::size_t N>
216struct tuple_element<I, Array<T, N>> {
217 using type = T;
218};
219
220template <class T, std::size_t N>
221struct tuple_size<Array<T, N>> : std::integral_constant<std::size_t, N> {};
222
223template <std::size_t I, class T, std::size_t N>
224auto get(Array<T, N> const &a) -> std::enable_if_t<(I < N), const T &> {
225 return a[I];
226}
227
228} // namespace Utils
229
230UTILS_ARRAY_BOOST_MPI_T(Utils::detail::Storage, N)
231UTILS_ARRAY_BOOST_BIT_S(Utils::detail::Storage, N)
232UTILS_ARRAY_BOOST_CLASS(Utils::detail::Storage, N, object_serializable)
233UTILS_ARRAY_BOOST_TRACK(Utils::detail::Storage, N, track_never)
236UTILS_ARRAY_BOOST_CLASS(Utils::Array, N, object_serializable)
238
239#endif
#define UTILS_ARRAY_BOOST_MPI_T(Container, N)
Mark array types as MPI data types.
Definition array.hpp:50
#define UTILS_ARRAY_BOOST_BIT_S(Container, N)
Mark array types as MPI bitwise serializable.
Definition array.hpp:63
#define UTILS_ARRAY_BOOST_CLASS(Container, N, ImplementationLevel)
Redefinition of BOOST_CLASS_IMPLEMENTATION for array types.
Definition array.hpp:78
#define UTILS_ARRAY_BOOST_TRACK(Container, N, TrackingLevel)
Redefinition of BOOST_CLASS_TRACKING for array types.
Definition array.hpp:97
cudaStream_t stream[1]
CUDA streams for parallel computing on CPU and GPU.
#define DEVICE_ASSERT(A)
#define DEVICE_QUALIFIER
#define DEVICE_THROW(E)
auto get(Array< T, N > const &a) -> std::enable_if_t<(I< N), const T & >
Definition Array.hpp:224
void serialize(Archive &ar, std::tuple< T... > &pack, unsigned int const)
Serialize std::tuple.
DEVICE_QUALIFIER constexpr reference at(size_type i)
Definition Array.hpp:96
value_type * iterator
Definition Array.hpp:89
static constexpr detail::ArrayFormatter formatter(char const *sep=" ")
Definition Array.hpp:183
DEVICE_QUALIFIER constexpr bool empty() const noexcept
Definition Array.hpp:162
DEVICE_QUALIFIER constexpr reference back()
Definition Array.hpp:124
value_type * pointer
Definition Array.hpp:91
DEVICE_QUALIFIER constexpr pointer data() noexcept
Definition Array.hpp:130
DEVICE_QUALIFIER constexpr const_reference front() const
Definition Array.hpp:122
DEVICE_QUALIFIER constexpr const_reference at(size_type i) const
Definition Array.hpp:103
DEVICE_QUALIFIER constexpr size_type max_size() const noexcept
Definition Array.hpp:166
DEVICE_QUALIFIER constexpr iterator begin() noexcept
Definition Array.hpp:138
DEVICE_QUALIFIER constexpr const_pointer data() const noexcept
Definition Array.hpp:134
DEVICE_QUALIFIER constexpr const_iterator cbegin() const noexcept
Definition Array.hpp:146
const value_type & const_reference
Definition Array.hpp:88
detail::Storage< T, N > m_storage
Definition Array.hpp:94
friend std::ostream & operator<<(std::ostream &out, Array const &a)
Definition Array.hpp:205
DEVICE_QUALIFIER constexpr const_iterator cend() const noexcept
Definition Array.hpp:158
static DEVICE_QUALIFIER constexpr Array< T, N > broadcast(const value_type &value)
Definition Array.hpp:175
DEVICE_QUALIFIER constexpr size_type size() const noexcept
Definition Array.hpp:164
DEVICE_QUALIFIER constexpr reference front()
Definition Array.hpp:120
const value_type * const_pointer
Definition Array.hpp:92
DEVICE_QUALIFIER constexpr const_reference back() const
Definition Array.hpp:126
DEVICE_QUALIFIER constexpr reference operator[](size_type i)
Definition Array.hpp:110
friend std::ostream & operator<<(detail::ArrayFormatterStream const &fmt, Array const &a)
Definition Array.hpp:209
DEVICE_QUALIFIER constexpr iterator end() noexcept
Definition Array.hpp:150
DEVICE_QUALIFIER void fill(const value_type &value)
Definition Array.hpp:168
const value_type * const_iterator
Definition Array.hpp:90
DEVICE_QUALIFIER constexpr const_iterator begin() const noexcept
Definition Array.hpp:142
DEVICE_QUALIFIER constexpr const_reference operator[](size_type i) const
Definition Array.hpp:115
std::size_t size_type
Definition Array.hpp:85
DEVICE_QUALIFIER constexpr const_iterator end() const noexcept
Definition Array.hpp:154
value_type & reference
Definition Array.hpp:87
std::ptrdiff_t difference_type
Definition Array.hpp:86