forked from fwyzard/soa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
soa_v1.h
447 lines (375 loc) · 11 KB
/
soa_v1.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
#include <type_traits>
/*
* Simple Structure-of-Arrays with a predefined layout, compile-time size and alignment,
* a struct-like reference to access a "row", and direct access to the "columns".
*/
// CUDA attributes
#ifdef __CUDACC__
#define SOA_HOST_ONLY __host__
#define SOA_HOST_DEVICE __host__ __device__
#else
#define SOA_HOST_ONLY
#define SOA_HOST_DEVICE
#endif
// return the smallest integer greater than or equal to `x` that is a multiple of `n`
template <typename T>
constexpr
T next_multiple(T x, T n) {
T c = (x + n - 1) / n;
return c * n;
}
// struct type with value semantics, corresponding to an individual entry in a SoA
struct SoAEntry {
public:
double x;
double y;
double z;
uint16_t colour;
int32_t value;
const char* name;
};
// compile-time sized SoA
template <size_t SIZE, size_t ALIGN=0>
struct SoATemplate {
// type used for the indices into the SoA
using size_type = size_t;
// type used for the difference between indices into the SoA
using difference_type = std::make_signed_t<size_type>;
// struct type with value semantics, corresponding to an individual entry in a SoA
using value_type = SoAEntry;
// forward declaration for types with reference semantics, used as accessors to individual entries in a SoA
struct reference;
struct const_reference;
// forward declaration for types with iterator semantics, used as accessors to individual entries in a SoA
struct iterator;
struct const_iterator;
// type with reference semantics, used as an accessor to individual entries in a SoA
struct reference {
public:
constexpr
reference(SoATemplate<SIZE,ALIGN> & soa, size_type index) :
x(soa.x[index]),
y(soa.y[index]),
z(soa.z[index]),
colour(soa.colour[index]),
value(soa.value[index]),
name(soa.name[index])
{ }
constexpr
reference& operator=(reference const& e) {
x = e.x;
y = e.y;
z = e.z;
colour = e.colour;
value = e.value;
name = e.name;
return *this;
}
constexpr
reference& operator=(const_reference const& e) {
x = e.x;
y = e.y;
z = e.z;
colour = e.colour;
value = e.value;
name = e.name;
return *this;
}
constexpr
reference& operator=(value_type const& e) {
x = e.x;
y = e.y;
z = e.z;
colour = e.colour;
value = e.value;
name = e.name;
return *this;
}
constexpr
operator value_type() const {
return value_type{x, y, z, colour, value, name};
}
public:
double & x;
double & y;
double & z;
uint16_t & colour;
int32_t & value;
const char* & name;
};
// type with const reference semantics, used as a const accessor to individual entries in a const SoA
struct const_reference {
public:
constexpr
const_reference(SoATemplate<SIZE,ALIGN> const& soa, size_type index) :
x(soa.x[index]),
y(soa.y[index]),
z(soa.z[index]),
colour(soa.colour[index]),
value(soa.value[index]),
name(soa.name[index])
{ }
const_reference& operator=(const_reference const& e) = delete;
const_reference& operator=(const_reference && e) = delete;
constexpr
operator value_type() const {
return value_type{x, y, z, colour, value, name};
}
public:
double const& x;
double const& y;
double const& z;
uint16_t const& colour;
int32_t const& value;
const char* const& name;
};
// type with iterator semantics, used to identify an individual entry in a SoA
struct iterator {
iterator() = default;
iterator(iterator const&) = default;
iterator(iterator &&) = default;
iterator& operator=(iterator const&) = default;
iterator& operator=(iterator &&) = default;
iterator(SoATemplate* soa, size_type index) :
soa_(soa),
index_(index)
{}
constexpr
iterator& operator++() {
++index_;
return *this;
}
constexpr
iterator operator++(int) {
iterator it = *this;
++index_;
return it;
}
constexpr
iterator& operator+=(difference_type inc) {
index_ += inc;
return *this;
}
constexpr
iterator& operator--() {
--index_;
return *this;
}
constexpr
iterator operator--(int) {
iterator it = *this;
--index_;
return it;
}
constexpr
iterator& operator-=(difference_type dec) {
index_ -= dec;
return *this;
}
constexpr
iterator operator+(difference_type inc) const {
iterator it = *this;
it += inc;
return it;
}
constexpr
iterator operator-(difference_type dec) const {
iterator it = *this;
it -= dec;
return it;
}
constexpr
difference_type operator-(iterator other) const {
assert(soa_ == other.soa_);
return static_cast<difference_type>(index_ - other.index_);
}
constexpr
bool operator==(iterator const& other) const {
return (soa_ == other.soa_) and (index_ == other.index_);
}
constexpr
bool operator!=(iterator const& other) const {
return not (*this == other);
}
constexpr
bool operator<(iterator const& other) const {
return (soa_ == other.soa_) ? (index_ < other.index_) : (soa_ < other.soa_);
}
constexpr
bool operator>(iterator const& other) const {
return (other < *this);
}
constexpr
bool operator<=(iterator const& other) const {
return not (*this > other);
}
constexpr
bool operator>=(iterator const& other) const {
return not (*this < other);
}
constexpr
reference operator*() const {
return reference(*soa_, index_);
}
private:
SoATemplate * soa_;
size_type index_;
};
// type with const iterator semantics, used to identify an individual entry in a const SoA
struct const_iterator {
const_iterator() = default;
const_iterator(const_iterator const&) = default;
const_iterator(const_iterator &&) = default;
const_iterator& operator=(const_iterator const&) = default;
const_iterator& operator=(const_iterator &&) = default;
const_iterator(SoATemplate const* soa, size_type index) :
soa_(soa),
index_(index)
{}
const_iterator(iterator const& it) :
soa_(it.soa_),
index_(it.index_)
{}
constexpr
const_iterator& operator++() {
++index_;
return *this;
}
constexpr
const_iterator operator++(int) {
const_iterator it = *this;
++index_;
return it;
}
constexpr
const_iterator& operator+=(difference_type inc) {
index_ += inc;
return *this;
}
constexpr
const_iterator& operator--() {
--index_;
return *this;
}
constexpr
const_iterator operator--(int) {
const_iterator it = *this;
--index_;
return it;
}
constexpr
const_iterator& operator-=(difference_type dec) {
index_ -= dec;
return *this;
}
constexpr
const_iterator operator+(difference_type inc) const {
const_iterator it = *this;
it += inc;
return it;
}
constexpr
const_iterator operator-(difference_type dec) const {
const_iterator it = *this;
it -= dec;
return it;
}
constexpr
difference_type operator-(const_iterator other) const {
assert(soa_ == other.soa_);
return static_cast<difference_type>(index_ - other.index_);
}
constexpr
bool operator==(const_iterator const& other) const {
return (soa_ == other.soa_) and (index_ == other.index_);
}
constexpr
bool operator!=(const_iterator const& other) const {
return not (*this == other);
}
constexpr
bool operator<(const_iterator const& other) const {
return (soa_ == other.soa_) ? (index_ < other.index_) : (soa_ < other.soa_);
}
constexpr
bool operator>(const_iterator const& other) const {
return (other < *this);
}
constexpr
bool operator<=(const_iterator const& other) const {
return not (*this > other);
}
constexpr
bool operator>=(const_iterator const& other) const {
return not (*this < other);
}
constexpr
const_reference operator*() const {
return const_reference(*soa_, index_);
}
private:
const SoATemplate * soa_;
size_type index_;
};
// size (numer of entries) in the SoA
static constexpr
size_type size() { return SIZE; }
// alignment of each "column" of the SoA
static constexpr
size_type alignment() { return ALIGN; }
// extent in bytes of the SoA
static constexpr
size_t extent() {
/*
return next_multiple(sizeof(double) * size(), std::max(alignof(double), alignment())) +
next_multiple(sizeof(double) * size(), std::max(alignof(double), alignment())) +
next_multiple(sizeof(double) * size(), std::max(alignof(double), alignment())) +
next_multiple(sizeof(uint16_t) * size(), std::max(alignof(uint16_t), alignment())) +
next_multiple(sizeof(int32_t) * size(), std::max(alignof(int32_t), alignment())) +
next_multiple(sizeof(const char*) * size(), std::max(alignof(const char*), alignment()));
*/
return sizeof(SoATemplate);
}
iterator begin() {
return iterator(this, 0);
}
const_iterator begin() const {
return const_iterator(this, 0);
}
iterator end() {
return iterator(this, size());
}
const_iterator end() const {
return const_iterator(this, size());
}
// AoS-like access
SOA_HOST_DEVICE
reference operator[](size_type index) { return reference(*this, index); }
SOA_HOST_DEVICE
const_reference operator[](size_type index) const { return const_reference(*this, index); }
// data members
alignas(ALIGN) double x[SIZE];
alignas(ALIGN) double y[SIZE];
alignas(ALIGN) double z[SIZE];
alignas(ALIGN) uint16_t colour[SIZE];
alignas(ALIGN) int32_t value[SIZE];
alignas(ALIGN) const char* name[SIZE];
};
// -----------------------------------------------------------------------------
#include <iostream>
// dump the SoA internal structure
template <typename T>
SOA_HOST_ONLY
void dump() {
using SoA = T;
std::cout << "SoATemplate<" << SoA::size() << ", " << SoA::alignment() << ">: " << '\n';
std::cout << " sizeof(...): " << sizeof(SoA) << '\n';
std::cout << " alignof(...): " << alignof(SoA) << '\n';
std::cout << " x[" << SoA::size() << "] at " << offsetof(SoA, x) << " has size " << sizeof(SoA::x) << '\n';
std::cout << " y[" << SoA::size() << "] at " << offsetof(SoA, y) << " has size " << sizeof(SoA::y) << '\n';
std::cout << " z[" << SoA::size() << "] at " << offsetof(SoA, z) << " has size " << sizeof(SoA::z) << '\n';
std::cout << " colour[" << SoA::size() << "] at " << offsetof(SoA, colour) << " has size " << sizeof(SoA::colour) << '\n';
std::cout << " value[" << SoA::size() << "] at " << offsetof(SoA, value) << " has size " << sizeof(SoA::value) << '\n';
std::cout << " name[" << SoA::size() << "] at " << offsetof(SoA, name) << " has size " << sizeof(SoA::name) << '\n';
std::cout << std::endl;
}