Skip to content

Commit

Permalink
Minor cleanup to memory resources (#2308)
Browse files Browse the repository at this point in the history
miscco authored Aug 29, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent d862315 commit e42d7b7
Showing 7 changed files with 39 additions and 35 deletions.
Original file line number Diff line number Diff line change
@@ -31,9 +31,10 @@
# include <cuda/__memory_resource/properties.h>
# include <cuda/__memory_resource/resource.h>
# include <cuda/__memory_resource/resource_ref.h>
# include <cuda/std/__concepts/__concept_macros.h>
# include <cuda/std/__cuda/api_wrapper.h>
# include <cuda/std/__cuda/ensure_current_device.h>
# include <cuda/std/__new/bad_alloc.h>
# include <cuda/std/detail/libcxx/include/stdexcept>

# if _CCCL_STD_VER >= 2014

@@ -59,14 +60,14 @@ class device_memory_resource
//! @brief Allocate device memory of size at least \p __bytes.
//! @param __bytes The size in bytes of the allocation.
//! @param __alignment The requested alignment of the allocation.
//! @throw std::bad_alloc in case of invalid alignment or \c cuda::cuda_error of the returned error code.
//! @throw std::invalid_argument in case of invalid alignment or \c cuda::cuda_error of the returned error code.
//! @return Pointer to the newly allocated memory
_CCCL_NODISCARD void* allocate(const size_t __bytes, const size_t __alignment = default_cuda_malloc_alignment) const
{
// We need to ensure that the provided alignment matches the minimal provided alignment
if (!__is_valid_alignment(__alignment))
{
_CUDA_VSTD::__throw_bad_alloc();
_CUDA_VSTD::__throw_invalid_argument("Invalid alignment passed to device_memory_resource::allocate.");
}

// We need to ensure that we allocate on the right device as `cudaMalloc` always uses the current device
27 changes: 14 additions & 13 deletions libcudacxx/include/cuda/__memory_resource/managed_memory_resource.h
Original file line number Diff line number Diff line change
@@ -31,8 +31,9 @@
# include <cuda/__memory_resource/properties.h>
# include <cuda/__memory_resource/resource.h>
# include <cuda/__memory_resource/resource_ref.h>
# include <cuda/std/__concepts/__concept_macros.h>
# include <cuda/std/__cuda/api_wrapper.h>
# include <cuda/std/__new/bad_alloc.h>
# include <cuda/std/detail/libcxx/include/stdexcept>

# if _CCCL_STD_VER >= 2014

@@ -56,14 +57,14 @@ class managed_memory_resource
//! @brief Allocate CUDA unified memory of size at least \p __bytes.
//! @param __bytes The size in bytes of the allocation.
//! @param __alignment The requested alignment of the allocation.
//! @throw cuda::cuda_error of the returned error code
//! @throw std::invalid_argument in case of invalid alignment or \c cuda::cuda_error of the returned error code.
//! @return Pointer to the newly allocated memory
_CCCL_NODISCARD void* allocate(const size_t __bytes, const size_t __alignment = default_cuda_malloc_alignment) const
{
// We need to ensure that the provided alignment matches the minimal provided alignment
if (!__is_valid_alignment(__alignment))
{
_CUDA_VSTD::__throw_bad_alloc();
_CUDA_VSTD::__throw_invalid_argument("Invalid alignment passed to managed_memory_resource::allocate.");
}

void* __ptr{nullptr};
@@ -73,7 +74,7 @@ class managed_memory_resource
}

//! @brief Deallocate memory pointed to by \p __ptr.
//! @param __ptr Pointer to be deallocated. Must have been allocated through a call to `allocate`
//! @param __ptr Pointer to be deallocated. Must have been allocated through a call to `allocate`.
//! @param __bytes The number of bytes that was passed to the `allocate` call that returned \p __ptr.
//! @param __alignment The alignment that was passed to the `allocate` call that returned \p __ptr.
void deallocate(void* __ptr, const size_t, const size_t __alignment = default_cuda_malloc_alignment) const noexcept
@@ -85,26 +86,26 @@ class managed_memory_resource
(void) __alignment;
}

//! @brief Equality comparison with another \c managed_memory_resource
//! @param __other The other \c managed_memory_resource
//! @return Whether both \c managed_memory_resource were constructed with the same flags
//! @brief Equality comparison with another \c managed_memory_resource.
//! @param __other The other \c managed_memory_resource.
//! @return Whether both \c managed_memory_resource were constructed with the same flags.
_CCCL_NODISCARD constexpr bool operator==(managed_memory_resource const& __other) const noexcept
{
return __flags_ == __other.__flags_;
}
# if _CCCL_STD_VER <= 2017
//! @brief Inequality comparison with another \c managed_memory_resource
//! @param __other The other \c managed_memory_resource
//! @return Whether both \c managed_memory_resource were constructed with different flags
//! @brief Inequality comparison with another \c managed_memory_resource.
//! @param __other The other \c managed_memory_resource.
//! @return Whether both \c managed_memory_resource were constructed with different flags.
_CCCL_NODISCARD constexpr bool operator!=(managed_memory_resource const& __other) const noexcept
{
return __flags_ != __other.__flags_;
}
# endif // _CCCL_STD_VER <= 2017

//! @brief Equality comparison between a \c managed_memory_resource and another resource
//! @param __lhs The \c managed_memory_resource
//! @param __rhs The resource to compare to
//! @brief Equality comparison between a \c managed_memory_resource and another resource.
//! @param __lhs The \c managed_memory_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 <class _Resource>
27 changes: 14 additions & 13 deletions libcudacxx/include/cuda/__memory_resource/pinned_memory_resource.h
Original file line number Diff line number Diff line change
@@ -32,8 +32,9 @@
# include <cuda/__memory_resource/properties.h>
# include <cuda/__memory_resource/resource.h>
# include <cuda/__memory_resource/resource_ref.h>
# include <cuda/std/__concepts/__concept_macros.h>
# include <cuda/std/__cuda/api_wrapper.h>
# include <cuda/std/__new/bad_alloc.h>
# include <cuda/std/detail/libcxx/include/stdexcept>

# if _CCCL_STD_VER >= 2014

@@ -58,15 +59,15 @@ class pinned_memory_resource
//! @brief Allocate host memory of size at least \p __bytes.
//! @param __bytes The size in bytes of the allocation.
//! @param __alignment The requested alignment of the allocation.
//! @throw cuda::cuda_error if allocation fails with a CUDA error.
//! @throw std::invalid_argument in case of invalid alignment or \c cuda::cuda_error of the returned error code.
//! @return Pointer to the newly allocated memory
_CCCL_NODISCARD void* allocate(const size_t __bytes,
const size_t __alignment = default_cuda_malloc_host_alignment) const
{
// We need to ensure that the provided alignment matches the minimal provided alignment
if (!__is_valid_alignment(__alignment))
{
_CUDA_VSTD::__throw_bad_alloc();
_CUDA_VSTD::__throw_invalid_argument("Invalid alignment passed to pinned_memory_resource::allocate.");
}

void* __ptr{nullptr};
@@ -75,7 +76,7 @@ class pinned_memory_resource
}

//! @brief Deallocate memory pointed to by \p __ptr.
//! @param __ptr Pointer to be deallocated. Must have been allocated through a call to `allocate`
//! @param __ptr Pointer to be deallocated. Must have been allocated through a call to `allocate`.
//! @param __bytes The number of bytes that was passed to the `allocate` call that returned \p __ptr.
//! @param __alignment The alignment that was passed to the `allocate` call that returned \p __ptr.
void
@@ -88,26 +89,26 @@ class pinned_memory_resource
(void) __alignment;
}

//! @brief Equality comparison with another \c pinned_memory_resource
//! @param __other The other \c pinned_memory_resource
//! @return Whether both \c pinned_memory_resource were constructed with the same flags
//! @brief Equality comparison with another \c pinned_memory_resource.
//! @param __other The other \c pinned_memory_resource.
//! @return Whether both \c pinned_memory_resource were constructed with the same flags.
_CCCL_NODISCARD constexpr bool operator==(pinned_memory_resource const& __other) const noexcept
{
return __flags_ == __other.__flags_;
}
# if _CCCL_STD_VER <= 2017
//! @brief Equality comparison with another \c pinned_memory_resource
//! @param __other The other \c pinned_memory_resource
//! @return Whether both \c pinned_memory_resource were constructed with different flags
//! @brief Equality comparison with another \c pinned_memory_resource.
//! @param __other The other \c pinned_memory_resource.
//! @return Whether both \c pinned_memory_resource were constructed with different flags.
_CCCL_NODISCARD constexpr bool operator!=(pinned_memory_resource const& __other) const noexcept
{
return __flags_ != __other.__flags_;
}
# endif // _CCCL_STD_VER <= 2017

//! @brief Equality comparison between a \c pinned_memory_resource and another resource
//! @param __lhs The \c pinned_memory_resource
//! @param __rhs The resource to compare to
//! @brief Equality comparison between a \c pinned_memory_resource and another resource.
//! @param __lhs The \c pinned_memory_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 <class _Resource>
1 change: 1 addition & 0 deletions libcudacxx/include/cuda/__memory_resource/resource.h
Original file line number Diff line number Diff line change
@@ -24,6 +24,7 @@
#if !defined(_CCCL_COMPILER_MSVC_2017) && defined(LIBCUDACXX_ENABLE_EXPERIMENTAL_MEMORY_RESOURCE)

# include <cuda/__memory_resource/get_property.h>
# include <cuda/std/__concepts/__concept_macros.h>
# include <cuda/std/__concepts/all_of.h>
# include <cuda/std/__concepts/convertible_to.h>
# include <cuda/std/__concepts/equality_comparable.h>
Original file line number Diff line number Diff line change
@@ -62,7 +62,7 @@ void test()
auto* ptr = res.allocate(5, 42);
unused(ptr);
}
catch (const std::bad_alloc&)
catch (const std::invalid_argument&)
{
break;
}
@@ -78,7 +78,7 @@ void test()
auto* ptr = res.allocate(5, 1337);
unused(ptr);
}
catch (const std::bad_alloc&)
catch (const std::invalid_argument&)
{
break;
}
Original file line number Diff line number Diff line change
@@ -57,7 +57,7 @@ void test(const unsigned int flag)
auto* ptr = res.allocate(5, 42);
unused(ptr);
}
catch (const std::bad_alloc&)
catch (const std::invalid_argument&)
{
break;
}
@@ -73,7 +73,7 @@ void test(const unsigned int flag)
auto* ptr = res.allocate(5, 1337);
unused(ptr);
}
catch (const std::bad_alloc&)
catch (const std::invalid_argument&)
{
break;
}
Original file line number Diff line number Diff line change
@@ -57,7 +57,7 @@ void test(const unsigned int flag)
auto* ptr = res.allocate(5, 42);
unused(ptr);
}
catch (const std::bad_alloc&)
catch (const std::invalid_argument&)
{
break;
}
@@ -73,7 +73,7 @@ void test(const unsigned int flag)
auto* ptr = res.allocate(5, 1337);
unused(ptr);
}
catch (const std::bad_alloc&)
catch (const std::invalid_argument&)
{
break;
}

0 comments on commit e42d7b7

Please sign in to comment.