simplify parallel programming with patterns primož gabrijelčič r&d manager, fab

13
Simplify Parallel Programming with Patterns Primož Gabrijelčič R&D Manager, FAB

Upload: isabel-skinner

Post on 19-Jan-2016

216 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Simplify Parallel Programming with Patterns Primož Gabrijelčič R&D Manager, FAB

Simplify Parallel Programming with Patterns

Primož GabrijelčičR&D Manager, FAB

Page 2: Simplify Parallel Programming with Patterns Primož Gabrijelčič R&D Manager, FAB

Multithreading is Hard

2

“New programmers are drawn to multithreading like moths to flame, with similar results.”

- Danny Thorpe

Page 3: Simplify Parallel Programming with Patterns Primož Gabrijelčič R&D Manager, FAB

Solution

• Extract all hard parts into a boilerplate code.• Test it. Test again. Test repeatedly.• Reuse as much as possible.• Test again. Don’t stop testing.

– or –

• Use existing library.

3

Page 4: Simplify Parallel Programming with Patterns Primož Gabrijelčič R&D Manager, FAB

Bugs Away!

4

“The code that you don’t write,contains no bugs.”

- Primož Gabrijelčič

Page 5: Simplify Parallel Programming with Patterns Primož Gabrijelčič R&D Manager, FAB

Adapt the Algorithm to the Pattern

• don‘t write the code for your algorithm• decompose the algorithm into the patterns

• when everything fails, go low-level• tasks first [CodeRage 9: Parallel Programming Library]• threads last

5

Page 6: Simplify Parallel Programming with Patterns Primož Gabrijelčič R&D Manager, FAB

Frameworks

• PPL• Parallel Programming Library• XE7+, all platforms, RTL license• patterns: For, Future, Join

• OTL• OmniThreadLibrary• 2009+ (patterns), 2007+ (tasks), Windows (VCL/console/service)

only (but working on that), OpenBSD license• patterns: Async[/Await], Background worker, For, Fork/Join, Future,

Join, Map, Parallel task, Pipeline

6

Page 7: Simplify Parallel Programming with Patterns Primož Gabrijelčič R&D Manager, FAB

Patterns

• Async/Await• Fire asynchronous tasks

• Future• Execute long calculation in background

https://en.wikipedia.org/wiki/Futures_and_promises

• For• Use all of available CPUs when processing large data

https://en.wikipedia.org/wiki/Embarrassingly_parallel

• Map• Converting data in parallel

7

Page 8: Simplify Parallel Programming with Patterns Primož Gabrijelčič R&D Manager, FAB

Async/Await

8

Async(code1).Await(code2)

code 1

code 2

Page 9: Simplify Parallel Programming with Patterns Primož Gabrijelčič R&D Manager, FAB

Future

9

Future(code)

code

.Value

Result

Page 10: Simplify Parallel Programming with Patterns Primož Gabrijelčič R&D Manager, FAB

For

10

For(first, last, code)

code code code

Page 11: Simplify Parallel Programming with Patterns Primož Gabrijelčič R&D Manager, FAB

Map

11

Map(source, mapping)

mapping mapping mapping

source

result

Page 12: Simplify Parallel Programming with Patterns Primož Gabrijelčič R&D Manager, FAB

Important Facts We Learned Today

• Don’t write boilerplate code – use patterns

• Be careful when accessing shared data

• Never access the GUI from a background thread!

12

Page 13: Simplify Parallel Programming with Patterns Primož Gabrijelčič R&D Manager, FAB

Q&A

Primož Gabrijelčič

blog www.thedelphigeekinfo primoz.gabrijelcic.orgemail [email protected]@thedelphigeekskype gabr42

13