Difference between "for" and "forall" on the right-hand side of assignment

Viewed 70

In the following code, I am assigning some dummy (test) values to all the elements of a 2-dimensional array (a[][]) and then checking whether the array is filled as expected. When using the line (A), the code works as expected (i.e., it prints "passed").

proc test()
{
    const N = 1000;

    var a: [1..N, 1..3] real =   (for m in 1 .. (3 * N) do (1.0 / m));  // (A)
 // var a: [1..N, 1..3] real =       [m in 1 .. (3 * N)]   (1.0 / m);   // (B)
 // var a: [1..N, 1..3] real = forall m in 1 .. (3 * N) do (1.0 / m);   // (C)

    var m = 0;
    for i in 1 .. N {
        for k in 1 .. 3 {
            m += 1;
            assert( abs( a[i, k] - 1.0 / m ) < 1.0e-10 );
        }
    }
    writeln("passed");
}

test();

On the other hand, if I use line (B) or (C) instead of (A), the code gives this error:

test.chpl:7: error: iteration over a range with multi-dimensional iterator

I am wondering what is the meaning of this error message? (My expectation is that the right-hand side of the assignment is evaluated first, i.e., a temporary array is created in parallel and assigned to the left-hand side. But is this expectation not correct?)

(I am also wondering whether it is valid to assign a one-dimensional array to a two-dimensional array (as in line (A)) without reshape...?)

0 Answers
Related