Chapel abbreviations currently ignore the initial values of variables. This means that this code
var x: int; for i in 1..3 { forall j in 1..10 with (+ reduce x) { x += 1; } } writeln(x);
returns 10, not 30, as this user naively thought. Although this behavior is great (and it’s documented in the notes on the reduction proposals — I just didn’t think about it), it turns out that if I want to get 30 (by accumulating over both loops), I need to actually do the amount manually. I think it would be rather elegant and symmetrical for for loops to also have the intention of reduce .... i.e. I would like to write
var x: int; for i in 1..3 with (+ reduce x) { forall j in 1..10 with (+ reduce x) { x += 1; } } writeln(x);
Please note that even when summing numbers, I need to enter a temporary variable. For max / min, like operations, you need to be even more careful.
Is there a reason not to support reduce intents inside loops? Alternatively, is there a more idiomatic way (Chapel-rrific) for this?
UPDATE. The more I think about it, the more obvious that my proposed code will work if the external for been replaced by forall . I think the problem is that the variables are local, not iterative, local, so the reduction will only happen on tasks. Thus, a separate internal recovery step will still be required. This may be due to the need for a temporary variable.
I think the more comprehensive question is that the right way to do these kinds of nested abbreviations is ...
source share