Skip to content
This repository was archived by the owner on Jan 25, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Small fixes
  • Loading branch information
AlexanderKalistratov committed Oct 2, 2020
commit eb847cda22a57ed218cb70d26c3da81eedf8d85b
11 changes: 2 additions & 9 deletions numba/core/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,15 +147,8 @@ def bar(x, y):
if 'target' in options:
>
warnings.warn("The 'target' keyword argument is deprecated.", NumbaDeprecationWarning)
elif '_target' in options:
'cpu')
else:
>

parallel_option = options.get('parallel')
if isinstance(parallel_option, dict) and parallel_option.get('offload') is True:
from numba.dppl import dppl_offload_dispatcher
>
None)

options['boundscheck'] = boundscheck

Expand Down Expand Up @@ -237,7 +230,7 @@ def __wrapper(func):
return wrapper(func, disp)

from numba.dppl.target_dispatcher import TargetDispatcher
disp = TargetDispatcher(func, wrapper, target)
disp = TargetDispatcher(func, wrapper, target, targetoptions.get('parallel'))
return disp

return __wrapper
Expand Down
45 changes: 23 additions & 22 deletions numba/dppl/dppl_offload_dispatcher.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
from numba.core import dispatcher, compiler
from numba.core.registry import cpu_target, dispatcher_registry
import numba.dppl_config as dppl_config


class DpplOffloadDispatcher(dispatcher.Dispatcher):
targetdescr = cpu_target

def __init__(self, py_func, locals={}, targetoptions={}, impl_kind='direct', pipeline_class=compiler.Compiler):
if dppl_config.dppl_present:
from numba.dppl.compiler import DPPLCompiler
targetoptions['parallel'] = True
dispatcher.Dispatcher.__init__(self, py_func, locals=locals,
targetoptions=targetoptions, impl_kind=impl_kind, pipeline_class=DPPLCompiler)
else:
print("---------------------------------------------------------------------")
print("WARNING : DPPL pipeline ignored. Ensure OpenCL drivers are installed.")
print("---------------------------------------------------------------------")
dispatcher.Dispatcher.__init__(self, py_func, locals=locals,
targetoptions=targetoptions, impl_kind=impl_kind, pipeline_class=pipeline_class)

dispatcher_registry['__dppl_offload_gpu__'] = DpplOffloadDispatcher
from numba.core import dispatcher, compiler
from numba.core.registry import cpu_target, dispatcher_registry
import numba.dppl_config as dppl_config


class DpplOffloadDispatcher(dispatcher.Dispatcher):
targetdescr = cpu_target

def __init__(self, py_func, locals={}, targetoptions={}, impl_kind='direct', pipeline_class=compiler.Compiler):
if dppl_config.dppl_present:
from numba.dppl.compiler import DPPLCompiler
targetoptions['parallel'] = True
dispatcher.Dispatcher.__init__(self, py_func, locals=locals,
targetoptions=targetoptions, impl_kind=impl_kind, pipeline_class=DPPLCompiler)
else:
print("---------------------------------------------------------------------")
print("WARNING : DPPL pipeline ignored. Ensure OpenCL drivers are installed.")
print("---------------------------------------------------------------------")
dispatcher.Dispatcher.__init__(self, py_func, locals=locals,
targetoptions=targetoptions, impl_kind=impl_kind, pipeline_class=pipeline_class)

dispatcher_registry['__dppl_offload_gpu__'] = DpplOffloadDispatcher
dispatcher_registry['__dppl_offload_cpu__'] = DpplOffloadDispatcher
36 changes: 28 additions & 8 deletions numba/dppl/target_dispatcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,24 @@
from numba import types
from numba.core.errors import UnsupportedError
import dpctl
import dpctl.ocldrv as ocldr
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):
target_offload_gpu = '__dppl_offload_gpu__'
target_offload_cpu = '__dppl_offload_cpu__'
target_dppl = 'dppl'

def __init__(self, py_func, wrapper, target, parallel_options, compiled=None):

self.__py_func = py_func
self.__target = target
self.__wrapper = wrapper
self.__compiled = compiled if compiled is not None else {}
self.__parallel = parallel_options
self.__doc__ = py_func.__doc__
self.__name__ = py_func.__name__
self.__module__ = py_func.__module__
Expand All @@ -31,8 +37,8 @@ def __repr__(self):
return self.get_compiled().__repr__()

@classmethod
def _rebuild(cls, py_func, wrapper, target, compiled):
self = cls(py_func, wrapper, target, compiled)
def _rebuild(cls, py_func, wrapper, target, parallel, compiled):
self = cls(py_func, wrapper, target, parallel, compiled)
return self

def get_compiled(self,>
Expand All @@ -47,15 +53,28 @@ def get_compiled(self,>

return self.__compiled[disp]

def __is_with_context_target(target):
return target is None or TargetDispatcher.target_dppl

def get_current_disp(self):
>
parallel = self.__parallel

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 not self.__is_with_context_target(target):
raise UnsupportedError(f"Can't use 'with' context with explicitly specified target '{target}'")
if parallel is False or (isinstance(parallel, dict) and parallel.get('offload') is False):
raise UnsupportedError(f"Can't use 'with' context with parallel option '{parallel}'")

from numba.dppl import dppl_offload_dispatcher

if target is None:
if dpctl.get_current_device_type() == dpctl.device_type.gpu:
return registry.dispatcher_registry[TargetDispatcher.target_offload_gpu]
elif dpctl.get_current_device_type() == dpctl.device_type.cpu:
return registry.dispatcher_registry[TargetDispatcher.target_offload_cpu]
else:
raise UnsupportedError('Unknown dppl device type')

if target is None:
>
Expand All @@ -67,5 +86,6 @@ def _reduce_states(self):
py_func=self.__py_func,
wrapper=self.__wrapper,
>
parallel=self.__parallel,
compiled=self.__compiled
)
29 changes: 21 additions & 8 deletions numba/dppl/tests/dppl/test_with_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
import dpctl


@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):

@unittest.skipIf(not dpctl.has_gpu_queues(), "No GPU platforms available")
def test_with_dppl_context_gpu(self):

@njit
Expand All @@ -34,9 +34,9 @@ def func(b):
np.testing.assert_array_equal(expected, got_gpu)
self.assertTrue('Parfor lowered on DPPL-device' in got_gpu_message.getvalue())


@unittest.skipIf(not dpctl.has_cpu_queues(), "No CPU platforms available")
def test_with_dppl_context_cpu(self):

@njit
def nested_func(a, b):
np.sin(a, b)
Expand All @@ -60,6 +60,7 @@ def func(b):
self.assertTrue('Parfor lowered on DPPL-device' not in got_cpu_message.getvalue())


@unittest.skipIf(not dpctl.has_gpu_queues(), "No GPU platforms available")
def test_with_dppl_context_target(self):

@njit(target='cpu')
Expand All @@ -76,6 +77,12 @@ def func_no_target(b):
a = np.ones((64), dtype=np.float64)
nested_func_target(a, b)

@njit(parallel=False)
def func_no_parallel(b):
a = np.ones((64), dtype=np.float64)
return a


a = np.ones((64), dtype=np.float64)
b = np.ones((64), dtype=np.float64)

Expand All @@ -91,10 +98,16 @@ def func_no_target(b):
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))
with self.assertRaises(errors.UnsupportedError) as raises_4:
with dpctl.device_context(dpctl.device_type.gpu):
func_no_target(a)

msg_1 = "Can't use 'with' context with explicitly specified target"
msg_2 = "Can't use 'with' context with parallel option"
self.assertTrue(msg_1 in str(raises_1.exception))
self.assertTrue(msg_1 in str(raises_2.exception))
self.assertTrue(msg_1 in str(raises_3.exception))
self.assertTrue(msg_2 in str(raises_4.exception))


if __name__ == '__main__':
Expand Down