Top Banner
N-body Simulation The HOW Series Day 5, Rev. 1b N-body Simulation © Colfax International, 2013–2015
23

Colfax HOW Session 05 rev 01b - CSUcs560/Fall2015/Lectures/Colfax... · 2015. 9. 6. · Colfax_HOW_Session_05_rev_01b.pdf Author: Andrey Vladimirov, Vadim Karpusenko, Ryo Asai Created

Oct 03, 2020

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Colfax HOW Session 05 rev 01b - CSUcs560/Fall2015/Lectures/Colfax... · 2015. 9. 6. · Colfax_HOW_Session_05_rev_01b.pdf Author: Andrey Vladimirov, Vadim Karpusenko, Ryo Asai Created

N-body Simulation

The HOW Series Day 5, Rev. 1b N-body Simulation © Colfax International, 2013–2015

Page 2: Colfax HOW Session 05 rev 01b - CSUcs560/Fall2015/Lectures/Colfax... · 2015. 9. 6. · Colfax_HOW_Session_05_rev_01b.pdf Author: Andrey Vladimirov, Vadim Karpusenko, Ryo Asai Created

Physics

The HOW Series Day 5, Rev. 1b N-body Simulation © Colfax International, 2013–2015

Page 3: Colfax HOW Session 05 rev 01b - CSUcs560/Fall2015/Lectures/Colfax... · 2015. 9. 6. · Colfax_HOW_Session_05_rev_01b.pdf Author: Andrey Vladimirov, Vadim Karpusenko, Ryo Asai Created

Application

1 Astrophysics:Ï planetary systemsÏ galaxiesÏ cosmological structures

2 Electrostatic systems:Ï moleculesÏ crystals

This work: “toy model” with all-to-allO

(n2

)algorithm. Practical N-body sim-

ulations may use tree algorithms withO

(n logn

)complexity.

Source: APOD, credit: Debra MeloyElmegreen (Vassar College) et al., & the

Hubble Heritage Team (AURA/ STScI/ NASA)

The HOW Series Day 5, Rev. 1b N-body Simulation © Colfax International, 2013–2015

Page 4: Colfax HOW Session 05 rev 01b - CSUcs560/Fall2015/Lectures/Colfax... · 2015. 9. 6. · Colfax_HOW_Session_05_rev_01b.pdf Author: Andrey Vladimirov, Vadim Karpusenko, Ryo Asai Created

Comparative Benchmarks and System Configuration

http://xeonphi.com/workstationsThe HOW Series Day 5, Rev. 1b N-body Simulation © Colfax International, 2013–2015

Page 5: Colfax HOW Session 05 rev 01b - CSUcs560/Fall2015/Lectures/Colfax... · 2015. 9. 6. · Colfax_HOW_Session_05_rev_01b.pdf Author: Andrey Vladimirov, Vadim Karpusenko, Ryo Asai Created

Initial Implementation of the N-Body Simulation

The HOW Series Day 5, Rev. 1b Initial Implementation of the N-Body Simulation © Colfax International, 2013–2015

Page 6: Colfax HOW Session 05 rev 01b - CSUcs560/Fall2015/Lectures/Colfax... · 2015. 9. 6. · Colfax_HOW_Session_05_rev_01b.pdf Author: Andrey Vladimirov, Vadim Karpusenko, Ryo Asai Created

Illustration of “Toy Model” Calculation Pattern

All-to-all interaction

O(n2

)complexity

All particles fit in memory ofeach compute node

No multipole approximation,tree algorithms, Debyescreening, etc.

Basis for more efficient real-lifemodels

Good educational example

The HOW Series Day 5, Rev. 1b Initial Implementation of the N-Body Simulation © Colfax International, 2013–2015

Page 7: Colfax HOW Session 05 rev 01b - CSUcs560/Fall2015/Lectures/Colfax... · 2015. 9. 6. · Colfax_HOW_Session_05_rev_01b.pdf Author: Andrey Vladimirov, Vadim Karpusenko, Ryo Asai Created

All-to-All Approach (O(n2

)Complexity Scaling)

Each particle is stored as a structure:1 struct ParticleType {2 float x, y, z;3 float vx, vy, vz;4 };

main() allocates an array of ParticleType:

1 ParticleType* particle = new ParticleType[nParticles];

Particle propagation step is timed:

1 const double tStart = omp_get_wtime(); // Start timing2 MoveParticles(nParticles, particle, dt);3 const double tEnd = omp_get_wtime(); // End timing

The HOW Series Day 5, Rev. 1b Initial Implementation of the N-Body Simulation © Colfax International, 2013–2015

Page 8: Colfax HOW Session 05 rev 01b - CSUcs560/Fall2015/Lectures/Colfax... · 2015. 9. 6. · Colfax_HOW_Session_05_rev_01b.pdf Author: Andrey Vladimirov, Vadim Karpusenko, Ryo Asai Created

Particle Update Engine1 void MoveParticles(int nParticles, ParticleType* particle, float dt) {2 for (int i = 0; i < nParticles; i++) { // Particles that experience force3 float Fx = 0, Fy = 0, Fz = 0; // Gravity force on particle i4 for (int j = 0; j < nParticles; j++) { // Particles that exert force5 // Newton’s law of universal gravity6 const float dx = particle[j].x - particle[i].x;7 const float dy = particle[j].y - particle[i].y;8 const float dz = particle[j].z - particle[i].z;9 const float drSquared = dx*dx + dy*dy + dz*dz + 1e-20;

10 const float drPower32 = pow(drSquared, 3.0/2.0);11 // Calculate the net force12 Fx += dx/drPower32; Fy += dy/drPower32; Fz += dz/drPower32;13 }14 // Accelerate particles in response to the gravitational force15 particle[i].vx+=dt*Fx; particle[i].vy+=dt*Fy; particle[i].vz+=dt*Fz;16 }17 ...

The HOW Series Day 5, Rev. 1b Initial Implementation of the N-Body Simulation © Colfax International, 2013–2015

Page 9: Colfax HOW Session 05 rev 01b - CSUcs560/Fall2015/Lectures/Colfax... · 2015. 9. 6. · Colfax_HOW_Session_05_rev_01b.pdf Author: Andrey Vladimirov, Vadim Karpusenko, Ryo Asai Created

Performance of Initial Implementation

Initial 0

5

10

15

20 S

ingl

e Pr

ecis

ion

GFL

OP/

s

5.3

0.8

N-Body Simulation Performance

Processor: Intel Xeon E5-2697 v2 Coprocessor: Intel Xeon Phi 7120P

The HOW Series Day 5, Rev. 1b Initial Implementation of the N-Body Simulation © Colfax International, 2013–2015

Page 10: Colfax HOW Session 05 rev 01b - CSUcs560/Fall2015/Lectures/Colfax... · 2015. 9. 6. · Colfax_HOW_Session_05_rev_01b.pdf Author: Andrey Vladimirov, Vadim Karpusenko, Ryo Asai Created

Optimization: Thread Parallelism

The HOW Series Day 5, Rev. 1b Optimization: Thread Parallelism © Colfax International, 2013–2015

Page 11: Colfax HOW Session 05 rev 01b - CSUcs560/Fall2015/Lectures/Colfax... · 2015. 9. 6. · Colfax_HOW_Session_05_rev_01b.pdf Author: Andrey Vladimirov, Vadim Karpusenko, Ryo Asai Created

Incorporating Thread Parallelism

Before:

1 for (int i = 0; i < nParticles; i++) { // Particles that experience force2 float Fx = 0, Fy = 0, Fz = 0; // Gravity force on particle i3 for (int j = 0; j < nParticles; j++) { // Particles that exert force4 // Newton’s law of universal gravity5 ...

After:

1 #pragma omp parallel for2 for (int i = 0; i < nParticles; i++) { // Particles that experience force3 float Fx = 0, Fy = 0, Fz = 0; // Gravity force on particle i4 for (int j = 0; j < nParticles; j++) { // Particles that exert force5 // Newton’s law of universal gravity6 ...

The HOW Series Day 5, Rev. 1b Optimization: Thread Parallelism © Colfax International, 2013–2015

Page 12: Colfax HOW Session 05 rev 01b - CSUcs560/Fall2015/Lectures/Colfax... · 2015. 9. 6. · Colfax_HOW_Session_05_rev_01b.pdf Author: Andrey Vladimirov, Vadim Karpusenko, Ryo Asai Created

Performance with Thread Parallelism

Initial Multi-threaded

0

50

100

150

200

250

300 S

ingl

e Pr

ecis

ion

GFL

OP/

s

5.3

140

0.8

120

N-Body Simulation Performance

Processor: Intel Xeon E5-2697 v2 Coprocessor: Intel Xeon Phi 7120P

The HOW Series Day 5, Rev. 1b Optimization: Thread Parallelism © Colfax International, 2013–2015

Page 13: Colfax HOW Session 05 rev 01b - CSUcs560/Fall2015/Lectures/Colfax... · 2015. 9. 6. · Colfax_HOW_Session_05_rev_01b.pdf Author: Andrey Vladimirov, Vadim Karpusenko, Ryo Asai Created

Optimization: Vectorization

The HOW Series Day 5, Rev. 1b Optimization: Vectorization © Colfax International, 2013–2015

Page 14: Colfax HOW Session 05 rev 01b - CSUcs560/Fall2015/Lectures/Colfax... · 2015. 9. 6. · Colfax_HOW_Session_05_rev_01b.pdf Author: Andrey Vladimirov, Vadim Karpusenko, Ryo Asai Created

Vectorizing with Unit-Stride Memory AccessBefore:

1 struct ParticleType {2 float x, y, z, vx, vy, vz;3 }; // ...4 const float dx = particle[j].x - particle[i].x;5 const float dy = particle[j].y - particle[i].y;6 const float dz = particle[j].z - particle[i].z;

After:

1 struct ParticleSet {2 float *x, *y, *z, *vx, *vy, *vz;3 }; // ...4 const float dx = particle.x[j] - particle.x[i];5 const float dy = particle.y[j] - particle.y[i];6 const float dz = particle.z[j] - particle.z[i];

The HOW Series Day 5, Rev. 1b Optimization: Vectorization © Colfax International, 2013–2015

Page 15: Colfax HOW Session 05 rev 01b - CSUcs560/Fall2015/Lectures/Colfax... · 2015. 9. 6. · Colfax_HOW_Session_05_rev_01b.pdf Author: Andrey Vladimirov, Vadim Karpusenko, Ryo Asai Created

Why AoS to SoA Conversion Helps: Unit Stride

The HOW Series Day 5, Rev. 1b Optimization: Vectorization © Colfax International, 2013–2015

Page 16: Colfax HOW Session 05 rev 01b - CSUcs560/Fall2015/Lectures/Colfax... · 2015. 9. 6. · Colfax_HOW_Session_05_rev_01b.pdf Author: Andrey Vladimirov, Vadim Karpusenko, Ryo Asai Created

Performance with Improved Vectorization

Initial Multi-threaded

Vectorizedwith SoA

0

50

100

150

200

250

300 S

ingl

e Pr

ecis

ion

GFL

OP/

s

5.3

140

180

0.8

120

220

N-Body Simulation Performance

Processor: Intel Xeon E5-2697 v2 Coprocessor: Intel Xeon Phi 7120P

The HOW Series Day 5, Rev. 1b Optimization: Vectorization © Colfax International, 2013–2015

Page 17: Colfax HOW Session 05 rev 01b - CSUcs560/Fall2015/Lectures/Colfax... · 2015. 9. 6. · Colfax_HOW_Session_05_rev_01b.pdf Author: Andrey Vladimirov, Vadim Karpusenko, Ryo Asai Created

Optimization: Scalar Tuning

The HOW Series Day 5, Rev. 1b Optimization: Scalar Tuning © Colfax International, 2013–2015

Page 18: Colfax HOW Session 05 rev 01b - CSUcs560/Fall2015/Lectures/Colfax... · 2015. 9. 6. · Colfax_HOW_Session_05_rev_01b.pdf Author: Andrey Vladimirov, Vadim Karpusenko, Ryo Asai Created

Improving Scalar ExpressionsBefore:

1 const float drSquared = dx*dx + dy*dy + dz*dz + 1e-20;2 const float drPower32 = pow(drSquared, 3.0/2.0);3 // Calculate the net force4 Fx += dx/drPower32; Fy += dy/drPower32; Fz += dz/drPower32;

After:

1 const float drRecip = 1.0f/sqrtf(dx*dx + dy*dy + dz*dz + 1e-20f);2 const float drPowerN32 = drRecip*drRecip*drRecip;3 // Calculate the net force4 Fx += dx*drPowerN32; Fy += dy*drPowerN32; Fz += dz*drPowerN32;

Strength reduction (division → multiplication by reciprocal)

Precision control (suffix -f on single-precision constants and functions)

Reliance on hardware-supported reciprocal square root

The HOW Series Day 5, Rev. 1b Optimization: Scalar Tuning © Colfax International, 2013–2015

Page 19: Colfax HOW Session 05 rev 01b - CSUcs560/Fall2015/Lectures/Colfax... · 2015. 9. 6. · Colfax_HOW_Session_05_rev_01b.pdf Author: Andrey Vladimirov, Vadim Karpusenko, Ryo Asai Created

Compilation with Relaxed Precision

For the CPU architecture (Intel Xeon E5-2697 v2 processor):vega@lyra% # Compile with relaxed precision: (-fp-model fast=2)vega@lyra% icpc -o nbody-CPU -qopenmp -fp-model fast=2 nbody.ccvega@lyra% export KMP_AFFINITY=compactvega@lyra% ./nbody-CPU

For the MIC architecture (Intel Xeon Phi 7120P coprocessor):vega@lyra% # Compile for Xeon Phi with relaxed precision: (-fp-model fast=2)vega@lyra% icpc -o nbody-MIC -mmic -qopenmp -fp-model fast=2 nbody.ccvega@lyra% export KMP_AFFINITY=compactvega@lyra% export SINK_LD_LIBRARY_PATH=$MIC_LD_LIBRARY_PATHvega@lyra% micnativeloadex ./nbody-MIC

The HOW Series Day 5, Rev. 1b Optimization: Scalar Tuning © Colfax International, 2013–2015

Page 20: Colfax HOW Session 05 rev 01b - CSUcs560/Fall2015/Lectures/Colfax... · 2015. 9. 6. · Colfax_HOW_Session_05_rev_01b.pdf Author: Andrey Vladimirov, Vadim Karpusenko, Ryo Asai Created

Performance after Scalar Tuning

Initial Multi-threaded

Vectorizedwith SoA

ScalarTuning

0

200

400

600

800

1000 S

ingl

e Pr

ecis

ion

GFL

OP/

s

5.3

140 180

480

0.8120

220

870

N-Body Simulation Performance

Processor: Intel Xeon E5-2697 v2 Coprocessor: Intel Xeon Phi 7120P

The HOW Series Day 5, Rev. 1b Optimization: Scalar Tuning © Colfax International, 2013–2015

Page 21: Colfax HOW Session 05 rev 01b - CSUcs560/Fall2015/Lectures/Colfax... · 2015. 9. 6. · Colfax_HOW_Session_05_rev_01b.pdf Author: Andrey Vladimirov, Vadim Karpusenko, Ryo Asai Created

Optimization: Memory Traffic

The HOW Series Day 5, Rev. 1b Optimization: Memory Traffic © Colfax International, 2013–2015

Page 22: Colfax HOW Session 05 rev 01b - CSUcs560/Fall2015/Lectures/Colfax... · 2015. 9. 6. · Colfax_HOW_Session_05_rev_01b.pdf Author: Andrey Vladimirov, Vadim Karpusenko, Ryo Asai Created

Improving Cache TrafficBefore:

1 for (int i = 0; i < nParticles; i++) { // Particles that experience force2 float Fx = 0, Fy = 0, Fz = 0; // Gravity force on particle i3 for (int j = 0; j < nParticles; j++) { // Particles that exert force4 // ...5 Fx += dx*drPowerN32; Fy += dy*drPowerN32; Fz += dz*drPowerN32;

After: (tileSize = 16)

1 for (int ii = 0; ii < nParticles; ii += tileSize) { // Particle blocks2 float Fx[tileSize], Fy[tileSize], Fz[tileSize]; // Force on particle block3 Fx[:] = Fy[:] = Fz[:] = 0;4 #pragma unroll(tileSize)5 for (int j = 0; j < nParticles; j++) { // Particles that exert force6 for (int i = ii; i < ii + tileSize; i++) { // Traverse the block7 // ...8 Fx[i-ii] += dx*drPowerN32; Fy[i-ii] += dy*drPowerN32; Fz[i-ii] += dz*drPowerN32;

The HOW Series Day 5, Rev. 1b Optimization: Memory Traffic © Colfax International, 2013–2015

Page 23: Colfax HOW Session 05 rev 01b - CSUcs560/Fall2015/Lectures/Colfax... · 2015. 9. 6. · Colfax_HOW_Session_05_rev_01b.pdf Author: Andrey Vladimirov, Vadim Karpusenko, Ryo Asai Created

Performance with Cache Optimization (Loop Tiling)

Initial Multi-threaded

Vectorizedwith SoA

ScalarTuning

Tiled,Unrolled

0

500

1000

1500

2000 S

ingl

e Pr

ecis

ion

GFL

OP/

s

5.3140 180

480 520

0.8120

220

870

1620

N-Body Simulation Performance

Processor: Intel Xeon E5-2697 v2 Coprocessor: Intel Xeon Phi 7120P

The HOW Series Day 5, Rev. 1b Optimization: Memory Traffic © Colfax International, 2013–2015