-
Notifications
You must be signed in to change notification settings - Fork 209
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove C++ Wrappers in
memory_resource_adaptors.hpp
Needed by Cython (
#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
1 parent
869d374
commit bbc3e97
Showing
6 changed files
with
280 additions
and
387 deletions.
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 |
---|---|---|
@@ -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() |
Oops, something went wrong.