25
Parallel Programming Done Right with OTL and PPL Primož Gabrijelčič

Parallel Programming Done Right with OTL and PPL17slon.com/blogs/gabr/presentations/zlot2017/Zlot-Parallel.pdf · Parallel Programming Done Right with OTL and PPL Primož Gabrijelčič

  • Upload
    doannga

  • View
    226

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Parallel Programming Done Right with OTL and PPL17slon.com/blogs/gabr/presentations/zlot2017/Zlot-Parallel.pdf · Parallel Programming Done Right with OTL and PPL Primož Gabrijelčič

Parallel Programming Done Right with OTL and PPL

Primož Gabrijelčič

Page 2: Parallel Programming Done Right with OTL and PPL17slon.com/blogs/gabr/presentations/zlot2017/Zlot-Parallel.pdf · Parallel Programming Done Right with OTL and PPL Primož Gabrijelčič

About me

o Pascal programmer since 1984 (HiSoft pascal on ZX Spectrum)

o First contact with Borland: Turbo Pascal 3 (on CP/M)

o Programming highly responsive 24/7 applications since 1997

o Writer: The Delphi Magazine, Blaise Pascal, Monitor (Slovenia)

o Blogger: http://thedelphigeek.com

o Contact me: http://primoz.gabrijelcic.org

Page 3: Parallel Programming Done Right with OTL and PPL17slon.com/blogs/gabr/presentations/zlot2017/Zlot-Parallel.pdf · Parallel Programming Done Right with OTL and PPL Primož Gabrijelčič

Multithreading

Page 4: Parallel Programming Done Right with OTL and PPL17slon.com/blogs/gabr/presentations/zlot2017/Zlot-Parallel.pdf · Parallel Programming Done Right with OTL and PPL Primož Gabrijelčič

Multithreading is hard

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

- Danny Thorpe

Page 5: Parallel Programming Done Right with OTL and PPL17slon.com/blogs/gabr/presentations/zlot2017/Zlot-Parallel.pdf · Parallel Programming Done Right with OTL and PPL Primož Gabrijelčič

Solution

o Extract all hard parts into a boilerplate code.

o Test it. Test again. Test repeatedly.

o Reuse as much as possible.

o Test again. Don’t stop testing.

– or –

o Use existing library.

o Continue testing.

Page 6: Parallel Programming Done Right with OTL and PPL17slon.com/blogs/gabr/presentations/zlot2017/Zlot-Parallel.pdf · Parallel Programming Done Right with OTL and PPL Primož Gabrijelčič

When to do it?

o Unblocking GUI◦ Long calculations

◦ Synchronous APIs◦ File system

◦ (Serial) communication

o Speeding up the computation◦ Faster calculation

◦ More/less appropriate tasks (algorithms)

◦ Serving more than one client at once

Page 7: Parallel Programming Done Right with OTL and PPL17slon.com/blogs/gabr/presentations/zlot2017/Zlot-Parallel.pdf · Parallel Programming Done Right with OTL and PPL Primož Gabrijelčič

Patterns

Page 8: Parallel Programming Done Right with OTL and PPL17slon.com/blogs/gabr/presentations/zlot2017/Zlot-Parallel.pdf · Parallel Programming Done Right with OTL and PPL Primož Gabrijelčič

Adapt algorithm to the pattern

o Don‘t write the code for your algorithm

o Decompose the algorithm into patterns◦ Use those patterns in the code

o When everything fails, go low-level

o Tasks first, threads last

Page 9: Parallel Programming Done Right with OTL and PPL17slon.com/blogs/gabr/presentations/zlot2017/Zlot-Parallel.pdf · Parallel Programming Done Right with OTL and PPL Primož Gabrijelčič

Frameworks

o PPL◦ Parallel Programming Library

◦ XE7+, all platforms, RTL license

◦ patterns: For, Future, Join

o OTL◦ OmniThreadLibrary

◦ 2009+ (patterns), 2007+ (tasks), Windows (VCL/console/service) only, OpenBSDlicense

◦ patterns: Async[/Await], Background worker, For, Fork/Join, Future, Join, Map, Parallel task, Pipeline

◦ http://www.omnithreadlibrary.com/

Page 10: Parallel Programming Done Right with OTL and PPL17slon.com/blogs/gabr/presentations/zlot2017/Zlot-Parallel.pdf · Parallel Programming Done Right with OTL and PPL Primož Gabrijelčič

Dish of the day

o Async/Await◦ Fire asynchronous tasks

o Future◦ Execute long calculation in background

o For◦ Use all of available CPUs when processing large data

o Map◦ Converting data in parallel

o TimedTask◦ Just like TTimer, but running in a thread

Page 11: Parallel Programming Done Right with OTL and PPL17slon.com/blogs/gabr/presentations/zlot2017/Zlot-Parallel.pdf · Parallel Programming Done Right with OTL and PPL Primož Gabrijelčič

Async/Await

Async(code1).Await(code2)

code 1

code 2

Page 12: Parallel Programming Done Right with OTL and PPL17slon.com/blogs/gabr/presentations/zlot2017/Zlot-Parallel.pdf · Parallel Programming Done Right with OTL and PPL Primož Gabrijelčič

Future

Future(code)

code

.Value

Result

Page 13: Parallel Programming Done Right with OTL and PPL17slon.com/blogs/gabr/presentations/zlot2017/Zlot-Parallel.pdf · Parallel Programming Done Right with OTL and PPL Primož Gabrijelčič

For

For(first, last, code)

code code code

Page 14: Parallel Programming Done Right with OTL and PPL17slon.com/blogs/gabr/presentations/zlot2017/Zlot-Parallel.pdf · Parallel Programming Done Right with OTL and PPL Primož Gabrijelčič

Map

Map(source, mapping)

mapping mapping mapping

source

result

Page 15: Parallel Programming Done Right with OTL and PPL17slon.com/blogs/gabr/presentations/zlot2017/Zlot-Parallel.pdf · Parallel Programming Done Right with OTL and PPL Primož Gabrijelčič

TimedTask

Page 16: Parallel Programming Done Right with OTL and PPL17slon.com/blogs/gabr/presentations/zlot2017/Zlot-Parallel.pdf · Parallel Programming Done Right with OTL and PPL Primož Gabrijelčič

Other OmniThreadLibrarypatterns

Page 17: Parallel Programming Done Right with OTL and PPL17slon.com/blogs/gabr/presentations/zlot2017/Zlot-Parallel.pdf · Parallel Programming Done Right with OTL and PPL Primož Gabrijelčič

ForEach

Page 18: Parallel Programming Done Right with OTL and PPL17slon.com/blogs/gabr/presentations/zlot2017/Zlot-Parallel.pdf · Parallel Programming Done Right with OTL and PPL Primož Gabrijelčič

ParallelTask

Page 19: Parallel Programming Done Right with OTL and PPL17slon.com/blogs/gabr/presentations/zlot2017/Zlot-Parallel.pdf · Parallel Programming Done Right with OTL and PPL Primož Gabrijelčič

Join

Page 20: Parallel Programming Done Right with OTL and PPL17slon.com/blogs/gabr/presentations/zlot2017/Zlot-Parallel.pdf · Parallel Programming Done Right with OTL and PPL Primož Gabrijelčič

Pipeline

Page 21: Parallel Programming Done Right with OTL and PPL17slon.com/blogs/gabr/presentations/zlot2017/Zlot-Parallel.pdf · Parallel Programming Done Right with OTL and PPL Primož Gabrijelčič

Fork/Join

Page 22: Parallel Programming Done Right with OTL and PPL17slon.com/blogs/gabr/presentations/zlot2017/Zlot-Parallel.pdf · Parallel Programming Done Right with OTL and PPL Primož Gabrijelčič

Get more information

o http://www.omnithreadlibrary.com/tutorials.htm

o “Parallel Programming with OmniThreadLibrary”◦ https://leanpub.com/omnithreadlibrary

◦ http://otl.17slon.com/book

Page 23: Parallel Programming Done Right with OTL and PPL17slon.com/blogs/gabr/presentations/zlot2017/Zlot-Parallel.pdf · Parallel Programming Done Right with OTL and PPL Primož Gabrijelčič

Keep in mind

Page 24: Parallel Programming Done Right with OTL and PPL17slon.com/blogs/gabr/presentations/zlot2017/Zlot-Parallel.pdf · Parallel Programming Done Right with OTL and PPL Primož Gabrijelčič

Important Facts We Learned Today

o Don’t write boilerplate code – use patterns

o Be careful when accessing shared data

o Never access the GUI from a background thread!

Page 25: Parallel Programming Done Right with OTL and PPL17slon.com/blogs/gabr/presentations/zlot2017/Zlot-Parallel.pdf · Parallel Programming Done Right with OTL and PPL Primož Gabrijelčič

Q & A