-
Notifications
You must be signed in to change notification settings - Fork 0
/
uni_ptr.py
255 lines (197 loc) · 6.63 KB
/
uni_ptr.py
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
# -*- coding: utf-8 -*-
__all__ = [
"Unified_Ptr",
]
from ctypes import cast, c_void_p
from functools import reduce
from operator import mul
import numpy as np
import warnings
from cuda_helpers import (cu_conj,
cu_free,
cu_iadd_val,
cu_iadd_vec,
cu_idiv_val,
cu_idiv_vec,
cu_imul_val,
cu_imul_vec,
cu_isub_val,
cu_isub_vec,
cu_malloc_managed,
cu_memcpy_d2d,
cu_memcpy_d2h,
cu_memcpy_h2d,
cu_memset,
cu_memcpy_d2d_async,
cu_memcpy_d2h_async,
cu_memcpy_h2d_async,
cu_memset_async,
cu_transpose)
dtype_map={np.dtype('f4') :0,
np.dtype('f8') :1,
np.dtype('c8') :2,
np.dtype('c16'):3}
c2f_map={np.dtype('f4') : 0,
np.dtype('f8') : 1,
np.dtype('c8') : 0,
np.dtype('c16'): 1}
def check_contiguous(arr):
if not arr.flags['C_CONTIGUOUS'] and not arr.flags['F_CONTIGUOUS']:
warnings.warn("Non-contiguous host memory detected, unexpected behavior/results may occur.")
class Unified_Ptr(object):
def __init__(self, shape, dtype, stream=None, fill=None):
"""
Allocates device memory, holds important information,
and provides useful operations.
Parameters
----------
shape : tuple
The shape of the array to allocate.
dtype : np.dtype
That data type of the array.
stream : c_void_p
CUDA stream to associate the returned object with.
fill : scalar, np.ndarray, or Device_Ptr, optional
Default value to fill in allocated memory space. If
None, then the memory is allocated with zeros.
"""
self.shape = shape
self.dtype = np.dtype(dtype)
self.stream = stream
try:
self.size = reduce(mul,shape)
except:
self.size = int(shape)
self.nbytes = self.size*self.dtype.itemsize
dev_ptr = cu_malloc_managed(self.nbytes)
self.ptr = cast(dev_ptr, c_void_p)
self.h = np.ctypeslib.as_array(cast(dev_ptr,
np.ctypeslib.ndpointer(dtype,
shape=shape,
flags='C')))
if fill is not None:
if isinstance(fill, (int, float, complex)):
self.h[:] = fill
elif type(fill) in [list,tuple]:
self.h[:] = fill
elif type(fill) == np.ndarray:
self.h[:] = fill.reshape(self.shape)
elif type(fill) == type(self):
self.d2d(src=fill, dst=self)
else:
raise TypeError('Unsupported fill value or type input')
def __call__(self):
return self.ptr
def __len__(self):
return self.size
def __repr__(self):
return repr(self.__dict__)
def __getitem__(self, slice):
return self.h[slice]
def __setitem__(self, slice, b):
self.h[slice] = b
def __iadd__(self, b):
"""
Perform in-place element-wise addition.
Parameters
----------
b : np.ndarray or scalar
Value(s) to add by
Returns
-------
self : Device_Ptr
Returns self with updated values in self.ptr
"""
self.h += b
return self
def __imul__(self, b):
"""
Perform in-place element-wise multiplication.
Parameters
----------
b : np.ndarray or scalar
Value(s) to multiply by
Returns
-------
self : Device_Ptr
Returns self with updated values in self.ptr
"""
self.h *= b
return self
def __isub__(self, b):
"""
Perform in-place element-wise subtraction.
Parameters
----------
b : np.ndarray or scalar
Value(s) to subtract by
Returns
-------
self : Device_Ptr
Returns self with updated values in self.ptr
"""
self.h -= b
return self
def __itruediv__(self, b):
"""
Perform in-place element-wise division.
Parameters
----------
b : np.ndarray or scalar
Value(s) to divide by
Returns
-------
self : Device_Ptr
Returns self with updated values in self.ptr
"""
self.h /= b
return self
def T(self, stream=None):
"""
Transpose the matrix on the device. This currently only works
with square matrices. Rectangular will be implemented in a
future update.
"""
self.h = self.h.T
self.shape = self.h.shape
return self
def conj(self, inplace=True, stream=None):
"""
Take and return the complex conjugate.
"""
self.h = self.h.conj
return self
@classmethod
def d2d(self, src, dst, nbytes=None):
"""
Copy memory from 'device to device'. This works both
for copying memory to a separate device, or for creating a
copy of memory on the same device.
Parameters
----------
src : Device_Ptr
Device_Ptr object containing the src ptr.
dst : Device_Ptr
Device_Ptr object containing the dst ptr.
nbytes : int
Size to copy/transfer in bytes.
"""
nbytes = min([src.nbytes, nbytes or src.nbytes])
if nbytes > dst.nbytes:
raise ValueError('Attempted to copy a src with size greater than dst.')
cu_memcpy_d2d(src.ptr, dst.ptr, nbytes)
@property
def dtype_depth(self):
if self.dtype in ['f4', 'f8']:
return 1
if self.dtype in ['c8', 'c16']:
return 2
def __enter__(self):
return self
def __exit__(self, *args, **kwargs):
"""
Frees the memory used by the object, and then
deletes the object.
"""
cu_free(self.ptr)
del self