-
Notifications
You must be signed in to change notification settings - Fork 209
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
Enable constructing DeviceBuffers using a non-default memory resource and correctly manage memory resource lifetime in DeviceBuffer #953
Changes from all commits
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 | ||||
---|---|---|---|---|---|---|
|
@@ -47,7 +47,8 @@ cdef class DeviceBuffer: | |||||
def __cinit__(self, *, | ||||||
uintptr_t ptr=0, | ||||||
size_t size=0, | ||||||
Stream stream=DEFAULT_STREAM): | ||||||
Stream stream=None, | ||||||
DeviceMemoryResource mr=None): | ||||||
"""Construct a ``DeviceBuffer`` with optional size and data pointer | ||||||
|
||||||
Parameters | ||||||
|
@@ -64,9 +65,13 @@ cdef class DeviceBuffer: | |||||
scope while the DeviceBuffer is in use. Destroying the | ||||||
underlying stream while the DeviceBuffer is in use will | ||||||
result in undefined behavior. | ||||||
mr : DeviceMemoryResource, optional | ||||||
The memory resource to use for the allocation. If not | ||||||
provided, the memory resource returned from | ||||||
``get_current_device_resource()`` is used. | ||||||
|
||||||
Note | ||||||
---- | ||||||
Notes | ||||||
----- | ||||||
|
||||||
If the pointer passed is non-null and ``stream`` is the default stream, | ||||||
it is synchronized after the copy. However if a non-default ``stream`` | ||||||
|
@@ -77,25 +82,36 @@ cdef class DeviceBuffer: | |||||
>>> import rmm | ||||||
>>> db = rmm.DeviceBuffer(size=5) | ||||||
""" | ||||||
cdef const void* c_ptr | ||||||
cdef: | ||||||
const void* c_ptr | ||||||
cuda_stream_view c_stream_view | ||||||
device_memory_resource* c_mr | ||||||
|
||||||
if stream is None: | ||||||
stream = DEFAULT_STREAM | ||||||
if mr is None: | ||||||
mr = get_current_device_resource() | ||||||
shwina marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
||||||
# Save a reference to the MR and stream used for allocation | ||||||
self.mr = mr | ||||||
self.stream = stream | ||||||
|
||||||
with nogil: | ||||||
c_ptr = <const void*>ptr | ||||||
shwina marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
c_stream_view = stream.view() | ||||||
c_mr = mr.c_obj.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. Just a side note, it may be possible to use rmm/python/rmm/_lib/memory_resource.pyx Lines 167 to 168 in d94bdfd
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. Ah, that's probably what we should do |
||||||
|
||||||
if size == 0: | ||||||
self.c_obj.reset(new device_buffer()) | ||||||
elif c_ptr == NULL: | ||||||
self.c_obj.reset(new device_buffer(size, stream.view())) | ||||||
self.c_obj.reset(new device_buffer(size, c_stream_view, c_mr)) | ||||||
else: | ||||||
self.c_obj.reset(new device_buffer(c_ptr, size, stream.view())) | ||||||
self.c_obj.reset(new device_buffer( | ||||||
c_ptr, size, c_stream_view, c_mr)) | ||||||
|
||||||
if stream.c_is_default(): | ||||||
stream.c_synchronize() | ||||||
|
||||||
# Save a reference to the MR and stream used for allocation | ||||||
self.mr = get_current_device_resource() | ||||||
self.stream = stream | ||||||
|
||||||
def __len__(self): | ||||||
return self.size | ||||||
|
||||||
|
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.
May need to
cimport
this anddevice_memory_resource
above