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/atomics.py
ADDED
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
import numba
|
|
2
|
+
import llvmlite
|
|
3
|
+
|
|
4
|
+
from harmonize import errors, config
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
from .prim import *
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
atomic_op_info = [
|
|
12
|
+
("add", [
|
|
13
|
+
(int32, int32),
|
|
14
|
+
(uint32, uint32),
|
|
15
|
+
(int64, uint64),
|
|
16
|
+
(uint64, uint64),
|
|
17
|
+
(float32,float32),
|
|
18
|
+
(float64,float64),
|
|
19
|
+
]),
|
|
20
|
+
("max", [
|
|
21
|
+
(int32, int32),
|
|
22
|
+
(uint32, uint32),
|
|
23
|
+
(uint64, uint64),
|
|
24
|
+
])
|
|
25
|
+
]
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
@numba.extending.intrinsic
|
|
29
|
+
def cast_ptr(typing_context, input_ptr, ptr_type):
|
|
30
|
+
|
|
31
|
+
def impl(context, builder, _signature, args):
|
|
32
|
+
llvm_type = context.get_value_type(target_type)
|
|
33
|
+
val = builder.bitcast(args[0], llvm_type)
|
|
34
|
+
return val
|
|
35
|
+
|
|
36
|
+
if isinstance(ptr_type, numba.types.TypeRef):
|
|
37
|
+
target_type = ptr_type.instance_type
|
|
38
|
+
else:
|
|
39
|
+
target_type = ptr_type
|
|
40
|
+
|
|
41
|
+
sig = target_type(input_ptr, ptr_type)
|
|
42
|
+
|
|
43
|
+
return sig, impl
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
@numba.extending.intrinsic
|
|
47
|
+
def get_ptr(typing_context, array):
|
|
48
|
+
|
|
49
|
+
def impl(context, builder, _signature, args):
|
|
50
|
+
llvm_array_type = context.get_value_type(array)
|
|
51
|
+
llvm_vp_type = context.get_value_type(vp)
|
|
52
|
+
four = llvmlite.ir.Constant(llvmlite.ir.IntType(32),0)
|
|
53
|
+
ptr = builder.extract_value(args[0],4)
|
|
54
|
+
gen_ptr = builder.bitcast(ptr,llvm_vp_type)
|
|
55
|
+
return gen_ptr
|
|
56
|
+
|
|
57
|
+
if not isinstance(array, numba.types.Array):
|
|
58
|
+
return None
|
|
59
|
+
|
|
60
|
+
sig = vp(array)
|
|
61
|
+
|
|
62
|
+
return sig, impl
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
def array_atomic_extern(op_name,face_type):
|
|
66
|
+
sig = numba.core.typing.signature
|
|
67
|
+
ext_fn = numba.types.ExternalFunction
|
|
68
|
+
|
|
69
|
+
signature = sig(face_type,vp,usize,face_type)
|
|
70
|
+
py_name = prim_info[face_type]["py_name"]
|
|
71
|
+
implementation = ext_fn(f"array_atomic_{op_name}_{py_name}", signature)
|
|
72
|
+
|
|
73
|
+
@numba.njit
|
|
74
|
+
def inner_cast(array,index,value):
|
|
75
|
+
return implementation(array.ctypes,index,value)
|
|
76
|
+
|
|
77
|
+
return inner_cast
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
def array_atomic_add(array,index,value):
|
|
82
|
+
return None
|
|
83
|
+
|
|
84
|
+
@numba.extending.overload(array_atomic_add)
|
|
85
|
+
def array_atomic_add_inner(array,index,value):
|
|
86
|
+
|
|
87
|
+
if config.CUDA_AVAILABLE:
|
|
88
|
+
def impl(array,index,value):
|
|
89
|
+
return numba.cuda.atomic.add(array,index,value)
|
|
90
|
+
return impl
|
|
91
|
+
elif not config.ROCM_AVAILABLE:
|
|
92
|
+
raise errors.no_platforms()
|
|
93
|
+
|
|
94
|
+
if not isinstance(array,numba.types.Array):
|
|
95
|
+
raise numba.errors.TypingError("First argument should be an array.")
|
|
96
|
+
|
|
97
|
+
is_tuple = False
|
|
98
|
+
|
|
99
|
+
if isinstance(index,numba.types.Tuple):
|
|
100
|
+
if len(index) == 0:
|
|
101
|
+
raise numba.errors.TypingError("Zero-length tuple given as index. At least one index must be provided.")
|
|
102
|
+
for kind in index:
|
|
103
|
+
if not isinstance(kind,numba.types.Integer):
|
|
104
|
+
raise numba.errors.TypingError(f"Non-integer value of type '{kind}' in index tuple.")
|
|
105
|
+
is_tuple = True
|
|
106
|
+
elif isinstance(index,numba.types.UniTuple):
|
|
107
|
+
if len(index) == 0:
|
|
108
|
+
raise numba.errors.TypingError("Zero-length tuple given as index. At least one index must be provided.")
|
|
109
|
+
if not isinstance(index.dtype,numba.types.Integer):
|
|
110
|
+
raise numba.errors.TypingError(f"Non-integer value of type '{kind.dtype}' in index tuple.")
|
|
111
|
+
is_tuple = True
|
|
112
|
+
elif not isinstance(index,numba.types.Integer):
|
|
113
|
+
raise numba.errors.TypingError("Index is not an integer or a positive-length tuple of integers.")
|
|
114
|
+
|
|
115
|
+
allowed = {
|
|
116
|
+
int32 : array_atomic_add_i32,
|
|
117
|
+
uint32 : array_atomic_add_u32,
|
|
118
|
+
int64 : array_atomic_add_i64,
|
|
119
|
+
uint64 : array_atomic_add_u64,
|
|
120
|
+
float32 : array_atomic_add_f32,
|
|
121
|
+
float64 : array_atomic_add_f64
|
|
122
|
+
}
|
|
123
|
+
if not (value in allowed):
|
|
124
|
+
return None
|
|
125
|
+
|
|
126
|
+
inner = allowed[value]
|
|
127
|
+
|
|
128
|
+
if is_tuple:
|
|
129
|
+
|
|
130
|
+
def array_atomic_add(array,index,value):
|
|
131
|
+
|
|
132
|
+
return inner(array[index[:-1]],index[-1],value)
|
|
133
|
+
|
|
134
|
+
return array_atomic_add
|
|
135
|
+
|
|
136
|
+
else:
|
|
137
|
+
|
|
138
|
+
def array_atomic_add(array,index,value):
|
|
139
|
+
return inner(array,index,value)
|
|
140
|
+
|
|
141
|
+
return array_atomic_add
|
|
142
|
+
|
|
143
|
+
|
|
144
|
+
|
|
145
|
+
def array_atomic_max(array,index,value):
|
|
146
|
+
return None
|
|
147
|
+
|
|
148
|
+
@numba.extending.overload(array_atomic_max)
|
|
149
|
+
def array_atomic_max_inner(array,index,value):
|
|
150
|
+
|
|
151
|
+
if config.CUDA_AVAILABLE:
|
|
152
|
+
def impl(array,index,value):
|
|
153
|
+
return numba.cuda.atomic.max(array,index,value)
|
|
154
|
+
return impl
|
|
155
|
+
elif not config.ROCM_AVAILABLE:
|
|
156
|
+
raise errors.no_platforms()
|
|
157
|
+
|
|
158
|
+
if not isinstance(array,numba.types.Array):
|
|
159
|
+
raise numba.errors.TypingError("First argument should be an array.")
|
|
160
|
+
|
|
161
|
+
if isinstance(index,numba.types.Tuple):
|
|
162
|
+
if len(index) == 0:
|
|
163
|
+
raise numba.errors.TypingError("Zero-length tuple given as index. At least one index must be provided.")
|
|
164
|
+
for kind in index:
|
|
165
|
+
if not isinstance(kind,numba.types.Integer):
|
|
166
|
+
raise numba.errors.TypingError(f"Non-integer value of type '{kind}' in index tuple.")
|
|
167
|
+
is_tuple = True
|
|
168
|
+
elif isinstance(index,numba.types.UniTuple):
|
|
169
|
+
if len(index) == 0:
|
|
170
|
+
raise numba.errors.TypingError("Zero-length tuple given as index. At least one index must be provided.")
|
|
171
|
+
if not isinstance(index.dtype,numba.types.Integer):
|
|
172
|
+
raise numba.errors.TypingError(f"Non-integer value of type '{kind.dtype}' in index tuple.")
|
|
173
|
+
is_tuple = True
|
|
174
|
+
elif not isinstance(index,numba.types.Integer):
|
|
175
|
+
raise numba.errors.TypingError("Index is not an integer or a positive-length tuple of integers.")
|
|
176
|
+
|
|
177
|
+
allowed = {
|
|
178
|
+
int32 : array_atomic_add_i32,
|
|
179
|
+
uint32 : array_atomic_add_u32,
|
|
180
|
+
uint64 : array_atomic_add_u64,
|
|
181
|
+
}
|
|
182
|
+
if not (value in allowed):
|
|
183
|
+
return None
|
|
184
|
+
|
|
185
|
+
inner = allowed[value]
|
|
186
|
+
|
|
187
|
+
def array_atomic_max(array,index,value):
|
|
188
|
+
return inner(array,index,value)
|
|
189
|
+
|
|
190
|
+
return array_atomic_max
|
|
191
|
+
|
|
192
|
+
|
|
193
|
+
|
|
194
|
+
|
|
195
|
+
|
|
196
|
+
array_atomic_add_i32 = array_atomic_extern("add", int32)
|
|
197
|
+
array_atomic_add_u32 = array_atomic_extern("add", uint32)
|
|
198
|
+
array_atomic_add_i64 = array_atomic_extern("add", int64)
|
|
199
|
+
array_atomic_add_u64 = array_atomic_extern("add", uint64)
|
|
200
|
+
array_atomic_add_f32 = array_atomic_extern("add",float32)
|
|
201
|
+
array_atomic_add_f64 = array_atomic_extern("add",float64)
|
|
202
|
+
|
|
203
|
+
array_atomic_max_i32 = array_atomic_extern("max", int32)
|
|
204
|
+
array_atomic_max_u32 = array_atomic_extern("max", uint32)
|
|
205
|
+
array_atomic_max_u64 = array_atomic_extern("max", uint64)
|
|
206
|
+
|
harmonize/codegen.py
ADDED
|
@@ -0,0 +1,409 @@
|
|
|
1
|
+
import re
|
|
2
|
+
import numba
|
|
3
|
+
import numpy as np
|
|
4
|
+
import struct
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
from harmonize import config, errors
|
|
8
|
+
from harmonize.logging import verbose_print, debug_print, progress_print
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
uuid_iterator = 0
|
|
12
|
+
|
|
13
|
+
def generate_uuid():
|
|
14
|
+
global uuid_iterator
|
|
15
|
+
result = uuid_iterator
|
|
16
|
+
uuid_iterator += 1
|
|
17
|
+
return result
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def declare_device(name,sig):
|
|
21
|
+
if config.CUDA_AVAILABLE:
|
|
22
|
+
return config.cuda.declare_device(name, sig)
|
|
23
|
+
elif config.ROCM_AVAILABLE:
|
|
24
|
+
return config.hip.declare_device(name, sig)
|
|
25
|
+
else:
|
|
26
|
+
errors.no_platforms()
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
# Injects `value` as the value of the global variable named `name` in the module
|
|
30
|
+
# that defined the function `index` calls down the stack, from the perspective
|
|
31
|
+
# of the function that calls `inject_global`.
|
|
32
|
+
def inject_global(name,value,index):
|
|
33
|
+
frm = inspect.stack()[index+1]
|
|
34
|
+
mod = inspect.getmodule(frm[0])
|
|
35
|
+
setattr(sys.modules[mod.__name__], name, value)
|
|
36
|
+
debug_print(f"Defined '{name}' as "+str(value)+" for module "+mod.__name__)
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
# Returns the type annotations of the arguments for the input function
|
|
40
|
+
def fn_arg_ano_list( func ):
|
|
41
|
+
result = []
|
|
42
|
+
for arg,ano in func.__annotations__.items():
|
|
43
|
+
if( arg != 'return' ):
|
|
44
|
+
result.append(ano)
|
|
45
|
+
return result
|
|
46
|
+
|
|
47
|
+
# Returns the numba type of the function signature of the input function
|
|
48
|
+
def fn_sig( func ):
|
|
49
|
+
arg_list = []
|
|
50
|
+
ret = numba.types.void
|
|
51
|
+
for arg,ano in func.__annotations__.items():
|
|
52
|
+
if( arg != 'return' ):
|
|
53
|
+
arg_list.append(ano)
|
|
54
|
+
else:
|
|
55
|
+
ret = ano
|
|
56
|
+
return ret(*arg_list)
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
# Returns the type annotations of the arguments for the input function
|
|
60
|
+
# as a tuple
|
|
61
|
+
def fn_arg_ano( func ):
|
|
62
|
+
return tuple( x for x in fn_arg_ano_list(func) )
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
# Raises an error if the annotated return type for the input function
|
|
66
|
+
# does not patch the input result type
|
|
67
|
+
def assert_fn_res_ano( func, res_type ):
|
|
68
|
+
if 'return' in func.__annotations__:
|
|
69
|
+
res_ano = func.__annotations__['return']
|
|
70
|
+
if res_ano != res_type :
|
|
71
|
+
arg_str = str(fn_arg_ano(func))
|
|
72
|
+
ano_str = arg_str + " -> " + str(res_ano)
|
|
73
|
+
cmp_str = arg_str + " -> " + str(res_type)
|
|
74
|
+
err_str = "Annotated function type '" + ano_str \
|
|
75
|
+
+ "' does not match the type deduced by the compiler '" \
|
|
76
|
+
+ cmp_str + "'\nMake sure the definition of the function '" \
|
|
77
|
+
+ func.__name__ + "' results in a return type matching its " \
|
|
78
|
+
+ "annotation when supplied arguments matching its annotation."
|
|
79
|
+
raise(TypeError(err_str))
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
# Returns the ptx of the input function, as a global CUDA function. If the return type deduced
|
|
83
|
+
# by compilation is not consistent with the annotated return type, an exception is raised.
|
|
84
|
+
def global_ir( func ):
|
|
85
|
+
ptx, res_type = cuda.compile_ptx_for_current_device(func,fn_arg_ano(func),debug=config.DEBUG,opt=(not config.DEBUG))
|
|
86
|
+
assert_fn_res_ano(func, res_type)
|
|
87
|
+
return ptx
|
|
88
|
+
|
|
89
|
+
# Returns the ptx of the input function, as a device CUDA function. If the return type deduced
|
|
90
|
+
# by compilation is not consistent with the annotated return type, an exception is raised.
|
|
91
|
+
def device_ir( func, platform ):
|
|
92
|
+
if platform == config.GPUPlatform.ROCM:
|
|
93
|
+
ptx, res_type = config.hip.compile_ptx_for_current_device(func,fn_arg_ano(func),device=True,debug=config.DEBUG,opt=(not config.DEBUG),name=f"_{func.__name__}",link_in_hipdevicelib=False)
|
|
94
|
+
else :
|
|
95
|
+
ptx, res_type = config.cuda.compile_ptx_for_current_device(func,fn_arg_ano(func),device=True,debug=config.DEBUG,opt=(not config.DEBUG))
|
|
96
|
+
assert_fn_res_ano(func, res_type)
|
|
97
|
+
return ptx, res_type
|
|
98
|
+
|
|
99
|
+
# Returns the modify date of the file containing the supplied function. This is useful for
|
|
100
|
+
# detecting if a function was possibly changed
|
|
101
|
+
def func_defn_time(func):
|
|
102
|
+
return getmtime(func.__globals__['__file__'])
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
def extern_device_ir( func, type_map, suffix, platform ):
|
|
107
|
+
arg_types = fn_arg_ano_list(func)
|
|
108
|
+
ir_text, res_type = device_ir(func,platform)
|
|
109
|
+
if platform == config.GPUPlatform.CUDA:
|
|
110
|
+
ir_text = re.sub( \
|
|
111
|
+
r'(?P<before>\.visible\s+\.func\s*\(\s*\.param\s+\.\w+\s+\w+\s*\)\s*)(?P<name>\w+)(?P<after>\((?P<params>(\s*\.param\s+\.\w+\s+\w+\s*)(,\s*.param\s+\.\w+\s+\w+\s*)*)\))', \
|
|
112
|
+
r'\g<before>'+f"_{func.__name__}_{suffix}"+r'\g<after>', \
|
|
113
|
+
ir_text \
|
|
114
|
+
)
|
|
115
|
+
elif platform == config.GPUPlatform.ROCM:
|
|
116
|
+
ir_text = re.sub( r'define', r'define internal', ir_text)
|
|
117
|
+
ir_text = re.sub( r'internal hidden', r'hidden', ir_text)
|
|
118
|
+
return ir_text
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
# Used to map numba types to numpy d_types
|
|
122
|
+
def map_type_to_np(kind):
|
|
123
|
+
return numba.np.numpy_support.as_dtype(kind)
|
|
124
|
+
primitives = {
|
|
125
|
+
numba.none : np.void,
|
|
126
|
+
bool : np.bool8,
|
|
127
|
+
numba.boolean : np.bool8,
|
|
128
|
+
numba.uint8 : np.uint8,
|
|
129
|
+
numba.uint16 : np.uint16,
|
|
130
|
+
numba.uint32 : np.uint32,
|
|
131
|
+
numba.uint64 : np.uint64,
|
|
132
|
+
numba.int8 : np.int8,
|
|
133
|
+
numba.int16 : np.int16,
|
|
134
|
+
numba.int32 : np.int32,
|
|
135
|
+
numba.int64 : np.int64,
|
|
136
|
+
numba.float32 : np.float32,
|
|
137
|
+
numba.float64 : np.float64
|
|
138
|
+
}
|
|
139
|
+
if kind in primitives:
|
|
140
|
+
return primitives[kind]
|
|
141
|
+
return kind
|
|
142
|
+
|
|
143
|
+
|
|
144
|
+
# Determines the alignment of an input record type. For proper operation,
|
|
145
|
+
# the input type MUST be a record type
|
|
146
|
+
def alignment(kind):
|
|
147
|
+
if isinstance(kind,dict):
|
|
148
|
+
return 8
|
|
149
|
+
elif isinstance(kind,numba.types.Record):
|
|
150
|
+
align = 1
|
|
151
|
+
for name,type in kind.members:
|
|
152
|
+
member_align = kind.alignof(name)
|
|
153
|
+
if member_align != None and member_align > align:
|
|
154
|
+
align = member_align
|
|
155
|
+
return align
|
|
156
|
+
elif isinstance(kind,numba.types.Type):
|
|
157
|
+
return kind.bitwidth // 8
|
|
158
|
+
else:
|
|
159
|
+
raise numba.errors.TypingError(
|
|
160
|
+
f"Cannot find alignment of type {kind}."
|
|
161
|
+
)
|
|
162
|
+
|
|
163
|
+
|
|
164
|
+
# Determines the alignment of an input type. For proper operation,
|
|
165
|
+
# the input type MUST be a record type or a dict of dicts/records.
|
|
166
|
+
# This limitation applies recursively
|
|
167
|
+
def size_of(kind):
|
|
168
|
+
align = 8
|
|
169
|
+
if isinstance(kind,dict):
|
|
170
|
+
result = 0
|
|
171
|
+
for sub_name,sub_kind in kind.items():
|
|
172
|
+
debug_print(f"Size is {result}")
|
|
173
|
+
sub_size = size_of(sub_kind)
|
|
174
|
+
sub_size = ((sub_size+(align-1))//align) * align
|
|
175
|
+
debug_print(f"Adding {sub_size}")
|
|
176
|
+
result += sub_size
|
|
177
|
+
debug_print(f"Final size : {result}")
|
|
178
|
+
return result
|
|
179
|
+
elif isinstance(kind,numba.types.Record):
|
|
180
|
+
return kind.size
|
|
181
|
+
elif isinstance(kind,numba.types.Array):
|
|
182
|
+
return size_of(numba.types.uintp)
|
|
183
|
+
elif isinstance(kind,numba.types.Type):
|
|
184
|
+
return kind.bitwidth // 8
|
|
185
|
+
else:
|
|
186
|
+
raise numba.errors.TypingError(
|
|
187
|
+
"Cannot find size of type '{kind}'."
|
|
188
|
+
)
|
|
189
|
+
|
|
190
|
+
|
|
191
|
+
|
|
192
|
+
# Maps an input type to the CUDA/C++ equivalent type name used by Harmonize
|
|
193
|
+
def map_type_name(type_map,kind,rec_mode=""):
|
|
194
|
+
primitives = {
|
|
195
|
+
numba.none : "void",
|
|
196
|
+
bool : "bool",
|
|
197
|
+
np.bool_ : "bool",
|
|
198
|
+
np.uint8 : "uint8_t",
|
|
199
|
+
np.uint16 : "uint16_t",
|
|
200
|
+
np.uint32 : "uint32_t",
|
|
201
|
+
np.uint64 : "uint64_t",
|
|
202
|
+
np.int8 : "int8_t",
|
|
203
|
+
np.int16 : "int16_t",
|
|
204
|
+
np.int32 : "int32_t",
|
|
205
|
+
np.int64 : "int64_t",
|
|
206
|
+
np.float32 : "float",
|
|
207
|
+
np.float64 : "double"
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
if isinstance(kind,dict):
|
|
211
|
+
size = size_of(kind)
|
|
212
|
+
align = 8
|
|
213
|
+
size = ((size + (align-1)) // align) * align
|
|
214
|
+
result = f"_{size}b{align}"
|
|
215
|
+
if rec_mode == "ptr":
|
|
216
|
+
result += "*"
|
|
217
|
+
elif rec_mode == "void_ptr":
|
|
218
|
+
result = "void*"
|
|
219
|
+
return result
|
|
220
|
+
elif kind in primitives:
|
|
221
|
+
return primitives[kind]
|
|
222
|
+
elif isinstance(kind,numba.types.abstract.Literal):
|
|
223
|
+
return map_type_name(type_map,type(kind._literal_value))
|
|
224
|
+
elif isinstance(kind,numba.types.Integer):
|
|
225
|
+
result = "int"
|
|
226
|
+
if not kind.signed :
|
|
227
|
+
result = "u" + result
|
|
228
|
+
result += str(kind.bitwidth)
|
|
229
|
+
return result + "_t"
|
|
230
|
+
elif isinstance(kind,numba.types.Float):
|
|
231
|
+
return "float" + str(kind.bitwidth)
|
|
232
|
+
elif isinstance(kind,numba.types.Boolean):
|
|
233
|
+
return "bool"
|
|
234
|
+
elif isinstance(kind,numba.types.Record):
|
|
235
|
+
size = kind.size
|
|
236
|
+
align = alignment(kind)
|
|
237
|
+
align = 8
|
|
238
|
+
size = ((size + (align-1)) // align) * align
|
|
239
|
+
result = f"_{size}b{align}"
|
|
240
|
+
if rec_mode == "ptr":
|
|
241
|
+
result += "*"
|
|
242
|
+
elif rec_mode == "void_ptr":
|
|
243
|
+
result = "void*"
|
|
244
|
+
return result
|
|
245
|
+
elif isinstance(kind,numba.types.npytypes.NestedArray):
|
|
246
|
+
return "void*"
|
|
247
|
+
elif isinstance(kind,numba.types.npytypes.Array):
|
|
248
|
+
return "void*"
|
|
249
|
+
elif isinstance(kind,numba.types.Array):
|
|
250
|
+
return "void*"
|
|
251
|
+
elif isinstance(kind,numba.types.misc.RawPointer):
|
|
252
|
+
return "void*"
|
|
253
|
+
else:
|
|
254
|
+
raise RuntimeError("Unrecognized type '"+str(kind)+"' with type '"+str(type(kind))+"'")
|
|
255
|
+
|
|
256
|
+
|
|
257
|
+
# Returns the number of arguments used by the input function
|
|
258
|
+
def arg_count(func):
|
|
259
|
+
return len(fn_arg_ano_list(func))
|
|
260
|
+
|
|
261
|
+
|
|
262
|
+
# Returns the CUDA/C++ text used as the parameter list for the input function, with various
|
|
263
|
+
# options to map record type names, account for preceding parameters, remove initial
|
|
264
|
+
# parameters from the signature, or force the first to be a void*.
|
|
265
|
+
def func_param_text(func,type_map,rec_mode="",prior=False,clip=0,first_void=False):
|
|
266
|
+
param_list = fn_arg_ano_list(func)
|
|
267
|
+
|
|
268
|
+
param_list = param_list[clip:]
|
|
269
|
+
|
|
270
|
+
if first_void:
|
|
271
|
+
param_list = param_list[1:]
|
|
272
|
+
param_list = ["void*"] + [ map_type_name(type_map,kind,rec_mode=rec_mode) for kind in param_list ]
|
|
273
|
+
else:
|
|
274
|
+
param_list = [ map_type_name(type_map,kind,rec_mode=rec_mode) for kind in param_list ]
|
|
275
|
+
|
|
276
|
+
param_text = ", ".join([ kind+" fn_param_"+str(idx+clip+1) for (idx,kind) in enumerate(param_list)])
|
|
277
|
+
if len(param_list) > 0 and prior:
|
|
278
|
+
param_text = ", " + param_text
|
|
279
|
+
return param_text
|
|
280
|
+
|
|
281
|
+
|
|
282
|
+
# Returns the CUDA/C++ text used as the argument list for a call to the input function,
|
|
283
|
+
# with various options to cast/deref/getadr values, account for preceding parameters, and
|
|
284
|
+
# remove initial parameters from the signature.
|
|
285
|
+
def func_arg_text(func,type_map,rec_mode="",prior=False,clip=0):
|
|
286
|
+
param_list = fn_arg_ano_list(func)
|
|
287
|
+
param_list = param_list[clip:]
|
|
288
|
+
|
|
289
|
+
arg_text = ""
|
|
290
|
+
if len(param_list) > 0 and prior:
|
|
291
|
+
arg_text = ", "
|
|
292
|
+
for (idx,kind) in enumerate(param_list):
|
|
293
|
+
if idx != 0:
|
|
294
|
+
arg_text += ", "
|
|
295
|
+
if isinstance(kind,numba.types.Record):
|
|
296
|
+
if rec_mode == "deref":
|
|
297
|
+
arg_text += "*"
|
|
298
|
+
elif rec_mode == "adrof":
|
|
299
|
+
arg_text += "&"
|
|
300
|
+
elif rec_mode == "cast_deref":
|
|
301
|
+
arg_text += "*("+map_type_name(type_map,kind,rec_mode="ptr")+")"
|
|
302
|
+
arg_text += "fn_param_"+str(idx+clip+1)
|
|
303
|
+
return arg_text
|
|
304
|
+
|
|
305
|
+
|
|
306
|
+
|
|
307
|
+
# Returns the CUDA/C++ text representing the input function as a Harmonize async template
|
|
308
|
+
# function. The wrapper type that surrounds such templates is handled in other functions.
|
|
309
|
+
def harm_template_func(func,template_name,function_map,type_map,inline,suffix,base=False):
|
|
310
|
+
|
|
311
|
+
|
|
312
|
+
return_type = "void"
|
|
313
|
+
if 'return' in func.__annotations__:
|
|
314
|
+
return_type = map_type_name(type_map, func.__annotations__['return'])
|
|
315
|
+
|
|
316
|
+
|
|
317
|
+
param_text = func_param_text(func,type_map,prior=True,clip=1,first_void=False)
|
|
318
|
+
arg_text = func_arg_text (func,type_map,rec_mode="adrof",prior=True,clip=1)
|
|
319
|
+
|
|
320
|
+
if base:
|
|
321
|
+
param_text = ""
|
|
322
|
+
arg_text = ""
|
|
323
|
+
|
|
324
|
+
|
|
325
|
+
code = "\ttemplate<typename PROGRAM>\n" \
|
|
326
|
+
+ "\t__device__ static " \
|
|
327
|
+
+ return_type+" "+template_name+"(PROGRAM prog" + param_text + ") {\n"
|
|
328
|
+
# + "printf(\""+template_name+"\");\n"
|
|
329
|
+
|
|
330
|
+
if return_type != "void":
|
|
331
|
+
code += "\t\t"+return_type+" result;\n"
|
|
332
|
+
code += "\t\t"+return_type+" *fn_param_0 = &result;\n"
|
|
333
|
+
else:
|
|
334
|
+
code += "\t\tint dummy_void_result = 0;\n"
|
|
335
|
+
code += "\t\tint *fn_param_0 = &dummy_void_result;\n"
|
|
336
|
+
|
|
337
|
+
if inline:
|
|
338
|
+
code += inlined_device_ir(func,function_map,type_map)
|
|
339
|
+
else:
|
|
340
|
+
code += f"\t\tint return_status = _{func.__name__}_{suffix}(fn_param_0, &prog"+arg_text+");\n"
|
|
341
|
+
code += f"\t\tif (return_status!=0) printf(\"Warning: Async function returned a non-zero exit code: %d\\n\",return_status);"
|
|
342
|
+
pass
|
|
343
|
+
|
|
344
|
+
if return_type != "void":
|
|
345
|
+
code += "\t\treturn result;\n"
|
|
346
|
+
code += "\t}\n"
|
|
347
|
+
|
|
348
|
+
return code
|
|
349
|
+
|
|
350
|
+
|
|
351
|
+
# Returns the input string in pascal case
|
|
352
|
+
def pascal_case(name):
|
|
353
|
+
return name.replace("_", " ").title().replace(" ", "")
|
|
354
|
+
|
|
355
|
+
|
|
356
|
+
# Returns the CUDA/C++ text for a Harmonize async function that would map onto the
|
|
357
|
+
# input function.
|
|
358
|
+
def harm_async_func(func, function_map, type_map,inline,suffix):
|
|
359
|
+
return_type = "void"
|
|
360
|
+
if 'return' in func.__annotations__:
|
|
361
|
+
return_type = map_type_name(type_map, func.__annotations__['return'])
|
|
362
|
+
func_name = str(func.__name__)
|
|
363
|
+
|
|
364
|
+
struct_name = pascal_case(func_name)
|
|
365
|
+
param_list = fn_arg_ano_list(func)[1:]
|
|
366
|
+
param_list = [ map_type_name(type_map,kind) for kind in param_list ]
|
|
367
|
+
param_text = ", ".join(param_list)
|
|
368
|
+
|
|
369
|
+
code = "struct " + struct_name + " {\n" \
|
|
370
|
+
+ "\tusing Type = " + return_type + "(*)(" + param_text + ");\n" \
|
|
371
|
+
+ harm_template_func(func,"eval",function_map,type_map,inline,suffix) \
|
|
372
|
+
+ "};\n"
|
|
373
|
+
|
|
374
|
+
return code
|
|
375
|
+
|
|
376
|
+
|
|
377
|
+
|
|
378
|
+
# Returns the address of the input device array
|
|
379
|
+
def cuda_adr_of(array):
|
|
380
|
+
return array.__cuda_array_interface__['data'][0]
|
|
381
|
+
|
|
382
|
+
|
|
383
|
+
|
|
384
|
+
|
|
385
|
+
def find_bundle_triples(bundle_file_name):
|
|
386
|
+
file = open(bundle_file_name,"rb")
|
|
387
|
+
data = file.read()
|
|
388
|
+
magic = struct.unpack_from("24sQ",data,offset=0)
|
|
389
|
+
if magic[0].decode('utf-8') != r"__CLANG_OFFLOAD_BUNDLE__":
|
|
390
|
+
raise RuntimeError(f"Invalid clang bundle file '{bundle_file_name}' - missing magic string.")
|
|
391
|
+
|
|
392
|
+
count = magic[1]
|
|
393
|
+
|
|
394
|
+
if count != 2:
|
|
395
|
+
raise RuntimeError(f"Invalid clang bundle file '{bundle_file_name}' - does not contain exactly 2 entries.")
|
|
396
|
+
|
|
397
|
+
gpu_offset = 32
|
|
398
|
+
gpu_header = struct.unpack_from("QQQ",data,gpu_offset)
|
|
399
|
+
gpu_triple = struct.unpack_from(f"{gpu_header[2]}s",data,gpu_offset+24)
|
|
400
|
+
|
|
401
|
+
cpu_offset = gpu_offset + 24 + gpu_header[2]
|
|
402
|
+
cpu_header = struct.unpack_from("QQQ",data,cpu_offset)
|
|
403
|
+
cpu_triple = struct.unpack_from(f"{cpu_header[2]}s",data,cpu_offset+24)
|
|
404
|
+
|
|
405
|
+
gpu_triple = gpu_triple[0].decode('utf-8').rstrip('-')
|
|
406
|
+
cpu_triple = cpu_triple[0].decode('utf-8').rstrip('-')
|
|
407
|
+
|
|
408
|
+
return gpu_triple, cpu_triple
|
|
409
|
+
|