interplane.Get Omega
Read in your language

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

branch main Branches Tags History

CUDApp / tests / cmdlist_plan_test.c

120 lines · 6938 bytes Raw Blame History
1/* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */
2/* Independent host oracle: enumerate overlapping half-open intervals, then drain
3 * all outstanding dispatches at the first conflicting dispatch. No GPU access. */
4#include "cudapp_native.h"
5#include "omega_blackwell_qmd.h"
6#include <stdint.h>
7#include <stdio.h>
8#include <stdlib.h>
9#include <string.h>
10#define MAX 4096u
11#define STRIDE 49u
12#define SLOTS (MAX * STRIDE)
13static uint64_t program[SLOTS], barriers[MAX], reference[MAX];
14static unsigned failures;
15static uint64_t seed = 0x9e3779b97f4a7c15ull;
16static uint64_t rnd(void) { seed ^= seed << 13; seed ^= seed >> 7; seed ^= seed << 17; return seed; }
17static void check(int ok, const char *s) { if (!ok) { fprintf(stderr,"FAIL %s\n",s); failures++; } }
18static unsigned conflict(unsigned a, unsigned b) {
19 const uint64_t *x=program+a*STRIDE,*y=program+b*STRIDE;
20 if (x[0]==17 || y[0]==17) return 7;
21 unsigned hazard=0;
22 for (unsigned i=0;i<x[0];i++) for (unsigned j=0;j<y[0];j++) {
23 const uint64_t *p=x+1+3*i,*q=y+1+3*j;
24 if (p[0] < q[0]+q[1] && q[0] < p[0]+p[1]) {
25 if ((q[2]&2) && (p[2]&1)) hazard|=1; /* RAW */
26 if ((q[2]&1) && (p[2]&2)) hazard|=2; /* WAR */
27 if ((q[2]&2) && (p[2]&2)) hazard|=4; /* WAW */
28 }
29 }
30 return hazard;
31}
32static uint64_t ref(unsigned n, unsigned mode, unsigned stamps, uint64_t *waits) {
33 unsigned first=0; *waits=n && stamps ? 1 : 0;
34 uint64_t words=35+(stamps?8:0);
35 for(unsigned i=0;i<n;i++) {
36 unsigned h=0;
37 for(unsigned j=first;j<i;j++) h|=conflict(i,j);
38 reference[i]=i==0?8:h;
39 if(reference[i]) { ++*waits; first=i; }
40 words+=(mode?198:4)+(reference[i]?4:0)+(stamps?6:0);
41 }
42 return n?words:0;
43}
44static int plan(unsigned n,unsigned cap,unsigned mode,unsigned stamps,uint64_t budget,uint64_t out[16]) {
45 uint64_t a[16]={n,cap,mode,stamps,budget,0},r=999;
46 memset(out,0,16*sizeof *out);
47 return omega_cmdlist_plan(&r,a,16,program,SLOTS,barriers,MAX,out,16)==0 && r==0;
48}
49static void compare(unsigned n,unsigned mode,unsigned stamps) {
50 uint64_t out[16],waits,words=ref(n,mode,stamps,&waits);
51 check(plan(n,MAX,mode,stamps,1000000,out),"plan accepted");
52 check(out[0]==words && out[1]==waits && out[2]==(n?1u:0u),"exact words/waits/submissions");
53 check(memcmp(reference,barriers,n*sizeof *barriers)==0,"hazard classes and outstanding epoch");
54}
55static void edge_tests(void) {
56 uint64_t out[16]; memset(program,0,sizeof program);
57 compare(0,0,0); compare(1,0,0); compare(MAX,0,0);
58 check(!plan(MAX+1,MAX,0,0,1000000,out),"capacity+1 refused");
59 check(!plan(1,0,0,0,1000000,out),"zero capacity refused");
60 check(plan(1,1,0,0,43,out),"exact word capacity accepted");
61 check(!plan(1,1,0,0,42,out),"word capacity+1 refused");
62 program[0]=1;program[1]=100;program[2]=16;program[3]=2;
63 program[STRIDE]=1;program[STRIDE+1]=108;program[STRIDE+2]=8;program[STRIDE+3]=1;
64 compare(2,0,0); check(barriers[1]==1,"overlapping VA alias RAW");
65 program[3]=1;program[STRIDE+3]=2;compare(2,0,0);check(barriers[1]==2,"WAR");
66 program[3]=2;compare(2,0,0);check(barriers[1]==4,"WAW");
67 program[STRIDE+1]=116;compare(2,0,0);check(barriers[1]==0,"adjacent ranges do not alias");
68 program[2]=0;check(!plan(2,MAX,0,0,1000000,out),"zero-byte access refused");
69 program[2]=32;program[1]=UINT64_MAX-7;check(!plan(2,MAX,0,0,1000000,out),"wrapping access refused");
70 memset(program,0,sizeof program);program[0]=17;compare(3,0,0);check(barriers[1]==7,"unknown access conservative");
71 memset(program,0,sizeof program);
72 uint64_t a[16]={1,1,0,0,47,4},r=0;
73 check(omega_cmdlist_plan(&r,a,16,program,SLOTS,barriers,MAX,out,16)==0&&r==0,"platform prefix exact fit");
74 a[5]=5;omega_cmdlist_plan(&r,a,16,program,SLOTS,barriers,MAX,out,16);check(r!=0,"platform prefix overflow refused");
75 a[5]=0;
76 for(unsigned k=6;k<=9;k++) {
77 a[k]=1;omega_cmdlist_plan(&r,a,16,program,SLOTS,barriers,MAX,out,16);
78 check(r!=0,"manual barrier/skip/first-none/cross-wait refused");a[k]=0;
79 }
80 a[0]=32;a[1]=5;a[2]=10;
81 omega_cmdlist_layout(&r,3,a,16,out,16);check(r==15,"bounded next chunk");
82 a[1]=30;omega_cmdlist_layout(&r,3,a,16,out,16);check(r==32,"final chunk clamps to count");
83 a[2]=0;omega_cmdlist_layout(&r,3,a,16,out,16);check(r==UINT64_MAX,"zero chunk room refused");
84}
85static void records(void) {
86 uint64_t a[16]={4,0,32,128,1,3,2,0x1234567800ull,81,2049,0x20000000,0x30000000,0x40000000,0x50000000};
87 uint64_t args[32],words[512],r=0; uint32_t bank[224],q1[96],q0[96];
88 for(unsigned round=0;round<1000;round++) {
89 a[1]=round%4;a[8]=round==0?UINT32_MAX:rnd()%250;a[9]=rnd()%32769;a[4]=rnd()%4;a[5]=1+rnd()%31;
90 for(unsigned j=0;j<32;j++) args[j]=(uint32_t)rnd();
91 check(omega_cmdlist_record(&r,a,16,args,32,words,512)==0 && r==0,"record accepted");
92 uint64_t bv=a[10]+a[1]*1024,qv=a[11]+a[1]*1024;
93 omega_blackwell_build_cbank_driver_2d(bank,bv,(uint32_t)a[3],a[4]?(uint32_t)a[4]:1,(uint32_t)a[5],(uint32_t)a[6]);
94 for(unsigned j=0;j<224;j++) check(words[j]==bank[j],"bank differential");
95 for(unsigned j=0;j<32;j++) check(words[224+j]==args[j],"argument record");
96 uint32_t gp=a[8]>64?((uint32_t)a[8]+7)/8*8:64;
97 OmegaBlackwellQmdConfig c={.code_va=a[7],.cbank_va=bv,.scratch_va=a[13],.sem_va=a[12]+a[1]*16,.qmd0_va=qv+512,.qmd1_va=qv,.threads_x=(uint32_t)a[3],.threads_y=a[4]?(uint32_t)a[4]:1,.grid_x=(uint32_t)a[5],.grid_y=(uint32_t)a[6],.gpr_count=gp,.shared_bytes=(uint32_t)a[9]};
98 check(omega_blackwell_build_qmd1(q1,&c)==0,"C QMD oracle");omega_blackwell_build_qmd0(q0,qv+512,qv);
99 for(unsigned j=0;j<96;j++) { check(words[256+j]==q1[j],"QMD1 differential");check(words[384+j]==q0[j],"QMD0 differential"); }
100 }
101 a[1]=4;check(omega_cmdlist_record(&r,a,16,args,32,words,512)==0 && r==2,"record full refusal");
102 a[1]=0;a[9]=32769;check(omega_cmdlist_record(&r,a,16,args,32,words,512)==0 && r!=0,"shared bound refusal");
103 a[9]=0;a[0]=MAX;a[1]=MAX-1;
104 check(omega_cmdlist_record(&r,a,16,args,32,words,512)==0 && r==0,"full-capacity final record accepted");
105 a[1]=MAX;check(omega_cmdlist_record(&r,a,16,args,32,words,512)==0 && r==2,"capacity+1 record refused");
106}
107int main(int argc,char **argv) {
108 (void)argc;(void)argv; edge_tests();
109 for(unsigned t=0;t<12000;t++) {
110 unsigned n=1+rnd()%32; memset(program,0,n*STRIDE*sizeof *program);
111 for(unsigned i=0;i<n;i++) { uint64_t *p=program+i*STRIDE;p[0]=rnd()%17;
112 for(unsigned j=0;j<p[0];j++) { p[1+3*j]=4096+(rnd()%32)*16;p[2+3*j]=1+rnd()%64;p[3+3*j]=1+rnd()%3; }
113 }
114 compare(n,t&1,(t>>1)&1);
115 if(failures) break;
116 }
117 records();
118 printf("%s cmdlist independent differential: 12000 randomized programs, edges, 1000 launch records\n",failures?"FAIL":"PASS");
119 return failures?1:0;
120}