Case Studies - N-Body Solvers - Tree Search - Openmp and Mpi Implementations and Comparison
Case Studies - N-Body Solvers - Tree Search - Openmp and Mpi Implementations and Comparison
Problem Formulation
To determine the positions and velocities.
Based on
Newton’s second law of motion
Newton’s law of universal gravitation
Newton’s second law of motion
1
The acceleration of an object is dependent upon two variables –
1. the net force acting upon the object
2. the mass of the object
The acceleration of an object depends directly upon the net force acting upon the object,
and inversely upon the mass of the object.
Newton’s law of universal gravitation
A particle attracts every other particle in the universe using a force that is directly
proportional to the product of their masses and inversely proportional to the square of the
distance between them.
Suppose we have n=2 particles (q and k) with
Masses - mq and mk
Positions - sq(t) and sk(t) at a time t
The force on particle q exerted by particle k is given by
The total force on any particle is calculated by adding all the forces due to all the particles.
If our n particles are numbered 0, 1, 2, … , n -1, then the total force on particle q is given by
2
Thus Newton’s laws give us a system of differential equations — equations involving
derivatives.
Our job is to find at each time t , the position and the velocity of the particle.
3
A Reduced Algorithm for Computing N-Body Forces
Euler’s Method
4
PARALLELIZING THE N-BODY SOLVERS USING OPENMP
Apply Foster’s methodology.
Initially, we want a lot of tasks.
Start by making our tasks the computations of the positions, the velocities, and the total
forces at each timestep.
Communications Among Tasks in the Basic N-Body Solver
5
PARALLELIZING THE BASIC SOLVER USING OPENMP
6
Communication In A Possible MPI Implementation of the N-Body Solver
7
Run-Times for OpenMP and MPI Versions of N-Body Solvers
TREE SEARCH
Many problems can be solved using a tree search. As a simple example, consider the traveling
salesperson problem, or TSP. In TSP, a salesperson is given a list of cities. The salesman needs
to visit and a cost for traveling between each pair of cities. The problem is to visit each city once,
returning to the starting city, with the least possible cost. Thus, the TSP is to find a minimum-
cost tour.
TSP is what’s known as an NP-complete problem. This means that there is no algorithm known
for solving it that, in all cases, is significantly better than exhaustive search. Exhaustive search
means examining all possible solutions to the problem and choosing the best. The number of
possible solutions to TSP grows exponentially as the number of cities is increased.
For example, if we add one additional city to an n-city problem, we’ll increase the number of
possible solutions by a factor of n - 1. Thus, although there are only six possible solutions to a
four-city problem, there are 4*6 = 24 to a five-city problem, 5*24 = 120 to a six-city problem,
6*120 =720 to a seven-city problem, and so on.
8
Solution :
Start at the origin, here city 0
Do the depth-first search
Maintain the current best tour, that is minimum cost
If a node is reached with cost larger than current minimum cost, do not go deeper
In the example, we’ll start at the root, and branch left until we reach the leaf Labeled
Then we back up to the tree node labeled 0→ 1, since it is the deepest ancestor node
with unvisited children, and we’ll branch down to get to the leaf labeled
Continuing, we’ll back up to the root and branch down to the node labeled 0→2.
When we visit its child, labeled
we’ll go no further in this subtree, since we’ve already found a complete tour with cost less than
21.We’ll back up to 0→2 and branch down to its remaining unvisited child. Continuing in this
fashion, we eventually find the least-cost tour.
Algorithm
Cities are numbered 0, 1, . . . , n − 1
A tour contains number of cities, the cities in the tour, and the cost of it
9
Number of cities is citycount (tour)
Initially, tour contains the first city 0 and cost 0
Besttour(tour) checks if this is the best tour so far
Ipdatebesttour(tour) updates the best tour
Feasible (tour, city ) checks if city has been visited, and if not, if it can be added to tour
so that cost up to city < cost( best tour )
Add(tour, city ) adds city to tour; city must be feasible
Removelast(tour, city ) removes last city from tour
Mapping
Assume p processes
One process could run until there are p tours in the stack
Assign them to processes
Best Tour
Processes work independently until each finds it local best tour
Do global reduction on process 0 to find the best tour Simple, but a process may search
through partial tours that cannot lead to global best tour
Dynamic Mapping
When a process runs out of work, get more work
Each stack entry is partial tour
10
A process can get a partial tour and work on it
The order in which nodes are visited does not matter
When a single thread executes some code, we use the OpenMP directive
# pragma omp single
This will insure that the following structured block of code will be executed by one thread in the
team, and the other threads in the team will wait in an implicit barrier at the end of the block
until the executing thread is finished.
We can use MPI_Iprobe to check if a message from src with tag in communicator comm
is available
If such is available ∗msg is 1 and status−>MPI SOURCE contains the source; otherwise
∗msg is 0
11
To check if there is a message from any source
MPI_Iprobe(MPI ANY SOURCE,NEW COST TAG, &msg, &status)
12