Repository navigation
NIXL: Zero-copy nixlDescList #1324
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
Changes from all commits
e66c2b0
faf6026
7dd9bc7
063fda3
fd529da
391f325
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 | ||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -22,6 +22,8 @@ | |||||||||||||||||||
| #include <vector> | ||||||||||||||||||||
| #include <string> | ||||||||||||||||||||
| #include <algorithm> | ||||||||||||||||||||
| #include <cassert> | ||||||||||||||||||||
| #include <stdexcept> | ||||||||||||||||||||
| #include "nixl_types.h" | ||||||||||||||||||||
|
|
||||||||||||||||||||
| /** | ||||||||||||||||||||
|
|
@@ -260,20 +262,63 @@ operator==(const nixlRemoteDesc &lhs, const nixlRemoteDesc &rhs); | |||||||||||||||||||
| */ | ||||||||||||||||||||
| template<class T> | ||||||||||||||||||||
| class nixlDescList { | ||||||||||||||||||||
| protected: | ||||||||||||||||||||
| private: | ||||||||||||||||||||
| /** @var NIXL memory type */ | ||||||||||||||||||||
| nixl_mem_t type; | ||||||||||||||||||||
| /** @var Vector for storing nixlDescs */ | ||||||||||||||||||||
| std::vector<T> descs; | ||||||||||||||||||||
| /** @var Whether the descriptor list is a shallow copy */ | ||||||||||||||||||||
| bool isShallowCopy_; | ||||||||||||||||||||
| /** @var Common view of the descriptor list, both for shallow copy and own data */ | ||||||||||||||||||||
| const T *view_; | ||||||||||||||||||||
|
Contributor
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. Suggest naming this
Contributor
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. Could we also use |
||||||||||||||||||||
| /** @var Size of the descriptor list */ | ||||||||||||||||||||
| size_t size_; | ||||||||||||||||||||
|
|
||||||||||||||||||||
| /** | ||||||||||||||||||||
| * @brief Synchronize the view of the descriptor list with the data in the vector. | ||||||||||||||||||||
| * Can be called only in owner mode (not shallow copy), after vector is modified | ||||||||||||||||||||
| */ | ||||||||||||||||||||
| inline void | ||||||||||||||||||||
| syncView() { | ||||||||||||||||||||
|
Contributor
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. The |
||||||||||||||||||||
| view_ = descs.data(); | ||||||||||||||||||||
| size_ = descs.size(); | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| /** | ||||||||||||||||||||
| * @brief Check that the descriptor list is not a shallow copy. | ||||||||||||||||||||
| * Throws std::logic_error if it is. | ||||||||||||||||||||
| */ | ||||||||||||||||||||
| void | ||||||||||||||||||||
| checkModifiable() const { | ||||||||||||||||||||
| if (isShallowCopy_) { | ||||||||||||||||||||
| throw std::logic_error("Descriptor list is a non-modifiable shallow copy"); | ||||||||||||||||||||
| } | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| /** | ||||||||||||||||||||
| * @brief Swap two nixlDescList objects. | ||||||||||||||||||||
| */ | ||||||||||||||||||||
| static void | ||||||||||||||||||||
| swap(nixlDescList<T> &first, nixlDescList<T> &second) noexcept; | ||||||||||||||||||||
|
|
||||||||||||||||||||
| /** | ||||||||||||||||||||
| * @brief Private constructor to create a shallow copy of a nixlDescList. | ||||||||||||||||||||
| * Used internally by the makeShallowCopy to return only a const reference | ||||||||||||||||||||
| * to the shallow copy. | ||||||||||||||||||||
| */ | ||||||||||||||||||||
| nixlDescList(const nixl_mem_t &type, const T *view, size_t size); | ||||||||||||||||||||
|
|
||||||||||||||||||||
| public: | ||||||||||||||||||||
| using iterator_t = typename std::vector<T>::iterator; | ||||||||||||||||||||
|
Contributor
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. The inconsistency between the two iterator types looks a bit weird to me. |
||||||||||||||||||||
| using const_iterator_t = const T *; | ||||||||||||||||||||
|
|
||||||||||||||||||||
| /** | ||||||||||||||||||||
| * @brief Parametrized Constructor for nixlDescList | ||||||||||||||||||||
| * | ||||||||||||||||||||
| * @param type NIXL memory type of descriptor list | ||||||||||||||||||||
| * @param init_size initial size for descriptor list (default = 0) | ||||||||||||||||||||
| */ | ||||||||||||||||||||
| nixlDescList(const nixl_mem_t &type, const int &init_size = 0); | ||||||||||||||||||||
| nixlDescList(const nixl_mem_t &type, size_t init_size = 0); | ||||||||||||||||||||
|
|
||||||||||||||||||||
| /** | ||||||||||||||||||||
| * @brief Deserializer constructor for nixlDescList from nixlSerDes object | ||||||||||||||||||||
|
|
@@ -289,15 +334,36 @@ class nixlDescList { | |||||||||||||||||||
| * | ||||||||||||||||||||
| * @param d_list other nixlDescList object of the same type | ||||||||||||||||||||
| */ | ||||||||||||||||||||
| nixlDescList(const nixlDescList<T> &d_list) = default; | ||||||||||||||||||||
| nixlDescList(const nixlDescList<T> &d_list); | ||||||||||||||||||||
|
|
||||||||||||||||||||
| /** | ||||||||||||||||||||
| * @brief Move constructor for creating nixlDescList from another object | ||||||||||||||||||||
| * of the same type. | ||||||||||||||||||||
| * | ||||||||||||||||||||
| * @param d_list other nixlDescList object of the same type | ||||||||||||||||||||
| */ | ||||||||||||||||||||
| nixlDescList(nixlDescList<T> &&d_list) noexcept; | ||||||||||||||||||||
|
|
||||||||||||||||||||
| /** | ||||||||||||||||||||
| * @brief Operator = overloading constructor for nixlDescList | ||||||||||||||||||||
| * | ||||||||||||||||||||
| * @param d_list nixlDescList object | ||||||||||||||||||||
| */ | ||||||||||||||||||||
| nixlDescList & | ||||||||||||||||||||
| operator=(const nixlDescList<T> &d_list) = default; | ||||||||||||||||||||
| operator=(nixlDescList<T> d_list) noexcept; | ||||||||||||||||||||
|
|
||||||||||||||||||||
| /** | ||||||||||||||||||||
| * @brief Create a shallow copy of a nixlDescList. | ||||||||||||||||||||
| * | ||||||||||||||||||||
| * @param type NIXL memory type of descriptor list | ||||||||||||||||||||
| * @param view View of the descriptor list | ||||||||||||||||||||
| * @param size Size of the descriptor list | ||||||||||||||||||||
| * @return Shallow copy of the descriptor list | ||||||||||||||||||||
| */ | ||||||||||||||||||||
| static const nixlDescList<T> | ||||||||||||||||||||
|
Contributor
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.
|
||||||||||||||||||||
| makeShallowCopy(const nixl_mem_t &type, const T *view, size_t size) { | ||||||||||||||||||||
| return nixlDescList<T>(type, view, size); | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| /** | ||||||||||||||||||||
| * @brief nixlDescList Destructor | ||||||||||||||||||||
|
|
@@ -315,49 +381,57 @@ class nixlDescList { | |||||||||||||||||||
| /** | ||||||||||||||||||||
| * @brief Get count of descriptors | ||||||||||||||||||||
| */ | ||||||||||||||||||||
| inline int | ||||||||||||||||||||
| int | ||||||||||||||||||||
| descCount() const { | ||||||||||||||||||||
| return descs.size(); | ||||||||||||||||||||
| return size_; | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
Comment on lines
+384
to
+387
Contributor
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. Potential narrowing conversion from
Suggested fix- int
+ size_t
descCount() const {
return size_;
}Or, if int
descCount() const {
+ assert(size_ <= static_cast<size_t>(std::numeric_limits<int>::max()));
return size_;
}📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||
|
|
||||||||||||||||||||
| size_t | ||||||||||||||||||||
| size() const { | ||||||||||||||||||||
| return size_; | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| /** | ||||||||||||||||||||
| * @brief Check if nixlDescList is empty or not | ||||||||||||||||||||
| */ | ||||||||||||||||||||
| bool | ||||||||||||||||||||
| isEmpty() const noexcept { | ||||||||||||||||||||
| return descs.empty(); | ||||||||||||||||||||
| inline bool | ||||||||||||||||||||
| isEmpty() const { | ||||||||||||||||||||
| return (size_ == 0); | ||||||||||||||||||||
|
Contributor
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. Brackets are superfluous (as is the |
||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| const T & | ||||||||||||||||||||
| inline const T & | ||||||||||||||||||||
|
Contributor
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. Same thing about |
||||||||||||||||||||
| operator[](size_t index) const { | ||||||||||||||||||||
| return descs[index]; | ||||||||||||||||||||
| return view_[index]; | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| T & | ||||||||||||||||||||
| inline T & | ||||||||||||||||||||
| operator[](size_t index) { | ||||||||||||||||||||
|
Contributor
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. Maybe add an |
||||||||||||||||||||
| assert(!isShallowCopy_); | ||||||||||||||||||||
| return descs[index]; | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| /** | ||||||||||||||||||||
| * @brief Vector like iterators for const and non-const elements | ||||||||||||||||||||
| */ | ||||||||||||||||||||
| inline typename std::vector<T>::const_iterator | ||||||||||||||||||||
| const_iterator_t | ||||||||||||||||||||
| begin() const { | ||||||||||||||||||||
| return descs.begin(); | ||||||||||||||||||||
| return view_; | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| inline typename std::vector<T>::const_iterator | ||||||||||||||||||||
| const_iterator_t | ||||||||||||||||||||
| end() const { | ||||||||||||||||||||
| return descs.end(); | ||||||||||||||||||||
| return view_ + size_; | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| inline typename std::vector<T>::iterator | ||||||||||||||||||||
| iterator_t | ||||||||||||||||||||
| begin() { | ||||||||||||||||||||
| assert(!isShallowCopy_); | ||||||||||||||||||||
| return descs.begin(); | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| inline typename std::vector<T>::iterator | ||||||||||||||||||||
| iterator_t | ||||||||||||||||||||
| end() { | ||||||||||||||||||||
| assert(!isShallowCopy_); | ||||||||||||||||||||
| return descs.end(); | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
|
|
@@ -378,28 +452,33 @@ class nixlDescList { | |||||||||||||||||||
| * @param count Number of elements after resizing DescList object | ||||||||||||||||||||
| */ | ||||||||||||||||||||
| virtual void | ||||||||||||||||||||
| resize(const size_t &count); | ||||||||||||||||||||
| resize(size_t count); | ||||||||||||||||||||
|
|
||||||||||||||||||||
| /** | ||||||||||||||||||||
| * @brief Empty the descriptors list | ||||||||||||||||||||
| */ | ||||||||||||||||||||
| inline void | ||||||||||||||||||||
| clear() { | ||||||||||||||||||||
| checkModifiable(); | ||||||||||||||||||||
| descs.clear(); | ||||||||||||||||||||
| syncView(); | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| /** | ||||||||||||||||||||
| * @brief Add Descriptors to descriptor list | ||||||||||||||||||||
| */ | ||||||||||||||||||||
| virtual void | ||||||||||||||||||||
| addDesc(const T &desc); | ||||||||||||||||||||
| addDesc(T desc); | ||||||||||||||||||||
|
|
||||||||||||||||||||
| void | ||||||||||||||||||||
| addDesc(T desc, iterator_t it); | ||||||||||||||||||||
|
|
||||||||||||||||||||
| /** | ||||||||||||||||||||
| * @brief Remove descriptor from list at index | ||||||||||||||||||||
| * Can throw std::out_of_range exception. | ||||||||||||||||||||
| */ | ||||||||||||||||||||
| void | ||||||||||||||||||||
| remDesc(const int &index); | ||||||||||||||||||||
| remDesc(size_t index); | ||||||||||||||||||||
|
Comment on lines
480
to
+481
Contributor
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. 🧩 Analysis chain🏁 Script executed: #!/bin/bash
# Verify callers of remDesc that may pass negative values
echo "=== Checking remDesc callers with int index ==="
rg -n -B2 -A2 'remDesc\(' --type cpp
echo ""
echo "=== Checking getIndex return type and usage ==="
rg -n 'int.*getIndex|getIndex.*int' --type cppRepository: ai-dynamo/nixl Length of output: 4346 🏁 Script executed: # Check remDesc implementation
sed -n '259,270p' src/infra/nixl_descriptors.cpp
echo ""
echo "=== Check Rust wrapper signatures ==="
sed -n '1080,1100p' src/bindings/rust/wrapper.cpp
sed -n '1275,1295p' src/bindings/rust/wrapper.cpp
echo ""
echo "=== Check NIXL_ERR_NOT_FOUND definition ==="
rg -n 'NIXL_ERR_NOT_FOUND|define.*NOT_FOUND' --type cpp --type h | head -20
echo ""
echo "=== Check getIndex implementation ==="
sed -n '298,320p' src/infra/nixl_descriptors.cppRepository: ai-dynamo/nixl Length of output: 4056 🏁 Script executed: rg -n 'define NIXL_ERR_NOT_FOUND|NIXL_ERR_NOT_FOUND\s*=' --type hRepository: ai-dynamo/nixl Length of output: 114 Critical: The signature change from When a negative Suggested fix: Keep int signature with proper validation void
- remDesc(size_t index);
+ remDesc(int index);Or add explicit validation in the implementation: void
nixlDescList<T>::remDesc(int index) {
checkModifiable();
if ((index >= (int)descs.size()) || (index < 0)) {
throw std::out_of_range("Index is out of range");
}
descs.erase(descs.begin() + index);
syncView();
}Note: The current check 📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||
|
|
||||||||||||||||||||
| /** | ||||||||||||||||||||
| * @brief Convert a nixlDescList with metadata by trimming it to a | ||||||||||||||||||||
|
|
||||||||||||||||||||
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.
I wonder whether "shallow copy" is the right term, how about something like non-owning view, or read-only view?