This repository was archived by the owner on Mar 1, 2023. It is now read-only.
Repository navigation
Expand file tree
/
Copy path_memory.pyx
More file actions
105 lines (80 loc) · 3.26 KB
/
Copy path_memory.pyx
File metadata and controls
105 lines (80 loc) · 3.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import dpctl
from dpctl.backend cimport *
from ._sycl_core cimport SyclContext, SyclQueue
from cpython cimport Py_buffer
cdef class Memory:
cdef DPPLSyclUSMRef memory_ptr
cdef Py_ssize_t nbytes
cdef SyclContext context
cdef _cinit(self, Py_ssize_t nbytes, ptr_type, SyclQueue queue):
cdef DPPLSyclUSMRef p
self.memory_ptr = NULL
self.nbytes = 0
self.context = None
if (nbytes > 0):
if queue is None:
queue = dpctl.get_current_queue()
if (ptr_type == "shared"):
p = DPPLmalloc_shared(nbytes, queue.get_queue_ref())
elif (ptr_type == "host"):
p = DPPLmalloc_host(nbytes, queue.get_queue_ref())
elif (ptr_type == "device"):
p = DPPLmalloc_device(nbytes, queue.get_queue_ref())
else:
raise RuntimeError("Pointer type is unknown: {}" \
.format(ptr_type))
if (p):
self.memory_ptr = p
self.nbytes = nbytes
self.context = queue.get_sycl_context()
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())
self.memory_ptr = NULL
self.nbytes = 0
self.context = None
cdef _getbuffer(self, Py_buffer *buffer, int flags):
# memory_ptr is Ref which is pointer to SYCL type. For USM it is void*.
buffer.buf = <char *>self.memory_ptr
buffer.format = 'B' # byte
buffer.internal = NULL # see References
buffer.itemsize = 1
buffer.len = self.nbytes
buffer.ndim = 1
buffer.obj = self
buffer.readonly = 0
buffer.shape = &self.nbytes
buffer.strides = &buffer.itemsize
buffer.suboffsets = NULL # for pointer arrays only
property nbytes:
def __get__(self):
return self.nbytes
property _context:
def __get__(self):
return self.context
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):
cdef const char* kind
kind = DPPLUSM_GetPointerType(self.memory_ptr,
self.context.get_context_ref())
return kind.decode('UTF-8')
cdef class MemoryUSMShared(Memory):
def __cinit__(self, Py_ssize_t nbytes, SyclQueue queue=None):
self._cinit(nbytes, "shared", queue)
def __getbuffer__(self, Py_buffer *buffer, int flags):
self._getbuffer(buffer, flags)
cdef class MemoryUSMHost(Memory):
def __cinit__(self, Py_ssize_t nbytes, SyclQueue queue=None):
self._cinit(nbytes, "host", queue)
def __getbuffer__(self, Py_buffer *buffer, int flags):
self._getbuffer(buffer, flags)
cdef class MemoryUSMDevice(Memory):
def __cinit__(self, Py_ssize_t nbytes, SyclQueue queue=None):
self._cinit(nbytes, "device", queue)