CS 240A : Monday, April 9, 2007 - PowerPoint PPT Presentation

cs 240a monday april 9 2007 l.
Skip this Video
Loading SlideShow in 5 Seconds..
CS 240A : Monday, April 9, 2007 PowerPoint Presentation
CS 240A : Monday, April 9, 2007

play fullscreen
1 / 28
Download
Download Presentation

CS 240A : Monday, April 9, 2007

Presentation Transcript

  1. CS 240A : Monday, April 9, 2007 • Join Google discussion group! (See course home page). • Accounts on DataStar, San Diego Supercomputing Center should be here this afternoon. • DataStar logon & tool intro at noon Tuesday (tomorrow) in ESB 1003. • Homework 0 (describe a parallel app) due Wednesday. • Homework 1 (first programming problem) due next Monday, April 16.

  2. Hello, world in MPI #include <stdio.h> #include "mpi.h" int main( int argc, char *argv[]) { int rank, size; MPI_Init( &argc, &argv ); MPI_Comm_size( MPI_COMM_WORLD, &size ); MPI_Comm_rank( MPI_COMM_WORLD, &rank ); printf( "Hello world from process %d of %d\n", rank, size ); MPI_Finalize(); return 0; }

  3. MPI in nine routines (all you really need) MPI_Init Initialize MPI_Finalize Finalize MPI_Comm_size How many processes? MPI_Comm_size Which process am I? MPI_Wtime Timer MPI_Send Send data to one proc MPI_Recv Receive data from one proc MPI_Bcast Broadcast data to all procs MPI_Reduce Combine data from all procs

  4. Ten more MPI routines (sometimes useful) More group routines (like Bcast and Reduce): MPI_Alltoall, MPI_Alltoallv MPI_Scatter, MPI_Gather Non-blocking send and receive: MPI_Isend, MPI_Irecv MPI_Wait, MPI_Test, MPI_Probe, MPI_Iprobe Synchronization: MPI_Barrier

  5. Some MPI Concepts • Communicator • A set of processes that are allowed to communicate between themselves. • A library can use its own communicator, separated from that of a user program. • Default Communicator: MPI_COMM_WORLD

  6. Some MPI Concepts • Data Type • What kind of data is being sent/recvd? • Mostly corresponds to C data type • MPI_INT, MPI_CHAR, MPI_DOUBLE, etc.

  7. Some MPI Concepts • Message Tag • Arbitrary (integer) label for a message • Tag of Send must match tag of Recv

  8. Parameters of blocking send MPI_Send(buf, count, datatype, dest, tag, comm) Address of Datatype of Message tag send b uff er each item Number of items Rank of destination Comm unicator to send process

  9. Parameters of blocking receive MPI_Recv(buf, count, datatype, src, tag, comm, status) Status Address of Datatype of Message tag after oper ation receiv e b uff er each item Maxim um n umber Rank of source Comm unicator of items to receiv e process

  10. Example: Send an integer x from proc 0 to proc 1 MPI_Comm_rank(MPI_COMM_WORLD,&myrank); /* get rank */ if (myrank == 0) { int x; MPI_Send(&x, 1, MPI_INT, 1, msgtag, MPI_COMM_WORLD); } else if (myrank == 1) { int x; MPI_Recv(&x, 1, MPI_INT,0,msgtag,MPI_COMM_WORLD,&status); }

  11. Partitioned Global Address Space Languages • Explicitly-parallel programming model with SPMD parallelism • Fixed at program start-up, typically 1 thread per processor • Global address space model of memory • Allows programmer to directly represent distributed data structures • Address space is logically partitioned • Local vs. remote memory (two-level hierarchy) • Performance transparency and tunability are goals • Initial implementation can use fine-grained shared memory • Languages: UPC (C), CAF (Fortran), Titanium (Java)

  12. Global Address Space Eases Programming Thread0 Thread1 Threadn • The languages share the global address space abstraction • Shared memory is partitioned by processors • Remote memory may stay remote: no automatic caching implied • One-sided communication through reads/writes of shared variables • Both individual and bulk memory copies • Differ on details • Some models have a separate private memory area • Distributed array generality and how they are constructed X[0] X[1] X[P] Shared Global address space ptr: ptr: ptr: Private

  13. UPC Execution Model • A number of threads working independently: SPMD • Number of threads specified at compile-time or run-time • Program variable THREADS specifies number of threads • MYTHREAD specifies thread index (0..THREADS-1) • upc_barrier is a global synchronization • upc_forall is a parallel loop

  14. Hello World in UPC • Any legal C program is also a legal UPC program • If you compile and run it as UPC with P threads, it will run P copies of the program. #include <upc.h> /* needed for UPC extensions */ #include <stdio.h> main() { printf("Thread %d of %d: hello UPC world\n", MYTHREAD, THREADS); }

  15. r =1 Example: Monte Carlo Pi Calculation • Estimate Pi by throwing darts at a unit square • Calculate percentage that fall in the unit circle • Area of square = r2 = 1 • Area of circle quadrant = ¼ * p r2 = p/4 • Randomly throw darts at x,y positions • If x2 + y2 < 1, then point is inside circle • Compute ratio: • # points inside / # points total • p = 4*ratio • See example code

  16. Private vs. Shared Variables in UPC • Normal C variables and objects are allocated in the private memory space for each thread. • Shared variables are allocated only once, with thread 0 shared int ours; int mine; • Simple shared variables of this kind may not occur in a within a function definition Thread0 Thread1 Threadn Shared ours: Global address space mine: mine: mine: Private

  17. Shared Arrays Are Cyclic By Default • Shared array elements are spread across the threads shared int x[THREADS] /* 1 element per thread */ shared int y[3][THREADS] /* 3 elements per thread */ shared int z[3*THREADS] /* 3 elements per thread, cyclic */ • In the pictures below • THREADS = 4 • Elements with affinity to thread 0 are blue As a 2D array, this is logically blocked by columns x y z

  18. Example: Vector Addition • Questions about parallel vector additions: • How to layout data (here it is cyclic) • Which processor does what (here it is “owner computes”) /* vadd.c */ #include <upc_relaxed.h>#define N 100*THREADSshared int v1[N], v2[N], sum[N];void main() { int i; for(i=0; i<N; i++) if (MYTHREAD = = i%THREADS) sum[i]=v1[i]+v2[i];} cyclic layout owner computes

  19. Vector Addition with upc_forall • The vadd example can be rewritten as follows • Equivalent code could use “&sum[i]” for affinity • The code would be correct but slow if the affinity expression were i+1 rather than i. /* vadd.c */ #include <upc_relaxed.h>#define N 100*THREADSshared int v1[N], v2[N], sum[N];void main() { int i;upc_forall(i=0; i<N; i++; i) sum[i]=v1[i]+v2[i];} The cyclic data distribution may perform poorly on a cache-based shared memory machine

  20. Pointers to Shared vs. Arrays • In the C tradition, array can be access through pointers • Here is the vector addition example using pointers #include <upc_relaxed.h> #define N 100*THREADS shared int v1[N], v2[N], sum[N]; void main() {int i;shared int *p1, *p2;p1=v1; p2=v2;for (i=0; i<N; i++, p1++, p2++ ) if (i %THREADS= = MYTHREAD) sum[i]= *p1 + *p2;} v1 p1

  21. UPC Pointers Where does the pointer reside? Where does it point? int *p1; /* private pointer to local memory */ shared int *p2; /* private pointer to shared space */ int *shared p3; /* shared pointer to local memory */ shared int *shared p4; /* shared pointer to shared space */ Shared to private is not recommended.

  22. UPC Pointers Thread0 Thread1 Threadn p3: p3: p3: Shared p4: p4: p4: Global address space p1: p1: p1: Private p2: p2: p2: int *p1; /* private pointer to local memory */ shared int *p2; /* private pointer to shared space */ int *shared p3; /* shared pointer to local memory */ shared int *shared p4; /* shared pointer to shared space */ Pointers to shared often require more storage and are more costly to dereference; they may refer to local or remote memory.

  23. Common Uses for UPC Pointer Types int *p1; • These pointers are fast • Use to access private data in part of code performing local work • Often cast a pointer-to-shared to one of these to get faster access to shared data that is local shared int *p2; • Use to refer to remote data • Larger and slower due to test-for-local + possible communication int *shared p3; • Not recommended shared int *shared p4; • Use to build shared linked structures, e.g., a linked list

  24. UPC Pointers • In UPC pointers to shared objects have three fields: • thread number • local address of block • phase (specifies position in the block)

  25. UPC Pointers • Pointer arithmetic supports blocked and non-blocked array distributions • Casting of shared to private pointers is allowed but not vice versa ! • When casting a pointer to shared to a private pointer, the thread number of the pointer to shared may be lost • Casting of shared to private is well defined only if the object pointed to by the pointer to shared has affinity with the thread performing the cast

  26. Synchronization • No implicit synchronization among threads • Several explicit synchronization mechanisms: • Barriers (Blocking) • upc_barrier • Split Phase Barriers (Non Blocking) • upc_notify • upc_wait • Optional label allows for debugging • Locks

  27. Bulk Copy Operations in UPC • UPC functions to move data to/from shared memory • Typically lots faster than a loop & assignment stmt! • Can be used to move chunks in the shared space or between shared and private spaces • Equivalent of memcpy : • upc_memcpy(dst, src, size) : copy from shared to shared • upc_memput(dst, src, size) : copy from private to shared • upc_memget(dst, src, size) : copy from shared to private • Equivalent of memset: • upc_memset(dst, char, size) : initialize shared memory

  28. Dynamic Memory Allocation in UPC • Dynamic memory allocation of shared memory is available in UPC • Functions can be collective or not • A collective function has to be called by every thread and will return the same value to all of them • See manual for details