Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 2 additions & 0 deletions cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,13 @@ add_library(
src/mr/detail/logging_resource_adaptor_impl.cpp
src/mr/detail/pool_memory_resource_impl.cpp
src/mr/detail/pool_memory_resource_impl.cpp
src/mr/detail/prefetch_resource_adaptor_impl.cpp
src/mr/detail/statistics_resource_adaptor_impl.cpp
src/mr/detail/tracking_resource_adaptor_impl.cpp
src/mr/fixed_size_memory_resource.cpp
src/mr/logging_resource_adaptor.cpp
src/mr/pool_memory_resource.cpp
src/mr/prefetch_resource_adaptor.cpp
src/mr/statistics_resource_adaptor.cpp
src/mr/tracking_resource_adaptor.cpp
src/prefetch.cpp)
Expand Down
74 changes: 74 additions & 0 deletions cpp/include/rmm/mr/detail/prefetch_resource_adaptor_impl.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* SPDX-FileCopyrightText: Copyright (c) 2024-2026, NVIDIA CORPORATION.
* SPDX-License-Identifier: Apache-2.0
*/
#pragma once

#include <rmm/detail/export.hpp>
#include <rmm/resource_ref.hpp>

#include <cuda/memory_resource>

#include <cstddef>

namespace RMM_NAMESPACE {
namespace mr {
namespace detail {

/**
* @brief Implementation class for prefetch_resource_adaptor.
*
* Prefetches all allocations to the current device. This class satisfies the
* CCCL `cuda::mr::resource` concept and is held by `prefetch_resource_adaptor`
* via `cuda::mr::shared_resource` for reference-counted ownership.
*/
class prefetch_resource_adaptor_impl {
public:
explicit prefetch_resource_adaptor_impl(device_async_resource_ref upstream);

~prefetch_resource_adaptor_impl() = default;

prefetch_resource_adaptor_impl(prefetch_resource_adaptor_impl const&) = delete;
prefetch_resource_adaptor_impl(prefetch_resource_adaptor_impl&&) = delete;
prefetch_resource_adaptor_impl& operator=(prefetch_resource_adaptor_impl const&) = delete;
prefetch_resource_adaptor_impl& operator=(prefetch_resource_adaptor_impl&&) = delete;

bool operator==(prefetch_resource_adaptor_impl const& other) const noexcept
{
return this == std::addressof(other);
}

bool operator!=(prefetch_resource_adaptor_impl const& other) const noexcept
{
return !(*this == other);
}

[[nodiscard]] device_async_resource_ref get_upstream_resource() const noexcept;

void* allocate(cuda::stream_ref stream,
std::size_t bytes,
std::size_t alignment = alignof(std::max_align_t));

void deallocate(cuda::stream_ref stream,
void* ptr,
std::size_t bytes,
std::size_t alignment = alignof(std::max_align_t)) noexcept;

void* allocate_sync(std::size_t bytes, std::size_t alignment = alignof(std::max_align_t));

void deallocate_sync(void* ptr,
std::size_t bytes,
std::size_t alignment = alignof(std::max_align_t)) noexcept;

RMM_CONSTEXPR_FRIEND void get_property(prefetch_resource_adaptor_impl const&,
cuda::mr::device_accessible) noexcept
{
}

private:
cuda::mr::any_resource<cuda::mr::device_accessible> upstream_mr_;
};

} // namespace detail
} // namespace mr
} // namespace RMM_NAMESPACE
120 changes: 50 additions & 70 deletions cpp/include/rmm/mr/prefetch_resource_adaptor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@
*/
#pragma once

#include <rmm/cuda_stream_view.hpp>
#include <rmm/detail/export.hpp>
#include <rmm/mr/detail/prefetch_resource_adaptor_impl.hpp>
#include <rmm/mr/device_memory_resource.hpp>
#include <rmm/prefetch.hpp>
#include <rmm/resource_ref.hpp>

#include <cuda/memory_resource>

#include <cstddef>
#include <memory>

namespace RMM_NAMESPACE {
namespace mr {
Expand All @@ -22,102 +24,80 @@ namespace mr {
/**
* @brief Resource that prefetches all memory allocations.
*
* @tparam Upstream Type of the upstream resource used for
* allocation/deallocation.
* This class is copyable and shares ownership of its internal state via
* `cuda::mr::shared_resource`.
*/
template <typename Upstream>
class prefetch_resource_adaptor final : public device_memory_resource {
class RMM_EXPORT prefetch_resource_adaptor
: public device_memory_resource,
private cuda::mr::shared_resource<detail::prefetch_resource_adaptor_impl> {
using shared_base = cuda::mr::shared_resource<detail::prefetch_resource_adaptor_impl>;

public:
/**
* @brief Construct a new prefetch resource adaptor using `upstream` to satisfy
* allocation requests.
*
* @param upstream The resource_ref used for allocating/deallocating device memory
*/
prefetch_resource_adaptor(device_async_resource_ref upstream) : upstream_{upstream} {}
// Begin legacy device_memory_resource compatibility layer
using device_memory_resource::allocate;
using device_memory_resource::allocate_sync;
using device_memory_resource::deallocate;
using device_memory_resource::deallocate_sync;

/**
* @brief Construct a new prefetch resource adaptor using `upstream` to satisfy
* allocation requests.
* @brief Compare two adaptors for equality (shared-impl identity).
*
* @throws rmm::logic_error if `upstream == nullptr`
*
* @param upstream The resource used for allocating/deallocating device memory
* @param other The other prefetch_resource_adaptor to compare against.
* @return true if both adaptors share the same underlying state.
*/
prefetch_resource_adaptor(Upstream* upstream)
: upstream_{to_device_async_resource_ref_checked(upstream)}
[[nodiscard]] bool operator==(prefetch_resource_adaptor const& other) const noexcept
{
return static_cast<shared_base const&>(*this) == static_cast<shared_base const&>(other);
}

prefetch_resource_adaptor() = delete;
~prefetch_resource_adaptor() override = default;
prefetch_resource_adaptor(prefetch_resource_adaptor const&) = delete;
prefetch_resource_adaptor& operator=(prefetch_resource_adaptor const&) = delete;
prefetch_resource_adaptor(prefetch_resource_adaptor&&) noexcept =
default; ///< @default_move_constructor
prefetch_resource_adaptor& operator=(prefetch_resource_adaptor&&) noexcept =
default; ///< @default_move_assignment{prefetch_resource_adaptor}

/**
* @briefreturn{rmm::device_async_resource_ref to the upstream resource}
* @brief Compare two adaptors for inequality.
*
* @param other The other prefetch_resource_adaptor to compare against.
* @return true if the adaptors do not share the same underlying state.
*/
[[nodiscard]] rmm::device_async_resource_ref get_upstream_resource() const noexcept
[[nodiscard]] bool operator!=(prefetch_resource_adaptor const& other) const noexcept
{
return upstream_;
return !(*this == other);
}
// End legacy device_memory_resource compatibility layer

private:
/**
* @brief Allocates memory of size at least `bytes` using the upstream
* resource as long as it fits inside the allocation limit.
*
* @note The allocation is always prefetched to the current device.
*
* @throws rmm::bad_alloc if the requested allocation could not be fulfilled
* by the upstream resource.
*
* @param bytes The size, in bytes, of the allocation
* @param stream Stream on which to perform the allocation
* @return void* Pointer to the newly allocated memory
* @brief Enables the `cuda::mr::device_accessible` property
*/
void* do_allocate(std::size_t bytes, cuda_stream_view stream) override
RMM_CONSTEXPR_FRIEND void get_property(prefetch_resource_adaptor const&,
cuda::mr::device_accessible) noexcept
{
void* ptr = get_upstream_resource().allocate(stream, bytes);
rmm::prefetch(ptr, bytes, rmm::get_current_cuda_device(), stream);
return ptr;
}

/**
* @brief Free allocation of size `bytes` pointed to by `ptr`
* @brief Construct a new prefetch resource adaptor using `upstream` to satisfy
* allocation requests.
*
* @param ptr Pointer to be deallocated
* @param bytes Size of the allocation
* @param stream Stream on which to perform the deallocation
* @param upstream The resource_ref used for allocating/deallocating device memory
*/
void do_deallocate(void* ptr, std::size_t bytes, cuda_stream_view stream) noexcept override
{
get_upstream_resource().deallocate(stream, ptr, bytes);
}
explicit prefetch_resource_adaptor(device_async_resource_ref upstream);

~prefetch_resource_adaptor() = default;

/**
* @brief Compare the upstream resource to another.
*
* @param other The other resource to compare to
* @return true If the two resources are equivalent
* @return false If the two resources are not equal
* @briefreturn{rmm::device_async_resource_ref to the upstream resource}
*/
bool do_is_equal(device_memory_resource const& other) const noexcept override
{
if (this == std::addressof(other)) { return true; }
auto cast = dynamic_cast<prefetch_resource_adaptor<Upstream> const*>(&other);
if (cast == nullptr) { return false; }
return get_upstream_resource() == cast->get_upstream_resource();
}
[[nodiscard]] device_async_resource_ref get_upstream_resource() const noexcept;

// the upstream resource used for satisfying allocation requests
device_async_resource_ref upstream_;
// Begin legacy device_memory_resource compatibility layer
private:
void* do_allocate(std::size_t bytes, cuda_stream_view stream) override;

void do_deallocate(void* ptr, std::size_t bytes, cuda_stream_view stream) noexcept override;

[[nodiscard]] bool do_is_equal(device_memory_resource const& other) const noexcept override;
// End legacy device_memory_resource compatibility layer
};

static_assert(cuda::mr::resource_with<prefetch_resource_adaptor, cuda::mr::device_accessible>,
"prefetch_resource_adaptor does not satisfy the cuda::mr::resource concept");

/** @} */ // end of group
} // namespace mr
} // namespace RMM_NAMESPACE
56 changes: 56 additions & 0 deletions cpp/src/mr/detail/prefetch_resource_adaptor_impl.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* SPDX-FileCopyrightText: Copyright (c) 2024-2026, NVIDIA CORPORATION.
* SPDX-License-Identifier: Apache-2.0
*/

#include <rmm/cuda_device.hpp>
#include <rmm/mr/detail/prefetch_resource_adaptor_impl.hpp>
#include <rmm/prefetch.hpp>

namespace RMM_NAMESPACE {
namespace mr {
namespace detail {

prefetch_resource_adaptor_impl::prefetch_resource_adaptor_impl(device_async_resource_ref upstream)
: upstream_mr_{upstream}
{
}

device_async_resource_ref prefetch_resource_adaptor_impl::get_upstream_resource() const noexcept
{
return device_async_resource_ref{
const_cast<cuda::mr::any_resource<cuda::mr::device_accessible>&>(upstream_mr_)};
}

void* prefetch_resource_adaptor_impl::allocate(cuda::stream_ref stream,
std::size_t bytes,
std::size_t /*alignment*/)
{
void* ptr = upstream_mr_.allocate(stream, bytes);
rmm::prefetch(ptr, bytes, rmm::get_current_cuda_device(), cuda_stream_view{stream.get()});
return ptr;
}

void prefetch_resource_adaptor_impl::deallocate(cuda::stream_ref stream,
void* ptr,
std::size_t bytes,
std::size_t /*alignment*/) noexcept
{
upstream_mr_.deallocate(stream, ptr, bytes);
}

void* prefetch_resource_adaptor_impl::allocate_sync(std::size_t bytes, std::size_t alignment)
{
return allocate(cuda_stream_view{}, bytes, alignment);
}

void prefetch_resource_adaptor_impl::deallocate_sync(void* ptr,
std::size_t bytes,
std::size_t alignment) noexcept
{
deallocate(cuda_stream_view{}, ptr, bytes, alignment);
}

} // namespace detail
} // namespace mr
} // namespace RMM_NAMESPACE
45 changes: 45 additions & 0 deletions cpp/src/mr/prefetch_resource_adaptor.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* SPDX-FileCopyrightText: Copyright (c) 2024-2026, NVIDIA CORPORATION.
* SPDX-License-Identifier: Apache-2.0
*/

#include <rmm/aligned.hpp>
#include <rmm/mr/prefetch_resource_adaptor.hpp>

namespace RMM_NAMESPACE {
namespace mr {

prefetch_resource_adaptor::prefetch_resource_adaptor(device_async_resource_ref upstream)
: shared_base(cuda::mr::make_shared_resource<detail::prefetch_resource_adaptor_impl>(upstream))
{
}

device_async_resource_ref prefetch_resource_adaptor::get_upstream_resource() const noexcept
{
return get().get_upstream_resource();
}

// Begin legacy device_memory_resource compatibility layer
void* prefetch_resource_adaptor::do_allocate(std::size_t bytes, cuda_stream_view stream)
{
return shared_base::allocate(stream, bytes, rmm::CUDA_ALLOCATION_ALIGNMENT);
}

void prefetch_resource_adaptor::do_deallocate(void* ptr,
std::size_t bytes,
cuda_stream_view stream) noexcept
{
shared_base::deallocate(stream, ptr, bytes, rmm::CUDA_ALLOCATION_ALIGNMENT);
}

bool prefetch_resource_adaptor::do_is_equal(device_memory_resource const& other) const noexcept
{
if (this == std::addressof(other)) { return true; }
auto const* cast = dynamic_cast<prefetch_resource_adaptor const*>(&other);
if (cast == nullptr) { return false; }
return static_cast<shared_base const&>(*this) == static_cast<shared_base const&>(*cast);
}
// End legacy device_memory_resource compatibility layer

} // namespace mr
} // namespace RMM_NAMESPACE
1 change: 1 addition & 0 deletions cpp/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ ConfigureTest(FAILURE_CALLBACK_TEST mr/failure_callback_mr_tests.cpp)

# prefetch adaptor tests
ConfigureTest(PREFETCH_ADAPTOR_TEST mr/prefetch_resource_adaptor_tests.cpp)
ConfigureTest(PREFETCH_MR_REF_TEST mr/mr_ref_prefetch_tests.cpp GPUS 1 PERCENT 5)

# aligned adaptor tests
ConfigureTest(ALIGNED_TEST mr/aligned_mr_tests.cpp)
Expand Down
Loading
Loading