I am again struggling to understand some things for parallel computation.
In the code I am working on, I have a class that extends list<list<double>>
. Inside this class, I'm writing a method to return the average of the values in the list<list<double>>
. This method is private.
public class myClass : list<list<double>>
{
//properties and stuff
private double average()
{
//method body
}
}
I two versions for this method, both of them working. The first version is serial:
private double average()
{
double avg = 0;
for (int i = 0; i < this.Count; i++)
{
for (int j = 0; j < this[0].Count; j++)
{
avg += this[i][j];
}
}
avg = avg / (this.Count*this[0].Count);
return avg;
}
The second version is parallel:
private double average()
{
double avg = 0;
double[] cumsum = new double[this.Count];
Parallel.For(0, this.Count, i =>
{
cumsum[i] = 0;
for (int j = 0; j < this[0].Count; j++)
{
cumsum[i] += this[i][j];
}
});
avg = cumsum.Sum() / ((this.Count * this[0].Count));
return avg;
}
As a learning exercise, I tried to mess things up by using more complex parallel threading. My idea was to make do without the intermediate array in which I sum the lines. Specifically, this was my attempt (doesn't work):
private double average()
{
double avg = 0;
Parallel.For<double>(0, this.Count, () => 0, (i, loop, sub) =>
{
for (int j = 0; j < this[0].Count; j++)
{
sub += this[i][j];
}
return sub;
},
(x) =>
{
double tot = avg;
Interlocked.CompareExchange(ref avg, tot+sub, tot);
});
return avg / ((this.Count * this[0].Count));
}
This snippet has (at least) two mistakes.
The first error it give me is on the sub += this[i][j];
:
The best overloaded method match for
'System.Collections.Generic.List>.this[int]'
has some invalid arguments
I do not understand this error here, because both i and j are of type int.
Then I have a further error on Interlocked.CompareExchange(ref avg, tot+sub, tot);
(expected, because I don't really understand how this method works):
The name 'sub' does not exist in the current context
Can someone point me to a correct form of the last snippet? And/or some material to clarify this things? I read from http://msdn.microsoft.com/en-us/library/dd460703.aspx but it didn't help make things clear for me.