cccl icon indicating copy to clipboard operation
cccl copied to clipboard

Missing two range overload for `thrust::transform_reduce`

Open codereport opened this issue 3 years ago • 4 comments

I was recently writing the following code using C++17:

auto max_gap(std::vector<int> nums) -> int {
    return std::transform_reduce(
        nums.begin() + 1, nums.end(), nums.begin(), 0,  
        [](auto a, auto b) { return std::max(a, b); }, 
        std::minus{});
}

and when I went to write the equivalent in Thrust it wasn't possible because there is no two range overload of thrust::transform_reduce. The C++17 std::transform_reduce has both a one range and two range overload. Because of this, the morally equivalent code in Thrust is:

auto max_gap(std::vector<int> nums) -> int {
    return thrust::transform_reduce(
        thrust::make_zip_iterator(thrust::make_tuple(nums.begin() + 1, nums.begin())), 
        thrust::make_zip_iterator(thrust::make_tuple(nums.end(),       nums.end() - 1)),
        [](auto t) { return thrust::get<0>(t) - thrust::get<1>(t); }, 0, 
        [](auto a, auto b) { return std::max(a, b); });
}

which is far from ideal.

I would recommend adding the two range overload of thrust::transform_reduce in order to:

  1. Be more consistent with what is available in standard C++
  2. Avoid the necessary code in the 2nd example above.

codereport avatar Mar 15 '23 19:03 codereport

I'd be happy to work on this if a maintainer would be willing to accept a PR implementing this functionality.

cc @senior-zero

11998956 avatar May 07 '23 01:05 11998956

@tfsingh thank you for reaching out and offering to contribute to our library. We welcome contributions from the community. Feel free to open a PR, and please do not hesitate to discuss any questions that require clarification on this issue or the relevant PR.

gevtushenko avatar May 07 '23 07:05 gevtushenko

Sounds great, @senior-zero . There may be a bit of a learning curve as this will be my first time working on thrust, but I'm excited to make a contribution.

11998956 avatar May 07 '23 19:05 11998956

Closing for the same reason as https://github.com/NVIDIA/cccl/issues/5160#issuecomment-5096893432. @codereport if you are still interested, try cuda:: PSTL and if overload is not there, file another issue.

gevtushenko avatar Oct 08 '26 20:10 gevtushenko