Hi Andrew,

// While I'm writing this e-mail, I found a reply from Brad,
// so just for some "plus alpha" thing...

That was indeed the first barrier for me to get started..., i.e., how to
allocate a multi-dimensional array (FYI, I'm from Fortran background).
I remember that it was written somewhere in one of the Primer pages
in the Chapel official site, but IIRC the description
was not very easy to understand for me at that time
(so it took a pretty long time until I notice it from some
example code on the net...)

In fact, it is extremely easy once I got to know the trick...

Ex.1:
    var n: int = 1000;
    var arr: [1..n] real;
       // this allocates "arr" on the heap, so equivalent to allocate()
       // in Fortran

  => This corresponds to this snippet in Fortran:
    integer(8) n
    real(8), allocatable :: arr( : )
    n = 1000
    allocate( arr( 1:n ) )

   --- "n" can be dynamical set just before the declaration
       of arr (in the same way as allocate).
   --- Note that "int" and "real" in Chapel are both 64-bit.
       (more precisely, int = int(64) and real = real(64)).

Ex.2:
    var n1 = 100, n2 = 200;   // type inference is used here, so no "int"
    var arr: [ n1.. n2 ] real;   // <--> allocate( arr( n1 : n2 ) )

So, if the index range is explicitly written, the declaration of array
simply means that it is allocated on the heap. (AFAIK, there is no
stack-based fixed-size arrays. But this can be emulated with
tuples to some extent.)

Ex.3:
    var D = { 1..0 };
    var arr: [D] real;

    This has no corresponding concept in Fortran.
    That is, we declare the "domain" variable D that holds
    the info of indices separately from the array.
    By assigning some index range to D, "arr" is automatically
     allocated or reallocated. In the above example, D is initially
    given an empty index range (from 1 to 0), so arr starts with
    an empty state. Once we assign a value to D, e.g.,

    D = { -100 .. 100 };

    then "arr" is reallocated to have the index range from -100
    to 100. This is a very useful feature not present in Fortran (AFAIK...)

    We can associate such D to multiple arrays at once:

    var arr1, arr2, arr3: [D] real;

    and reassigning a value to D means that all of 'arr1' to 'arr2'
    are reallocated simultaneously.

PS. I still feel that this is the most important point to be covered
(or emphasized) in the Introductory materials in Chapel,
before entering deeper into parallel things... (because the above
"domain <-> array" concept simply does not exist in Fortran etc).

PS2. Because my syntax knowledge and understanding is still very poor,
please suggest any mistakes (and possible improvements :)

Best regards,
Takeshi

2018-06-20 7:36 GMT+09:00 Andrew Halper <ashal...@usgs.gov>:

> Hello,
>
> I was wondering if there is a recommended strategy in Chapel for imitating
> Fortran90's "allocatable" arrays
> <http://kea.princeton.edu/ChE422/arrays.htm>? (From what I read here
> <https://chapel-lang.org/docs/1.14/primers/primers/associative.html#primers-associative>,
> it seems like there might not be one, for good reasons, and that's fine.)
>
> Thank you,
>
> --
> Andy Halper
> USGS
> Integrated Modeling and Prediction Division
>
>
> ------------------------------------------------------------
> ------------------
> Check out the vibrant tech community on one of the world's most
> engaging tech sites, Slashdot.org! http://sdm.link/slashdot
> _______________________________________________
> Chapel-users mailing list
> Chapel-users@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/chapel-users
>
>
------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Chapel-users mailing list
Chapel-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/chapel-users

Reply via email to