TParallel.For()
has an argument called AStride
. In my case AStride is 2:
TParallel.&For(2, 1, 10,
procedure(index: Integer)
begin
TThread.Queue(nil,
procedure
begin
memo1.Lines.Add(index.ToString());
end
);
end
);
I cannot understand the technical meaning of "AStride" here.
Does AStride = 2
mean the first thread will handle two consecutive numbers in the range [1..10]
, the second thread will handle the next consecutive numbers etc?
** English is not my native language and I translate "Stride" to "long step" or "pace".
One might be tempted to think that that the answer can be found in the documentation:
I'd read that as implying that the loop variable values are 1, 3, 5, 7 and 9. However that is not the case. This program:
outputs the ten numbers from
1
to10
.In fact the stride parameter allows you to tune the performance. The parallel for loop uses a thread pool to schedule the work. If the work packets are very small then the synchronization overhead within the thread pool can dominate performance. The way to get around this is to make sure that the work packets are large enough to dominate the synchronization overhead.
The stride allows you to achieve this. In your example, the loop index values 1 and 2 are performed as one piece of work. Index values 3 and 4 are another piece of work. And so on. By grouping multiple indices into a single piece of work, the amount of time spent on synchronization overhead is reduced.