-
Notifications
You must be signed in to change notification settings - Fork 49
Add ucxx::MemoryHandle and ucxx::RemoteKey C++ classes
#190
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
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
d11f779
Add `ucxx::MemoryHandle` and `ucxx::RemoteKey` C++ classes
pentschev a83235c
Add tests for `ucxx::MemoryHandle` and `ucxx::RemoteKey`
pentschev 6294d19
Clarify lifetime of `buffer` in `ucxx::MemoryHandle` constructor
pentschev b5ad49a
Clarify `ucxx::RemoteKey::getHandle` docs
pentschev 74a2bc2
Fix `deserialize` docstring
pentschev a700b11
Improve `_packedRemoteKey` description
pentschev 13233f0
Clarify `ucxx::MemoryHandle` constructor docstring
pentschev 908daf2
Merge remote-tracking branch 'upstream/branch-0.37' into ucp-mem
pentschev af48318
Fix docstring typo
pentschev 7980d1b
Merge remote-tracking branch 'upstream/branch-0.37' into ucp-mem
pentschev 9d86100
Merge remote-tracking branch 'origin/ucp-mem' into ucp-mem
pentschev 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
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,162 @@ | ||
| /** | ||
| * SPDX-FileCopyrightText: Copyright (c) 2024, NVIDIA CORPORATION & AFFILIATES. | ||
| * SPDX-License-Identifier: BSD-3-Clause | ||
| */ | ||
| #pragma once | ||
| #include <memory> | ||
| #include <string> | ||
| #include <utility> | ||
|
|
||
| #include <ucp/api/ucp.h> | ||
|
|
||
| #include <ucxx/component.h> | ||
| #include <ucxx/context.h> | ||
|
|
||
| namespace ucxx { | ||
|
|
||
| class RemoteKey; | ||
|
|
||
| /** | ||
| * @brief Component holding a UCP memory handle. | ||
| * | ||
| * The UCP layer provides RMA (Remote Memory Access) to memory handles that it controls | ||
| * in form of `ucp_mem_h` object, this class encapsulates that object and provides | ||
| * methods to simplify its handling. | ||
| */ | ||
| class MemoryHandle : public Component { | ||
| private: | ||
| ucp_mem_h _handle{}; ///< The UCP handle to the memory allocation. | ||
| size_t _size{0}; ///< The actual allocation size. | ||
| uint64_t _baseAddress{0}; ///< The allocation's base address. | ||
|
|
||
| /** | ||
| * @brief Private constructor of `ucxx::MemoryHandle`. | ||
| * | ||
| * This is the internal implementation of `ucxx::MemoryHandle` constructor, made private | ||
| * not to be called directly. This constructor is made private to ensure all UCXX objects | ||
| * are shared pointers and the correct lifetime management of each one. | ||
| * | ||
| * Instead the user should use one of the following: | ||
| * | ||
| * - `ucxx::Context::createMemoryHandle` | ||
| * - `ucxx::createMemoryHandle()` | ||
| * | ||
| * @throws ucxx::Error if either `ucp_mem_map` or `ucp_mem_query` fail. | ||
| * | ||
| * @param[in] context parent context where to map memory. | ||
| * @param[in] size the minimum size of the memory allocation | ||
| * @param[in] buffer the pointer to an existing allocation or `nullptr` to allocate a | ||
| * new memory region. | ||
| */ | ||
| MemoryHandle(std::shared_ptr<Context> context, const size_t size, void* buffer); | ||
|
|
||
| public: | ||
| MemoryHandle() = delete; | ||
| MemoryHandle(const MemoryHandle&) = delete; | ||
| MemoryHandle& operator=(MemoryHandle const&) = delete; | ||
| MemoryHandle(MemoryHandle&& o) = delete; | ||
| MemoryHandle& operator=(MemoryHandle&& o) = delete; | ||
|
|
||
| /** | ||
| * @brief Constructor for `shared_ptr<ucxx::MemoryHandle>`. | ||
| * | ||
| * The constructor for a `shared_ptr<ucxx::MemoryHandle>` object, mapping a memory buffer | ||
| * with UCP to provide RMA (Remote Memory Access) to. | ||
| * | ||
| * The allocation requires a `size` and a `buffer`. The `buffer` provided may be either | ||
| * a `nullptr`, in which case UCP will allocate a new memory region for it, or an already | ||
| * existing allocation, in which case UCP will only map it for RMA and it's the caller's | ||
| * responsibility to keep `buffer` alive until this object is destroyed. When the UCP | ||
| * allocates `buffer` (i.e., when the value passed is `nullptr`), the actual size of the | ||
| * allocation may be larger than requested, and can later be found calling the `getSize()` | ||
| * method, if a preallocated buffer is passed `getSize()` will return the same value | ||
| * specified for `size`. | ||
| * | ||
| * @code{.cpp} | ||
| * // `context` is `std::shared_ptr<ucxx::Context>` | ||
| * // Allocate a 128-byte buffer with UCP. | ||
| * auto memoryHandle = context->createMemoryHandle(128, nullptr); | ||
| * | ||
| * // Equivalent to line above | ||
| * // auto memoryHandle = ucxx::createMemoryHandle(context, 128, nullptr); | ||
| * | ||
| * // Map an existing 128-byte buffer with UCP. | ||
| * size_t allocationSize = 128; | ||
| * auto buffer = new uint8_t[allocationSize]; | ||
| * auto memoryHandleFromBuffer = context->createMemoryHandle( | ||
| * allocationSize * sizeof(*buffer), reinterpret_cast<void*>(buffer) | ||
| * ); | ||
| * | ||
| * // Equivalent to line above | ||
| * // auto memoryHandleFromBuffer = ucxx::createMemoryHandle( | ||
| * // context, allocationSize * sizeof(*buffer), reinterpret_cast<void*>(buffer) | ||
| * // ); | ||
| * @endcode | ||
| * | ||
| * @throws ucxx::Error if either `ucp_mem_map` or `ucp_mem_query` fail. | ||
| * | ||
| * @param[in] context parent context where to map memory. | ||
| * @param[in] size the minimum size of the memory allocation | ||
| * @param[in] buffer the pointer to an existing allocation or `nullptr` to allocate a | ||
| * new memory region. | ||
wence- marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| * | ||
| * @returns The `shared_ptr<ucxx::MemoryHandle>` object | ||
| */ | ||
| friend std::shared_ptr<MemoryHandle> createMemoryHandle(std::shared_ptr<Context> context, | ||
| const size_t size, | ||
| void* buffer); | ||
|
|
||
| ~MemoryHandle(); | ||
|
|
||
| /** | ||
| * @brief Get the underlying `ucp_mem_h` handle. | ||
| * | ||
| * Lifetime of the `ucp_mem_h` handle is managed by the `ucxx::MemoryHandle` object and | ||
| * its ownership is non-transferrable. Once the `ucxx::MemoryHandle` is destroyed the | ||
| * memory is unmapped and the handle is not valid anymore, it is the user's responsibility | ||
| * to ensure the owner's lifetime while using the handle. | ||
wence- marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| * | ||
| * @code{.cpp} | ||
| * // memoryHandle is `std::shared_ptr<ucxx::MemoryHandle>` | ||
| * ucp_mem_h ucpMemoryHandle = memoryHandle->getHandle(); | ||
| * @endcode | ||
| * | ||
| * @returns The underlying `ucp_mem_h` handle. | ||
| */ | ||
| ucp_mem_h getHandle(); | ||
|
|
||
| /** | ||
| * @brief Get the size of the memory allocation. | ||
| * | ||
| * Get the size of the memory allocation, which is at least the number of bytes specified | ||
| * with the `size` argument passed to `createMemoryHandle()`. | ||
| * | ||
| * @code{.cpp} | ||
| * // memoryHandle is `std::shared_ptr<ucxx::MemoryHandle>` | ||
| * auto memorySize = memoryHandle->getSize(); | ||
| * @endcode | ||
| * | ||
| * @returns The size of the memory allocation. | ||
| */ | ||
| size_t getSize() const; | ||
|
|
||
| /** | ||
| * @brief Get the base address of the memory allocation. | ||
| * | ||
| * Get the base address of the memory allocation, which is going to be used as the remote | ||
| * address to put or get memory via the `ucxx::Endpoint::memPut()` or | ||
| * `ucxx::Endpoint::memGet()` methods. | ||
| * | ||
| * @code{.cpp} | ||
| * // memoryHandle is `std::shared_ptr<ucxx::MemoryHandle>` | ||
| * auto memoryBase Address = memoryHandle->getBaseAddress(); | ||
| * @endcode | ||
| * | ||
| * @returns The base address of the memory allocation. | ||
| */ | ||
| uint64_t getBaseAddress(); | ||
|
|
||
| std::shared_ptr<RemoteKey> createRemoteKey(); | ||
| }; | ||
|
|
||
| } // namespace ucxx | ||
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.
Uh oh!
There was an error while loading. Please reload this page.