-
Notifications
You must be signed in to change notification settings - Fork 217
Add the cuda.core.experimental.system singleton #256
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 3 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
7d5c742
add the system class
ksimpson-work 4c4acef
fix old test change
ksimpson-work 36f045c
run ruff manually
ksimpson-work 5f8ff80
Update cuda_core/docs/source/release/0.2.0-notes.md
ksimpson-work d1d6928
Update cuda_core/docs/source/release.md
ksimpson-work cfa9d16
address comments
ksimpson-work 8e43cd2
rename release file
ksimpson-work 1315bc0
merge main
ksimpson-work bff2627
update link style to match other PR
ksimpson-work 5e3bdcd
add copyright header
ksimpson-work b825cac
Merge branch 'main' into add_system
leofang 1a3f1e6
fix docs
leofang 6a59594
fix build warning and output format of docs
ksimpson-work 1bbeb87
Merge branch 'main' into add_system
ksimpson-work 769ac66
Update cuda_core/docs/source/api_private.rst
ksimpson-work 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
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 |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| from typing import Tuple | ||
|
|
||
| from cuda import cuda, cudart | ||
| from cuda.core.experimental._device import Device | ||
| from cuda.core.experimental._utils import handle_return | ||
|
|
||
|
|
||
| class System: | ||
| """ Provide information about the cuda system. | ||
| This class is a singleton and should not be instantiated directly. | ||
| """ | ||
|
|
||
| _instance = None | ||
|
|
||
| def __new__(cls): | ||
| if cls._instance is None: | ||
| cls._instance = super().__new__(cls) | ||
| return cls._instance | ||
|
|
||
| def __init__(self): | ||
| if hasattr(self, '_initialized') and self._initialized: | ||
| return | ||
| self._initialized = True | ||
|
|
||
| @property | ||
| def driver_version(self) -> Tuple[int, int]: | ||
| """ | ||
| Query the CUDA driver version. | ||
|
|
||
| Returns | ||
| ------- | ||
| tuple of int | ||
| A 2-tuple of (major, minor) version numbers. | ||
| """ | ||
| version = handle_return(cuda.cuDriverGetVersion()) | ||
| major = version // 1000 | ||
| minor = (version % 1000) // 10 | ||
| return (major, minor) | ||
|
|
||
| @property | ||
| def num_devices(self) -> int: | ||
| """ | ||
| Query the number of available GPUs. | ||
|
|
||
| Returns | ||
| ------- | ||
| int | ||
| The number of available GPU devices. | ||
| """ | ||
| return handle_return(cudart.cudaGetDeviceCount()) | ||
|
|
||
| @property | ||
| def devices(self) -> tuple: | ||
| """ | ||
| Query the available device instances. | ||
|
|
||
| Returns | ||
| ------- | ||
| tuple of Device | ||
| A tuple containing instances of available devices. | ||
| """ | ||
| total = self.num_devices | ||
| return tuple(Device(device_id) for device_id in range(total)) | ||
|
|
||
| system = System() | ||
| system.__doc__ = """ | ||
| Singleton instance of the :obj:`~cuda.core.experimental._system.System` class. | ||
| """ |
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 |
|---|---|---|
|
|
@@ -16,6 +16,7 @@ CUDA runtime | |
|
|
||
| Device | ||
| launch | ||
| system | ||
|
|
||
| :template: dataclass.rst | ||
|
|
||
|
|
||
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 |
|---|---|---|
|
|
@@ -16,6 +16,7 @@ CUDA runtime | |
| _memory.Buffer | ||
| _stream.Stream | ||
| _event.Event | ||
| _system.System | ||
|
|
||
|
|
||
| CUDA compilation toolchain | ||
|
|
||
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
ksimpson-work marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
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 |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| # `cuda.core` Release notes | ||
|
|
||
| Released on <TODO>, 2024 | ||
|
|
||
| ## Hightlights | ||
| - Addition of the system singleton | ||
ksimpson-work marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| ## Limitations | ||
|
|
||
| <TODO> | ||
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 |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| try: | ||
| from cuda.bindings import driver, runtime | ||
| except ImportError: | ||
| from cuda import cuda as driver | ||
| from cuda import cudart as runtime | ||
|
|
||
| from cuda.core.experimental import Device, system | ||
| from cuda.core.experimental._utils import handle_return | ||
|
|
||
|
|
||
| def test_system_singleton(): | ||
| system1 = system | ||
| system2 = system | ||
| assert system1 is system2, "system is not a singleton" | ||
ksimpson-work marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| def test_driver_version(): | ||
| driver_version = system.driver_version | ||
| print(driver_version) | ||
| version = handle_return(driver.cuDriverGetVersion()) | ||
| expected_driver_version = (version // 1000, (version % 1000) // 10) | ||
| assert driver_version == expected_driver_version, "Driver version does not match expected value" | ||
|
|
||
| def test_num_devices(): | ||
| num_devices = system.num_devices | ||
| expected_num_devices = handle_return(runtime.cudaGetDeviceCount()) | ||
| assert num_devices == expected_num_devices, "Number of devices does not match expected value" | ||
|
|
||
| def test_devices(): | ||
| devices = system.devices | ||
| expected_num_devices = handle_return(runtime.cudaGetDeviceCount()) | ||
| expected_devices = tuple(Device(device_id) for device_id in range(expected_num_devices)) | ||
| assert len(devices) == len(expected_devices), "Number of devices does not match expected value" | ||
| for device, expected_device in zip(devices, expected_devices): | ||
| assert device.device_id == expected_device.device_id, "Device ID does not match expected value" | ||
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.
Uh oh!
There was an error while loading. Please reload this page.