Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor cleanup to memory resources #2308

Merged
merged 1 commit into from
Aug 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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};
Expand All @@ -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
Expand All @@ -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>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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};
Expand All @@ -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
Expand All @@ -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>
Expand Down
1 change: 1 addition & 0 deletions libcudacxx/include/cuda/__memory_resource/resource.h
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand Down
Loading