Saturday, September 28, 2013

Why sum is not 6 with this parallel algorithm

Why sum is not 6 with this parallel algorithm



/*
integer B[1..n]
forall i in 1 : n do
B[i] := ai
enddo
for h = 1 to k do
forall i in 1 : n/2^h do
B[i] := B[2i-1] + B[2i]
enddo
enddo
S := B[1]

*/

int n = 3;
int[] hello = new int[n+1];
int[] B = new int[n+1];
hello
[0] = 0;
hello
[1] = 1;
hello
[2] = 2;
hello
[3] = 3;
//hello[4] = 4;
//hello[5] = 5;
Parallel.For(1, n+1, ctr => { B[ctr] = hello[ctr]; });

for(int h =1; h<=3; ++h)
Parallel.For(1, Convert.ToInt32(n/Math.Pow(2,h)), ctr => {
//if (2 * ctr - 1 >= 0 && 2 * ctr <= n)
B
[ctr] = B[2 * ctr - 1] + B[2 * ctr];
//else if(2 * ctr <= n)
// B[ctr] = B[2 * ctr];
});

int sum = B[1];
Console.WriteLine(sum.ToString());




bots


No comments:

Post a Comment