Hi there,
I am trying to use parallel.for in order to calculate the summation of all the values in an multidimensional array.
I googled and found Click Here.
However, this is just for one dimensional arrays. The LINQ does not work for multidimensional array.
And when I use parallel.for it gives me different answer than the sequential approach.

Does anybody know how to implement parallel.for for summing up multidimensional array.


Can you give us something specific like the type of the array, the number of dimensions, perhaps some sample data and the results you are trying to generate from that data?

Okay here is the sequential code for calculating the overall summation of the whole array.

For h = 1 To 9
    For j = 1 To 751
        For i = 0 To 282
            TSM += stateprob(h, j, i)
         Next i
     Next j
Next h

This is fast, but because it is inside another loop which is going for 5000 iterations I thought calculating it in parallel improve the performance of the program. I should say the next iteration depends on the results of this summation.

Does anybody know how to implement parallel.for for summing up multidimensional array.

So you want one number which is the sum of all of the numbers in a three dimensional array. Why do you have it inside another loop? If the containing loop is running for 5000 iterations then you are going to calculate the sum 5000 times. The only reason I can imagine for putting it inside that loop is if the numbers in the 3D array are changing over the 5000 iterations in which case you can't use a separate thread (parallel for) to calculate the sum. If the values in the 3D array are independent of the loop then you could take the summation code and put it in a separate thread. I have never done threads so I am a little lacking in expertise in this area.

If I am missing something then you will have to be clearer on the definition of the problem.

You are right the summation depends on the iteration. Then I can not use parallel.for in this case.
Thanks for your response