openaccel-lab
aien-dev/openaccel-lab
Brought by drakestapleton
openaccel-lab / oal / entrants / opencl.py
| 1 | # SPDX-License-Identifier: Apache-2.0 |
| 2 | """OpenCL C entrant host: python -m oal.entrants.opencl WORKLOAD [SCRATCH_BYTES...] < case > outputs. |
| 3 | |
| 4 | Builds workloads/<name>/<backends.opencl.src> on the first OpenCL CPU or GPU device (OAL_OPENCL_PLATFORM |
| 5 | picks a platform by substring), runs every launch in backends.opencl.launches, and times warmup+repeats |
| 6 | rounds of all launches plus clFinish. Output buffers start as 0xFF bytes so an unwritten element is NaN |
| 7 | (f32) or -1 (integers). Exit 2 on a build failure, 3 on a runtime failure. |
| 8 | |
| 9 | INTERIM host glue (Python + pyopencl). Successor: the Omega OpenCL host of aien-dev/omega-mv |
| 10 | seed/spirv (opencl-host.c today), once it takes a case container. |
| 11 | """ |
| 12 | import os |
| 13 | import sys |
| 14 | import time |
| 15 | |
| 16 | import numpy as np |
| 17 | import pyopencl as cl |
| 18 | |
| 19 | from oal import case, manifest |
| 20 | |
| 21 | |
| 22 | def pick_device(): |
| 23 | want = os.environ.get("OAL_OPENCL_PLATFORM", "") |
| 24 | for plat in cl.get_platforms(): |
| 25 | if want and want.lower() not in plat.name.lower(): |
| 26 | continue |
| 27 | devs = plat.get_devices() |
| 28 | if devs: |
| 29 | return plat, devs[0] |
| 30 | raise SystemExit("opencl: no platform matching %r" % want) |
| 31 | |
| 32 | |
| 33 | def main(): |
| 34 | name = sys.argv[1] |
| 35 | m = manifest.load(name) |
| 36 | spec = m["backends"]["opencl"] |
| 37 | blob = sys.stdin.buffer.read() |
| 38 | pvals, sizes, ins, warm, reps = case.unpack(blob) |
| 39 | params = dict(zip(manifest.params_of(m).keys(), pvals)) |
| 40 | plat, dev = pick_device() |
| 41 | sys.stderr.write("oal-version opencl %s | %s | %s\n" % (plat.name, plat.version, dev.name)) |
| 42 | ctx = cl.Context([dev]) |
| 43 | q = cl.CommandQueue(ctx) |
| 44 | src = open(os.path.join(m["_dir"], spec["src"])).read() |
| 45 | try: |
| 46 | prog = cl.Program(ctx, src).build(options=spec.get("options", "")) |
| 47 | except cl.RuntimeError as e: |
| 48 | sys.stderr.write("opencl build failed: %s\n" % e) |
| 49 | sys.exit(2) |
| 50 | mf = cl.mem_flags |
| 51 | bufs = {} |
| 52 | for k, t in enumerate(m["inputs"]): |
| 53 | bufs[t["name"]] = cl.Buffer(ctx, mf.READ_WRITE | mf.COPY_HOST_PTR, hostbuf=np.frombuffer(ins[k], np.uint8).copy()) |
| 54 | nin = len(m["inputs"]) |
| 55 | outs = [] |
| 56 | for k, t in enumerate(m["outputs"]): |
| 57 | host = np.full(sizes[nin + k], 0xFF, np.uint8) |
| 58 | bufs[t["name"]] = cl.Buffer(ctx, mf.READ_WRITE | mf.COPY_HOST_PTR, hostbuf=host) |
| 59 | outs.append(host) |
| 60 | for t in m.get("scratch", []): |
| 61 | nb = max(1, manifest.nbytes(t, params)) |
| 62 | bufs[t["name"]] = cl.Buffer(ctx, mf.READ_WRITE, size=nb) |
| 63 | launches = [] |
| 64 | for L in spec["launches"]: |
| 65 | kern = getattr(prog, L["kernel"]) |
| 66 | args = [] |
| 67 | for a in L["args"]: |
| 68 | if a in bufs: |
| 69 | args.append(bufs[a]) |
| 70 | elif isinstance(a, str) and a.startswith("local:"): |
| 71 | _, dt, count = a.split(":") |
| 72 | args.append(cl.LocalMemory(manifest.DTYPES[dt][1] * int(manifest.evaluate(count, params)))) |
| 73 | elif isinstance(a, str) and a.startswith("f32:"): |
| 74 | args.append(np.float32(manifest.evaluate(a[4:], params))) |
| 75 | else: |
| 76 | args.append(np.int32(manifest.evaluate(a, params))) |
| 77 | gsz = tuple(int(manifest.evaluate(g, params)) for g in L["global"]) |
| 78 | lsz = tuple(int(manifest.evaluate(g, params)) for g in L["local"]) if L.get("local") else None |
| 79 | launches.append((kern, gsz, lsz, args)) |
| 80 | try: |
| 81 | for r in range(warm + reps): |
| 82 | t0 = time.perf_counter_ns() |
| 83 | for kern, gsz, lsz, args in launches: |
| 84 | kern(q, gsz, lsz, *args) |
| 85 | q.finish() |
| 86 | t1 = time.perf_counter_ns() |
| 87 | if r >= warm: |
| 88 | sys.stderr.write("oal-time-ns %d\n" % (t1 - t0)) |
| 89 | for k, t in enumerate(m["outputs"]): |
| 90 | cl.enqueue_copy(q, outs[k], bufs[t["name"]]) |
| 91 | q.finish() |
| 92 | except cl.Error as e: |
| 93 | sys.stderr.write("opencl runtime failure: %s\n" % e) |
| 94 | sys.exit(3) |
| 95 | w = sys.stdout.buffer |
| 96 | for h in outs: |
| 97 | w.write(h.tobytes()) |
| 98 | w.write(b"\0" * (case.pad8(h.size) - h.size)) |
| 99 | w.flush() |
| 100 | |
| 101 | |
| 102 | if __name__ == "__main__": |
| 103 | main() |