NVIDIA / cccl

CUDA Core Compute Libraries
https://nvidia.github.io/cccl/
Other
1.21k stars 153 forks source link

thrust::distance does not work with std::ranges::iota_view #787

Open jeffhammond opened 3 years ago

jeffhammond commented 3 years ago

Is it unreasonable for me to expect this to work?

error

$ nvc++  -stdpar=multicore --c++20 -c thrust-ranges.cc 
"thrust-ranges.cc", line 15: error: no instance of function template "thrust::distance" matches the argument list
            argument types are: (std::ranges::iota_view<Index_type, Index_type>::_Iterator, std::ranges::iota_view<Index_type, Index_type>::_Iterator)
    auto d = thrust::distance(begin,end);
             ^

1 error detected in the compilation of "thrust-ranges.cc".

thrust-ranges.cc

#include <ranges>
#include <thrust/distance.h>

typedef std::ptrdiff_t Index_type;

void foo(void)
{
  const Index_type ibegin = 0;
  const Index_type iend   = 100;

  auto range = std::views::iota(ibegin, iend);
  auto begin = std::begin(range);
  auto end   = std::end(range);

  auto d = thrust::distance(begin,end);
}

Note that the type of Index_type doesn't matter. The same error occurs if I #define it to int.

alliepiper commented 3 years ago

I would expect that to work.

NVIDIA/thrust#1491 updated thrust::iterator_traits to support more types, this may address the issue since thrust::distance requires a valid specialization of those traits.

However, from your slack message, this also fails with std::distance so there may be more at play.

miscco commented 2 years ago

I believe this is a bug in totally fine by libstdc++

They are differentiating their implementation based on iterator_category which does not make sense for iota_view with ptrdiff_t as input type (This is because we need __int128 as a difference type there and that breaks some of the usual machinery)

One can see this nicely here: https://godbolt.org/z/Yo86zE4K4

Long story short. Both thrust and libstc++ are perfectly conforming in their implementation. It is just a bad user experience that old machinery does not support new iterator types.