-
Notifications
You must be signed in to change notification settings - Fork 223
Standardize use of Device arguments #1238
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
Merged
Merged
Changes from all commits
Commits
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 hidden or 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 |
|---|---|---|
|
|
@@ -948,9 +948,16 @@ class Device: | |
| Default value of `None` return the currently used device. | ||
|
|
||
| """ | ||
| __slots__ = ("_id", "_mr", "_has_inited", "_properties", "_uuid") | ||
| __slots__ = ("_id", "_memory_resource", "_has_inited", "_properties", "_uuid") | ||
|
|
||
| def __new__(cls, device_id: int | None = None): | ||
| def __new__(cls, device_id: Device | int | None = None): | ||
| # Handle device_id argument. | ||
| if isinstance(device_id, Device): | ||
| return device_id | ||
| else: | ||
| device_id = getattr(device_id, 'device_id', device_id) | ||
|
|
||
| # Initialize CUDA. | ||
| global _is_cuInit | ||
| if _is_cuInit is False: | ||
| with _lock, nogil: | ||
|
|
@@ -976,7 +983,7 @@ class Device: | |
| raise ValueError(f"device_id must be >= 0, got {device_id}") | ||
|
|
||
| # ensure Device is singleton | ||
| cdef int total, attr | ||
| cdef int total | ||
| try: | ||
| devices = _tls.devices | ||
| except AttributeError: | ||
|
|
@@ -986,21 +993,7 @@ class Device: | |
| for dev_id in range(total): | ||
| device = super().__new__(cls) | ||
| device._id = dev_id | ||
| # If the device is in TCC mode, or does not support memory pools for some other reason, | ||
| # use the SynchronousMemoryResource which does not use memory pools. | ||
| with nogil: | ||
| HANDLE_RETURN( | ||
| cydriver.cuDeviceGetAttribute( | ||
| &attr, cydriver.CUdevice_attribute.CU_DEVICE_ATTRIBUTE_MEMORY_POOLS_SUPPORTED, dev_id | ||
| ) | ||
| ) | ||
| if attr == 1: | ||
| from cuda.core.experimental._memory import DeviceMemoryResource | ||
| device._mr = DeviceMemoryResource(dev_id) | ||
| else: | ||
| from cuda.core.experimental._memory import _SynchronousMemoryResource | ||
| device._mr = _SynchronousMemoryResource(dev_id) | ||
|
|
||
|
Comment on lines
-989
to
-1003
Contributor
Author
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. Moved to |
||
| device._memory_resource = None | ||
| device._has_inited = False | ||
| device._properties = None | ||
| device._uuid = None | ||
|
|
@@ -1128,13 +1121,31 @@ class Device: | |
| @property | ||
| def memory_resource(self) -> MemoryResource: | ||
| """Return :obj:`~_memory.MemoryResource` associated with this device.""" | ||
| return self._mr | ||
| cdef int attr, device_id | ||
| if self._memory_resource is None: | ||
| # If the device is in TCC mode, or does not support memory pools for some other reason, | ||
| # use the SynchronousMemoryResource which does not use memory pools. | ||
| device_id = self._id | ||
| with nogil: | ||
| HANDLE_RETURN( | ||
| cydriver.cuDeviceGetAttribute( | ||
| &attr, cydriver.CUdevice_attribute.CU_DEVICE_ATTRIBUTE_MEMORY_POOLS_SUPPORTED, device_id | ||
| ) | ||
| ) | ||
| if attr == 1: | ||
| from cuda.core.experimental._memory import DeviceMemoryResource | ||
| self._memory_resource = DeviceMemoryResource(self._id) | ||
| else: | ||
| from cuda.core.experimental._memory import _SynchronousMemoryResource | ||
| self._memory_resource = _SynchronousMemoryResource(self._id) | ||
|
|
||
| return self._memory_resource | ||
|
|
||
| @memory_resource.setter | ||
| def memory_resource(self, mr): | ||
| from cuda.core.experimental._memory import MemoryResource | ||
| assert_type(mr, MemoryResource) | ||
| self._mr = mr | ||
| self._memory_resource = mr | ||
|
|
||
| @property | ||
| def default_stream(self) -> Stream: | ||
|
|
@@ -1324,7 +1335,7 @@ class Device: | |
| self._check_context_initialized() | ||
| if stream is None: | ||
| stream = default_stream() | ||
| return self._mr.allocate(size, stream) | ||
| return self.memory_resource.allocate(size, stream) | ||
|
|
||
| def sync(self): | ||
| """Synchronize the device. | ||
|
|
||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
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.
I renamed this to match a request in a recent change to _memory/_buffer.*