Hi Andy --

Let me start with some meta stuff:

First, note that you're pointing to the 1.14 version of the documentation but probably want to be looking at the current (1.17) version. I'm guessing you got there through a Google search. We've been working to teach Google to point people to our latest documents, but haven't yet been successful at that... In the meantime, anytime you're on a Chapel documentation page, try to glance at the color of the version menu (upper left), and if it's orange, use that as a warning that you might want to jump to the latest version (which you can often do by pulling down on the version menu and choosing the latest version if the page hasn't changed location between releases... otherwise, you'll have to search for it within the documentation's search function directly).

Second, the page you pointed to is for associative arrays which support dictionary or hash-table styles of arrays, associating arbitrary keys to values. Coming from Fortran (and looking at the example on the allocatable arrays page you pointed to), you're much more likely to want to use Chapel's rectangular domains and arrays which are described here:

        https://chapel-lang.org/docs/primers/arrays.html

Third, if you're a Stack Overflow user, questions like this are great to post there so that they'll help others in the future as well. Gitter is another place that short questions can be asked if you prefer a chat format.

OK, now to your specific question about allocatable arrays: Chapel's arrays are _very_ dynamically allocatable (and reallocatable). There are a few ways you can approach this (and the primer above may be more useful than my terse response here):

First, declarations and computations can be mixed in Chapel, so you could do something like the following to intermix the setting of a size like numParticles and the declaration of the arrays themselves:

        // this will eventually say how large our arrays should be
        var numParticles: int;

        // ...do some arbitrary computations and stuff...

        // read in a value for numParticles
        read(numParticles);

        // now declare our arrays over that size
        var charge: [1..numParticles] real,
            xyz: [1..numParticles, 1..3] real;

You can often just get the size right the first time by using a variable initializer that does the computation:

        var numParticles = read(int),
            charge: [1..numParticles] real,
            xyz: [1..numParticles, 1..3] real;

And this is often a good place to use a 'config' declaration of the sort I was mailing you about earlier:

        config const numParticles = read(int);

        var charge: [1..numParticles] real,
            xyz: [1..numParticles, 1..3] real;

Finally, you can always reallocate arrays over time by separating their index set ("domain" in Chapel) from their declaration as follows:

        // create a domain that's initially empty
        var particleSpace = {1..0};

        // create some initially empty arrays
        var charge: [particleSpace] real,
            xyz: [particleSpace] [1..3] real;

        // read a size
        const numParticles = read(int);

        // re-assign the domain -- this will cause the arrays declared
        // using particleSpace to be re-allocated to incorporate these new
        // indices.
        particleSpace = {1..numParticles};

        // ...do some computations with charge and xyz here...

        // double the size of particle space (for some reason).
        // Now all the arrays will be twice as large
        particleSpace = {1..2*numParticles};

Reallocating arrays is expensive by nature, so typically you don't want to do this unless you have no other choice; but this gives you a sense of how you can when the need arises.

Hope this helps,
-Brad



On Tue, 19 Jun 2018, Andrew Halper wrote:

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
[email protected]
https://lists.sourceforge.net/lists/listinfo/chapel-users

Reply via email to