forked from fwyzard/soa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
soa_v4.h
268 lines (227 loc) · 21.5 KB
/
soa_v4.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
/*
* Structure-of-Arrays template with "columns" and "scalars", defined through preprocessor macros,
* with compile-time size and alignment, and accessors to the "rows" and "columns".
*/
#include <iostream>
#include <boost/preprocessor.hpp>
// 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
// compile-time sized SoA
/* declare "scalars" (one value shared across the whole SoA) and "columns" (one vale per element) */
#define SoA_scalar(TYPE, NAME) (0, TYPE, NAME)
#define SoA_column(TYPE, NAME) (1, TYPE, NAME)
/* declare SoA data members; these should exapnd to, for columns:
*
* alignas(ALIGN) double x_[SIZE];
*
* and for scalars:
*
* double x_;
*
*/
#define _DECLARE_SOA_DATA_MEMBER_IMPL(IS_COLUMN, TYPE, NAME) \
BOOST_PP_IIF(IS_COLUMN, \
alignas(ALIGN) TYPE BOOST_PP_CAT(NAME, _[SIZE]); \
, \
TYPE BOOST_PP_CAT(NAME, _); \
)
#define _DECLARE_SOA_DATA_MEMBER(R, DATA, TYPE_NAME) \
BOOST_PP_EXPAND(_DECLARE_SOA_DATA_MEMBER_IMPL TYPE_NAME)
#define _DECLARE_SOA_DATA_MEMBERS(...) \
BOOST_PP_SEQ_FOR_EACH(_DECLARE_SOA_DATA_MEMBER, ~, BOOST_PP_VARIADIC_TO_SEQ(__VA_ARGS__))
/* declare SoA accessors; these should expand to, for columns:
*
* double* x() { return x_; }
*
* and for scalars:
*
* double& x() { return x_; }
*
*/
#define _DECLARE_SOA_ACCESSOR_IMPL(IS_COLUMN, TYPE, NAME) \
SOA_HOST_DEVICE \
BOOST_PP_IIF(IS_COLUMN, \
TYPE* NAME() { return BOOST_PP_CAT(NAME, _); } \
, \
TYPE& NAME() { return BOOST_PP_CAT(NAME, _); } \
)
#define _DECLARE_SOA_ACCESSOR(R, DATA, TYPE_NAME) \
BOOST_PP_EXPAND(_DECLARE_SOA_ACCESSOR_IMPL TYPE_NAME)
#define _DECLARE_SOA_ACCESSORS(...) \
BOOST_PP_SEQ_FOR_EACH(_DECLARE_SOA_ACCESSOR, ~, BOOST_PP_VARIADIC_TO_SEQ(__VA_ARGS__))
#define _DECLARE_SOA_CONST_ACCESSOR_IMPL(IS_COLUMN, TYPE, NAME) \
SOA_HOST_DEVICE \
BOOST_PP_IIF(IS_COLUMN, \
TYPE const* NAME() const { return BOOST_PP_CAT(NAME, _); } \
, \
TYPE const& NAME() const { return BOOST_PP_CAT(NAME, _); } \
)
#define _DECLARE_SOA_CONST_ACCESSOR(R, DATA, TYPE_NAME) \
BOOST_PP_EXPAND(_DECLARE_SOA_CONST_ACCESSOR_IMPL TYPE_NAME)
#define _DECLARE_SOA_CONST_ACCESSORS(...) \
BOOST_PP_SEQ_FOR_EACH(_DECLARE_SOA_CONST_ACCESSOR, ~, BOOST_PP_VARIADIC_TO_SEQ(__VA_ARGS__))
/* assignment of individual fields; these should expand to, for columns
*
* x() = other.x();
*
* and to nothing for scalars.
*/
#define _DECLARE_SOA_ELEMENT_ASSIGNMENT_IMPL(IS_COLUMN, TYPE, NAME) \
BOOST_PP_IIF(IS_COLUMN, \
NAME() = other.NAME(); \
, \
)
#define _DECLARE_SOA_ELEMENT_ASSIGNMENT(R, DATA, TYPE_NAME) \
BOOST_PP_EXPAND(_DECLARE_SOA_ELEMENT_ASSIGNMENT_IMPL TYPE_NAME)
#define _DECLARE_SOA_ELEMENT_ASSIGNMENTS(...) \
BOOST_PP_SEQ_FOR_EACH(_DECLARE_SOA_ELEMENT_ASSIGNMENT, ~, BOOST_PP_VARIADIC_TO_SEQ(__VA_ARGS__))
/* declare AoS-like element accessors; these should expand to, for columns:
*
* double & x() { return * (soa_.x() + index_); }
*
* and for scalars:
*
* double & x() { return soa_.x(); }
*
*/
#define _DECLARE_SOA_ELEMENT_ACCESSOR_IMPL(IS_COLUMN, TYPE, NAME) \
SOA_HOST_DEVICE \
BOOST_PP_IIF(IS_COLUMN, \
TYPE & NAME() { return * (soa_. NAME () + index_); } \
, \
TYPE & NAME() { return soa_. NAME (); } \
)
#define _DECLARE_SOA_ELEMENT_ACCESSOR(R, DATA, TYPE_NAME) \
BOOST_PP_EXPAND(_DECLARE_SOA_ELEMENT_ACCESSOR_IMPL TYPE_NAME)
#define _DECLARE_SOA_ELEMENT_ACCESSORS(...) \
BOOST_PP_SEQ_FOR_EACH(_DECLARE_SOA_ELEMENT_ACCESSOR, ~, BOOST_PP_VARIADIC_TO_SEQ(__VA_ARGS__))
#define _DECLARE_SOA_CONST_ELEMENT_ACCESSOR_IMPL(IS_COLUMN, TYPE, NAME) \
SOA_HOST_DEVICE \
BOOST_PP_IIF(IS_COLUMN, \
TYPE const & NAME() { return * (soa_. NAME () + index_); } \
, \
TYPE const & NAME() { return soa_. NAME (); } \
)
#define _DECLARE_SOA_CONST_ELEMENT_ACCESSOR(R, DATA, TYPE_NAME) \
BOOST_PP_EXPAND(_DECLARE_SOA_CONST_ELEMENT_ACCESSOR_IMPL TYPE_NAME)
#define _DECLARE_SOA_CONST_ELEMENT_ACCESSORS(...) \
BOOST_PP_SEQ_FOR_EACH(_DECLARE_SOA_CONST_ELEMENT_ACCESSOR, ~, BOOST_PP_VARIADIC_TO_SEQ(__VA_ARGS__))
/* dump SoA fields information; these should expand to, for columns:
*
* std::cout << " x_[" << SoA::size << "] at "
* << offsetof(SoA, SoA::x_) << " has size " << sizeof(SoA::x_) << std::endl;
*
* and for scalars:
*
* std::cout << " x_ at "
* << offsetof(SoA, SoA::x_) << " has size " << sizeof(SoA::x_) << std::endl;
*
*/
#define _DECLARE_SOA_DUMP_INFO_IMPL(IS_COLUMN, TYPE, NAME) \
BOOST_PP_IIF(IS_COLUMN, \
std::cout << " " BOOST_PP_STRINGIZE(NAME) "_[" << SoA::size << "] at " \
<< offsetof(SoA, SoA:: BOOST_PP_CAT(NAME, _)) << " has size " << sizeof(SoA:: BOOST_PP_CAT(NAME, _)) << std::endl; \
, \
std::cout << " " BOOST_PP_STRINGIZE(NAME) "_ at " \
<< offsetof(SoA, SoA:: BOOST_PP_CAT(NAME, _)) << " has size " << sizeof(SoA:: BOOST_PP_CAT(NAME, _)) << std::endl; \
)
#define _DECLARE_SOA_DUMP_INFO(R, DATA, TYPE_NAME) \
BOOST_PP_EXPAND(_DECLARE_SOA_DUMP_INFO_IMPL TYPE_NAME)
#define _DECLARE_SOA_DUMP_INFOS(...) \
BOOST_PP_SEQ_FOR_EACH(_DECLARE_SOA_DUMP_INFO, CLASS, BOOST_PP_VARIADIC_TO_SEQ(__VA_ARGS__))
#define declare_SoA_template(CLASS, ...) \
\
/* dump the SoA internaul structure */ \
template <typename T> \
SOA_HOST_ONLY \
void dump() { \
using SoA = T; \
std::cout << #CLASS "<" << SoA::size << ", " << SoA::alignment << ">: " << '\n'; \
std::cout << " sizeof(...): " << sizeof(CLASS) << '\n'; \
std::cout << " alignof(...): " << alignof(CLASS) << '\n'; \
_DECLARE_SOA_DUMP_INFOS(__VA_ARGS__) \
std::cout << std::endl; \
} \
\
template <size_t SIZE, size_t ALIGN=0> \
struct CLASS { \
\
/* these could be moved to an external type trait to free up the symbol names */ \
using self_type = CLASS; \
static const size_t size = SIZE; \
static const size_t alignment = ALIGN; \
\
/* AoS-like accessor to individual elements */ \
struct const_element { \
SOA_HOST_DEVICE \
const_element(CLASS const& soa, int index) : \
soa_(soa), \
index_(index) \
{ } \
\
_DECLARE_SOA_CONST_ELEMENT_ACCESSORS(__VA_ARGS__) \
\
private: \
CLASS const& soa_; \
const int index_; \
}; \
\
struct element { \
SOA_HOST_DEVICE \
element(CLASS & soa, size_t index) : \
soa_(soa), \
index_(index) \
{ } \
\
SOA_HOST_DEVICE \
element& operator=(element const& other) { \
_DECLARE_SOA_ELEMENT_ASSIGNMENTS(__VA_ARGS__) \
return *this; \
} \
\
SOA_HOST_DEVICE \
element& operator=(element && other) { \
_DECLARE_SOA_ELEMENT_ASSIGNMENTS(__VA_ARGS__) \
return *this; \
} \
\
SOA_HOST_DEVICE \
element& operator=(const_element const& other) { \
_DECLARE_SOA_ELEMENT_ASSIGNMENTS(__VA_ARGS__) \
return *this; \
} \
\
SOA_HOST_DEVICE \
element& operator=(const_element && other) { \
_DECLARE_SOA_ELEMENT_ASSIGNMENTS(__VA_ARGS__) \
return *this; \
} \
\
_DECLARE_SOA_ELEMENT_ACCESSORS(__VA_ARGS__) \
\
private: \
CLASS & soa_; \
const size_t index_; \
}; \
\
/* AoS-like accessor */ \
SOA_HOST_DEVICE \
element operator[](size_t index) { return element(*this, index); } \
\
/* accessors */ \
_DECLARE_SOA_ACCESSORS(__VA_ARGS__) \
_DECLARE_SOA_CONST_ACCESSORS(__VA_ARGS__) \
\
/* dump the SoA internal structure */ \
template <typename T> SOA_HOST_ONLY friend void dump(); \
\
private: \
/* data members */ \
_DECLARE_SOA_DATA_MEMBERS(__VA_ARGS__) \
}