-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathPFL.pas
More file actions
221 lines (204 loc) · 6.43 KB
/
PFL.pas
File metadata and controls
221 lines (204 loc) · 6.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
unit PFL;
////////////////////////////////////////////////////////////////////////////////
//
// Unlike the PPL, the parallel for loop implemented in this unit provides the
// index of the thread in the thread pool for each iteration. This allows to avoid intensive
// use of TInterlocked.
//
// For example, the PPL code:
//
// TParallel.For(2,1,Max,procedure(I:Int64)
// begin
// if IsPrime(I) then TInterlocked.Increment(NPrimes);
// end);
//
// Can be implemented using a ParallelFor-object of type TParallelFor:
//
// ParallelFor.Execute(1,Max,procedure(I,Thread:Integer)
// begin
// if IsPrime(I) then Inc(NPrimes[Thread]);
// end, 2);
//
// and finally summing the NPrimes-array over all threads.
//
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
//
// Author: Jaap Baak
// https://github.com/transportmodelling/Utils
//
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
interface
////////////////////////////////////////////////////////////////////////////////
Uses
Classes, SysUtils, Math, SyncObjs;
Type
TLoopIteration = reference to Procedure(Iteration,Thread: Integer);
TParallelFor = Class
private
Type
TIterationThread = Class(TThread)
private
Active: TEvent;
Loop: TParallelFor;
Thread,Iteration,Stride: Integer;
LoopIteration: TLoopIteration;
public
Constructor Create;
Procedure Execute; override;
Destructor Destroy; override;
end;
Var
FMaxThreads: Integer;
LoopCompleted: TEvent;
FirstException: Exception;
Next,IterationCount,ActiveThreads: Integer;
ThreadPool: array of TIterationThread;
public
Constructor Create(ThreadCount: Integer);
Procedure Execute(const FromIteration,ToIteration: Integer;
const Iteration: TLoopIteration;
const Stride: Integer = 1); overload;
Procedure Execute(const NThreads,FromIteration,ToIteration: Integer;
const Iteration: TLoopIteration;
const Stride: Integer = 1); overload;
Destructor Destroy; override;
public
Property MaxThreads: Integer read FMaxThreads;
end;
////////////////////////////////////////////////////////////////////////////////
implementation
////////////////////////////////////////////////////////////////////////////////
Constructor TParallelFor.TIterationThread.Create;
begin
inherited Create(true);
Active := TEvent.Create(nil,true,false,'');
end;
Procedure TParallelFor.TIterationThread.Execute;
begin
repeat
Active.WaitFor(Infinite);
// Execute iteration
if not Terminated then
try
LoopIteration(Iteration,Thread);
except
on E: Exception do
begin
TMonitor.Enter(Loop);
try
Dec(Loop.ActiveThreads);
if Loop.ActiveThreads = 0 then Loop.LoopCompleted.SetEvent;
if Loop.FirstException = nil then Loop.FirstException := AcquireExceptionObject as Exception;
Terminate;
finally
TMonitor.Exit(Loop);
end;
end;
end;
if not Terminated then
begin
TMonitor.Enter(Loop);
try
if (Loop.FirstException = nil) and (Loop.IterationCount > 0) then
begin
Iteration := Loop.Next;
Inc(Loop.Next,Stride);
Dec(Loop.IterationCount,Stride);
end else
begin
Active.ResetEvent;
Dec(Loop.ActiveThreads);
if Loop.ActiveThreads = 0 then Loop.LoopCompleted.SetEvent;
end;
finally
TMonitor.Exit(Loop);
end;
end;
until Terminated;
end;
Destructor TParallelFor.TIterationThread.Destroy;
begin
Active.Free;
inherited Destroy;
end;
////////////////////////////////////////////////////////////////////////////////
Constructor TParallelFor.Create(ThreadCount: Integer);
begin
inherited Create;
FMaxThreads := ThreadCount;
if ThreadCount > 1 then
begin
SetLength(ThreadPool,ThreadCount);
LoopCompleted := TEvent.Create(nil,false,true,'');
for var Thread := 0 to ThreadCount-1 do
begin
ThreadPool[Thread] := TIterationThread.Create;
ThreadPool[Thread].Loop := Self;
ThreadPool[Thread].Thread := Thread;
ThreadPool[Thread].FreeOnTerminate := true;
ThreadPool[Thread].Start;
end;
end;
end;
Procedure TParallelFor.Execute(const FromIteration,ToIteration: Integer;
const Iteration: TLoopIteration;
const Stride: Integer = 1);
begin
Execute(FMaxThreads,FromIteration,ToIteration,Iteration,Stride);
end;
Procedure TParallelFor.Execute(const NThreads,FromIteration,ToIteration: Integer;
const Iteration: TLoopIteration;
const Stride: Integer = 1);
begin
if ToIteration >= FromIteration then
begin
var ThreadCount := Min(NThreads,FMaxThreads);
if ThreadCount = 1 then
begin
// Single threaded, avoid synchronization overhead
var Iter := FromIteration;
while Iter <= ToIteration do
begin
Iteration(Iter,0);
Inc(Iter,Stride);
end;
end else
begin
// Multi threaded
FirstException := nil;
TMonitor.Enter(Self);
try
LoopCompleted.ResetEvent;
Next := FromIteration;
IterationCount := ToIteration-FromIteration+1;
while (IterationCount > 0) and (ActiveThreads < ThreadCount) do
begin
ThreadPool[ActiveThreads].Stride := Stride;
ThreadPool[ActiveThreads].Iteration := Next;
ThreadPool[ActiveThreads].LoopIteration := Iteration;
ThreadPool[ActiveThreads].Active.SetEvent;
Inc(ActiveThreads);
Inc(Next,Stride);
Dec(IterationCount,Stride);
end;
finally
TMonitor.Exit(Self);
end;
LoopCompleted.WaitFor(Infinite);
if FirstException <> nil then raise FirstException;
end;
end;
end;
Destructor TParallelFor.Destroy;
begin
for var Thread := low(ThreadPool) to high(ThreadPool) do
begin
ThreadPool[Thread].Terminate;
ThreadPool[Thread].Active.SetEvent;
end;
LoopCompleted.Free;
inherited Destroy;
end;
end.