Docsity

Understanding Parallelism and Its Models: RAM, PRAM, CTA and Consequences, Slides of Programming Languages

Partial preview of the text

Download Understanding Parallelism and Its Models: RAM, PRAM, CTA and Consequences and more Slides Programming Languages in PDF only on Docsity!

Parallelism

Can we make it faster?

docsity.com

2

The RAM model

 The RAM (Random Access Machine) model of computation assumes:  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

 This simple model is very useful guide for algorithm design  For maximum efficiency, “tuning” to the particular hardware is required

 The RAM model breaks down when the assumptions are violated  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

docsity.com

4

The PRAM model

 An obvious extension to the RAM model is the Parallel Random Access model, which assumes:  There are multiple processing units  There is an arbitrarily large amount of memory  Accessing any memory location takes unit time

 The third assumption is “good enough” for many in-memory sequential programs, but not good enough for parallel programs  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!

docsity.com

5

The CTA model

 The Candidate Type Architecture model makes these

assumptions:

 There are P standard sequential processors, each with its own local memory  One of the processors may be acting as “controller,” doing things like initialization and synchronization  Processors can access non-local memory over a communication network  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

  1. of simultaneous non-local memory accesses

docsity.com

7

Costs of parallelism

 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

docsity.com

8

Overhead

Overhead is any cost incurred by the parallel algorithm but not by the corresponding sequential algorithm  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  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

docsity.com

10

Consequences of Amdahl’s law

 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.

 But with 40 processors--ten times as many--the speedup is only 1 / ((1 - 0.75) + 0.75/40) = 3.

 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,  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

docsity.com

11

Idle time

 Idle time results when

 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  Data is registers is most quickly accessed  Data in a cache is next most quickly accessed  A level 1 cache is the fastest, but also the smallest  A level 2 cache is larger, but slower  Memory--RAM--is much slower  Disk access is very much slower

docsity.com

Parallelism in Java

 Java uses the shared memory model

 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

 Scala’s message passing is based on Erlang

docsity.com 13

Concurrency in Java, I

 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

1. It’s the mutable state, stupid!

2. Make fields final unless they need to be mutable.

3. Immutable objects are automatically thread-safe.

4. Encapsulation makes it practical to manage the complexity.

5. Guard each mutable variable with a lock.

docsity.com 14

Models of concurrency

Shared memory model—as in Java.

Actors  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  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  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

docsity.com 16

Functional programming

 In functional programming (FP):

 A function is a value  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  Scala example: a => 101 * a  A function acts like a function in mathematics  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  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

docsity.com 17

What’s happening now?

 Moore’s law has ended

 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

docsity.com 19