Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the meaning of AStride in TParallel.For?

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

like image 452
iPath ツ Avatar asked Nov 29 '14 19:11

iPath ツ


1 Answers

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.

like image 123
David Heffernan Avatar answered Nov 09 '22 11:11

David Heffernan