39
No. 1

Sean Kenney - Solving Parallel Software Challenges with Patterns

Embed Size (px)

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.

Citation preview

Page 1: Sean Kenney - Solving Parallel Software Challenges with Patterns

No. 1

Page 2: Sean Kenney - Solving Parallel Software Challenges with Patterns

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

No. 2

Page 3: Sean Kenney - Solving Parallel Software Challenges with Patterns

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

No. 3

Page 4: Sean Kenney - Solving Parallel Software Challenges with Patterns

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

No. 4

Architect Developer Tester

Product

Management

Business

Management

Page 5: Sean Kenney - Solving Parallel Software Challenges with Patterns

© 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

Page 6: Sean Kenney - Solving Parallel Software Challenges with Patterns

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

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

Page 7: Sean Kenney - Solving Parallel Software Challenges with Patterns

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

Page 8: Sean Kenney - Solving Parallel Software Challenges with Patterns

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

Page 9: Sean Kenney - Solving Parallel Software Challenges with Patterns

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

No. 9

Page 10: Sean Kenney - Solving Parallel Software Challenges with Patterns

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

TPL for .net

MPL Express/JFFP

RiverTrail for Javascript

Page 11: Sean Kenney - Solving Parallel Software Challenges with Patterns

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

Decomposition Coordination

Scalable

Sharing

Page 12: Sean Kenney - Solving Parallel Software Challenges with Patterns

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

Page 13: Sean Kenney - Solving Parallel Software Challenges with Patterns

© 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

Page 14: Sean Kenney - Solving Parallel Software Challenges with Patterns

© 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

Page 15: Sean Kenney - Solving Parallel Software Challenges with Patterns

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

Page 16: Sean Kenney - Solving Parallel Software Challenges with Patterns

Parallel Loop

Parallel Tasks

Parallel Aggregation

Futures

Pipelines

Page 17: Sean Kenney - Solving Parallel Software Challenges with Patterns

© 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

Page 18: Sean Kenney - Solving Parallel Software Challenges with Patterns

© 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

Page 19: Sean Kenney - Solving Parallel Software Challenges with Patterns

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

Page 20: Sean Kenney - Solving Parallel Software Challenges with Patterns

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

Page 21: Sean Kenney - Solving Parallel Software Challenges with Patterns

© 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

Page 22: Sean Kenney - Solving Parallel Software Challenges with Patterns

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

Page 23: Sean Kenney - Solving Parallel Software Challenges with Patterns

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

Page 24: Sean Kenney - Solving Parallel Software Challenges with Patterns

© 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

Page 25: Sean Kenney - Solving Parallel Software Challenges with Patterns

© 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

Page 26: Sean Kenney - Solving Parallel Software Challenges with Patterns

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

Page 27: Sean Kenney - Solving Parallel Software Challenges with Patterns

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

Page 28: Sean Kenney - Solving Parallel Software Challenges with Patterns

© 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

Page 29: Sean Kenney - Solving Parallel Software Challenges with Patterns

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

Page 30: Sean Kenney - Solving Parallel Software Challenges with Patterns

© 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

Page 31: Sean Kenney - Solving Parallel Software Challenges with Patterns

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

Page 32: Sean Kenney - Solving Parallel Software Challenges with Patterns

© 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

Page 33: Sean Kenney - Solving Parallel Software Challenges with Patterns

© 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

Page 34: Sean Kenney - Solving Parallel Software Challenges with Patterns

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

No. 34

Page 35: Sean Kenney - Solving Parallel Software Challenges with Patterns

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

Page 36: Sean Kenney - Solving Parallel Software Challenges with Patterns
Page 37: Sean Kenney - Solving Parallel Software Challenges with Patterns

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

Concurrency Visualizer

Debugging

Parallel Stacks Windows

Parallel Tasks Windows

Page 38: Sean Kenney - Solving Parallel Software Challenges with Patterns

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

Page 39: Sean Kenney - Solving Parallel Software Challenges with Patterns

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