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-2026 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
20#pragma once
21
22/**
23 * @file
24 *
25 * @brief Array implementation with CUDA support.
26 */
27
28#include "device_qualifier.hpp"
30
31#include <boost/serialization/access.hpp>
32
33#include <cassert>
34#include <cstddef>
35#include <iterator>
36#include <ostream>
37#include <stdexcept>
38#include <tuple>
39#include <utility>
40
41namespace Utils {
42namespace detail {
43
44template <typename T, std::size_t N> struct Storage {
45 T m_data[N] = {};
46
47private:
48 friend boost::serialization::access;
49 template <typename Archive>
50 void serialize(Archive &ar, const unsigned int /* version */) {
51 ar & m_data;
52 }
53};
55template <typename T> struct Storage<T, 0> {};
56
57struct ArrayFormatterStream {
58 std::ostream &stream;
59 char const *separator;
60 ArrayFormatterStream(std::ostream &s, char const *sep)
61 : stream(s), separator(sep) {}
62};
64struct ArrayFormatter {
65 char const *separator;
66 friend ArrayFormatterStream operator<<(std::ostream &os,
67 ArrayFormatter const &fmt) {
68 return {os, fmt.separator};
69 }
70};
71
72/**
73 * @brief Alias to create prvalue C-style arrays.
74 *
75 * This type is necessary when converting arrays from one type to another type
76 * within functions marked as @c noexcept. The @c std::initializer_list<T>
77 * type doesn't include the backing array length as part of the type,
78 * and is therefore not strictly equivalent.
79 */
80template <typename T, std::size_t N> using carray_alias = T const[N];
81
82} // namespace detail
83
84template <typename T, std::size_t N> struct Array {
85 using value_type = T;
86 using size_type = std::size_t;
87 using difference_type = std::ptrdiff_t;
89 using const_reference = const value_type &;
91 using const_iterator = const value_type *;
93 using const_pointer = const value_type *;
94
95 detail::Storage<T, N> m_storage;
96
98 if (i >= N) {
99 DEVICE_THROW(std::out_of_range("Array access out of bounds."));
100 }
101 return m_storage.m_data[i];
102 }
103
105 if (i >= N) {
106 DEVICE_THROW(std::out_of_range("Array access out of bounds."));
107 }
108 return m_storage.m_data[i];
109 }
110
112 DEVICE_ASSERT(i < N);
113 return m_storage.m_data[i];
114 }
115
117 operator[](size_type i) const noexcept {
118 DEVICE_ASSERT(i < N);
119 return m_storage.m_data[i];
120 }
121
122 DEVICE_QUALIFIER constexpr reference front() { return *begin(); }
123
124 DEVICE_QUALIFIER constexpr const_reference front() const { return *cbegin(); }
125
126 DEVICE_QUALIFIER constexpr reference back() { return *(end() - 1); }
127
129 return *(cend() - 1);
130 }
131
133 return &m_storage.m_data[0];
134 }
135
137 return &m_storage.m_data[0];
138 }
139
141 return &m_storage.m_data[0];
142 }
143
145 return &m_storage.m_data[0];
146 }
147
149 return &m_storage.m_data[0];
150 }
151
153 return &m_storage.m_data[N];
154 }
155
157 return &m_storage.m_data[N];
158 }
159
161 return &m_storage.m_data[N];
162 }
163
164 DEVICE_QUALIFIER constexpr bool empty() const noexcept { return size() == 0; }
165
167
169
170 DEVICE_QUALIFIER void fill(const value_type &value) {
171 for (size_type i = 0; i != size(); ++i) {
172 m_storage.m_data[i] = value;
173 }
174 }
175
176 static constexpr detail::ArrayFormatter formatter(char const *sep = " ") {
177 return {sep};
178 }
179
180private:
181 friend boost::serialization::access;
182 template <typename Archive>
183 void serialize(Archive &ar, const unsigned int /* version */) {
184 ar & m_storage;
185 }
186
187 static std::ostream &format(std::ostream &out, Array const &a,
188 char const *const sep) {
189 if (not a.empty())
190 out << a[0];
191
192 for (auto it = std::next(a.begin()); it != a.end(); ++it)
193 out << sep << *it;
194
195 return out;
196 }
197
198 friend std::ostream &operator<<(std::ostream &out, Array const &a) {
199 return format(out, a, ", ");
200 }
201
202 friend std::ostream &operator<<(detail::ArrayFormatterStream const &fmt,
203 Array const &a) {
204 return format(fmt.stream, a, fmt.separator);
205 }
206};
207
208template <std::size_t I, class T, std::size_t N>
209T &get(Array<T, N> &a) noexcept {
210 return a[I];
211}
212
213template <std::size_t I, class T, std::size_t N>
214T const &get(Array<T, N> const &a) noexcept {
215 return a[I];
216}
217
218} // namespace Utils
219
220template <std::size_t I, class T, std::size_t N>
221struct std::tuple_element<I, Utils::Array<T, N>> {
222 static_assert(I < N, "Utils::Array index must be in range");
223 using type = T;
224};
225
226template <class T, std::size_t N>
227struct std::tuple_size<Utils::Array<T, N>>
228 : std::integral_constant<std::size_t, N> {};
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)
#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)
T & get(Array< T, N > &a) noexcept
Definition Array.hpp:209
void serialize(Archive &ar, std::tuple< T... > &pack, unsigned int const)
Serialize std::tuple.
DEVICE_QUALIFIER constexpr const_reference operator[](size_type i) const noexcept
Definition Array.hpp:117
DEVICE_QUALIFIER constexpr reference at(size_type i)
Definition Array.hpp:97
value_type * iterator
Definition Array.hpp:90
static constexpr detail::ArrayFormatter formatter(char const *sep=" ")
Definition Array.hpp:176
DEVICE_QUALIFIER constexpr bool empty() const noexcept
Definition Array.hpp:164
DEVICE_QUALIFIER constexpr reference back()
Definition Array.hpp:126
value_type * pointer
Definition Array.hpp:92
DEVICE_QUALIFIER constexpr pointer data() noexcept
Definition Array.hpp:132
DEVICE_QUALIFIER constexpr const_reference front() const
Definition Array.hpp:124
DEVICE_QUALIFIER constexpr const_reference at(size_type i) const
Definition Array.hpp:104
DEVICE_QUALIFIER constexpr size_type max_size() const noexcept
Definition Array.hpp:168
DEVICE_QUALIFIER constexpr iterator begin() noexcept
Definition Array.hpp:140
DEVICE_QUALIFIER constexpr const_pointer data() const noexcept
Definition Array.hpp:136
DEVICE_QUALIFIER constexpr const_iterator cbegin() const noexcept
Definition Array.hpp:148
const value_type & const_reference
Definition Array.hpp:89
detail::Storage< T, N > m_storage
Definition Array.hpp:95
DEVICE_QUALIFIER constexpr reference operator[](size_type i) noexcept
Definition Array.hpp:111
friend std::ostream & operator<<(std::ostream &out, Array const &a)
Definition Array.hpp:198
DEVICE_QUALIFIER constexpr const_iterator cend() const noexcept
Definition Array.hpp:160
DEVICE_QUALIFIER constexpr size_type size() const noexcept
Definition Array.hpp:166
DEVICE_QUALIFIER constexpr reference front()
Definition Array.hpp:122
const value_type * const_pointer
Definition Array.hpp:93
DEVICE_QUALIFIER constexpr const_reference back() const
Definition Array.hpp:128
friend std::ostream & operator<<(detail::ArrayFormatterStream const &fmt, Array const &a)
Definition Array.hpp:202
DEVICE_QUALIFIER constexpr iterator end() noexcept
Definition Array.hpp:152
DEVICE_QUALIFIER void fill(const value_type &value)
Definition Array.hpp:170
const value_type * const_iterator
Definition Array.hpp:91
DEVICE_QUALIFIER constexpr const_iterator begin() const noexcept
Definition Array.hpp:144
std::size_t size_type
Definition Array.hpp:86
DEVICE_QUALIFIER constexpr const_iterator end() const noexcept
Definition Array.hpp:156
value_type & reference
Definition Array.hpp:88
std::ptrdiff_t difference_type
Definition Array.hpp:87