25+-+Concurrency - 12/6/10 Concurrency What is it?...

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

View Full Document Right Arrow Icon
12/6/10 1 Concurrency EECS 280 Programming and Introductory Data Structures 1 Concurrency What is it? 2 Increasingly, gains in performance coming from more processors rather than faster processors. This trend is accelerating. There are two ways to exploit “more processors”: Run more programs at the same time. Have single programs do more than one thing at once. A program that can do more than one thing at a time is called a “concurrent program”. Concurrent programs are interesting, but they are surprisingly difficult to do well. Concurrency Why is it hard to program concurrently? 3 To get a sense for why programming for concurrency is so hard, think about the List ADT. Each method is allowed to assume that the List invariants are true, as long as it establishes them before exiting. In effect, no other method can “see” the broken invariants. 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. This is clearly Not Good. 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 However, there is an interesting class of concurrent programs referred to as “embarrassingly parallel”. These are programs where there are lots of things that could happen at the same time, without any shared state between them. 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 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]); } In this loop, each iteration could be done concurrently: Each works on a different element. Each execution of the function can be independent. Concurrency Embarrassingly Parallel 6 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. As it happens, a C++ library called Threading Building Blocks (TBB) can do exactly this. 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/6/10 2 Concurrency Parallel For 7 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.

Page1 / 5

25+-+Concurrency - 12/6/10 Concurrency What is it?...

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