Skip to content

Method _usm_type of Memory class should accept sycl_context #50

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

Merged
merged 4 commits into from
Sep 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
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
40 changes: 30 additions & 10 deletions dppl/_memory.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -34,19 +34,19 @@ 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:
raise ValueError("Non-positive number of bytes found.")

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*.
Expand All @@ -68,16 +68,36 @@ 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 "<Intel(R) USM allocated memory block of {} bytes at {}>" \
.format(self.nbytes, hex(<object>(<Py_ssize_t>self.memory_ptr)))

def _usm_type(self):
def _usm_type(self, context=None):
cdef const char* kind
kind = DPPLUSM_GetPointerType(self.memory_ptr,
self.context.get_context_ref())
cdef SyclContext ctx
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 = <SyclContext>(context)
kind = DPPLUSM_GetPointerType(self.memory_ptr,
ctx.get_context_ref())
elif isinstance(context, SyclQueue):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if name the variable context_or_queue ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, makes sense. Since the argument can be either context or queue. If we do not like long names, let us just call it syclobj or something other than context.

q = <SyclQueue>(context)
ctx = q.get_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')


Expand Down
18 changes: 16 additions & 2 deletions dppl/tests/dppl_tests/test_sycl_usm.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,28 @@ def test_memory_cpu_context (self):

# 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:
Expand Down