Search code examples
arraysmultidimensional-arrayparallel-processingsizechapel

How the get the sizes of the dimensions of a multi-dimensional array in Chapel?


Consider that we have defined the array below in Chapel:


var arr : [1..10] [1..5] int;

How to get the size of the dimensions of this array? In other words, I need a procedure that gets arr as the input and gives (10,5) as the output.

To get its second dimension, I used arr.domain.dim(1).size, but it gave me an error.


Solution

  • A potential misunderstanding here is that your array:

    var arr : [1..10] [1..5] int;
    

    is technically not a multidimensional array in Chapel; rather it is a 1D array whose elements are each 1D arrays. So to get the size of one of its inner arrays, you would need to take the size of an element as follows (ATO):

    writeln(arr.size);        // the size of arr
    writeln(arr.first.size);  // the size of arr[1] (and because of your declaration, arr[i]
    

    This solution assumes that the arrays are 1D since we're using size, which will return the total number of elements in an array.

    A multidimensional array in Chapel looks like this:

    var arr: [1..10, 1..5] int;
    

    and you could determine its dimensions' sizes using one of these queries (ATO):

    writeln(arr.shape);                           // the shape of arr as a tuple
    writeln((arr.dim(0).size, arr.dim(1).size));  // a tuple whose values are the sizes of each array dimension