Chapel: an array of arrays compared to a 2-dimensional array in memory

In the Chapel, it seems possible to declare an array using a notation [][]. It looks very similar to an “array of arrays” in other languages, so I wonder if it is a so-called “jagged array” with each allocated array independently in memory? For example, in the following code a[0][..]and a[1][..]not necessarily contiguous in memory? (My interest here is whether using this can be [][]less efficient than [,]due to non-contiguous memory.)

proc test( D1, D2 )
{
    var a: [D1][D2] int;   // "jagged" array?
    var b: [D1, D2] int;   // I assume this is a rectanguar (contiguous) array

    for i in D1 do
    for j in D2 do
        a[i][j] = i * 100 + j;

    for (i, j) in b.domain do
        b[i, j] = i * 100 + j;

    var lo = D1.low, hi = D1.high;

    writeln( "a = ", a );
    writeln( "a[ lo ] = ", a[ lo ] );
    writeln( "a[ hi ] = ", a[ hi ] );
    writeln();
    writeln( "b = ", b );
    writeln( "b[ lo, .. ] = ", b[ lo, .. ] );
    writeln( "b[ hi, .. ] = ", b[ hi, .. ] );
}

test( 0..1, 1..3 );

$ chpl test.chpl
$ ./a.out

a = 1 2 3 101 102 103
a[ lo ] = 1 2 3
a[ hi ] = 101 102 103

b = 1 2 3
101 102 103
b[ lo, .. ] = 1 2 3
b[ hi, .. ] = 101 102 103

A related question: is there a way or command to know the location (address) of the memory of a given element or array element (to get information about the memory allocation)?

+4
1

, (var A: […][…] …) , , ( , , ). , , , .

(var A: […, …] …) , , . .

, , , . , , , - , .

, , , . , c_ptrTo(), , printf(), :

use CPtr;

config const n = 3;

var A: [1..n][1..n] real;

var a11 = c_ptrTo(A[1][1]),
    a12 = c_ptrTo(A[1][2]),
    a21 = c_ptrTo(A[2][1]);

var B: [1..n, 1..n] real;

var b11 = c_ptrTo(B[1,1]),
    b12 = c_ptrTo(B[1,2]),
    b21 = c_ptrTo(B[2,1]);

extern proc printf(x...);

printf("%p %p %p\n", a11, a12, a21);
printf("%p %p %p\n", b11, b12, b21);
+3

Source: https://habr.com/ru/post/1681398/


All Articles