Sean Kenney - Solving Parallel Software Challenges with Patterns

Post on 02-Dec-2014

164 views 1 download

description

Sean will talk about the various parallel patterns and specifically how they can be used to solve specific parallel design challenges. This session will focus on 6 patterns that attendees will need to understand in order to architect and develop software that leverages parallel programming.

Transcript of Sean Kenney - Solving Parallel Software Challenges with Patterns

No. 1

© Copyright 2012 Avanade Inc. All Rights Reserved. Confidential For Internal Use Only

No. 2

© Copyright 2012 Avanade Inc. All Rights Reserved. Confidential For Internal Use Only

No. 3

© Copyright 2012 Avanade Inc. All Rights Reserved. Confidential For Internal Use Only

No. 4

Architect Developer Tester

Product

Management

Business

Management

© Copyright 2012 Avanade Inc. All Rights Reserved. Confidential For Internal Use Only

No. 5

Most servers

today both have

multi-core and

multi-processor

architecture

You should not

rely on the OS to

do parallel

programming

Parallel

programming is

just not for Super

Geeks any more

(Driver

developers, OS

developers, C++

guys)

All verticals are

starting to

want/need

parallel

programming

Parallel

programming can

be leveraged well

in some cloud

scenarios

© Copyright 2012 Avanade Inc. All Rights Reserved. Confidential For Internal Use Only

Beijing National Stadium - a.k.a “Bird’s Nest”

© Copyright 2012 Avanade Inc. All Rights Reserved. Confidential For Internal Use Only

© Copyright 2012 Avanade Inc. All Rights Reserved. Confidential For Internal Use Only

Concurrency

• GOAL: Prevent thread starvation

• concept related to multitasking and asynchronous input-output (I/O)

• existence of multiple threads of execution that may each get a slice of

time to execute before being preempted by another thread

Parallelism

• GOAL: Maximize processor usage across all available cores

• concurrent threads execute at the same time across cores

• focuses on improving the performance of applications that use a lot

of processor power and are not constantly interrupted when multiple

cores are available.

© Copyright 2012 Avanade Inc. All Rights Reserved. Confidential For Internal Use Only

No. 9

© Copyright 2012 Avanade Inc. All Rights Reserved. Confidential For Internal Use Only

TPL for .net

MPL Express/JFFP

RiverTrail for Javascript

© Copyright 2012 Avanade Inc. All Rights Reserved. Confidential For Internal Use Only

Decomposition Coordination

Scalable

Sharing

© Copyright 2012 Avanade Inc. All Rights Reserved. Confidential For Internal Use Only

No. 12

• Too fine = overhead to manage will become to much

• Too course = parallel opportunities will be lost

Identify tasks at a level of granularity that results in

efficient use of hardware resources.

• They should remain independent of each other, and have enough tasks to

keep the cores busy

Tasks should be as large as possible

• Dedicate some time to understand these components.

Decomposing a problem into tasks requires a good

understanding of the algorithmic and structural aspects

of your application.

© Copyright 2012 Avanade Inc. All Rights Reserved. Confidential For Internal Use Only

Sean’s General Rule of thumb: If iteration takes longer than 1 minute, review further.

Change the address of a user

© Copyright 2012 Avanade Inc. All Rights Reserved. Confidential For Internal Use Only

No. 14

Coordination depends on

specifically which parallel

patterns you use to

implement

Application algorithms are

constrained by order of

execution and degree of

parallelism

• Constraints can come from data

flow or control flow.

The Futures pattern uses

Continuation to manage

coordination.

• Make sure that you understand any

coordination, before modifying you

application.

Mapping out dependencies

in a graph or inheritance

tree helps truly understand

the landscape

© Copyright 2012 Avanade Inc. All Rights Reserved. Confidential For Internal Use Only

No. 15

Limit your

shared

variables

Use immutable

data when you

can Introduce new steps in

your algorithm that merge

local versions of mutable

state at checkpoints

Adding synchronization

reduces the parallelism of

your application.

Parallel Loop

Parallel Tasks

Parallel Aggregation

Futures

Pipelines

© Copyright 2012 Avanade Inc. All Rights Reserved. Confidential For Internal Use Only

No. 17

Do you have sequential loops where there's no

communication among the steps of each

iteration?

Use the Parallel Loop pattern

© Copyright 2012 Avanade Inc. All Rights Reserved. Confidential For Internal Use Only

Parallel loops apply an independent operation to

multiple inputs simultaneously.

Very similar to for and foreach.

Sequence in the collection will not matter.

Do not replace all for and for each loops with the

parallel equivalent. You will get into trouble

© Copyright 2012 Avanade Inc. All Rights Reserved. Confidential For Internal Use Only

© Copyright 2012 Avanade Inc. All Rights Reserved. Confidential For Internal Use Only

No – array cannot be divided into parts that can be sorted independently.

No, because the sum of the entire collection is needed, not the sums of separate parts.

Yes, because each slide can be considered independently.

© Copyright 2012 Avanade Inc. All Rights Reserved. Confidential For Internal Use Only

No. 21

Do you have distinct operations with well-

defined control dependencies and are these

operations largely free of serializing

dependencies?

Use the Parallel Task pattern

© Copyright 2012 Avanade Inc. All Rights Reserved. Confidential For Internal Use Only

• Sometimes referred to as Fork/Join pattern or the

Master/Worker pattern.

Parallel Tasks allow you to establish parallel

control flow in the style of fork and join.

• Don’t assume that all parallel tasks will immediately run. That is

up to the scheduler.

You can wait for a single task or multiple tasks.

© Copyright 2012 Avanade Inc. All Rights Reserved. Confidential For Internal Use Only

© Copyright 2012 Avanade Inc. All Rights Reserved. Confidential For Internal Use Only

No. 24

Do you need to summarize data by applying some

kind of combination operator? Do you have loops

with steps that are not fully independent?

Use the Parallel Aggregation pattern

© Copyright 2012 Avanade Inc. All Rights Reserved. Confidential For Internal Use Only

Introduces special steps in the algorithm for

merging partial results.

This pattern expresses a reduction operation and

includes map/reduce as one of its variations

Uses unshared, local variables that are merged at

the end of the computation to give the final

result

a.k.a. as The Parallel Reduction pattern because it

combines multiple inputs into a single output

© Copyright 2012 Avanade Inc. All Rights Reserved. Confidential For Internal Use Only

© Copyright 2012 Avanade Inc. All Rights Reserved. Confidential For Internal Use Only

© Copyright 2012 Avanade Inc. All Rights Reserved. Confidential For Internal Use Only

No. 28

Does the ordering of steps in your algorithm

depend on data flow constraints?

Use the Futures pattern

© Copyright 2012 Avanade Inc. All Rights Reserved. Confidential For Internal Use Only

Makes the data flow dependencies between tasks

explicit.

A future is a stand-in for a computational result

that is initially unknown but becomes known

The Futures pattern integrates task parallelism

with the familiar world of arguments and return

values

If a task in the chain is depending on another to

finish, it will block. The core will be available for

other tasks.

a.k.a Task Graph pattern.

© Copyright 2012 Avanade Inc. All Rights Reserved. Confidential For Internal Use Only

No. 30

F1 F2

F3

F4

F5

F6

F1

F2

F3

F4

F5

F6

Method Chain Method Chain using Futures

© Copyright 2012 Avanade Inc. All Rights Reserved. Confidential For Internal Use Only

© Copyright 2012 Avanade Inc. All Rights Reserved. Confidential For Internal Use Only

No. 32

Does your application perform a sequence of

operations repetitively? Does the order of

processing matter?

Use the Pipeline pattern

© Copyright 2012 Avanade Inc. All Rights Reserved. Confidential For Internal Use Only

Pipelines consist of components that are

connected by queues, in the style of producers

and consumers.

All the components run in parallel even though

the order of inputs is respected.

Analogous to assembly lines in a factory

Pipelines allow you to use parallelism in cases

where there are too many dependencies to use a

parallel loop

© Copyright 2012 Avanade Inc. All Rights Reserved. Confidential For Internal Use Only

No. 34

© Copyright 2012 Avanade Inc. All Rights Reserved. Confidential For Internal Use Only

© Copyright 2012 Avanade Inc. All Rights Reserved. Confidential For Internal Use Only

Concurrency Visualizer

Debugging

Parallel Stacks Windows

Parallel Tasks Windows

© Copyright 2012 Avanade Inc. All Rights Reserved. Confidential For Internal Use Only

• Parallel Programming is expected in most software

• Clearly understand the core design aspects • Decomposition

• Coordination

• Scalable Sharing

• Get to know the 5 key parallel patterns • Parallel Loop

• Parallel Tasks

• Parallel Aggregation

• Futures

• Pipelines

• Leverage industry tooling to make you experience

easier.

© Copyright 2012 Avanade Inc. All Rights Reserved. Confidential For Internal Use Only