Debian Codegraph @

components/multithreadprocs/examples/parallelloop1.lpr

raw
1 { Example for a parallel loop with MTProcs.
2
3 Copyright (C) 2009 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 ParallelLoop1;
31
32 {$mode objfpc}{$H+}
33
34 uses
35 {$IFDEF UNIX}
36 cthreads, cmem,
37 {$ENDIF}
38 Classes, SysUtils, MTProcs;
39
40 type
41 TFindBestData = record
42 List: TList;
43 Value: Pointer;
44 BlockCount: integer;
45 Results: array of integer;
46 end;
47 PFindBestData = ^TFindBestData;
48
49 procedure FindBestParallel(Index: PtrInt; Data: Pointer; Item: TMultiThreadProcItem);
50 var
51 i: integer;
52 begin
53 with PFindBestData(Data)^ do begin
54 Results[Index]:=-1;
55 i:=Index;
56 while i<List.Count-1 do begin
57 if List[i]=Value then // hier wuerde die teure Vergleichsoperation stehen
58 Results[Index]:=i;
59 inc(i,BlockCount);
60 end;
61 end;
62 end;
63
64 function FindBest(aList: TList; aValue: Pointer): integer;
65 var
66 Index: integer;
67 Data: TFindBestData;
68 begin
69 with Data do begin
70 List:=aList;
71 Value:=aValue;
72 BlockCount:=ProcThreadPool.MaxThreadCount;
73 SetLength(Results,BlockCount);
74 ProcThreadPool.DoParallel(@FindBestParallel,0,BlockCount-1,@Data);
75 // Ergebnisse zusammenfassen
76 Result:=-1;
77 for Index:=0 to BlockCount-1 do
78 if Results[Index]>=0 then
79 Result:=Results[Index];
80 end;
81 end;
82
83 function FindBest1(List: TList; Value: Pointer): integer;
84 var
85 i: integer;
86 begin
87 Result:=-1;
88 i:=0;
89 while i<List.Count do begin
90 if List[i]=Value then // hier wuerde die teure Vergleichsoperation stehen
91 Result:=i;
92 inc(i);
93 end;
94 end;
95
96 var
97 List: TList;
98 i: Integer;
99 begin
100 List:=TList.Create;
101 for i:=0 to 100000000 do
102 List.Add(Pointer(i));
103 i:=FindBest(List,Pointer(9999));
104 //i:=FindBest1(List,Pointer(9999));
105 writeln('i=',i);
106 end.
107