/* * Cycle 4 Phase 6 — QPU bench for VP9 wd=8 LPF. * Mirrors bench_v3d_lpf.c (cycle 2); changes: calls the wd=8 ref * + asserts dst_stride >= 6 (cycle 4 contract). */ #define _POSIX_C_SOURCE 200809L #include #include #include #include #include #include #include #include #include #include "v3d_runner.h" extern void daedalus_vp9_loop_filter_h_8_8_ref( uint8_t *dst, ptrdiff_t stride, int E, int I, int H); #define EDGE_STRIDE 8 #define EDGE_BYTES 64 static uint64_t xs_state; static inline uint64_t xs(void) { uint64_t x = xs_state; x ^= x<<13; x ^= x>>7; x ^= x<<17; return xs_state = x; } static void gen_edge_pixels(uint8_t *buf) { int a = (int)(xs() % 200) + 20; int b = (int)(xs() % 200) + 20; int n = (int)(xs() % 30); for (int r = 0; r < 8; r++) for (int c = 0; c < 8; c++) { int base = (c < 4) ? a : b; int noise = ((int)(xs() % (2*n + 1))) - n; int v = base + noise; buf[r*EDGE_STRIDE + c] = (uint8_t)(v < 0 ? 0 : v > 255 ? 255 : v); } } static void gen_thresholds(int *E, int *I, int *H) { *E = (int)(xs() % 81); *I = (int)(xs() % 41); *H = (int)(xs() % 11); } static double now_seconds(void) { struct timespec ts; clock_gettime(CLOCK_MONOTONIC_RAW, &ts); return ts.tv_sec + ts.tv_nsec * 1e-9; } typedef struct { uint32_t n_edges, blocks_per_row, dst_stride_u8, _pad; } push_consts; int main(int argc, char **argv) { int n_edges = 65536, iters = 100, verify_only = 0; uint64_t seed = 0; const char *spv = "v3d_lpf_h_8_8.spv"; static struct option opts[] = { {"edges", required_argument, 0, 'e'}, {"iters", required_argument, 0, 'i'}, {"seed", required_argument, 0, 's'}, {"spv", required_argument, 0, 'S'}, {"verify-only", no_argument, 0, 'V'}, {0,0,0,0} }; for (int c; (c = getopt_long(argc, argv, "e:i:s:S:V", opts, 0)) != -1;) { switch (c) { case 'e': n_edges = atoi(optarg); break; case 'i': iters = atoi(optarg); break; case 's': seed = strtoull(optarg, 0, 0); break; case 'S': spv = optarg; break; case 'V': verify_only = 1; break; default: return 2; } } xs_state = seed ? seed : 0xa57edbeef5717ULL; v3d_runner *r = v3d_runner_create(); if (!r) return 1; printf("=== v3d LPF h_8_8 bench ===\n"); printf(" device: %s\n n_edges: %d iters: %d\n", v3d_runner_device_name(r), n_edges, iters); size_t dst_bytes = (size_t) n_edges * EDGE_BYTES; size_t meta_bytes = (size_t) n_edges * 4 * sizeof(uint32_t); v3d_buffer buf_meta = {0}, buf_dst = {0}; v3d_runner_create_buffer(r, meta_bytes, &buf_meta); v3d_runner_create_buffer(r, dst_bytes, &buf_dst); uint8_t *master = malloc(dst_bytes); uint8_t *expected = malloc(dst_bytes); int *Es = malloc(n_edges*sizeof(int)), *Is = malloc(n_edges*sizeof(int)), *Hs = malloc(n_edges*sizeof(int)); for (int i = 0; i < n_edges; i++) { gen_edge_pixels(master + (size_t)i * EDGE_BYTES); gen_thresholds(&Es[i], &Is[i], &Hs[i]); } memcpy(expected, master, dst_bytes); for (int i = 0; i < n_edges; i++) daedalus_vp9_loop_filter_h_8_8_ref(expected + (size_t)i * EDGE_BYTES + 4, EDGE_STRIDE, Es[i], Is[i], Hs[i]); uint32_t dst_stride = EDGE_STRIDE; assert(dst_stride >= 6 && "cycle 4 §4 contract: dst_stride_u8 >= 6 (flat8in 6-write)"); uint32_t *meta = buf_meta.mapped; for (int i = 0; i < n_edges; i++) { uint32_t mx = (uint32_t)((size_t)i * EDGE_BYTES + 4); assert(mx >= 4); meta[4*i + 0] = mx; meta[4*i + 1] = (uint32_t) Es[i]; meta[4*i + 2] = (uint32_t) Is[i]; meta[4*i + 3] = (uint32_t) Hs[i]; } memcpy(buf_dst.mapped, master, dst_bytes); v3d_pipeline pipe = {0}; if (v3d_runner_create_pipeline(r, spv, 2, sizeof(push_consts), &pipe)) return 1; v3d_buffer bufs[2] = { buf_meta, buf_dst }; v3d_runner_bind_buffers(r, &pipe, bufs, 2); const uint32_t edges_per_wg = 32; uint32_t gc = (uint32_t)((n_edges + edges_per_wg - 1) / edges_per_wg); push_consts pc = { .n_edges = (uint32_t) n_edges, .dst_stride_u8 = dst_stride }; VkCommandBuffer cb = v3d_runner_alloc_cmdbuf(r); VkCommandBufferBeginInfo cbbi = { .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO }; vkBeginCommandBuffer(cb, &cbbi); vkCmdBindPipeline(cb, VK_PIPELINE_BIND_POINT_COMPUTE, pipe.pipeline); vkCmdBindDescriptorSets(cb, VK_PIPELINE_BIND_POINT_COMPUTE, pipe.layout, 0, 1, &pipe.desc_set, 0, NULL); vkCmdPushConstants(cb, pipe.layout, VK_SHADER_STAGE_COMPUTE_BIT, 0, sizeof(pc), &pc); vkCmdDispatch(cb, gc, 1, 1); vkEndCommandBuffer(cb); /* M1'''' */ printf("\n=== M1'''': QPU vs C bit-exact ===\n"); memcpy(buf_dst.mapped, master, dst_bytes); if (v3d_runner_submit_wait(r, cb)) return 1; int mis = 0, bytediffs = 0; for (int i = 0; i < n_edges; i++) { const uint8_t *q = (uint8_t *) buf_dst.mapped + (size_t)i * EDGE_BYTES; const uint8_t *e = expected + (size_t)i * EDGE_BYTES; if (memcmp(q, e, EDGE_BYTES) != 0) { int d = 0; for (int j = 0; j < EDGE_BYTES; j++) if (q[j] != e[j]) d++; bytediffs += d; if (mis < 3) fprintf(stderr, "MISMATCH edge %d (E=%d I=%d H=%d): %d/64 bytes\n", i, Es[i], Is[i], Hs[i], d); mis++; } } printf(" edges bit-exact: %d / %d (%.4f%%)\n", n_edges - mis, n_edges, 100.0 * (n_edges - mis) / n_edges); if (mis > 0) { fprintf(stderr, "REFUSING throughput on broken kernel.\n"); return 1; } if (verify_only) return 0; /* M2'''' */ printf("\n=== M2'''': QPU throughput ===\n"); for (int i = 0; i < 10; i++) { memcpy(buf_dst.mapped, master, dst_bytes); v3d_runner_submit_wait(r, cb); } double t0 = now_seconds(); for (int i = 0; i < iters; i++) { memcpy(buf_dst.mapped, master, dst_bytes); v3d_runner_submit_wait(r, cb); } double t1 = now_seconds(); double s0 = now_seconds(); for (int i = 0; i < iters; i++) memcpy(buf_dst.mapped, master, dst_bytes); double s1 = now_seconds(); double ks = (t1 - t0) - (s1 - s0); double total = (double) n_edges * iters; double mes = total / ks / 1e6; printf(" edges/dispatch: %d, iters: %d, total: %.0f\n", n_edges, iters, total); printf(" elapsed (kernel)=%.6f s\n per-edge = %.1f ns\n per-dispatch = %.1f us\n", ks, ks / total * 1e9, ks / iters * 1e6); printf(" M2'''' = %.3f Medge/s\n", mes); double M3 = 52.382; /* k4 phase 3 baseline */ double R = mes / M3; printf("\n Cycle 4 NEON M3'''' = %.3f Medge/s\n", M3); printf(" R'''' = M2''''/M3'''' = %.3f\n", R); if (R >= 1.0) printf(" decision band = GREEN\n"); else if (R >= 0.5) printf(" decision band = YELLOW\n"); else if (R >= 0.1) printf(" decision band = ORANGE\n"); else printf(" decision band = RED\n"); double floor30 = 64530.0 * 30 / 1e6; printf(" 30fps@1080p floor : %.3f Medge/s — %.1fx margin\n", floor30, mes / floor30); v3d_runner_destroy_pipeline(r, &pipe); v3d_runner_destroy_buffer(r, &buf_dst); v3d_runner_destroy_buffer(r, &buf_meta); v3d_runner_destroy(r); return 0; }