Question

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.

Was it helpful?

Solution

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);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top