[Lazarus] Is there any alternative to PPL (Parallel Programming Library) on FPC?

silvioprog silvioprog at gmail.com
Thu Feb 25 16:38:18 CET 2016


Hello,

Just a quote from Embarcadero doc
<http://docwiki.embarcadero.com/RADStudio/XE8/en/Using_the_Parallel_Programming_Library>
:

The RTL provides the Parallel Programming Library (PPL), giving your
> applications the ability to have tasks running in parallel taking advantage
> of working across multiple CPU devices and computers. The PPL includes a
> number of advanced features for running tasks, joining tasks, waiting on
> groups of tasks, etc. to process. For all this, there is a thread pool that
> self tunes itself automatically (based on the load on the CPU’s) so you do
> not have to care about creating or managing threads for this purpose.


To check it, we can take a small example (full source here
<http://docwiki.embarcadero.com/RADStudio/XE8/en/Tutorial:_Using_the_For_Loop_from_the_Parallel_Programming_Library>)
that looks for prime numbers using a classic for loop:

var
  I, Tot: Integer;
  SW: TStopwatch;
begin
  Tot := 0;
  SW := TStopwatch.Create;
  SW.Start;
  for I := 1 to Max do
    if IsPrime(I) then
      Inc(Tot);
  SW.Stop;
  WriteLn
    (Format('Sequential For loop. Time (in milliseconds): %d - Primes
found: %d',
    [SW.ElapsedMilliseconds, Tot]));
end;

The result:

Sequential For loop. Time (in milliseconds): 764 - Primes found: 348513

Now, a similar example however using PPL:

var
  Tot: Integer;
  SW: TStopwatch;
begin
  try
    Tot := 0;
    SW := TStopwatch.Create;
    SW.Start;
    TParallel.For(2, 1, Max,
      procedure(I: Int64)
      begin
        if IsPrime(I) then
          TInterlocked.Increment(Tot);
      end);
    SW.Stop;
    WriteLn
      (Format('Parallel For loop. Time (in milliseconds): %d - Primes
found: %d',
      [SW.ElapsedMilliseconds, Tot]));
  except
    on E: EAggregateException do
      ShowMessage(E.ToString);
  end;
end;

Result:

Parallel For loop. Time (in milliseconds): 315 - Primes found: 348513

Awesome, I got a nice performance using PPL and it seems a nice feature,
but where can I get anything like this for FPC?

Thank you!

--
Silvio Clécio
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus-ide.org/pipermail/lazarus/attachments/20160225/475dd379/attachment-0002.html>


More information about the Lazarus mailing list