11

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".

No Idea For Name
  • 10,935
  • 10
  • 37
  • 61
iPath ツ
  • 2,358
  • 16
  • 30

1 Answers1

12

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.

Toon Krijthe
  • 50,865
  • 37
  • 137
  • 200
David Heffernan
  • 572,264
  • 40
  • 974
  • 1,389
  • I hope the parameter you've expected from this one will be described and named as iteration step, or counter step (if they ever introduce it). Out of curiosity, do they use the thread pool API on Windows platform ? – TLama Nov 29 '14 at 20:55
  • 2
    @TLama I doubt they'd add such a parameter since it's easy for programmer to do so. Other Parallel.For implementations work out the "stride" themselves I think. The thread pool is native to the Delphi RTL. So not the system thread pool. – David Heffernan Nov 29 '14 at 20:59
  • In addition, the stride is "self-tuning". I.e. when no start value is given it increments periodically. I think, that this is done to counteract false sharing in long running loops. – iamjoosy Dec 17 '14 at 20:51