|
| 1 | +import dppl |
| 2 | +from dppl.backend cimport * |
| 3 | +from ._sycl_core cimport SyclContext, SyclQueue |
| 4 | + |
| 5 | +from cpython cimport Py_buffer |
| 6 | + |
| 7 | + |
| 8 | +cdef class Memory: |
| 9 | + cdef DPPLSyclUSMRef memory_ptr |
| 10 | + cdef Py_ssize_t nbytes |
| 11 | + cdef SyclContext context |
| 12 | + |
| 13 | + cdef _cinit(self, Py_ssize_t nbytes, ptr_type, SyclQueue queue): |
| 14 | + cdef DPPLSyclUSMRef p |
| 15 | + |
| 16 | + self.memory_ptr = NULL |
| 17 | + self.nbytes = 0 |
| 18 | + self.context = None |
| 19 | + |
| 20 | + if (nbytes > 0): |
| 21 | + if queue is None: |
| 22 | + queue = dppl.get_current_queue() |
| 23 | + |
| 24 | + if (ptr_type == "shared"): |
| 25 | + p = DPPLmalloc_shared(nbytes, queue.get_queue_ref()) |
| 26 | + elif (ptr_type == "host"): |
| 27 | + p = DPPLmalloc_host(nbytes, queue.get_queue_ref()) |
| 28 | + elif (ptr_type == "device"): |
| 29 | + p = DPPLmalloc_device(nbytes, queue.get_queue_ref()) |
| 30 | + else: |
| 31 | + raise RuntimeError("Pointer type is unknown: {}" \ |
| 32 | + .format(ptr_type)) |
| 33 | + |
| 34 | + if (p): |
| 35 | + self.memory_ptr = p |
| 36 | + self.nbytes = nbytes |
| 37 | + self.context = queue.get_sycl_context() |
| 38 | + else: |
| 39 | + raise RuntimeError("Null memory pointer returned") |
| 40 | + else: |
| 41 | + raise ValueError("Non-positive number of bytes found.") |
| 42 | + |
| 43 | + def __dealloc__(self): |
| 44 | + if (self.memory_ptr): |
| 45 | + DPPLfree_with_context(self.memory_ptr, |
| 46 | + self.context.get_context_ref()) |
| 47 | + self.memory_ptr = NULL |
| 48 | + self.nbytes = 0 |
| 49 | + self.context = None |
| 50 | + |
| 51 | + cdef _getbuffer(self, Py_buffer *buffer, int flags): |
| 52 | + # memory_ptr is Ref which is pointer to SYCL type. For USM it is void*. |
| 53 | + buffer.buf = <char *>self.memory_ptr |
| 54 | + buffer.format = 'B' # byte |
| 55 | + buffer.internal = NULL # see References |
| 56 | + buffer.itemsize = 1 |
| 57 | + buffer.len = self.nbytes |
| 58 | + buffer.ndim = 1 |
| 59 | + buffer.obj = self |
| 60 | + buffer.readonly = 0 |
| 61 | + buffer.shape = &self.nbytes |
| 62 | + buffer.strides = &buffer.itemsize |
| 63 | + buffer.suboffsets = NULL # for pointer arrays only |
| 64 | + |
| 65 | + property nbytes: |
| 66 | + def __get__(self): |
| 67 | + return self.nbytes |
| 68 | + |
| 69 | + property _context: |
| 70 | + def __get__(self): |
| 71 | + return self.context |
| 72 | + |
| 73 | + def __repr__(self): |
| 74 | + return "<Intel(R) USM allocated memory block of {} bytes at {}>" \ |
| 75 | + .format(self.nbytes, hex(<object>(<Py_ssize_t>self.memory_ptr))) |
| 76 | + |
| 77 | + def _usm_type(self): |
| 78 | + cdef const char* kind |
| 79 | + kind = DPPLUSM_GetPointerType(self.memory_ptr, |
| 80 | + self.context.get_context_ref()) |
| 81 | + return kind.decode('UTF-8') |
| 82 | + |
| 83 | + |
| 84 | +cdef class MemoryUSMShared(Memory): |
| 85 | + |
| 86 | + def __cinit__(self, Py_ssize_t nbytes, SyclQueue queue=None): |
| 87 | + self._cinit(nbytes, "shared", queue) |
| 88 | + |
| 89 | + def __getbuffer__(self, Py_buffer *buffer, int flags): |
| 90 | + self._getbuffer(buffer, flags) |
| 91 | + |
| 92 | + |
| 93 | +cdef class MemoryUSMHost(Memory): |
| 94 | + |
| 95 | + def __cinit__(self, Py_ssize_t nbytes, SyclQueue queue=None): |
| 96 | + self._cinit(nbytes, "host", queue) |
| 97 | + |
| 98 | + def __getbuffer__(self, Py_buffer *buffer, int flags): |
| 99 | + self._getbuffer(buffer, flags) |
| 100 | + |
| 101 | + |
| 102 | +cdef class MemoryUSMDevice(Memory): |
| 103 | + |
| 104 | + def __cinit__(self, Py_ssize_t nbytes, SyclQueue queue=None): |
| 105 | + self._cinit(nbytes, "device", queue) |
0 commit comments