Debian Codegraph @

components/multithreadprocs/mtputils.pas

raw
1 {
2 **********************************************************************
3 This file is part of the Free Pascal run time library.
4
5 See the file COPYING.FPC, included in this distribution,
6 for details about the license.
7 **********************************************************************
8
9 Utilities using light weight threads.
10
11 Copyright (C) 2008 Mattias Gaertner [email protected]
12
13 Abstract:
14 Utility functions using mtprocs.
15 For example a parallel sort.
16
17 }
18 unit MTPUtils;
19
20 {$mode objfpc}{$H+}
21
22 interface
23
24 uses
25 Classes, SysUtils, MTProcs;
26
27 type
28 TSortPartEvent = procedure(aList: PPointer; aCount: PtrInt);
29
30 { TParallelSortPointerList }
31
32 TParallelSortPointerList = class
33 protected
34 fBlockSize: PtrInt;
35 fBlockCntPowOf2Offset: PtrInt;
36 FMergeBuffer: PPointer;
37 procedure MTPSort(Index: PtrInt; {%H-}Data: Pointer; Item: TMultiThreadProcItem);
38 public
39 List: PPointer;
40 Count: PtrInt;
41 Compare: TListSortCompare;
42 BlockCnt: PtrInt;
43 OnSortPart: TSortPartEvent;
44 constructor Create(aList: PPointer; aCount: PtrInt; const aCompare: TListSortCompare;
45 MaxThreadCount: integer = 0);
46 procedure Sort;
47 end;
48
49 { Sort a list in parallel using merge sort.
50 You must provide a compare function.
51 You can provide your own sort function for the blocks which are sorted in a
52 single thread, for example a normal quicksort. }
53 procedure ParallelSortFPList(List: TFPList; const Compare: TListSortCompare;
54 MaxThreadCount: integer = 0; const OnSortPart: TSortPartEvent = nil);
55
56 implementation
57
58 procedure ParallelSortFPList(List: TFPList; const Compare: TListSortCompare;
59 MaxThreadCount: integer; const OnSortPart: TSortPartEvent);
60 var
61 Sorter: TParallelSortPointerList;
62 begin
63 if List.Count<=1 then exit;
64 Sorter:=TParallelSortPointerList.Create(@List.List[0],List.Count,Compare,
65 MaxThreadCount);
66 try
67 Sorter.OnSortPart:=OnSortPart;
68 Sorter.Sort;
69 finally
70 Sorter.Free;
71 end;
72 end;
73
74 { TParallelSortPointerList }
75
76 procedure TParallelSortPointerList.MTPSort(Index: PtrInt; Data: Pointer;
77 Item: TMultiThreadProcItem);
78
79 procedure MergeSort(L, M, R: PtrInt; Recursive: boolean);
80 var
81 Src1: PtrInt;
82 Src2: PtrInt;
83 Dest1: PtrInt;
84 begin
85 if R-L<=1 then begin
86 // sort lists of 1 and 2 items directly
87 if L<R then begin
88 if Compare(List[L],List[R])>0 then begin
89 FMergeBuffer[L]:=List[L];
90 List[L]:=List[R];
91 List[R]:=FMergeBuffer[L];
92 end;
93 end;
94 exit;
95 end;
96 // sort recursively
97 if Recursive then begin
98 MergeSort(L,(L+M) div 2,M-1,true);
99 MergeSort(M,(M+R+1) div 2,R,true);
100 end;
101 // merge both blocks
102 Src1:=L;
103 Src2:=M;
104 Dest1:=L;
105 repeat
106 if (Src1<M)
107 and ((Src2>R) or (Compare(List[Src1],List[Src2])<=0)) then begin
108 FMergeBuffer[Dest1]:=List[Src1];
109 inc(Dest1);
110 inc(Src1);
111 end else if (Src2<=R) then begin
112 FMergeBuffer[Dest1]:=List[Src2];
113 inc(Dest1);
114 inc(Src2);
115 end else
116 break;
117 until false;
118 // write the mergebuffer back
119 Src1:=L;
120 Dest1:=l;
121 while Src1<=R do begin
122 List[Dest1]:=FMergeBuffer[Src1];
123 inc(Src1);
124 inc(Dest1);
125 end;
126 end;
127
128 var
129 L, M, R: PtrInt;
130 i: integer;
131 NormIndex: Integer;
132 Range: integer;
133 MergeIndex: Integer;
134 begin
135 L:=fBlockSize*Index;
136 R:=L+fBlockSize-1;
137 if R>=Count then
138 R:=Count-1; // last block
139 //WriteLn('TParallelSortPointerList.LWTSort Index=',Index,' sort block: ',L,' ',(L+R+1) div 2,' ',R);
140 if Assigned(OnSortPart) then
141 OnSortPart(@List[L],R-L+1)
142 else
143 MergeSort(L,(L+R+1) div 2,R,true);
144
145 // merge
146 // 0 1 2 3 4 5 6 7
147 // \/ \/ \/ \/
148 // \/ \/
149 // \/
150 // For example: BlockCnt = 5 => Index in 0..4
151 // fBlockCntPowOf2Offset = 3 (=8-5)
152 // NormIndex = Index + 3 => NormIndex in 3..7
153 NormIndex:=Index+fBlockCntPowOf2Offset;
154 i:=0;
155 repeat
156 Range:=1 shl i;
157 if NormIndex and Range=0 then break;
158 // merge left and right block(s)
159 MergeIndex:=NormIndex-Range-fBlockCntPowOf2Offset;
160 if (MergeIndex+Range-1>=0) then begin
161 // wait until left blocks have finished
162 //WriteLn('TParallelSortPointerList.LWTSort Index=',Index,' wait for block ',MergeIndex);
163 if (MergeIndex>=0) and (not Item.WaitForIndex(MergeIndex)) then exit;
164 // compute left and right block bounds
165 M:=L;
166 L:=(MergeIndex-Range+1)*fBlockSize;
167 if L<0 then L:=0;
168 //WriteLn('TParallelSortPointerList.LWTSort Index=',Index,' merge blocks ',L,' ',M,' ',R);
169 MergeSort(L,M,R,false);
170 end;
171 inc(i);
172 until false;
173 //WriteLn('TParallelSortPointerList.LWTSort END Index='+IntToStr(Index));
174 end;
175
176 constructor TParallelSortPointerList.Create(aList: PPointer; aCount: PtrInt;
177 const aCompare: TListSortCompare; MaxThreadCount: integer);
178 begin
179 List:=aList;
180 Count:=aCount;
181 Compare:=aCompare;
182 BlockCnt:=Count div 100; // at least 100 items per thread
183 if BlockCnt>ProcThreadPool.MaxThreadCount then
184 BlockCnt:=ProcThreadPool.MaxThreadCount;
185 if (MaxThreadCount>0) and (BlockCnt>MaxThreadCount) then
186 BlockCnt:=MaxThreadCount;
187 if BlockCnt<1 then BlockCnt:=1;
188 end;
189
190 procedure TParallelSortPointerList.Sort;
191 begin
192 if (Count<=1) then exit;
193 fBlockSize:=(Count+BlockCnt-1) div BlockCnt;
194 fBlockCntPowOf2Offset:=1;
195 while fBlockCntPowOf2Offset<BlockCnt do
196 fBlockCntPowOf2Offset:=fBlockCntPowOf2Offset*2;
197 fBlockCntPowOf2Offset:=fBlockCntPowOf2Offset-BlockCnt;
198 //WriteLn('TParallelSortPointerList.Sort BlockCnt=',BlockCnt,' fBlockSize=',fBlockSize,' fBlockCntPowOf2Offset=',fBlockCntPowOf2Offset);
199 GetMem(FMergeBuffer,SizeOf(Pointer)*Count);
200 try
201 ProcThreadPool.DoParallel(@MTPSort,0,BlockCnt-1);
202 finally
203 FreeMem(FMergeBuffer);
204 FMergeBuffer:=nil;
205 end;
206 end;
207
208 end.
209