Skip to content
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

Prevent DeviceBuffer DeviceMemoryResource premature release #931

Merged
merged 8 commits into from
Jan 5, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions python/rmm/_lib/device_buffer.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,19 @@
# limitations under the License.

from libc.stdint cimport uintptr_t
from libcpp.memory cimport unique_ptr
from libcpp.memory cimport unique_ptr, shared_ptr

from rmm._cuda.stream cimport Stream
from rmm._lib.cuda_stream_view cimport cuda_stream_view
from rmm._lib.memory_resource cimport DeviceMemoryResource
viclafargue marked this conversation as resolved.
Show resolved Hide resolved


cdef extern from "rmm/mr/device/device_memory_resource.hpp" \
namespace "rmm::mr" nogil:
cdef cppclass device_memory_resource:
pass

viclafargue marked this conversation as resolved.
Show resolved Hide resolved

cdef extern from "rmm/device_buffer.hpp" namespace "rmm" nogil:
cdef cppclass device_buffer:
device_buffer()
Expand All @@ -36,10 +42,10 @@ cdef extern from "rmm/device_buffer.hpp" namespace "rmm" nogil:
cdef class DeviceBuffer:
cdef unique_ptr[device_buffer] c_obj

# Holds a reference to the DeviceMemoryResource used for allocation.
# Ensures the MR does not get destroyed before this DeviceBuffer. `mr` is
# needed for deallocation
cdef DeviceMemoryResource mr
# Holds a reference to the device_memory_resource used for allocation.
# Ensures the memory resource does not get destroyed before
# this DeviceBuffer as it is needed for deallocation.
viclafargue marked this conversation as resolved.
Show resolved Hide resolved
cdef shared_ptr[device_memory_resource] mr

# Holds a reference to the stream used by the underlying `device_buffer`.
# Ensures the stream does not get destroyed before this DeviceBuffer
Expand Down
2 changes: 1 addition & 1 deletion python/rmm/_lib/device_buffer.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ cdef class DeviceBuffer:
stream.c_synchronize()

# Save a reference to the MR and stream used for allocation
self.mr = get_current_device_resource()
self.mr = get_current_device_resource().get()
viclafargue marked this conversation as resolved.
Show resolved Hide resolved
self.stream = stream

def __len__(self):
Expand Down
1 change: 1 addition & 0 deletions python/rmm/_lib/memory_resource.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ cdef extern from "rmm/mr/device/device_memory_resource.hpp" \
cdef class DeviceMemoryResource:
cdef shared_ptr[device_memory_resource] c_obj

cdef shared_ptr[device_memory_resource] get(self)
cdef device_memory_resource* get_mr(self)

cdef class UpstreamResourceAdaptor(DeviceMemoryResource):
Expand Down
3 changes: 3 additions & 0 deletions python/rmm/_lib/memory_resource.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,9 @@ cdef extern from "rmm/mr/device/per_device_resource.hpp" namespace "rmm" nogil:

cdef class DeviceMemoryResource:

cdef shared_ptr[device_memory_resource] get(self):
return self.c_obj

cdef device_memory_resource* get_mr(self):
return self.c_obj.get()

Expand Down