-
Notifications
You must be signed in to change notification settings - Fork 1
/
soa_v3.h
167 lines (137 loc) · 12.6 KB
/
soa_v3.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
/*
* Structure-of-Arrays template defined at compile time 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 SoA data members; these should exapnd to, for example
*
* alignas(ALIGN) double x_[SIZE];
*
*/
#define _DECLARE_SOA_DATA_MEMBER_IMPL(TYPE, NAME) \
alignas(ALIGN) TYPE BOOST_PP_CAT(NAME, _[SIZE]);
#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 example
*
* double* x() { return x_; }
*
*/
#define _DECLARE_SOA_ACCESSOR_IMPL(TYPE, NAME) \
SOA_HOST_DEVICE \
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__))
/* assignment of individual fields; these should expand to, for example
*
* x() = other.x();
*
*/
#define _DECLARE_SOA_ELEMENT_ASSIGNMENT_IMPL(TYPE, NAME) \
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 example
*
* double & x() { return * (soa_.x() + index_); }
*
*/
#define _DECLARE_SOA_ELEMENT_ACCESSOR_IMPL(TYPE, NAME) \
SOA_HOST_DEVICE \
TYPE & NAME() { return * (soa_. NAME () + index_); }
#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__))
/* dump SoA fields information; these should expand to, for example
*
* std::cout << " x_[" << SoA::size << "] at "
* << offsetof(SoA, SoA::x_) << " has size " << sizeof(SoA::x_) << std::endl;
*
*/
#define _DECLARE_SOA_DUMP_INFO_IMPL(TYPE, NAME) \
std::cout << " " BOOST_PP_STRINGIZE(NAME) "_[" << SoA::size << "] at " \
<< offsetof(SoA, SoA:: BOOST_PP_CAT(NAME, _)) << " has size " << sizeof(SoA:: BOOST_PP_CAT(NAME, _)) << '\n';
#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 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; \
} \
\
_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__) \
\
/* dump the SoA internal structure */ \
template <typename T> SOA_HOST_ONLY friend void dump(); \
\
private: \
/* data members */ \
_DECLARE_SOA_DATA_MEMBERS(__VA_ARGS__) \
}