From 26b2a8d7198567e8d3cf189e1cc8f3af28e5e7ca Mon Sep 17 00:00:00 2001 From: Oleksandr Pavlyk Date: Mon, 21 Sep 2020 11:20:36 -0500 Subject: [PATCH 1/4] Method _usm_type of Memory class should accept sycl_context --- dppl/_memory.pyx | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/dppl/_memory.pyx b/dppl/_memory.pyx index 22b9329073..5db7113172 100644 --- a/dppl/_memory.pyx +++ b/dppl/_memory.pyx @@ -74,10 +74,20 @@ cdef class Memory: return "" \ .format(self.nbytes, hex((self.memory_ptr))) - def _usm_type(self): + def _usm_type(self, sycl_context=None): cdef const char* kind - kind = DPPLUSM_GetPointerType(self.memory_ptr, - self.context.get_context_ref()) + cdef SyclContext ctx + if sycl_context is None: + ctx = self.context + kind = DPPLUSM_GetPointerType(self.memory_ptr, + ctx.get_context_ref()) + elif isinstance(sycl_context, SyclContext): + ctx = (sycl_context) + kind = DPPLUSM_GetPointerType(self.memory_ptr, + ctx.get_context_ref()) + else: + raise ValueError("sycl_context keyword can be either None, " + "or an instance of dppl.SyclConext") return kind.decode('UTF-8') From c0da58b064feb6111fffdcba7fbed659fec478c8 Mon Sep 17 00:00:00 2001 From: Oleksandr Pavlyk Date: Mon, 21 Sep 2020 13:52:51 -0500 Subject: [PATCH 2/4] Memory class stores SyclQueue inside to enable copying to host --- dppl/_memory.pyx | 34 ++++++++++++++++++++++------------ 1 file changed, 22 insertions(+), 12 deletions(-) diff --git a/dppl/_memory.pyx b/dppl/_memory.pyx index 5db7113172..6b14164f43 100644 --- a/dppl/_memory.pyx +++ b/dppl/_memory.pyx @@ -8,14 +8,14 @@ from cpython cimport Py_buffer cdef class Memory: cdef DPPLSyclUSMRef memory_ptr cdef Py_ssize_t nbytes - cdef SyclContext context + cdef SyclQueue queue cdef _cinit(self, Py_ssize_t nbytes, ptr_type, SyclQueue queue): cdef DPPLSyclUSMRef p self.memory_ptr = NULL self.nbytes = 0 - self.context = None + self.queue = None if (nbytes > 0): if queue is None: @@ -34,7 +34,7 @@ cdef class Memory: if (p): self.memory_ptr = p self.nbytes = nbytes - self.context = queue.get_sycl_context() + self.queue = queue else: raise RuntimeError("Null memory pointer returned") else: @@ -42,11 +42,11 @@ cdef class Memory: def __dealloc__(self): if (self.memory_ptr): - DPPLfree_with_context(self.memory_ptr, - self.context.get_context_ref()) + DPPLfree_with_queue(self.memory_ptr, + self.queue.get_queue_ref()) self.memory_ptr = NULL self.nbytes = 0 - self.context = None + self.queue = None cdef _getbuffer(self, Py_buffer *buffer, int flags): # memory_ptr is Ref which is pointer to SYCL type. For USM it is void*. @@ -68,21 +68,31 @@ cdef class Memory: property _context: def __get__(self): - return self.context + return self.queue.get_sycl_context() + + property _queue: + def __get__(self): + return self.queue def __repr__(self): return "" \ .format(self.nbytes, hex((self.memory_ptr))) - def _usm_type(self, sycl_context=None): + def _usm_type(self, context=None): cdef const char* kind cdef SyclContext ctx - if sycl_context is None: - ctx = self.context + cdef SyclQueue q + if context is None: + ctx = self._context + kind = DPPLUSM_GetPointerType(self.memory_ptr, + ctx.get_context_ref()) + elif isinstance(context, SyclContext): + ctx = (context) kind = DPPLUSM_GetPointerType(self.memory_ptr, ctx.get_context_ref()) - elif isinstance(sycl_context, SyclContext): - ctx = (sycl_context) + elif isinstance(context, SyclQueue): + q = (context) + ctx = q.get_sycl_context() kind = DPPLUSM_GetPointerType(self.memory_ptr, ctx.get_context_ref()) else: From 32effa72acd263da9dd551b1fbfc73aa387717a8 Mon Sep 17 00:00:00 2001 From: Oleksandr Pavlyk Date: Mon, 21 Sep 2020 16:31:03 -0500 Subject: [PATCH 3/4] adjusted tests per PR feedback --- dppl/tests/dppl_tests/test_sycl_usm.py | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/dppl/tests/dppl_tests/test_sycl_usm.py b/dppl/tests/dppl_tests/test_sycl_usm.py index 15bb397a9f..e39e720ea3 100644 --- a/dppl/tests/dppl_tests/test_sycl_usm.py +++ b/dppl/tests/dppl_tests/test_sycl_usm.py @@ -46,16 +46,30 @@ def test_memory_without_context (self): def test_memory_cpu_context (self): mobj = self._create_memory() - # CPU context + # CPU context with dppl.device_context(dppl.device_type.cpu): - self.assertEqual(mobj._usm_type(), 'shared') + # type respective to the context in which + # memory was created + usm_type = mobj._usm_type() + self.assertEqual(usm_type, 'shared') + + current_queue = dppl.get_current_queue() + # type as view from current queue + usm_type = mobj._usm_type(context=current_queue) + # type can be unknown if current queue is + # not in the same SYCL context + self.assertTrue(usm_type in ['unknown', 'shared']) def test_memory_gpu_context (self): mobj = self._create_memory() # GPU context with dppl.device_context(dppl.device_type.gpu): - self.assertEqual(mobj._usm_type(), 'shared') + usm_type = mobj._usm_type() + self.assertEqual(usm_type, 'shared') + current_queue = dppl.get_current_queue() + usm_type = mobj._usm_type(context=current_queue) + self.assertTrue(usm_type in ['unknown', 'shared']) class TestMemoryUSMBase: From 704090b5d322401ab8c97b4f6f4966fdffeac39f Mon Sep 17 00:00:00 2001 From: Oleksandr Pavlyk Date: Mon, 21 Sep 2020 16:55:44 -0500 Subject: [PATCH 4/4] superfluous whitespaces be banished --- dppl/tests/dppl_tests/test_sycl_usm.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dppl/tests/dppl_tests/test_sycl_usm.py b/dppl/tests/dppl_tests/test_sycl_usm.py index e39e720ea3..94b14c50fb 100644 --- a/dppl/tests/dppl_tests/test_sycl_usm.py +++ b/dppl/tests/dppl_tests/test_sycl_usm.py @@ -46,7 +46,7 @@ def test_memory_without_context (self): def test_memory_cpu_context (self): mobj = self._create_memory() - # CPU context + # CPU context with dppl.device_context(dppl.device_type.cpu): # type respective to the context in which # memory was created