parallel programming done right with otl and...
TRANSCRIPT
![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č](https://reader031.vdocuments.us/reader031/viewer/2022021823/5b34f5287f8b9aec518cc22c/html5/thumbnails/1.jpg)
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č](https://reader031.vdocuments.us/reader031/viewer/2022021823/5b34f5287f8b9aec518cc22c/html5/thumbnails/2.jpg)
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č](https://reader031.vdocuments.us/reader031/viewer/2022021823/5b34f5287f8b9aec518cc22c/html5/thumbnails/3.jpg)
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č](https://reader031.vdocuments.us/reader031/viewer/2022021823/5b34f5287f8b9aec518cc22c/html5/thumbnails/4.jpg)
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č](https://reader031.vdocuments.us/reader031/viewer/2022021823/5b34f5287f8b9aec518cc22c/html5/thumbnails/5.jpg)
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č](https://reader031.vdocuments.us/reader031/viewer/2022021823/5b34f5287f8b9aec518cc22c/html5/thumbnails/6.jpg)
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č](https://reader031.vdocuments.us/reader031/viewer/2022021823/5b34f5287f8b9aec518cc22c/html5/thumbnails/7.jpg)
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č](https://reader031.vdocuments.us/reader031/viewer/2022021823/5b34f5287f8b9aec518cc22c/html5/thumbnails/8.jpg)
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č](https://reader031.vdocuments.us/reader031/viewer/2022021823/5b34f5287f8b9aec518cc22c/html5/thumbnails/9.jpg)
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č](https://reader031.vdocuments.us/reader031/viewer/2022021823/5b34f5287f8b9aec518cc22c/html5/thumbnails/10.jpg)
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č](https://reader031.vdocuments.us/reader031/viewer/2022021823/5b34f5287f8b9aec518cc22c/html5/thumbnails/11.jpg)
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č](https://reader031.vdocuments.us/reader031/viewer/2022021823/5b34f5287f8b9aec518cc22c/html5/thumbnails/12.jpg)
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č](https://reader031.vdocuments.us/reader031/viewer/2022021823/5b34f5287f8b9aec518cc22c/html5/thumbnails/13.jpg)
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č](https://reader031.vdocuments.us/reader031/viewer/2022021823/5b34f5287f8b9aec518cc22c/html5/thumbnails/14.jpg)
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č](https://reader031.vdocuments.us/reader031/viewer/2022021823/5b34f5287f8b9aec518cc22c/html5/thumbnails/15.jpg)
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č](https://reader031.vdocuments.us/reader031/viewer/2022021823/5b34f5287f8b9aec518cc22c/html5/thumbnails/16.jpg)
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č](https://reader031.vdocuments.us/reader031/viewer/2022021823/5b34f5287f8b9aec518cc22c/html5/thumbnails/17.jpg)
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č](https://reader031.vdocuments.us/reader031/viewer/2022021823/5b34f5287f8b9aec518cc22c/html5/thumbnails/18.jpg)
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č](https://reader031.vdocuments.us/reader031/viewer/2022021823/5b34f5287f8b9aec518cc22c/html5/thumbnails/19.jpg)
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č](https://reader031.vdocuments.us/reader031/viewer/2022021823/5b34f5287f8b9aec518cc22c/html5/thumbnails/20.jpg)
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č](https://reader031.vdocuments.us/reader031/viewer/2022021823/5b34f5287f8b9aec518cc22c/html5/thumbnails/21.jpg)
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č](https://reader031.vdocuments.us/reader031/viewer/2022021823/5b34f5287f8b9aec518cc22c/html5/thumbnails/22.jpg)
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č](https://reader031.vdocuments.us/reader031/viewer/2022021823/5b34f5287f8b9aec518cc22c/html5/thumbnails/23.jpg)
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č](https://reader031.vdocuments.us/reader031/viewer/2022021823/5b34f5287f8b9aec518cc22c/html5/thumbnails/24.jpg)
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č](https://reader031.vdocuments.us/reader031/viewer/2022021823/5b34f5287f8b9aec518cc22c/html5/thumbnails/25.jpg)
Q & A