23+-+Concurrency - 12/9/2009 Concurrency What is it? EECS...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
12/9/2009 1 Concurrency EECS 280 Programming and Introductory Data Structures 1 Concurrency What is it? 2 c Increasingly, gains in performance coming from more processors rather than faster processors. c This trend is accelerating. c There are two ways to exploit “more processors”: c Run more programs at the same time. c Have single programs do more than one thing at once. c A program that can do more than one thing at a time is called a “concurrent program”. c Concurrent programs are interesting, but they are surprisingly difficult to do well. Concurrency Why is it hard to program concurrently? 3 c To get a sense for why programming for concurrency is so hard, think about the List ADT. c Each method is allowed to assume that the List invariants are true, as long as it establishes them before exiting. c In effect, no other method can “see” the broken invariants. c But, if two (or more) List methods can execute at the same time , then they could each be in “the middle”, and the invariants broken by one would be visible in the other. c This is clearly Not Good. c Any time two different pieces of a concurrent program could possibly touch the same piece of state, Bad Things can happen. Concurrency What is good for? 4 c However, there is an interesting class of concurrent programs referred to as “embarrassingly parallel”. c These are programs where there are lots of things that could happen at the same time, without any shared state between them. c Such programs are very easy to make concurrent, because you don't have to worry about a whole class of problems. Concurrency Embarrassingly Parallel 5 c Consider the following loop: template <typename T> void apply(T a[], int size, T (*fn)(T)) { // REQUIRES: fn has no side effects, // a has size elements // EFFECTS: a[i] = fn(a[i]), // for each element in a. for (int i = 0; i < size; i++) a[i] = fn(a[i]); } c In this loop, each iteration could be done concurrently: c Each works on a different element. c Each execution of the function can be independent. Concurrency Embarrassingly Parallel 6 c Ideally, we would chop the loop into N equal-sized pieces, where N was the number of processors we had, and then run each of the N pieces on its own processor. c As it happens, a C++ library called Threading Building Blocks (TBB) can do exactly this. c It has a templated function that performs this for us: template <typename Range, typename Body> void parallel_for(Range &r, Body &b); template <typename T> void apply(T a[],int size,T (*fn)(T)){ for (int i = 0; i < size; i++) a[i] = fn(a[i]); }
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
12/9/2009 2 Concurrency Parallel For 7 c This is just a templated function, except now there are two
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 10/26/2010 for the course EECS 280 taught by Professor Noble during the Fall '08 term at University of Michigan.

Page1 / 5

23+-+Concurrency - 12/9/2009 Concurrency What is it? EECS...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online