openaccel-lab
aien-dev/openaccel-lab
Brought by drakestapleton
openaccel-lab / runners / cuda / host.cu
| 1 | // SPDX-License-Identifier: Apache-2.0 |
| 2 | // OpenAccel Lab CUDA entrant host. The runner writes workloads/<name>/kernel.cu followed by this file |
| 3 | // into one translation unit; kernel.cu defines |
| 4 | // void oal_launch(const long long *p, void *const *buf); |
| 5 | // p holds the case parameters in manifest order, buf the device buffers: inputs, outputs, then the |
| 6 | // scratch buffers whose byte sizes are this program's arguments. |
| 7 | // stdin: one case container (docs/CONTAINER.md). stdout: outputs, each padded to 8 bytes. |
| 8 | // stderr: "oal-time-ns N" per timed repeat (launch + cudaDeviceSynchronize, host clock). |
| 9 | // Exit 2 on a malformed case, 3 on a CUDA error. |
| 10 | // The HIP entrant is this file and kernel.cu passed through hipify-perl unchanged by hand. |
| 11 | #include <cuda_runtime.h> |
| 12 | #include <chrono> |
| 13 | #include <cstdio> |
| 14 | #include <cstdlib> |
| 15 | #include <cstring> |
| 16 | #include <vector> |
| 17 | |
| 18 | #define OAL_CK(x) do { cudaError_t e_ = (x); if (e_ != cudaSuccess) { \ |
| 19 | std::fprintf(stderr, "cuda error %s at %s:%d\n", cudaGetErrorString(e_), __FILE__, __LINE__); \ |
| 20 | std::exit(3); } } while (0) |
| 21 | |
| 22 | static unsigned long long oal_rd64(const unsigned char *b) { |
| 23 | unsigned long long v = 0; |
| 24 | for (int k = 7; k >= 0; k--) v = (v << 8) | b[k]; |
| 25 | return v; |
| 26 | } |
| 27 | |
| 28 | static size_t oal_pad8(size_t n) { return (n + 7) / 8 * 8; } |
| 29 | |
| 30 | int main(int argc, char **argv) { |
| 31 | std::vector<unsigned char> in; |
| 32 | unsigned char chunk[65536]; |
| 33 | size_t got; |
| 34 | while ((got = std::fread(chunk, 1, sizeof chunk, stdin)) > 0) in.insert(in.end(), chunk, chunk + got); |
| 35 | if (in.size() < 48 || std::memcmp(in.data(), "OALCASE1", 8) != 0) { std::fprintf(stderr, "bad case\n"); return 2; } |
| 36 | unsigned long long np = oal_rd64(&in[8]), nin = oal_rd64(&in[16]), nout = oal_rd64(&in[24]); |
| 37 | unsigned long long warm = oal_rd64(&in[32]), reps = oal_rd64(&in[40]); |
| 38 | if (np > 32 || nin + nout > 32) { std::fprintf(stderr, "bad case counts\n"); return 2; } |
| 39 | size_t o = 48; |
| 40 | std::vector<long long> p(np + 1); |
| 41 | for (unsigned long long k = 0; k < np; k++, o += 8) p[k] = (long long)oal_rd64(&in[o]); |
| 42 | std::vector<size_t> sz(nin + nout); |
| 43 | for (unsigned long long k = 0; k < nin + nout; k++, o += 8) sz[k] = (size_t)oal_rd64(&in[o]); |
| 44 | int nscr = argc - 1; |
| 45 | std::vector<void *> buf(nin + nout + nscr + 1); |
| 46 | for (unsigned long long k = 0; k < nin; k++) { |
| 47 | if (o + sz[k] > in.size()) { std::fprintf(stderr, "case truncated\n"); return 2; } |
| 48 | OAL_CK(cudaMalloc(&buf[k], sz[k] ? sz[k] : 1)); |
| 49 | OAL_CK(cudaMemcpy(buf[k], &in[o], sz[k], cudaMemcpyHostToDevice)); |
| 50 | o += oal_pad8(sz[k]); |
| 51 | } |
| 52 | for (unsigned long long k = nin; k < nin + nout; k++) { |
| 53 | OAL_CK(cudaMalloc(&buf[k], sz[k] ? sz[k] : 1)); |
| 54 | OAL_CK(cudaMemset(buf[k], 0xFF, sz[k])); |
| 55 | } |
| 56 | for (int s = 0; s < nscr; s++) { |
| 57 | size_t n = (size_t)std::strtoull(argv[1 + s], nullptr, 10); |
| 58 | OAL_CK(cudaMalloc(&buf[nin + nout + s], n ? n : 1)); |
| 59 | OAL_CK(cudaMemset(buf[nin + nout + s], 0, n)); |
| 60 | } |
| 61 | OAL_CK(cudaDeviceSynchronize()); |
| 62 | for (unsigned long long r = 0; r < warm + reps; r++) { |
| 63 | auto t0 = std::chrono::steady_clock::now(); |
| 64 | oal_launch(p.data(), buf.data()); |
| 65 | OAL_CK(cudaGetLastError()); |
| 66 | OAL_CK(cudaDeviceSynchronize()); |
| 67 | auto t1 = std::chrono::steady_clock::now(); |
| 68 | if (r >= warm) |
| 69 | std::fprintf(stderr, "oal-time-ns %lld\n", |
| 70 | (long long)std::chrono::duration_cast<std::chrono::nanoseconds>(t1 - t0).count()); |
| 71 | } |
| 72 | std::vector<unsigned char> out; |
| 73 | for (unsigned long long k = nin; k < nin + nout; k++) { |
| 74 | size_t base = out.size(); |
| 75 | out.resize(base + oal_pad8(sz[k]), 0); |
| 76 | OAL_CK(cudaMemcpy(&out[base], buf[k], sz[k], cudaMemcpyDeviceToHost)); |
| 77 | } |
| 78 | std::fwrite(out.data(), 1, out.size(), stdout); |
| 79 | std::fflush(stdout); |
| 80 | return 0; |
| 81 | } |