S
S
Sergey Vinogradov2014-06-27 12:55:38
C++ / C#
Sergey Vinogradov, 2014-06-27 12:55:38

Where does the contents of the array go when passing it to an intermediate method?

There is a method producing some arithmetic operations. In the first half of the method, an array x is calculated, consisting of numbers arranged in ascending order, which is passed to the intermediate method for subsequent calculations.
The code is something like this:

{
...
//Это выходные параметры
double[] L; 
double[] V;

...

for (int i = 2; i < n; i++)
{
     FormANk(V, ref d, ref e, ref a, L);
     Value(d, e, a, i + 1, ref L, ref V, epsilon);
}

//t1
if (need)
{
     vr = EV(dA, eA, L);
     ...
}

lam = L;
vec = v;

}

static void Value(double[] d, double[] e, double[] A, int n, ref double[] l, ref double[] v, double epsilon)
{
     double[] L = new double[n];
     double[] V;

     //p1
     Parallel.For(0, n - 2, i =>
     {
          Interlocked.Exchange(ref L[i + 1], EVIn(d, i, A, epsilon));
     });

     EVOut(d, A, ref L);

     V = EVs(A, d, L);

      l = L;
      v = V;
}

private static double[,] EV(double[] d, double[] e, double[] L)
{
     //t2
     int n = d.Length;
     double[,] res = new double[n, n];
     
     //p2       
     Parallel.For(0, n, j =>
     {
          Interlocked.Exchange(ref res[0, j], 1.0d);                
          Interlocked.Exchange(ref res[1, j], (L[j] - d[0]) * res[0, j] / e[0]);
           
          for (int i = 2; i < n; i++)
          {
               Interlocked.Exchange(ref res[i, j], ((L[j] - d[i - 1]) * res[i - 1, j] - e[i - 2] * res[i - 2, j]) / e[i - 1]); ;                   
           }
     });

     Norm(res);

     return res;
}

Here t1 and t2 are the conditional location of the breakpoint.
And what is interesting - if you consistently stop at t1, t2 - then everything works correctly and without complaints. However, if we stop execution only at t2, then 3 out of 7 times at t2 we get when viewing the passed array x we ​​get approximately the following {0.0, NaN, 0.0, 0.0, ..., NaN, NaN, 0.0}. Accordingly, if we do not stop the execution at the output, we will get either the required answer, or complete rubbish.
The question is how?! Where do the values ​​disappear and why only in the case of not stopping at t1?
UPD: It is unequivocally established that if Parallel.For() is replaced with the usual for() in places p1 and p2, then, it seems, this problem does not arise. Then another question - why is this happening? Filling is safe. At the same time, replacing internal arrays with ConcurrentBag does not help. The glitch is no longer clear to me.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
Y
Yuri Tseretyan, 2014-07-03
@Karasb

Try playing around with ParallelOptions.MaxDegreeOfParallelism and make sure that Parallel.For evaluates to positive.

S
Sergey Vinogradov, 2014-07-04
@genI3

An error in the code was found and does not lie in Parallel.For itself. However, the randomness of execution / non-execution of the code remained unclear.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question