Skip to content

Commit

Permalink
make uninitialized[_async]_buffer's range accessors const-correct (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
ericniebler authored Jan 30, 2025
1 parent 51a890a commit 0ce59c7
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ private:
size_t __space = __get_allocation_size(__count_);
void* __ptr = __buf_;
return _CUDA_VSTD::launder(
reinterpret_cast<_Tp*>(_CUDA_VSTD::align(__alignment, __count_ * sizeof(_Tp), __ptr, __space)));
static_cast<_Tp*>(_CUDA_VSTD::align(__alignment, __count_ * sizeof(_Tp), __ptr, __space)));
}

//! @brief Causes the buffer to be treated as a span when passed to cudax::launch.
Expand Down Expand Up @@ -136,10 +136,12 @@ private:
}

public:
using value_type = _Tp;
using reference = _Tp&;
using pointer = _Tp*;
using size_type = size_t;
using value_type = _Tp;
using reference = _Tp&;
using const_reference = const _Tp&;
using pointer = _Tp*;
using const_pointer = const _Tp*;
using size_type = size_t;

//! @brief Constructs an \c uninitialized_async_buffer, allocating sufficient storage for \p __count elements through
//! \p __mr
Expand Down Expand Up @@ -215,20 +217,38 @@ public:
}

//! @brief Returns an aligned pointer to the first element in the buffer
_CCCL_NODISCARD _CCCL_HIDE_FROM_ABI constexpr pointer begin() const noexcept
_CCCL_NODISCARD _CCCL_HIDE_FROM_ABI constexpr pointer begin() noexcept
{
return __get_data();
}

//! @overload
_CCCL_NODISCARD _CCCL_HIDE_FROM_ABI constexpr const_pointer begin() const noexcept
{
return __get_data();
}

//! @brief Returns an aligned pointer to the element following the last element of the buffer.
//! This element acts as a placeholder; attempting to access it results in undefined behavior.
_CCCL_NODISCARD _CCCL_HIDE_FROM_ABI constexpr pointer end() const noexcept
_CCCL_NODISCARD _CCCL_HIDE_FROM_ABI constexpr pointer end() noexcept
{
return __get_data() + __count_;
}

//! @overload
_CCCL_NODISCARD _CCCL_HIDE_FROM_ABI constexpr const_pointer end() const noexcept
{
return __get_data() + __count_;
}

//! @brief Returns an aligned pointer to the first element in the buffer
_CCCL_NODISCARD _CCCL_HIDE_FROM_ABI constexpr pointer data() const noexcept
_CCCL_NODISCARD _CCCL_HIDE_FROM_ABI constexpr pointer data() noexcept
{
return __get_data();
}

//! @overload
_CCCL_NODISCARD _CCCL_HIDE_FROM_ABI constexpr const_pointer data() const noexcept
{
return __get_data();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ private:
size_t __space = __get_allocation_size(__count_);
void* __ptr = __buf_;
return _CUDA_VSTD::launder(
reinterpret_cast<_Tp*>(_CUDA_VSTD::align(__alignment, __count_ * sizeof(_Tp), __ptr, __space)));
static_cast<_Tp*>(_CUDA_VSTD::align(__alignment, __count_ * sizeof(_Tp), __ptr, __space)));
}

//! @brief Causes the buffer to be treated as a span when passed to cudax::launch.
Expand All @@ -124,10 +124,12 @@ private:
}

public:
using value_type = _Tp;
using reference = _Tp&;
using pointer = _Tp*;
using size_type = size_t;
using value_type = _Tp;
using reference = _Tp&;
using const_reference = const _Tp&;
using pointer = _Tp*;
using const_pointer = const _Tp*;
using size_type = size_t;

//! @brief Constructs an \c uninitialized_buffer and allocates sufficient storage for \p __count elements through
//! \p __mr
Expand Down Expand Up @@ -198,20 +200,38 @@ public:
}

//! @brief Returns an aligned pointer to the first element in the buffer
_CCCL_NODISCARD _CCCL_HIDE_FROM_ABI pointer begin() const noexcept
_CCCL_NODISCARD _CCCL_HIDE_FROM_ABI pointer begin() noexcept
{
return __get_data();
}

//! @overload
_CCCL_NODISCARD _CCCL_HIDE_FROM_ABI const_pointer begin() const noexcept
{
return __get_data();
}

//! @brief Returns an aligned pointer to the element following the last element of the buffer.
//! This element acts as a placeholder; attempting to access it results in undefined behavior.
_CCCL_NODISCARD _CCCL_HIDE_FROM_ABI pointer end() const noexcept
_CCCL_NODISCARD _CCCL_HIDE_FROM_ABI pointer end() noexcept
{
return __get_data() + __count_;
}

//! @overload
_CCCL_NODISCARD _CCCL_HIDE_FROM_ABI const_pointer end() const noexcept
{
return __get_data() + __count_;
}

//! @brief Returns an aligned pointer to the first element in the buffer
_CCCL_NODISCARD _CCCL_HIDE_FROM_ABI pointer data() const noexcept
_CCCL_NODISCARD _CCCL_HIDE_FROM_ABI pointer data() noexcept
{
return __get_data();
}

//! @overload
_CCCL_NODISCARD _CCCL_HIDE_FROM_ABI const_pointer data() const noexcept
{
return __get_data();
}
Expand Down
6 changes: 6 additions & 0 deletions cudax/test/containers/uninitialized_async_buffer.cu
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,9 @@ TEMPLATE_TEST_CASE(
SECTION("access")
{
uninitialized_async_buffer buf{resource, stream, 42};
static_assert(cuda::std::is_same<decltype(buf.begin()), TestType*>::value, "");
static_assert(cuda::std::is_same<decltype(buf.end()), TestType*>::value, "");
static_assert(cuda::std::is_same<decltype(buf.data()), TestType*>::value, "");
CUDAX_CHECK(buf.data() != nullptr);
CUDAX_CHECK(buf.size() == 42);
CUDAX_CHECK(buf.size_bytes() == 42 * sizeof(TestType));
Expand All @@ -145,6 +148,9 @@ TEMPLATE_TEST_CASE(
CUDAX_CHECK(buf.get_stream() == stream);
CUDAX_CHECK(buf.get_memory_resource() == resource);

static_assert(cuda::std::is_same<decltype(cuda::std::as_const(buf).begin()), TestType const*>::value, "");
static_assert(cuda::std::is_same<decltype(cuda::std::as_const(buf).end()), TestType const*>::value, "");
static_assert(cuda::std::is_same<decltype(cuda::std::as_const(buf).data()), TestType const*>::value, "");
CUDAX_CHECK(cuda::std::as_const(buf).data() != nullptr);
CUDAX_CHECK(cuda::std::as_const(buf).size() == 42);
CUDAX_CHECK(cuda::std::as_const(buf).size_bytes() == 42 * sizeof(TestType));
Expand Down
6 changes: 6 additions & 0 deletions cudax/test/containers/uninitialized_buffer.cu
Original file line number Diff line number Diff line change
Expand Up @@ -155,13 +155,19 @@ TEMPLATE_TEST_CASE(
SECTION("access")
{
uninitialized_buffer buf{resource, 42};
static_assert(cuda::std::is_same<decltype(buf.begin()), TestType*>::value, "");
static_assert(cuda::std::is_same<decltype(buf.end()), TestType*>::value, "");
static_assert(cuda::std::is_same<decltype(buf.data()), TestType*>::value, "");
CUDAX_CHECK(buf.data() != nullptr);
CUDAX_CHECK(buf.size() == 42);
CUDAX_CHECK(buf.size_bytes() == 42 * sizeof(TestType));
CUDAX_CHECK(buf.begin() == buf.data());
CUDAX_CHECK(buf.end() == buf.begin() + buf.size());
CUDAX_CHECK(buf.get_memory_resource() == resource);

static_assert(cuda::std::is_same<decltype(cuda::std::as_const(buf).begin()), TestType const*>::value, "");
static_assert(cuda::std::is_same<decltype(cuda::std::as_const(buf).end()), TestType const*>::value, "");
static_assert(cuda::std::is_same<decltype(cuda::std::as_const(buf).data()), TestType const*>::value, "");
CUDAX_CHECK(cuda::std::as_const(buf).data() != nullptr);
CUDAX_CHECK(cuda::std::as_const(buf).size() == 42);
CUDAX_CHECK(cuda::std::as_const(buf).begin() == buf.data());
Expand Down

0 comments on commit 0ce59c7

Please sign in to comment.