Skip to content

[Bug, Reduction] Dependency violation in kernels containing reductions nested within conditionals #533

Description

@kaushikcfd

Consider the kernel:

knl = lp.make_kernel(
    "{[i, j, k]: 0<=i,j,k<10}",
    """
    y[i] = 0 if sum(j, j) else sum(k, k)
    """)
print(lp.generate_code_v2(knl).device_code())

which generates:

void loopy_kernel(double* x, double* y)
{
  int acc_j;
  double acc_k;
  int tmp[10];

  for (int i = 0; i <= 9; ++i)
  {
    acc_k = 0.0;
    tmp[i] = i;
    acc_j = 0;
    if (!acc_j)
      for (int k = 0; k <= 9; ++k)
        acc_k = acc_k + x[10 * i + k];
    for (int j = 0; j <= 9; ++j)
      acc_j = acc_j + tmp[j];
    y[i] = (acc_j ? 0.0 : acc_k);
  }
}

Notice how acc_j is read before it is written. The reason being realize_reduction doesn't enforce dependencies between the statements during the transformation.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions