5

Or any similar data structure of dynamic length, which, can be cast easily to an array. The only workaround I have found is entering the array as a string and manually parsing it.

config var not_array: string = '[1,2,3,4,5]' ;

proc main() {
    // config array workaround
    writeln("I am string. Definitely not array ", not_array) ;

    // parse string
    not_array = not_array.replace(' ','') ;
    not_array = not_array.replace('[','') ;
    not_array = not_array.replace(']','') ;
    var still_not_array = not_array.split(',') ;

    // prepare array
    var dom = 0..#still_not_array.size ;
    var array: [dom] real ;

    // populate array
    for (i, x) in zip(dom, still_not_array) {
        array[i] = x:real ;
    }
    writeln("Ha! Tricked you, am actually array ", array) ;
}

This works as intended, but is there a better way?

uqtredd1
  • 187
  • 3
  • One may always arrange inputs to take place from a configuration file ( a common step in a such configurable, pipeline organised computation flows ). Would you mind to explain your actual criteria for a fuzzy but important {worse|better|best}-ness of any alternative approach? Initial, post-compilation step is ( and always will ) a one-time cost in both [TIME] & [SPACE] dimensions of the process execution, so would you kindly explicitly state also an actual quantitative cost/reward-metric function, to compare any two approaches to solve an already working concept of run-time configurable input? – user3666197 Dec 08 '17 at 13:07
  • 1
    At this point I'm just experimenting with Chapel's features. I was just interested to see if it were possible to input arrays via the command line to make for example, command line polynomial interpolation script. What would a configuration file based approach look like for Chapel? – uqtredd1 Dec 09 '17 at 01:28

1 Answers1

4

Is it possible to declare an array with config?

No, this is not yet supported in Chapel as of Chapel 1.16.

That said, there are ways to work around this as you demonstrate.

As an alternative work-around, you can utilize IO calls to write the input string to memory and then read it in as an array, e.g.

config type  arrType = int;
config const arrSize = 3,
             arrString = '1 2 3';

var A : [1..arrSize] arrType;

// Create a memory buffer to write in
var f = openmem();

// Create a writer on the memory buffer and write the input string
var w = f.writer();
w.writeln(arrString);
w.close();

// Create a reader on the memory buffer and read the input string as an array
var r = f.reader();
r.readln(A);
r.close();

writeln(A);

Note this requires the array size up front. I think you'd have to do some string processing like your original example to compute that on the fly.

Some resources:

ben-albrecht
  • 1,785
  • 10
  • 23
  • 4
    I'll note that Chapel should some day support arrays as configs (I believe it's just a matter of implementing array: string casting). Also, my example makes me wish for a read() method on strings, similar to sscanf for C. That would reduce the example by ~7 lines of code. – ben-albrecht Dec 08 '17 at 15:47