Supercomputing in Plain English Collective Communications and N-Body Problems Henry Neeman, Director OU Supercomputing Center for Education & Research.

Slides:



Advertisements
Similar presentations
Its.unc.edu 1 Collective Communication University of North Carolina - Chapel Hill ITS - Research Computing Instructor: Mark Reed
Advertisements

N-Body I CS 170: Computing for the Sciences and Mathematics.
MPI Basics Introduction to Parallel Programming and Cluster Computing University of Washington/Idaho State University MPI Basics Charlie Peck Earlham College.
Introduction to Parallel Programming & Cluster Computing Applications and Types of Parallelism Josh Alexander, University of Oklahoma Ivan Babic, Earlham.
Parallel Programming in C with MPI and OpenMP
MPI Program Structure Self Test with solution. Self Test 1.How would you modify "Hello World" so that only even-numbered processors print the greeting.
MPI_Gatherv CISC372 Fall 2006 Andrew Toy Tom Lynch Bill Meehan.
A Message Passing Standard for MPP and Workstations Communications of the ACM, July 1996 J.J. Dongarra, S.W. Otto, M. Snir, and D.W. Walker.
Collective Communications Self Test with solution.
MPI Collective Communication CS 524 – High-Performance Computing.
MPI Workshop - II Research Staff Week 2 of 3.
Collective Communications
Collective Communication.  Collective communication is defined as communication that involves a group of processes  More restrictive than point to point.
Jonathan Carroll-Nellenback CIRC Summer School MESSAGE PASSING INTERFACE (MPI)
Parallel Programming & Cluster Computing Applications and Types of Parallelism Henry Neeman, University of Oklahoma Charlie Peck, Earlham College Tuesday.
Search Lesson CS1313 Spring Search Lesson Outline 1.Searching Lesson Outline 2.How to Find a Value in an Array? 3.Linear Search 4.Linear Search.
Parallel Programming Using Basic MPI Presented by Timothy H. Kaiser, Ph.D. San Diego Supercomputer Center Presented by Timothy H. Kaiser, Ph.D. San Diego.
L15: Putting it together: N-body (Ch. 6) October 30, 2012.
Supercomputing in Plain English Distributed Multiprocessing Blue Waters Undergraduate Petascale Education Program May 23 – June
Paul Gray, University of Northern Iowa Henry Neeman, University of Oklahoma Charlie Peck, Earlham College Tuesday October University of Oklahoma.
Parallel & Cluster Computing MPI Basics Paul Gray, University of Northern Iowa David Joiner, Shodor Education Foundation Tom Murphy, Contra Costa College.
Parallel Processing1 Parallel Processing (CS 676) Lecture 7: Message Passing using MPI * Jeremy R. Johnson *Parts of this lecture was derived from chapters.
Supercomputing in Plain English Distributed Multiprocessing PRESENTERNAME PRESENTERTITLE PRESENTERDEPARTMENT PRESENTERINSTITUTION DAY MONTH DATE YEAR Your.
Parallel & Cluster Computing MPI Introduction Henry Neeman, Director OU Supercomputing Center for Education & Research University of Oklahoma SC08 Education.
Director of Contra Costa College High Performance Computing Center
1 Collective Communications. 2 Overview  All processes in a group participate in communication, by calling the same function with matching arguments.
ECE 1747H : Parallel Programming Message Passing (MPI)
1 MPI: Message-Passing Interface Chapter 2. 2 MPI - (Message Passing Interface) Message passing library standard (MPI) is developed by group of academics.
Part I MPI from scratch. Part I By: Camilo A. SilvaBIOinformatics Summer 2008 PIRE :: REU :: Cyberbridges.
Parallel Programming & Cluster Computing Applications and Types of Parallelism Henry Neeman, Director OU Supercomputing Center for Education & Research.
Parallel Programming with MPI Prof. Sivarama Dandamudi School of Computer Science Carleton University.
Supercomputing in Plain English Supercomputing in Plain English Parallel Programming for Beginners Henry Neeman, University of Oklahoma Assistant Vice.
Parallel Programming & Cluster Computing Distributed Multiprocessing Henry Neeman, University of Oklahoma Charlie Peck, Earlham College Tuesday October.
CS 838: Pervasive Parallelism Introduction to MPI Copyright 2005 Mark D. Hill University of Wisconsin-Madison Slides are derived from an online tutorial.
MPI Communications Point to Point Collective Communication Data Packaging.
Message Passing Programming Model AMANO, Hideharu Textbook pp. 140-147.
Summary of MPI commands Luis Basurto. Large scale systems Shared Memory systems – Memory is shared among processors Distributed memory systems – Each.
Supercomputing in Plain English Applications and Types of Parallelism PRESENTERNAME PRESENTERTITLE PRESENTERDEPARTMENT PRESENTERINSTITUTION DAY MONTH DATE.
Supercomputing in Plain English Applications and Types of Parallelism Henry Neeman, Director OU Supercomputing Center for Education & Research Blue Waters.
Introduction to Parallel Programming & Cluster Computing Applications and Types of Parallelism Joshua Alexander, U Oklahoma Ivan Babic, Earlham College.
Parallel Programming with MPI By, Santosh K Jena..
Parallel & Cluster Computing N-Body Simulation and Collective Communications Henry Neeman, Director OU Supercomputing Center for Education & Research University.
Parallel Programming & Cluster Computing MPI Collective Communications Dan Ernst Andrew Fitz Gibbon Tom Murphy Henry Neeman Charlie Peck Stephen Providence.
CSCI-455/522 Introduction to High Performance Computing Lecture 4.
Supercomputing in Plain English An Introduction to High Performance Computing Part VI: Distributed Multiprocessing Henry Neeman, Director OU Supercomputing.
Oct. 23, 2002Parallel Processing1 Parallel Processing (CS 730) Lecture 6: Message Passing using MPI * Jeremy R. Johnson *Parts of this lecture was derived.
Message Passing and MPI Laxmikant Kale CS Message Passing Program consists of independent processes, –Each running in its own address space –Processors.
Parallel Programming & Cluster Computing Distributed Multiprocessing David Joiner, Kean University Tom Murphy, Contra Costa College Henry Neeman, University.
Parallel Programming & Cluster Computing N-Body Simulation and Collective Communications Henry Neeman, University of Oklahoma Paul Gray, University of.
Introduction to Parallel Programming & Cluster Computing MPI Collective Communications Josh Alexander, University of Oklahoma Ivan Babic, Earlham College.
2.1 Collective Communication Involves set of processes, defined by an intra-communicator. Message tags not present. Principal collective operations: MPI_BCAST()
3/12/2013Computer Engg, IIT(BHU)1 MPI-1. MESSAGE PASSING INTERFACE A message passing library specification Extended message-passing model Not a language.
Introduction to Parallel Programming & Cluster Computing MPI Collective Communications Joshua Alexander, U Oklahoma Ivan Babic, Earlham College Michial.
Message Passing Programming Based on MPI Collective Communication I Bora AKAYDIN
Message Passing Interface Using resources from
Lecture 3: Today’s topics MPI Broadcast (Quinn Chapter 5) –Sieve of Eratosthenes MPI Send and Receive calls (Quinn Chapter 6) –Floyd’s algorithm Other.
ITCS 4/5145 Parallel Computing, UNC-Charlotte, B
CS4402 – Parallel Computing
Saturday November 12 – Tuesday November
MPI Message Passing Interface
Send and Receive.
CS 584.
MPI: The Message-Passing Interface
Send and Receive.
ITCS 4/5145 Parallel Computing, UNC-Charlotte, B
CS 5334/4390 Spring 2017 Rogelio Long
Lecture 14: Inter-process Communication
Supercomputing in Plain English Distributed Multiprocessing
Hardware Environment VIA cluster - 8 nodes Blade Server – 5 nodes
CS 584 Lecture 8 Assignment?.
Presentation transcript:

Supercomputing in Plain English Collective Communications and N-Body Problems Henry Neeman, Director OU Supercomputing Center for Education & Research Blue Waters Undergraduate Petascale Education Program May 29 – June

Collective Communication

Point to Point Always Works MPI_Send and MPI_Recv are known as “point to point” communications: they communicate from one MPI process to another MPI process. But, what if you want to communicate like one of these? one to many many to one many to many These are known as collective communications. MPI_Send and MPI_Recv can accomplish any and all of these – but should you use them that way? Collective Communications & N-Body BWUPEP2011, UIUC, May 29 - June

Point to Point Isn’t Always Good We’re interested in collective communications: one to many many to one many to many In principle, MPI_Send and MPI_Recv can accomplish any and all of these. But that may be: inefficient; inconvenient and cumbersome to code. So, the designers of MPI came up with routines that perform these collective communications for you. Collective Communications & N-Body BWUPEP2011, UIUC, May 29 - June

Collective Communications MPI_Bcast MPI_Reduce, MPI_Allreduce MPI_Gather, MPI_Gatherv, MPI_Allgather, MPI_Allgatherv MPI_Scatter, MPI_Scatterv MPI_Alltoall, MPI_Alltoallv Collective Communications & N-Body BWUPEP2011, UIUC, May 29 - June

6 MPI_Bcast What happens if one process has data that everyone else needs to know? For example, what if the server process needs to send a value that it input from standard input to the other processes? MPI_Bcast(&length, 1, MPI_INTEGER, source, MPI_COMM_WORLD); Notice: MPI_Bcast doesn’t use a tag. The call is the same for both the sender and all of the receivers (COUNTERINTUITIVE!). All processes have to call MPI_Bcast at the same time; everyone waits until everyone is done. Collective Communications & N-Body BWUPEP2011, UIUC, May 29 - June

7 Broadcast Example Part 1 #include int main (int argc, char** argv) { /* main */ const int server = 0; const int source = server; float* array = (float*)NULL; int length, index; int number_of_processes, my_rank, mpi_error_code; mpi_error_code = MPI_Init(&argc, &argv); mpi_error_code = MPI_Comm_rank(MPI_COMM_WORLD, &my_rank); mpi_error_code = MPI_Comm_size(MPI_COMM_WORLD, &number_of_processes); Collective Communications & N-Body BWUPEP2011, UIUC, May 29 - June

8 Broadcast Example Part 2 if (my_rank == source) { scanf("%d", &length); } /* if (my_rank == source) */ fprintf(stderr, "%d: before MPI_Bcast, length = %d\n", my_rank, length); mpi_error_code = MPI_Bcast(&length, 1, MPI_INTEGER, source, MPI_COMM_WORLD); fprintf(stderr, "%d: after MPI_Bcast, length = %d\n", my_rank, length); array = (float*)malloc(sizeof(float) * length); if (my_rank == source) { for (index = 0; index < length; index++) { array[index] = sqrt(index * 1.0); /* Or whatever you want */ } /* for index */ } /* if (my_rank == source) */ mpi_error_code = MPI_Bcast(array, length, MPI_FLOAT, source, MPI_COMM_WORLD); mpi_error_code = MPI_Finalize(); } /* main */ Collective Communications & N-Body BWUPEP2011, UIUC, May 29 - June

9 Broadcast Compile & Run % mpicc -o mpibroadcast mpibroadcast.c -lm % mpirun -np 8 mpibroadcast 4: before MPI_Bcast, length = 0 7: before MPI_Bcast, length = 0 3: before MPI_Bcast, length = 0 5: before MPI_Bcast, length = 0 6: before MPI_Bcast, length = 0 2: before MPI_Bcast, length = 0 0: before MPI_Bcast, length = : after MPI_Bcast, length = : after MPI_Bcast, length = : after MPI_Bcast, length = : after MPI_Bcast, length = : after MPI_Bcast, length = : after MPI_Bcast, length = : after MPI_Bcast, length = : before MPI_Bcast, length = 0 1: after MPI_Bcast, length = Collective Communications & N-Body BWUPEP2011, UIUC, May 29 - June

10 Reductions A reduction converts an array to a scalar: for example, sum, product, minimum value, maximum value, Boolean AND, Boolean OR, etc. Reductions are so common, and so important, that MPI has two routines to handle them: MPI_Reduce : sends result to a single specified process MPI_Allreduce : sends result to all processes (and therefore takes longer) Collective Communications & N-Body BWUPEP2011, UIUC, May 29 - June

11 Reduction Example Part 1 #include int main (int argc, char** argv) { /* main */ const int server = 0; const int destination = server; int value, value_sum; int number_of_processes, my_rank, mpi_error_code; mpi_error_code = MPI_Init(&argc, &argv); mpi_error_code = MPI_Comm_rank(MPI_COMM_WORLD, &my_rank); mpi_error_code = MPI_Comm_size(MPI_COMM_WORLD, &number_of_processes); Collective Communications & N-Body BWUPEP2011, UIUC, May 29 - June

12 Reduction Example Part 1 value = my_rank * number_of_processes; fprintf(stderr, "%d: reduce value = %d\n", my_rank, value); mpi_error_code = MPI_Reduce (&value, &value_sum, 1, MPI_INT, MPI_SUM, destination, MPI_COMM_WORLD); fprintf(stderr, "%d: reduce value_sum = %d\n", my_rank, value_sum); mpi_error_code = MPI_Allreduce(&value, &value_sum, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD); fprintf(stderr, "%d: allreduce value_sum = %d\n", my_rank, value_sum); mpi_error_code = MPI_Finalize(); } /* main */ Collective Communications & N-Body BWUPEP2011, UIUC, May 29 - June

Reduce: Compiling and Running % mpicc -o mpireduce mpireduce.c % mpirun -np 8 mpireduce 0: reduce value = 0 4: reduce value = 32 6: reduce value = 48 7: reduce value = 56 3: reduce value = 24 2: reduce value = 16 5: reduce value = 40 1: reduce value = 8 7: reduce value_sum = : reduce value_sum = : reduce value_sum = : reduce value_sum = : reduce value_sum = : reduce value_sum = : reduce value_sum = : reduce value_sum = 224 2: allreduce value_sum = 224 7: allreduce value_sum = 224 4: allreduce value_sum = 224 3: allreduce value_sum = 224 1: allreduce value_sum = 224 5: allreduce value_sum = 224 0: allreduce value_sum = 224 6: allreduce value_sum = 224 Collective Communications & N-Body BWUPEP2011, UIUC, May 29 - June

14 Why Two Reduction Routines? MPI has two reduction routines because of the high cost of each communication. If only one process needs the result, then it doesn’t make sense to pay the cost of sending the result to all processes. But if all processes need the result, then it may be cheaper to reduce to all processes than to reduce to a single process and then broadcast to all. You can think of MPI_Allreduce as MPI_Reduce followed by MPI_Bcast (though it doesn’t have to be implemented that way). Collective Communications & N-Body BWUPEP2011, UIUC, May 29 - June

Reduction on Arrays #1 MPI_Reduce and MPI_Allreduce are actually designed to work on arrays, where the corresponding elements of each source array are reduced into the corresponding element of the destination array (all of the same length): MPI_Allreduce(source_array, destination_array, number_of_array_elements, MPI_DATATYPE, MPI_OPERATION, MPI_COMMUNICATOR); For example: MPI_Allreduce(local_force_on_particle, global_force_on_particle, number_of_particles, MPI_FLOAT, MPI_SUM, MPI_COMM_WORLD); Collective Communications & N-Body BWUPEP2011, UIUC, May 29 - June

Reduction on Arrays #2 MPI_Allreduce(local_force_on_particle, global_force_on_particle, number_of_particles, MPI_FLOAT, MPI_SUM, MPI_COMM_WORLD); global_force_on_particle[p] = local_force_on_particle[p] on Rank 0 + local_force_on_particle[p] on Rank 1 + local_force_on_particle[p] on Rank local_force_on_particle[p] on Rank np–1; Collective Communications & N-Body BWUPEP2011, UIUC, May 29 - June

Scatter and Gather To scatter is to send data from one place to many places. To gather is to receive data from many places into one place. MPI has a variety of scatter and gather routines: MPI_Scatter, MPI_Scatterv MPI_Gather, MPI_Gatherv, MPI_Allgather, MPI_Allgatherv The scatter routines split up a single larger array into smaller subarrays, one per MPI process, and send each subarray to an MPI process. The gather routines receive many smaller subarrays, one per MPI process, and assemble them into a single larger array. Collective Communications & N-Body BWUPEP2011, UIUC, May 29 - June

MPI_Scatter MPI_Scatter takes an array whose length is divisible by the number of MPI processes, and splits it up into subarrays of equal length, then sends one subarray to each MPI process. MPI_Scatter(large_array, small_array_length, MPI_DATATYPE, small_subarray, small_subarray_length, MPI_DATATYPE, source, MPI_COMMUNICATOR); So, for a large array of length 100 on 5 MPI processes: each smaller subarray has length 20; large_array[ 0].. large_array[19] go to small_array on Rank 0; large_array[20]..large_array[39] go to small_array on Rank 1; etc Collective Communications & N-Body BWUPEP2011, UIUC, May 29 - June

MPI_Scatterv MPI_Scatterv is just like MPI_Scatter, except that the subarray lengths don’t have to be the same (and therefore the length of the large array doesn’t have to be divisible by the number of MPI processes). MPI_Scatterv(large_array, small_array_length, displacements, MPI_DATATYPE, small_subarray, small_subarray_lengths, MPI_DATATYPE, source, MPI_COMMUNICATOR); The displacements array says where each small subarray begins within the large array. Collective Communications & N-Body BWUPEP2011, UIUC, May 29 - June

MPI_Gather MPI_Gather receives a small array on each of the MPI processes, all subarrays of equal length, and joins them into a single large array on the destination MPI process. MPI_Gather(small_subarray, small_subarray_length, MPI_DATATYPE, large_array, large_array_length, MPI_DATATYPE, destination, MPI_COMMUNICATOR); So, for a small subarray of length 20 on each of 5 MPI processes: the large array on the destination process has length 100; large_array[ 0].. large_array[19] come from small_array on Rank 0; large_array[20]..large_array[39] come from small_array on Rank 1; etc Collective Communications & N-Body BWUPEP2011, UIUC, May 29 - June

MPI_Gatherv MPI_Gatherv is just like MPI_Gather, except that the subarray lengths don’t have to be the same (and therefore the length of the large array doesn’t have to be divisible by the number of MPI processes). MPI_Gatherv(small_subarray, small_subarray_length, MPI_DATATYPE, large_array, small_subarray_lengths, displacements, MPI_DATATYPE, destination, MPI_COMMUNICATOR); The displacements array says where each small subarray begins within the large array. Collective Communications & N-Body BWUPEP2011, UIUC, May 29 - June

MPI_Allgather & MPI_Allgatherv MPI_Allgather and MPI_Allgatherv are the same as MPI_Gather and MPI_Gatherv, except that the large array gets filled on every MPI process, so no destination process argument is needed. Collective Communications & N-Body BWUPEP2011, UIUC, May 29 - June

N-Body: Task Parallelism and Collective Communication [2]

24 N Bodies Collective Communications & N-Body BWUPEP2011, UIUC, May 29 - June

25 N-Body Problems An N-body problem is a problem involving N “bodies” – that is, particles (for example, stars, atoms) – each of which applies a force to all of the others. For example, if you have N stars, then each of the N stars exerts a force (gravity) on all of the other N–1 stars. Likewise, if you have N atoms, then every atom exerts a force (nuclear) on all of the other N–1 atoms. Collective Communications & N-Body BWUPEP2011, UIUC, May 29 - June

26 1-Body Problem When N is 1, you have a simple 1-Body Problem: a single particle, with no forces acting on it. Given the particle’s position P and velocity V at some time t 0, you can trivially calculate the particle’s position at time t 0 +Δt: P(t 0 +Δt) = P(t 0 ) + VΔt V(t 0 +Δt) = V(t 0 ) Collective Communications & N-Body BWUPEP2011, UIUC, May 29 - June

27 2-Body Problem When N is 2, you have – surprise! – a 2-Body Problem: exactly 2 particles, each exerting a force that acts on the other. The relationship between the 2 particles can be expressed as a differential equation that can be solved analytically, producing a closed-form solution. So, given the particles’ initial positions and velocities, you can trivially calculate their positions and velocities at any later time. Collective Communications & N-Body BWUPEP2011, UIUC, May 29 - June

28 3-Body Problem When N is 3, you have – surprise! – a 3-Body Problem: exactly 3 particles, each exerting a force that acts on the other 2. The relationship between the 3 particles can be expressed as a differential equation that can be solved using an infinite series, producing a closed-form solution, due to Karl Fritiof Sundman in However, in practice, the number of terms of the infinite series that you need to calculate to get a reasonable solution is so large that the infinite series is impractical, so you’re stuck with the generalized formulation. Collective Communications & N-Body BWUPEP2011, UIUC, May 29 - June

29 N-Body Problems (N > 3) When N > 3, you have a general N-Body Problem: N particles, each exerting a force that acts on the other N-1 particles. The relationship between the N particles can be expressed as a differential equation that can be solved using an infinite series, producing a closed-form solution, due to Qiudong Wang in However, in practice, the number of terms of the infinite series that you need to calculate to get a reasonable solution is so large that the infinite series is impractical, so you’re stuck with the generalized formulation. Collective Communications & N-Body BWUPEP2011, UIUC, May 29 - June

30 N-Body Problems (N > 3) For N > 3, the relationship between the N particles can be expressed as a differential equation that can be solved using an infinite series, producing a closed-form solution, but convergence takes so long that this approach is impractical. So, numerical simulation is pretty much the only way to study groups of 3 or more bodies. Popular applications of N-body codes include: astronomy (that is, galaxy formation, cosmology); chemistry (that is, protein folding, molecular dynamics). Note that, for N bodies, there are on the order of N 2 forces, denoted O(N 2 ). Collective Communications & N-Body BWUPEP2011, UIUC, May 29 - June

31 N Bodies Collective Communications & N-Body BWUPEP2011, UIUC, May 29 - June

32 Force #1 A Collective Communications & N-Body BWUPEP2011, UIUC, May 29 - June

33 Force #2 A Collective Communications & N-Body BWUPEP2011, UIUC, May 29 - June

34 Force #3 A Collective Communications & N-Body BWUPEP2011, UIUC, May 29 - June

35 Force #4 A Collective Communications & N-Body BWUPEP2011, UIUC, May 29 - June

36 Force #5 A Collective Communications & N-Body BWUPEP2011, UIUC, May 29 - June

37 Force #6 A Collective Communications & N-Body BWUPEP2011, UIUC, May 29 - June

38 Force #N-1 A Collective Communications & N-Body BWUPEP2011, UIUC, May 29 - June

39 N-Body Problems Given N bodies, each body exerts a force on all of the other N – 1 bodies. Therefore, there are N (N – 1) forces in total. You can also think of this as (N (N – 1)) / 2 forces, in the sense that the force from particle A to particle B is the same (except in the opposite direction) as the force from particle B to particle A. Collective Communications & N-Body BWUPEP2011, UIUC, May 29 - June

40 Aside: Big-O Notation Let’s say that you have some task to perform on a certain number of things, and that the task takes a certain amount of time to complete. Let’s say that the amount of time can be expressed as a polynomial on the number of things to perform the task on. For example, the amount of time it takes to read a book might be proportional to the number of words, plus the amount of time it takes to settle into your favorite easy chair. C 1. N + C 2 Collective Communications & N-Body BWUPEP2011, UIUC, May 29 - June

41 Big-O: Dropping the Low Term C 1. N + C 2 When N is very large, the time spent settling into your easy chair becomes such a small proportion of the total time that it’s virtually zero. So from a practical perspective, for large N, the polynomial reduces to: C 1. N In fact, for any polynomial, if N is large, then all of the terms except the highest-order term are irrelevant. Collective Communications & N-Body BWUPEP2011, UIUC, May 29 - June

42 Big-O: Dropping the Constant C 1. N Computers get faster and faster all the time. And there are many different flavors of computers, having many different speeds. So, computer scientists don’t care about the constant, only about the order of the highest-order term of the polynomial. They indicate this with Big-O notation: O(N) This is often said as: “of order N.” Collective Communications & N-Body BWUPEP2011, UIUC, May 29 - June

43 N-Body Problems Given N bodies, each body exerts a force on all of the other N – 1 bodies. Therefore, there are N (N – 1) forces total. In Big-O notation, that’s O(N 2 ) forces. So, calculating the forces takes O(N 2 ) time to execute. But, there are only N particles, each taking up the same amount of memory, so we say that N-body codes are of: O(N) spatial complexity (memory) O(N 2 ) time complexity Collective Communications & N-Body BWUPEP2011, UIUC, May 29 - June

44 O(N 2 ) Forces Note that this picture shows only the forces between A and everyone else. A Collective Communications & N-Body BWUPEP2011, UIUC, May 29 - June

45 How to Calculate? Whatever your physics is, you have some function, F(B i,B j ), that expresses the force between two bodies B i and B j. For example, for stars and galaxies, F(A,B) = G · m B i · m B j / dist(B i, B j ) 2 where G is the gravitational constant and m is the mass of the body in question. If you have all of the forces for every pair of particles, then you can calculate their sum, obtaining the force on every particle. From that, you can calculate every particle’s new position and velocity. Collective Communications & N-Body BWUPEP2011, UIUC, May 29 - June

46 How to Parallelize? Okay, so let’s say you have a nice serial (single-CPU) code that does an N-body calculation. How are you going to parallelize it? You could: have a server feed particles to processes; have a server feed interactions (particle pairs) to processes; have each process decide on its own subset of the particles, and then share around the summed forces on those particles; have each process decide its own subset of the interactions, and then share around the summed forces from those interactions. Collective Communications & N-Body BWUPEP2011, UIUC, May 29 - June

47 Do You Need a Server? Let’s say that you have N bodies, and therefore you have ½ N (N - 1) interactions (every particle interacts with all of the others, but you don’t need to calculate both B i  B j and B j  B i ). Do you need a server? Well, can each processor determine, on its own, either (a) which of the bodies to process, or (b) which of the interactions to process? If the answer is yes, then you don’t need a server. Collective Communications & N-Body BWUPEP2011, UIUC, May 29 - June

48 Parallelize How? Suppose you have N p processors. Should you parallelize: by assigning a subset of N / N p of the bodies to each processor, OR by assigning a subset of N (N - 1) / N p of the interactions to each processor? Collective Communications & N-Body BWUPEP2011, UIUC, May 29 - June

49 Data vs. Task Parallelism Data Parallelism means parallelizing by giving a subset of the data to each process, and then each process performs the same tasks on the different subsets of data. Task Parallelism means parallelizing by giving a subset of the tasks to each process, and then each process performs a different subset of tasks on the same data. Collective Communications & N-Body BWUPEP2011, UIUC, May 29 - June

50 Data Parallelism for N-Body? If you parallelize an N-body code by data, then each processor gets N / N p pieces of data. For example, if you have 8 bodies and 2 processors, then: Processor P 0 gets the first 4 bodies; Processor P 1 gets the second 4 bodies. But, every piece of data (that is, every body) has to interact with every other piece of data, to calculate the forces. So, every processor will have to send all of its data to all of the other processors, for every single interaction that it calculates. That’s a lot of communication! Collective Communications & N-Body BWUPEP2011, UIUC, May 29 - June

51 Task Parallelism for N-body? If you parallelize an N-body code by task, then each processor gets all of the pieces of data that describe the particles (for example, positions, velocities, masses). Then, each processor can calculate its subset of the interaction forces on its own, without talking to any of the other processors. But, at the end of the force calculations, everyone has to share all of the forces that have been calculated, so that each particle ends up with the total force that acts on it (global reduction). Collective Communications & N-Body BWUPEP2011, UIUC, May 29 - June

52 MPI_Reduce (C) Here’s the C syntax for MPI_Reduce : mpi_error_code = MPI_Reduce(sendbuffer, recvbuffer, count, datatype, operation, root, communicator, mpi_error_code); For example, to do a sum over all of the particle forces: mpi_error_code = MPI_Reduce( local_particle_force_sum, global_particle_force_sum, number_of_particles, MPI_DOUBLE, MPI_SUM, server_process, MPI_COMM_WORLD); Collective Communications & N-Body BWUPEP2011, UIUC, May 29 - June

53 MPI_Reduce (F90) Here’s the Fortran 90 syntax for MPI_Reduce : CALL MPI_Reduce(sendbuffer, recvbuffer, & & count, datatype, operation, & & root, communicator, mpi_error_code) For example, to do a sum over all of the particle forces: CALL MPI_Reduce( & & local_particle_force_sum, & & global_particle_force_sum, & & number_of_particles, & & MPI_DOUBLE_PRECISION, MPI_SUM, & & server_process, MPI_COMM_WORLD, & & mpi_error_code) Collective Communications & N-Body BWUPEP2011, UIUC, May 29 - June

54 Sharing the Result In the N-body case, we don’t want just one processor to know the result of the sum, we want every processor to know. So, we could do a reduce followed immediately by a broadcast. But, MPI gives us a routine that packages all of that for us: MPI_Allreduce. MPI_Allreduce is just like MPI_Reduce except that every process gets the result (so we drop the server_process argument). Collective Communications & N-Body BWUPEP2011, UIUC, May 29 - June

55 MPI_Allreduce (C) Here’s the C syntax for MPI_Allreduce : mpi_error_code = MPI_Allreduce( sendbuffer, recvbuffer, count, datatype, operation, communicator); For example, to do a sum over all of the particle forces: mpi_error_code = MPI_Allreduce( local_particle_force_sum, global_particle_force_sum, number_of_particles, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD); Collective Communications & N-Body BWUPEP2011, UIUC, May 29 - June

56 MPI_Allreduce (F90) Here’s the Fortran 90 syntax for MPI_Allreduce : CALL MPI_Allreduce( & & sendbuffer, recvbuffer, count, & & datatype, operation, & & communicator, mpi_error_code) For example, to do a sum over all of the particle forces: CALL MPI_Allreduce( & & local_particle_force_sum, & & global_particle_force_sum, & & number_of_particles, & & MPI_DOUBLE_PRECISION, MPI_SUM, & & MPI_COMM_WORLD, mpi_error_code) Collective Communications & N-Body BWUPEP2011, UIUC, May 29 - June

57 Collective Communications A collective communication is a communication that is shared among many processes, not just a sender and a receiver. MPI_Reduce and MPI_Allreduce are collective communications. Others include: broadcast, gather/scatter, all-to-all. Collective Communications & N-Body BWUPEP2011, UIUC, May 29 - June

58 Collectives Are Expensive Collective communications are very expensive relative to point-to-point communications, because so much more communication has to happen. But, they can be much cheaper than doing zillions of point-to- point communications, if that’s the alternative. Collective Communications & N-Body BWUPEP2011, UIUC, May 29 - June

Thanks for your attention! Questions?

60 References [1] [2] [3] content/uploads/2007/10/Magritte-Pipe.jpghttp://lostbiro.com/blog/wp- content/uploads/2007/10/Magritte-Pipe.jpg Collective Communications & N-Body BWUPEP2011, UIUC, May 29 - June