| 1 |
program RecursiveMTP1; |
| 2 |
|
| 3 |
{$mode objfpc}{$H+} |
| 4 |
|
| 5 |
uses |
| 6 |
{$IFDEF UNIX} |
| 7 |
cthreads, cmem, |
| 8 |
{$ENDIF} |
| 9 |
MTProcs; |
| 10 |
|
| 11 |
type |
| 12 |
TArrayOfInteger = array of integer; |
| 13 |
var |
| 14 |
Items: TArrayOfInteger; |
| 15 |
|
| 16 |
type |
| 17 |
TFindMaximumParallelData = record |
| 18 |
Items: TArrayOfInteger; |
| 19 |
Left, Middle, Right: integer; |
| 20 |
LeftMaxIndex, RightMaxIndex: integer; |
| 21 |
end; |
| 22 |
PFindMaximumParallelData = ^TFindMaximumParallelData; |
| 23 |
|
| 24 |
procedure FindMaximumParallel(Index: PtrInt; Data: Pointer; Item: TMultiThreadProcItem); |
| 25 |
var |
| 26 |
Params: PFindMaximumParallelData absolute Data; |
| 27 |
LeftParams, RightParams: TFindMaximumParallelData; |
| 28 |
begin |
| 29 |
if Params^.Left+1000>Params^.Right then begin |
| 30 |
// compute the maximum of the few remaining items |
| 31 |
Params^.LeftMaxIndex:=Params^.Items[Params^.Left]; |
| 32 |
for i:=Params^.Left+1 to Params^.Right do |
| 33 |
if Params^.Items[i]>Params^.LeftMaxIndex then |
| 34 |
end else begin |
| 35 |
|
| 36 |
end; |
| 37 |
end; |
| 38 |
|
| 39 |
function FindMaximumIndex(Items: TArrayOfInteger): integer; |
| 40 |
begin |
| 41 |
|
| 42 |
end; |
| 43 |
|
| 44 |
begin |
| 45 |
SetLength(Items,10000000); |
| 46 |
for i:=0 to length(Items)-1 do Items[i]:=Random(1000); |
| 47 |
ProcThreadPool.DoParallel(@DoSomethingParallel,1,5,nil); // address, startindex, endindex, optional data |
| 48 |
end. |
| 49 |
|