I am trying to iterate through a 2D array and then add to parts of another 2D array in a Parallel.For loop. I have found examples where an accumulation is being done inside the loop on a single variable but am not sure what to do here. GridB is doing accumulation so surely it needs to be locked as items in it could be added to from different threads If it needs to be locked how do you go about locking an entire array like this?
int[,] GridA = new int[D + 2, D + 2];
int[,] GridB = new int[D + 2, D + 2];
Parallel.For(1, D+1 , r =>
{
for (int c = 1; c <= D ; c++)
if (GridA[r, c] != 0)
{
int v = GridA[r, c];
GridB[r - 1, c - 1] += v;
GridB[r - 1, c] += v;
GridB[r - 1, c + 1] += v;
GridB[r, c - 1] += v;
GridB[r, c + 1] += v;
GridB[r + 1, c - 1] += v;
GridB[r + 1, c] += v;
GridB[r + 1, c + 1] += v;
}
});
[][]instead of a two dimensional array[,].GridB, which would pretty much secure the threading, while still allowing them to switch properly.