Outline • Overview of parallel machines (~hardware) and CS 267: programming models (~software) • Introduction to Parallel Machines • Shared address space and Programming Models • Message passing Lecture 3 • Data parallel • Clusters of SMPs or GPUs • Grid James Demmel • Note: Parallel machine may or may not be tightly www.cs.berkeley.edu/~demmel/cs267_Spr15/ coupled to programming model • Historically, tight coupling • Today, portability is important

CS267 Lecture 3 1 01/27/2015 CS267 Lecture 3 2

01/28/2011

A generic parallel architecture Parallel Programming Models • Programming model is made up of the languages and libraries that create an abstract view of the machine Proc Proc Proc Proc Proc • Control Proc • How is parallelism created? • What orderings exist between operations? Interconnection Network • Data

• What data is private vs. shared? • How is logically shared data accessed or communicated? Memory Memory Memory Memory Memory • Synchronization • What operations can be used to coordinate parallelism? • What are the atomic (indivisible) operations? • Where is the memory physically located? • Cost • Is it connected directly to processors? • How do we account for the cost of each of the above? • What is the connectivity of the network?

01/27/2015 CS267 Lecture 3 3 01/27/2015 CS267 Lecture 3 4

CS267 Lecture 2 1 Simple Example Programming Model 1: Shared Memory • Consider applying a function f to the elements • Program is a collection of threads of control. of an array A and then computing its sum: • Can be created dynamically, mid-execution, in some languages n−1 • Each has a set of private variables, e.g., local stack variables ∑ f (A[i]) • Also a set of shared variables, e.g., static variables, shared common • Questions: i=0 blocks, or global heap. • Threads communicate implicitly by writing and reading shared • Where does A live? All in single memory? variables. Partitioned? • Threads coordinate by synchronizing on shared variables • What work will be done by each processors? • They need to coordinate to get a single result, how? Shared memory A: s A = array of all data s = ... f y = ..s ... fA = f(A) fA: s = sum(fA) sum i: 2 i: 5 Private i: 8 s: memory

P0 P1 Pn

01/27/2015 CS267 Lecture 3 5 01/27/2015 CS267 Lecture 3 6

Simple Example Shared Memory “Code” for Computing a Sum

• Shared memory strategy: fork(sum,a[0:n/2-1]); • small number p << n=size(A) processors sum(a[n/2,n-1]); static int s = 0; n−1 • attached to single memory f (A[i]) ∑ Thread 1 Thread 2 • Parallel Decomposition: i=0

• Each evaluation and each partial sum is a task. for i = 0, n/2-1 for i = n/2, n-1 • Assign n/p numbers to each of p procs s = s + f(A[i]) s = s + f(A[i]) • Each computes independent “private” results and partial sum. • Collect the p partial sums and compute a global sum. • What is the problem with this program? Two Classes of Data: • Logically Shared • A or data race occurs when: • The original n numbers, the global sum. - Two processors (or two threads) access the same • Logically Private variable, and at least one does a write. • The individual function evaluations. - The accesses are concurrent (not synchronized) so • What about the individual partial sums? they could happen simultaneously

01/27/2015 CS267 Lecture 3 7 01/27/2015 CS267 Lecture 3 8

CS267 Lecture 2 2 Shared Memory “Code” for Computing a Sum Improved Code for Computing a Sum

2 static int s = 0; A= 3 5 f (x) = x Why not do lock static int s = 0; static lock lk; Inside loop?

Thread 1 Thread 2 Thread 1 Thread 2 …. … compute f([A[i]) and put in reg0 9 compute f([A[i]) and put in reg0 25 local_s1= 0 local_s2 = 0 reg1 = s 0 reg1 = s 0 for i = 0, n/2-1 for i = n/2, n-1 reg1 = reg1 + reg0 9 reg1 = reg1 + reg0 25 local_s1 = local_s1 + f(A[i]) local_s2= local_s2 + f(A[i]) s = reg1 9 s = reg1 25 lock(lk); lock(lk); … … s = s + local_s1 s = s +local_s2 unlock(lk); unlock(lk); • Assume A = [3,5], f(x) = x2, and s=0 initially • For this program to work, s should be 32 + 52 = 34 at the end • Since addition is associative, it’s OK to rearrange order • but it may be 34,9, or 25 • Most computation is on private variables • The atomic operations are reads and writes - Sharing frequency is also reduced, which might improve speed - But there is still a race condition on the update of shared s • Never see ½ of one number, but += operation is not atomic - The race condition can be fixed by adding locks (only one • All computations happen in (private) registers thread can hold a lock at a time; others wait for it)

01/27/2015 CS267 Lecture 3 9 01/27/2015 CS267 Lecture 3 10

Review so far and plan for Lecture 3 Machine Model 1a: Shared Memory • Processors all connected to a large shared memory. Programming Models Machine Models • Typically called Symmetric Multiprocessors (SMPs) • SGI, Sun, HP, Intel, IBM SMPs 1. Shared Memory 1a. Shared Memory • Multicore chips, except that all caches are shared 1b. Multithreaded Procs. • Advantage: (UMA) 1c. Distributed Shared Mem. • Cost: much cheaper to access data in cache than main memory • Difficulty scaling to large numbers of processors 2. Message Passing 2a. • <= 32 processors typical 2b. Internet & Grid Computing 2a. Global Address Space 2c. Global Address Space P1 P2 Pn $ $ $ 3. Data Parallel 3a. SIMD 3b. Vector bus 4. Hybrid 4. Hybrid shared $ Note: $ = cache memory

01/27/2015 CS267 Lecture 3 11 01/27/2015 CS267 Lecture 3 12

What about GPU? What about Cloud?

CS267 Lecture 2 3 Problems Scaling Shared Memory Hardware Example: Problem in Scaling Shared Memory • Why not put more processors on (with larger memory?) • The memory bus becomes a bottleneck • Caches need to be kept coherent • Performance degradation is a “smooth” function of • Example from a Parallel Spectral Transform Shallow the number of processes. Water Model (PSTSWM) demonstrates the problem • No shared data between them, so there should be • Experimental results (and slide) from Pat Worley at ORNL perfect parallelism. • This is an important kernel in atmospheric models • 99% of the floating point operations are multiplies or adds, which generally run well on all processors • (Code was run for a 18 • But it does sweeps through memory with little reuse of vertical levels with a operands, so uses bus and shared memory frequently range of horizontal • These experiments show performance per , with sizes.) one “copy” of the code running independently on varying numbers of procs • The best case for shared memory: no sharing • But the data doesn’t all fit in the registers/cache

01/27/2015 CS267 Lecture 3 13 01/27/2015 CS267 Lecture 3 From Pat Worley, ORNL 14

Machine Model 1b: Multithreaded Processor Eldorado Processor (logical view) • Multiple thread “contexts” without full processors

• Memory and some other state is shared Programs 1 2 3 4 running in • Sun Niagra processor (for servers) parallel

i = n i = n • Up to 64 threads all running simultaneously (8 threads x 8 cores) Sub- . problem . Serial . i = 3 A . i = 1 Code Concurrent • In addition to sharing memory, they share floating point units . . threads of i = 2 Su b- i = 0 computation • Why? Switch between threads for long-latency memory operations problem i = 1 B • Cray MTA and Eldorado processors (for HPC) Subproblem A

T0 T1 Tn Multithreaded ...... across multiple processors

shared $, shared floating point units, etc.

Memory Source: John Feo, Cray 01/27/2015 CS267 Lecture 3 15 01/27/2015 CS267 Lecture 3 16

CS267 Lecture 2 4 Machine Model 1c: Distributed Shared Memory Review so far and plan for Lecture 3 • Memory is logically shared, but physically distributed Programming Models Machine Models • Any processor can access any address in memory • Cache lines (or pages) are passed around machine 1. Shared Memory 1a. Shared Memory • SGI is canonical example (+ research machines) 1b. Multithreaded Procs. • Scales to 512 (SGI Altix (Columbia) at NASA/Ames) 1c. Distributed Shared Mem. • Limitation is cache coherency protocols – how to 2. Message Passing 2a. Distributed Memory keep cached copies of the same address consistent 2b. Internet &

P1 P2 Pn 2a. Global Address Space 2c. Global Address Space Cache lines (pages) $ $ $ must be large to 3. Data Parallel 3a. SIMD amortize overhead 3b. Vector network ! locality still critical 4. Hybrid 4. Hybrid memory memory memory to performance

01/27/2015 CS267 Lecture 3 17 01/27/2015 CS267 Lecture 3 18

What about GPU? What about Cloud?

Review so far and plan for Lecture 3 Programming Model 2: Message Passing • Program consists of a collection of named processes. Programming Models Machine Models • Usually fixed at program startup time • Thread of control plus local address space -- NO shared data. 1. Shared Memory 1a. Shared Memory • Logically shared data is partitioned over local processes. 1b. Multithreaded Procs. • Processes communicate by explicit send/receive pairs 1c. Distributed Shared Mem. • Coordination is implicit in every communication event. 2. Message Passing 2a. Distributed Memory • MPI (Message Passing Interface) is the most commonly used SW Private 2b. Internet & Grid Computing memory 2a. Global Address Space 2c. Global Address Space s: 12 s: 14 s: 11 3. Data Parallel 3a. SIMD receive Pn,s 3b. Vector y = ..s ... i: 2 i: 3 i: 1 4. Hybrid 4. Hybrid P0 P1 send P1,s Pn Network

01/27/2015 CS267 Lecture 3 19 01/27/2015 CS267 Lecture 3 20

What about GPU? What about Cloud?

CS267 Lecture 2 5 Computing s = f(A[1])+f(A[2]) on each processor MPI – the de facto standard

° First possible solution – what could go wrong? MPI has become the de facto standard for parallel Processor 1 Processor 2 computing using message passing xlocal = f(A[1]) xlocal = f(A[2]) send xlocal, proc2 send xlocal, proc1 Pros and Cons of standards receive xremote, proc2 receive xremote, proc1 s = xlocal + xremote s = xlocal + xremote • MPI created finally a standard for applications development in the HPC community → portability

° If send/receive acts like the telephone system? The post office? • The MPI standard is a least common denominator ° Second possible solution building on mid-80s technology, so may discourage innovation Processor 1 Processor 2 Programming Model reflects hardware! xlocal = f(A[1]) xlocal = f(A[2]) send xlocal, proc2 receive xremote, proc1 receive xremote, proc2 send xlocal, proc1 s = xlocal + xremote s = xlocal + xremote “I am not sure how I will program a Petaflops computer, but I am sure that I will need MPI somewhere” – HDS 2001 ° What if there are more than 2 processors? 01/27/2015 CS267 Lecture 3 21 01/27/2015 CS267 Lecture 3 22

Machine Model 2a: Distributed Memory PC Clusters: Contributions of Beowulf • Cray XE6 (Hopper), Cray XC30 (Edison) • An experiment in systems (1994) • PC Clusters (Berkeley NOW, Beowulf) • Established vision of low cost, high end computing • Edison, Hopper, most of the Top500, are distributed • Cost effective because it uses off-the-shelf parts memory machines, but the nodes are SMPs. • Demonstrated effectiveness of PC clusters for • Each processor has its own memory and cache but some (not all) classes of applications ’ cannot directly access another processor s memory. • Provided networking software • Each “node” has a Network Interface (NI) for all • Conveyed findings to broad community (great PR) communication and synchronization. • Tutorials and book • Design standard to rally P0 NI P1 NI Pn NI community!

memory memory . . . memory • Standards beget: books, trained people, interconnect software … virtuous cycle Adapted from Gordon Bell, presentation at Salishan 2000 01/27/2015 CS267 Lecture 3 23 01/27/2015 CS267 Lecture 3 24

CS267 Lecture 2 6 Tflop/s and Pflop/s Clusters (2009 data) Machine Model 2b: Internet/Grid Computing • SETI@Home: Running on 3.3M hosts, 1.3M users (1/2013) The following are examples of clusters configured out of • ~1000 CPU Years per Day (older data) separate networks and processor components • 485,821 CPU Years so far • Sophisticated Data & Signal Processing Analysis • Distributes Datasets from Arecibo Radio Telescope • About 82% of Top 500 are clusters (Nov 2009, up from 72% in 2005), • 4 of top 10 • IBM Cell cluster at Los Alamos (Roadrunner) is #2 • 12,960 Cell chips + 6,948 dual-core AMD Opterons; Next Step- Allen Telescope Array • 129600 cores altogether • 1.45 PFlops peak, 1.1PFlops Linpack, 2.5MWatts • Infiniband connection network • For more details use “database/sublist generator” at www.top500.org

Google “volunteer computing” or “BOINC” 01/27/2015 CS267 Lecture 3 25 01/27/2015 CS267 Lecture 4 26

Programming Model 2a: Global Address Space Machine Model 2c: Global Address Space • Cray T3D, T3E, X1, and HP Alphaserver cluster • Program consists of a collection of named threads. • Clusters built with Quadrics, Myrinet, or Infiniband • Usually fixed at program startup time • Local and shared data, as in shared memory model • The network interface supports RDMA (Remote Direct • But, shared data is partitioned over local processes Memory Access) • Cost models says remote data is expensive • NI can directly access memory without interrupting the CPU • One processor can read/write memory with one-sided • Examples: UPC, Titanium, Co-Array Fortran operations (put/get) • Global Address Space programming is an intermediate • Not just a load/store as on a shared memory machine point between message passing and shared memory • Continue computing while waiting for memory op to finish • Remote data is typically not cached locally Shared memory P0 NI P1 NI Global address Pn NI space may be s[0]: 26 s[1]: 32 s[n]: 27 memory memory . . . memory supported in y = ..s[i] ... varying degrees i: 1 i: 5 Private i: 8 memory interconnect

P0 P1 Pn s[myThread] = ... 01/27/2015 CS267 Lecture 3 27 01/27/2015 CS267 Lecture 3 28

CS267 Lecture 2 7 Review so far and plan for Lecture 3 Programming Model 3: Data Parallel Programming Models Machine Models • Single thread of control consisting of parallel operations. • A = B+C could mean add two arrays in parallel 1. Shared Memory 1a. Shared Memory • Parallel operations applied to all (or a defined subset) of a 1b. Multithreaded Procs. data structure, usually an array 1c. Distributed Shared Mem. • Communication is implicit in parallel operators • Elegant and easy to understand and reason about 2. Message Passing 2a. Distributed Memory • Coordination is implicit – statements executed synchronously 2b. Internet & Grid Computing • Similar to Matlab language for array operations 2a. Global Address Space 2c. Global Address Space • Drawbacks: 3. Data Parallel 3a. SIMD • Not all problems fit this model 3b. Vector • Difficult to map onto coarse-grained machines A: A = array of all data 4. Hybrid 4. Hybrid f fA = f(A) fA: s = sum(fA) sum s:

01/27/2015 CS267 Lecture 3 29 01/27/2015 CS267 Lecture 3 30

What about GPU? What about Cloud?

Machine Model 3a: SIMD System Machine Model 3b: Vector Machines • A large number of (usually) small processors. • Vector architectures are based on a single processor • A single “control processor” issues each instruction. • Multiple functional units • Each processor executes the same instruction. • All performing the same operation • Some processors may be turned off on some instructions. • Instructions may specific large amounts of parallelism (e.g., 64- • Originally machines were specialized to scientific computing, way) but hardware executes only a subset in parallel few made (CM2, Maspar) • Historically important • Programming model can be implemented in the compiler • Overtaken by MPPs in the 90s • mapping n-fold parallelism to p processors, n >> p, but it’s hard • Re-emerging in recent years (e.g., HPF) • At a large scale in the Earth Simulator (NEC SX6) and Cray X1 • At a small scale in SIMD media extensions to control processor • SSE, SSE2 (Intel: Pentium/IA64) • Altivec (IBM/Motorola/Apple: PowerPC) P1 NI P2 NI P3 NI Pn-1 NI Pn NI • VIS (Sun: Sparc) . . . memory memory memory memory memory • At a larger scale in GPUs • Key idea: Compiler does some of the difficult work of finding interconnect parallelism, so the hardware doesn’t have to 01/27/2015 CS267 Lecture 3 31 01/27/2015 CS267 Lecture 3 32

CS267 Lecture 2 8 Vector Processors Cray X1: Parallel Vector Architecture

• Vector instructions operate on a vector of elements Cray combines several technologies in the X1 • These are specified as operations on vector registers • 12.8 Gflop/s Vector processors (MSP) • Shared caches (unusual on earlier vector machines) r1 r2 vr1 … vr2 … • 4 processor nodes sharing up to 64 GB of memory + + (logically, performs # elts • Single System Image to 4096 Processors adds in parallel) • Remote put/get between nodes (faster than MPI) r3 vr3 … • A vector register holds ~32-64 elts • The number of elements is larger than the amount of parallel hardware, called vector pipes or lanes, say 2-4 • The hardware performs a full vector operation in • #elements-per-vector-register / #pipes vr1 … vr2 … + + + ++ + (actually, performs #pipes adds in parallel)

01/27/2015 CS267 Lecture 3 33 01/27/2015 CS267 Lecture 3 34

Earth Simulator Architecture Review so far and plan for Lecture 3 Programming Models Machine Models Parallel Vector Architecture • High speed (vector) 1. Shared Memory 1a. Shared Memory processors 1b. Multithreaded Procs. • High memory 1c. Distributed Shared Mem. bandwidth (vector 2. Message Passing 2a. Distributed Memory architecture) 2b. Internet & Grid Computing • Fast network (new 2a. Global Address Space 2c. Global Address Space crossbar switch) 3. Data Parallel 3a. SIMD & GPU 3b. Vector

Rearranging commodity 4. Hybrid 4. Hybrid parts can’t match this performance

01/27/2015 CS267 Lecture 3 35 01/27/2015 CS267 Lecture 3 36

What about GPU? What about Cloud?

CS267 Lecture 2 9 Machine Model 4: Hybrid machines Accelerators in Top 500 (Nov 2014) • Multicore/SMPs are a building block for a larger machine with a network • Old name: • CLUMP = Cluster of SMPs • Many modern machines look like this: • Edison and Hopper (2x12 way nodes), most of Top500 • What is an appropriate programming model #4 ??? • Treat machine as “flat”, always use message passing, even within SMP (simple, but ignores an important part of memory hierarchy). • Shared memory within one SMP, but message passing outside of an SMP. • GPUs may also be building block • Nov 2014 Top500: 14% have accelerators, but 35% of performance

01/27/2015 CS267 Lecture 3 37 01/27/2015

Performance of Accelerators in Top500, Nov 2014 Performance Share of Accelerators in Top500, Nov 2014

01/27/2015 01/27/2015

CS267 Lecture 2 10 Programming Model 4: Hybrids Review so far and plan for Lecture 3 • Programming models can be mixed Programming Models Machine Models • Message passing (MPI) at the top level with shared memory within a node is common 1. Shared Memory 1a. Shared Memory • New DARPA HPCS languages mix data parallel and 1b. Multithreaded Procs. threads in a global address space 1c. Distributed Shared Mem. • Global address space models can (often) call message 2. Message Passing 2a. Distributed Memory passing libraries or vice verse 2b. Internet & Grid Computing • Global address space models can be used in a hybrid 2a. Global Address Space 2c. Global Address Space mode 3. Data Parallel 3a. SIMD & GPU • Shared memory when it exists in hardware 3b. Vector • Communication (done by the runtime system) otherwise • For better or worse 4. Hybrid 4. Hybrid • often programmed this way for peak performance What about GPU? What about Cloud?

01/27/2015 CS267 Lecture 3 41 01/27/2015 CS267 Lecture 3 42

What about GPU and Cloud? Lessons from Lecture 3 • GPU’s big performance opportunity is data parallelism • Most programs have a mixture of highly parallel operations, and • Three basic conceptual models some not so parallel • Shared memory • GPUs provide a threaded programming model (CUDA) for data • Distributed memory parallelism to accommodate both • Current research attempting to generalize programming model • Data parallel to other architectures, for portability (OpenCL) and hybrids of these machines • Guest lecture later in the semester • All of these machines rely on dividing up work • lets large numbers of people easily into parts that are: share O(105) machines • Mostly independent (little synchronization) • MapReduce was first programming model: data parallel on distributed memory • About same size (load balanced) • More flexible models (Hadoop, Spark, …) invented since then • Have good locality (little communication) • Guest lecture later in the semester • Next Lecture: How to identify parallelism and • Both may be used for class projects locality in applications 01/27/2015 CS267 Lecture 3 43 01/27/2015 CS267 Lecture 3 44

CS267 Lecture 2 11