Parallel procedures

This is an old revision of Parallel procedures, edited by Mattias2 at . The current revision is 57076.

/* Overhead, slow down */

Overview

This page describes how to run single procedures in parallel using the MTProcs unit. Parallel procedures and methods are often found in parallel algorithms and some languages provide built-in support for them (e.g. OpenMP in gcc). See here for the plans adding such language features to FPC. This page describes parallel procedures using the unit MTProcs, which simplifies running procedures in parallel.

First example

Here is a short example:

<Delphi> program Test;

{$mode objfpc}{$H+}

uses

 {$IFDEF UNIX}
 cthreads, cmem,
 {$ENDIF}
 MTProcs;

// a simple parallel procedure procedure DoSomethingParallel(Index: PtrInt; Data: Pointer; Item: TMultiThreadProcItem); var

 i: Integer;

begin

 writeln(Index);
 for i:=1 to Index*1000000 do ; // do some work

end;

begin

 ProcThreadPool.DoParallel(@DoSomethingParallel,1,10,nil); // address, startindex, endindex, optional data

end. </Delphi>

The output will be something like this:

2
3
1
4
5

Features

Running a procedure in parallel means:

 ProcThreadPool.MaxThreadCount:=8;

Overhead, slow down

The overhead heavily depends on the system (number and types of cores, type of shared memory, speed of critical sections, cache size). Here are some general hints:

Multi threading overhead, which is independent of the MTProcs units, but simply results from todays computer architectures:

Setting the maximum number of threads for a procedure

You can specify the maximum number of threads for a procedure as the fifth parameter.

<Delphi> begin

 ProcThreadPool.DoParallel(@DoSomethingParallel,1,10,nil,2); // address, startindex, endindex, 
     // optional: data (here: nil), optional: maximum number of threads (here: 2)

end. </Delphi>

This can be useful, when the threads work on the same data, and a too many threads will create so many cache conflicts, that they slow down each other.

Exceptions

If an exception occur in one of the threads, the other threads will finish normally, but will not start a new Index. The pool waits for all threads to finish and will then raise the exception. That's why you can use try..except like always:

<Delphi> try

 ...
 ProcThreadPool.DoParallel(...);
 ...

except

 On E: Exception do ...

end; </Delphi>

If there are multiple exceptions, only the first exception will be raised. To handle all exceptions, add a try..except inside your parallel method.