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/config.py ADDED
@@ -0,0 +1,220 @@
1
+ import numba
2
+ import shutil
3
+ import subprocess
4
+
5
+
6
+ from os import environ, path
7
+ from enum import Enum
8
+ from os.path import dirname, abspath
9
+ from harmonize import errors, logging
10
+
11
+ class ShouldCompile(Enum):
12
+ ALWAYS = 1
13
+ NEVER = 2
14
+
15
+
16
+ DEBUG = False
17
+ VERBOSE = False
18
+ INTERNAL_DEBUG = False
19
+ ERROR_PRINT = True
20
+ COLOR_LOG = True
21
+
22
+ NONE_AVAILABLE = True
23
+ ROCM_AVAILABLE = None
24
+ CUDA_AVAILABLE = None
25
+ SHOULD_COMPILE = None
26
+
27
+
28
+ # The GPU platform is chosen once, at import. Set HARMONIZE_PLATFORM to "cuda"
29
+ # or "rocm" to require a platform; the default, "auto", prefers ROCm when its
30
+ # Numba backend is installed and falls back to CUDA.
31
+ PLATFORM_ENV_VAR = "HARMONIZE_PLATFORM"
32
+ PLATFORM_CHOICES = ("auto", "cuda", "rocm")
33
+ PLATFORM_REQUEST = environ.get(PLATFORM_ENV_VAR, "auto").strip().lower() or "auto"
34
+ if PLATFORM_REQUEST not in PLATFORM_CHOICES:
35
+ raise ValueError(
36
+ f"{PLATFORM_ENV_VAR}={PLATFORM_REQUEST!r} is not one of {PLATFORM_CHOICES}."
37
+ )
38
+
39
+ # Why each Numba GPU backend could not be imported, for diagnostics
40
+ PLATFORM_IMPORT_ERRORS = {}
41
+
42
+
43
+ if PLATFORM_REQUEST in ("auto", "rocm"):
44
+ try:
45
+ import numba.hip as hip
46
+ ROCM_AVAILABLE = True
47
+ NONE_AVAILABLE = False
48
+ logging.verbose_print(f"ROCM_PLATFORM detected. Proceeding under ROCM.")
49
+ except ImportError as error:
50
+ ROCM_AVAILABLE = False
51
+ PLATFORM_IMPORT_ERRORS["rocm"] = str(error)
52
+ else:
53
+ ROCM_AVAILABLE = False
54
+
55
+
56
+ # The HIP version of Numba "has" a cuda module,
57
+ # but doesn't support the things normal Numba does
58
+ if not ROCM_AVAILABLE and PLATFORM_REQUEST in ("auto", "cuda"):
59
+ try :
60
+ import numba.cuda as cuda
61
+ CUDA_AVAILABLE = True
62
+ NONE_AVAILABLE = False
63
+ logging.verbose_print(f"CUDA_PLATFORM detected. Proceeding under CUDA.")
64
+ except ImportError as error:
65
+ CUDA_AVAILABLE = False
66
+ PLATFORM_IMPORT_ERRORS["cuda"] = str(error)
67
+ else:
68
+ CUDA_AVAILABLE = False
69
+
70
+
71
+ def require_platform():
72
+ """Raise a descriptive error if no GPU platform was selected at import.
73
+
74
+ This is deferred to the point where GPU code is built, so harmonize can be
75
+ imported (for example by `python -m harmonize check`) on machines without
76
+ a usable GPU backend.
77
+ """
78
+ if NONE_AVAILABLE:
79
+ errors.no_platforms(PLATFORM_REQUEST, PLATFORM_IMPORT_ERRORS)
80
+
81
+
82
+ def set_debug(debug):
83
+ global DEBUG
84
+ DEBUG = debug
85
+
86
+ def set_verbose(verbose):
87
+ global VERBOSE
88
+ VERBOSE = verbose
89
+
90
+ def set_internal_debug(internal_debug):
91
+ global INTERNAL_DEBUG
92
+ INTERNAL_DEBUG = internal_debug
93
+
94
+
95
+ class GPUPlatform(Enum):
96
+ CUDA = 1
97
+ ROCM = 2
98
+
99
+ CUDA_PATH = None
100
+ ROCM_PATH = None
101
+
102
+ # The installed harmonize package; its C++ headers are in cpp/
103
+ HARMONIZE_ROOT_DIR = dirname(abspath(__file__))
104
+ HARMONIZE_ROOT_HEADER = HARMONIZE_ROOT_DIR+"/cpp/harmonize.h"
105
+
106
+
107
+ def set_platform(platform):
108
+ global GPU_PLATFORM
109
+ GPU_PLATFORM = platform
110
+ logging.verbose_print(f"GPU_PLATFORM set to {GPU_PLATFORM}")
111
+
112
+ def set_cuda_path(path):
113
+ global CUDA_PATH
114
+ CUDA_PATH = path
115
+ if path == None:
116
+ logging.verbose_print(f"CUDA_PATH cleared to default")
117
+ else:
118
+ logging.verbose_print(f"CUDA_PATH set to {CUDA_PATH}")
119
+
120
+ def set_rocm_path(path):
121
+ global ROCM_PATH
122
+ ROCM_PATH = path
123
+ if path == None:
124
+ logging.verbose_print(f"ROCM_PATH cleared to default")
125
+ else:
126
+ logging.verbose_print(f"ROCM_PATH set to {ROCM_PATH}")
127
+
128
+
129
+ def should_compile(mode):
130
+ global SHOULD_COMPILE
131
+ SHOULD_COMPILE = mode
132
+
133
+ def compilation_gate(auto):
134
+ if SHOULD_COMPILE == ShouldCompile.ALWAYS:
135
+ return True
136
+ elif SHOULD_COMPILE == ShouldCompile.NEVER:
137
+ return False
138
+ else:
139
+ return auto
140
+
141
+
142
+ def check_rocm_path(quiet=False):
143
+
144
+ dep_paths = [
145
+ hipcc_path(),
146
+ hipcc_clang_offload_bundler_path(),
147
+ hipcc_llvm_link_path(),
148
+ hipcc_llvm_as_path(),
149
+ ]
150
+
151
+ fail = False
152
+ for dep in dep_paths:
153
+ if not path.isfile(dep):
154
+ if not quiet:
155
+ logging.error_print(f"Path '{dep}' does not correspond to a file.")
156
+ fail = True
157
+ if fail and (not quiet):
158
+ raise RuntimeError("Some required ROCM dependencies missing.")
159
+
160
+ return not fail
161
+
162
+
163
+ def try_autoset_rocm_path():
164
+ path = shutil.which("hipcc")
165
+ if path is None:
166
+ return
167
+
168
+ tail = "/bin/hipcc"
169
+ if not path.endswith(tail):
170
+ return
171
+ logging.verbose_print(f"Found hipcc located at '{path}'")
172
+
173
+ path = path[:-len(tail)]
174
+ set_rocm_path(path)
175
+ if not check_rocm_path(quiet=True):
176
+ set_rocm_path(None)
177
+
178
+ def nvcc_path():
179
+ if CUDA_PATH == None:
180
+ return "nvcc"
181
+ else:
182
+ return CUDA_PATH + "/bin/nvcc"
183
+
184
+ def hipcc_path():
185
+ if ROCM_PATH == None:
186
+ return "hipcc"
187
+ else:
188
+ return ROCM_PATH + "/bin/hipcc"
189
+
190
+ def hipcc_clang_offload_bundler_path():
191
+ if ROCM_PATH == None:
192
+ return "clang-offload-bundler"
193
+ else:
194
+ return ROCM_PATH + "/llvm/bin/clang-offload-bundler"
195
+
196
+ def hipcc_llvm_link_path():
197
+ if ROCM_PATH == None:
198
+ return "llvm-link"
199
+ else:
200
+ return ROCM_PATH + "/llvm/bin/llvm-link"
201
+
202
+ def hipcc_llvm_as_path():
203
+ if ROCM_PATH == None:
204
+ return "llvm-as"
205
+ else:
206
+ return ROCM_PATH + "/llvm/bin/llvm-as"
207
+
208
+
209
+ # Uses nvidia-smi to query the compute level of the GPUs on the system. This
210
+ # compute level is what is used for compling PTX.
211
+ def native_gpu_arch(platform):
212
+ if platform == GPUPlatform.ROCM:
213
+ return ""
214
+ else:
215
+ capability = cuda.get_current_device().compute_capability
216
+ output = f"{capability[0]}{capability[1]}"
217
+ return output
218
+
219
+ if ROCM_AVAILABLE:
220
+ try_autoset_rocm_path()
@@ -0,0 +1,64 @@
1
+
2
+
3
+ #ifndef HARMONIZE_ASYNC_FUTURE
4
+ #define HARMONIZE_ASYNC_FUTURE
5
+
6
+ //! A `Future` is a value tied to a barrier.
7
+ template<typename TYPE, typename BARRIER>
8
+ struct Future
9
+ {
10
+ using BarrierType = BARRIER;
11
+ using Type = TYPE;
12
+
13
+ BarrierType barrier;
14
+ Type data;
15
+
16
+ Future<TYPE,BARRIER>() = default;
17
+
18
+ __host__ __device__ Future<TYPE,BARRIER>(unsigned int semaphore_value)
19
+ : barrier(semaphore_value)
20
+ {}
21
+
22
+ template<typename PROGRAM, typename OP_TYPE>
23
+ __device__ void await(PROGRAM program, Promise<OP_TYPE> promise) {
24
+ barrier.await(program,promise);
25
+ }
26
+
27
+
28
+ template<typename PROGRAM>
29
+ __device__ void fulfill(PROGRAM program) {
30
+ barrier.sub_semaphore(program,1);
31
+ }
32
+
33
+ };
34
+
35
+
36
+
37
+ //! The `ReturnOp` operation is used to resolve dependencies between barriers and peform
38
+ //! the data transfers required to fill in future values.
39
+ struct ReturnOp {
40
+
41
+ using Type = void(*)(TaggedSemaphore*,void*,void*,size_t);
42
+
43
+ template<typename PROGRAM>
44
+ __device__ void eval(TaggedSemaphore* sem,void* dst, void* src, size_t size) {
45
+
46
+ using UFuture = typename PROGRAM::UFuture;
47
+ using RFuture = typename PROGRAM::RFuture;
48
+
49
+ memcpy(dst,src,size);
50
+ __threadfence();
51
+ if(sem->tag == 0){
52
+ UFuture* unit_future = sem;
53
+ unit_future ->fulfill();
54
+ } else {
55
+ RFuture* remap_future = sem;
56
+ remap_future->fulfill();
57
+ }
58
+ }
59
+
60
+ };
61
+
62
+ #endif
63
+
64
+
@@ -0,0 +1,9 @@
1
+
2
+ #ifndef HARMONIZE_ASYNC
3
+ #define HARMONIZE_ASYNC
4
+
5
+ #include "promise.h"
6
+ #include "future.h"
7
+
8
+ #endif
9
+