Debian Codegraph @

components/multithreadprocs/examples/parallelloop_nested1.lpr

raw
1 { Example for a parallel loop with MTProcs.
2
3 Copyright (C) 2017 Mattias Gaertner [email protected]
4
5 This library is free software; you can redistribute it and/or modify it
6 under the terms of the GNU Library General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or (at your
8 option) any later version with the following modification:
9
10 As a special exception, the copyright holders of this library give you
11 permission to link this library with independent modules to produce an
12 executable, regardless of the license terms of these independent modules,and
13 to copy and distribute the resulting executable under terms of your choice,
14 provided that you also meet, for each linked independent module, the terms
15 and conditions of the license of that module. An independent module is a
16 module which is not derived from or based on this library. If you modify
17 this library, you may extend this exception to your version of the library,
18 but you are not obligated to do so. If you do not wish to do so, delete this
19 exception statement from your version.
20
21 This program is distributed in the hope that it will be useful, but WITHOUT
22 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
23 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License
24 for more details.
25
26 You should have received a copy of the GNU Library General Public License
27 along with this library; if not, write to the Free Software Foundation,
28 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
29 }
30 program parallelloop_nested1;
31
32 {$mode objfpc}{$H+}
33 {$ModeSwitch nestedprocvars}
34
35 uses
36 {$IFDEF UNIX}
37 cthreads, cmem,
38 {$ENDIF}
39 Classes, SysUtils, Math, MTProcs;
40
41
42 function FindBestParallel(aList: TList; aValue: Pointer): integer;
43 var
44 BlockSize: PtrInt;
45 Results: array of integer;
46
47 procedure InParallel(Index: PtrInt; Data: Pointer; Item: TMultiThreadProcItem);
48 var
49 i, StartIndex, EndIndex: PtrInt;
50 begin
51 Results[Index]:=-1;
52 StartIndex:=Index*BlockSize;
53 EndIndex:=Min(StartIndex+BlockSize,aList.Count);
54 //if MainThreadID=GetCurrentThreadId then
55 // writeln('FindBestParallel Index=',Index,' StartIndex=',StartIndex,' EndIndex=',EndIndex);
56 for i:=StartIndex to EndIndex-1 do begin
57 if aList[i]=aValue then // imagine here an expensive compare function
58 Results[Index]:=i;
59 end;
60 end;
61
62 var
63 Index: integer;
64 BlockCount: PtrInt;
65 begin
66 ProcThreadPool.CalcBlockSize(aList.Count,BlockCount,BlockSize);
67 SetLength(Results,BlockCount);
68 //writeln('FindBestParallel BlockCount=',BlockCount,' List.Count=',aList.Count,' BlockSize=',BlockSize);
69 ProcThreadPool.DoParallelNested(@InParallel,0,BlockCount-1);
70 // collect results
71 Result:=-1;
72 for Index:=0 to BlockCount-1 do
73 if Results[Index]>=0 then
74 Result:=Results[Index];
75 end;
76
77 function FindBestSingleThreaded(List: TList; Value: Pointer): integer;
78 var
79 i: integer;
80 begin
81 Result:=-1;
82 i:=0;
83 while i<List.Count do begin
84 if List[i]=Value then // imagine here an expensive compare function
85 Result:=i;
86 inc(i);
87 end;
88 end;
89
90 var
91 List: TList;
92 i: Integer;
93 begin
94 List:=TList.Create;
95 for i:=0 to 100000000 do
96 List.Add(Pointer(i));
97 writeln('searching ...');
98 i:=FindBestParallel(List,Pointer(List.Count-2));
99 writeln('parallel search i=',i);
100 i:=FindBestSingleThreaded(List,Pointer(List.Count-2));
101 writeln('linear search i=',i);
102 end.
103