+ All Categories
Home > Software > Deep Dive into Futures and the Parallel Programming Library

Deep Dive into Futures and the Parallel Programming Library

Date post: 15-Jul-2015
Category:
Upload: jim-mckeeth
View: 7,168 times
Download: 0 times
Share this document with a friend
Popular Tags:
22
EMBARCADERO TECHNOLOGIES Deep Dive into Futures and the PPL Jim McKeeth Lead World Wide Developer Evangelist / Engineer [email protected] @JimMcKeeth Tuesday, the 10 rd February, 2015
Transcript
Page 1: Deep Dive into Futures and the Parallel Programming Library

EMBARCADERO  TECHNOLOGIESEMBARCADERO  TECHNOLOGIES

Deep  Dive  into    Futures  and  the  PPLJim  McKeeth  Lead  World  Wide  Developer  Evangelist  /  Engineer  [email protected]  @JimMcKeeth  Tuesday,  the  10rd  February,  2015

Page 2: Deep Dive into Futures and the Parallel Programming Library

EMBARCADERO  TECHNOLOGIES

This  Skill  Sprint  Works  with  .  .  .

Windows  Mac  OS  X  Android  iOS  RAD  Studio  Appmethod  Object  Pascal  C++

• RAD  Studio  XE7  DocWiki  – http://embt.co/latestdocwiki  

http://docwiki.embarcadero.com/RADStudio/en/    

• Appmethod  September  2014  DocWiki  – http://embt.co/AppmethodTopics  

http://docwiki.appmethod.com/appmethod/topics/en/  • Appmethod  supports  the  FireMonkey  

framework  on  all  4  platforms  while  RAD  Studio,  Delphi  and  C++Builder  also  support  VCL  on  Windows.  Contact  sales  with  any  questions!OP

C++

Page 3: Deep Dive into Futures and the Parallel Programming Library

EMBARCADERO  TECHNOLOGIES

The  Parallel  Programming  Library  (PPL)

• Part  of  the  Run-­‐Time  Library  (RTL)  in  System.Threading  • Self-­‐tuning  thread  pool  (based  on  CPU  and  Load)  • Support  for:  • Parallel  For  Loops  • Tasks  -­‐  Units  of  work  • Custom  Thread  Pools  • Futures  -­‐  Async  values

⬅ (Last  week)  ⬅ Part  1  ⬅ Part  2  ⬅ Part  3

Page 4: Deep Dive into Futures and the Parallel Programming Library

EMBARCADERO  TECHNOLOGIES

Last  Week’s  Skill  Sprint

• Parallel  For  Loops  • TInterlocked.Increment  • TThread.Queue  • TThread.Synchronize    • For  more  information:  http://delphi.org/?p=1886  

4

Page 5: Deep Dive into Futures and the Parallel Programming Library

EMBARCADERO  TECHNOLOGIES

5 Tasks - Asynchronous Units of Work

Page 6: Deep Dive into Futures and the Parallel Programming Library

EMBARCADERO  TECHNOLOGIES

The  TTask  Class  for  Asynchronous  Work  

• A  Task  is  a  unit  of  work  to  be  done  • Not  specific  to  an  individual  thread  • Implements  ITask  with  Start,  Wait,  Cancel  and  Status  • Statuses:  Created,  WaitingToRun,  Running,  Completed,  WaitingForChildren,  Canceled,  Exception  

• Provides  WaitForAll  and  WaitForAny  methods

Page 7: Deep Dive into Futures and the Parallel Programming Library

EMBARCADERO  TECHNOLOGIES

WaitForAll  Task  Syntaxvar        tasks:  array  of  ITask;    begin        SetLength  (tasks,  2);            tasks[0]  :=  TTask.Create  (procedure  ()  begin  {Long  running  work}  end);    

   tasks[1]  :=  TTask.Create  (procedure  ()  begin            {  Other  long  running  work  }        end);      tasks[0].Start;        tasks[1].Start;          TTask.WaitForAll(tasks);  //  Blocks  until  completion  end;

OP1  

2  

3  

4  5  

6  7  

9

Page 8: Deep Dive into Futures and the Parallel Programming Library

EMBARCADERO  TECHNOLOGIES

WaitForAll  Task  Syntax{      _di_ITask  tasks[2];            tasks[0]  =  TTask::Create(_di_TProc(new  TCppTask(3000,  Label1)));        tasks[1]  =  TTask::Create(_di_TProc(new  TCppTask(5000,  Label1)));  

     tasks[0]-­‐>Start();        tasks[1]-­‐>Start();            TTask::WaitForAll(tasks,(sizeof(tasks)/sizeof(tasks[0])-­‐1));        ShowMessage("All  done!");  }

C++1  

2  3  

4  5  

6  7

Page 9: Deep Dive into Futures and the Parallel Programming Library

EMBARCADERO  TECHNOLOGIES

9 Customizing the Thread Pool

Page 10: Deep Dive into Futures and the Parallel Programming Library

EMBARCADERO  TECHNOLOGIES

Construct  a  New  Thread  Pool

• Construct  new  TTheadPool  • Customize  Min  &  Max  worker  threads  

• Pass  as  last  optional  parameter  to  Parallel  For,  Task  or  Future  

• Generally  better  off  using  default  thread  pool10

var      FPool  :  TThreadPool;  

//…  

if  FPool  =  nil  then  begin      FPool  :=  TThreadPool.Create;      FPool.SetMaxWorkerThreads(10);  end;  

//…  Use  FPool

Page 11: Deep Dive into Futures and the Parallel Programming Library

EMBARCADERO  TECHNOLOGIES

Customizing  the  Thread  Pool

• Customizing  the  default  is  discouraged:  TThreadPool.Default    • SetMaxWorkerThreads(Value:  Integer):  Boolean;  

• Returns  false  if  attempting  to  set  a  value  smaller  than  the  number  of  processors.  Setting  this  value  too  high  can  lead  problems  with  other  libraries  and  processes.  If  too  many  threads  execute  at  the  same  time,  task/thread  switching  overhead  can  become  restrictive.  

• SetMinWorkerThreads(Value:  Integer):  Boolean;  • Returns  false  if  attempting  to  set  a  value  <  0  or  >  MaxWorkerThreads.  The  actual  number  of  pool  threads  could  be  less  than  this  value  depending  on  actual  demand.  Setting  this  to  too  few  threads  could  be  less  than  optimal  resource  utilization.  

• property  MaxWorkerThreads:  Integer  • property  MinWorkerThreads:  Integer

Page 12: Deep Dive into Futures and the Parallel Programming Library

EMBARCADERO  TECHNOLOGIES

12 Futures - Calculating Values Asynchronously

Page 13: Deep Dive into Futures and the Parallel Programming Library

EMBARCADERO  TECHNOLOGIES

Futures  -­‐  Calculating  Values  Asynchronously

• Future:  Essentially  a  Task  that  returns  a  result  value  • A  future  is  to  a  task  as  a  function  is  to  a  procedure  • Future  is  defined  with  Generic  Type  of  the  result  • Calculates  result  in  the  background  asynchronously  • Will  block  on  read  if  not  finished  • Special  instructions  to  enable  for  C++  (see  download)

Page 14: Deep Dive into Futures and the Parallel Programming Library

EMBARCADERO  TECHNOLOGIES

Future  Syntax

var        FutureString:  IFuture<string>;  //  .  .  .      FutureString  :=  TTask.Future<string>(          function:string          begin              Result  :=  DoRESTCall(editArtist.Text);          end);  //  .  .  .      ResultsMemo.Text  :=  FutureString.Value;

OP1  

2  3  

4  

5

Page 15: Deep Dive into Futures and the Parallel Programming Library

EMBARCADERO  TECHNOLOGIES

DEMONSTRATIONTasks  and  Futures

Page 16: Deep Dive into Futures and the Parallel Programming Library

EMBARCADERO  TECHNOLOGIES

16

Background  by  Tdvance  used  under  CC-­‐BY-­‐SA  2.5  Font  from  http://www.dafont.com/back-­‐to-­‐the-­‐future.font  

Page 17: Deep Dive into Futures and the Parallel Programming Library

EMBARCADERO  TECHNOLOGIES

Review:  Futures  and  the  PPL

• Uses  System.Threading  unit  • Self-­‐tuning  thread  pool  (based  on  CPU  and  Load)  • Tasks  are  asynchronous  units  of  work  • Futures  allow  asynchronous  value  calculation  • Customizable  Thread  Pools

Page 18: Deep Dive into Futures and the Parallel Programming Library

EMBARCADERO  TECHNOLOGIES

PPL  Resources

• Samples  – C:\Users\Public\Documents\Embarcadero\Studio\15.0\Samples\Object  Pascal\RTL\Parallel  Library  – C:\Users\Public\Documents\Embarcadero\Studio\15.0\Samples\CPP\RTL\Parallel  Library  

• DocWiki  Overview  – http://docwiki.embarcadero.com/RADStudio/en/Using_the_Parallel_Programming_Library    

• Blog  Posts  – CodeRage  9  Video  http://dannywind.nl/delphi/coderage9/    – Stephen  Ball’s  series  http://delphiaball.co.uk/tag/parallel-­‐programming/    – Malcolm  Groves’  series  http://www.malcolmgroves.com/blog/?cat=109    – Examples,  links  and  more:  http://delphi.org/?p=1914  (Including  the  C++  Lambda  links)

Special  offers:  http://embarcadero.com/radoffer/  

Page 19: Deep Dive into Futures and the Parallel Programming Library

EMBARCADERO  TECHNOLOGIES

Next  Time….

• FireDAC:  Array  DML  • Submit  a  single  DBMS  command  with  an  array  of  parameters  • Each  command  parameter  has  an  array  of  values  • All  parameters  have  arrays  of  the  same  length  • Thursday  the  12th  of  February  

– 6AM  San  Francisco  /  9AM  New  York  /  2PM  London  /  3PM  Milan  – 11AM  San  Francisco  /  2PM  New  York  /  7PM  London  /  8PM  Milan  – 5PM  San  Francisco  /  Fri  9AM  Tokyo  /  Fri  10AM  Sydney

Sign-­‐up:  http://www.embarcadero.com/landing-­‐pages/skill-­‐sprints  2nd  Reg  Form

Page 20: Deep Dive into Futures and the Parallel Programming Library

EMBARCADERO  TECHNOLOGIES

• Separating  App  Logic  from  the  UI    • Promotes  Healthy  Apps  • How  to  use  Common  Patterns  • Tuesday  the  17th  of  February  

– 6AM  San  Francisco  /  9AM  New  York  /  2PM  London  /  3PM  Milan  – 11AM  San  Francisco  /  2PM  New  York  /  7PM  London  /  8PM  Milan  – 5PM  San  Francisco  /  Wed  9AM  Tokyo  /  Wed  10AM  Sydney

Next  Time….

Special  offers:  http://embarcadero.com/radoffer/  

Page 21: Deep Dive into Futures and the Parallel Programming Library

EMBARCADERO  TECHNOLOGIES

RAD  Studio  XE7  Special  Offers

More  details  http://www.embarcadero.com/radoffer

Save  45%  when  upgrading  from  any  previous  version  

When  you  purchase  the  upgrade  with  1  year  Support  and  

Maintenance!

Build  a  Mobile  Platform  for  the  Enterprise  

With  Enterprise  edition  or  higher.

Powerful  Tools  to  Boost  Your  Coding  (Worth  Over  $700)  

• New  Object  Pascal  Handbook  by  Marco  Cantu    

• Castalia  for  Delphi  2014.11  • VCL  and  FireMonkey  Premium  Styles  • Mida  Converter  Basic

Page 22: Deep Dive into Futures and the Parallel Programming Library

EMBARCADERO  TECHNOLOGIESEMBARCADERO  TECHNOLOGIES

Q  &  A  @EmbarcaderoTech

Special  offers:  http://embarcadero.com/radoffer/  


Recommended