chapel-lang / chapel

a Productive Parallel Programming Language
https://chapel-lang.org
Other
1.78k stars 420 forks source link

Replicated array assign/init behaves differently depending on copy elision #24595

Open vasslitvinov opened 7 months ago

vasslitvinov commented 7 months ago

A replicated array presents itself as its replicand on the current locale, by design. However, it is passed "as a whole" upon a move initialization. This may lead to subtle changes in program behavior when copy elision engages or disengages depending on a code change elsewhere. Analogously, a user may expect that an assignment or initialization between two replicated arrays copies over all replicands, whereas currently it does just the current replicand, unless it is a move initialization.

This issue requests revisiting these semantics, aiming at consistent behavior to reduce user surprises.

use ReplicatedDist;

var localDom = {1..3};
var replDom = localDom dmapped replicatedDist();

proc useArray(in arr) {
  showArray(arr, "in useArray");
}

proc showArray(arr, msg) {
  writeln(msg);
  for loc in Locales do on loc do writeln(" ", arr);
}

proc makeReplicatedArray() {
  var result: [replDom] real;
  for loc in Locales do on loc do
    for i in localDom do
      result[i] = here.id + i*0.1;
  return result;
}

proc main {
  var replArr1 = makeReplicatedArray();
  showArray(replArr1, "replArr1");

  useArray(replArr1);              // passes only the first replicand

  var replArr2: replArr1.type;
  replArr2; // ensure the next line is an assignment
  replArr2 = replArr1;             // copies only the first replicand
  showArray(replArr2, "replArr2");

  var replArr3 = replArr1;         // uses only the first replicand
  showArray(replArr3, "replArr3");

  var replArr4 = replArr1;         // uses ALL replicands b/c copy elision
  showArray(replArr4, "replArr4");
}

Test: test/distributions/replicated/one-or-all-replicands-1.chpl

mstrout commented 7 months ago

Is this the issue that Oliver from the Arkouda Devs meeting brought to our attention? If so, could we provide some context of how this showed up as a problem for Oliver?

vasslitvinov commented 7 months ago

Not sure. My understanding is that Oliver spent quite a bit of time figuring out what is going on and what is the right way to use replicated arrays in his context. It surely took me a while.