-
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
Fallback resource adaptor #1665
Draft
madsbk
wants to merge
18
commits into
rapidsai:branch-24.10
Choose a base branch
from
madsbk:failure_alternate_resource_adaptor
base: branch-24.10
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 12 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
397265f
failure_callback_resource_adaptor
madsbk 6d76ec6
alternate_upstream_mr
madsbk bcaef4c
AlternateUpstream
madsbk e42fc60
cleanup
madsbk cbd7f43
use device_async_resource_ref
madsbk 451900d
doc
madsbk bbec291
doc
madsbk 0324f51
doc
madsbk 263a76a
style
madsbk db596fa
doc
madsbk 72ada89
c++ tests
madsbk ff3be66
typo
madsbk 3caf138
Merge branch 'branch-24.10' of github.com:rapidsai/rmm into failure_a…
madsbk 5345aed
rename to fallback_resource_adapater
madsbk f31d588
style
madsbk d20dc82
rename test file
madsbk 865d010
adaptor
madsbk 38aaf5c
Merge branch 'branch-24.10' of github.com:rapidsai/rmm into failure_a…
madsbk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
159 changes: 159 additions & 0 deletions
159
include/rmm/mr/device/failure_alternate_resource_adaptor.hpp
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,159 @@ | ||
/* | ||
* 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/detail/error.hpp> | ||
#include <rmm/detail/export.hpp> | ||
#include <rmm/mr/device/device_memory_resource.hpp> | ||
#include <rmm/resource_ref.hpp> | ||
|
||
#include <cstddef> | ||
#include <mutex> | ||
#include <unordered_set> | ||
|
||
namespace RMM_NAMESPACE { | ||
namespace mr { | ||
/** | ||
* @addtogroup device_resource_adaptors | ||
* @{ | ||
* @file | ||
*/ | ||
|
||
/** | ||
* @brief A device memory resource that uses an alternate upstream resource when the primary | ||
* upstream resource throws a specified exception type. | ||
* | ||
* An instance of this resource must be constructed with two upstream resources to satisfy | ||
* allocation requests. | ||
* | ||
* @tparam ExceptionType The type of exception that this adaptor should respond to. | ||
*/ | ||
template <typename ExceptionType = rmm::out_of_memory> | ||
class failure_alternate_resource_adaptor final : public device_memory_resource { | ||
public: | ||
using exception_type = ExceptionType; ///< The type of exception this object catches/throws | ||
|
||
/** | ||
* @brief Construct a new `failure_alternate_resource_adaptor` that uses `primary_upstream` | ||
* to satisfy allocation requests and if that fails with `ExceptionType`, uses | ||
* `alternate_upstream`. | ||
* | ||
* @param primary_upstream The primary resource used for allocating/deallocating device memory | ||
* @param alternate_upstream The alternate resource used for allocating/deallocating device memory | ||
* memory | ||
*/ | ||
failure_alternate_resource_adaptor(device_async_resource_ref primary_upstream, | ||
device_async_resource_ref alternate_upstream) | ||
: primary_upstream_{primary_upstream}, alternate_upstream_{alternate_upstream} | ||
{ | ||
} | ||
|
||
failure_alternate_resource_adaptor() = delete; | ||
~failure_alternate_resource_adaptor() override = default; | ||
failure_alternate_resource_adaptor(failure_alternate_resource_adaptor const&) = delete; | ||
failure_alternate_resource_adaptor& operator=(failure_alternate_resource_adaptor const&) = delete; | ||
failure_alternate_resource_adaptor(failure_alternate_resource_adaptor&&) noexcept = | ||
default; ///< @default_move_constructor | ||
failure_alternate_resource_adaptor& operator=(failure_alternate_resource_adaptor&&) noexcept = | ||
default; ///< @default_move_assignment{failure_alternate_resource_adaptor} | ||
|
||
/** | ||
* @briefreturn{rmm::device_async_resource_ref to the upstream resource} | ||
*/ | ||
[[nodiscard]] rmm::device_async_resource_ref get_upstream_resource() const noexcept | ||
{ | ||
return primary_upstream_; | ||
} | ||
|
||
/** | ||
* @briefreturn{rmm::device_async_resource_ref to the alternate upstream resource} | ||
*/ | ||
[[nodiscard]] rmm::device_async_resource_ref get_alternate_upstream_resource() const noexcept | ||
{ | ||
return alternate_upstream_; | ||
} | ||
|
||
private: | ||
/** | ||
* @brief Allocates memory of size at least `bytes` using the upstream | ||
* resource. | ||
* | ||
* @throws any exceptions thrown from the upstream resources, only `exception_type` | ||
* thrown by the primary upstream is caught. | ||
* | ||
* @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 | ||
*/ | ||
void* do_allocate(std::size_t bytes, cuda_stream_view stream) override | ||
{ | ||
void* ret{}; | ||
try { | ||
ret = primary_upstream_.allocate_async(bytes, stream); | ||
} catch (exception_type const& e) { | ||
ret = alternate_upstream_.allocate_async(bytes, stream); | ||
std::lock_guard<std::mutex> lock(mtx_); | ||
alternate_allocations_.insert(ret); | ||
} | ||
return ret; | ||
} | ||
|
||
/** | ||
* @brief Free allocation of size `bytes` pointed to by `ptr` | ||
* | ||
* @param ptr Pointer to be deallocated | ||
* @param bytes Size of the allocation | ||
* @param stream Stream on which to perform the deallocation | ||
*/ | ||
void do_deallocate(void* ptr, std::size_t bytes, cuda_stream_view stream) override | ||
{ | ||
std::size_t count{0}; | ||
{ | ||
std::lock_guard<std::mutex> lock(mtx_); | ||
count = alternate_allocations_.erase(ptr); | ||
} | ||
if (count > 0) { | ||
alternate_upstream_.deallocate_async(ptr, bytes, stream); | ||
} else { | ||
primary_upstream_.deallocate_async(ptr, bytes, stream); | ||
} | ||
} | ||
|
||
/** | ||
* @brief Compare the 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 | ||
*/ | ||
[[nodiscard]] bool do_is_equal(device_memory_resource const& other) const noexcept override | ||
madsbk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
if (this == &other) { return true; } | ||
auto cast = dynamic_cast<failure_alternate_resource_adaptor const*>(&other); | ||
if (cast == nullptr) { return false; } | ||
return get_upstream_resource() == cast->get_upstream_resource() && | ||
get_alternate_upstream_resource() == cast->get_alternate_upstream_resource(); | ||
} | ||
|
||
device_async_resource_ref primary_upstream_; | ||
device_async_resource_ref alternate_upstream_; | ||
std::unordered_set<void*> alternate_allocations_; | ||
mutable std::mutex mtx_; | ||
}; | ||
|
||
/** @} */ // end of group | ||
} // namespace mr | ||
} // namespace RMM_NAMESPACE |
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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
nit:
fallback_resource_adapater
feels like a more concise name.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.
+1
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.
+1, will rename.
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.
done