This repository was archived by the owner on Jan 25, 2023. It is now read-only.
Repository navigation
Semantics "with context" #57
Merged
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
Next
Next commit
Add semantics 'with context' for gpu and cpu
- Loading branch information
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,35 @@ | ||
| import numpy as np | ||
| from numba import dppl, njit, prange | ||
| import dpctl | ||
| import dpctl.ocldrv as ocldrv | ||
|
|
||
|
|
||
| @njit | ||
| def g(a): | ||
| return a + 1 | ||
|
|
||
|
|
||
| @njit | ||
| def f(a, b, c, N): | ||
| for i in prange(N): | ||
| a[i] = b[i] + g(c[i]) | ||
|
|
||
|
|
||
| def main(): | ||
| N = 10 | ||
| a = np.ones(N) | ||
| b = np.ones(N) | ||
| c = np.ones(N) | ||
|
|
||
| if ocldrv.has_gpu_device: | ||
| with dpctl.device_context(dpctl.device_type.gpu): | ||
| f(a, b, c, N) | ||
| elif ocldrv.has_cpu_device: | ||
| with dpctl.device_context(dpctl.device_type.cpu): | ||
| f(a, b, c, N) | ||
| else: | ||
| print("No device found") | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| main() |
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,72 @@ | ||
| from numba.core import registry, serialize, dispatcher | ||
| from numba import types | ||
| from numba.core.errors import UnsupportedError | ||
| import dpctl | ||
| import dpctl.ocldrv as ocldrv | ||
| from numba.core.compiler_lock import global_compiler_lock | ||
|
|
||
|
|
||
| class TargetDispatcher(serialize.ReduceMixin, metaclass=dispatcher.DispatcherMeta): | ||
| __numba__ = 'py_func' | ||
|
|
||
| def __init__(self, py_func, wrapper, target, compiled=None): | ||
|
|
||
| self.__py_func = py_func | ||
| self.__target = target | ||
| self.__wrapper = wrapper | ||
| self.__compiled = compiled if compiled is not None else {} | ||
| self.__doc__ = py_func.__doc__ | ||
| self.__name__ = py_func.__name__ | ||
| self.__module__ = py_func.__module__ | ||
|
|
||
| def __call__(self, *args, **kwargs): | ||
| return self.get_compiled()(*args, **kwargs) | ||
|
|
||
| def __getattr__(self, name): | ||
| return getattr(self.get_compiled(), name) | ||
|
|
||
| def __get__(self, obj, objtype=None): | ||
| return self.get_compiled().__get__(obj, objtype) | ||
|
|
||
| def __repr__(self): | ||
| return self.get_compiled().__repr__() | ||
|
|
||
| @classmethod | ||
| def _rebuild(cls, py_func, wrapper, target, compiled): | ||
| self = cls(py_func, wrapper, target, compiled) | ||
| return self | ||
|
|
||
| def get_compiled(self,> | ||
| if target is None: | ||
| > | ||
|
|
||
| disp = self.get_current_disp() | ||
| if not disp in self.__compiled.keys(): | ||
| with global_compiler_lock: | ||
| if not disp in self.__compiled.keys(): | ||
| self.__compiled[disp] = self.__wrapper(self.__py_func, disp) | ||
|
|
||
| return self.__compiled[disp] | ||
|
|
||
| def get_current_disp(self): | ||
| > | ||
|
|
||
| if dpctl.is_in_device_context(): | ||
| if self.__target is not None: | ||
| raise UnsupportedError("Unsupported defined 'target' with using context device") | ||
| if dpctl.get_current_device_type() == dpctl.device_type.gpu: | ||
| from numba.dppl import dppl_offload_dispatcher | ||
| return registry.dispatcher_registry['__dppl_offload_gpu__'] | ||
|
|
||
| if target is None: | ||
| > | ||
|
|
||
| return registry.dispatcher_registry[target] | ||
|
|
||
| def _reduce_states(self): | ||
| return dict( | ||
| py_func=self.__py_func, | ||
| wrapper=self.__wrapper, | ||
| > | ||
| compiled=self.__compiled | ||
| ) |
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,102 @@ | ||
| import numba | ||
| import numpy as np | ||
| from numba import dppl, njit | ||
| from numba.core import errors | ||
| from numba.tests.support import captured_stdout | ||
| from numba.dppl.testing import DPPLTestCase, unittest | ||
| import dpctl | ||
| import dpctl.ocldrv as ocldrv | ||
|
|
||
|
|
||
| @unittest.skipIf(not dpctl.has_gpu_queues(), "No GPU platforms available") | ||
| @unittest.skipIf(not dpctl.has_cpu_queues(), "No CPU platforms available") | ||
| class TestWithDPPLContext(DPPLTestCase): | ||
| def test_with_dppl_context_gpu(self): | ||
|
|
||
| @njit | ||
| def nested_func(a, b): | ||
| np.sin(a, b) | ||
|
|
||
| @njit | ||
| def func(b): | ||
| a = np.ones((64), dtype=np.float64) | ||
| nested_func(a, b) | ||
|
|
||
| numba.dppl.compiler.DEBUG = 1 | ||
| expected = np.ones((64), dtype=np.float64) | ||
| got_gpu = np.ones((64), dtype=np.float64) | ||
|
|
||
| with captured_stdout() as got_gpu_message: | ||
| with dpctl.device_context(dpctl.device_type.gpu): | ||
| func(got_gpu) | ||
|
|
||
| func(expected) | ||
|
|
||
| np.testing.assert_array_equal(expected, got_gpu) | ||
| self.assertTrue('Parfor lowered on DPPL-device' in got_gpu_message.getvalue()) | ||
|
|
||
|
|
||
| def test_with_dppl_context_cpu(self): | ||
|
|
||
| @njit | ||
| def nested_func(a, b): | ||
| np.sin(a, b) | ||
|
|
||
| @njit | ||
| def func(b): | ||
| a = np.ones((64), dtype=np.float64) | ||
| nested_func(a, b) | ||
|
|
||
| numba.dppl.compiler.DEBUG = 1 | ||
| expected = np.ones((64), dtype=np.float64) | ||
| got_cpu = np.ones((64), dtype=np.float64) | ||
|
|
||
| with captured_stdout() as got_cpu_message: | ||
| with dpctl.device_context(dpctl.device_type.cpu): | ||
| func(got_cpu) | ||
|
|
||
| func(expected) | ||
|
|
||
| np.testing.assert_array_equal(expected, got_cpu) | ||
| self.assertTrue('Parfor lowered on DPPL-device' not in got_cpu_message.getvalue()) | ||
|
|
||
|
|
||
| def test_with_dppl_context_target(self): | ||
|
|
||
| @njit(target='cpu') | ||
| def nested_func_target(a, b): | ||
| np.sin(a, b) | ||
|
|
||
| @njit(target='gpu') | ||
| def func_target(b): | ||
| a = np.ones((64), dtype=np.float64) | ||
| nested_func_target(a, b) | ||
|
|
||
| @njit | ||
| def func_no_target(b): | ||
| a = np.ones((64), dtype=np.float64) | ||
| nested_func_target(a, b) | ||
|
|
||
| a = np.ones((64), dtype=np.float64) | ||
| b = np.ones((64), dtype=np.float64) | ||
|
|
||
| with self.assertRaises(errors.UnsupportedError) as raises_1: | ||
| with dpctl.device_context(dpctl.device_type.gpu): | ||
| nested_func_target(a, b) | ||
|
|
||
| with self.assertRaises(errors.UnsupportedError) as raises_2: | ||
| with dpctl.device_context(dpctl.device_type.gpu): | ||
| func_target(a) | ||
|
|
||
| with self.assertRaises(errors.UnsupportedError) as raises_3: | ||
| with dpctl.device_context(dpctl.device_type.gpu): | ||
| func_no_target(a) | ||
|
|
||
| msg = "Unsupported defined 'target' with using context device" | ||
| self.assertTrue(msg in str(raises_1.exception)) | ||
| self.assertTrue(msg in str(raises_2.exception)) | ||
| self.assertTrue(msg in str(raises_3.exception)) | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| unittest.main() | ||
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
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.
ocldrv