From 8536b918e044ae31f8cf5618fe8ce2ec6df81a3a Mon Sep 17 00:00:00 2001 From: Eric Niebler Date: Fri, 6 Dec 2024 14:01:45 -0800 Subject: [PATCH] fix assorted problems in cudax memory resource equality fns (#3079) --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> --- .../device_memory_resource.cuh | 101 ++++++------------ .../managed_memory_resource.cuh | 58 +++++----- .../pinned_memory_resource.cuh | 55 ++++------ 3 files changed, 77 insertions(+), 137 deletions(-) diff --git a/cudax/include/cuda/experimental/__memory_resource/device_memory_resource.cuh b/cudax/include/cuda/experimental/__memory_resource/device_memory_resource.cuh index 7c5076602c5..747698c63f7 100644 --- a/cudax/include/cuda/experimental/__memory_resource/device_memory_resource.cuh +++ b/cudax/include/cuda/experimental/__memory_resource/device_memory_resource.cuh @@ -43,8 +43,6 @@ # include # include -# if _CCCL_STD_VER >= 2014 - //! @file //! The \c device_memory_pool class provides an asynchronous memory resource that allocates device memory in stream //! order. @@ -301,7 +299,7 @@ public: { return __pool_ == __rhs.__pool_; } -# if _CCCL_STD_VER <= 2017 +# if _CCCL_STD_VER <= 2017 //! @brief Inequality comparison with another \c device_memory_resource. //! @returns true if underlying \c cudaMemPool_t are inequal. @@ -309,16 +307,13 @@ public: { return __pool_ != __rhs.__pool_; } -# endif // _CCCL_STD_VER <= 2017 +# endif // _CCCL_STD_VER <= 2017 -# if _CCCL_STD_VER >= 2020 - //! @brief Equality comparison between a \c device_memory_resource and another resource. - //! @param __rhs The resource to compare to. - //! @returns If the underlying types are equality comparable, returns the result of equality comparison of both - //! resources. Otherwise, returns false. - _CCCL_TEMPLATE(class _Resource) - _CCCL_REQUIRES((_CUDA_VMR::__different_resource) ) - _CCCL_NODISCARD bool operator==(_Resource const& __rhs) const noexcept +# ifndef _CCCL_DOXYGEN_INVOKED // Do not document + +private: + template + _CCCL_NODISCARD bool __equal_to(_Resource const& __rhs) const noexcept { if constexpr (has_property<_Resource, device_accessible>) { @@ -330,94 +325,64 @@ public: return false; } } -# else // ^^^ C++20 ^^^ / vvv C++17 - template - _CCCL_NODISCARD_FRIEND auto operator==(device_memory_resource const& __lhs, _Resource const& __rhs) noexcept - _CCCL_TRAILING_REQUIRES(bool)( - _CUDA_VMR::__different_resource&& has_property<_Resource, device_accessible>) - { - return _CUDA_VMR::resource_ref{const_cast(__lhs)} - == _CUDA_VMR::resource_ref{const_cast<_Resource&>(__rhs)}; - } +public: +# if _CCCL_STD_VER >= 2020 + //! @brief Equality comparison between a \c device_memory_resource and another resource. + //! @param __rhs The resource to compare to. + //! @returns If the underlying types are equality comparable, returns the result of equality comparison of both + //! resources. Otherwise, returns false. template - _CCCL_NODISCARD_FRIEND auto operator==(device_memory_resource const&, _Resource const&) noexcept - _CCCL_TRAILING_REQUIRES(bool)(_CUDA_VMR::__different_resource - && !has_property<_Resource, device_accessible>) + requires _CUDA_VMR::__different_resource + _CCCL_NODISCARD bool operator==(_Resource const& __rhs) const noexcept { - return false; + return this->__equal_to(__rhs); } - +# else // ^^^ C++20 ^^^ / vvv C++17 template - _CCCL_NODISCARD_FRIEND auto operator==(_Resource const& __rhs, device_memory_resource const& __lhs) noexcept - _CCCL_TRAILING_REQUIRES(bool)( - _CUDA_VMR::__different_resource&& has_property<_Resource, device_accessible>) + _CCCL_NODISCARD_FRIEND auto operator==(device_memory_resource const& __lhs, _Resource const& __rhs) noexcept + _CCCL_TRAILING_REQUIRES(bool)(_CUDA_VMR::__different_resource) { - return _CUDA_VMR::resource_ref{const_cast(__lhs)} - == _CUDA_VMR::resource_ref{const_cast<_Resource&>(__rhs)}; + return __lhs.__equal_to(__rhs); } template - _CCCL_NODISCARD_FRIEND auto operator==(_Resource const&, device_memory_resource const&) noexcept - _CCCL_TRAILING_REQUIRES(bool)(_CUDA_VMR::__different_resource - && !has_property<_Resource, device_accessible>) + _CCCL_NODISCARD_FRIEND auto operator==(_Resource const& __lhs, device_memory_resource const& __rhs) noexcept + _CCCL_TRAILING_REQUIRES(bool)(_CUDA_VMR::__different_resource) { - return false; + return __rhs.__equal_to(__lhs); } template _CCCL_NODISCARD_FRIEND auto operator!=(device_memory_resource const& __lhs, _Resource const& __rhs) noexcept - _CCCL_TRAILING_REQUIRES(bool)( - _CUDA_VMR::__different_resource&& has_property<_Resource, device_accessible>) + _CCCL_TRAILING_REQUIRES(bool)(_CUDA_VMR::__different_resource) { - return _CUDA_VMR::resource_ref{const_cast(__lhs)} - != _CUDA_VMR::resource_ref{const_cast<_Resource&>(__rhs)}; + return !__lhs.__equal_to(__rhs); } template - _CCCL_NODISCARD_FRIEND auto operator!=(device_memory_resource const&, _Resource const&) noexcept - _CCCL_TRAILING_REQUIRES(bool)(_CUDA_VMR::__different_resource - && !has_property<_Resource, device_accessible>) + _CCCL_NODISCARD_FRIEND auto operator!=(_Resource const& __lhs, device_memory_resource const& __rhs) noexcept + _CCCL_TRAILING_REQUIRES(bool)(_CUDA_VMR::__different_resource) { - return true; - } - - template - _CCCL_NODISCARD_FRIEND auto operator!=(_Resource const& __rhs, device_memory_resource const& __lhs) noexcept - _CCCL_TRAILING_REQUIRES(bool)( - _CUDA_VMR::__different_resource&& has_property<_Resource, device_accessible>) - { - return _CUDA_VMR::resource_ref{const_cast(__lhs)} - != _CUDA_VMR::resource_ref{const_cast<_Resource&>(__rhs)}; - } - - template - _CCCL_NODISCARD_FRIEND auto operator!=(_Resource const&, device_memory_resource const&) noexcept - _CCCL_TRAILING_REQUIRES(bool)(_CUDA_VMR::__different_resource - && !has_property<_Resource, device_accessible>) - { - return true; + return !__rhs.__equal_to(__lhs); } # endif // _CCCL_STD_VER <= 2017 + //! @brief Enables the \c device_accessible property for \c device_memory_resource. + //! @relates device_memory_resource + friend constexpr void get_property(device_memory_resource const&, device_accessible) noexcept {} +# endif // _CCCL_DOXYGEN_INVOKED + //! @brief Returns the underlying handle to the CUDA memory pool. _CCCL_NODISCARD constexpr cudaMemPool_t get() const noexcept { return __pool_; } - -# ifndef _CCCL_DOXYGEN_INVOKED // Doxygen cannot handle the friend function - //! @brief Enables the \c device_accessible property for \c device_memory_resource. - //! @relates device_memory_resource - friend constexpr void get_property(device_memory_resource const&, device_accessible) noexcept {} -# endif // _CCCL_DOXYGEN_INVOKED }; static_assert(_CUDA_VMR::resource_with, ""); } // namespace cuda::experimental -# endif // _CCCL_STD_VER >= 2014 - #endif // !_CCCL_COMPILER(MSVC2017) && _CCCL_CUDACC_AT_LEAST(11, 2) #endif //_CUDAX__MEMORY_RESOURCE_CUDA_DEVICE_MEMORY_RESOURCE diff --git a/cudax/include/cuda/experimental/__memory_resource/managed_memory_resource.cuh b/cudax/include/cuda/experimental/__memory_resource/managed_memory_resource.cuh index 0baba56ba3a..a7c286ab644 100644 --- a/cudax/include/cuda/experimental/__memory_resource/managed_memory_resource.cuh +++ b/cudax/include/cuda/experimental/__memory_resource/managed_memory_resource.cuh @@ -160,76 +160,66 @@ public: #endif // _CCCL_STD_VER <= 2017 #ifndef _CCCL_DOXYGEN_INVOKED // Do not document -# if _CCCL_STD_VER >= 2020 - //! @brief Equality comparison between a \c managed_memory_resource and another resource - //! @param __rhs The resource to compare to - //! @return If the underlying types are equality comparable, returns the result of equality comparison of both - //! resources. Otherwise, returns false. - _CCCL_TEMPLATE(class _Resource) - _CCCL_REQUIRES(_CUDA_VMR::__different_resource) - _CCCL_NODISCARD bool operator==(_Resource const& __rhs) const noexcept + +private: + template + _CCCL_NODISCARD bool __equal_to(_Resource const& __rhs) const noexcept { if constexpr (has_property<_Resource, mr::device_accessible>) { return _CUDA_VMR::resource_ref{const_cast(this)} == _CUDA_VMR::resource_ref{const_cast<_Resource&>(__rhs)}; } - else if constexpr (has_property<_Resource, mr::device_accessible>) + else if constexpr (has_property<_Resource, mr::host_accessible>) { - return _CUDA_VMR::resource_ref{const_cast(this)} - == _CUDA_VMR::resource_ref{const_cast<_Resource&>(__rhs)}; + return _CUDA_VMR::resource_ref{const_cast(this)} + == _CUDA_VMR::resource_ref{const_cast<_Resource&>(__rhs)}; } else { return false; } } -# else // ^^^ C++20 ^^^ / vvv C++17 + +public: +# if _CCCL_STD_VER >= 2020 + //! @brief Equality comparison between a \c managed_memory_resource and another resource + //! @param __rhs The resource to compare to + //! @return If the underlying types are equality comparable, returns the result of equality comparison of both + //! resources. Otherwise, returns false. template - _CCCL_NODISCARD_FRIEND auto operator==(managed_memory_resource const& __lhs, _Resource const& __rhs) noexcept - _CCCL_TRAILING_REQUIRES(bool)(_CUDA_VMR::__different_resource&& - has_property<_Resource, mr::device_accessible>) + requires _CUDA_VMR::__different_resource + _CCCL_NODISCARD bool operator==(_Resource const& __rhs) const noexcept { - return _CUDA_VMR::resource_ref{const_cast(__lhs)} - == _CUDA_VMR::resource_ref{const_cast<_Resource&>(__rhs)}; + return this->__equal_to(__rhs); } +# else // ^^^ C++20 ^^^ / vvv C++17 template _CCCL_NODISCARD_FRIEND auto operator==(managed_memory_resource const& __lhs, _Resource const& __rhs) noexcept - _CCCL_TRAILING_REQUIRES(bool)( - _CUDA_VMR::__different_resource - && !has_property<_Resource, mr::device_accessible> && has_property<_Resource, mr::device_accessible>) - { - return _CUDA_VMR::resource_ref{const_cast(__lhs)} - == _CUDA_VMR::resource_ref{const_cast<_Resource&>(__rhs)}; - } - template - _CCCL_NODISCARD_FRIEND auto operator==(managed_memory_resource const&, _Resource const&) noexcept - _CCCL_TRAILING_REQUIRES(bool)( - _CUDA_VMR::__different_resource - && !has_property<_Resource, mr::device_accessible> && !has_property<_Resource, mr::device_accessible>) + _CCCL_TRAILING_REQUIRES(bool)(_CUDA_VMR::__different_resource) { - return false; + return __lhs.__equal_to(__rhs); } template _CCCL_NODISCARD_FRIEND auto operator==(_Resource const& __lhs, managed_memory_resource const& __rhs) noexcept _CCCL_TRAILING_REQUIRES(bool)(_CUDA_VMR::__different_resource) { - return __rhs == __lhs; + return __rhs.__equal_to(__lhs); } template _CCCL_NODISCARD_FRIEND auto operator!=(managed_memory_resource const& __lhs, _Resource const& __rhs) noexcept _CCCL_TRAILING_REQUIRES(bool)(_CUDA_VMR::__different_resource) { - return !(__lhs == __rhs); + return !__lhs.__equal_to(__rhs); } template - _CCCL_NODISCARD_FRIEND auto operator!=(_Resource const& __rhs, managed_memory_resource const& __lhs) noexcept + _CCCL_NODISCARD_FRIEND auto operator!=(_Resource const& __lhs, managed_memory_resource const& __rhs) noexcept _CCCL_TRAILING_REQUIRES(bool)(_CUDA_VMR::__different_resource) { - return !(__rhs == __lhs); + return !__rhs.__equal_to(__lhs); } # endif // _CCCL_STD_VER <= 2017 diff --git a/cudax/include/cuda/experimental/__memory_resource/pinned_memory_resource.cuh b/cudax/include/cuda/experimental/__memory_resource/pinned_memory_resource.cuh index 6993b1d6958..fa3d2c44f8a 100644 --- a/cudax/include/cuda/experimental/__memory_resource/pinned_memory_resource.cuh +++ b/cudax/include/cuda/experimental/__memory_resource/pinned_memory_resource.cuh @@ -161,77 +161,62 @@ public: #endif // _CCCL_STD_VER <= 2017 #ifndef _CCCL_DOXYGEN_INVOKED // Do not document -# if _CCCL_STD_VER >= 2020 - //! @brief Equality comparison between a \c pinned_memory_resource and another resource - //! @param __rhs The resource to compare to - //! @return If the underlying types are equality comparable, returns the result of equality comparison of both - //! resources. Otherwise, returns false. - _CCCL_TEMPLATE(class _Resource) - _CCCL_REQUIRES(_CUDA_VMR::__different_resource) - _CCCL_NODISCARD bool operator==(_Resource const& __rhs) const noexcept + template + _CCCL_NODISCARD bool __equal_to(_Resource const& __rhs) const noexcept { if constexpr (has_property<_Resource, device_accessible>) { return _CUDA_VMR::resource_ref{const_cast(this)} == _CUDA_VMR::resource_ref{const_cast<_Resource&>(__rhs)}; } - else if constexpr (has_property<_Resource, device_accessible>) + else if constexpr (has_property<_Resource, host_accessible>) { - return _CUDA_VMR::resource_ref{const_cast(this)} - == _CUDA_VMR::resource_ref{const_cast<_Resource&>(__rhs)}; + return _CUDA_VMR::resource_ref{const_cast(this)} + == _CUDA_VMR::resource_ref{const_cast<_Resource&>(__rhs)}; } else { return false; } } -# else // ^^^ C++20 ^^^ / vvv C++17 +# if _CCCL_STD_VER >= 2020 + //! @brief Equality comparison between a \c pinned_memory_resource and another resource + //! @param __rhs The resource to compare to + //! @return If the underlying types are equality comparable, returns the result of equality comparison of both + //! resources. Otherwise, returns false. template - _CCCL_NODISCARD_FRIEND auto operator==(pinned_memory_resource const& __lhs, _Resource const& __rhs) noexcept - _CCCL_TRAILING_REQUIRES(bool)( - _CUDA_VMR::__different_resource&& ::cuda::has_property<_Resource, device_accessible>) + requires _CUDA_VMR::__different_resource + _CCCL_NODISCARD bool operator==(_Resource const& __rhs) const noexcept { - return _CUDA_VMR::resource_ref{const_cast(__lhs)} - == _CUDA_VMR::resource_ref{const_cast<_Resource&>(__rhs)}; + return this->__equal_to(__rhs); } +# else // ^^^ C++20 ^^^ / vvv C++17 template _CCCL_NODISCARD_FRIEND auto operator==(pinned_memory_resource const& __lhs, _Resource const& __rhs) noexcept - _CCCL_TRAILING_REQUIRES(bool)( - _CUDA_VMR::__different_resource - && !::cuda::has_property<_Resource, device_accessible> && ::cuda::has_property<_Resource, device_accessible>) - { - return _CUDA_VMR::resource_ref{const_cast(__lhs)} - == _CUDA_VMR::resource_ref{const_cast<_Resource&>(__rhs)}; - } - template - _CCCL_NODISCARD_FRIEND auto operator==(pinned_memory_resource const&, _Resource const&) noexcept - _CCCL_TRAILING_REQUIRES(bool)( - _CUDA_VMR::__different_resource - && !::cuda::has_property<_Resource, device_accessible> && !::cuda::has_property<_Resource, device_accessible>) + _CCCL_TRAILING_REQUIRES(bool)(_CUDA_VMR::__different_resource) { - return false; + return __lhs.__equal_to(__rhs); } template _CCCL_NODISCARD_FRIEND auto operator==(_Resource const& __lhs, pinned_memory_resource const& __rhs) noexcept _CCCL_TRAILING_REQUIRES(bool)(_CUDA_VMR::__different_resource) { - return __rhs == __lhs; + return __rhs.__equal_to(__lhs); } template _CCCL_NODISCARD_FRIEND auto operator!=(pinned_memory_resource const& __lhs, _Resource const& __rhs) noexcept _CCCL_TRAILING_REQUIRES(bool)(_CUDA_VMR::__different_resource) { - return !(__lhs == __rhs); + return !__lhs.__equal_to(__rhs); } template - _CCCL_NODISCARD_FRIEND auto operator!=(_Resource const& __rhs, pinned_memory_resource const& __lhs) noexcept + _CCCL_NODISCARD_FRIEND auto operator!=(_Resource const& __lhs, pinned_memory_resource const& __rhs) noexcept _CCCL_TRAILING_REQUIRES(bool)(_CUDA_VMR::__different_resource) { - return !(__rhs == __lhs); + return !__rhs.__equal_to(__lhs); } # endif // _CCCL_STD_VER <= 2017