I have a loop in C# that looks like this:
for (int level = 0; level < numLevels; level++){
for (int y = 0; y < ysize; y++){
for (int x = 0; x < xsize; x++){
outArray[y*xsize + x] = SomeMathWasDone(level);
}
}
}
I want to parallelize this code using a Parallel.For loop, something that seems pretty straightforward, at least according to Reed's answer to an earlier question I asked. So I go ahead and do this:
for (int level = 0; level < numLevels; level++){
Parallel.ForEach(Partitioner.Create(0, ysize),
(range) => {
for (int y = range.Item1; y < range.Item2; y++){
for (int x = 0; x < xsize; x++){
outArray[y*xsize + x] = SomeMathWasDone(level);
}
}
});
}
This code throws a System.AggregateException. The inner exception is:
InnerException {"Destination array was not long enough. Check destIndex and length, and the array's lower bounds."} System.Exception {System.ArgumentException}
I know that the arrays are the right size, since the code works just fine when it's not parallel. What does this mean? How can I debug this problem? Or should I be doing something else entirely when parallelizing code of this nature?
The inner exception stack trace is:
at System.Array.Copy(Array sourceArray, Int32 sourceIndex, Array destinationArray, Int32 destinationIndex, Int32 length, Boolean reliable)
at System.Array.Copy(Array sourceArray, Int32 sourceIndex, Array destinationArray, Int32 destinationIndex, Int32 length)
at System.Array.CopyTo(Array array, Int32 index)
at MyFunction(Single[] inImage, Int32 inXSize, Int32 inYSize, Int32 inLevels) ... line 100
After going to that line, I messed with it to see if that could be the issue. The line was:
float[] theKernel =
{1.0f, 4.0f, 6.0f, 4.0f, 1.0f,
4.0f, 16.0f, 24.0f, 16.0f, 4.0f,
6.0f, 24.0f, 36.0f, 24.0f, 6.0f,
4.0f, 16.0f, 24.0f, 16.0f, 4.0f,
1.0f, 4.0f, 6.0f, 4.0f, 1.0f};
And thinking that maybe that wouldn't work, I then tried the more explicit:
//outside the function, now a member of the class
float[] downsizeKernel=
{1.0f, 4.0f, 6.0f, 4.0f, 1.0f,
4.0f, 16.0f, 24.0f, 16.0f, 4.0f,
6.0f, 24.0f, 36.0f, 24.0f, 6.0f,
4.0f, 16.0f, 24.0f, 16.0f, 4.0f,
1.0f, 4.0f, 6.0f, 4.0f, 1.0f};
//now inside the function
float[] theKernel = new float[25];
downsizeKernel.CopyTo(theKernel, 0);
So could that be the problem, some kind of internal array copying? How should this array declaration be handled to avoid the exception, if that's the problem?