Parallelism Can we make it faster 09 Sep20

  • Slides: 20
Download presentation
Parallelism Can we make it faster? 09 -Sep-20

Parallelism Can we make it faster? 09 -Sep-20

The RAM model n The RAM (Random Access Machine) model of computation assumes: n

The RAM model n The RAM (Random Access Machine) model of computation assumes: n n This simple model is very useful guide for algorithm design n n There is a single processing unit There is an arbitrarily large amount of memory Accessing any arbitrarily chosen (i. e. random) memory location takes unit time For maximum efficiency, “tuning” to the particular hardware is required The RAM model breaks down when the assumptions are violated n If an array is so large that only a portion of it fits in memory (the rest is on disk), very different sorting algorithms should be used 2

Approaches to parallelism n n The basic question is, do the processing units share

Approaches to parallelism n n The basic question is, do the processing units share memory, or do they send messages to one another? A thread consists of a single flow of control, a program counter, a call stack, and a small amount of threadspecific data n n Threads share memory, and communicate by reading and writing to that memory This is thread-based or shared-memory parallel processing Java “out of the box” is thread-based A process is a thread that has its own private memory n n Threads (sometimes called actors) send messages to one another This is message-passing parallel processing 3

The PRAM model n An obvious extension to the RAM model is the Parallel

The PRAM model n An obvious extension to the RAM model is the Parallel Random Access model, which assumes: n n The third assumption is “good enough” for many in-memory sequential programs, but not good enough for parallel programs n n n There are multiple processing units There is an arbitrarily large amount of memory Accessing any memory location takes unit time If the processing units share memory, then complicated and expensive synchronization mechanisms must be used If the processing units do not share memory, then each has its own (fast) local memory, and communicates with other processes by sending messages to them (much slower--especially if over a network!) Bottom line: Because there seems to be no way to meet the unit time assumption, the PRAM model is seriously broken! 4

The CTA model n The Candidate Type Architecture model makes these assumptions: n There

The CTA model n The Candidate Type Architecture model makes these assumptions: n There are P standard sequential processors, each with its own local memory n n Processors can access non-local memory over a communication network n n One of the processors may be acting as “controller, ” doing things like initialization and synchronization Non-local memory is between 100 times and 10000 times slower to access than local memory (based on common architectures) A processor can make only a very small number (maybe 1 or 2) of simultaneous non-local memory accesses 5

Consequences of CTA n The CTA model does not specify how many processors are

Consequences of CTA n The CTA model does not specify how many processors are available n n n The programmer does not need to plan for some specific number of processors More processors may cause the code to execute somewhat more quickly The CTA modes does specify a huge discrepancy between local and non-local memory access n The programmer should minimize the number of non-local memory accesses 6

Costs of parallelism n n n It would be great if having N processors

Costs of parallelism n n n It would be great if having N processors meant our programs would run N times as fast, but. . . There is overhead involved in setting up the parallelism, which we don’t need to pay for a sequential program There are parts of any program that cannot be parallelized Some processors will be idle because there is nothing for them to do Processors have to contend for the same resources, such as memory, and may have to wait for one another 7

Overhead n Overhead is any cost incurred by the parallel algorithm but not by

Overhead n Overhead is any cost incurred by the parallel algorithm but not by the corresponding sequential algorithm n n n Communication among threads and processes (a single thread has no other threads with which to communicate) Synchronization is when one thread or process has to wait for results or events from another thread or process Contention for a shared resource, such as memory n n n Java’s synchronized is used to wait for a lock to become free Extra computation to combine the results of the various threads or processes Extra memory may be needed to give each thread or process the memory required to do its job 8

Amdahl’s law n n n Some proportion P of a program can be made

Amdahl’s law n n n Some proportion P of a program can be made to run in parallel, while the remaining (1 - P) must remain sequential If there are N processors, then the computation can be done in (1 - P) + P/N time The maximum speedup is then 1. (1 - P) + P/N As N goes to infinity, the maximum speedup is 1/(1 - P) For example, if P = 0. 75, the maximum speedup is (1/0. 25), or four times 9

Consequences of Amdahl’s law n n n If 75% of a process can be

Consequences of Amdahl’s law n n n If 75% of a process can be parallelized, and there are four processors, then the possible speedup is 1 / ((1 - 0. 75) + 0. 75/4) = 2. 286 But with 40 processors--ten times as many--the speedup is only 1 / ((1 - 0. 75) + 0. 75/40) = 3. 721 This has led many people (including Amdahl) to conclude that having lots of processors won’t help very much However. . For many problems, as the data set gets larger, n n n The inherently sequential part of the program remains (fairly) constant Thus, the sequential proportion P becomes smaller So: The greater the volume of data, the more speedup we can get 10

Idle time n Idle time results when n n There is a load imbalance--one

Idle time n Idle time results when n n There is a load imbalance--one process may have much less work to do than another A process must wait for access to memory or some other shared resource n n Data is registers is most quickly accessed Data in a cache is next most quickly accessed n A level 1 cache is the fastest, but also the smallest n A level 2 cache is larger, but slower n Memory--RAM--is much slower n Disk access is very much slower 11

Dependencies n A dependency is when one thread or process requires the result of

Dependencies n A dependency is when one thread or process requires the result of another thread or process n Example: (a + b) * (c + d) n n The additions can be done in parallel The multiplication must wait for the results of the additions Of course, at this level, the hardware itself handles the parallelism Threads or processors that depend on results from other threads or processors must wait for those results 12

Parallelism in Java uses the shared memory model n n n There are various

Parallelism in Java uses the shared memory model n n n There are various competing Java packages (such as Akka and Kilim) to support message passing, but nothing yet in the official Java release The programming language Erlang has developed the message passing approach Scala is a Java competitor that supports both approaches n Scala’s message passing is based on Erlang 13

Concurrency in Java, I n n 1. 2. 3. 4. 5. Java Concurrency in

Concurrency in Java, I n n 1. 2. 3. 4. 5. Java Concurrency in Practice, by Brian Goetz, is the book to have if you need to do much concurrent programming in Java The following 11 points are from his summary of basic principles It’s the mutable state, stupid! Make fields final unless they need to be mutable. Immutable objects are automatically thread-safe. Encapsulation makes it practical to manage the complexity. Guard each mutable variable with a lock. 14

Concurrency in Java, II 6. 7. 8. Guard all variables in an invariant with

Concurrency in Java, II 6. 7. 8. Guard all variables in an invariant with the same lock. Hold locks for the duration of compound actions. A program that accesses a mutable variable from multiple threads without synchronization is a broken program. 9. Don’t rely on clever reasoning about why you don’t need to synchronize. 10. Include thread safety in the design process—or explicitly document that your class in not thread-safe. 11. Document your synchronization policy. 15

Models of concurrency n n Shared memory model—as in Java. Actors n n n

Models of concurrency n n Shared memory model—as in Java. Actors n n n An actor is a lightweight thread with its own private memory Actors can send messages to one another Messages are received in an actor’s “mailbox” (a queue) Actors can monitor the health of other actors STM, Software Transactional Memory n n n A copy is made of the input data to a function The function is computed The input data is compared to the current values of that data n n If nothing has changed, the function result is stored If the input data has changed, the result is discarded and the function is tried again with the new data 16

Functional programming n In functional programming (FP): n A function is a value n

Functional programming n In functional programming (FP): n A function is a value n n n A function acts like a function in mathematics n n n It can be assigned to variables It can be passed as an argument to another function It can be returned as the result of a function call There are much briefer ways of writing a literal function n Scala example: a => 101 * a If you call it with the same arguments, you will get the same result. Every time. Guaranteed. Functions have no side effects Immutable values are strongly emphasized over mutable values n n Some languages, such as Haskell, don’t allow mutable values at all Computation proceeds by the application of functions, not by changing the state of mutable variables 17

Why functional programming? n Here are three most important reasons that functional programming is

Why functional programming? n Here are three most important reasons that functional programming is better for concurrency than imperative programming: Immutable values are automatically thread safe n n n Why not functional programming? n n Functional languages—Lisp, Haskell, ML, OCaml—have long been regarded as only for ivory-tower academics Functional languages are “weird” (meaning: unfamiliar) 18

What’s happening now? n Moore’s law has ended n n n n Instead of

What’s happening now? n Moore’s law has ended n n n n Instead of getting faster processors, we’re now getting more of them Consequently, parallelism, and concurrency, have become much more important After about ten years, CIS 120 is once again starting with OCaml Python has gotten more functional Other languages are getting more functional Microsoft is starting to promote F# (based on ML? ) Java 8 will have some functional features Scala is a hybrid object/functional language based on Java, and is freely available now 19

The End …for now 20

The End …for now 20