Skip to content

Commit 4aae1da

Browse files
Merge pull request #50 from IntelPython/memory-usm-type
Method _usm_type of Memory class should accept sycl_context
2 parents c9b2f94 + 704090b commit 4aae1da

File tree

2 files changed

+46
-12
lines changed

2 files changed

+46
-12
lines changed

dppl/_memory.pyx

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@ from cpython cimport Py_buffer
88
cdef class Memory:
99
cdef DPPLSyclUSMRef memory_ptr
1010
cdef Py_ssize_t nbytes
11-
cdef SyclContext context
11+
cdef SyclQueue queue
1212

1313
cdef _cinit(self, Py_ssize_t nbytes, ptr_type, SyclQueue queue):
1414
cdef DPPLSyclUSMRef p
1515

1616
self.memory_ptr = NULL
1717
self.nbytes = 0
18-
self.context = None
18+
self.queue = None
1919

2020
if (nbytes > 0):
2121
if queue is None:
@@ -34,19 +34,19 @@ cdef class Memory:
3434
if (p):
3535
self.memory_ptr = p
3636
self.nbytes = nbytes
37-
self.context = queue.get_sycl_context()
37+
self.queue = queue
3838
else:
3939
raise RuntimeError("Null memory pointer returned")
4040
else:
4141
raise ValueError("Non-positive number of bytes found.")
4242

4343
def __dealloc__(self):
4444
if (self.memory_ptr):
45-
DPPLfree_with_context(self.memory_ptr,
46-
self.context.get_context_ref())
45+
DPPLfree_with_queue(self.memory_ptr,
46+
self.queue.get_queue_ref())
4747
self.memory_ptr = NULL
4848
self.nbytes = 0
49-
self.context = None
49+
self.queue = None
5050

5151
cdef _getbuffer(self, Py_buffer *buffer, int flags):
5252
# memory_ptr is Ref which is pointer to SYCL type. For USM it is void*.
@@ -68,16 +68,36 @@ cdef class Memory:
6868

6969
property _context:
7070
def __get__(self):
71-
return self.context
71+
return self.queue.get_sycl_context()
72+
73+
property _queue:
74+
def __get__(self):
75+
return self.queue
7276

7377
def __repr__(self):
7478
return "<Intel(R) USM allocated memory block of {} bytes at {}>" \
7579
.format(self.nbytes, hex(<object>(<Py_ssize_t>self.memory_ptr)))
7680

77-
def _usm_type(self):
81+
def _usm_type(self, context=None):
7882
cdef const char* kind
79-
kind = DPPLUSM_GetPointerType(self.memory_ptr,
80-
self.context.get_context_ref())
83+
cdef SyclContext ctx
84+
cdef SyclQueue q
85+
if context is None:
86+
ctx = self._context
87+
kind = DPPLUSM_GetPointerType(self.memory_ptr,
88+
ctx.get_context_ref())
89+
elif isinstance(context, SyclContext):
90+
ctx = <SyclContext>(context)
91+
kind = DPPLUSM_GetPointerType(self.memory_ptr,
92+
ctx.get_context_ref())
93+
elif isinstance(context, SyclQueue):
94+
q = <SyclQueue>(context)
95+
ctx = q.get_sycl_context()
96+
kind = DPPLUSM_GetPointerType(self.memory_ptr,
97+
ctx.get_context_ref())
98+
else:
99+
raise ValueError("sycl_context keyword can be either None, "
100+
"or an instance of dppl.SyclConext")
81101
return kind.decode('UTF-8')
82102

83103

dppl/tests/dppl_tests/test_sycl_usm.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,28 @@ def test_memory_cpu_context (self):
4848

4949
# CPU context
5050
with dppl.device_context(dppl.device_type.cpu):
51-
self.assertEqual(mobj._usm_type(), 'shared')
51+
# type respective to the context in which
52+
# memory was created
53+
usm_type = mobj._usm_type()
54+
self.assertEqual(usm_type, 'shared')
55+
56+
current_queue = dppl.get_current_queue()
57+
# type as view from current queue
58+
usm_type = mobj._usm_type(context=current_queue)
59+
# type can be unknown if current queue is
60+
# not in the same SYCL context
61+
self.assertTrue(usm_type in ['unknown', 'shared'])
5262

5363
def test_memory_gpu_context (self):
5464
mobj = self._create_memory()
5565

5666
# GPU context
5767
with dppl.device_context(dppl.device_type.gpu):
58-
self.assertEqual(mobj._usm_type(), 'shared')
68+
usm_type = mobj._usm_type()
69+
self.assertEqual(usm_type, 'shared')
70+
current_queue = dppl.get_current_queue()
71+
usm_type = mobj._usm_type(context=current_queue)
72+
self.assertTrue(usm_type in ['unknown', 'shared'])
5973

6074

6175
class TestMemoryUSMBase:

0 commit comments

Comments
 (0)