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 ADDED
@@ -0,0 +1,65 @@
1
+ """Harmonize: an asynchronous GPU execution framework.
2
+
3
+ The names below are Harmonize's public Python API. The C++ headers are in the
4
+ `cpp` directory of this package; see `get_include()`.
5
+ """
6
+
7
+ from importlib.metadata import PackageNotFoundError, version as _version
8
+ from os.path import dirname, join
9
+
10
+ try:
11
+ __version__ = _version("harmonize-python")
12
+ except PackageNotFoundError: # imported from a source tree that is not installed
13
+ __version__ = "unknown"
14
+
15
+ import harmonize.config as config
16
+ import harmonize.pointer as pointer
17
+
18
+ from harmonize.runtime import (
19
+ RuntimeSpec,
20
+ )
21
+
22
+ from harmonize.atomics import (
23
+ array_atomic_add,
24
+ array_atomic_max,
25
+ )
26
+
27
+ from harmonize.printing import print_formatted
28
+ from harmonize.timing import get_wall_clock
29
+
30
+ from harmonize.array import (
31
+ local_array,
32
+ array_from_ptr,
33
+ alloc_device_bytes,
34
+ alloc_managed_bytes,
35
+ free_device_bytes,
36
+ memcpy_device_to_host,
37
+ memcpy_host_to_device,
38
+ )
39
+
40
+
41
+ def get_include():
42
+ """Return the directory containing Harmonize's C++ headers.
43
+
44
+ Add it to a C++ build's include path to use `#include "harmonize.h"`.
45
+ """
46
+ return join(dirname(__file__), "cpp")
47
+
48
+
49
+ __all__ = [
50
+ "config",
51
+ "pointer",
52
+ "RuntimeSpec",
53
+ "array_atomic_add",
54
+ "array_atomic_max",
55
+ "print_formatted",
56
+ "get_wall_clock",
57
+ "local_array",
58
+ "array_from_ptr",
59
+ "alloc_device_bytes",
60
+ "alloc_managed_bytes",
61
+ "free_device_bytes",
62
+ "memcpy_device_to_host",
63
+ "memcpy_host_to_device",
64
+ "get_include",
65
+ ]
harmonize/__main__.py ADDED
@@ -0,0 +1,39 @@
1
+ """Command-line entry point: `python -m harmonize {info,check}`."""
2
+
3
+ import argparse
4
+ import sys
5
+
6
+ from harmonize import diagnostics
7
+
8
+
9
+ def main(argv=None):
10
+ parser = argparse.ArgumentParser(
11
+ prog="python -m harmonize",
12
+ description="Inspect Harmonize's GPU platform setup.",
13
+ )
14
+ commands = parser.add_subparsers(dest="command", required=True)
15
+ commands.add_parser(
16
+ "info", help="Report the detected GPU platform, toolchain, and devices."
17
+ )
18
+ commands.add_parser(
19
+ "check",
20
+ help="Report problems that would stop Harmonize from building GPU programs; "
21
+ "exits non-zero if any are found.",
22
+ )
23
+ args = parser.parse_args(argv)
24
+
25
+ info = diagnostics.collect_info()
26
+ if args.command == "info":
27
+ print(diagnostics.format_info(info))
28
+ return 0
29
+
30
+ findings = diagnostics.run_checks(info)
31
+ print(diagnostics.format_findings(findings))
32
+ if any(finding.level == "error" for finding in findings):
33
+ return 1
34
+ else:
35
+ return 0
36
+
37
+
38
+ if __name__ == "__main__":
39
+ sys.exit(main())
harmonize/adapt.py ADDED
@@ -0,0 +1,105 @@
1
+ import llvmlite
2
+ from numba.extending import intrinsic
3
+ from .types import *
4
+
5
+ # =============================================================================
6
+ # Atomics
7
+ # =============================================================================
8
+
9
+ ATOMIC_FUNCTION_ROSTER = {
10
+ "add" : [i32,u32,u64,f32,f64],
11
+ "sub" : [i32,u32],
12
+ "exch": [i32,u32,u64,f32],
13
+ "min" : [i32,u32,u64,i64],
14
+ "max" : [i32,u32,u64,i64],
15
+ "inc" : [u32],
16
+ "dec" : [u32],
17
+ "CAS" : [i32,u32,u64,u16],
18
+ "and" : [i32,u32,u64],
19
+ "or" : [i32,u32,u64],
20
+ "xor" : [i32,u32,u64]
21
+ }
22
+
23
+ def build_rmw_atomic_intrinsic(operation):
24
+
25
+ # Defines intrinisic over
26
+ def atomicIntrinsic(typingctx,target,index,value):
27
+
28
+ # Atomic operations must be performed upon something in
29
+ # memory (pointed by the first argument)
30
+
31
+ if not isinstance(target, numba.types.Array):
32
+ raise Exception("First argument is not an array type.")
33
+ return None
34
+
35
+ # The base type of the target pointer must match the second
36
+ # argument
37
+ is_lit_int = False
38
+ if isinstance(value,numba.types.Literal) and numba.types.unliteral(value) == numba.types.Integer:
39
+ is_lit_int = True
40
+ if (value != target.dtype):
41
+ raise Exception(f"Type of value argument ({value}) does not match the element type of the array argument ({target.dtype})")
42
+ return None
43
+
44
+ # The type must be supported by the function
45
+ if not target.dtype in ATOMIC_FUNCTION_ROSTER[operation]:
46
+ raise Exception(f"Atomic {operation} not supported for values of type {target.dtype}")
47
+ return None
48
+
49
+ # create the expected type signature
50
+ result_type = target.dtype
51
+ sig = result_type(target,usize,value)
52
+
53
+ # defines the custom code generation
54
+ def codegen(context, builder, signature, args):
55
+ # llvm IRBuilder code here
56
+ [target,index,value] = args
57
+ zero = llvmlite.ir.Constant(llvmlite.ir.IntType(usize.bitwidth),0)
58
+ four = llvmlite.ir.Constant(llvmlite.ir.IntType(usize.bitwidth),5)
59
+ ptr = builder.gep(target, [four,four,zero], inbounds=False)
60
+ return builder.atomic_rmw(operation, ptr, value, 'seq_cst')
61
+
62
+ return sig, codegen
63
+
64
+ return atomicIntrinsic
65
+
66
+ @intrinsic
67
+ def atomic_add(typingctx,target,index,value):
68
+ return build_rmw_atomic_intrinsic("add")(typingctx,target,index,value)
69
+
70
+ #@intrinsic
71
+ def atomic_sub(typingctx,target,index,value):
72
+ return build_rmw_atomic_intrinsic("sub")(typingctx,target,index,value)
73
+
74
+ #@intrinsic
75
+ def atomic_exch(typingctx,target,index,value):
76
+ return build_rmw_atomic_intrinsic("exch")(typingctx,target,index,value)
77
+
78
+ #@intrinsic
79
+ def atomic_min(typingctx,target,index,value):
80
+ return build_rmw_atomic_intrinsic("min")(typingctx,target,index,value)
81
+
82
+ #@intrinsic
83
+ def atomic_max(typingctx,target,index,value):
84
+ return build_rmw_atomic_intrinsic("max")(typingctx,target,index,value)
85
+
86
+ #@intrinsic
87
+ def atomic_inc(typingctx,target,index,value):
88
+ return build_rmw_atomic_intrinsic("inc")(typingctx,target,index,value)
89
+
90
+ #@intrinsic
91
+ def atomic_dec(typingctx,target,index,value):
92
+ return build_rmw_atomic_intrinsic("dec")(typingctx,target,index,value)
93
+
94
+ #@intrinsic
95
+ def atomic_and(typingctx,target,index,value):
96
+ return build_rmw_atomic_intrinsic("and")(typingctx,target,index,value)
97
+
98
+ #@intrinsic
99
+ def atomic_or(typingctx,target,index,value):
100
+ return build_rmw_atomic_intrinsic( "or")(typingctx,target,index,value)
101
+
102
+ #@intrinsic
103
+ def atomic_xor(typingctx,target,index,value):
104
+ return build_rmw_atomic_intrinsic("xor")(typingctx,target,index,value)
105
+