/
soa.d
421 lines (361 loc) · 9.9 KB
/
soa.d
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
/**
* Auto-reimplementation of array types from arrays of structures
* to structures of arrays.
*
* Author: Justin Whear
*/
module soa;
import std.traits : Unqual, isMutable, isArray;
import std.range : ElementType;
import std.typetuple : Erase, staticMap, TypeTuple;
/**
* Reimplements Array-of-Structs $(D A) as a Struct-of-Arrays.
* Effectively does the following transformation:
----
// Array of structs
struct Vector3 {
float x;
float y;
float z;
}
Vector3[100] vectors;
// Struct of arrays
struct Vector3_SOA {
float[100] x;
float[100] y;
}
Vector3_SOA vectors2;
----
* This overload works with static arrays. Provides slicing and indexing, as well
* as member access.
----
SOA!(Vector3[100]) vectors;
vectors[0].x = 100.0;
assert(vectors[0].x == 100.0);
vectors[1] = Vector3(2.0, 2.0, 2.0);
assert(vectors[1] == Vector3(2.0, 2.0, 2.0));
foreach (vec; vectors[0 .. 5])
writeln(vec.x);
----
* Note that the result of indexing (and the element type of the sliced range) is
* not the actual type parameterized on (e.g. Vector3), but rather a dispatching
* type. The dispatching type does implement $(D opEquals) as well as a repacking
* $(opCast) to the original type.
*
* Access to the underlying contiguous arrays is available by using the member
* names on SOA type:
----
SOA!(Vector3[100]) vectors;
assert(is(typeof(vectors.x) == float[100]));
mySIMDInstruction(vectors.x, vectors.y);
----
*
* The underlying static arrays are packed sequentially, so the SOA type can be
* reinterpreted via casts if desired:
----
SOA!(Vector3[2]) vectors;
vectors[0] = Vector3(1.0, 2.0, 3.0);
vectors[1] = Vector3(10.0, 20.0, 30.0);
// prove the data is actually packed SOA
auto fv = (cast(float*)&vectors)[0 .. vectors.length * 3];
assert(fv[0] == 1.0); // x0
assert(fv[1] == 10.0); // x1
assert(fv[2] == 2.0); // y0
assert(fv[3] == 20.0); // y1
assert(fv[4] == 3.0); // z0
assert(fv[5] == 30.0); // z1
----
* Finally, static SOA types require no extra storage. Dynamic SOA types incur
* the overhead inherent to dynamic array types for each member.
----
Vector3[2] a;
SOA!(typeof(b)) b;
static assert(typeof(a).sizeof == typeof(b).sizeof);
----
*/
struct SOA(A : T[N], T, size_t N) if (is(T == struct))
{
mixin CommonImpl;
// This string mixin is, unfortunately, the only way to initialize the member arrays
private static string getMemberDecls() @property pure
{
string ret;
foreach (name; Fields)
ret ~= `typeof(U.`~name~`)[N] `~name~` = initValues.`~name~`;`;
return ret;
}
// Actual storage
mixin(getMemberDecls);
/// Array lengths
static enum length = N;
}
/**
* Ditto previous overload with a few notes.
* 1) The length member may be set. If grown, the added space in the member arrays
* will initialized to the appropriate member's initialization value.
* 2) The member arrays ARE NOT contiguous with one another as in the case of
* static array overload. If this is desired, you may allocate these arrays
* yourself and set them:
----
SOA!(Vector3[]) vectors;
// allocation functions not actually supplied ;)
vectors.x = allocate();
vectors.y = allocateNextTo(vectors.x);
vectors.z = allocateNextTo(vectors.z);
----
* 3) If you choose to tinker with the member arrays, you are responsible for
* ensuring that they all have equal lengths.
*/
struct SOA(A : T[], T) if (is(T == struct))
{
mixin CommonImpl;
private static string getMemberDecls() @property pure
{
string ret;
foreach (name; Fields)
ret ~= `typeof(U.`~name~`)[] `~name~`;`;
return ret;
}
// Actual storage
mixin(getMemberDecls);
/// Array lengths
auto length() @property const
{
// We expect all arrays to have the same length
static enum FirstMember = Fields[0];
return __traits(getMember, this, FirstMember).length;
}
///
void length(size_t newLen) @property
{
auto oldLen = length;
foreach (Name; Fields)
{
__traits(getMember, this, Name).length = newLen;
if (oldLen < newLen)
{
// initialize new values
foreach (ref e; __traits(getMember, this, Name)[oldLen .. newLen])
e = __traits(getMember, initValues, Name);
}
}
}
}
// Static length SOA tests
unittest {
struct Vector3
{
float x = 1, y = 4, z = 9;
}
import std.typetuple;
import std.algorithm : equal;
Vector3[3] witness;
// Ensure that SOA of various types can be constructed and all expected operations work
alias MutableTypes = TypeTuple!(Vector3[3]);
alias ImmutableTypes = TypeTuple!(
const(Vector3)[3],
const(Vector3[3])
);
foreach (B; TypeTuple!(MutableTypes, ImmutableTypes))
{
SOA!(B) b;
static assert(typeof(b).sizeof == typeof(witness).sizeof);
// Ensure storage is actually SOA by checking the underlying memory
auto fv = (cast(float*)&b)[0 .. b.length * Vector3.tupleof.length];
assert(fv[0 .. 3].equal([1,1,1])); // x's are all 1
assert(fv[3 .. 6].equal([4,4,4])); // y's are all 4
assert(fv[6 .. 9].equal([9,9,9])); // z's are all 9
// Test memberwise access
foreach (i, wEl; witness)
{
assert(wEl.x == b[i].x);
assert(wEl.y == b[i].y);
assert(wEl.z == b[i].z);
}
// test opEquals with original type
assert(witness[0] == b[0]);
assert(witness[].equal(b[]));
// test opEquals with Dispatch type
assert(b[0] == b[1]);
assert(b[].equal(b[]));
}
foreach (B; MutableTypes)
{
SOA!(B) b;
// Test setting
b[0].x = 11;
b[1].y = 21;
b[2].z = 31;
assert(b[0].x == 11);
assert(b[1].y == 21);
assert(b[2].z == 31);
// Or whole Vector at a time
b[0] = Vector3(3.0,4,5);
assert(b[0] == Vector3(3.0,4,5));
}
SOA!(Vector3[2]) vectors;
vectors[0] = Vector3(1.0, 2.0, 3.0);
vectors[1] = Vector3(10.0, 20.0, 30.0);
auto fv = (cast(float*)&vectors)[0 .. vectors.length * 3];
assert(fv[0] == 1.0); // x0
assert(fv[1] == 10.0); // x1
assert(fv[2] == 2.0); // y0
assert(fv[3] == 20.0); // y1
assert(fv[4] == 3.0); // z0
assert(fv[5] == 30.0); // z1
// Will it explode if the struct has non-field members?
struct Person
{
int age;
string name;
void sayMyName()
{
import std.stdio;
writeln(name);
}
bool x;
bool y() @property const { return false; }
}
SOA!(Person[3]) people;
//pragma(msg, typeof(people).Fields); // should be 'age', 'name', 'x'
}
// Dynamic length SOA tests
unittest {
struct Vector3
{
float x = 1, y = 4, z = 9;
}
import std.typetuple;
import std.algorithm : equal;
Vector3[] witness = [Vector3(), Vector3(), Vector3()];
// Ensure that SOA of various types can be constructed and all expected operations work
alias MutableTypes = TypeTuple!(Vector3[]);
alias ImmutableTypes = TypeTuple!(
const(Vector3)[],
const(Vector3[])
);
foreach (B; TypeTuple!(MutableTypes, ImmutableTypes))
{
SOA!(B) b;
b.length = 3;
// Ensure storage is actually SOA by checking the underlying memory
//NOTE THAT THE FIELD ARRAYS ARE NOT THEMSELVES CONTIGUOUS AS WITH THE
// STATIC VERSION
assert(b.x == [1, 1, 1]);
assert(b.y == [4, 4, 4]);
assert(b.z == [9, 9, 9]);
// Test memberwise access
foreach (i, wEl; witness)
{
assert(wEl.x == b[i].x);
assert(wEl.y == b[i].y);
assert(wEl.z == b[i].z);
}
// test opEquals with original type
assert(witness[0] == b[0]);
assert(witness[].equal(b[]));
// test opEquals with Dispatch type
assert(b[0] == b[1]);
assert(b[].equal(b[]));
}
foreach (B; MutableTypes)
{
SOA!(B) b;
b.length = 3;
// Test setting
b[0].x = 11;
b[1].y = 21;
b[2].z = 31;
assert(b[0].x == 11);
assert(b[1].y == 21);
assert(b[2].z == 31);
// Or whole Vector at a time
b[0] = Vector3(3.0,4,5);
assert(b[0] == Vector3(3.0,4,5));
}
}
// This implementation is identical between the static and dynamic versions
private mixin template CommonImpl()
{
alias U = Unqual!T;
// T may have methods and non-field members, preventing the use of the
// allMembers trait. We work around this by getting the names of the fields
// by using tupleof.
template Iota(size_t I, size_t Len) {
static if (I < Len)
alias Iota = TypeTuple!(I, Iota!(I + 1, Len));
else
alias Iota = TypeTuple!();
}
static enum GetFieldName(size_t I) = T.tupleof[I].stringof;
// The names of the fields in order
static enum Fields = Erase!("this", staticMap!(GetFieldName, Iota!(0, T.tupleof.length)));
// This is required to initialize the arrays to corresponding init values from
// the user's T definition
private static enum T initValues = T.init;
/// Provides element access just like original AOS type
auto ref opIndex(size_t i) pure
{
alias Parent = typeof(this);
// Dispatches `.x`, etc. to parent.x[i]
static struct Dispatcher
{
Parent* parent;
const size_t idx;
// Support `a[0].x`
auto opDispatch(string op)() @property const pure
{
return __traits(getMember, parent, op)[idx];
}
// Support `a[0].x = y`
static if (isMutable!T)
void opDispatch(string op, V)(V newVal) @property
{
__traits(getMember, parent, op)[idx] = newVal;
}
// Check equality with other instances of this Dispatch type
bool opEquals()(auto ref const typeof(this) v) const
{
foreach (Name; Fields)
if (__traits(getMember, v, Name) != opDispatch!Name) return false;
return true;
}
// Check equality with the original type
bool opEquals()(auto ref const U v) const
{
// statically unrolled
foreach (Name; Fields)
if (__traits(getMember, v, Name) != opDispatch!Name) return false;
return true;
}
/// Assign from original structure type
static if (isMutable!T)
void opAssign(T v)
{
// statically unrolled
foreach (Name; Fields)
opDispatch!Name(__traits(getMember, v, Name));
}
/// Repacks to the original structure type
T opCast(T)()
{
U ret;
foreach (Name; Fields)
__traits(getMember, ret, Name) = opDispatch!Name;
return cast(T)ret;
}
}
return Dispatcher(&this, i);
}
///
auto opSlice()
{
return this[0 .. length];
}
///
auto opSlice(size_t s, size_t e)
{
import std.range, std.algorithm;
return iota(s, e).map!(n => this.opIndex(n));
}
}