-
Notifications
You must be signed in to change notification settings - Fork 200
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
Add a host-pinned memory resource that can be used as upstream for pool_memory_resource
.
#1392
Changes from 40 commits
fae33fa
15be572
e8c227b
2b37372
c43a8c1
d238daa
3d65d4c
66d85b4
265de9b
0be364b
266afa9
5d66f40
fae5b73
92c0653
2acf759
a70b24e
b6edcd1
782ff55
4ef844a
ce58ff5
0b4c968
2f827a5
4f91478
f581809
bafd70a
a77d215
07dffa3
8afff2d
0140bd4
91752c8
baf429c
c90e81c
c2843be
6e0aeaa
c3c61e1
014ac5b
909b733
0fc3fba
9a876b5
b819738
27fe52c
da934ba
7d51fea
85286b0
f7b0ca5
52fc2f1
6162699
fa140ae
aafa18a
92c8e23
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,225 @@ | ||
/* | ||
* Copyright (c) 2023, 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/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. | ||
* | ||
* @todo Alignment is not implemented yet. | ||
* | ||
* @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::detail::RMM_DEFAULT_HOST_ALIGNMENT) | ||
{ | ||
void* ptr{nullptr}; | ||
RMM_CUDA_TRY_ALLOC(cudaHostAlloc(&ptr, bytes, cudaHostAllocDefault)); | ||
return ptr; | ||
} | ||
|
||
/** | ||
* @brief Deallocate memory pointed to by \p ptr of size \p bytes bytes. | ||
* | ||
* @todo Alignment is not implemented yet. | ||
* | ||
* @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, | ||
[[maybe_unused]] std::size_t bytes, | ||
[[maybe_unused]] std::size_t alignment = rmm::detail::RMM_DEFAULT_HOST_ALIGNMENT) noexcept | ||
{ | ||
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. | ||
* | ||
* @todo Alignment is not implemented yet. | ||
* | ||
* @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. | ||
* | ||
* @todo Alignment is not implemented yet. | ||
* | ||
* @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. | ||
* | ||
* @todo Alignment is not implemented yet. | ||
* | ||
* @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, otherwise | ||
* false.} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This docstring implies it's possible to compare with another type of resource and get There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh yeah, I had that thought. Is there a blanket "false" implementation in the base class somehow? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this is how comparison works with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note also that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (also note there is no base class) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I changed the docstring so it doesn't say that false can be returned. Note that we should probably followup with more explicit tests of this MR and future MRs like it. Right now, though, our test machinery for MRs assumes they are all There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Okay. If there's no base class, I've just lost track of how the class hierarchy works. I don't have any further comments here but I'll need to refresh myself on how things are supposed to work someday. |
||
*/ | ||
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 | ||
{ | ||
} | ||
}; | ||
|
||
harrism marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} // namespace rmm::mr |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Double-nit (no need to act on it)
non-negative integer exponent
(all integers can be expressed as powers of two if we admit real exponents).