//not threaded code (yet to be threaded):
var am: Matrix;
begin
am.Size(1001,100);
for LoopIndex := 0
to a.Rows - 1
do
begin
for j := 0
to a.Cols - 1
do
begin
a[LoopIndex, j] := a[LoopIndex, j] + 1;
end;
end;
end;
//threaded code using TMtxForLoop:
procedure MyLoop(LoopIndex: integer;
const Context: TObjectArray; ThreadIndex: integer);
var j: integer;
a:
TMtx;
begin
a :=
TMtx(Context[0]);
for j := 0
to a.Cols - 1
do
begin
a[LoopIndex, j] := a[LoopIndex, j] + 1;
end;
end;
procedure TMainForm.OnButtonClick(Sender: TObject);
var am: Matrix;
forLoop: TMtxForLoop
begin
forLoop := TMtxForLoop.Create;
//local var only in this example, allocate globaly for performance
am.Size(1001,100);
am.SetVal(1);
//Start the threads:
DoForLoop(0,1000, MyLoop, forLoop,[
TMtx(am)]);
//blocking call for i := 0 to 1000 do
//Code execution will not continue until all threads have finished.
ViewValues(am);
forLoop.Free;
End;
//alternative method (unused here), called only once per each started thread:
procedure MyLoopRange(IdxStart, IdxEnd: integer;
const Context: TObjectArray; ThreadIndex: integer);
var j, k: integer;
a:
TMtx;
begin
a :=
TMtx(Context[0]);
for k := IdxStart
to IdxEnd
do //additional vectorization possible inside these two loops
for j := 0
to a.Cols - 1
do
begin
a[k, j] := a[k, j] + 1;
end;
end;