harmonize-python 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- harmonize/__init__.py +65 -0
- harmonize/__main__.py +39 -0
- harmonize/adapt.py +105 -0
- harmonize/array.py +588 -0
- harmonize/atomics.py +206 -0
- harmonize/codegen.py +409 -0
- harmonize/config.py +220 -0
- harmonize/cpp/async/future.h +64 -0
- harmonize/cpp/async/mod.h +9 -0
- harmonize/cpp/async/promise.h +747 -0
- harmonize/cpp/harmonize.h +4 -0
- harmonize/cpp/mem/mod.h +14 -0
- harmonize/cpp/mem/work.h +168 -0
- harmonize/cpp/preamble/adapt.h +162 -0
- harmonize/cpp/preamble/mod.h +133 -0
- harmonize/cpp/program/async.h +2923 -0
- harmonize/cpp/program/event.h +560 -0
- harmonize/cpp/program/mod.h +84 -0
- harmonize/cpp/sync/barrier.h +704 -0
- harmonize/cpp/sync/mod.h +9 -0
- harmonize/cpp/util/cli.h +510 -0
- harmonize/cpp/util/host.h +287 -0
- harmonize/cpp/util/iter.h +565 -0
- harmonize/cpp/util/mem.h +1050 -0
- harmonize/cpp/util/mod.h +148 -0
- harmonize/diagnostics.py +487 -0
- harmonize/errors.py +12 -0
- harmonize/logging.py +51 -0
- harmonize/pointer.py +108 -0
- harmonize/prim.py +59 -0
- harmonize/printing.py +104 -0
- harmonize/python/__init__.py +48 -0
- harmonize/runtime.py +1501 -0
- harmonize/templates.py +369 -0
- harmonize/timing.py +10 -0
- harmonize/types.py +16 -0
- harmonize_python-0.1.0.dist-info/METADATA +161 -0
- harmonize_python-0.1.0.dist-info/RECORD +40 -0
- harmonize_python-0.1.0.dist-info/WHEEL +4 -0
- harmonize_python-0.1.0.dist-info/licenses/LICENSE +29 -0
harmonize/array.py
ADDED
|
@@ -0,0 +1,588 @@
|
|
|
1
|
+
import numba as nb
|
|
2
|
+
import numpy as np
|
|
3
|
+
|
|
4
|
+
from numba import types
|
|
5
|
+
|
|
6
|
+
from harmonize import config
|
|
7
|
+
from functools import reduce
|
|
8
|
+
import operator
|
|
9
|
+
|
|
10
|
+
import cffi
|
|
11
|
+
ffi = cffi.FFI()
|
|
12
|
+
|
|
13
|
+
from .pointer import *
|
|
14
|
+
from .templates import *
|
|
15
|
+
from .codegen import generate_uuid
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def local_array(shape,dtype):
|
|
19
|
+
return np.empty(shape,dtype=dtype)
|
|
20
|
+
|
|
21
|
+
@nb.extending.type_callable(local_array)
|
|
22
|
+
def type_local_array(context):
|
|
23
|
+
|
|
24
|
+
from numba.core.typing.npydecl import parse_dtype, parse_shape
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
if isinstance(context,nb.core.typing.context.Context):
|
|
28
|
+
|
|
29
|
+
# Function repurposed from numba's ol_np_empty.
|
|
30
|
+
def typer(shape, dtype):
|
|
31
|
+
nb.np.arrayobj._check_const_str_dtype("empty", dtype)
|
|
32
|
+
|
|
33
|
+
# Only integer literals and tuples of integer literals are valid
|
|
34
|
+
# shapes
|
|
35
|
+
if isinstance(shape, nb.types.Integer):
|
|
36
|
+
if not isinstance(shape, nb.types.IntegerLiteral):
|
|
37
|
+
raise nb.core.errors.UnsupportedError(f"Integer shape type {shape} is not literal.")
|
|
38
|
+
elif isinstance(shape, (nb.types.Tuple, nb.types.UniTuple)):
|
|
39
|
+
if any([not isinstance(s, nb.types.IntegerLiteral)
|
|
40
|
+
for s in shape]):
|
|
41
|
+
raise nb.core.errors.UnsupportedError(
|
|
42
|
+
f"At least one element of shape tuple type{shape} is not an integer literal."
|
|
43
|
+
)
|
|
44
|
+
else:
|
|
45
|
+
raise nb.core.errors.UnsupportedError(f"Shape is of unsupported type {shape}.")
|
|
46
|
+
|
|
47
|
+
# No default arguments.
|
|
48
|
+
nb_dtype = parse_dtype(dtype)
|
|
49
|
+
nb_shape = parse_shape(shape)
|
|
50
|
+
|
|
51
|
+
if nb_dtype is not None and nb_shape is not None:
|
|
52
|
+
retty = nb.types.Array(dtype=nb_dtype, ndim=nb_shape, layout='C')
|
|
53
|
+
# Inlining the signature construction from numpy_empty_nd
|
|
54
|
+
sig = retty(shape, dtype)
|
|
55
|
+
return sig
|
|
56
|
+
else:
|
|
57
|
+
msg = f"Cannot parse input types to function np.empty({shape}, {dtype})"
|
|
58
|
+
raise nb.errors.TypingError(msg)
|
|
59
|
+
return typer
|
|
60
|
+
|
|
61
|
+
elif config.CUDA_AVAILABLE and isinstance(context,nb.cuda.target.CUDATypingContext):
|
|
62
|
+
|
|
63
|
+
# Function repurposed from numba's Cuda_array_decl.
|
|
64
|
+
def typer(shape, dtype):
|
|
65
|
+
|
|
66
|
+
# Only integer literals and tuples of integer literals are valid
|
|
67
|
+
# shapes
|
|
68
|
+
if isinstance(shape, nb.types.Integer):
|
|
69
|
+
if not isinstance(shape, nb.types.IntegerLiteral):
|
|
70
|
+
return None
|
|
71
|
+
elif isinstance(shape, (nb.types.Tuple, nb.types.UniTuple)):
|
|
72
|
+
if any([not isinstance(s, nb.types.IntegerLiteral)
|
|
73
|
+
for s in shape]):
|
|
74
|
+
return None
|
|
75
|
+
else:
|
|
76
|
+
return None
|
|
77
|
+
|
|
78
|
+
ndim = parse_shape(shape)
|
|
79
|
+
nb_dtype = parse_dtype(dtype)
|
|
80
|
+
if nb_dtype is not None and ndim is not None:
|
|
81
|
+
return nb.types.Array(dtype=nb_dtype, ndim=ndim, layout='C')
|
|
82
|
+
|
|
83
|
+
return typer
|
|
84
|
+
|
|
85
|
+
elif config.ROCM_AVAILABLE and isinstance(context,nb.hip.target.HIPTypingContext):
|
|
86
|
+
|
|
87
|
+
def typer(shape, dtype):
|
|
88
|
+
# Only integer literals and tuples of integer literals are valid
|
|
89
|
+
# shapes
|
|
90
|
+
if isinstance(shape, nb.types.Integer):
|
|
91
|
+
if not isinstance(shape, nb.types.IntegerLiteral):
|
|
92
|
+
return None
|
|
93
|
+
elif isinstance(shape, (nb.types.Tuple, nb.types.UniTuple)):
|
|
94
|
+
if any([not isinstance(s, nb.types.IntegerLiteral) for s in shape]):
|
|
95
|
+
return None
|
|
96
|
+
else:
|
|
97
|
+
return None
|
|
98
|
+
|
|
99
|
+
ndim = parse_shape(shape)
|
|
100
|
+
nb_dtype = parse_dtype(dtype)
|
|
101
|
+
if nb_dtype is not None and ndim is not None:
|
|
102
|
+
result = nb.types.Array(dtype=nb_dtype, ndim=ndim, layout="C")
|
|
103
|
+
return result
|
|
104
|
+
|
|
105
|
+
return typer
|
|
106
|
+
|
|
107
|
+
else:
|
|
108
|
+
raise nb.core.errors.UnsupportedError(f"Unsupported target context {context}.")
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
@nb.extending.lower_builtin(local_array, nb.types.IntegerLiteral, nb.types.Any)
|
|
114
|
+
def builtin_local_array(context, builder, sig, args):
|
|
115
|
+
|
|
116
|
+
shape, dtype = sig.args
|
|
117
|
+
|
|
118
|
+
from numba.core.typing.npydecl import parse_dtype, parse_shape
|
|
119
|
+
import numba.np.arrayobj as arrayobj
|
|
120
|
+
|
|
121
|
+
if isinstance(context,nb.core.cpu.CPUContext):
|
|
122
|
+
|
|
123
|
+
# No default arguments.
|
|
124
|
+
nb_dtype = parse_dtype(dtype)
|
|
125
|
+
nb_shape = parse_shape(shape)
|
|
126
|
+
|
|
127
|
+
retty = nb.types.Array(dtype=nb_dtype, ndim=nb_shape, layout='C')
|
|
128
|
+
|
|
129
|
+
# In ol_np_empty, the reference type of the array is fed into the
|
|
130
|
+
# signatrue as a third argument. This third argument is not used by
|
|
131
|
+
# _parse_empty_args.
|
|
132
|
+
sig = retty(shape, dtype)
|
|
133
|
+
|
|
134
|
+
arrtype, shapes = arrayobj._parse_empty_args(context, builder, sig, args)
|
|
135
|
+
ary = arrayobj._empty_nd_impl(context, builder, arrtype, shapes)
|
|
136
|
+
|
|
137
|
+
return ary._getvalue()
|
|
138
|
+
elif config.CUDA_AVAILABLE and isinstance(context,nb.cuda.target.CUDATargetContext):
|
|
139
|
+
length = sig.args[0].literal_value
|
|
140
|
+
dtype = parse_dtype(sig.args[1])
|
|
141
|
+
return nb.cuda.cudaimpl._generic_array(
|
|
142
|
+
context,
|
|
143
|
+
builder,
|
|
144
|
+
shape=(length,),
|
|
145
|
+
dtype=dtype,
|
|
146
|
+
symbol_name='_cudapy_harm_lmem',
|
|
147
|
+
addrspace=nb.cuda.cudadrv.nvvm.ADDRSPACE_LOCAL,
|
|
148
|
+
can_dynsized=False
|
|
149
|
+
)
|
|
150
|
+
elif config.ROCM_AVAILABLE and isinstance(context,nb.hip.target.HIPTargetContext):
|
|
151
|
+
length = sig.args[0].literal_value
|
|
152
|
+
dtype = parse_dtype(sig.args[1])
|
|
153
|
+
result = nb.hip.typing_lowering.hip.lowering._generic_array(
|
|
154
|
+
context,
|
|
155
|
+
builder,
|
|
156
|
+
shape=(length,),
|
|
157
|
+
dtype=dtype,
|
|
158
|
+
symbol_name="_HIPpy_lmem",
|
|
159
|
+
addrspace=nb.hip.amdgcn.ADDRSPACE_LOCAL,
|
|
160
|
+
can_dynsized=False,
|
|
161
|
+
)
|
|
162
|
+
return result
|
|
163
|
+
else:
|
|
164
|
+
raise nb.core.errors.UnsupportedError(f"Unsupported target context {context}.")
|
|
165
|
+
|
|
166
|
+
|
|
167
|
+
|
|
168
|
+
from numba.core.typing.npydecl import parse_dtype
|
|
169
|
+
import llvmlite.binding as ll
|
|
170
|
+
from llvmlite import ir
|
|
171
|
+
numba_dev_ptr = nb.types.voidptr
|
|
172
|
+
|
|
173
|
+
|
|
174
|
+
|
|
175
|
+
def array_from_ptr(ptr,shape,dtype):
|
|
176
|
+
pass
|
|
177
|
+
|
|
178
|
+
|
|
179
|
+
@nb.extending.type_callable(array_from_ptr)
|
|
180
|
+
def type_array_from_ptr(context):
|
|
181
|
+
|
|
182
|
+
from numba.core.typing.npydecl import parse_dtype, parse_shape
|
|
183
|
+
|
|
184
|
+
if isinstance(context, nb.core.typing.context.Context):
|
|
185
|
+
# Function repurposed from Numba's ol_np_empty.
|
|
186
|
+
def typer(ptr,shape, dtype):
|
|
187
|
+
nb.np.arrayobj._check_const_str_dtype("empty", dtype)
|
|
188
|
+
|
|
189
|
+
# Only integer literals and tuples of integer literals are valid
|
|
190
|
+
# shapes
|
|
191
|
+
if isinstance(shape, types.Integer):
|
|
192
|
+
if not isinstance(shape, types.IntegerLiteral):
|
|
193
|
+
raise nb.core.errors.UnsupportedError(
|
|
194
|
+
f"Integer shape type {shape} is not literal."
|
|
195
|
+
)
|
|
196
|
+
elif isinstance(shape, (types.Tuple, types.UniTuple)):
|
|
197
|
+
if any([not isinstance(s, types.IntegerLiteral) for s in shape]):
|
|
198
|
+
raise nb.core.errors.UnsupportedError(
|
|
199
|
+
f"At least one element of shape tuple type{shape} is not an integer literal."
|
|
200
|
+
)
|
|
201
|
+
else:
|
|
202
|
+
raise nb.core.errors.UnsupportedError(
|
|
203
|
+
f"Shape is of unsupported type {shape}."
|
|
204
|
+
)
|
|
205
|
+
|
|
206
|
+
# No default arguments.
|
|
207
|
+
nb_dtype = parse_dtype(dtype)
|
|
208
|
+
nb_shape = parse_shape(shape)
|
|
209
|
+
|
|
210
|
+
if nb_dtype is not None and nb_shape is not None:
|
|
211
|
+
retty = types.Array(dtype=nb_dtype, ndim=nb_shape, layout="C")
|
|
212
|
+
# Inlining the signature construction from numpy_empty_nd
|
|
213
|
+
sig = retty(shape, dtype)
|
|
214
|
+
return sig
|
|
215
|
+
else:
|
|
216
|
+
msg = f"Cannot parse input types to function np.empty({shape}, {dtype})"
|
|
217
|
+
raise numba.errors.TypingError(msg)
|
|
218
|
+
|
|
219
|
+
return typer
|
|
220
|
+
|
|
221
|
+
elif isinstance(context, nb.cuda.target.CUDATypingContext):
|
|
222
|
+
|
|
223
|
+
# Function repurposed from Numba's Cuda_array_decl.
|
|
224
|
+
def typer(ptr, shape, dtype):
|
|
225
|
+
|
|
226
|
+
# Only integer literals and tuples of integer literals are valid
|
|
227
|
+
# shapes
|
|
228
|
+
if isinstance(shape, nb.types.Integer):
|
|
229
|
+
if not isinstance(shape, types.IntegerLiteral):
|
|
230
|
+
msg = "At least one dimension in the input shape is non-literal"
|
|
231
|
+
raise nb.errors.TypingError(msg)
|
|
232
|
+
elif isinstance(shape, (nb.types.Tuple, nb.types.UniTuple)):
|
|
233
|
+
if any([not isinstance(s, nb.types.IntegerLiteral) for s in shape]):
|
|
234
|
+
msg = "At least one dimension in the input shape is non-literal"
|
|
235
|
+
raise nb.errors.TypingError(msg)
|
|
236
|
+
else:
|
|
237
|
+
msg = "Unexpected shape type. Shapes must be literal integers or tuples of literal integers."
|
|
238
|
+
raise nb.errors.TypingError(msg)
|
|
239
|
+
|
|
240
|
+
if not isinstance(ptr,nb.types.RawPointer):
|
|
241
|
+
msg = f"Expected raw pointer as third argument, recieved {ptr}"
|
|
242
|
+
raise nb.errors.TypingError(msg)
|
|
243
|
+
|
|
244
|
+
ndim = parse_shape(shape)
|
|
245
|
+
nb_dtype = parse_dtype(dtype)
|
|
246
|
+
if nb_dtype is not None and ndim is not None:
|
|
247
|
+
return nb.types.Array(dtype=nb_dtype, ndim=ndim, layout="C")
|
|
248
|
+
|
|
249
|
+
return typer
|
|
250
|
+
|
|
251
|
+
elif isinstance(context, nb.hip.target.HIPTypingContext):
|
|
252
|
+
def typer(ptr, shape, dtype):
|
|
253
|
+
|
|
254
|
+
# Only integer literals and tuples of integer literals are valid
|
|
255
|
+
# shapes
|
|
256
|
+
if isinstance(shape, nb.types.Integer):
|
|
257
|
+
if not isinstance(shape, nb.types.IntegerLiteral):
|
|
258
|
+
msg = "At least one dimension in the input shape is non-literal"
|
|
259
|
+
raise nb.errors.TypingError(msg)
|
|
260
|
+
elif isinstance(shape, (nb.types.Tuple, nb.types.UniTuple)):
|
|
261
|
+
if any([not isinstance(s, nb.types.IntegerLiteral) for s in shape]):
|
|
262
|
+
msg = "At least one dimension in the input shape is non-literal"
|
|
263
|
+
raise nb.errors.TypingError(msg)
|
|
264
|
+
else:
|
|
265
|
+
msg = "Unexpected shape type. Shapes must be literal integers or tuples of literal integers."
|
|
266
|
+
raise nb.errors.TypingError(msg)
|
|
267
|
+
|
|
268
|
+
if not isinstance(ptr,nb.types.RawPointer):
|
|
269
|
+
msg = f"Expected raw pointer as third argument, recieved {ptr}"
|
|
270
|
+
raise nb.errors.TypingError(msg)
|
|
271
|
+
|
|
272
|
+
ndim = parse_shape(shape)
|
|
273
|
+
nb_dtype = parse_dtype(dtype)
|
|
274
|
+
if nb_dtype is not None and ndim is not None:
|
|
275
|
+
return nb.types.Array(dtype=nb_dtype, ndim=ndim, layout="C")
|
|
276
|
+
|
|
277
|
+
return typer
|
|
278
|
+
|
|
279
|
+
else:
|
|
280
|
+
raise nb.core.errors.UnsupportedError(
|
|
281
|
+
f"Unsupported target context {context}."
|
|
282
|
+
)
|
|
283
|
+
|
|
284
|
+
|
|
285
|
+
|
|
286
|
+
|
|
287
|
+
@nb.extending.lower_builtin(array_from_ptr, numba_dev_ptr, nb.types.Tuple, nb.types.Any)
|
|
288
|
+
@nb.extending.lower_builtin(array_from_ptr, numba_dev_ptr, nb.types.UniTuple, nb.types.Any)
|
|
289
|
+
@nb.extending.lower_builtin(array_from_ptr, numba_dev_ptr, nb.types.IntegerLiteral, nb.types.Any)
|
|
290
|
+
def def_array_from_ptr(context, builder, sig, args):
|
|
291
|
+
|
|
292
|
+
if isinstance(context,nb.core.cpu.CPUContext):
|
|
293
|
+
raw_ptr_type = sig.args[0]
|
|
294
|
+
raw_ptr = args[0]
|
|
295
|
+
if isinstance(sig.args[1],nb.types.IntegerLiteral):
|
|
296
|
+
shape = [sig.args[1].literal_value]
|
|
297
|
+
else:
|
|
298
|
+
shape = [s.literal_value for s in sig.args[1]]
|
|
299
|
+
dtype = parse_dtype(sig.args[2])
|
|
300
|
+
|
|
301
|
+
elemcount = reduce(operator.mul, shape, 1)
|
|
302
|
+
lmod = builder.module
|
|
303
|
+
|
|
304
|
+
lldtype = context.get_data_type(dtype)
|
|
305
|
+
itemsize = context.get_abi_sizeof(lldtype)
|
|
306
|
+
|
|
307
|
+
laststride = itemsize
|
|
308
|
+
rstrides = []
|
|
309
|
+
for i, lastsize in enumerate(reversed(shape)):
|
|
310
|
+
rstrides.append(laststride)
|
|
311
|
+
laststride *= lastsize
|
|
312
|
+
strides = [s for s in reversed(rstrides)]
|
|
313
|
+
kstrides = [context.get_constant(types.intp, s) for s in strides]
|
|
314
|
+
|
|
315
|
+
ndim = len(shape)
|
|
316
|
+
aryty = types.Array(dtype=dtype, ndim=ndim, layout="C")
|
|
317
|
+
ary = context.make_array(aryty)(context, builder)
|
|
318
|
+
|
|
319
|
+
dataptr = builder.addrspacecast(
|
|
320
|
+
raw_ptr, ir.PointerType(ir.IntType(8)), "generic"
|
|
321
|
+
)
|
|
322
|
+
|
|
323
|
+
kshape = [context.get_constant(types.intp, s) for s in shape]
|
|
324
|
+
context.populate_array(
|
|
325
|
+
ary,
|
|
326
|
+
data=builder.bitcast(dataptr, ary.data.type),
|
|
327
|
+
shape=kshape,
|
|
328
|
+
strides=kstrides,
|
|
329
|
+
itemsize=context.get_constant(types.intp, itemsize),
|
|
330
|
+
meminfo=None,
|
|
331
|
+
)
|
|
332
|
+
return ary._getvalue()
|
|
333
|
+
elif isinstance(context, nb.cuda.target.CUDATargetContext):
|
|
334
|
+
raw_ptr_type = sig.args[0]
|
|
335
|
+
raw_ptr = args[0]
|
|
336
|
+
if isinstance(sig.args[1],nb.types.IntegerLiteral):
|
|
337
|
+
shape = [sig.args[1].literal_value]
|
|
338
|
+
else:
|
|
339
|
+
shape = [s.literal_value for s in sig.args[1]]
|
|
340
|
+
dtype = parse_dtype(sig.args[2])
|
|
341
|
+
|
|
342
|
+
elemcount = reduce(operator.mul, shape, 1)
|
|
343
|
+
lmod = builder.module
|
|
344
|
+
|
|
345
|
+
targetdata = ll.create_target_data(nb.cuda.cudadrv.nvvm.NVVM().data_layout)
|
|
346
|
+
lldtype = context.get_data_type(dtype)
|
|
347
|
+
itemsize = lldtype.get_abi_size(targetdata)
|
|
348
|
+
|
|
349
|
+
laststride = itemsize
|
|
350
|
+
rstrides = []
|
|
351
|
+
for i, lastsize in enumerate(reversed(shape)):
|
|
352
|
+
rstrides.append(laststride)
|
|
353
|
+
laststride *= lastsize
|
|
354
|
+
strides = [s for s in reversed(rstrides)]
|
|
355
|
+
kstrides = [context.get_constant(types.intp, s) for s in strides]
|
|
356
|
+
|
|
357
|
+
ndim = len(shape)
|
|
358
|
+
aryty = types.Array(dtype=dtype, ndim=ndim, layout="C")
|
|
359
|
+
ary = context.make_array(aryty)(context, builder)
|
|
360
|
+
|
|
361
|
+
dataptr = builder.addrspacecast(
|
|
362
|
+
raw_ptr, ir.PointerType(ir.IntType(8)), "generic"
|
|
363
|
+
)
|
|
364
|
+
|
|
365
|
+
kshape = [context.get_constant(types.intp, s) for s in shape]
|
|
366
|
+
context.populate_array(
|
|
367
|
+
ary,
|
|
368
|
+
data=builder.bitcast(dataptr, ary.data.type),
|
|
369
|
+
shape=kshape,
|
|
370
|
+
strides=kstrides,
|
|
371
|
+
itemsize=context.get_constant(types.intp, itemsize),
|
|
372
|
+
meminfo=None,
|
|
373
|
+
)
|
|
374
|
+
return ary._getvalue()
|
|
375
|
+
elif isinstance(context, nb.hip.target.HIPTargetContext):
|
|
376
|
+
raw_ptr_type = sig.args[0]
|
|
377
|
+
raw_ptr = args[0]
|
|
378
|
+
|
|
379
|
+
|
|
380
|
+
if isinstance(sig.args[1],nb.types.IntegerLiteral):
|
|
381
|
+
shape = [sig.args[1].literal_value]
|
|
382
|
+
else:
|
|
383
|
+
shape = [s.literal_value for s in sig.args[1]]
|
|
384
|
+
|
|
385
|
+
dtype = parse_dtype(sig.args[2])
|
|
386
|
+
|
|
387
|
+
elemcount = reduce(operator.mul, shape, 1)
|
|
388
|
+
lmod = builder.module
|
|
389
|
+
|
|
390
|
+
targetdata = ll.create_target_data(
|
|
391
|
+
nb.hip.amdgcn.DATA_LAYOUT
|
|
392
|
+
)
|
|
393
|
+
lldtype = context.get_data_type(dtype)
|
|
394
|
+
itemsize = lldtype.get_abi_size(targetdata)
|
|
395
|
+
|
|
396
|
+
laststride = itemsize
|
|
397
|
+
rstrides = []
|
|
398
|
+
for i, lastsize in enumerate(reversed(shape)):
|
|
399
|
+
rstrides.append(laststride)
|
|
400
|
+
laststride *= lastsize
|
|
401
|
+
strides = [s for s in reversed(rstrides)]
|
|
402
|
+
kstrides = [context.get_constant(types.intp, s) for s in strides]
|
|
403
|
+
|
|
404
|
+
ndim = len(shape)
|
|
405
|
+
aryty = types.Array(dtype=dtype, ndim=ndim, layout="C")
|
|
406
|
+
ary = context.make_array(aryty)(context, builder)
|
|
407
|
+
|
|
408
|
+
|
|
409
|
+
dataptr = builder.addrspacecast(
|
|
410
|
+
raw_ptr, ir.PointerType(ir.IntType(8)), "generic"
|
|
411
|
+
)
|
|
412
|
+
|
|
413
|
+
kshape = [context.get_constant(types.intp, s) for s in shape]
|
|
414
|
+
|
|
415
|
+
|
|
416
|
+
context.populate_array(
|
|
417
|
+
ary,
|
|
418
|
+
data=builder.bitcast(dataptr, ary.data.type),
|
|
419
|
+
shape=kshape,
|
|
420
|
+
strides=kstrides,
|
|
421
|
+
itemsize=context.get_constant(types.intp, itemsize),
|
|
422
|
+
meminfo=None,
|
|
423
|
+
)
|
|
424
|
+
result = ary._getvalue()
|
|
425
|
+
return result
|
|
426
|
+
else:
|
|
427
|
+
raise nb.core.errors.UnsupportedError(
|
|
428
|
+
f"Unsupported target context {context}."
|
|
429
|
+
)
|
|
430
|
+
|
|
431
|
+
|
|
432
|
+
|
|
433
|
+
@nb.njit()
|
|
434
|
+
def python_array_from_ptr(ptr):
|
|
435
|
+
return array_from_ptr(ptr)
|
|
436
|
+
|
|
437
|
+
|
|
438
|
+
|
|
439
|
+
|
|
440
|
+
sig = nb.core.typing.signature
|
|
441
|
+
ext_fn = nb.types.ExternalFunction
|
|
442
|
+
|
|
443
|
+
|
|
444
|
+
ext_alloc_device_bytes = ext_fn("harmonize_alloc_device_bytes", sig(nb.types.voidptr,nb.types.intp))
|
|
445
|
+
ext_alloc_managed_bytes = ext_fn("harmonize_alloc_managed_bytes", sig(nb.types.voidptr,nb.types.intp))
|
|
446
|
+
ext_free_device_bytes = ext_fn("harmonize_free_device_bytes", sig(nb.types.void,nb.types.voidptr))
|
|
447
|
+
#ext_memcpy_host_to_device = ext_fn("harmonize_memcpy_host_to_device", sig(nb.types.void,nb.types.voidptr,nb.types.void_ptr))
|
|
448
|
+
#ext_memcpy_device_to_host = ext_fn("harmonize_memcpy_device_to_host", sig(nb.types.void,nb.types.voidptr,nb.types.void_ptr))
|
|
449
|
+
|
|
450
|
+
@nb.jit()
|
|
451
|
+
def alloc_device_bytes(size):
|
|
452
|
+
return ext_alloc_device_bytes(size)
|
|
453
|
+
|
|
454
|
+
@nb.jit()
|
|
455
|
+
def alloc_managed_bytes(size):
|
|
456
|
+
return ext_alloc_managed_bytes(size)
|
|
457
|
+
|
|
458
|
+
@nb.jit()
|
|
459
|
+
def free_device_bytes(size):
|
|
460
|
+
return ext_free_device_bytes(size)
|
|
461
|
+
|
|
462
|
+
@nb.jit()
|
|
463
|
+
def ptr_from_array(array):
|
|
464
|
+
return nb.carray(array)
|
|
465
|
+
|
|
466
|
+
|
|
467
|
+
|
|
468
|
+
|
|
469
|
+
|
|
470
|
+
host_to_device_array_template = """
|
|
471
|
+
def {name}(dev_ptr,host_obj):
|
|
472
|
+
ptr = (ffi.from_buffer(host_obj))
|
|
473
|
+
vptr = into_voidptr(ptr)
|
|
474
|
+
host_to_device(dev_ptr,vptr,size*host_obj.size)
|
|
475
|
+
"""
|
|
476
|
+
|
|
477
|
+
host_to_device_object_template = """
|
|
478
|
+
def {name}(dev_ptr,host_obj):
|
|
479
|
+
host_to_device(dev_ptr,host_obj,size)
|
|
480
|
+
"""
|
|
481
|
+
|
|
482
|
+
@nb.njit()
|
|
483
|
+
def memcpy_host_to_device(dev_ptr,host_obj):
|
|
484
|
+
memcpy_host_to_device_python(dev_ptr,host_obj)
|
|
485
|
+
|
|
486
|
+
def memcpy_host_to_device_python(dev_ptr,host_obj):
|
|
487
|
+
raise RuntimeError("Host/device memory copies are only supported in nopython mode, and only on CPU.")
|
|
488
|
+
|
|
489
|
+
@nb.extending.overload(memcpy_host_to_device_python,target='cpu')
|
|
490
|
+
def cpu_memcpy_host_to_device_overload(dev_ptr,host_obj):
|
|
491
|
+
|
|
492
|
+
void = nb.types.void
|
|
493
|
+
vp = nb.types.voidptr
|
|
494
|
+
uintp = nb.types.uintp
|
|
495
|
+
sig = nb.core.typing.signature
|
|
496
|
+
|
|
497
|
+
if isinstance(host_obj,nb.types.Array) :
|
|
498
|
+
id = generate_uuid()
|
|
499
|
+
name = f"memcpy_host_to_device_{id}"
|
|
500
|
+
if isinstance(host_obj.dtype,nb.types.Record):
|
|
501
|
+
size = host_obj.dtype.size
|
|
502
|
+
else :
|
|
503
|
+
size = host_obj.dtype.bitwidth / 8
|
|
504
|
+
host_to_device = ext_fn(f"harmonize_memcpy_host_to_device", sig(void, vp, vp, uintp))
|
|
505
|
+
exec(host_to_device_array_template.format(name=name),globals()|locals(),locals())
|
|
506
|
+
impl = eval(name)
|
|
507
|
+
else :
|
|
508
|
+
id = generate_uuid()
|
|
509
|
+
name = f"memcpy_host_to_device_{id}"
|
|
510
|
+
size = host_obj.size
|
|
511
|
+
host_to_device = ext_fn(f"harmonize_memcpy_host_to_device", sig(void, vp, host_obj, uintp))
|
|
512
|
+
exec(host_to_device_object_template.format(name=name),globals()|locals(),locals())
|
|
513
|
+
impl = eval(name)
|
|
514
|
+
return impl
|
|
515
|
+
|
|
516
|
+
@nb.extending.overload(memcpy_host_to_device_python,target='gpu')
|
|
517
|
+
def gpu_memcpy_host_to_device_overload(dev_ptr,host_obj):
|
|
518
|
+
raise RuntimeError("Host/device memory copies are only supported in nopython mode, and only on CPU.")
|
|
519
|
+
|
|
520
|
+
|
|
521
|
+
|
|
522
|
+
|
|
523
|
+
|
|
524
|
+
|
|
525
|
+
|
|
526
|
+
device_to_host_array_template = """
|
|
527
|
+
def {name}(host_obj,dev_ptr):
|
|
528
|
+
ptr = (ffi.from_buffer(host_obj))
|
|
529
|
+
vptr = into_voidptr(ptr)
|
|
530
|
+
device_to_host(vptr,dev_ptr,size*host_obj.size)
|
|
531
|
+
"""
|
|
532
|
+
|
|
533
|
+
device_to_host_object_template = """
|
|
534
|
+
def {name}(host_obj,dev_ptr):
|
|
535
|
+
device_to_host(host_obj,dev_ptr,size)
|
|
536
|
+
"""
|
|
537
|
+
|
|
538
|
+
|
|
539
|
+
|
|
540
|
+
@nb.njit()
|
|
541
|
+
def memcpy_device_to_host(host_obj,dev_ptr):
|
|
542
|
+
memcpy_device_to_host_python(host_obj,dev_ptr)
|
|
543
|
+
|
|
544
|
+
def memcpy_device_to_host_python(host_obj,dev_ptr):
|
|
545
|
+
raise RuntimeError("Host/device memory copies are only supported in nopython mode, and only on CPU.")
|
|
546
|
+
|
|
547
|
+
@nb.extending.overload(memcpy_device_to_host_python,target='cpu')
|
|
548
|
+
def cpu_memcpy_device_to_host_overload(host_obj,dev_ptr):
|
|
549
|
+
|
|
550
|
+
void = nb.types.void
|
|
551
|
+
vp = nb.types.voidptr
|
|
552
|
+
uintp = nb.types.uintp
|
|
553
|
+
sig = nb.core.typing.signature
|
|
554
|
+
|
|
555
|
+
if isinstance(host_obj,nb.types.Array) :
|
|
556
|
+
id = generate_uuid()
|
|
557
|
+
name = f"memcpy_device_to_host_{id}"
|
|
558
|
+
if isinstance(host_obj.dtype,nb.types.Record):
|
|
559
|
+
size = host_obj.dtype.size
|
|
560
|
+
else :
|
|
561
|
+
size = host_obj.dtype.bitwidth / 8
|
|
562
|
+
device_to_host = ext_fn(f"harmonize_memcpy_device_to_host", sig(void, vp, vp, uintp))
|
|
563
|
+
exec(device_to_host_array_template.format(name=name),globals()|locals(),locals())
|
|
564
|
+
impl = eval(name)
|
|
565
|
+
else :
|
|
566
|
+
id = generate_uuid()
|
|
567
|
+
name = f"memcpy_device_to_host_{id}"
|
|
568
|
+
device_to_host = ext_fn(f"harmonize_memcpy_device_to_host", sig(void, host_obj, vp, uintp))
|
|
569
|
+
size = host_obj.size
|
|
570
|
+
exec(device_to_host_object_template.format(name=name),globals()|locals(),locals())
|
|
571
|
+
impl = eval(name)
|
|
572
|
+
return impl
|
|
573
|
+
|
|
574
|
+
@nb.extending.overload(memcpy_device_to_host_python,target='gpu')
|
|
575
|
+
def gpu_memcpy_device_to_host_overload(host_obj,dev_ptr):
|
|
576
|
+
raise RuntimeError("Host/device memory copies are only supported in nopython mode, and only on CPU.")
|
|
577
|
+
|
|
578
|
+
|
|
579
|
+
|
|
580
|
+
def generate_array_code():
|
|
581
|
+
text = ""
|
|
582
|
+
text += alloc_device_bytes_template
|
|
583
|
+
text += alloc_managed_bytes_template
|
|
584
|
+
text += free_device_bytes_template
|
|
585
|
+
text += memcpy_host_to_device_template
|
|
586
|
+
text += memcpy_device_to_host_template
|
|
587
|
+
return text
|
|
588
|
+
|