4

I was wondering if I can use OTL to parallelize this loop.

I have the following code.

for i := 1 to XRes do
 begin
   for j := 1 to XRes do
     begin
       GridMat.Elem[i,j] := StrToFloat(ListOfValues[(i-1)+((j-1)*Xres)]);
     end;
   Invalidate;
 end;

Is it possible to pass the GridMat (from SDL_matrix.TMatrix) as a parameter to all the parallel ForEach and add the values to it.

Kromster
  • 7,181
  • 7
  • 63
  • 111
Diego Rueda
  • 2,226
  • 4
  • 21
  • 41

1 Answers1

5

If you use the synchronous version of Parallel.ForEach (the default one; i.e. if you don't use the NoWait specifier) then you can simply access the GridMat through variable capture.

Something like this should just work:

Parallel.ForEach(1, XRes).Execute(
  procedure (const i: integer)
  var
    j: integer;
  begin
    for j := 1 to XRes do
      GridMat.Elem[i,j] := StrToFloat(ListOfValues[(i-1)+((j-1)*Xres)]);
  end);
David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
gabr
  • 26,580
  • 9
  • 75
  • 141