-
Notifications
You must be signed in to change notification settings - Fork 197
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a host-pinned memory resource that can be used as upstream for `p…
…ool_memory_resource`. (#1392) Depends on #1417 Adds a new `host_pinned_memory_resource` that implements the new `cuda::mr::memory_resource` and `cuda::mr::async_memory_resource` concepts which makes it usable as an upstream MR for `rmm::mr::device_memory_resource`. Also tests a pool made with this new MR as the upstream. Note that the tests explicitly set the initial and maximum pool sizes as using the defaults does not currently work. See #1388 . Closes #618 Authors: - Mark Harris (https://github.com/harrism) - Lawrence Mitchell (https://github.com/wence-) Approvers: - Michael Schellenberger Costa (https://github.com/miscco) - Alessandro Bellina (https://github.com/abellina) - Lawrence Mitchell (https://github.com/wence-) - Jake Hemstad (https://github.com/jrhemstad) - Bradley Dice (https://github.com/bdice) URL: #1392
- Loading branch information
Showing
9 changed files
with
336 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,222 @@ | ||
/* | ||
* Copyright (c) 2024, 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/aligned.hpp> | ||
#include <rmm/detail/aligned.hpp> | ||
#include <rmm/detail/error.hpp> | ||
|
||
#include <cuda/memory_resource> | ||
#include <cuda/stream_ref> | ||
|
||
#include <cuda_runtime_api.h> | ||
|
||
#include <cstddef> | ||
#include <utility> | ||
|
||
namespace rmm::mr { | ||
|
||
/** | ||
* @brief Memory resource class for allocating pinned host memory. | ||
* | ||
* This class uses CUDA's `cudaHostAlloc` to allocate pinned host memory. It implements the | ||
* `cuda::mr::memory_resource` and `cuda::mr::device_memory_resource` concepts, and | ||
* the `cuda::mr::host_accessible` and `cuda::mr::device_accessible` properties. | ||
*/ | ||
class pinned_host_memory_resource { | ||
public: | ||
// Disable clang-tidy complaining about the easily swappable size and alignment parameters | ||
// of allocate and deallocate | ||
// NOLINTBEGIN(bugprone-easily-swappable-parameters) | ||
|
||
/** | ||
* @brief Allocates pinned host memory of size at least \p bytes bytes. | ||
* | ||
* @throws `rmm::out_of_memory` if the requested allocation could not be fulfilled due to to a | ||
* CUDA out of memory error. | ||
* @throws `rmm::bad_alloc` if the requested allocation could not be fulfilled due to any other | ||
* reason. | ||
* | ||
* @param bytes The size, in bytes, of the allocation. | ||
* @param alignment Alignment in bytes. Default alignment is used if unspecified. | ||
* | ||
* @return Pointer to the newly allocated memory. | ||
*/ | ||
static void* allocate(std::size_t bytes, | ||
[[maybe_unused]] std::size_t alignment = rmm::RMM_DEFAULT_HOST_ALIGNMENT) | ||
{ | ||
// don't allocate anything if the user requested zero bytes | ||
if (0 == bytes) { return nullptr; } | ||
|
||
return rmm::detail::aligned_host_allocate(bytes, alignment, [](std::size_t size) { | ||
void* ptr{nullptr}; | ||
RMM_CUDA_TRY_ALLOC(cudaHostAlloc(&ptr, size, cudaHostAllocDefault)); | ||
return ptr; | ||
}); | ||
} | ||
|
||
/** | ||
* @brief Deallocate memory pointed to by \p ptr of size \p bytes bytes. | ||
* | ||
* @throws Nothing. | ||
* | ||
* @param ptr Pointer to be deallocated. | ||
* @param bytes Size of the allocation. | ||
* @param alignment Alignment in bytes. Default alignment is used if unspecified. | ||
*/ | ||
static void deallocate(void* ptr, | ||
std::size_t bytes, | ||
std::size_t alignment = rmm::RMM_DEFAULT_HOST_ALIGNMENT) noexcept | ||
{ | ||
rmm::detail::aligned_host_deallocate( | ||
ptr, bytes, alignment, [](void* ptr) { RMM_ASSERT_CUDA_SUCCESS(cudaFreeHost(ptr)); }); | ||
} | ||
|
||
/** | ||
* @brief Allocates pinned host memory of size at least \p bytes bytes. | ||
* | ||
* @note Stream argument is ignored and behavior is identical to allocate. | ||
* | ||
* @throws `rmm::out_of_memory` if the requested allocation could not be fulfilled due to to a | ||
* CUDA out of memory error. | ||
* @throws `rmm::bad_alloc` if the requested allocation could not be fulfilled due to any other | ||
* error. | ||
* | ||
* @param bytes The size, in bytes, of the allocation. | ||
* @param stream CUDA stream on which to perform the allocation (ignored). | ||
* @return Pointer to the newly allocated memory. | ||
*/ | ||
static void* allocate_async(std::size_t bytes, [[maybe_unused]] cuda::stream_ref stream) | ||
{ | ||
return allocate(bytes); | ||
} | ||
|
||
/** | ||
* @brief Allocates pinned host memory of size at least \p bytes bytes and alignment \p alignment. | ||
* | ||
* @note Stream argument is ignored and behavior is identical to allocate. | ||
* | ||
* @throws `rmm::out_of_memory` if the requested allocation could not be fulfilled due to to a | ||
* CUDA out of memory error. | ||
* @throws `rmm::bad_alloc` if the requested allocation could not be fulfilled due to any other | ||
* error. | ||
* | ||
* @param bytes The size, in bytes, of the allocation. | ||
* @param alignment Alignment in bytes. | ||
* @param stream CUDA stream on which to perform the allocation (ignored). | ||
* @return Pointer to the newly allocated memory. | ||
*/ | ||
static void* allocate_async(std::size_t bytes, | ||
std::size_t alignment, | ||
[[maybe_unused]] cuda::stream_ref stream) | ||
{ | ||
return allocate(bytes, alignment); | ||
} | ||
|
||
/** | ||
* @brief Deallocate memory pointed to by \p ptr of size \p bytes bytes. | ||
* | ||
* @note Stream argument is ignored and behavior is identical to deallocate. | ||
* | ||
* @throws Nothing. | ||
* | ||
* @param ptr Pointer to be deallocated. | ||
* @param bytes Size of the allocation. | ||
* @param stream CUDA stream on which to perform the deallocation (ignored). | ||
*/ | ||
static void deallocate_async(void* ptr, | ||
std::size_t bytes, | ||
[[maybe_unused]] cuda::stream_ref stream) noexcept | ||
{ | ||
return deallocate(ptr, bytes); | ||
} | ||
|
||
/** | ||
* @brief Deallocate memory pointed to by \p ptr of size \p bytes bytes and alignment \p | ||
* alignment bytes. | ||
* | ||
* @note Stream argument is ignored and behavior is identical to deallocate. | ||
* | ||
* @throws Nothing. | ||
* | ||
* @param ptr Pointer to be deallocated. | ||
* @param bytes Size of the allocation. | ||
* @param alignment Alignment in bytes. | ||
* @param stream CUDA stream on which to perform the deallocation (ignored). | ||
*/ | ||
static void deallocate_async(void* ptr, | ||
std::size_t bytes, | ||
std::size_t alignment, | ||
[[maybe_unused]] cuda::stream_ref stream) noexcept | ||
{ | ||
return deallocate(ptr, bytes, alignment); | ||
} | ||
// NOLINTEND(bugprone-easily-swappable-parameters) | ||
|
||
/** | ||
* @briefreturn{true if the specified resource is the same type as this resource.} | ||
*/ | ||
bool operator==(const pinned_host_memory_resource&) const { return true; } | ||
|
||
/** | ||
* @briefreturn{true if the specified resource is not the same type as this resource, otherwise | ||
* false.} | ||
*/ | ||
bool operator!=(const pinned_host_memory_resource&) const { return false; } | ||
|
||
/** | ||
* @brief Query whether the resource supports reporting free and available memory. | ||
* | ||
* @return false | ||
*/ | ||
static bool supports_get_mem_info() { return false; } | ||
|
||
/** | ||
* @brief Query the total amount of memory and free memory available for allocation by this | ||
* resource. | ||
* | ||
* @throws nothing | ||
* | ||
* @return std::pair containing 0 for both total and free memory. | ||
*/ | ||
[[nodiscard]] static std::pair<std::size_t, std::size_t> get_mem_info(cuda::stream_ref) noexcept | ||
{ | ||
return {0, 0}; | ||
} | ||
|
||
/** | ||
* @brief Enables the `cuda::mr::device_accessible` property | ||
* | ||
* This property declares that a `pinned_host_memory_resource` provides device accessible memory | ||
*/ | ||
friend void get_property(pinned_host_memory_resource const&, cuda::mr::device_accessible) noexcept | ||
{ | ||
} | ||
|
||
/** | ||
* @brief Enables the `cuda::mr::host_accessible` property | ||
* | ||
* This property declares that a `pinned_host_memory_resource` provides host accessible memory | ||
*/ | ||
friend void get_property(pinned_host_memory_resource const&, cuda::mr::host_accessible) noexcept | ||
{ | ||
} | ||
}; | ||
|
||
static_assert(cuda::mr::async_resource_with<pinned_host_memory_resource, | ||
cuda::mr::device_accessible, | ||
cuda::mr::host_accessible>); | ||
} // namespace rmm::mr |
Oops, something went wrong.