Compile Time Allocated arrays within a single routine

I am assuming that Chapel allocates and fills a const array that is internal to a routine e at runtime, e.g.

const zeros : [0..1[ real = [ 0.0:, -0.0 ];

I want to avoid the filling overhead every time that the routine is instantiated, i.e. to define an array at compile time, obviously containing only compile-time known constants, or what C would call astatic const. If an array could be qualified by a param, it might achieve the same thing, but the language does not currently support that as I read the documentation (or as I experienced when I tried it).

If I did it within a module, I could achieve almost the same sort of thing but I want to avoid that complexity. Any suggestions on how to achieve it now? If not, is it likely to be achievable into the future?

Thanks - Damian

Hi Damian —

This is something that I want with some regularity as well, but which we currently don't support. The two main sticking points are (a) how to express it and (b) what the implications should be w.r.t. the locality of the declaration (e.g., should it exist only on locale 0 or only on the locale where the original call was made? should it exist on all locales?).

There's some discussion around this feature request here: Feature request: Support for generic-function "static" local variables · Issue #12281 · chapel-lang/chapel · GitHub

In practice, for most cases where I need it, I find I can usually work around this by making it a private module-scope constant, which causes it to be set up once and persist, and to be broadcast to all locales. But the obvious negative is that it puts something in the module scope that arguably is more logical as a local to the single procedure that was designed to refer to it.

-Brad

Glad it is on the radar Brad Your comments reflect my thoughts too, including workrounds. Sorry I did not find that earlier discussion. I did find some like #20250 that dealt with the static concept but they were not quite the same. I did not think to go back so far as 2019 to find #12281. Lazy/Silly me. Should I ask to have a static array in an inline routine???

Thanks - Damian

Are you asking whether you should add that particular use-case to the issue? I don't think there's a need to, in the sense that I think what we're discussing there already covers that case well enough. Though you're certainly welcome to register your interest in / support for the idea if you'd like (which could even just be giving it a thumbs-up).

-Brad

1 Like

I do not need to add my use case. I think we are on the same page. Thanks