-
Notifications
You must be signed in to change notification settings - Fork 189
ArrayBuffer Lazy-assignment for GPU Context #5076
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
Draft
SamSJackson
wants to merge
29
commits into
connorjward/pyop3
Choose a base branch
from
SamSJackson/pyop3-outer
base: connorjward/pyop3
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+395
−37
Draft
Changes from 18 commits
Commits
Show all changes
29 commits
Select commit
Hold shift + click to select a range
8ceb830
introduce device.py and set up branch
ba6de9b
const parameter for host device
6a9fd21
include gpu demo and update petsc version as 3.24.5 misaligned for PC…
fb3d0b7
noting areas for change
e39f215
introducing context variable and lazy cupy
2a54e84
lazy evaluation of arrays
98d6010
passes basic script functionality
b044c7a
tofix: revised approach ensuring explicit choice of GPU device
1de6320
implicit transfer and defaultdict implementation (pub/sub eager copy …
6a26334
explicit check if GPU available on init
8d1f967
cudagpu and fix incoming re: remove eager copying/register & dev syncing
9a729e9
move conversion logic to device.py
21bac65
managing buffer duplicate
67fe76a
cleanup unnecessary todos/notes
1568315
removing notes and cleaning
2517994
fix: added copy to avoid weak reference
9e7c6ad
test: data_wo access works in context
cb5f28e
add flatten from prev logic
36d2b07
context function as global function and def state
e5a0107
fix: maintaining constant array property
39d0acd
pr review: removing unused variables
e48d698
remove dispatch to allow no-import cupy
7d582d6
pr: fix property, duplicate, init
0528e76
fix: change petsc config version to v3.25.0
e07e6c6
include cupy callable pointer
0aea6d0
maintain CPU SF comms and cond exec with cupy
6f64d83
basic GPU unit tests covering context
c8db3e2
test cases for gpu - no fixtures due to bug
7aa8425
fixed bug by amending record_modified state
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
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,97 @@ | ||
| # File to handle op3.device context manager | ||
| from abc import ABCMeta, abstractmethod | ||
| import contextlib | ||
| import contextvars | ||
| import warnings | ||
|
|
||
| import numpy as np | ||
|
|
||
| class Device(metaclass=ABCMeta): | ||
| _name: str | ||
|
SamSJackson marked this conversation as resolved.
Outdated
|
||
| _device_index: int | None | ||
|
SamSJackson marked this conversation as resolved.
Outdated
|
||
|
|
||
| def __init__(self, device_index: int | None = None): | ||
| pass | ||
|
|
||
| @property | ||
| def name(self): | ||
| return self._name | ||
|
|
||
| @property | ||
| def device_index(self): | ||
| return self._device_index | ||
|
|
||
| @abstractmethod | ||
| def asarray(self, arr): | ||
| pass | ||
|
|
||
| @abstractmethod | ||
| def zeros_like(self, arr): | ||
| pass | ||
|
|
||
| def __repr__(self): | ||
| return self._name | ||
|
|
||
| def __str__(self): | ||
| return self._name | ||
|
|
||
| class CPU(Device): | ||
|
|
||
| def __init__(self, device_index: int | None = None): | ||
| super().__init__() | ||
| self._name = "cpu" | ||
| self._registered_arrays = set() | ||
|
SamSJackson marked this conversation as resolved.
Outdated
|
||
| self._device_index = device_index | ||
|
|
||
| def asarray(self, arr): | ||
| # NOTE: Better logic needed if we switch from just NumPy/CuPy | ||
| if not isinstance(arr, np.ndarray): | ||
| import cupy as cp | ||
| return cp.asnumpy(arr) | ||
|
|
||
| return np.array(arr) | ||
|
|
||
| def zeros_like(self, arr): | ||
| return np.zeros_like(arr) | ||
|
|
||
| class CUDAGPU(Device): | ||
|
|
||
| def __init__(self, device_index: int | None = None): | ||
| super().__init__() | ||
| self._name = "CudaGPU" | ||
|
SamSJackson marked this conversation as resolved.
Outdated
|
||
| self._registered_arrays = set() | ||
|
SamSJackson marked this conversation as resolved.
Outdated
|
||
| self._token = None | ||
| self._device_index = device_index | ||
|
|
||
| try: | ||
| import cupy as cp | ||
| assert cp.is_available() | ||
| except: | ||
| # TODO: Raise No GPU exception | ||
| raise NotImplementedError | ||
|
|
||
| def asarray(self, arr): | ||
| import cupy as cp | ||
| return cp.asarray(arr) | ||
|
|
||
| def zeros_like(self, arr): | ||
| import cupy as cp | ||
| return cp.zeros_like(arr) | ||
|
|
||
| @contextlib.contextmanager | ||
| def offloading(dev: Device): | ||
| # TODO: Not Device exception | ||
| if not isinstance(dev, Device): | ||
| raise NotImplementedError | ||
|
|
||
| token = _current_device.set(dev) | ||
| try: | ||
| yield | ||
| finally: | ||
| _current_device.reset(token) | ||
|
|
||
| # NOTE: Should this const variable be here? | ||
|
SamSJackson marked this conversation as resolved.
Outdated
|
||
| HOST_DEVICE = CPU() | ||
|
|
||
| # NOTE: Use contextvars to act as a bridge between buffer and manager | ||
| _current_device = contextvars.ContextVar("current_device", default=HOST_DEVICE) | ||
|
SamSJackson marked this conversation as resolved.
Outdated
|
||
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.
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.
Uh oh!
There was an error while loading. Please reload this page.