-
Notifications
You must be signed in to change notification settings - Fork 193
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 gdb
pretty-printers for rmm types
#1088
Merged
rapids-bot
merged 9 commits into
rapidsai:branch-22.10
from
upsj:feature/pretty-printers
Aug 26, 2022
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
46ef3dc
add pretty-printers
upsj 15bedd1
wip
upsj 533774e
pull in thrust pretty-printer sources
upsj 69eb5d2
provide script to load pretty-printers
upsj 7c839a3
review updates
upsj 4171b2f
modernize python code
upsj 4e73504
consistent quotes
upsj ccfb112
review updates
upsj 433fda0
remove unnecessary error message
upsj 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 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,144 @@ | ||
# Copyright (c) 2022, 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. | ||
# | ||
|
||
import gdb | ||
|
||
|
||
class HostIterator: | ||
"""Iterates over arrays in host memory.""" | ||
|
||
def __init__(self, start, size): | ||
self.item = start | ||
self.size = size | ||
self.count = 0 | ||
|
||
def __iter__(self): | ||
return self | ||
|
||
def __next__(self): | ||
if self.count >= self.size: | ||
raise StopIteration | ||
elt = self.item.dereference() | ||
count = self.count | ||
self.item += 1 | ||
self.count += 1 | ||
return (f"[{count}]", elt) | ||
|
||
|
||
class DeviceIterator: | ||
"""Iterates over arrays in device memory by copying chunks into host memory.""" | ||
|
||
def __init__(self, start, size): | ||
self.exec = exec | ||
self.item = start | ||
self.size = size | ||
self.count = 0 | ||
self.buffer = None | ||
self.sizeof = self.item.dereference().type.sizeof | ||
self.buffer_start = 0 | ||
# At most 1 MB or size, at least 1 | ||
self.buffer_size = min(size, max(1, 2 ** 20 // self.sizeof)) | ||
self.buffer = gdb.parse_and_eval(f"(void*)malloc({self.buffer_size * self.sizeof})") | ||
self.buffer.fetch_lazy() | ||
self.buffer_count = self.buffer_size | ||
self.update_buffer() | ||
|
||
def update_buffer(self): | ||
if self.buffer_count >= self.buffer_size: | ||
self.buffer_item = gdb.parse_and_eval( | ||
hex(self.buffer)).cast(self.item.type) | ||
self.buffer_count = 0 | ||
self.buffer_start = self.count | ||
device_addr = hex(self.item.dereference().address) | ||
buffer_addr = hex(self.buffer) | ||
size = min(self.buffer_size, self.size - | ||
self.buffer_start) * self.sizeof | ||
status = gdb.parse_and_eval( | ||
f"(cudaError)cudaMemcpy({buffer_addr}, {device_addr}, {size}, cudaMemcpyDeviceToHost)") | ||
if status != 0: | ||
raise gdb.MemoryError( | ||
f"memcpy from device failed: {status}") | ||
|
||
def __del__(self): | ||
gdb.parse_and_eval(f"(void)free({hex(self.buffer)})").fetch_lazy() | ||
|
||
def __iter__(self): | ||
return self | ||
|
||
def __next__(self): | ||
if self.count >= self.size: | ||
raise StopIteration | ||
self.update_buffer() | ||
elt = self.buffer_item.dereference() | ||
self.buffer_item += 1 | ||
self.buffer_count += 1 | ||
count = self.count | ||
self.item += 1 | ||
self.count += 1 | ||
return (f"[{count}]", elt) | ||
|
||
|
||
class RmmDeviceUVectorPrinter(gdb.printing.PrettyPrinter): | ||
"""Print a rmm::device_uvector.""" | ||
|
||
def __init__(self, val): | ||
self.val = val | ||
el_type = val.type.template_argument(0) | ||
self.pointer = val["_storage"]["_data"].cast(el_type.pointer()) | ||
self.size = int(val["_storage"]["_size"]) // el_type.sizeof | ||
self.capacity = int(val["_storage"]["_capacity"]) // el_type.sizeof | ||
|
||
def children(self): | ||
return DeviceIterator(self.pointer, self.size) | ||
|
||
def to_string(self): | ||
return (f"{self.val.type} of length {self.size}, capacity {self.capacity}") | ||
|
||
def display_hint(self): | ||
return "array" | ||
|
||
|
||
# Workaround to avoid using the pretty printer on things like std::vector<int>::iterator | ||
def is_template_type_not_alias(typename): | ||
loc = typename.find("<") | ||
if loc is None: | ||
return False | ||
depth = 0 | ||
for char in typename[loc:-1]: | ||
if char == "<": | ||
depth += 1 | ||
if char == ">": | ||
depth -= 1 | ||
if depth == 0: | ||
return False | ||
return True | ||
|
||
|
||
def template_match(typename, template_name): | ||
return typename.startswith(template_name + "<") and typename.endswith(">") | ||
|
||
|
||
def lookup_rmm_type(val): | ||
if not str(val.type.unqualified()).startswith("rmm::"): | ||
return None | ||
suffix = str(val.type.unqualified())[5:] | ||
if not is_template_type_not_alias(suffix): | ||
return None | ||
if template_match(suffix, "device_uvector"): | ||
return RmmDeviceUVectorPrinter(val) | ||
return None | ||
|
||
|
||
gdb.pretty_printers.append(lookup_rmm_type) |
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,2 @@ | ||
source @Thrust_SOURCE_DIR@/scripts/gdb-pretty-printers.py | ||
source @PROJECT_SOURCE_DIR@/scripts/gdb-pretty-printers.py |
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.
What is a .in file?
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.
Not sure if this is a general convention, but in the projects I worked in, we use it for template files that will be expanded into an actual file by CMake - config headers, pkg-config files etc.
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.
The CMake convention is
<file>.suffix.in
for the input toconfigure_file
. (.cpp.in
=>.cpp
)