Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/CUDApp/blob/main/tests/test_ampere_session.c # CUDApp CUDA++: AIEN’s GPU machine-code toolchain, runtime and driver client for NVIDIA GPUs without the CUDA toolkit. Hardware coverage and driver replacement limits are explicit. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `CUDApp / tests / test_ampere_session.c` 128 lines · 7523 bytes Raw Blame History 1 /* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */ 2 /* Real session/QMD path with an in-memory platform oracle. No GPU or ioctl. 3 * This proves ordering/serialization, not device cache coherence. */ 4 #include "omega_gpu_session.h" 5 #include "omega_gpu_platform_linux.h" 6 #include "omega_ampere_qmd.h" 7 #include 8 #include 9 #include 10 #include 11 static struct { void *cpu[16]; uint64_t va[16], bytes[16]; unsigned next, submits; uint64_t ns; } sim; 12 static uint64_t expected_code; 13 static uint32_t expected_arg; 14 static uint32_t expected_instruction; 15 static uint64_t now_ns(void *c) { (void)c; return sim.ns += 1000; } 16 static uint64_t now_ms(void *c) { return now_ns(c) / `1000000`; } 17 static int wait_ms(void *c, uint64_t ms) { (void)c; sim.ns += ms * `1000000`; return 0; } 18 static int lock(void *c, uint32_t id) { (void)c; (void)id; return 0; } 19 static const char *diag(void *c) { (void)c; return "simulated Ampere"; } 20 static int dev_open(void *c, uint32_t ord, OmegaGpuDeviceHandle *d) { 21 (void)c; assert(ord == 0); *d = (OmegaGpuDeviceHandle){15, 1}; return 0; 22 } 23 static int dev_close(void *c, OmegaGpuDeviceHandle d) { (void)c; (void)d; return 0; } 24 static int alloc(void *c, OmegaGpuDeviceHandle d, const OmegaGpuBufferDesc *b, OmegaGpuBufferHandle *h, 25 OmegaCpuView *cpu, OmegaGpuVa *va, OmegaGpuBusAddr *bus) { 26 (void)c; (void)d; unsigned i = ++sim.next; assert(i < 15); 27 sim.cpu[i] = calloc(1, b->bytes); assert(sim.cpu[i]); sim.bytes[i] = b->bytes; 28 sim.va[i] = 0x100000000ull + (uint64_t)i * 0x1000000ull; 29 *h = (OmegaGpuBufferHandle){i, 1}; *cpu = (OmegaCpuView){sim.cpu[i], b->bytes}; va->va = sim.va[i]; 30 if (bus) bus->iova = 0; 31 return 0; 32 } 33 static int dealloc(void *c, OmegaGpuBufferHandle h) { 34 (void)c; assert(h.index < 16); free(sim.cpu[h.index]); sim.cpu[h.index] = NULL; return 0; 35 } 36 static int sync_mem(void *c, OmegaGpuBufferHandle h, uint64_t off, uint64_t len) { 37 (void)c; assert(h.index < 16 && sim.cpu[h.index]); 38 assert(off <= sim.bytes[h.index] && len <= sim.bytes[h.index] - off); return 0; 39 } 40 static void *resolve(uint64_t va) { 41 for (unsigned i = 1; i <= sim.next; i++) 42 if (sim.cpu[i] && va >= sim.va[i] && va - sim.va[i] < sim.bytes[i]) return (uint8_t *)sim.cpu[i] + (va - sim.va[i]); 43 assert(!"unmapped simulated GPU address"); return NULL; 44 } 45 static int poll(void *c, OmegaGpuBufferHandle h, uint64_t off, uint32_t want, uint64_t ms, uint32_t *seen) { 46 (void)c; (void)ms; *seen = *(uint32_t *)((uint8_t *)sim.cpu[h.index] + off); assert(*seen == want); return 0; 47 } 48 static int qopen(void *c, OmegaGpuDeviceHandle d, OmegaGpuBufferHandle ring, OmegaGpuQueueHandle *q) { 49 (void)c; (void)d; (void)ring; *q = (OmegaGpuQueueHandle){14, 1}; return 0; 50 } 51 static int qclose(void *c, OmegaGpuQueueHandle q) { (void)c; (void)q; return 0; } 52 static int submit(void *c, OmegaGpuQueueHandle queue, const uint32_t *w, uint32_t n) { 53 (void)c; (void)queue; unsigned invalidated = 0, launched = 0; uint64_t qva = 0; 54 for (uint32_t i = 0; i < n;) { 55 uint32_t header = w[i++], count = (header >> 16) & 0x1fffu; 56 uint32_t method = (header & 0x1fffu) << 2, subc = (header >> 13) & 7u; 57 assert(header >> 28 == 2 && count && count <= n - i); 58 const uint64_t m[16] = {4, method, count, subc, w[i], OMEGA_AMPERE_COMPUTE_B, 59 0x02b4, 0x02c0, 0x005c, 0x0028, 0x021c, (1u << 0) | (1u << 4) | (1u << 12)}; 60 assert(omega_ampere_policy(m, NULL) == 0); 61 if (subc == 1 && method == 0x021c) invalidated++; 62 if (subc == 1 && method == 0x02b4) qva = (uint64_t)w[i] << 8; 63 if (subc == 1 && method == 0x02c0) { 64 assert(invalidated == 1 && qva && !launched++); 65 const uint32_t *q = resolve(qva); 66 uint64_t code = q[48] | ((uint64_t)(q[49] & 0x1ffffu) << 32); 67 uint64_t cb = q[32] | ((uint64_t)(q[33] & 0x1ffffu) << 32); 68 assert(*(uint32_t *)resolve(code) == expected_instruction); 69 assert(code == expected_code); 70 assert(*(uint32_t *)((uint8_t *)resolve(cb) + 0x160) == expected_arg); 71 uint64_t sem = q[24] | ((uint64_t)(q[25] & 0xffu) << 32); 72 assert(((q[25] >> 23) & 1u) == 1); /* QMD3 RELEASE0_ENABLE */ 73 *(uint32_t *)resolve(sem) = q[26]; 74 } 75 if (subc == 0 && method == 0x005c) { 76 assert(launched == 1); 77 *(uint32_t *)resolve(w[i] | ((uint64_t)w[i + 1] << 32)) = w[i + 2]; 78 } 79 i += count; 80 } 81 assert(invalidated == 1 && launched == 1); sim.submits++; return 0; 82 } 83 static void *halloc(void *c, size_t n) { (void)c; return malloc(n); } 84 static void hfree(void *c, void *p) { (void)c; free(p); } 85 static const OmegaGpuPlatformCaps caps = { 86 .abi_major = OMEGA_GPU_PLATFORM_ABI_MAJOR, .abi_minor = OMEGA_GPU_PLATFORM_ABI_MINOR, 87 .services = OMEGA_GPU_SVC_REQUIRED_SESSION, .max_devices = 1, .max_buffers = 14, .max_queues = 1, 88 .max_inflight = 1, .max_buffer_bytes = 1ull << 30, .max_total_bytes = 1ull << 32, 89 }; 90 static const OmegaGpuPlatform platform = { 91 .time_now_ms = now_ms, .wait_ms = wait_ms, .lock = lock, .unlock = lock, 92 .dma_alloc = alloc, .dma_free = dealloc, .sync_cpu_to_gpu = sync_mem, .sync_gpu_to_cpu = sync_mem, 93 .poll_word = poll, .device_open = dev_open, .device_close = dev_close, .diag_text = diag, 94 .queue_open = qopen, .queue_close = qclose, .queue_submit = submit, .queue_retire = qclose, 95 .time_now_ns = now_ns, .host_alloc = halloc, .host_free = hfree, 96 }; 97 /* Model the Linux-selected device target without linking a real driver. */ 98 const OmegaGpuPlatform *omega_gpu_platform_linux(OmegaGpuPlatformCaps *out) { if (out) *out = caps; return &platform; } 99 uint32_t omega_gpu_platform_linux_target_sm(void) { return 86; } 100 int main(void) { 101 omega_gpu_session_lock(); assert(omega_gpu_session_open()); 102 assert(omega_gpu_session_target_sm() == 86); 103 OmegaGpuMem code0, code1; 104 assert(omega_gpu_session_alloc_code(16, &code0) == 0); 105 assert(omega_gpu_session_alloc_code(16, &code1) == 0); 106 *(uint32_t *)code0.cpu = 0x1234; *(uint32_t *)code1.cpu = 0x5678; 107 assert(omega_gpu_session_sync_to_gpu(&code0) == 0 && omega_gpu_session_sync_to_gpu(&code1) == 0); 108 uint32_t arg = 7; 109 OmegaGpuLaunch launch = {.code_va = code0.va, .gpr_count = 64, .threads_x = 32, .grid_x = 1, .grid_y = 1, 110 .num_elements = 32, .args = &arg, .n_args = 1, .timeout_ms = 1000, .target_sm = 86}; 111 expected_code = launch.code_va; expected_arg = arg; expected_instruction = *(uint32_t *)code0.cpu; 112 assert(omega_gpu_session_launch(&launch, NULL, NULL) == 0); 113 arg = 11; launch.code_va = code1.va; expected_code = launch.code_va; expected_arg = arg; 114 expected_instruction = *(uint32_t *)code1.cpu; 115 assert(omega_gpu_session_launch(&launch, NULL, NULL) == 0); 116 /* Reuse the code VA with new content: invalidation remains mandatory. */ 117 *(uint32_t *)code1.cpu = 0x9abc; expected_instruction = *(uint32_t *)code1.cpu; 118 assert(omega_gpu_session_sync_to_gpu(&code1) == 0); 119 arg = 19; expected_arg = arg; 120 assert(omega_gpu_session_launch(&launch, NULL, NULL) == 0); 121 launch.target_sm = 0; 122 assert(omega_gpu_session_launch(&launch, NULL, NULL) != 0 && sim.submits == 3); 123 omega_gpu_session_free(&code0); omega_gpu_session_free(&code1); 124 omega_gpu_session_unlock(); 125 omega_gpu_session_close(); 126 puts("Ampere session ordering/argument/code serialization simulation passed (HOST_ONLY; device NOT_RUN)"); 127 return 0; 128 }