thrust icon indicating copy to clipboard operation
thrust copied to clipboard

Reduction function with lvlaue reference parameters should be a compilation error

Open dkolsen-pgi opened this issue 3 years ago • 0 comments

This code compiles cleanly with NVCC. It should be a compilation error. The reduction function takes it's parameters by lvalue reference. But the arguments to the reduction function are the result of the transformation function, which in this case are not lvalues. Fixing this will result in problematic code being caught at compile time rather leaving the user puzzling over strange runtime behavior. I am guessing that fixing this requires adding std::move (or equivalent) in the right places, though I don't know where those places are.

#include <stdio.h>
#include <thrust/host_vector.h>
#include <thrust/device_vector.h>
#include <thrust/transform_reduce.h>

struct trans_op {
  __device__ int operator()(int z) const { return z + 1; }
};
struct reduce_op {
  __device__ int operator()(int& x, int& y) const { return x + y; }
};

int main() {
  thrust::host_vector<int> hv(1000, 2);
  thrust::device_vector<int> dv(hv);
  int sum = thrust::transform_reduce(dv.begin(), dv.end(), trans_op(), 0, reduce_op());
  printf("%d\n", sum);
}

This bug was originally reported against nvc++ -stdpar. See NVBug 3631502.

The Thrust OpenMP back end does not appear to have this problem, at least not with thrust::transform_reduce. I get a compilation error as expected when compiling the test in NVBug 3631502 with nvc++ -stdpar=multicore. I haven't tried any back ends other than CUDA and OpenMP.

dkolsen-pgi avatar May 12 '22 05:05 dkolsen-pgi