Skip to content

Commit

Permalink
Remove C++ Wrappers in memory_resource_adaptors.hpp Needed by Cython (
Browse files Browse the repository at this point in the history
#662)

Depends on #661 

While working on PR #661, it looked like it was possible to remove the "owning" C++ memory resource adaptors in `memory_resource_adaptors.hpp`. This PR is a quick implementation of what that would look like to run through CI and get feedback.

The main driving factor of this PR is to eliminate the need for 2 layers of wrappers around every memory resource in the library. When adding new memory resources, C++ wrappers must be created in `memory_resource_adaptors.hpp` and Cython wrappers must be created in `memory_resource.pyx`, for any property/function that needs to be exposed at the python level. This removes the C++ wrappers in favor of using pythons reference counting for lifetime management.

A few notes:

1. `MemoryResource` was renamed `DeviceMemoryResource` to more closely match the C++ class names. Easily can be changed back
1. Upstream MR are kept alive by a base class `UpstreamResourceAdaptor` that stores a single property `upstream_mr`. Any MR that has an upstream, needs to inherit from this class.
1. Once the `UpstreamResourceAdaptor` was created, most of the work/changes were updating the Cython imports to use the C++ classes instead of the C++ wrappers.
1. This should make it easier to expose more methods/properties at the python layer in the future.

Would appreciate any feedback.

Authors:
  - Michael Demoret (@mdemoret-nv)

Approvers:
  - Christopher Harris (@cwharris)
  - Keith Kraus (@kkraus14)
  - Ashwin Srinath (@shwina)

URL: #662
  • Loading branch information
mdemoret-nv authored Mar 1, 2021
1 parent 869d374 commit bbc3e97
Show file tree
Hide file tree
Showing 6 changed files with 280 additions and 387 deletions.
14 changes: 7 additions & 7 deletions python/rmm/_lib/device_buffer.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@
# See the License for the specific language governing permissions and
# limitations under the License.

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

from rmm._lib.cuda_stream_view cimport cuda_stream_view
from rmm._cuda.stream cimport Stream
from rmm._lib.memory_resource cimport MemoryResource
from rmm._lib.cuda_stream_view cimport cuda_stream_view
from rmm._lib.memory_resource cimport DeviceMemoryResource


cdef extern from "rmm/device_buffer.hpp" namespace "rmm" nogil:
Expand All @@ -39,10 +39,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 MemoryResource used for allocation. Ensures the
# MR does not get destroyed before this DeviceBuffer. `mr` is needed for
# deallocation
cdef MemoryResource mr
# 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 stream used by the underlying `device_buffer`.
# Ensures the stream does not get destroyed before this DeviceBuffer
Expand Down
120 changes: 31 additions & 89 deletions python/rmm/_lib/memory_resource.pxd
Original file line number Diff line number Diff line change
@@ -1,108 +1,50 @@
# Copyright (c) 2020, NVIDIA CORPORATION.

from libc.stdint cimport int8_t
from libcpp.vector cimport vector
from libcpp.string cimport string
from libcpp.memory cimport shared_ptr
from libcpp.string cimport string
from libcpp.vector cimport vector


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

cdef class DeviceMemoryResource:
cdef shared_ptr[device_memory_resource] c_obj

cdef extern from "memory_resource_wrappers.hpp" nogil:
cdef cppclass device_memory_resource_wrapper:
shared_ptr[device_memory_resource_wrapper] get_mr() except +

cdef cppclass default_memory_resource_wrapper(
device_memory_resource_wrapper
):
default_memory_resource_wrapper(int device) except +

cdef cppclass cuda_memory_resource_wrapper(device_memory_resource_wrapper):
cuda_memory_resource_wrapper() except +

cdef cppclass managed_memory_resource_wrapper(
device_memory_resource_wrapper
):
managed_memory_resource_wrapper() except +

cdef cppclass pool_memory_resource_wrapper(device_memory_resource_wrapper):
pool_memory_resource_wrapper(
shared_ptr[device_memory_resource_wrapper] upstream_mr,
size_t initial_pool_size,
size_t maximum_pool_size
) except +

cdef cppclass fixed_size_memory_resource_wrapper(
device_memory_resource_wrapper
):
fixed_size_memory_resource_wrapper(
shared_ptr[device_memory_resource_wrapper] upstream_mr,
size_t block_size,
size_t blocks_to_preallocate
) except +

cdef cppclass binning_memory_resource_wrapper(
device_memory_resource_wrapper
):
binning_memory_resource_wrapper(
shared_ptr[device_memory_resource_wrapper] upstream_mr
) except +
binning_memory_resource_wrapper(
shared_ptr[device_memory_resource_wrapper] upstream_mr,
int8_t min_size_exponent,
int8_t max_size_exponent
) except +
void add_bin(
size_t allocation_size,
shared_ptr[device_memory_resource_wrapper] bin_mr
) except +
void add_bin(
size_t allocation_size
) except +

cdef cppclass logging_resource_adaptor_wrapper(
device_memory_resource_wrapper
):
logging_resource_adaptor_wrapper(
shared_ptr[device_memory_resource_wrapper] upstream_mr,
string filename
) except +
void flush() except +

cdef cppclass thread_safe_resource_adaptor_wrapper(
device_memory_resource_wrapper
):
thread_safe_resource_adaptor_wrapper(
shared_ptr[device_memory_resource_wrapper] upstream_mr,
) except +

void set_per_device_resource(
int device,
shared_ptr[device_memory_resource_wrapper] new_resource
) except +


cdef class MemoryResource:
cdef shared_ptr[device_memory_resource_wrapper] c_obj

cdef class CudaMemoryResource(MemoryResource):
cdef device_memory_resource* get_mr(self)

cdef class UpstreamResourceAdaptor(DeviceMemoryResource):
cdef readonly DeviceMemoryResource upstream_mr

cpdef DeviceMemoryResource get_upstream(self)

cdef class CudaMemoryResource(DeviceMemoryResource):
pass

cdef class ManagedMemoryResource(MemoryResource):
cdef class ManagedMemoryResource(DeviceMemoryResource):
pass

cdef class PoolMemoryResource(MemoryResource):
cdef class PoolMemoryResource(UpstreamResourceAdaptor):
pass

cdef class FixedSizeMemoryResource(MemoryResource):
cdef class FixedSizeMemoryResource(UpstreamResourceAdaptor):
pass

cdef class BinningMemoryResource(MemoryResource):
cpdef add_bin(self, size_t allocation_size, object bin_resource=*)
cdef class BinningMemoryResource(UpstreamResourceAdaptor):

cdef readonly list bin_mrs

cpdef add_bin(
self,
size_t allocation_size,
DeviceMemoryResource bin_resource=*)

cdef class LoggingResourceAdaptor(MemoryResource):
cdef MemoryResource upstream
cdef class LoggingResourceAdaptor(UpstreamResourceAdaptor):
cdef object _log_file_name
cpdef MemoryResource get_upstream(self)
cpdef get_file_name(self)
cpdef flush(self)

cpdef MemoryResource get_current_device_resource()
cpdef DeviceMemoryResource get_current_device_resource()
Loading

0 comments on commit bbc3e97

Please sign in to comment.