Recent

Author Topic: Parallel procedures MTProcs package improvement?  (Read 2459 times)

heju

  • New member
  • *
  • Posts: 7
Parallel procedures MTProcs package improvement?
« on: October 28, 2016, 11:52:00 pm »
Hi all,

I'm quite new to fpc, but having already a lot of fun :)

Currently I am working on a data processing tool. The required algos are really simple to spread to several threads as nothing depends on each other, but each calculation portion is time consuming.

I got the MTProcs package (http://wiki.freepascal.org/Parallel_procedures) running, which does a perfect job.
Especially the "experimental" feature to parallelize nested procedures is really helpfull
Currently I am using "DoParallelLocalProc" in the following form:

Code: Pascal  [Select][+][-]
  1. procedure main();
  2.  
  3.   procedure DoSomethingParallel(Index: PtrInt; Data: Pointer; Item: TMultiThreadProcItem);
  4.   var
  5.     i: Integer;
  6.   begin
  7.     writeln(Index);
  8.     for i:=1 to Index*1000000000 do ; // do some work
  9.     writeln('done')
  10.   end;
  11.  
  12. begin
  13.   ProcThreadPool.DoParallelLocalProc(@DoSomethingParallel,1,5,nil); // address, startindex, endindex, optional data
  14. end;
  15.  
  16.  
  17. begin
  18.   main();
  19.   readln;
  20. end.}
  21.  

However the wiki page warns: "Beware that calling nested procedures is not yet supported by the compiler and therefore one has to use unsafe type casts."

I am wondering: This info is not up to date anymore:  nested proc vars are available and will help to overcome this unsafe call!
To test this proposal i set this type def in MTProcs.pas as is nested:
Code: Pascal  [Select][+][-]
  1. TMTProcedure = procedure(Index: PtrInt; Data: Pointer;
  2.                            Item: TMultiThreadProcItem) is nested;
  3.  


Now the user can use "DoParallel" for both local procedures and normal procedures, without unsafe calls!

Code: Pascal  [Select][+][-]
  1. program partest;
  2.  
  3. {$mode objfpc}{$H+}
  4. {$modeswitch nestedprocvars}
  5. uses
  6.   {$IFDEF UNIX}
  7.   cthreads, cmem,
  8.   {$ENDIF}
  9.   MTProcs;
  10.  
  11. // a simple parallel procedure
  12.  
  13. procedure main();
  14.  
  15.   procedure DoSomethingParallel(Index: PtrInt; Data: Pointer; Item: TMultiThreadProcItem);
  16.   var
  17.     i: Integer;
  18.   begin
  19.     writeln(Index);
  20.     for i:=1 to Index*1000000000 do ; // do some work
  21.     writeln('done')
  22.   end;
  23.  
  24. begin
  25.   ProcThreadPool.DoParallel(@DoSomethingParallel,1,5,nil); // address, startindex, endindex, optional data
  26. end;
  27.  
  28.  
  29. begin
  30.   main();
  31.   readln;
  32. end.
  33.  

The original wiki testprog is working as well without change.

What do you (more experienced users) say, can this be integrated to the official MTProcs package?


Thanks for reading ;)
 
« Last Edit: October 28, 2016, 11:53:46 pm by heju »

 

TinyPortal © 2005-2018