-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsoa_compare.benchmark.cpp
1210 lines (1125 loc) · 35.8 KB
/
soa_compare.benchmark.cpp
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
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//Purpose:
// Compare methods of Structure Of Arrays data structure.
//OriginalSource:
// On 2016-10-19 by cppljevans
// WHAT: downloaded original source from:
// http://codepad.org/eol6auRN
//RESULT:
/*
/tmp/build/clangxx3_8_pkg/clang/struct_of_arrays/work/soa_compare.benchmark.exe
particle_count=1,000,000
minimum duration=5.18095
comparitive performance table:
method rel_duration
________ ______________
Block 1
StdArray 1.00159
Flat 1.00874
SoA 1.0325
AoS 1.39591
Compilation finished at Mon Oct 24 08:26:48
*/
//=============================
#define NDEBUG //disable assert's.
#include "sse.hpp"
#include <vector>
#include <string>
#include <chrono>
#include <iostream>
#include <locale>
#include <random>
#include <algorithm>
#include <array>
#include <boost/align/aligned_allocator.hpp>
#include <boost/align/align_up.hpp>
#include "bit_vector.hpp"//also: bits_per_block.
#include <boost/fusion/include/for_each.hpp>
#include <boost/fusion/include/at_c.hpp>
#include <boost/fusion/container/vector.hpp>
template<typename... T>
using
soa_struct=boost::fusion::vector<T...>
;
template<std::size_t Index,typename... T>
auto&
get_soa(soa_struct<T...>&t)
{ return boost::fusion::at_c<Index>(t)
;}
template<std::size_t Index,typename... T>
auto const&
get_soa(soa_struct<T...>const &t)
{ return boost::fusion::at_c<Index>(t)
;}
using namespace std;
using boost::alignment::aligned_allocator;
struct float2_t {
sse_float x, y;
};
struct float3_t {
sse_float x, y, z;
friend float3_t operator-( const float3_t & lhs, const float3_t & rhs ) {
return float3_t{ lhs.x - rhs.x, lhs.y - rhs.y, lhs.z - rhs.z };
}
friend float3_t operator+( const float3_t & lhs, const float3_t & rhs ) {
return float3_t{ lhs.x + rhs.x, lhs.y + rhs.y, lhs.z + rhs.z };
}
friend float3_t operator*( const float3_t & lhs, sse_float rhs ) {
return float3_t{ lhs.x * rhs, lhs.y * rhs, lhs.z * rhs };
}
float3_t& operator+=( const float3_t & rhs ) {
x += rhs.x;
y += rhs.y;
z += rhs.z;
return *this;
}
};
struct float4_t {
sse_float x, y, z, w;
};
struct particle_nest_t {
float3_t position;
float3_t velocity;
float3_t acceleration;
float2_t size;
float4_t color;
sse_float energy;
bool alive;
};
struct particle_nest_enum
{
enum particle_e {
position,
velocity,
acceleration,
size,
color,
energy,
alive
};
};
struct particle_flat_enum
{
enum particle_e {
position_x,
position_y,
position_z,
velocity_x,
velocity_y,
velocity_z,
acceleration_x,
acceleration_y,
acceleration_z,
size,
color,
energy,
alive
};
};
uniform_real_distribution<float> pdist( -10.f, 10.f );
uniform_real_distribution<float> vxzdist( -5.f, 5.f );
uniform_real_distribution<float> vydist( 4.f, 20.f );
uniform_real_distribution<float> adist( -1.f, 1.f );
uniform_real_distribution<float> sdist( 0.1f, 5.f );
uniform_real_distribution<float> cdist( 0.f, 1.f );
uniform_real_distribution<float> edist( 10.f, 1000.f );
constexpr float gravity = -9.8f;
constexpr float dt = 1.0f;
enum
method_enum
{ AoS
, SoA_vec
, SoA_flat
, SoA_block
, SSE_block
, SSE_vec
, SSEopt_vec
, LFA
, method_last
};
constexpr std::size_t
loop_increment[method_last]=
{ 1//AoS
, 1//SoA_vec
, 1//SoA_block
, sse_width//SSE_block
, sse_width//SSE_vec
, sse_width//SSEopt_vec
, 0//LFA(unused)
};
#define STRINGIZE(x) #x
std::string const
method_name[method_last+1]=
{ STRINGIZE(AoS)
, STRINGIZE(SoA_vec)
, STRINGIZE(SoA_flat)
, STRINGIZE(SoA_block)
, STRINGIZE(SSE_block)
, STRINGIZE(SSE_vec)
, STRINGIZE(SSEopt_vec)
, STRINGIZE(LFA)
, STRINGIZE(method_last)
};
template
< method_enum Method
>
struct emitter_method
{ static constexpr method_enum
method=Method;
static std::size_t
resize_up(std::size_t n)
{
return boost::alignment::
align_up
( n
, loop_increment[method]
);
}
};
template
< method_enum Method
>
struct emitter_t
/**@brief
* struct emitter_t<Method>
* : public emitter_method<Method>
* {
* private:
* [storage<Method>]
* //where [storage<Method>]
* //is some data structure
* //dependent on Method.
* particles;
* public:
* void resize( size_t n)
* //resize number of particles
* ;
* size_t particles_size()const
* //return number of particles
* ;
* emitter_t
* ( size_t particles_size
* , mt19937 & rng
* )
* //Create particles_size number of particles,
* //then generate(rng).
* ;
* void generate( mt19937 & rng )
* //reset particles with some function of rng
* ;
* void update()
* //for all particles, update the particle as function of that particle.
* ;
* };
*/
;
template<>
struct emitter_t<AoS>
: emitter_method<AoS>
{
private:
vector<particle_nest_t> particles;
public:
void resize( size_t n ) {
n = resize_up(n);
if(n>0) particles.resize( n );
}
size_t particles_size()const {
return particles.size();
}
emitter_t
( size_t particles_size
, mt19937& rng
)
: particles(particles_size)
{
generate(rng);
}
void generate( mt19937 & rng) {
auto n=particles_size();
for ( size_t i = 0; i < n; ++i ) {
particles[i].position = float3_t{ pdist( rng ), pdist( rng ), pdist( rng ) };
particles[i].velocity = float3_t{ vxzdist( rng ), vydist( rng ), vxzdist( rng ) };
particles[i].acceleration = float3_t{ adist( rng ), adist( rng ), adist( rng ) };
particles[i].size = float2_t{ sdist( rng ), sdist( rng ) };
particles[i].color = float4_t{ cdist( rng ), cdist( rng ), cdist( rng ), cdist( rng ) };
particles[i].energy = edist( rng );
particles[i].alive = true;
}
}
void update() {
for ( auto & prt : particles ) {
auto & p = prt.position;
auto & v = prt.velocity;
auto & a = prt.acceleration;
auto & e = prt.energy;
v += (a * dt);
v.y += gravity * dt;
p += (v * dt);
e -= dt;
if ( e <= 0 ) {
prt.alive = false;
}
}
}
};
template<>
struct emitter_t<SoA_vec>
: emitter_method<SoA_vec>
, particle_nest_enum
{
private:
soa_struct <
vector<float3_t>,// position;
vector<float3_t>,// velocity;
vector<float3_t>,// acceleration;
vector<float2_t>,// size;
vector<float4_t>,// color;
vector<float>,// energy;
vector<char>// alive;
>
particles;
template
< size_t Index
>
auto*
data()
{ return get_soa<Index>(particles).data();}
public:
void resize( size_t n ) {
n=resize_up(n);
auto fun=[n](auto& e){ e.resize(n);};
boost::fusion::for_each(particles,fun);
}
std::size_t particles_size()const {
return get_soa<position>(particles).size();
}
emitter_t
( std::size_t particles_size
, mt19937 & rng
)
{
resize(particles_size);
generate(rng);
}
void generate( mt19937 & rng ) {
auto n=particles_size();
for ( size_t i = 0; i < n; ++i ) {
data<position>()[i] = float3_t{ pdist( rng ), pdist( rng ), pdist( rng ) };
data<velocity>()[i] = float3_t{ vxzdist( rng ), vydist( rng ), vxzdist( rng ) };
data<acceleration>()[i] = float3_t{ adist( rng ), adist( rng ), adist( rng ) };
data<size>()[i] = float2_t{ sdist( rng ), sdist( rng ) };
data<color>()[i] = float4_t{ cdist( rng ), cdist( rng ), cdist( rng ), cdist( rng ) };
data<energy>()[i] = edist( rng );
}
}
void update() {
size_t n = particles_size();
auto ps = data<position>();
auto vs = data<velocity>();
auto as = data<acceleration>();
auto es = data<energy>();
auto als = data<alive>();
for ( size_t i = 0; i < n; ++i ) {
auto & p = ps[i];
auto & v = vs[i];
auto & a = as[i];
auto & e = es[i];
v += (a * dt);
v.y += gravity * dt;
p += (v * dt);
e -= dt;
if ( e <= 0 ) {
als[i] = false;
}
}
}
};
template<>
struct emitter_t<SoA_flat>
: emitter_method<SoA_flat>
{
private:
char * data;
size_t capacity;
void free() {
delete[] data;
}
public:
~emitter_t() {
free();
}
float3_t* get_position()
{ return reinterpret_cast<float3_t*>(data); }
float3_t* get_velocity()
{ return reinterpret_cast<float3_t*>(data + capacity * offsetof(particle_nest_t, velocity)); }
float3_t* get_acceleration()
{ return reinterpret_cast<float3_t*>(data + capacity * offsetof(particle_nest_t, acceleration)); }
float2_t* get_size()
{ return reinterpret_cast<float2_t*>(data + capacity * offsetof(particle_nest_t, size)); }
float4_t* get_color()
{ return reinterpret_cast<float4_t*>(data + capacity * offsetof(particle_nest_t, color)); }
float* get_energy()
{ return reinterpret_cast<float*>(data + capacity * offsetof(particle_nest_t, energy)); }
char* get_alive()
{ return reinterpret_cast<char*>(data + capacity * offsetof(particle_nest_t, alive)); }
void resize( size_t n ) {
n = resize_up(n);
capacity = n;
data = new char[
sizeof( float3_t )*n +
sizeof( float3_t )*n +
sizeof( float3_t )*n +
sizeof( float2_t )*n +
sizeof( float4_t )*n +
sizeof( sse_float )*n +
sizeof( char )*n
];
}
size_t particles_size()const {
return capacity;
}
emitter_t
( size_t particles_size
, mt19937 & rng
)
: capacity(particles_size)
{
resize(particles_size);
generate(rng);
}
void generate(mt19937 & rng ) {
auto n = particles_size();
for ( size_t i = 0; i < n; ++i ) {
get_position()[i] = float3_t{ pdist( rng ), pdist( rng ), pdist( rng ) };
get_velocity()[i] = float3_t{ vxzdist( rng ), vydist( rng ), vxzdist( rng ) };
get_acceleration()[i] = float3_t{ adist( rng ), adist( rng ), adist( rng ) };
get_size()[i] = float2_t{ sdist( rng ), sdist( rng ) };
get_color()[i] = float4_t{ cdist( rng ), cdist( rng ), cdist( rng ), cdist( rng ) };
get_energy()[i] = edist( rng );
get_alive()[i] = true;
}
}
void update() {
size_t n = capacity;
auto ps = get_position();
auto vs = get_velocity();
auto as = get_acceleration();
auto es = get_energy();
auto als = get_alive();
for ( size_t i = 0; i < n; ++i ) {
auto & p = ps[i];
auto & v = vs[i];
auto & a = as[i];
auto & e = es[i];
v += (a * dt);
v.y += gravity * dt;
p += (v * dt);
e -= dt;
if ( e <= 0 ) {
als[i] = false;
}
}
}
};
#include "soa_block.hpp"
template<>
struct emitter_t<SoA_block>
: emitter_method<SoA_block>
/**@brief
* Pretty much cut&past from above
* soa_emitter_static_t
* but use soa_block instead of soa_array.
*/
, particle_nest_enum
{
private:
soa_block<
float3_t,
float3_t,
float3_t,
float2_t,
float4_t,
float,
bool>
particles;
public:
void resize( size_t n ) {
n = resize_up(n);
particles.resize(n);
}
std::size_t particles_size()const {
return particles.vec_size();
}
emitter_t
( std::size_t particle_count
, mt19937 & rng
)
: particles(particle_count)
{
generate(rng);
}
void generate( mt19937 & rng ) {
auto n=particles_size();
auto begins_v=particles.begin_all();
for ( size_t i = 0; i < n; ++i ) {
get<position>(begins_v)[i] = float3_t{ pdist( rng ), pdist( rng ), pdist( rng ) };
get<velocity>(begins_v)[i] = float3_t{ vxzdist( rng ), vydist( rng ), vxzdist( rng ) };
get<acceleration>(begins_v)[i] = float3_t{ adist( rng ), adist( rng ), adist( rng ) };
get<size>(begins_v)[i] = float2_t{ sdist( rng ), sdist( rng ) };
get<color>(begins_v)[i] = float4_t{ cdist( rng ), cdist( rng ), cdist( rng ), cdist( rng ) };
get<energy>(begins_v)[i] = edist( rng );
get<alive>(begins_v)[i] = true;
}
}
void update() {
auto begins_v=particles.begin_all();
size_t n = particles_size();
for ( size_t i = 0; i < n; ++i ) {
auto & p = get<position>(begins_v)[i];
auto & v = get<velocity>(begins_v)[i];
auto & a = get<acceleration>(begins_v)[i];
auto & e = get<energy>(begins_v)[i];
v += (a * dt);
v.y += gravity * dt;
p += (v * dt);
e -= dt;
if ( e <= 0 ) {
get<alive>(begins_v)[i] = false;
}
}
}
};
#include <libflatarray/flat_array.hpp>
class particle_lfa_t
{
public:
float position[3];
float velocity[3];
float acceleration[3];
float size[2];
float color[4];
float energy;
float alive;
};
LIBFLATARRAY_REGISTER_SOA(
particle_lfa_t,
((float)(position)(3))
((float)(velocity)(3))
((float)(acceleration)(3))
((float)(size)(2))
((float)(color)(4))
((float)(energy))
((float)(alive))
)
template<>
struct emitter_t<LFA>
: emitter_method<LFA>
{
private:
LibFlatArray::soa_vector<particle_lfa_t> particles;
public:
void resize( size_t n){
if(n>0) particles.resize(n);
}
size_t particles_size()const{
return particles.size();
}
emitter_t( size_t n, mt19937 & rng) {
particles.resize(n);
generate(rng);
}
void generate( mt19937 & rng)
{
auto n = particles_size();
// The SoA layout is fixed at compile time. Multiple layouts
// are available via templates. callback() dispatches at
// runtime to the correct template instantiation:
particles.callback([n, &rng](auto particle){
for (; particle.index() < n; ++particle ) {
particle.position()[0] = pdist( rng );
particle.position()[1] = pdist( rng );
particle.position()[2] = pdist( rng );
particle.velocity()[0] = vxzdist( rng );
particle.velocity()[1] = vxzdist( rng );
particle.velocity()[2] = vxzdist( rng );
particle.acceleration()[0] = adist( rng );
particle.acceleration()[1] = adist( rng );
particle.acceleration()[2] = adist( rng );
particle.size()[0] = sdist( rng );
particle.size()[1] = sdist( rng );
particle.color()[0] = cdist( rng );
particle.color()[1] = cdist( rng );
particle.color()[2] = cdist( rng );
particle.color()[3] = cdist( rng );
particle.energy() = edist( rng );
particle.alive() = 1;
}
});
}
void update()
{
using LibFlatArray::any;
using LibFlatArray::get;
long n = particles.size();
long hits = 0;
particles.callback
( [n, &hits](auto particle)
{
// short_vec behaves much like an ordinary float, but
// maps all computation to vector intrinsics. The ISA
// (SSE, AVX, AVX512...) is chosen automatically at
// compile time.
typedef LibFlatArray::short_vec<float, 16> Float;
// The loop peeler handles left-over loop iterations
// when the number of particles is not a multiple of
// the vector arity...
LibFlatArray::loop_peeler<Float>
( &particle.index()
, n
, [&particle, n, &hits](auto new_float, long *i, long end)
{
// ...by switching arities:
typedef decltype(new_float) Float;
Float dt2 = dt;
for (; particle.index() < end; particle += Float::ARITY)
{
// vector loads are issued by passing a pointer to the c-tor:
Float v0 = Float(&particle.velocity()[0]) + (Float(&particle.acceleration()[0]) * dt2);
Float v1 = Float(&particle.velocity()[1]) + (Float(&particle.acceleration()[1]) * dt2);
Float v2 = Float(&particle.velocity()[2]) + (Float(&particle.acceleration()[2]) * dt2);
v1 += gravity * dt2;
&particle.velocity()[0] << v0;
&particle.velocity()[1] << v1;
&particle.velocity()[2] << v2;
Float p0 = Float(&particle.position()[0]) + (Float(&particle.velocity()[0]) * dt2);
Float p1 = Float(&particle.position()[1]) + (Float(&particle.velocity()[1]) * dt2);
Float p2 = Float(&particle.position()[2]) + (Float(&particle.velocity()[2]) * dt2);
&particle.position()[0] << p0;
&particle.position()[1] << p1;
&particle.position()[2] << p2;
Float e = Float(&particle.energy()) - dt2;
&particle.energy() << e;
// initialization from scalar value
// broadcasts the value to all vector
// lanes:
Float alive = 1;
// Comparison creates a bit-mask which can
// be used to selectively set values in
// the target register(s):
alive.blend((e <= 0.0f), 0.0);
&particle.alive() << alive;
}
}
);
}
);
}
};
template<>
struct emitter_t<SSE_block>
: emitter_method<SSE_block>
, particle_flat_enum
{
private:
soa_block
<
type_align<float,sse_align>,// position_x;
type_align<float,sse_align>,// position_y;
type_align<float,sse_align>,// position_z;
type_align<float,sse_align>,// velocity_x;
type_align<float,sse_align>,// velocity_y;
type_align<float,sse_align>,// velocity_z;
type_align<float,sse_align>,// acceleration_x;
type_align<float,sse_align>,// acceleration_y;
type_align<float,sse_align>,// acceleration_z;
float2_t,// size;
float4_t,// color;
type_align<float,sse_align>,// energy;
char// alive;
> particles;
template<particle_e Index>
auto* data(){ return particles.begin<Index>();}
public:
void resize( size_t n) {
n = resize_up(n);
particles.resize(n);
}
std::size_t particles_size()const {
return particles.vec_size();
}
emitter_t
( size_t particles_size
, mt19937 & rng
)
: particles(particles_size)
{
generate(rng);
}
void generate( mt19937 & rng ) {
size_t n=particles_size();
for ( size_t i = 0; i < n; ++i ) {
data<position_x>()[i] = pdist( rng );
data<position_y>()[i] = pdist( rng );
data<position_z>()[i] = pdist( rng );
data<velocity_x>()[i] = vxzdist( rng );
data<velocity_y>()[i] = vxzdist( rng );
data<velocity_z>()[i] = vxzdist( rng );
data<acceleration_x>()[i] = adist( rng );
data<acceleration_y>()[i] = adist( rng );
data<acceleration_z>()[i] = adist( rng );
data<size>()[i] = float2_t{ sdist( rng ), sdist( rng ) };
data<color>()[i] = float4_t{ cdist( rng ), cdist( rng ), cdist( rng ), cdist( rng ) };
data<energy>()[i] = edist( rng );
}
}
void update() {
size_t n = particles_size();
__m128 vx, vy, vz;
__m128 t = _mm_set1_ps( dt );
__m128 g = _mm_set1_ps( gravity * dt );
__m128 zero = _mm_setzero_ps();
for ( size_t i = 0; i < n; i += sse_width ) {
vx = _mm_add_ps( _mm_load_ps( data<velocity_x>()+i ), _mm_mul_ps( t, _mm_load_ps( data<acceleration_x>()+i )));
vy = _mm_add_ps( _mm_load_ps( data<velocity_y>()+i ), _mm_mul_ps( t, _mm_load_ps( data<acceleration_y>()+i )));
vz = _mm_add_ps( _mm_load_ps( data<velocity_z>()+i ), _mm_mul_ps( t, _mm_load_ps( data<acceleration_z>()+i )));
vy = _mm_add_ps( vy, g );
_mm_store_ps( data<position_x>()+i, _mm_add_ps(_mm_load_ps(data<position_x>()+i), _mm_mul_ps(vx, t)));
_mm_store_ps( data<position_y>()+i, _mm_add_ps(_mm_load_ps(data<position_y>()+i), _mm_mul_ps(vy, t)));
_mm_store_ps( data<position_z>()+i, _mm_add_ps(_mm_load_ps(data<position_z>()+i), _mm_mul_ps(vz, t)));
_mm_store_ps( data<velocity_x>()+i, vx );
_mm_store_ps( data<velocity_y>()+i, vy );
_mm_store_ps( data<velocity_z>()+i, vz );
_mm_store_ps( data<energy>()+i, _mm_sub_ps(_mm_load_ps(data<energy>()+i), t));
auto a = _mm_movemask_ps( _mm_cmple_ps( _mm_load_ps( data<energy>()+i ), zero ));
for ( int j = 0; j < sse_width; ++j ) {
data<alive>()[i+j] = (a & (1 << j));
}
}
}
};
template< typename T >
using sse_vector = vector<T, aligned_allocator<T,sse_align> >;
template<>
struct emitter_t<SSE_vec>
: emitter_method<SSE_vec>
, particle_flat_enum
{
private:
soa_struct
<
sse_vector<float>,// position_x;
sse_vector<float>,// position_y;
sse_vector<float>,// position_z;
sse_vector<float>,// velocity_x;
sse_vector<float>,// velocity_y;
sse_vector<float>,// velocity_z;
sse_vector<float>,// acceleration_x;
sse_vector<float>,// acceleration_y;
sse_vector<float>,// acceleration_z;
vector<float2_t>,// size;
vector<float4_t>,// color;
sse_vector<float>,// energy;
vector<char>// alive;
> particles;
template<particle_e Index>
auto& get(){ return get_soa<Index>(particles);}
template<particle_e Index>
auto const& get()const{ return get_soa<Index>(particles);}
template<particle_e Index>
auto* data(){ return get<Index>().data();}
public:
void resize( size_t n) {
n = resize_up(n);
get<position_x>().resize( n );
get<position_y>().resize( n );
get<position_z>().resize( n );
get<velocity_x>().resize( n );
get<velocity_y>().resize( n );
get<velocity_z>().resize( n );
get<acceleration_x>().resize( n );
get<acceleration_y>().resize( n );
get<acceleration_z>().resize( n );
get<size>().resize( n );
get<color>().resize( n );
get<energy>().resize( n );
get<alive>().resize( n, true );
}
size_t particles_size()const {
return get<position_x>().size();
}
emitter_t
( size_t particles_size
, mt19937 & rng
)
{
resize(particles_size);
generate(rng);
}
void generate( mt19937 & rng ) {
auto n=particles_size();
for ( size_t i = 0; i < n; ++i ) {
data<position_x>()[i] = pdist( rng );
data<position_y>()[i] = pdist( rng );
data<position_z>()[i] = pdist( rng );
data<velocity_x>()[i] = vxzdist( rng );
data<velocity_y>()[i] = vxzdist( rng );
data<velocity_z>()[i] = vxzdist( rng );
data<acceleration_x>()[i] = adist( rng );
data<acceleration_y>()[i] = adist( rng );
data<acceleration_z>()[i] = adist( rng );
data<size>()[i] = float2_t{ sdist( rng ), sdist( rng ) };
data<color>()[i] = float4_t{ cdist( rng ), cdist( rng ), cdist( rng ), cdist( rng ) };
data<energy>()[i] = edist( rng );
}
}
std::size_t particles_size() {
return get<position_x>().size();
}
void update() {
size_t n = particles_size();
__m128 vx, vy, vz;
__m128 t = _mm_set1_ps( dt );
__m128 g = _mm_set1_ps( gravity * dt );
__m128 zero = _mm_setzero_ps();
for ( size_t i = 0; i < n; i += sse_width ) {
vx = _mm_add_ps( _mm_load_ps( data<velocity_x>()+i ), _mm_mul_ps( t, _mm_load_ps( data<acceleration_x>()+i )));
vy = _mm_add_ps( _mm_load_ps( data<velocity_y>()+i ), _mm_mul_ps( t, _mm_load_ps( data<acceleration_y>()+i )));
vz = _mm_add_ps( _mm_load_ps( data<velocity_z>()+i ), _mm_mul_ps( t, _mm_load_ps( data<acceleration_z>()+i )));
vy = _mm_add_ps( vy, g );
_mm_store_ps( data<position_x>()+i, _mm_add_ps(_mm_load_ps(data<position_x>()+i), _mm_mul_ps(vx, t)));
_mm_store_ps( data<position_y>()+i, _mm_add_ps(_mm_load_ps(data<position_y>()+i), _mm_mul_ps(vy, t)));
_mm_store_ps( data<position_z>()+i, _mm_add_ps(_mm_load_ps(data<position_z>()+i), _mm_mul_ps(vz, t)));
_mm_store_ps( data<velocity_x>()+i, vx );
_mm_store_ps( data<velocity_y>()+i, vy );
_mm_store_ps( data<velocity_z>()+i, vz );
_mm_store_ps( data<energy>()+i, _mm_sub_ps(_mm_load_ps(data<energy>()+i), t));
auto a = _mm_movemask_ps( _mm_cmple_ps( _mm_load_ps( data<energy>()+i ), zero ));
for ( int j = 0; j < sse_width; ++j ) {
data<alive>()[i+j] = (a & (1 << j));
}
}
}
};
template<>
struct emitter_t<SSEopt_vec>
: emitter_method<SSEopt_vec>
{
private:
sse_vector<float> position_x;
sse_vector<float> position_y;
sse_vector<float> position_z;
sse_vector<float> velocity_x;
sse_vector<float> velocity_y;
sse_vector<float> velocity_z;
sse_vector<float> acceleration_x;
sse_vector<float> acceleration_y;
sse_vector<float> acceleration_z;
vector<float2_t> size;
vector<float4_t> color;
sse_vector<float> energy;
bit_vector alive;
public:
~emitter_t() {
//std::cout<<"~emitter_t("<<method_name[method]<<")"<<std::endl;
}
void resize( size_t n ) {
if(n>0)
{
n = resize_up(n);
position_x.resize( n );
position_y.resize( n );
position_z.resize( n );
velocity_x.resize( n );
velocity_y.resize( n );
velocity_z.resize( n );
acceleration_x.resize( n );
acceleration_y.resize( n );
acceleration_z.resize( n );
size.resize( n );
color.resize( n );
energy.resize( n );
alive.resize( n, true );
}
}
std::size_t particles_size()const {
return position_x.size();
}
emitter_t( size_t particles_size, mt19937 & rng) {
resize(particles_size);
generate(rng);
}
void generate( mt19937 & rng) {
auto n = particles_size();
for ( size_t i = 0; i < n; ++i ) {
position_x[i] = pdist( rng );
position_y[i] = pdist( rng );
position_z[i] = pdist( rng );
velocity_x[i] = vxzdist( rng );
velocity_y[i] = vxzdist( rng );
velocity_z[i] = vxzdist( rng );
acceleration_x[i] = adist( rng );
acceleration_y[i] = adist( rng );
acceleration_z[i] = adist( rng );
size[i] = float2_t{ sdist( rng ), sdist( rng ) };
color[i] = float4_t{ cdist( rng ), cdist( rng ), cdist( rng ), cdist( rng ) };
energy[i] = edist( rng );
}
}
void update() {
auto n_array = particles_size();
__m128 vx, vy, vz;
__m128 t = _mm_set1_ps( dt );
__m128 g = _mm_set1_ps( gravity * dt );
for ( size_t i = 0; i < n_array; i += sse_width ) {
vx = _mm_add_ps( _mm_load_ps( velocity_x.data()+i ), _mm_mul_ps( t, _mm_load_ps( acceleration_x.data()+i )));
vy = _mm_add_ps( _mm_load_ps( velocity_y.data()+i ), _mm_mul_ps( t, _mm_load_ps( acceleration_y.data()+i )));
vz = _mm_add_ps( _mm_load_ps( velocity_z.data()+i ), _mm_mul_ps( t, _mm_load_ps( acceleration_z.data()+i )));
vy = _mm_add_ps( vy, g );
_mm_store_ps( position_x.data()+i, _mm_add_ps( _mm_load_ps( position_x.data()+i ), _mm_mul_ps( vx, t )));
_mm_store_ps( position_y.data()+i, _mm_add_ps( _mm_load_ps( position_y.data()+i ), _mm_mul_ps( vy, t )));
_mm_store_ps( position_z.data()+i, _mm_add_ps( _mm_load_ps( position_z.data()+i ), _mm_mul_ps( vz, t )));
_mm_store_ps( velocity_x.data()+i, vx );
_mm_store_ps( velocity_y.data()+i, vy );
_mm_store_ps( velocity_z.data()+i, vz );
}
__m128 zero = _mm_setzero_ps();
auto* block_ptr = alive.data();
using block_t = std::remove_reference<decltype(*block_ptr)>::type;
auto e_ptr = energy.data();
/*
* alive update loop:
*/
#ifdef HAVE_GOON_BIT_VECTOR
for ( size_t i = 0; i < n_array; ) {
block_t block_v = 0;
do {
auto e_i = e_ptr + i;
_mm_store_ps( e_i, _mm_sub_ps( _mm_load_ps( e_i ), t ));
block_v |=
block_t
( _mm_movemask_ps( _mm_cmple_ps( _mm_load_ps( e_i ), zero )))
<< (i % bits_per_block)
;
i += sse_width;
} while ( i % bits_per_block != 0 );
*block_ptr++ = block_v;
}
#else
auto* block_end = alive.end();//absent in orig. code
for
( size_t i_array //was i in orig. code
= 0
; ( i_array < n_array //requires n_array%sse_width==0 to work correctly.
&& block_ptr<block_end//absent in orig. code
)
; ++block_ptr
//Absent in orig. code.
//Instead, there was *block_ptr++ = block_v
//just after the, in orig. coe, do...while statement.
)
{