What is the meaning of AStride in TParallel.For? What is the meaning of AStride in TParallel.For? multithreading multithreading

What is the meaning of AStride in TParallel.For?


One might be tempted to think that that the answer can be found in the documentation:

AStride: The Integer that represents the increment of the loop iteration.

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:

{$APPTYPE CONSOLE}uses  System.Threading;var  Lock: TMonitor;  LockObj: TObject;procedure Proc(Index: Integer);begin  Lock.Enter(LockObj);  Writeln(Index);  Lock.Exit(LockObj);end;begin  LockObj := TObject.Create;  TParallel.&For(2, 1, 10, Proc);end.

outputs the ten numbers from 1 to 10.

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.