Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / lazarus / 2016-02

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

3 messages · 2 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

silvioprog

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

Show quoted text 6 lines
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.freepascal.org/pipermail/lazarus/attachments/20160225/475dd379/attachment.html>

Reply by email

Kostas Michalopoulos
to silvioprog

Check the MTProcs unit in the multithreadprocs package (part of Lazarus, but you need to manually install it from the Install/Uninstall Packages box). This wiki page describes how to use it:

http://wiki.freepascal.org/Parallel_procedures

silvioprog wrote 86 lines
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 -- _______________________________________________ Lazarus mailing list http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20160226/bf40a327/attachment-0001.html>

Reply by email

silvioprog
to Kostas Michalopoulos

On Thu, Feb 25, 2016 at 9:55 PM, Kostas Michalopoulos <

Show quoted text 8 lines
wrote: Check the MTProcs unit in the multithreadprocs package (part of Lazarus, but you need to manually install it from the Install/Uninstall Packages box). This wiki page describes how to use it: http://wiki.freepascal.org/Parallel_procedures

Awesome!

I didn't know that this package existed. I'll test it tonight.

Thanks a lot for share this link! :-)

Silvio Cl?cio -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20160226/569abdf4/attachment.html>

Reply by email