Top Banner
Parallel Programming Made Easy Primož Gabrijelčič, [email protected] www.thedelphigeek.com
16
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Parallel Programming Made Easy Primož Gabrijelčič, primoz@gabrijelcic.org .

Parallel Programming Made Easy

Primož Gabrijelčič, [email protected]

Page 2: Parallel Programming Made Easy Primož Gabrijelčič, primoz@gabrijelcic.org .

Why and How

Page 3: Parallel Programming Made Easy Primož Gabrijelčič, primoz@gabrijelcic.org .

The End of Free Lunch

© Herb Sutter, www.gotw.ca/publications/concurrency-ddj.htm

What we want

What we have

Page 4: Parallel Programming Made Easy Primož Gabrijelčič, primoz@gabrijelcic.org .

Three Paths …

• The Delphi Way– TMyThread = class(TThread)

• The Windows Way– FHandle := BeginThread(nil, 0, @ThreadProc, Pointer(Self), 0, FThreadID);

• The Lightweight Way (AsyncCalls)– TAsyncCalls.Invoke(procedure begin DoTheCalculation;end);

– andy.jgknet.de

Page 5: Parallel Programming Made Easy Primož Gabrijelčič, primoz@gabrijelcic.org .

… Plus One

• The No-Fuss Way (OmniThreadLibrary)– thread := TOmniFuture<integer>.Create(YourFunction);// do some workShow(thread.Value);

– otl.17slon.com– code.google.com/p/omnithreadlibrary– ... D2007/2009⇨ only

Page 6: Parallel Programming Made Easy Primož Gabrijelčič, primoz@gabrijelcic.org .

When To Use

• Slow background process• Background communication• Executing synchronous API• Multicore data processing• Multiple clients

Page 7: Parallel Programming Made Easy Primož Gabrijelčič, primoz@gabrijelcic.org .

OmniThreadLibrary

Page 8: Parallel Programming Made Easy Primož Gabrijelčič, primoz@gabrijelcic.org .

OmniThreadLibrary is …

• … VCL for multithreading– Simplifies programming tasks– Componentizes solutions– Allows access to the bare metal

• … trying to make multithreading possible for mere mortals

• … providing well-tested components packed in reusable classes with high-level parallel programming support

Page 9: Parallel Programming Made Easy Primož Gabrijelčič, primoz@gabrijelcic.org .

Today‘s Topic

• Futures• Join• Parallel for

Page 10: Parallel Programming Made Easy Primož Gabrijelčič, primoz@gabrijelcic.org .

Futures

• Wikipedia– “They (futures) describe an object

that acts as a proxy for a result that is initially not known, usually because the computation of its value has not yet completed.”

– Start background calculation, wait on result.

• How to use?– Future := TOmniFuture<type>.Create(calculation);

– Query Future.Value;

Page 11: Parallel Programming Made Easy Primož Gabrijelčič, primoz@gabrijelcic.org .

Join

• “Fork and Wait”• Similar to Future– Start multiple background calculations– Wait for all to complete– No result is returned (directly)

• Two basic forms– Join(task1, task2);– Join([task1, task2, task3, … taskN]);

Page 12: Parallel Programming Made Easy Primož Gabrijelčič, primoz@gabrijelcic.org .

Parallel For

• A simple façade for terriblycomplicated stuff

Parallel .ForEach(1, CMaxSGPrimeTest) .Execute( procedure (const value: integer) begin if IsPrime(value) then numPrimes.Increment; end);

Page 13: Parallel Programming Made Easy Primož Gabrijelčič, primoz@gabrijelcic.org .

Parting Notes

Page 14: Parallel Programming Made Easy Primož Gabrijelčič, primoz@gabrijelcic.org .

Be Afraid

• Be very afraid!

• Designing parallel solutions is hard• Writing multithreaded code is hard• Testing multicore apps is hard• Debugging multithreading code is

pure insanity

Page 15: Parallel Programming Made Easy Primož Gabrijelčič, primoz@gabrijelcic.org .

Keep in Mind

• Don’t parallelize everything• Don’t create thousands of threads• Rethink the algorithm• Prove the improvements• Test, test and test

Page 16: Parallel Programming Made Easy Primož Gabrijelčič, primoz@gabrijelcic.org .

Q & A