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/pointer.py ADDED
@@ -0,0 +1,108 @@
1
+ from numba import njit, jit, objmode, literal_unroll, types
2
+ from numba.extending import intrinsic
3
+ import numba as nb
4
+
5
+ import cffi
6
+ ffi = cffi.FFI()
7
+
8
+ from .codegen import generate_uuid
9
+
10
+ # =============================================================================
11
+ # uintp/voidptr casters
12
+ # =============================================================================
13
+
14
+
15
+ @intrinsic
16
+ def cast_any_to_voidptr(typingctx, src):
17
+ # create the expected type signature
18
+ result_type = types.voidptr
19
+ sig = result_type(src)
20
+
21
+ # defines the custom code generation
22
+ def codegen(context, builder, signature, args):
23
+ # llvm IRBuilder code here
24
+ [src] = args
25
+ rtype = signature.return_type
26
+ llrtype = context.get_value_type(rtype)
27
+ return builder.bitcast(src, llrtype)
28
+
29
+ return sig, codegen
30
+
31
+
32
+ @intrinsic
33
+ def cast_uintp_to_voidptr(typingctx, src):
34
+ # check for accepted types
35
+ if isinstance(src, types.Integer):
36
+ # create the expected type signature
37
+ result_type = types.voidptr
38
+ sig = result_type(types.uintp)
39
+
40
+ # defines the custom code generation
41
+ def codegen(context, builder, signature, args):
42
+ # llvm IRBuilder code here
43
+ [src] = args
44
+ rtype = signature.return_type
45
+ llrtype = context.get_value_type(rtype)
46
+ return builder.inttoptr(src, llrtype)
47
+
48
+ return sig, codegen
49
+
50
+
51
+ @intrinsic
52
+ def cast_voidptr_to_uintp(typingctx, src):
53
+ # check for accepted types
54
+ if isinstance(src, types.RawPointer):
55
+ # create the expected type signature
56
+ result_type = types.uintp
57
+ sig = result_type(types.voidptr)
58
+
59
+ # defines the custom code generation
60
+ def codegen(context, builder, signature, args):
61
+ # llvm IRBuilder code here
62
+ [src] = args
63
+ rtype = signature.return_type
64
+ llrtype = context.get_value_type(rtype)
65
+ return builder.ptrtoint(src, llrtype)
66
+
67
+ return sig, codegen
68
+
69
+ @njit()
70
+ def uintp_to_voidptr(value):
71
+ val = nb.uintp(value)
72
+ return cast_uintp_to_voidptr(val)
73
+
74
+ @njit()
75
+ def voidptr_to_uintp(value):
76
+ return cast_voidptr_to_uintp(value)
77
+
78
+
79
+ @njit()
80
+ def into_voidptr(value):
81
+ return into_voidptr_python(value)
82
+
83
+ def into_voidptr_python(value):
84
+ raise RuntimeError("`into_voidptr` is only supported in nopython mode.")
85
+
86
+ @nb.extending.overload(into_voidptr_python)
87
+ def into_voidptr_overload(value):
88
+
89
+ if isinstance(value,nb.types.Array) :
90
+ def impl(value):
91
+ ptr = (ffi.from_buffer(value))
92
+ vptr = cast_any_to_voidptr(ptr)
93
+ return vptr
94
+ return impl
95
+ elif isinstance(value,nb.types.CPointer) :
96
+ def impl(value):
97
+ return cast_any_to_voidptr(value)
98
+ return impl
99
+ elif isinstance(value,nb.types.Integer) :
100
+ def impl(value):
101
+ return cast_uintp_to_voidptr(value)
102
+ return impl
103
+ else :
104
+ raise RuntimeError(f"`into_voidptr` is not supported for type '{value}'")
105
+
106
+
107
+
108
+
harmonize/prim.py ADDED
@@ -0,0 +1,59 @@
1
+ from numba.types import int8, int16,int32,int64,uint8,uint16,uint32,uint64,float32,float64
2
+ from numba.types import uintp as usize
3
+ from numba.types import voidptr as vp
4
+ from numba.types import void
5
+
6
+ prim_info = {
7
+ int8: {
8
+ "py_name": "int8",
9
+ "cpp_name": "char",
10
+ "fmt_name": "%hhd"
11
+ },
12
+ int16: {
13
+ "py_name": "int16",
14
+ "cpp_name": "short",
15
+ "fmt_name": "%hd"
16
+ },
17
+ int32: {
18
+ "py_name": "int32",
19
+ "cpp_name": "int",
20
+ "fmt_name": "%d"
21
+ },
22
+ int64: {
23
+ "py_name": "int64",
24
+ "cpp_name": "long long int",
25
+ "fmt_name": "%lld"
26
+ },
27
+ uint8: {
28
+ "py_name": "uint8",
29
+ "cpp_name": "unsigned char",
30
+ "fmt_name": "%hhu"
31
+ },
32
+ uint16: {
33
+ "py_name": "uint16",
34
+ "cpp_name": "unsigned short",
35
+ "fmt_name": "%hu"
36
+ },
37
+ uint32: {
38
+ "py_name": "uint32",
39
+ "cpp_name": "unsigned int",
40
+ "fmt_name": "%u"
41
+ },
42
+ uint64: {
43
+ "py_name": "uint64",
44
+ "cpp_name": "unsigned long long int",
45
+ "fmt_name": "%llu"
46
+ },
47
+ float32: {
48
+ "py_name": "float32",
49
+ "cpp_name": "float",
50
+ "fmt_name": "%f"
51
+ },
52
+ float64: {
53
+ "py_name": "float64",
54
+ "cpp_name": "double",
55
+ "fmt_name": "%f"
56
+ },
57
+ }
58
+
59
+
harmonize/printing.py ADDED
@@ -0,0 +1,104 @@
1
+ import numba
2
+ import llvmlite
3
+
4
+ import harmonize.config as config
5
+
6
+ from .templates import print_template
7
+ from .prim import *
8
+
9
+
10
+ print_specialization_registry = {}
11
+ print_extern_registry = {}
12
+
13
+
14
+ @numba.njit
15
+ def print_formatted(value):
16
+ print_formatted_inner(value)
17
+
18
+
19
+
20
+ def print_formatted_inner(value):
21
+ print("(",value,")")
22
+
23
+
24
+
25
+ @numba.extending.overload(print_formatted_inner,target='cpu')
26
+ def cpu_print_formatted_overload(value):
27
+
28
+ print(value,flush=True)
29
+
30
+ def impl(value):
31
+ print("(",value,")")
32
+
33
+ return impl
34
+
35
+
36
+ # WIP Printing function
37
+ @numba.extending.overload(print_formatted_inner,target='gpu')
38
+ def gpu_print_formatted_overload(value):
39
+
40
+ kind = None
41
+ if isinstance(value,numba.types.Literal):
42
+ kind = value.literal_type
43
+ else:
44
+ kind = value
45
+
46
+ if not (kind in prim_info):
47
+ print(f"{kind} not in prim_info",flush=True)
48
+ return None
49
+
50
+
51
+ if not (kind in print_specialization_registry):
52
+ print(f"{kind} not in spec registry",flush=True)
53
+ py_name = prim_info[kind]["py_name"]
54
+ cpp_name = prim_info[kind]["cpp_name"]
55
+ fmt_name = prim_info[kind]["fmt_name"]
56
+ print_specialization_registry[kind] = {
57
+ "type_sig" : py_name,
58
+ "args" : f"{cpp_name} v0",
59
+ "arg_vals" : "v0",
60
+ "format_str" : fmt_name
61
+ }
62
+ print(print_specialization_registry[kind])
63
+
64
+ sig = numba.core.typing.signature
65
+ ext_fn = numba.types.ExternalFunction
66
+
67
+ signature = sig(void,kind)
68
+ #print(signature)
69
+ py_name = prim_info[kind]["py_name"]
70
+ if config.CUDA_AVAILABLE:
71
+ implementation = numba.cuda.declare_device(f"harmonize_print_{py_name}", signature)
72
+ elif config.ROCM_AVAILABLE:
73
+ implementation = numba.hip.declare_device(f"harmonize_print_{py_name}", signature)
74
+ else:
75
+ errors.no_platforms()
76
+ #implementation = ext_fn(f"harmonize_print_{py_name}", signature)
77
+ print_extern_registry[kind] = implementation
78
+ else:
79
+ print(f"{kind} already in spec registry",flush=True)
80
+
81
+ inner_impl = print_extern_registry[kind]
82
+ print(inner_impl,flush=True)
83
+
84
+ def impl(value):
85
+ inner_impl(value)
86
+
87
+ print(impl,flush=True)
88
+
89
+ return impl
90
+
91
+
92
+ def generate_print_code():
93
+ text = ""
94
+ for pair in print_specialization_registry.items():
95
+ kind, info = pair
96
+ text += print_template.format(
97
+ type_sig = info["type_sig"],
98
+ args = info["args"],
99
+ arg_vals = info["arg_vals"],
100
+ format_str = info["format_str"],
101
+ )
102
+ return text
103
+
104
+
@@ -0,0 +1,48 @@
1
+ """Deprecated alias for the modules now directly in `harmonize`.
2
+
3
+ Harmonize's Python modules used to live in `harmonize.python`
4
+ (e.g. `harmonize.python.config`). They are now `harmonize.config` and so on.
5
+ The old names keep working, with a `DeprecationWarning`, and will be removed in
6
+ a future release.
7
+
8
+ Each `harmonize.python.<module>` is the same module object as
9
+ `harmonize.<module>`, not a copy, so settings changed through either name
10
+ (e.g. `config.DEBUG`) are shared.
11
+ """
12
+
13
+ import importlib
14
+ import sys
15
+ import warnings
16
+
17
+ _MODULES = (
18
+ "adapt",
19
+ "array",
20
+ "atomics",
21
+ "codegen",
22
+ "config",
23
+ "diagnostics",
24
+ "errors",
25
+ "logging",
26
+ "pointer",
27
+ "prim",
28
+ "printing",
29
+ "runtime",
30
+ "templates",
31
+ "timing",
32
+ "types",
33
+ )
34
+
35
+ warnings.warn(
36
+ "harmonize.python is deprecated; its modules are now directly in harmonize "
37
+ "(e.g. harmonize.config instead of harmonize.python.config)",
38
+ DeprecationWarning,
39
+ stacklevel=2,
40
+ )
41
+
42
+ # Registering the modules under their old names makes both
43
+ # `import harmonize.python.config` and `from harmonize.python import config` work.
44
+ for _name in _MODULES:
45
+ _module = importlib.import_module(f"harmonize.{_name}")
46
+ sys.modules[f"{__name__}.{_name}"] = _module
47
+ globals()[_name] = _module
48
+ del _name, _module