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/runtime.py
ADDED
|
@@ -0,0 +1,1501 @@
|
|
|
1
|
+
import re
|
|
2
|
+
import sys
|
|
3
|
+
import numba
|
|
4
|
+
import inspect
|
|
5
|
+
import subprocess
|
|
6
|
+
|
|
7
|
+
import numpy as np
|
|
8
|
+
import cffi
|
|
9
|
+
ffi = cffi.FFI()
|
|
10
|
+
|
|
11
|
+
import harmonize.config as config
|
|
12
|
+
|
|
13
|
+
from os import makedirs, getcwd, path
|
|
14
|
+
from time import sleep
|
|
15
|
+
from numba import njit
|
|
16
|
+
from os.path import getmtime, exists, dirname, abspath
|
|
17
|
+
from llvmlite import binding
|
|
18
|
+
|
|
19
|
+
from .pointer import *
|
|
20
|
+
from .templates import *
|
|
21
|
+
from .array import generate_array_code
|
|
22
|
+
from .config import compilation_gate
|
|
23
|
+
from .atomics import atomic_op_info
|
|
24
|
+
from .prim import prim_info
|
|
25
|
+
from .printing import generate_print_code
|
|
26
|
+
from .timing import generate_clock_code
|
|
27
|
+
from .logging import verbose_print, debug_print, progress_print
|
|
28
|
+
from .codegen import (
|
|
29
|
+
pascal_case,
|
|
30
|
+
size_of,
|
|
31
|
+
alignment,
|
|
32
|
+
fn_sig,
|
|
33
|
+
fn_arg_ano_list,
|
|
34
|
+
func_arg_text,
|
|
35
|
+
func_param_text,
|
|
36
|
+
harm_async_func,
|
|
37
|
+
harm_template_func,
|
|
38
|
+
map_type_to_np,
|
|
39
|
+
map_type_name,
|
|
40
|
+
find_bundle_triples,
|
|
41
|
+
extern_device_ir,
|
|
42
|
+
declare_device,
|
|
43
|
+
generate_uuid,
|
|
44
|
+
)
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
# A base class representing all possible runtime types
|
|
48
|
+
class Runtime():
|
|
49
|
+
def __init__(self,spec,context,state,init_fn,exec_fn):
|
|
50
|
+
pass
|
|
51
|
+
|
|
52
|
+
def init(self,*args):
|
|
53
|
+
pass
|
|
54
|
+
|
|
55
|
+
def exec(self,*args):
|
|
56
|
+
pass
|
|
57
|
+
|
|
58
|
+
def load_state(self):
|
|
59
|
+
pass
|
|
60
|
+
|
|
61
|
+
def store_state(self):
|
|
62
|
+
pass
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
# The class representing the Async runtime type, which
|
|
67
|
+
# asynchronously schedules calls on-GPU and within a single kernel
|
|
68
|
+
class AsyncRuntime(Runtime):
|
|
69
|
+
|
|
70
|
+
def __init__(self,spec,context,state,fn,gpu_objects):
|
|
71
|
+
self.spec = spec
|
|
72
|
+
self.context = context
|
|
73
|
+
self.context_ptr = cuda_adr_of(context)
|
|
74
|
+
self.state = state
|
|
75
|
+
self.state_ptr = cuda_adr_of(state)
|
|
76
|
+
self.fn = fn
|
|
77
|
+
self.gpu_objects = gpu_objects
|
|
78
|
+
|
|
79
|
+
# Initializes the runtime using `wg_count` work groups
|
|
80
|
+
def init(self,wg_count=1):
|
|
81
|
+
self.fn["init_program"](self.context_ptr,self.state_ptr,wg_count,32)
|
|
82
|
+
|
|
83
|
+
# Executes the runtime through `cycle_count` iterations using `wg_count` work_groups
|
|
84
|
+
def exec(self,cycle_count,wg_count=1):
|
|
85
|
+
self.fn["exec_program"](self.context_ptr,self.state_ptr,cycle_count,wg_count,32)
|
|
86
|
+
|
|
87
|
+
# Loads the device state of the runtime, returning value normally and also through the
|
|
88
|
+
# supplied argument, if it is not None
|
|
89
|
+
def load_state(self,result = None):
|
|
90
|
+
res = self.state.copy_to_host(result)
|
|
91
|
+
return res
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
# Stores the input value into the device state
|
|
95
|
+
def store_state(self,state):
|
|
96
|
+
cpu_state = np.zeros((1,),self.spec.dev_state)
|
|
97
|
+
cpu_state[0] = state
|
|
98
|
+
return self.state.copy_to_device(cpu_state)
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
# The Harmonize implementation of standard event-based execution
|
|
102
|
+
class EventRuntime(Runtime):
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
def __init__(self,spec,context,state,fn,checkout,io_buffers):
|
|
106
|
+
self.spec = spec
|
|
107
|
+
self.context = context
|
|
108
|
+
self.context_ptr = cuda_adr_of(context)
|
|
109
|
+
self.state = state
|
|
110
|
+
self.state_ptr = cuda_adr_of(state)
|
|
111
|
+
self.fn = fn
|
|
112
|
+
self.checkout = checkout
|
|
113
|
+
self.io_buffers = io_buffers
|
|
114
|
+
|
|
115
|
+
# Used for debugging. Prints out the contents of the buffers used to store
|
|
116
|
+
# intermediate data
|
|
117
|
+
def io_summary(self):
|
|
118
|
+
for field in ["handle","data_a","data_b"]:
|
|
119
|
+
bufs = [ buf[field].copy_to_host() for buf in self.io_buffers ]
|
|
120
|
+
for buf in bufs:
|
|
121
|
+
print(buf)
|
|
122
|
+
return True
|
|
123
|
+
|
|
124
|
+
# Returns true if and only if the instance has halted, indicating that no
|
|
125
|
+
# work is left to be performed
|
|
126
|
+
def halted(self):
|
|
127
|
+
bufs = [ buf["handle"].copy_to_host()[0] for buf in self.io_buffers ]
|
|
128
|
+
for buf in bufs:
|
|
129
|
+
if buf["input_iter"]["limit"] != 0:
|
|
130
|
+
return False
|
|
131
|
+
return True
|
|
132
|
+
|
|
133
|
+
# Does nothing, since no on-GPU initialization is required for the runtime
|
|
134
|
+
def init(self,wg_count=1):
|
|
135
|
+
pass
|
|
136
|
+
|
|
137
|
+
# Executes the program, claiming events from the intermediate data buffers in
|
|
138
|
+
# `chunk_size`-sized units for each thread. Execution continues until the program
|
|
139
|
+
# is halted
|
|
140
|
+
def exec(self,chunk_size,wg_count=1):
|
|
141
|
+
has_halted = False
|
|
142
|
+
count = 0
|
|
143
|
+
while not has_halted:
|
|
144
|
+
self.exec_fn[wg_count,32](self.context_ptr,self.state_ptr,chunk_size)
|
|
145
|
+
has_halted = self.halted()
|
|
146
|
+
count += 1
|
|
147
|
+
|
|
148
|
+
# Loads the device state of the runtime, returning value normally and also through the
|
|
149
|
+
# supplied argument, if it is not None
|
|
150
|
+
def load_state(self,result = None):
|
|
151
|
+
res = self.state.copy_to_host(result)
|
|
152
|
+
return res
|
|
153
|
+
|
|
154
|
+
# Stores the input value into the device state
|
|
155
|
+
def store_state(self,state):
|
|
156
|
+
cpu_state = np.zeros((1,),self.spec.dev_state)
|
|
157
|
+
cpu_state[0] = state
|
|
158
|
+
return self.state.copy_to_device(cpu_state)
|
|
159
|
+
|
|
160
|
+
|
|
161
|
+
class ProgramField:
|
|
162
|
+
|
|
163
|
+
def __init__(self,label,path,offset,kind,is_pointer):
|
|
164
|
+
|
|
165
|
+
self.label = label
|
|
166
|
+
self.path = path
|
|
167
|
+
self.offset = offset
|
|
168
|
+
self.kind = kind
|
|
169
|
+
self.ext_kind = kind
|
|
170
|
+
self.size = size_of(kind)
|
|
171
|
+
self.is_pointer = is_pointer
|
|
172
|
+
self.is_array = False
|
|
173
|
+
debug_print(f"\n\nSize of '{label}' ---> {self.size}\n\n")
|
|
174
|
+
|
|
175
|
+
|
|
176
|
+
if (isinstance(kind,numba.core.types.npytypes.Array)) :
|
|
177
|
+
self.ext_kind = numba.types.voidptr
|
|
178
|
+
self.kind = numba.types.voidptr
|
|
179
|
+
self.is_array = True
|
|
180
|
+
#print(f"FOUND ARRAY: {self.label} = {self.kind}")
|
|
181
|
+
|
|
182
|
+
|
|
183
|
+
def prefix(self):
|
|
184
|
+
if self.is_pointer:
|
|
185
|
+
return ""
|
|
186
|
+
else:
|
|
187
|
+
return "&"
|
|
188
|
+
|
|
189
|
+
|
|
190
|
+
# Represents the specification for a specific program and its
|
|
191
|
+
# runtime meta-parameters
|
|
192
|
+
#class RuntimeSpec(numba.types.Type):
|
|
193
|
+
class RuntimeSpec():
|
|
194
|
+
|
|
195
|
+
gpu_platforms = set()
|
|
196
|
+
obj_set = set()
|
|
197
|
+
gpu_bc_set = set()
|
|
198
|
+
|
|
199
|
+
gpu_triple = None
|
|
200
|
+
|
|
201
|
+
registry = {}
|
|
202
|
+
|
|
203
|
+
kinds = [("Event","event"),("Async","async")]
|
|
204
|
+
gpu_arch = None
|
|
205
|
+
cache_path = "__harmonize_cache__/"
|
|
206
|
+
debug_flag = " -g "
|
|
207
|
+
dirty = False
|
|
208
|
+
|
|
209
|
+
def __init__(
|
|
210
|
+
self,
|
|
211
|
+
# The name of the program specification
|
|
212
|
+
spec_name,
|
|
213
|
+
# A triplet containing types which should be
|
|
214
|
+
# held per-device, per-group, and per-thread
|
|
215
|
+
state_spec,
|
|
216
|
+
# A triplet containing the functions that
|
|
217
|
+
# should be run to initialize/finalize states
|
|
218
|
+
# and to generate work for running programs
|
|
219
|
+
base_fns,
|
|
220
|
+
# A list of python functions that are to be
|
|
221
|
+
# included as async functions in the program
|
|
222
|
+
async_fns,
|
|
223
|
+
# A value of the config.GPUPlatform enum, indicating the
|
|
224
|
+
# platform that the runtime will be executed on.
|
|
225
|
+
# Currently, only one platform may be used for
|
|
226
|
+
# a given program using Harmonize.
|
|
227
|
+
gpu_platform=None,
|
|
228
|
+
**kwargs
|
|
229
|
+
):
|
|
230
|
+
|
|
231
|
+
#super(RuntimeSpec,self).__init__(name='Runtime')
|
|
232
|
+
|
|
233
|
+
config.require_platform()
|
|
234
|
+
|
|
235
|
+
if gpu_platform == None:
|
|
236
|
+
platform_name = None
|
|
237
|
+
if config.CUDA_AVAILABLE:
|
|
238
|
+
gpu_platform = config.GPUPlatform.CUDA
|
|
239
|
+
platform_name = "CUDA"
|
|
240
|
+
elif config.ROCM_AVAILABLE:
|
|
241
|
+
gpu_platform = config.GPUPlatform.ROCM
|
|
242
|
+
platform_name = "ROCM"
|
|
243
|
+
debug_print(f"Runtime GPU platform set as {platform_name}")
|
|
244
|
+
|
|
245
|
+
|
|
246
|
+
if not(isinstance(gpu_platform, config.GPUPlatform) or gpu_platform in [v.value for v in config.GPUPlatform.__members__.values()]):
|
|
247
|
+
raise RuntimeError(
|
|
248
|
+
"Provided GPU platform is not valid.\n\n"
|
|
249
|
+
"Please provide a member of the harmonize.config.GPUPlatform enum.\n\n"
|
|
250
|
+
"Valid enum members include: CUDA, HIP"
|
|
251
|
+
)
|
|
252
|
+
|
|
253
|
+
|
|
254
|
+
if len(RuntimeSpec.gpu_platforms) == 0:
|
|
255
|
+
RuntimeSpec.gpu_platforms.add(gpu_platform)
|
|
256
|
+
RuntimeSpec.gpu_arch = config.native_gpu_arch(gpu_platform)
|
|
257
|
+
if gpu_platform == config.GPUPlatform.ROCM:
|
|
258
|
+
RuntimeSpec.kinds = [("Event","event")]
|
|
259
|
+
elif gpu_platform not in RuntimeSpec.gpu_platforms:
|
|
260
|
+
raise RuntimeError(
|
|
261
|
+
"A different config.GPUPlatform has previously been provided to the RuntimeSpec constructor\n\n"
|
|
262
|
+
"Currently, only one GPU platform may be used at a time across all instances."
|
|
263
|
+
)
|
|
264
|
+
|
|
265
|
+
self.spec_name = spec_name
|
|
266
|
+
|
|
267
|
+
|
|
268
|
+
# The states tracked per-device, per-group, and per-thread
|
|
269
|
+
self.dev_state, self.grp_state, self.thd_state = state_spec
|
|
270
|
+
|
|
271
|
+
# The base functions used to set up states and generate work
|
|
272
|
+
self.init_fn, self.final_fn, self.source_fn = base_fns
|
|
273
|
+
|
|
274
|
+
self.async_fns = async_fns
|
|
275
|
+
|
|
276
|
+
self.meta = kwargs
|
|
277
|
+
|
|
278
|
+
self.program_fields = {}
|
|
279
|
+
self.type_specs = {}
|
|
280
|
+
self.accessors = {}
|
|
281
|
+
|
|
282
|
+
if 'function_map' in kwargs:
|
|
283
|
+
self.function_map = kwargs['function_map']
|
|
284
|
+
else:
|
|
285
|
+
self.function_map = {}
|
|
286
|
+
|
|
287
|
+
if 'type_map' in kwargs:
|
|
288
|
+
self.type_map = kwargs['type_map']
|
|
289
|
+
else:
|
|
290
|
+
self.type_map = {}
|
|
291
|
+
|
|
292
|
+
self.generate_meta()
|
|
293
|
+
self.generate_code(gpu_platform)
|
|
294
|
+
|
|
295
|
+
RuntimeSpec.registry[self.spec_name] = self
|
|
296
|
+
|
|
297
|
+
|
|
298
|
+
|
|
299
|
+
|
|
300
|
+
# Generates the meta-parameters used by the runtimme, applying defaults for whichever
|
|
301
|
+
# fields that aren't defined
|
|
302
|
+
def generate_meta(self):
|
|
303
|
+
|
|
304
|
+
# The number of work links that work groups hold in their shared memory
|
|
305
|
+
if 'STASH_SIZE' not in self.meta:
|
|
306
|
+
self.meta['STASH_SIZE'] = 8
|
|
307
|
+
|
|
308
|
+
# The size of the work groups used by the program. Currently, the only
|
|
309
|
+
# supported size is 32
|
|
310
|
+
if 'GROUP_SIZE' not in self.meta:
|
|
311
|
+
self.meta['GROUP_SIZE'] = 32
|
|
312
|
+
|
|
313
|
+
# Finding all input types, each represented as dtypes
|
|
314
|
+
input_list = []
|
|
315
|
+
for async_fn in self.async_fns:
|
|
316
|
+
arg_list = []
|
|
317
|
+
for idx, arg in enumerate(fn_arg_ano_list(async_fn)):
|
|
318
|
+
np_type = map_type_to_np(arg)
|
|
319
|
+
arg_list.append(("param_"+str(idx),np_type))
|
|
320
|
+
input_list.append(np.dtype(arg_list))
|
|
321
|
+
|
|
322
|
+
# The maximum size of all input types. This must be known in order to
|
|
323
|
+
# allocate memory spans with a size that can support all async functions
|
|
324
|
+
max_input_size = 0
|
|
325
|
+
for input in input_list:
|
|
326
|
+
max_input_size = max(max_input_size,input.itemsize)
|
|
327
|
+
self.meta['MAX_INP_SIZE'] = max_input_size #type_.particle.itemsize
|
|
328
|
+
|
|
329
|
+
# The integer type used by the system to address promises
|
|
330
|
+
self.meta['ADR_TYPE'] = np.uint32
|
|
331
|
+
|
|
332
|
+
# The type used to store per-link meta-data, currently used only
|
|
333
|
+
# for debugging purposes
|
|
334
|
+
self.meta['META_TYPE'] = np.uint32
|
|
335
|
+
|
|
336
|
+
# The type used to store the per-link discriminant, identifying the
|
|
337
|
+
# async call that corresponds to the link's data
|
|
338
|
+
if 'OP_DISC_TYPE' not in self.meta:
|
|
339
|
+
self.meta['OP_DISC_TYPE'] = np.uint32
|
|
340
|
+
|
|
341
|
+
# The type used to represent the union of all potential inputs
|
|
342
|
+
self.meta['UNION_TYPE'] = np.dtype((np.void,self.meta['MAX_INP_SIZE']))
|
|
343
|
+
|
|
344
|
+
# The type used to represent the array of promises held by a link
|
|
345
|
+
self.meta['PROMISE_ARR_TYPE'] = np.dtype((self.meta['UNION_TYPE'],self.meta['GROUP_SIZE']))
|
|
346
|
+
|
|
347
|
+
# The work link type, the fundamental type used to organize work
|
|
348
|
+
# in the system
|
|
349
|
+
self.meta['WORK_LINK_TYPE'] = np.dtype([
|
|
350
|
+
('promises',self.meta['PROMISE_ARR_TYPE']),
|
|
351
|
+
('next',self.meta['ADR_TYPE']),
|
|
352
|
+
('meta_data',self.meta['META_TYPE']),
|
|
353
|
+
('id',self.meta['OP_DISC_TYPE']),
|
|
354
|
+
])
|
|
355
|
+
|
|
356
|
+
# The integer type used to iterate across IO buffers
|
|
357
|
+
self.meta['IO_ITER_TYPE'] = self.meta['ADR_TYPE']
|
|
358
|
+
|
|
359
|
+
# The atomic iterator type used to iterate across IO buffers
|
|
360
|
+
self.meta['IO_ATOMIC_ITER_TYPE'] = np.dtype([
|
|
361
|
+
('value',self.meta['IO_ITER_TYPE']),
|
|
362
|
+
('limit',self.meta['IO_ITER_TYPE']),
|
|
363
|
+
])
|
|
364
|
+
|
|
365
|
+
# The atomic iterator type used to iterate across IO buffers
|
|
366
|
+
self.meta['IO_BUFFER_TYPE'] = np.dtype([
|
|
367
|
+
('toggle', np.bool_),
|
|
368
|
+
('padding1', np.uint8),
|
|
369
|
+
('padding2', np.uint16),
|
|
370
|
+
('padding3', np.uint32),
|
|
371
|
+
('data_a', np.uintp),
|
|
372
|
+
('data_b', np.uintp),
|
|
373
|
+
('capacity', self.meta['IO_ITER_TYPE']),
|
|
374
|
+
('input_iter', self.meta['IO_ATOMIC_ITER_TYPE']),
|
|
375
|
+
('output_iter',self.meta['IO_ATOMIC_ITER_TYPE']),
|
|
376
|
+
])
|
|
377
|
+
|
|
378
|
+
# The array of IO buffers used by the event-based method
|
|
379
|
+
self.meta['IO_BUFFER_ARRAY_TYPE'] = np.dtype(
|
|
380
|
+
(np.uintp,(len(self.async_fns),))
|
|
381
|
+
)
|
|
382
|
+
|
|
383
|
+
|
|
384
|
+
# The size of the array used to house all work links (measured in links)
|
|
385
|
+
if 'WORK_ARENA_SIZE' not in self.meta:
|
|
386
|
+
self.meta['WORK_ARENA_SIZE'] = 65536
|
|
387
|
+
|
|
388
|
+
# The type used as a handle to the arena of work links
|
|
389
|
+
self.meta['WORK_ARENA_TYPE'] = np.dtype([('size',np.intp),('links',np.intp)])
|
|
390
|
+
|
|
391
|
+
# The number of independent queues maintained per stack frame and per
|
|
392
|
+
# link pool
|
|
393
|
+
if 'QUEUE_COUNT' not in self.meta:
|
|
394
|
+
self.meta['QUEUE_COUNT'] = 8192 #1024
|
|
395
|
+
# The type of value used to represent links
|
|
396
|
+
self.meta['QUEUE_TYPE'] = np.intp
|
|
397
|
+
# The type used to hold a series of work queues
|
|
398
|
+
self.meta['WORK_POOL_ARR_TYPE'] = np.dtype((
|
|
399
|
+
self.meta['QUEUE_TYPE'],
|
|
400
|
+
(self.meta['QUEUE_COUNT'],)
|
|
401
|
+
))
|
|
402
|
+
|
|
403
|
+
# The struct type that wraps a series of work queues
|
|
404
|
+
self.meta['WORK_POOL_TYPE'] = np.dtype([
|
|
405
|
+
('queues',self.meta['WORK_POOL_ARR_TYPE'])
|
|
406
|
+
])
|
|
407
|
+
# The type used to represent
|
|
408
|
+
self.meta['WORK_FRAME_TYPE'] = np.dtype([
|
|
409
|
+
('children_residents',np.uint32),
|
|
410
|
+
('padding',np.uintp),
|
|
411
|
+
('pool',self.meta['WORK_POOL_TYPE'])
|
|
412
|
+
])
|
|
413
|
+
|
|
414
|
+
# The size of the stack (only size-one stacks are currently supported)
|
|
415
|
+
self.meta['STACK_SIZE'] = 1
|
|
416
|
+
# The type used to hold a series of frames
|
|
417
|
+
self.meta['FRAME_ARR_TYPE'] = np.dtype((
|
|
418
|
+
self.meta['WORK_FRAME_TYPE'],
|
|
419
|
+
(self.meta['STACK_SIZE'],)
|
|
420
|
+
))
|
|
421
|
+
# The type used to hold a work stack and it's associated meta-data
|
|
422
|
+
self.meta['WORK_STACK_TYPE'] = np.dtype([
|
|
423
|
+
('checkout', np.uint32),
|
|
424
|
+
('status_flags',np.uint32),
|
|
425
|
+
('depth_live', np.uint32),
|
|
426
|
+
('frames', self.meta['FRAME_ARR_TYPE'])
|
|
427
|
+
])
|
|
428
|
+
|
|
429
|
+
# The device context type, tracking the arena, how much of the arena dddddhas
|
|
430
|
+
# been claimed the 'easy' way, the pool for allocating/deallocating work links
|
|
431
|
+
# in the arena, and the stack for tracking work links that contain outstanding
|
|
432
|
+
# promises waiting for processing
|
|
433
|
+
self.meta['DEV_CTX_TYPE'] = {}
|
|
434
|
+
|
|
435
|
+
self.meta['DEV_CTX_TYPE']["Async"] = np.dtype([
|
|
436
|
+
('claim_count',np.intp),
|
|
437
|
+
('arena', self.meta['WORK_ARENA_TYPE']),
|
|
438
|
+
('pool' , np.intp),
|
|
439
|
+
('stack', np.intp)
|
|
440
|
+
])
|
|
441
|
+
|
|
442
|
+
self.meta['DEV_CTX_TYPE']["Event"] = np.dtype([
|
|
443
|
+
('checkout', np.uintp),
|
|
444
|
+
('load_margin',np.uint32),
|
|
445
|
+
('padding', np.uint32),
|
|
446
|
+
('event_io', self.meta['IO_BUFFER_ARRAY_TYPE']),
|
|
447
|
+
])
|
|
448
|
+
|
|
449
|
+
return self.meta
|
|
450
|
+
|
|
451
|
+
def register_type_specs(self,kind,allow_multifield=False):
|
|
452
|
+
# Take alignment/size info from Records
|
|
453
|
+
size = size_of(kind)
|
|
454
|
+
align = alignment(kind)
|
|
455
|
+
self.type_specs[(size,align)] = ()
|
|
456
|
+
|
|
457
|
+
|
|
458
|
+
# Generates the CUDA/C++ code specifying the program and its rutimme's
|
|
459
|
+
# meta-parameters
|
|
460
|
+
def generate_specification_code(
|
|
461
|
+
self,
|
|
462
|
+
suffix,
|
|
463
|
+
# Whether or not the async functions should be inlined through ptx
|
|
464
|
+
# (currently, this feature is NOT supported)
|
|
465
|
+
inline=False
|
|
466
|
+
):
|
|
467
|
+
|
|
468
|
+
|
|
469
|
+
# Accumulator for type definitions
|
|
470
|
+
type_defs = ""
|
|
471
|
+
|
|
472
|
+
# Add in parameter specs from the basic async functions
|
|
473
|
+
for func in self.async_fns:
|
|
474
|
+
for kind in fn_arg_ano_list(func):
|
|
475
|
+
self.register_type_specs(kind)
|
|
476
|
+
|
|
477
|
+
# Add in specs for the state types
|
|
478
|
+
state_kinds = [self.dev_state, self.grp_state, self.thd_state]
|
|
479
|
+
for kind in state_kinds:
|
|
480
|
+
self.register_type_specs(kind,allow_multifield=True)
|
|
481
|
+
|
|
482
|
+
# An accumulator for parameter type declarations
|
|
483
|
+
param_decls = ""
|
|
484
|
+
|
|
485
|
+
# A map from alignment sizes to their corresponding element type
|
|
486
|
+
element_map = {
|
|
487
|
+
1 : "unsigned char",
|
|
488
|
+
2 : "unsigned short int",
|
|
489
|
+
4 : "unsigned int",
|
|
490
|
+
8 : "unsigned long long int"
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
# Create types matching the required size and alignment. Until reliable alignment
|
|
494
|
+
# deduction is implemented, an alignment of 8 will always be used.
|
|
495
|
+
for size, align in self.type_specs.keys():
|
|
496
|
+
align = 8
|
|
497
|
+
count = (size + (align-1)) // align
|
|
498
|
+
size = ((size + (align-1)) // align) * align
|
|
499
|
+
param_decls += "struct _"+str(size)+"b"+str(align) \
|
|
500
|
+
+" { "+element_map[align]+" data["+str(count)+"]; };\n"
|
|
501
|
+
|
|
502
|
+
|
|
503
|
+
|
|
504
|
+
# Accumulator for prototypes of extern definitions of async functions, initialized
|
|
505
|
+
# with prototypes corresponding to the required async functions
|
|
506
|
+
proto_decls = "" \
|
|
507
|
+
+ f"extern \"C\" __device__ int _initialize_{suffix}(void*, void* prog);\n" \
|
|
508
|
+
+ f"extern \"C\" __device__ int _finalize_{suffix} (void*, void* prog);\n" \
|
|
509
|
+
+ f"extern \"C\" __device__ int _make_work_{suffix} (bool* result, void* prog);\n"
|
|
510
|
+
|
|
511
|
+
# Generate and accumulate prototypes for other async function definitons
|
|
512
|
+
for func in self.async_fns:
|
|
513
|
+
param_text = func_param_text(func,self.type_map,rec_mode="void_ptr",prior=True,clip=0,first_void=True)
|
|
514
|
+
return_type = "void"
|
|
515
|
+
if 'return' in func.__annotations__:
|
|
516
|
+
return_type = map_type_name(self.type_map, func.__annotations__['return'])
|
|
517
|
+
proto_decls += f"extern \"C\" __device__ int _{func.__name__}_{suffix}" \
|
|
518
|
+
+ f"({return_type}*{param_text});\n"
|
|
519
|
+
|
|
520
|
+
# Accumulator for async function definitions
|
|
521
|
+
async_defs = ""
|
|
522
|
+
|
|
523
|
+
# Forward-declare the structs used to house the definitons
|
|
524
|
+
for func in self.async_fns:
|
|
525
|
+
async_defs += "struct " + pascal_case(func.__name__) + ";\n"
|
|
526
|
+
|
|
527
|
+
# Generate an async function struct (with appropriate members)
|
|
528
|
+
# for each async function provided, either attempting to inline the
|
|
529
|
+
# function definition (this currently does not work), or inserting
|
|
530
|
+
# a call to a matching extern function, allowing the scheduling
|
|
531
|
+
# program to jump to the appropriate logic
|
|
532
|
+
for func in self.async_fns:
|
|
533
|
+
async_defs += harm_async_func(func,self.function_map,self.type_map,inline,suffix)
|
|
534
|
+
|
|
535
|
+
# The metaparameters of the program specification, indicating the sizes
|
|
536
|
+
# of the data structures used
|
|
537
|
+
metaparams = {
|
|
538
|
+
"STASH_SIZE" : self.meta['STASH_SIZE'],
|
|
539
|
+
"FRAME_SIZE" : self.meta['QUEUE_COUNT'],
|
|
540
|
+
"POOL_SIZE" : self.meta['QUEUE_COUNT'],
|
|
541
|
+
}
|
|
542
|
+
|
|
543
|
+
# The declarations of each of the metaparameters
|
|
544
|
+
meta_defs = "".join(["\tstatic const size_t "+name+" = "+str(value) +";\n" for name,value in metaparams.items()])
|
|
545
|
+
|
|
546
|
+
# The declaration of the union of all input types
|
|
547
|
+
union_def = "\ttypedef OpUnion<"+",".join([pascal_case(str(func.__name__)) for func in self.async_fns])+"> OpSet;\n"
|
|
548
|
+
|
|
549
|
+
# The definition of the device, group, and thread states
|
|
550
|
+
state_defs = "\ttypedef "+map_type_name(self.type_map,self.dev_state,rec_mode="ptr")+" DeviceState;\n" \
|
|
551
|
+
+ "\ttypedef "+map_type_name(self.type_map,self.grp_state,rec_mode="ptr")+" GroupState;\n" \
|
|
552
|
+
+ "\ttypedef "+map_type_name(self.type_map,self.thd_state,rec_mode="ptr")+" ThreadState;\n"
|
|
553
|
+
|
|
554
|
+
# The base function definitions
|
|
555
|
+
spec_def = "struct " + self.spec_name + "{\n" \
|
|
556
|
+
+ meta_defs + union_def + state_defs \
|
|
557
|
+
+ harm_template_func(self.init_fn ,"initialize",self.function_map,self.type_map,inline,suffix,True) \
|
|
558
|
+
+ harm_template_func(self.final_fn ,"finalize" ,self.function_map,self.type_map,inline,suffix,True) \
|
|
559
|
+
+ harm_template_func(self.source_fn,"make_work" ,self.function_map,self.type_map,inline,suffix,True) \
|
|
560
|
+
+ "};\n"
|
|
561
|
+
|
|
562
|
+
return type_defs + param_decls + proto_decls + async_defs + spec_def
|
|
563
|
+
|
|
564
|
+
def enumerate_program_fields(self,base_label,path,kind,offset=0,is_pointer=True):
|
|
565
|
+
|
|
566
|
+
if isinstance(kind,numba.types.Record):
|
|
567
|
+
#print("\n\n\n\nRECORD", path)
|
|
568
|
+
self.program_fields[base_label] = ProgramField(
|
|
569
|
+
base_label,
|
|
570
|
+
path,
|
|
571
|
+
offset,
|
|
572
|
+
kind,
|
|
573
|
+
is_pointer
|
|
574
|
+
)
|
|
575
|
+
elif isinstance(kind,numba.types.Array):
|
|
576
|
+
#print("\n\n\n\nARRAY", path)
|
|
577
|
+
self.program_fields[base_label] = ProgramField(
|
|
578
|
+
base_label,
|
|
579
|
+
path,
|
|
580
|
+
offset,
|
|
581
|
+
numba.types.voidptr,
|
|
582
|
+
False
|
|
583
|
+
)
|
|
584
|
+
elif isinstance(kind,dict):
|
|
585
|
+
result = []
|
|
586
|
+
for name, sub_kind in kind.items():
|
|
587
|
+
label = f"{path}_{name}"
|
|
588
|
+
field = ProgramField(
|
|
589
|
+
label,
|
|
590
|
+
path,
|
|
591
|
+
offset,
|
|
592
|
+
sub_kind,
|
|
593
|
+
is_pointer
|
|
594
|
+
)
|
|
595
|
+
self.program_fields[field.label] = field
|
|
596
|
+
offset = offset + ((field.size+7)//8)*8
|
|
597
|
+
return result
|
|
598
|
+
|
|
599
|
+
|
|
600
|
+
|
|
601
|
+
# Returns the CUDA/C++ code specializing the specification for a program type
|
|
602
|
+
def generate_specialization_code(self,kind,shorthand,suffix):
|
|
603
|
+
# String template to alias the appropriate specialization to a convenient name
|
|
604
|
+
spec_decl_template = "typedef {kind}Program<{name}> {short_name};\n"
|
|
605
|
+
|
|
606
|
+
|
|
607
|
+
state_struct = map_type_name(self.type_map,self.dev_state,rec_mode="")
|
|
608
|
+
|
|
609
|
+
|
|
610
|
+
# Accumulator for includes and initial declarations/typedefs
|
|
611
|
+
preamble = ""
|
|
612
|
+
|
|
613
|
+
# Accumulator for init/exec/async/sync wrappers
|
|
614
|
+
dispatch_defs = ""
|
|
615
|
+
# Accumulator for accessors
|
|
616
|
+
accessor_defs = ""
|
|
617
|
+
|
|
618
|
+
# The name used to refer to the program template specialization
|
|
619
|
+
short_name = self.spec_name.lower() +"_"+shorthand
|
|
620
|
+
# Typedef the specialization to a more convenient shothand
|
|
621
|
+
preamble += spec_decl_template.format(kind=kind,name=self.spec_name,short_name=short_name,suffix=suffix)
|
|
622
|
+
|
|
623
|
+
# Generate the wrappers for kernel entry points
|
|
624
|
+
dispatch_defs += init_template.format(short_name=short_name,name=self.spec_name,kind=kind,suffix=suffix)
|
|
625
|
+
dispatch_defs += exec_template.format(short_name=short_name,name=self.spec_name,kind=kind,suffix=suffix)
|
|
626
|
+
|
|
627
|
+
if kind == "Event":
|
|
628
|
+
dispatch_defs += alloc_event_prog_template.format(short_name=short_name,suffix=suffix)
|
|
629
|
+
else :
|
|
630
|
+
dispatch_defs += alloc_harm_prog_template.format(short_name=short_name,suffix=suffix)
|
|
631
|
+
|
|
632
|
+
dispatch_defs += free_prog_template .format(short_name=short_name,suffix=suffix)
|
|
633
|
+
dispatch_defs += alloc_state_template.format(state_struct=state_struct,suffix=suffix)
|
|
634
|
+
dispatch_defs += free_state_template .format(suffix=suffix,state_struct=state_struct)
|
|
635
|
+
dispatch_defs += complete_template .format(short_name=short_name,suffix=suffix)
|
|
636
|
+
dispatch_defs += clear_flags_template.format(short_name=short_name,suffix=suffix)
|
|
637
|
+
dispatch_defs += set_device_template.format(suffix=suffix)
|
|
638
|
+
|
|
639
|
+
|
|
640
|
+
for label, field in self.program_fields.items():
|
|
641
|
+
field_struct = map_type_name(self.type_map,field.kind,rec_mode="")
|
|
642
|
+
dispatch_defs += load_state_template .format(
|
|
643
|
+
label=label,
|
|
644
|
+
size=field.size,
|
|
645
|
+
offset=field.offset,
|
|
646
|
+
suffix=suffix
|
|
647
|
+
)
|
|
648
|
+
dispatch_defs += store_state_template.format(
|
|
649
|
+
label=label,
|
|
650
|
+
size=field.size,
|
|
651
|
+
offset=field.offset,
|
|
652
|
+
suffix=suffix
|
|
653
|
+
)
|
|
654
|
+
is_array_str = "false"
|
|
655
|
+
if field.is_array:
|
|
656
|
+
is_array_str = "true"
|
|
657
|
+
dispatch_defs += store_pointer_state_template.format(
|
|
658
|
+
label=label,
|
|
659
|
+
size=field.size,
|
|
660
|
+
offset=field.offset,
|
|
661
|
+
suffix=suffix,
|
|
662
|
+
is_array=is_array_str
|
|
663
|
+
)
|
|
664
|
+
|
|
665
|
+
|
|
666
|
+
|
|
667
|
+
# Generate the dispatch functions for each async function
|
|
668
|
+
for fn in self.async_fns:
|
|
669
|
+
# Accepts record parameters as void pointers
|
|
670
|
+
param_text = func_param_text(fn,self.type_map,rec_mode="void_ptr",prior=True,clip=0,first_void=True)
|
|
671
|
+
# Casts the void pointers of parameters to types with matching size and alignment
|
|
672
|
+
arg_text = func_arg_text(fn,self.type_map,rec_mode="cast_deref",prior=False,clip=1)
|
|
673
|
+
# Generates a wrapper for both async and sync dispatches
|
|
674
|
+
for kind in ["async","sync"]:
|
|
675
|
+
dispatch_defs += dispatch_template.format(
|
|
676
|
+
short_name=short_name,
|
|
677
|
+
fn=fn.__name__,
|
|
678
|
+
fn_type=pascal_case(fn.__name__),
|
|
679
|
+
params=param_text,
|
|
680
|
+
args=arg_text,
|
|
681
|
+
kind=kind,
|
|
682
|
+
suffix=suffix,
|
|
683
|
+
)
|
|
684
|
+
|
|
685
|
+
# Creates a field accessing function for each field
|
|
686
|
+
for label, field in self.program_fields.items():
|
|
687
|
+
if field.is_array:
|
|
688
|
+
deref = "*(void**)"
|
|
689
|
+
else:
|
|
690
|
+
deref = ""
|
|
691
|
+
accessor_defs += accessor_template.format(
|
|
692
|
+
short_name=short_name,
|
|
693
|
+
label=field.label,
|
|
694
|
+
field=field.path,
|
|
695
|
+
prefix=field.prefix(),
|
|
696
|
+
suffix=suffix,
|
|
697
|
+
offset=field.offset,
|
|
698
|
+
deref=deref
|
|
699
|
+
)
|
|
700
|
+
|
|
701
|
+
if field.is_array:
|
|
702
|
+
accessor_defs += indirect_accessor_template.format(
|
|
703
|
+
short_name=short_name,
|
|
704
|
+
label=field.label,
|
|
705
|
+
field=field.path,
|
|
706
|
+
prefix=field.prefix(),
|
|
707
|
+
suffix=suffix,
|
|
708
|
+
offset=field.offset,
|
|
709
|
+
deref=deref
|
|
710
|
+
)
|
|
711
|
+
|
|
712
|
+
# Query definitions currently disabled
|
|
713
|
+
fn_query_defs = ""
|
|
714
|
+
query_defs = ""
|
|
715
|
+
if False:
|
|
716
|
+
fn_query_list = [ ("load_fraction","float", "") ]
|
|
717
|
+
for (field,kind,prefix) in fn_query_list:
|
|
718
|
+
for fn in self.async_fns:
|
|
719
|
+
fn_query_defs += fn_query_template.format(
|
|
720
|
+
short_name=short_name,
|
|
721
|
+
fn_type=pascal_case(fn.__name__),
|
|
722
|
+
field=field,
|
|
723
|
+
kind=kind,
|
|
724
|
+
prefix=prefix,
|
|
725
|
+
)
|
|
726
|
+
|
|
727
|
+
query_list = [ ]
|
|
728
|
+
for (field,kind,prefix) in query_list:
|
|
729
|
+
query_defs += query_template.format(
|
|
730
|
+
short_name=short_name,
|
|
731
|
+
field=field,
|
|
732
|
+
kind=kind,
|
|
733
|
+
prefix=prefix
|
|
734
|
+
)
|
|
735
|
+
|
|
736
|
+
flag_defs = early_halt_template.format(
|
|
737
|
+
short_name=short_name,
|
|
738
|
+
suffix=suffix
|
|
739
|
+
)
|
|
740
|
+
|
|
741
|
+
return preamble + dispatch_defs + accessor_defs + fn_query_defs + query_defs + flag_defs
|
|
742
|
+
|
|
743
|
+
|
|
744
|
+
def generate_async_ptx(self,cache_path,suffix,platform):
|
|
745
|
+
# The list of required async functions
|
|
746
|
+
base_fns = [self.init_fn, self.final_fn, self.source_fn]
|
|
747
|
+
# The full list of async functions
|
|
748
|
+
comp_list = [fn for fn in base_fns] + self.async_fns
|
|
749
|
+
|
|
750
|
+
|
|
751
|
+
# The set of fields that should have accessors, each annotated with
|
|
752
|
+
# the code (if any) that should prefix references to those fields.
|
|
753
|
+
# This is mainly useful for working with references.
|
|
754
|
+
|
|
755
|
+
base_fields = [
|
|
756
|
+
("device",self.dev_state),
|
|
757
|
+
("group", self.grp_state),
|
|
758
|
+
("thread",self.thd_state),
|
|
759
|
+
]
|
|
760
|
+
|
|
761
|
+
|
|
762
|
+
for field in base_fields:
|
|
763
|
+
field_path, field_kind = field
|
|
764
|
+
self.enumerate_program_fields(field_path,field_path,field_kind)
|
|
765
|
+
|
|
766
|
+
|
|
767
|
+
|
|
768
|
+
rep_list = [ f"_{fn.__name__}" for fn in comp_list]
|
|
769
|
+
rep_list += [ f"dispatch_{fn.__name__}_async" for fn in comp_list ]
|
|
770
|
+
rep_list += [ f"dispatch_{fn.__name__}_sync" for fn in comp_list ]
|
|
771
|
+
rep_list += [ f"access_{label}" for label in self.program_fields ]
|
|
772
|
+
rep_list += [ f"access_indirect_{label}" for label in self.program_fields ]
|
|
773
|
+
rep_list += [ "halt_early" ]
|
|
774
|
+
|
|
775
|
+
debug_print(f"REP LIST: {rep_list}")
|
|
776
|
+
|
|
777
|
+
# Compile each user-provided function defintion to ptx
|
|
778
|
+
# and save it to an appropriately named file
|
|
779
|
+
for fn in comp_list:
|
|
780
|
+
base_name = fn.__name__
|
|
781
|
+
base_name = base_name + "_" + suffix
|
|
782
|
+
base_name = cache_path + base_name
|
|
783
|
+
|
|
784
|
+
if config.GPUPlatform.ROCM in RuntimeSpec.gpu_platforms:
|
|
785
|
+
ir_path = f"{base_name}.ll"
|
|
786
|
+
bc_path = f"{base_name}.bc"
|
|
787
|
+
|
|
788
|
+
if config.GPUPlatform.CUDA in RuntimeSpec.gpu_platforms:
|
|
789
|
+
ir_path = f"{base_name}.ptx"
|
|
790
|
+
obj_path = f"{base_name}.o"
|
|
791
|
+
|
|
792
|
+
def_path = inspect.getfile(fn)
|
|
793
|
+
|
|
794
|
+
touched = False
|
|
795
|
+
if not path.isfile(def_path):
|
|
796
|
+
touched = True
|
|
797
|
+
elif not path.isfile(ir_path):
|
|
798
|
+
touched = True
|
|
799
|
+
elif getmtime(def_path) > getmtime(ir_path):
|
|
800
|
+
touched = True
|
|
801
|
+
|
|
802
|
+
if compilation_gate(touched):
|
|
803
|
+
|
|
804
|
+
progress_print(f"Compiling function '{fn.__name__}' for '{suffix}'")
|
|
805
|
+
ir_text = extern_device_ir(fn,self.type_map,suffix,platform)
|
|
806
|
+
for term in rep_list:
|
|
807
|
+
ir_text = re.sub( \
|
|
808
|
+
f'(?P<before>[^a-zA-Z0-9_])(?P<name>{term})(?P<after>[^a-zA-Z0-9_])', \
|
|
809
|
+
f"\g<before>\g<name>_{suffix}\g<after>", \
|
|
810
|
+
ir_text \
|
|
811
|
+
)
|
|
812
|
+
ir_file = open(ir_path,mode='a+')
|
|
813
|
+
ir_file.seek(0)
|
|
814
|
+
old_text = ir_file.read()
|
|
815
|
+
|
|
816
|
+
dirty = False
|
|
817
|
+
if old_text != ir_text:
|
|
818
|
+
dirty = True
|
|
819
|
+
RuntimeSpec.dirty = True
|
|
820
|
+
|
|
821
|
+
if compilation_gate(touched or dirty):
|
|
822
|
+
ir_file.seek(0)
|
|
823
|
+
ir_file.truncate()
|
|
824
|
+
ir_file.write(ir_text)
|
|
825
|
+
ir_file.close()
|
|
826
|
+
|
|
827
|
+
if compilation_gate(dirty):
|
|
828
|
+
if config.GPUPlatform.ROCM in RuntimeSpec.gpu_platforms:
|
|
829
|
+
dev_comp_cmd = [f"{config.hipcc_llvm_as_path()} {ir_path} -o {bc_path}"]
|
|
830
|
+
for cmd in dev_comp_cmd:
|
|
831
|
+
verbose_print(cmd)
|
|
832
|
+
progress_print(f"Compiling '{bc_path}'")
|
|
833
|
+
subprocess.run(cmd.split(),shell=False,check=True)
|
|
834
|
+
|
|
835
|
+
if config.GPUPlatform.CUDA in RuntimeSpec.gpu_platforms:
|
|
836
|
+
dev_comp_cmd = [f"{config.nvcc_path()} -rdc=true -dc -arch=sm_{RuntimeSpec.gpu_arch} --cudart shared --compiler-options -fPIC {ir_path} -o {obj_path} {RuntimeSpec.debug_flag}"]
|
|
837
|
+
for cmd in dev_comp_cmd:
|
|
838
|
+
verbose_print(cmd)
|
|
839
|
+
progress_print(f"Compiling '{obj_path}'")
|
|
840
|
+
subprocess.run(cmd.split(),shell=False,check=True)
|
|
841
|
+
|
|
842
|
+
# Record the path of the generated (or pre-existing) object
|
|
843
|
+
if config.GPUPlatform.ROCM in RuntimeSpec.gpu_platforms:
|
|
844
|
+
RuntimeSpec.gpu_bc_set.add(bc_path)
|
|
845
|
+
else:
|
|
846
|
+
RuntimeSpec.obj_set.add(obj_path)
|
|
847
|
+
|
|
848
|
+
|
|
849
|
+
# Generates the CUDA/C++ code specifying the structure of the program, for later
|
|
850
|
+
# specialization to a specific program type, and compiles the ir for each async
|
|
851
|
+
# function supplied to the specfication. Both this cuda code and the ptx are
|
|
852
|
+
# saved to the `__ptxcache__` directory for future re-use.
|
|
853
|
+
def generate_code(self,gpu_platform):
|
|
854
|
+
|
|
855
|
+
# Folder used to cache cuda and ptx code
|
|
856
|
+
|
|
857
|
+
if compilation_gate(True):
|
|
858
|
+
makedirs(RuntimeSpec.cache_path,exist_ok=True)
|
|
859
|
+
|
|
860
|
+
self.fn = {}
|
|
861
|
+
|
|
862
|
+
# A list to record the files containing the definitions
|
|
863
|
+
# of each async function definition
|
|
864
|
+
self.fn_def_list = []
|
|
865
|
+
|
|
866
|
+
self.link_list = []
|
|
867
|
+
|
|
868
|
+
# Generate and compile specializations of the specification for
|
|
869
|
+
# each kind of runtime
|
|
870
|
+
for kind, shortname in RuntimeSpec.kinds:
|
|
871
|
+
|
|
872
|
+
self.fn[kind] = {}
|
|
873
|
+
|
|
874
|
+
# Generate the cuda code implementing the specialization
|
|
875
|
+
suffix = self.spec_name+"_"+shortname
|
|
876
|
+
|
|
877
|
+
# !!!Rep list here
|
|
878
|
+
self.generate_async_ptx(RuntimeSpec.cache_path,suffix,gpu_platform)
|
|
879
|
+
|
|
880
|
+
if not compilation_gate(True):
|
|
881
|
+
continue
|
|
882
|
+
|
|
883
|
+
# Generate and save generic program specification
|
|
884
|
+
base_code = self.generate_specification_code(suffix)
|
|
885
|
+
|
|
886
|
+
# Compile the async function definitions to ptx
|
|
887
|
+
|
|
888
|
+
|
|
889
|
+
# !!!Field enumeration here
|
|
890
|
+
spec_code = self.generate_specialization_code(kind,shortname,suffix)
|
|
891
|
+
|
|
892
|
+
|
|
893
|
+
# Save the code to an appropriately named file
|
|
894
|
+
spec_filename = RuntimeSpec.cache_path+suffix
|
|
895
|
+
|
|
896
|
+
spec_file = open(spec_filename+".cpp",mode='a+')
|
|
897
|
+
spec_file.seek(0)
|
|
898
|
+
old_text = spec_file.read()
|
|
899
|
+
new_text = base_code + spec_code
|
|
900
|
+
if old_text != new_text:
|
|
901
|
+
RuntimeSpec.dirty = True
|
|
902
|
+
spec_file.seek(0)
|
|
903
|
+
spec_file.truncate()
|
|
904
|
+
spec_file.write(new_text)
|
|
905
|
+
spec_file.close()
|
|
906
|
+
|
|
907
|
+
|
|
908
|
+
if config.GPUPlatform.ROCM in RuntimeSpec.gpu_platforms:
|
|
909
|
+
source = f"{spec_filename}.cpp"
|
|
910
|
+
obj = f"{spec_filename}.o"
|
|
911
|
+
touched = False
|
|
912
|
+
if not path.isfile(obj):
|
|
913
|
+
touched = True
|
|
914
|
+
elif getmtime(source) > getmtime(obj):
|
|
915
|
+
touched = True
|
|
916
|
+
|
|
917
|
+
|
|
918
|
+
# Get target triples for later
|
|
919
|
+
if RuntimeSpec.gpu_triple == None:
|
|
920
|
+
ir = f"{spec_filename}.bc"
|
|
921
|
+
|
|
922
|
+
# Compile IR if not exists or stale
|
|
923
|
+
if not path.isfile(ir) or getmtime(source) > getmtime(ir):
|
|
924
|
+
# This compilation is done solely to extract target triples
|
|
925
|
+
dev_comp_cmd = f"{config.hipcc_path()} -fPIC -c -fgpu-rdc -emit-llvm -o {ir} -x hip {source} -include {config.HARMONIZE_ROOT_HEADER} {RuntimeSpec.debug_flag}"
|
|
926
|
+
|
|
927
|
+
verbose_print(dev_comp_cmd)
|
|
928
|
+
progress_print(f"Compiling '{ir}' (need target triples)")
|
|
929
|
+
subprocess.run(dev_comp_cmd.split(),shell=False,check=True)
|
|
930
|
+
|
|
931
|
+
gpu_triple, _ = find_bundle_triples(ir)
|
|
932
|
+
|
|
933
|
+
RuntimeSpec.gpu_triple = gpu_triple
|
|
934
|
+
|
|
935
|
+
# Compile object files
|
|
936
|
+
if compilation_gate(touched):
|
|
937
|
+
RuntimeSpec.dirty = True
|
|
938
|
+
comp_cmd = f"{config.hipcc_path()} -fPIC -c -fgpu-rdc -o {obj} -x hip {source} -include {config.HARMONIZE_ROOT_HEADER} {RuntimeSpec.debug_flag}"
|
|
939
|
+
|
|
940
|
+
verbose_print(comp_cmd)
|
|
941
|
+
progress_print(f"Compiling '{obj}'")
|
|
942
|
+
subprocess.run(comp_cmd.split(),shell=False,check=True)
|
|
943
|
+
|
|
944
|
+
RuntimeSpec.obj_set.add(obj)
|
|
945
|
+
|
|
946
|
+
|
|
947
|
+
if config.GPUPlatform.CUDA in RuntimeSpec.gpu_platforms:
|
|
948
|
+
|
|
949
|
+
source_list = [ (f"{spec_filename}.cpp", f"{spec_filename}.o") ]
|
|
950
|
+
for (source,obj) in source_list:
|
|
951
|
+
|
|
952
|
+
touched = False
|
|
953
|
+
if not path.isfile(obj):
|
|
954
|
+
touched = True
|
|
955
|
+
elif getmtime(source) > getmtime(obj):
|
|
956
|
+
touched = True
|
|
957
|
+
|
|
958
|
+
if compilation_gate(touched):
|
|
959
|
+
RuntimeSpec.dirty = True
|
|
960
|
+
progress_print(f"Compiling '{obj}'")
|
|
961
|
+
dev_comp_cmd = f"{config.nvcc_path()} -x cu -rdc=true -dc -arch=sm_{RuntimeSpec.gpu_arch} --cudart shared --compiler-options -fPIC {source} -include {config.HARMONIZE_ROOT_HEADER} -o {obj} {RuntimeSpec.debug_flag}"
|
|
962
|
+
verbose_print(dev_comp_cmd)
|
|
963
|
+
subprocess.run(dev_comp_cmd.split(),shell=False,check=True)
|
|
964
|
+
RuntimeSpec.obj_set.add(obj)
|
|
965
|
+
|
|
966
|
+
|
|
967
|
+
|
|
968
|
+
|
|
969
|
+
|
|
970
|
+
def async_functions(self):
|
|
971
|
+
return self.fn["Async"]
|
|
972
|
+
|
|
973
|
+
def event_functions(self):
|
|
974
|
+
return self.fn["Event"]
|
|
975
|
+
|
|
976
|
+
|
|
977
|
+
|
|
978
|
+
|
|
979
|
+
# Injects the async/sync dispatch functions and state accessors into the
|
|
980
|
+
# global scope of the caller. This should only be done if you are sure that
|
|
981
|
+
# the injection of these fields into the global namespace of the calling
|
|
982
|
+
# module won't overwrite anything. While convenient in some respects, this
|
|
983
|
+
# function also gives no indication to linters that the corresponding fields
|
|
984
|
+
# will be injected, leading to linters incorrectly (though understandably)
|
|
985
|
+
# marking the fields as undefined
|
|
986
|
+
#def inject_fns(state_spec,async_fns):
|
|
987
|
+
|
|
988
|
+
# dev_state, grp_state, thd_state = state_spec
|
|
989
|
+
|
|
990
|
+
# for func in async_fns:
|
|
991
|
+
# sig = fn_sig(func)
|
|
992
|
+
# name = func.__name__
|
|
993
|
+
# for kind in ["async","sync"]:
|
|
994
|
+
# dispatch_fn = declare_device(f"dispatch_{name}_{kind}", sig)
|
|
995
|
+
# inject_global(kind+"_"+name,dispatch_fn,1)
|
|
996
|
+
|
|
997
|
+
# for name, kind in field_list:
|
|
998
|
+
# sig = kind(numba.uintp)
|
|
999
|
+
# access_fn = declare_device(f"access_{name}_{kind}",sig)
|
|
1000
|
+
# inject_global(name,access_fn,1)
|
|
1001
|
+
|
|
1002
|
+
@staticmethod
|
|
1003
|
+
def declare_program_accessors(field_set,base_path=""):
|
|
1004
|
+
result = {}
|
|
1005
|
+
|
|
1006
|
+
for name, kind in field_set.items():
|
|
1007
|
+
|
|
1008
|
+
if base_path == "":
|
|
1009
|
+
path = name
|
|
1010
|
+
else:
|
|
1011
|
+
path = f"{base_path}_{name}"
|
|
1012
|
+
|
|
1013
|
+
if isinstance(kind,numba.types.Record):
|
|
1014
|
+
sig = kind(numba.uintp)
|
|
1015
|
+
result[name] = (declare_device(f"access_{path}",sig))
|
|
1016
|
+
elif isinstance(kind,numba.types.Array):
|
|
1017
|
+
result[name] = {}
|
|
1018
|
+
sig = numba.types.voidptr(numba.uintp)
|
|
1019
|
+
result[name]["direct"] = (declare_device(f"access_{path}",sig))
|
|
1020
|
+
itemkind = kind.dtype
|
|
1021
|
+
sig = itemkind(numba.uintp)
|
|
1022
|
+
result[name]["indirect"] = (declare_device(f"access_indirect_{path}",sig))
|
|
1023
|
+
elif isinstance(kind,dict):
|
|
1024
|
+
result[name] = RuntimeSpec.declare_program_accessors(kind,path)
|
|
1025
|
+
else:
|
|
1026
|
+
raise numba.errors.TypingError( ""
|
|
1027
|
+
+ "Invalid field specification. "
|
|
1028
|
+
+ "Fields may either be a numba Record, a numba Array,"
|
|
1029
|
+
+ " or a dictionary of fields."
|
|
1030
|
+
)
|
|
1031
|
+
|
|
1032
|
+
return result
|
|
1033
|
+
|
|
1034
|
+
|
|
1035
|
+
|
|
1036
|
+
# Returns the `device`, `group`, and `thread` accessor function
|
|
1037
|
+
# handles of the specification as a triplet
|
|
1038
|
+
@staticmethod
|
|
1039
|
+
def access_fns(state_spec):
|
|
1040
|
+
|
|
1041
|
+
dev_state, grp_state, thd_state = state_spec
|
|
1042
|
+
|
|
1043
|
+
field_set = {
|
|
1044
|
+
"device" : dev_state,
|
|
1045
|
+
"group" : grp_state,
|
|
1046
|
+
"thread" : thd_state,
|
|
1047
|
+
}
|
|
1048
|
+
|
|
1049
|
+
return RuntimeSpec.declare_program_accessors(field_set,"")
|
|
1050
|
+
|
|
1051
|
+
@staticmethod
|
|
1052
|
+
def program_interface():
|
|
1053
|
+
result = {}
|
|
1054
|
+
result["halt_early"] = declare_device(f"halt_early", numba.void(numba.uintp))
|
|
1055
|
+
return result
|
|
1056
|
+
|
|
1057
|
+
|
|
1058
|
+
# Returns the async/sync function handles for the supplied functions, using
|
|
1059
|
+
# `kind` to switch between async and sync
|
|
1060
|
+
def dispatch_fns(kind,*async_fns):
|
|
1061
|
+
async_fns, = async_fns
|
|
1062
|
+
result = []
|
|
1063
|
+
for func in async_fns:
|
|
1064
|
+
sig = fn_sig(func)
|
|
1065
|
+
name = func.__name__
|
|
1066
|
+
result.append(declare_device(f"dispatch_{name}_{kind}", sig))
|
|
1067
|
+
return tuple(result)
|
|
1068
|
+
|
|
1069
|
+
#def query(kind,*fields):
|
|
1070
|
+
# fields, = fields
|
|
1071
|
+
# result = []
|
|
1072
|
+
# for field in fields:
|
|
1073
|
+
# result.append(cuda.declare_device("dispatch_"+name+"_"+kind, sig))
|
|
1074
|
+
# return tuple(result)
|
|
1075
|
+
|
|
1076
|
+
|
|
1077
|
+
# Returns the handles for the on-gpu functions used to asynchronously schedule
|
|
1078
|
+
# the supplied function
|
|
1079
|
+
def async_dispatch(*async_fns):
|
|
1080
|
+
return RuntimeSpec.dispatch_fns("async",async_fns)
|
|
1081
|
+
|
|
1082
|
+
# Returns the handles for the on-gpu functions used to immediately call
|
|
1083
|
+
# the supplied function
|
|
1084
|
+
def sync_dispatch(*async_fns):
|
|
1085
|
+
return RuntimeSpec.dispatch_fns("sync",async_fns)
|
|
1086
|
+
|
|
1087
|
+
|
|
1088
|
+
@staticmethod
|
|
1089
|
+
def generate_hipdevicelib():
|
|
1090
|
+
|
|
1091
|
+
if not compilation_gate(True):
|
|
1092
|
+
return
|
|
1093
|
+
|
|
1094
|
+
def harmonize_version() -> numba.int64:
|
|
1095
|
+
return 0
|
|
1096
|
+
|
|
1097
|
+
ir_text, res_type = config.hip.compile_ptx_for_current_device (
|
|
1098
|
+
harmonize_version,
|
|
1099
|
+
numba.int32(),
|
|
1100
|
+
device=True,
|
|
1101
|
+
debug=config.DEBUG,
|
|
1102
|
+
opt=(not config.DEBUG),
|
|
1103
|
+
name=f"harmonize_version",
|
|
1104
|
+
link_in_hipdevicelib=True
|
|
1105
|
+
)
|
|
1106
|
+
|
|
1107
|
+
ir_path = f"{RuntimeSpec.cache_path}/hipdevicelib.ll"
|
|
1108
|
+
bc_path = f"{RuntimeSpec.cache_path}/hipdevicelib.bc"
|
|
1109
|
+
ir_file = open(ir_path,mode='a+')
|
|
1110
|
+
ir_file.seek(0)
|
|
1111
|
+
old_text = ir_file.read()
|
|
1112
|
+
|
|
1113
|
+
dirty = False
|
|
1114
|
+
if old_text != ir_text:
|
|
1115
|
+
dirty = True
|
|
1116
|
+
RuntimeSpec.dirty = True
|
|
1117
|
+
|
|
1118
|
+
if compilation_gate(dirty):
|
|
1119
|
+
ir_file.seek(0)
|
|
1120
|
+
ir_file.truncate()
|
|
1121
|
+
ir_file.write(ir_text)
|
|
1122
|
+
ir_file.close()
|
|
1123
|
+
|
|
1124
|
+
if compilation_gate(True):
|
|
1125
|
+
progress_print(f"Compiling '{bc_path}'")
|
|
1126
|
+
cmd = f"{config.hipcc_llvm_as_path()} {ir_path} -o {bc_path}"
|
|
1127
|
+
subprocess.run(cmd.split(),shell=False,check=True)
|
|
1128
|
+
RuntimeSpec.gpu_bc_set.add(bc_path)
|
|
1129
|
+
|
|
1130
|
+
|
|
1131
|
+
|
|
1132
|
+
|
|
1133
|
+
# Generates the CUDA/C++ code that provides basic functionality,
|
|
1134
|
+
# such as atomic operations. This is necessary due to varying support
|
|
1135
|
+
# for such operations in Numba for different platforms.
|
|
1136
|
+
@staticmethod
|
|
1137
|
+
def generate_builtin_code(gpu_platform):
|
|
1138
|
+
|
|
1139
|
+
text = ""
|
|
1140
|
+
|
|
1141
|
+
for op_sig_roster in atomic_op_info:
|
|
1142
|
+
op_name, roster = op_sig_roster
|
|
1143
|
+
for op_sig in roster:
|
|
1144
|
+
face_type, real_type = op_sig
|
|
1145
|
+
face_type_cpp = prim_info[face_type]["cpp_name"]
|
|
1146
|
+
face_type_py = prim_info[face_type]["py_name"]
|
|
1147
|
+
real_type_cpp = prim_info[real_type]["cpp_name"]
|
|
1148
|
+
real_type_py = prim_info[real_type]["py_name"]
|
|
1149
|
+
text += atomic_template.format(
|
|
1150
|
+
face_type_cpp=face_type_cpp,
|
|
1151
|
+
face_type_py =face_type_py,
|
|
1152
|
+
real_type_cpp=real_type_cpp,
|
|
1153
|
+
real_type_py =real_type_py,
|
|
1154
|
+
op_py=op_name,
|
|
1155
|
+
op_cpp=f"atomic{op_name.title()}"
|
|
1156
|
+
)
|
|
1157
|
+
|
|
1158
|
+
text += generate_clock_code()
|
|
1159
|
+
text += generate_print_code()
|
|
1160
|
+
text += generate_array_code()
|
|
1161
|
+
|
|
1162
|
+
file_path = f"{RuntimeSpec.cache_path}builtin"
|
|
1163
|
+
source = f"{file_path}.cpp"
|
|
1164
|
+
|
|
1165
|
+
if config.GPUPlatform.CUDA in RuntimeSpec.gpu_platforms:
|
|
1166
|
+
obj = f"{file_path}.o"
|
|
1167
|
+
touched = False
|
|
1168
|
+
if not path.isfile(obj):
|
|
1169
|
+
touched = True
|
|
1170
|
+
elif getmtime(source) > getmtime(obj):
|
|
1171
|
+
touched = True
|
|
1172
|
+
|
|
1173
|
+
if compilation_gate(touched):
|
|
1174
|
+
|
|
1175
|
+
source_file = open(source,mode='a+')
|
|
1176
|
+
source_file.seek(0)
|
|
1177
|
+
old_text = source_file.read()
|
|
1178
|
+
|
|
1179
|
+
dirty = False
|
|
1180
|
+
if old_text != text:
|
|
1181
|
+
dirty = True
|
|
1182
|
+
RuntimeSpec.dirty = True
|
|
1183
|
+
|
|
1184
|
+
if compilation_gate(touched or dirty):
|
|
1185
|
+
source_file.seek(0)
|
|
1186
|
+
source_file.truncate()
|
|
1187
|
+
source_file.write(text)
|
|
1188
|
+
source_file.close()
|
|
1189
|
+
|
|
1190
|
+
RuntimeSpec.dirty = True
|
|
1191
|
+
dev_comp_cmd = f"{config.nvcc_path()} -x cu -rdc=true -dc -arch=sm_{RuntimeSpec.gpu_arch} --cudart shared --compiler-options -fPIC {source} -include {config.HARMONIZE_ROOT_HEADER} -o {obj} {RuntimeSpec.debug_flag}"
|
|
1192
|
+
verbose_print(dev_comp_cmd)
|
|
1193
|
+
progress_print(f"Compiling '{obj}'")
|
|
1194
|
+
subprocess.run(dev_comp_cmd.split(),shell=False,check=True)
|
|
1195
|
+
RuntimeSpec.obj_set.add(obj)
|
|
1196
|
+
|
|
1197
|
+
if config.GPUPlatform.ROCM in RuntimeSpec.gpu_platforms:
|
|
1198
|
+
if RuntimeSpec.gpu_triple == None:
|
|
1199
|
+
raise RuntimeError("Attempted to build builtin code with no target triples defined.")
|
|
1200
|
+
|
|
1201
|
+
obj = f"{file_path}.o"
|
|
1202
|
+
touched = False
|
|
1203
|
+
if not path.isfile(obj):
|
|
1204
|
+
touched = True
|
|
1205
|
+
elif getmtime(source) > getmtime(obj):
|
|
1206
|
+
touched = True
|
|
1207
|
+
|
|
1208
|
+
if compilation_gate(touched):
|
|
1209
|
+
|
|
1210
|
+
source_file = open(source,mode='a+')
|
|
1211
|
+
source_file.seek(0)
|
|
1212
|
+
old_text = source_file.read()
|
|
1213
|
+
|
|
1214
|
+
dirty = False
|
|
1215
|
+
if old_text != text:
|
|
1216
|
+
dirty = True
|
|
1217
|
+
RuntimeSpec.dirty = True
|
|
1218
|
+
|
|
1219
|
+
if compilation_gate(touched or dirty):
|
|
1220
|
+
source_file.seek(0)
|
|
1221
|
+
source_file.truncate()
|
|
1222
|
+
source_file.write(text)
|
|
1223
|
+
source_file.close()
|
|
1224
|
+
|
|
1225
|
+
RuntimeSpec.dirty = True
|
|
1226
|
+
comp_cmd = f"{config.hipcc_path()} -fPIC -c -fgpu-rdc -o {obj} -x hip {source} -include {config.HARMONIZE_ROOT_HEADER} {RuntimeSpec.debug_flag}"
|
|
1227
|
+
|
|
1228
|
+
verbose_print(comp_cmd)
|
|
1229
|
+
progress_print(f"Compiling '{obj}'")
|
|
1230
|
+
subprocess.run(comp_cmd.split(),shell=False,check=True)
|
|
1231
|
+
|
|
1232
|
+
RuntimeSpec.obj_set.add(obj)
|
|
1233
|
+
|
|
1234
|
+
|
|
1235
|
+
|
|
1236
|
+
@staticmethod
|
|
1237
|
+
def bind_specs():
|
|
1238
|
+
|
|
1239
|
+
debug_print("About to bind and load")
|
|
1240
|
+
|
|
1241
|
+
if len(RuntimeSpec.gpu_platforms) == 0:
|
|
1242
|
+
raise RuntimeError(
|
|
1243
|
+
"No GPU platforms are registered for any RuntimeSpec. "
|
|
1244
|
+
"It is likely no RuntimeSpec has been constructed yet."
|
|
1245
|
+
)
|
|
1246
|
+
elif len(RuntimeSpec.gpu_platforms) > 1:
|
|
1247
|
+
raise RuntimeError(
|
|
1248
|
+
"Multiple GPU platforms are registered under the RuntimeSpec class. "
|
|
1249
|
+
"Currently, only one GPU platform may be used at a time across all instances."
|
|
1250
|
+
)
|
|
1251
|
+
|
|
1252
|
+
dev_path = f"{RuntimeSpec.cache_path}harmonize_device.o"
|
|
1253
|
+
so_path = f"{RuntimeSpec.cache_path}harmonize.so"
|
|
1254
|
+
touched = False
|
|
1255
|
+
|
|
1256
|
+
if (config.GPUPlatform.CUDA in RuntimeSpec.gpu_platforms) and (not path.isfile(dev_path)):
|
|
1257
|
+
touched = True
|
|
1258
|
+
elif not path.isfile(so_path):
|
|
1259
|
+
touched = True
|
|
1260
|
+
|
|
1261
|
+
if compilation_gate(touched or RuntimeSpec.dirty):
|
|
1262
|
+
|
|
1263
|
+
|
|
1264
|
+
if config.GPUPlatform.CUDA in RuntimeSpec.gpu_platforms:
|
|
1265
|
+
|
|
1266
|
+
verbose_print("Building for CUDA")
|
|
1267
|
+
|
|
1268
|
+
RuntimeSpec.generate_builtin_code(config.GPUPlatform.CUDA)
|
|
1269
|
+
|
|
1270
|
+
link_list = [ obj for obj in RuntimeSpec.obj_set ]
|
|
1271
|
+
|
|
1272
|
+
comp_cmd = [
|
|
1273
|
+
f"{config.nvcc_path()} -dlink {' '.join(link_list)} -arch=sm_{RuntimeSpec.gpu_arch} --cudart shared -o {dev_path} --compiler-options -fPIC {RuntimeSpec.debug_flag}",
|
|
1274
|
+
f"{config.nvcc_path()} -shared {' '.join(link_list)} {dev_path} -arch=sm_{RuntimeSpec.gpu_arch} --cudart shared -o {so_path} {RuntimeSpec.debug_flag}"
|
|
1275
|
+
]
|
|
1276
|
+
comp_desc = [
|
|
1277
|
+
f"Linking device code",
|
|
1278
|
+
f"Creating shared object file"
|
|
1279
|
+
]
|
|
1280
|
+
|
|
1281
|
+
elif config.GPUPlatform.ROCM in RuntimeSpec.gpu_platforms:
|
|
1282
|
+
|
|
1283
|
+
verbose_print("Building for ROCM")
|
|
1284
|
+
|
|
1285
|
+
RuntimeSpec.generate_builtin_code(config.GPUPlatform.ROCM)
|
|
1286
|
+
|
|
1287
|
+
RuntimeSpec.generate_hipdevicelib()
|
|
1288
|
+
|
|
1289
|
+
so_path = f"{RuntimeSpec.cache_path}harmonize.so"
|
|
1290
|
+
link_list = " ".join([ obj for obj in RuntimeSpec.obj_set ])
|
|
1291
|
+
gpu_bc_list = " ".join( [bc for bc in RuntimeSpec.gpu_bc_set] )
|
|
1292
|
+
device_linked_bc = f"{RuntimeSpec.cache_path}device_linked.bc"
|
|
1293
|
+
device_bundle_bc = f"{RuntimeSpec.cache_path}device_bundle.bc"
|
|
1294
|
+
|
|
1295
|
+
if RuntimeSpec.gpu_bc_set:
|
|
1296
|
+
gpu_bc_sorted = sorted(RuntimeSpec.gpu_bc_set)
|
|
1297
|
+
if len(gpu_bc_sorted) == 1:
|
|
1298
|
+
llvm_link_cmd = f"{config.hipcc_llvm_link_path()} {gpu_bc_sorted[0]} -o {device_linked_bc}"
|
|
1299
|
+
else:
|
|
1300
|
+
base = gpu_bc_sorted[0]
|
|
1301
|
+
overrides = " ".join(f"--override={bc}" for bc in gpu_bc_sorted[1:]) # TODO: this is a workaround for multiply-defined symbols during llvm-link
|
|
1302
|
+
llvm_link_cmd = f"{config.hipcc_llvm_link_path()} {base} {overrides} -o {device_linked_bc}"
|
|
1303
|
+
comp_cmd = [
|
|
1304
|
+
llvm_link_cmd,
|
|
1305
|
+
f"{config.hipcc_clang_offload_bundler_path()} --type=bc --input={device_linked_bc} --output={device_bundle_bc} --targets={RuntimeSpec.gpu_triple}",
|
|
1306
|
+
f"{config.hipcc_path()} -fPIC -shared -fgpu-rdc --hip-link {link_list} {device_bundle_bc} -o {so_path} -g"
|
|
1307
|
+
]
|
|
1308
|
+
|
|
1309
|
+
comp_desc = [
|
|
1310
|
+
"Linking device code",
|
|
1311
|
+
"Bundling linked code",
|
|
1312
|
+
"Creating shared object file",
|
|
1313
|
+
]
|
|
1314
|
+
else:
|
|
1315
|
+
comp_cmd = [f"{config.hipcc_path()} -fPIC -shared -fgpu-rdc --hip-link {link_list} -o {so_path} -g"]
|
|
1316
|
+
comp_desc = ["Creating shared object file"]
|
|
1317
|
+
|
|
1318
|
+
for idx, cmd in enumerate(comp_cmd):
|
|
1319
|
+
verbose_print(cmd)
|
|
1320
|
+
progress_print(comp_desc[idx])
|
|
1321
|
+
subprocess.run(cmd.split(),shell=False,check=True)
|
|
1322
|
+
|
|
1323
|
+
|
|
1324
|
+
|
|
1325
|
+
@staticmethod
|
|
1326
|
+
def load_specs():
|
|
1327
|
+
load_wrapper_template="""
|
|
1328
|
+
def load_wrapper_{id} (field,vp):
|
|
1329
|
+
pass
|
|
1330
|
+
|
|
1331
|
+
@numba.extending.overload(load_wrapper_{id})
|
|
1332
|
+
def load_wrapper_{id}_obj_overload (field,vp):
|
|
1333
|
+
if isinstance(field,numba.types.Array):
|
|
1334
|
+
def impl(field,vp):
|
|
1335
|
+
load_state(field.ctypes.data,vp)
|
|
1336
|
+
return impl
|
|
1337
|
+
else:
|
|
1338
|
+
def impl(field,vp):
|
|
1339
|
+
load_state(field,vp)
|
|
1340
|
+
return impl
|
|
1341
|
+
"""
|
|
1342
|
+
store_wrapper_template="""
|
|
1343
|
+
def store_wrapper_{id} (vp,field):
|
|
1344
|
+
pass
|
|
1345
|
+
|
|
1346
|
+
@numba.extending.overload(store_wrapper_{id})
|
|
1347
|
+
def store_wrapper_{id}_obj_overload (vp,field):
|
|
1348
|
+
if isinstance(field,numba.types.Array):
|
|
1349
|
+
def impl(vp,field):
|
|
1350
|
+
store_state(vp,field.ctypes.data)
|
|
1351
|
+
return impl
|
|
1352
|
+
else:
|
|
1353
|
+
def impl(vp,field):
|
|
1354
|
+
store_state(vp,field)
|
|
1355
|
+
return impl
|
|
1356
|
+
"""
|
|
1357
|
+
|
|
1358
|
+
so_path = f"{RuntimeSpec.cache_path}harmonize.so"
|
|
1359
|
+
abs_so_path = abspath(so_path)
|
|
1360
|
+
#print(path)
|
|
1361
|
+
binding.load_library_permanently(abs_so_path)
|
|
1362
|
+
|
|
1363
|
+
for name, spec in RuntimeSpec.registry.items():
|
|
1364
|
+
for kind, shortname in RuntimeSpec.kinds:
|
|
1365
|
+
|
|
1366
|
+
suffix = spec.spec_name+"_"+shortname
|
|
1367
|
+
|
|
1368
|
+
# Create handles to reference the cuda entry wrapper functions
|
|
1369
|
+
void = numba.types.void
|
|
1370
|
+
vp = numba.types.voidptr
|
|
1371
|
+
i32 = numba.types.int32
|
|
1372
|
+
usize = numba.types.uintp
|
|
1373
|
+
sig = numba.core.typing.signature
|
|
1374
|
+
ext_fn = numba.types.ExternalFunction
|
|
1375
|
+
context = numba.from_dtype(spec.meta['DEV_CTX_TYPE'][kind])
|
|
1376
|
+
boolean = numba.types.boolean
|
|
1377
|
+
|
|
1378
|
+
state = spec.dev_state
|
|
1379
|
+
|
|
1380
|
+
init_program = ext_fn(f"init_program_{suffix}", sig(void, vp, usize))
|
|
1381
|
+
exec_program = ext_fn(f"exec_program_{suffix}", sig(void, vp, usize, usize))
|
|
1382
|
+
|
|
1383
|
+
for label, field in spec.program_fields.items():
|
|
1384
|
+
store_name = f"store_state_{label}"
|
|
1385
|
+
store_p_name = f"store_pointer_state_{label}"
|
|
1386
|
+
load_name = f"load_state_{label}"
|
|
1387
|
+
store_state = ext_fn(f"{store_name}_{suffix}", sig(void, vp, field.ext_kind))
|
|
1388
|
+
store_p_state = ext_fn(f"{store_p_name}_{suffix}", sig(void, vp, field.ext_kind))
|
|
1389
|
+
load_state = ext_fn(f"{load_name}_{suffix}", sig(void, field.ext_kind, vp))
|
|
1390
|
+
if field.is_array :
|
|
1391
|
+
id = generate_uuid()
|
|
1392
|
+
exec(f"def store_pointer_wrapper_{id}(vp,field):\n vptr = into_voidptr(field)\n store_p_state(vp,vptr)",globals()|locals(),locals())
|
|
1393
|
+
exec(store_wrapper_template.format(id=id),globals()|locals(),locals())
|
|
1394
|
+
exec(load_wrapper_template.format(id=id),globals()|locals(),locals())
|
|
1395
|
+
store_state = eval(f"numba.njit()(store_wrapper_{id})")
|
|
1396
|
+
store_p_state = eval(f"numba.njit()(store_pointer_wrapper_{id})")
|
|
1397
|
+
load_state = eval(f"numba.njit()(load_wrapper_{id})")
|
|
1398
|
+
|
|
1399
|
+
spec.fn[kind][store_name] = store_state
|
|
1400
|
+
spec.fn[kind][store_p_name] = store_p_state
|
|
1401
|
+
spec.fn[kind][load_name] = load_state
|
|
1402
|
+
#exit()
|
|
1403
|
+
|
|
1404
|
+
if kind == "Event":
|
|
1405
|
+
# IO_SIZE, LOAD_MARGIN
|
|
1406
|
+
alloc_program = ext_fn(f"alloc_program_{suffix}", sig(vp,vp,usize))
|
|
1407
|
+
free_program = ext_fn(f"free_program_{suffix}", sig(void,vp))
|
|
1408
|
+
else:
|
|
1409
|
+
# ARENA SIZE, POOL SIZE, STACK SIZE
|
|
1410
|
+
alloc_program = ext_fn(f"alloc_program_{suffix}", sig(vp,vp,usize))
|
|
1411
|
+
free_program = ext_fn(f"free_program_{suffix}", sig(void,vp))
|
|
1412
|
+
|
|
1413
|
+
alloc_state = ext_fn(f"alloc_state_{suffix}", sig(vp))
|
|
1414
|
+
free_state = ext_fn(f"free_state_{suffix}", sig(void,vp))
|
|
1415
|
+
|
|
1416
|
+
complete = ext_fn(f"complete_{suffix}", sig(i32,vp))
|
|
1417
|
+
clear_flags = ext_fn(f"clear_flags_{suffix}", sig(void,vp))
|
|
1418
|
+
set_device = ext_fn(f"set_device_{suffix}", sig(void,i32))
|
|
1419
|
+
|
|
1420
|
+
# Finally, compile the entry functions, saving it for later use
|
|
1421
|
+
spec.fn[kind]['init_program'] = init_program
|
|
1422
|
+
spec.fn[kind]['exec_program'] = exec_program
|
|
1423
|
+
|
|
1424
|
+
|
|
1425
|
+
spec.fn[kind]['alloc_program'] = alloc_program
|
|
1426
|
+
spec.fn[kind]['free_program'] = free_program
|
|
1427
|
+
|
|
1428
|
+
spec.fn[kind]['alloc_state'] = alloc_state
|
|
1429
|
+
spec.fn[kind]['free_state'] = free_state
|
|
1430
|
+
|
|
1431
|
+
@njit(sig(boolean,vp))
|
|
1432
|
+
def complete_wrapper(instance):
|
|
1433
|
+
result = complete(instance)
|
|
1434
|
+
return (result != 0)
|
|
1435
|
+
|
|
1436
|
+
spec.fn[kind]['complete'] = complete_wrapper
|
|
1437
|
+
spec.fn[kind]['clear_flags'] = clear_flags
|
|
1438
|
+
spec.fn[kind]['set_device'] = set_device
|
|
1439
|
+
|
|
1440
|
+
debug_print("Bound and loaded")
|
|
1441
|
+
|
|
1442
|
+
|
|
1443
|
+
@staticmethod
|
|
1444
|
+
def bind_and_load_specs():
|
|
1445
|
+
RuntimeSpec.bind_specs()
|
|
1446
|
+
RuntimeSpec.load_specs()
|
|
1447
|
+
|
|
1448
|
+
|
|
1449
|
+
class AsyncRuntimeSpec(RuntimeSpec):
|
|
1450
|
+
def __init__(self,spec_name,state_spec,base_fns,async_fns,gpu_platform,
|
|
1451
|
+
**kwargs
|
|
1452
|
+
):
|
|
1453
|
+
super(AsyncRuntime,self).__init__(name='AsyncRuntime')
|
|
1454
|
+
pass
|
|
1455
|
+
|
|
1456
|
+
class EventRuntimeSpec(RuntimeSpec):
|
|
1457
|
+
def __init__(self):
|
|
1458
|
+
super(EventRuntime,self).__init__(name='EventRuntime')
|
|
1459
|
+
pass
|
|
1460
|
+
|
|
1461
|
+
|
|
1462
|
+
#class RuntimeType(numba.types.Type):
|
|
1463
|
+
#
|
|
1464
|
+
# def __init__(self):
|
|
1465
|
+
# super(Instance,self).__init__(name='Runtime')
|
|
1466
|
+
#
|
|
1467
|
+
#
|
|
1468
|
+
#runtime_type = RuntimeType()
|
|
1469
|
+
#
|
|
1470
|
+
#@typeof_impl.register(RuntimeType)
|
|
1471
|
+
#def typeof_index(val, c):
|
|
1472
|
+
# return runtime_type
|
|
1473
|
+
#
|
|
1474
|
+
#as_numba_type.register(Runtime,runtime_type)
|
|
1475
|
+
#
|
|
1476
|
+
#@numba.extending.type_callable(Runtime)
|
|
1477
|
+
#def type_instance(context):
|
|
1478
|
+
# def typer(kind):
|
|
1479
|
+
# if isinstance(kind.context_ptr,numba.types.uintp):
|
|
1480
|
+
# return runtime_type
|
|
1481
|
+
# else:
|
|
1482
|
+
# return None
|
|
1483
|
+
# return typer
|
|
1484
|
+
#
|
|
1485
|
+
#@numba.extending.register_model(RuntimeType)
|
|
1486
|
+
#class RuntimeModel(models.RuntimeModel):
|
|
1487
|
+
# def __init__(self, dmm, fe_type):
|
|
1488
|
+
#
|
|
1489
|
+
# state = fe_type.spec.dev_state
|
|
1490
|
+
# context = numba.from_dtype(spec.meta['DEV_CTX_TYPE'][kind])
|
|
1491
|
+
# members = [
|
|
1492
|
+
# ('context_ptr', context),
|
|
1493
|
+
# ('state_ptr', state),
|
|
1494
|
+
# ]
|
|
1495
|
+
# models.StructModel.__init__(self, dmm, fe_type, members)
|
|
1496
|
+
#
|
|
1497
|
+
#numba.extending.make_attribute_wrapper(InstanceType,'context_ptr','context_ptr')
|
|
1498
|
+
#numba.extending.make_attribute_wrapper(InstanceType,'state_ptr', 'state_ptr')
|
|
1499
|
+
#
|
|
1500
|
+
#
|
|
1501
|
+
|