-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathnbody.cc
284 lines (251 loc) · 12.1 KB
/
nbody.cc
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
#include <cmath>
#include <cstdio>
#include <random>
#include <memory>
#include <numeric>
#include <random>
#include "timer.h"
#include <VecCore/VecCore>
using namespace vecCore;
constexpr auto kNruns = 1;
constexpr auto kN = 1024;
#ifdef VECCORE_TIMER_CYCLES
using time_unit = cycles;
constexpr auto time_unit_name = "cycles";
#else
using time_unit = nanoseconds;
constexpr auto time_unit_name = "ns";
#endif
constexpr float eps2 = 0.01f;
constexpr float timeStep = 0.0001f;
inline void pPInteraction(float p1posx, float p1posy, float p1posz, float& p1velx, float& p1vely, float& p1velz,
float p2posx, float p2posy, float p2posz, float p2mass) {
const float xdistance = p1posx - p2posx;
const float ydistance = p1posy - p2posy;
const float zdistance = p1posz - p2posz;
const float xdistanceSqr = xdistance * xdistance;
const float ydistanceSqr = ydistance * ydistance;
const float zdistanceSqr = zdistance * zdistance;
const float distSqr = eps2 + xdistanceSqr + ydistanceSqr + zdistanceSqr;
const float distSixth = distSqr * distSqr * distSqr;
const float invDistCube = 1.0f / std::sqrt(distSixth);
const float sts = p2mass * invDistCube * timeStep;
p1velx += xdistanceSqr * sts;
p1vely += ydistanceSqr * sts;
p1velz += zdistanceSqr * sts;
}
VECCORE_FORCE_NOINLINE
void TestNBody(float* __restrict__ posx, float* __restrict__ posy, float* __restrict__ posz,
float* __restrict__ velx, float* __restrict__ vely, float* __restrict__ velz,
float* __restrict__ mass, size_t kN)
{
Timer<time_unit> timer;
unsigned long long t[kNruns];
for (size_t n = 0; n < kNruns; n++) {
timer.Start();
for (std::size_t i = 0; i < kN; i++) {
const float piposx = posx[i];
const float piposy = posy[i];
const float piposz = posz[i];
float pivelx = velx[i];
float pively = vely[i];
float pivelz = velz[i];
for (std::size_t j = 0; j < kN; j++)
pPInteraction(piposx, piposy, piposz, pivelx, pively, pivelz,
posx[j], posy[j], posz[j], mass[j]);
velx[i] = pivelx;
vely[i] = pively;
velz[i] = pivelz;
}
for (std::size_t i = 0; i < kN; i++) {
posx[i] += velx[i] * timeStep;
posy[i] += vely[i] * timeStep;
posz[i] += velz[i] * timeStep;
}
t[n] = timer.Elapsed();
}
const unsigned long long mean = std::accumulate(std::begin(t), std::end(t), 0ull) / (kN * kNruns);
printf("%20s %6llu\n", "Scalar", mean);
}
#if defined(__AVX2__) && defined(__FMA__)
const __m256 vEps2 = _mm256_set1_ps(eps2);
const __m256 vTimestep = _mm256_broadcast_ss(&timeStep);
inline void pPInteractionAVX2(
__m256 p1posx,
__m256 p1posy,
__m256 p1posz,
__m256& p1velx,
__m256& p1vely,
__m256& p1velz,
__m256 p2posx,
__m256 p2posy,
__m256 p2posz,
__m256 p2mass)
{
const __m256 xdistance = _mm256_sub_ps(p1posx, p2posx);
const __m256 ydistance = _mm256_sub_ps(p1posy, p2posy);
const __m256 zdistance = _mm256_sub_ps(p1posz, p2posz);
const __m256 xdistanceSqr = _mm256_mul_ps(xdistance, xdistance);
const __m256 ydistanceSqr = _mm256_mul_ps(ydistance, ydistance);
const __m256 zdistanceSqr = _mm256_mul_ps(zdistance, zdistance);
const __m256 distSqr = _mm256_add_ps(_mm256_add_ps(_mm256_add_ps(vEps2, xdistanceSqr), ydistanceSqr), zdistanceSqr);
const __m256 distSixth = _mm256_mul_ps(_mm256_mul_ps(distSqr, distSqr), distSqr);
const __m256 invDistCube = _mm256_div_ps(_mm256_set1_ps(1.0f), _mm256_sqrt_ps(distSixth));
const __m256 sts = _mm256_mul_ps(_mm256_mul_ps(p2mass, invDistCube), vTimestep);
p1velx = _mm256_fmadd_ps(xdistanceSqr, sts, p1velx);
p1vely = _mm256_fmadd_ps(ydistanceSqr, sts, p1vely);
p1velz = _mm256_fmadd_ps(zdistanceSqr, sts, p1velz);
}
constexpr auto AVX2FloatVectorSize = 8;
VECCORE_FORCE_NOINLINE
void TestNBodyAVX2(float* __restrict__ posx, float* __restrict__ posy, float* __restrict__ posz,
float* __restrict__ velx, float* __restrict__ vely, float* __restrict__ velz,
float* __restrict__ mass, size_t kN)
{
Timer<time_unit> timer;
unsigned long long t[kNruns];
for (size_t n = 0; n < kNruns; n++) {
timer.Start();
for (std::size_t i = 0; i < kN; i += AVX2FloatVectorSize) {
const __m256 piposx = _mm256_load_ps(&posx[i]);
const __m256 piposy = _mm256_load_ps(&posy[i]);
const __m256 piposz = _mm256_load_ps(&posz[i]);
__m256 pivelx = _mm256_load_ps(&velx[i]);
__m256 pively = _mm256_load_ps(&vely[i]);
__m256 pivelz = _mm256_load_ps(&velz[i]);
for (std::size_t j = 0; j < kN; j++) {
pPInteractionAVX2(
piposx,
piposy,
piposz,
pivelx,
pively,
pivelz,
_mm256_broadcast_ss(&posx[j]),
_mm256_broadcast_ss(&posy[j]),
_mm256_broadcast_ss(&posz[j]),
_mm256_broadcast_ss(&mass[j])
);
}
_mm256_store_ps(&velx[i], pivelx);
_mm256_store_ps(&vely[i], pively);
_mm256_store_ps(&velz[i], pivelz);
}
for (std::size_t i = 0; i < kN; i += AVX2FloatVectorSize) {
_mm256_store_ps(&posx[i], _mm256_fmadd_ps(_mm256_load_ps(&velx[i]), vTimestep, _mm256_load_ps(&posx[i])));
_mm256_store_ps(&posy[i], _mm256_fmadd_ps(_mm256_load_ps(&vely[i]), vTimestep, _mm256_load_ps(&posy[i])));
_mm256_store_ps(&posz[i], _mm256_fmadd_ps(_mm256_load_ps(&velz[i]), vTimestep, _mm256_load_ps(&posz[i])));
}
t[n] = timer.Elapsed();
}
const unsigned long long mean = std::accumulate(std::begin(t), std::end(t), 0ull) / (kN * kNruns);
printf("%20s %6llu\n", "AVX2 Intrinsics", mean);
}
#endif
template <typename Vec>
inline void pPInteractionSIMD(Vec p1posx, Vec p1posy, Vec p1posz, Vec& p1velx, Vec& p1vely, Vec& p1velz,
Vec p2posx, Vec p2posy, Vec p2posz, Vec p2mass)
{
const Vec xdistance = p1posx - p2posx;
const Vec ydistance = p1posy - p2posy;
const Vec zdistance = p1posz - p2posz;
const Vec xdistanceSqr = xdistance * xdistance;
const Vec ydistanceSqr = ydistance * ydistance;
const Vec zdistanceSqr = zdistance * zdistance;
const Vec distSqr = eps2 + xdistanceSqr + ydistanceSqr + zdistanceSqr;
const Vec distSixth = distSqr * distSqr * distSqr;
const Vec invDistCube = 1.0f / math::Sqrt(distSixth);
const Vec sts = p2mass * invDistCube * timeStep;
p1velx += xdistanceSqr * sts;
p1vely += ydistanceSqr * sts;
p1velz += zdistanceSqr * sts;
}
template <typename Vec>
VECCORE_FORCE_NOINLINE
void TestNBodySIMD(float* __restrict__ posx, float* __restrict__ posy, float* __restrict__ posz,
float* __restrict__ velx, float* __restrict__ vely, float* __restrict__ velz,
float* __restrict__ mass, size_t kN, const char* name)
{
Timer<time_unit> timer;
unsigned long long t[kNruns];
for (size_t n = 0; n < kNruns; n++) {
timer.Start();
for (std::size_t i = 0; i < kN; i += VectorSize<Vec>()) {
const Vec piposx = reinterpret_cast<Vec&>(posx[i]);
const Vec piposy = reinterpret_cast<Vec&>(posy[i]);
const Vec piposz = reinterpret_cast<Vec&>(posz[i]);
Vec pivelx = reinterpret_cast<Vec&>(velx[i]);
Vec pively = reinterpret_cast<Vec&>(vely[i]);
Vec pivelz = reinterpret_cast<Vec&>(velz[i]);
for (std::size_t j = 0; j < kN; j++ ) {
pPInteractionSIMD(piposx, piposy, piposz, pivelx, pively, pivelz,
Vec(posx[j]), Vec(posy[j]), Vec(posz[j]), Vec(mass[j]));
}
reinterpret_cast<Vec&>(velx[i]) = pivelx;
reinterpret_cast<Vec&>(vely[i]) = pively;
reinterpret_cast<Vec&>(velz[i]) = pivelz;
}
for (std::size_t i = 0; i < kN; i += VectorSize<Vec>()) {
reinterpret_cast<Vec&>(posx[i]) += reinterpret_cast<const Vec&>(velx[i]) * timeStep;
reinterpret_cast<Vec&>(posy[i]) += reinterpret_cast<const Vec&>(vely[i]) * timeStep;
reinterpret_cast<Vec&>(posz[i]) += reinterpret_cast<const Vec&>(velz[i]) * timeStep;
}
t[n] = timer.Elapsed();
}
const unsigned long long mean = std::accumulate(std::begin(t), std::end(t), 0ull) / (kN * kNruns);
printf("%20s %6llu\n", name, mean);
}
int main()
{
auto alloc = [] {
return std::unique_ptr<float[], decltype(&AlignedFree)>{
static_cast<float*>(AlignedAlloc(VECCORE_SIMD_ALIGN, kN * sizeof(float))), &AlignedFree
};
};
auto posx = alloc();
auto posy = alloc();
auto posz = alloc();
auto velx = alloc();
auto vely = alloc();
auto velz = alloc();
auto mass = alloc();
std::random_device rng;
std::default_random_engine engine{rng()};
std::normal_distribution<float> dist{0.0f, 1.0f};
for (std::size_t i = 0; i < kN; ++i) {
posx[i] = dist(engine);
posy[i] = dist(engine);
posz[i] = dist(engine);
velx[i] = dist(engine) / 10.0f;
vely[i] = dist(engine) / 10.0f;
velz[i] = dist(engine) / 10.0f;
mass[i] = dist(engine) / 100.0f;
}
printf(" Backend / Mean (%s)\n", time_unit_name);
TestNBody(posx.get(), posy.get(), posz.get(), velx.get(), vely.get(), velz.get(), mass.get(), kN);
#if defined(__AVX2__) && defined(__FMA__)
TestNBodyAVX2(posx.get(), posy.get(), posz.get(), velx.get(), vely.get(), velz.get(), mass.get(), kN);
#endif
TestNBodySIMD<backend::Scalar::Float_v>(posx.get(), posy.get(), posz.get(), velx.get(), vely.get(), velz.get(), mass.get(), kN, "Scalar Backend");
TestNBodySIMD<backend::ScalarWrapper::Float_v>(posx.get(), posy.get(), posz.get(), velx.get(), vely.get(), velz.get(), mass.get(), kN, "ScalarWrapper");
#ifdef VECCORE_ENABLE_VC
TestNBodySIMD<backend::VcScalar::Float_v>(posx.get(), posy.get(), posz.get(), velx.get(), vely.get(), velz.get(), mass.get(), kN, "VcScalar");
TestNBodySIMD<backend::VcVector::Float_v>(posx.get(), posy.get(), posz.get(), velx.get(), vely.get(), velz.get(), mass.get(), kN, "VcVector");
TestNBodySIMD<backend::VcSimdArray<8>::Float_v>(posx.get(), posy.get(), posz.get(), velx.get(), vely.get(), velz.get(), mass.get(), kN, "VcSimdArray<8>");
TestNBodySIMD<backend::VcSimdArray<16>::Float_v>(posx.get(), posy.get(), posz.get(), velx.get(), vely.get(), velz.get(), mass.get(), kN, "VcSimdArray<16>");
TestNBodySIMD<backend::VcSimdArray<32>::Float_v>(posx.get(), posy.get(), posz.get(), velx.get(), vely.get(), velz.get(), mass.get(), kN, "VcSimdArray<32>");
#endif
#ifdef VECCORE_ENABLE_UMESIMD
TestNBodySIMD<backend::UMESimd::Float_v>(posx.get(), posy.get(), posz.get(), velx.get(), vely.get(), velz.get(), mass.get(), kN, "UME::SIMD");
TestNBodySIMD<backend::UMESimdArray<8>::Float_v>(posx.get(), posy.get(), posz.get(), velx.get(), vely.get(), velz.get(), mass.get(), kN, "UME::SIMD<8>");
TestNBodySIMD<backend::UMESimdArray<16>::Float_v>(posx.get(), posy.get(), posz.get(), velx.get(), vely.get(), velz.get(), mass.get(), kN, "UME::SIMD<16>");
TestNBodySIMD<backend::UMESimdArray<32>::Float_v>(posx.get(), posy.get(), posz.get(), velx.get(), vely.get(), velz.get(), mass.get(), kN, "UME::SIMD<32>");
#endif
#ifdef VECCORE_ENABLE_STD_SIMD
TestNBodySIMD<backend::SIMDScalar::Float_v>(posx.get(), posy.get(), posz.get(), velx.get(), vely.get(), velz.get(), mass.get(), kN, "SIMDScalar");
TestNBodySIMD<backend::SIMDVector<4>::Float_v>(posx.get(), posy.get(), posz.get(), velx.get(), vely.get(), velz.get(), mass.get(), kN, "SIMDVector4");
TestNBodySIMD<backend::SIMDVector<8>::Float_v>(posx.get(), posy.get(), posz.get(), velx.get(), vely.get(), velz.get(), mass.get(), kN, "SIMDVector8");
TestNBodySIMD<backend::SIMDNative::Float_v>(posx.get(), posy.get(), posz.get(), velx.get(), vely.get(), velz.get(), mass.get(), kN, "SIMDNative");
#endif
return 0;
}