23
Barnes Hut N-body Simulation Martin Burtscher Fall 2009

Barnes Hut N-body Simulation Martin Burtscher Fall 2009

Embed Size (px)

DESCRIPTION

Barnes Hut Idea  Precise force calculation  Requires O(n 2 ) operations (O(n 2 ) body pairs)  Barnes and Hut (1986)  Algorithm to approximately compute forces  Bodies’ initial position & velocity are also approximate  Requires only O(n log n) operations  Idea is to “combine” far away bodies  Error should be small because force  1/r 2 Barnes Hut N-body Simulation 3

Citation preview

Page 1: Barnes Hut N-body Simulation Martin Burtscher Fall 2009

Barnes HutN-body Simulation

Martin BurtscherFall 2009

Page 2: Barnes Hut N-body Simulation Martin Burtscher Fall 2009

Introduction Physical system simulation (time evolution)

System consists of bodies “n” is the number of bodies Bodies interact via pair-wise forces

Many systems can be modeled in these terms Galaxy clusters (gravitational force) Particles (electric force, magnetic force)

Barnes Hut N-body Simulation2

Page 3: Barnes Hut N-body Simulation Martin Burtscher Fall 2009

Barnes Hut Idea Precise force calculation

Requires O(n2) operations (O(n2) body pairs)

Barnes and Hut (1986) Algorithm to approximately compute forces

Bodies’ initial position & velocity are also approximate Requires only O(n log n) operations Idea is to “combine” far away bodies Error should be small because force 1/r2

Barnes Hut N-body Simulation3

Page 4: Barnes Hut N-body Simulation Martin Burtscher Fall 2009

Barnes Hut Algorithm Set bodies’ initial position and velocity Iterate over time steps

1. Subdivide space until at most one body per cell Record this spatial hierarchy in an octree

2. Compute mass and center of mass of each cell3. Compute force on bodies by traversing octree

Stop traversal path when encountering a leaf (body) or an internal node (cell) that is far enough away

4. Update each body’s position and velocity

Barnes Hut N-body Simulation4

Page 5: Barnes Hut N-body Simulation Martin Burtscher Fall 2009

Build Tree (Level 1)

Barnes Hut N-body Simulation

*

* *

** *

* ** * *

* * *

* **

* * *

* * *

5

o

Subdivide space until at most one body per cell

Page 6: Barnes Hut N-body Simulation Martin Burtscher Fall 2009

Build Tree (Level 2)

Barnes Hut N-body Simulation

*

* *

** *

* ** * *

* * *

* **

* * *

* * *

6

o o o o

o

Subdivide space until at most one body per cell

Page 7: Barnes Hut N-body Simulation Martin Burtscher Fall 2009

Build Tree (Level 3)

Barnes Hut N-body Simulation

*

* *

** *

* ** * *

* * *

* **

* * *

* * *

7

o o o o

o

o o o o o o o o o o o o

Subdivide space until at most one body per cell

Page 8: Barnes Hut N-body Simulation Martin Burtscher Fall 2009

Build Tree (Level 4)

Barnes Hut N-body Simulation

*

* *

** *

* ** * *

* * *

* **

* * *

* * *

8

o o o o

o

o o o o

o o o o o o o o o o o o

o o o o o o o o

Subdivide space until at most one body per cell

Page 9: Barnes Hut N-body Simulation Martin Burtscher Fall 2009

Build Tree (Level 5)

Barnes Hut N-body Simulation

*

* *

** *

* ** * *

* * *

* **

* * *

* * *

9

o o o o

o o o o

o

o o o o

o o o o o o o o o o o o

o o o o o o o o

Subdivide space until at most one body per cell

Page 10: Barnes Hut N-body Simulation Martin Burtscher Fall 2009

Compute Cells’ Center of Mass

Barnes Hut N-body Simulation

*

* *

** *

* ** * *

* * * o

* **

* * o *

* * *

10

o o o o

o o o o

o

o o o o

o o o o o o o o o o o o

o o o o o o o o

For each internal cell, compute sum of mass and weighted averageof position of all bodies in subtree; example shows two cells only

Page 11: Barnes Hut N-body Simulation Martin Burtscher Fall 2009

Compute Forces

Barnes Hut N-body Simulation

*

* *

** *

* ** * *

* * * o

* **

* * o *

* * *

11

o o o o

o o o o

o

o o o o

o o o o o o o o o o o o

o o o o o o o o

Compute force, for example, acting upon green body

Page 12: Barnes Hut N-body Simulation Martin Burtscher Fall 2009

Compute Force (short distance)

Barnes Hut N-body Simulation

*

* *

** *

* ** * *

* * * o

* **

* * o *

* * *

12

o o o o

o o o o

o

o o o o

o o o o o o o o o o o o

o o o o o o o o

Scan tree depth first from left to right; green portion already completed

Page 13: Barnes Hut N-body Simulation Martin Burtscher Fall 2009

Compute Force (down one level)

Barnes Hut N-body Simulation

*

* *

** *

* ** * *

* * * o

* **

* * o *

* * *

13

o o o o

o o o o

o

o o o o

o o o o o o o o o o o o

o o o o o o o o

Red center of mass is too close, need to go down one level

Page 14: Barnes Hut N-body Simulation Martin Burtscher Fall 2009

Compute Force (long distance)

Barnes Hut N-body Simulation

*

* *

** *

* ** * *

* * * o

* **

* * o *

* * *

14

o o o o

o o o o

o

o o o o

o o o o o o o o o o o o

o o o o o o o o

Yellow center of mass is far enough away

Page 15: Barnes Hut N-body Simulation Martin Burtscher Fall 2009

Compute Force (skip subtree)

Barnes Hut N-body Simulation

*

* *

** *

* ** * *

* * * o

* **

* * o *

* * *

15

o o o o

o o o o

o

o o o o

o o o o o o o o o o o o

o o o o o o o o

Therefore, entire subtree rooted in the yellow cell can be skipped

Page 16: Barnes Hut N-body Simulation Martin Burtscher Fall 2009

Pseudocode Set bodySet = ... foreach timestep do { Octree octree = new Octree(); foreach Body b in bodySet { octree.Insert(b); } OrderedList cellList = octree.CellsByLevel(); foreach Cell c in cellList { c.Summarize(); } foreach Body b in bodySet { b.ComputeForce(octree); } foreach Body b in bodySet { b.Advance(); } } Barnes Hut N-body Simulation

16

Page 17: Barnes Hut N-body Simulation Martin Burtscher Fall 2009

Complexity Set bodySet = ... foreach timestep do { // O(n log n) per i Octree octree = new Octree(); foreach Body b in bodySet { // O(n log n) octree.Insert(b); } OrderedList cellList = octree.CellsByLevel(); foreach Cell c in cellList { // O(n) c.Summarize(); } foreach Body b in bodySet { // O(n log n) b.ComputeForce(octree); } foreach Body b in bodySet { // O(n) b.Advance(); } } Barnes Hut N-body Simulation

17

Page 18: Barnes Hut N-body Simulation Martin Burtscher Fall 2009

Parallelism Set bodySet = ... foreach timestep do { // sequential Octree octree = new Octree(); foreach Body b in bodySet { // tree building octree.Insert(b); } OrderedList cellList = octree.CellsByLevel(); foreach Cell c in cellList { // tree traversal c.Summarize(); } foreach Body b in bodySet { // fully parallel b.ComputeForce(octree); } foreach Body b in bodySet { // fully parallel b.Advance(); } } Barnes Hut N-body Simulation

18

Page 19: Barnes Hut N-body Simulation Martin Burtscher Fall 2009

Amorphous Data-Parallelism (1) Top-down tree building

Topology: tree Operator: morph (refinement) Active nodes: new nodes Neighborhoods: active nodes and their parents

(the path leading to the parent is only read) Ordering: unordered Parallelism: increasing from none to a lot

Barnes Hut N-body Simulation19

Page 20: Barnes Hut N-body Simulation Martin Burtscher Fall 2009

Amorphous Data-Parallelism (2) Bottom-up tree summarization

Topology: tree Operator: local computation (structure driven) Active nodes: internal nodes Neighborhoods: active nodes and their children Ordering: ordered (children first, priority is

determined by tree level) Parallelism: decreasing from a lot to none

Barnes Hut N-body Simulation20

Page 21: Barnes Hut N-body Simulation Martin Burtscher Fall 2009

Amorphous Data-Parallelism (3) Force computation

Topology: tree + set Operator: reader + local computation (structure

driven) Active nodes: nodes in set Neighborhoods: active nodes (the tree is only

read) Ordering: unordered Parallelism: full

Barnes Hut N-body Simulation21

Page 22: Barnes Hut N-body Simulation Martin Burtscher Fall 2009

Amorphous Data-Parallelism (4) Advancing bodies

Topology: set Operator: local computation (structure driven) Active nodes: nodes Neighborhoods: active nodes Ordering: unordered Parallelism: full

Barnes Hut N-body Simulation22

Page 23: Barnes Hut N-body Simulation Martin Burtscher Fall 2009

23

Conclusion Barnes Hut is an algorithm for N-body

interactions It exhibits amorphous data parallelism

Contains four parallel ‘foreach’ kernels Three kernels involve operations on a tree

Parallelism dependent on the structure of the tree

Barnes Hut N-body Simulation