chapel-lang / chapel

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

Warn for whole-array assignments to outer ref arrays within parallel loops #23609

Open bradcray opened 1 year ago

bradcray commented 1 year ago

At times, we've run into confusing correctness or performance issues when accidentally doing whole-array assignment within a parallel loop, where the inention was to just assign one element. For example:

// an attempt at a simple stream triad benchmark
var A, B, C: [1..n] real;

forall i in 1..n do
  A = B + C;  // oops, I meant A[i] = B[i] + C[i];  ...

or, more recently, in the code found in https://github.com/chapel-lang/chapel/issues/23602#issuecomment-1753378157.

This issue proposes that when we find a whole-array assignment being performed in a top-level statement of a parallel loop (or, alternatively, a non-top-level statement that we know will be executed in every iteration / more than one iteration), we generate a warning if that array is an outer variable that is available by ref parallel intent within the loop. For example, the above code might generate a warning like:

code.chpl:5: warning: whole-array assignment to outer array 'A' is likely to lead to race conditions—is this what you intended?

Cases that should not generate warnings include:

forall i in 1..n {
  var A, B, C: [1..n] real;
  A = B + C;  // OK, we're modifying a local variable
}
var A, B, C: [1..n] real;
forall i in 1..n with (in A) do
  A = B + C;  // OK, we're modifying a local, per-task variable
var A, B, C: [1..n] real;
forall i in 1..n do
  if someTest then
    A = B + C;  // OK since we're potentially only doing this computation once if 'someTest' is written intelligently
bradcray commented 1 year ago

Despite being a fairly seasoned Chapel programmer, I just hit a case where I would've benefitted from this... :(