Skip to content
Open
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
101 changes: 101 additions & 0 deletions cpp/include/rmm/mr/device/cuda_host_memory_resource.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/*
* Copyright (c) 2025, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once

#include <rmm/cuda_stream_view.hpp>
#include <rmm/detail/error.hpp>
#include <rmm/detail/export.hpp>
#include <rmm/mr/device/device_memory_resource.hpp>

#include <cstddef>

namespace RMM_NAMESPACE {
namespace mr {
/**
* @addtogroup device_memory_resources
* @{
* @file
*/
/**
* @brief `device_memory_resource` derived class that uses cudaMallocHost/cudaFreeHost for
* allocation/deallocation of pinned host memory.
*/
class cuda_host_memory_resource final : public device_memory_resource {
public:
cuda_host_memory_resource() = default;
~cuda_host_memory_resource() override = default;
cuda_host_memory_resource(cuda_host_memory_resource const&) =
default; ///< @default_copy_constructor
cuda_host_memory_resource(cuda_host_memory_resource&&) = default; ///< @default_move_constructor
cuda_host_memory_resource& operator=(cuda_host_memory_resource const&) =
default; ///< @default_copy_assignment{cuda_host_memory_resource}
cuda_host_memory_resource& operator=(cuda_host_memory_resource&&) =
default; ///< @default_move_assignment{cuda_host_memory_resource}

private:
/**
* @brief Allocates pinned host memory of size at least \p bytes.
*
* The returned pointer will have at minimum 256 byte alignment.
*
* The stream argument is ignored.
*
* @param bytes The size of the allocation
* @param stream This argument is ignored
* @return void* Pointer to the newly allocated memory
*/
void* do_allocate(std::size_t bytes, [[maybe_unused]] cuda_stream_view stream) override
{
void* ptr{nullptr};
RMM_CUDA_TRY_ALLOC(cudaMallocHost(&ptr, bytes), bytes);
return ptr;
}

/**
* @brief Deallocate pinned host memory pointed to by \p ptr.
*
* The stream argument is ignored.
*
* @param ptr Pointer to be deallocated
* @param bytes The size in bytes of the allocation. This must be equal to the
* value of `bytes` that was passed to the `allocate` call that returned `ptr`.
* @param stream This argument is ignored.
*/
void do_deallocate(void* ptr,
[[maybe_unused]] std::size_t bytes,
[[maybe_unused]] cuda_stream_view stream) override
{
RMM_ASSERT_CUDA_SUCCESS(cudaFreeHost(ptr));
}

/**
* @brief Compare this resource to another.
*
* Two cuda_host_memory_resources always compare equal, because they can each
* deallocate memory allocated by the other.
*
* @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
*/
[[nodiscard]] bool do_is_equal(device_memory_resource const& other) const noexcept override
{
return dynamic_cast<cuda_host_memory_resource const*>(&other) != nullptr;
}
};
/** @} */ // end of group
} // namespace mr
} // namespace RMM_NAMESPACE
3 changes: 3 additions & 0 deletions cpp/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,9 @@ ConfigureTest(PINNED_POOL_MR_TEST mr/host/pinned_pool_mr_tests.cpp)
# cuda stream tests
ConfigureTest(CUDA_STREAM_TEST cuda_stream_tests.cpp cuda_stream_pool_tests.cpp)

# cuda host memory resource tests
ConfigureTest(CUDA_HOST_MR_TEST mr/device/cuda_host_memory_resource_tests.cu GPUS 1 PERCENT 100)

# device buffer tests
ConfigureTest(DEVICE_BUFFER_TEST device_buffer_tests.cu)

Expand Down
Loading