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
CUDApp / tests / dagsim / resolve_diff.c
| 1 | /* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */ |
| 2 | #include "dagsim.h" |
| 3 | #include "runtime/rx_costmodel.h" |
| 4 | #include <math.h> |
| 5 | #include <stdio.h> |
| 6 | #include <stdlib.h> |
| 7 | #include <string.h> |
| 8 | #ifndef CUDAPP_MIGRATION_PENDING_C_DAGSIM_RESOLVE |
| 9 | #error resolve migration guard is not implemented |
| 10 | #endif |
| 11 | extern int ds_resolved_pack(const ds_model *, const ds_opts *, uint64_t *, ds_result *); |
| 12 | extern int oracle_ds_resolved_pack(const ds_model *, const ds_opts *, uint64_t *, ds_result *); |
| 13 | extern int oracle_ds_diff_models(const ds_model *, const ds_model *, const ds_opts *, ds_diff *); |
| 14 | extern double oracle_ds_counterfactual(const ds_model *, const ds_opts *, int); |
| 15 | extern int oracle_ds_find_op(const ds_model *, const char *); |
| 16 | extern int oracle_ds_find_res(const ds_model *, const char *); |
| 17 | static unsigned checks, failed; |
| 18 | static int fault; |
| 19 | static int fail_alloc; |
| 20 | void *__real_malloc(size_t n); |
| 21 | void *__wrap_malloc(size_t n) { return fail_alloc ? NULL : __real_malloc(n); } |
| 22 | static RxCmFeatures feature; |
| 23 | static const char *label; |
| 24 | #define CK(c, msg) do { checks++; if (!(c)) { failed++; fprintf(stderr,"FAIL %s: %s\n",label,msg); } } while (0) |
| 25 | int __wrap_rx_cm_predict(const RxCostModel *m, const RxCmFeatures *f, uint32_t arm, RxCmPrediction *p) { |
| 26 | (void)m; (void)arm; feature=*f; memset(p,0,sizeof *p); |
| 27 | p->known=1; p->n=32; p->mean_log2_ps=20.0; p->sd_log2=0.25; p->dof=12.0; |
| 28 | if (fault==1) return RX_CM_ERR_UNSUPPORTED; |
| 29 | if (fault==2) p->refused=1; |
| 30 | if (fault==3) p->known=0; |
| 31 | if (fault==4) p->n=0; |
| 32 | return fault==5 ? RX_CM_ERR_ARG : RX_CM_OK; |
| 33 | } |
| 34 | static void init(ds_model *m) { |
| 35 | memset(m,0,sizeof *m); m->n_res=4; m->n_ops=2; |
| 36 | m->has_calibration=1; m->cal_taken_day=100; m->cal_valid_days=20; |
| 37 | strcpy(m->cal_id,"cal"); strcpy(m->instrument,"none"); |
| 38 | for(int i=0;i<4;i++) { |
| 39 | ds_resource *r=&m->res[i]; snprintf(r->id,sizeof r->id,"r%d",i); |
| 40 | r->kind=i+1; r->slots=1; r->cap=1000000000.0; r->lo=500000000.0; r->hi=2000000000.0; r->prov=DS_PROV_SPECIFIED; |
| 41 | } |
| 42 | for(int i=0;i<2;i++) { |
| 43 | ds_op *p=&m->ops[i]; snprintf(p->id,sizeof p->id,"op%d",i); |
| 44 | p->kind=DS_OP_COMPUTE; p->engine=0; p->t_ns=16.0; p->lo=8.0; p->hi=32.0; |
| 45 | p->t_prov=DS_PROV_SPECIFIED; p->has_t=1; p->bytes=8.0; p->bw=1; |
| 46 | p->launch_ns=4.0; p->launcher=3; p->M=64; p->N=16; strcpy(p->src,"source"); |
| 47 | } |
| 48 | } |
| 49 | static void packcheck(ds_model *m, ds_opts *o) { |
| 50 | uint64_t a[8192],b[8192]; ds_result x,y; memset(a,0xa5,sizeof a); memset(b,0xa5,sizeof b); |
| 51 | memset(&x,0,sizeof x); memset(&y,0,sizeof y); |
| 52 | memset(&feature,0,sizeof feature); |
| 53 | int sy=oracle_ds_resolved_pack(m,o,b,&y); RxCmFeatures expected=feature; |
| 54 | int sx=ds_resolved_pack(m,o,a,&x); |
| 55 | if(getenv("DS_RESOLVE_RED")) a[0]^=1; |
| 56 | CK(sx==sy,"pack status"); CK(!memcmp(&x,&y,sizeof x),"refusal bytes/state"); |
| 57 | if(!sx) CK(!memcmp(a,b,sizeof a),"packed model bytes"); |
| 58 | CK(!memcmp(&feature,&expected,sizeof feature),"costmodel features"); |
| 59 | } |
| 60 | static void diffcheck(ds_model *b, ds_model *t, ds_opts *o) { |
| 61 | ds_diff x,y; int sy=oracle_ds_diff_models(b,t,o,&y); int sx=ds_diff_models(b,t,o,&x); |
| 62 | if(getenv("DS_RESOLVE_RED")) x.n_added^=1; |
| 63 | CK(sx==sy,"diff status"); CK(!memcmp(&x,&y,sizeof x),"diff fields/reason/accounting"); |
| 64 | } |
| 65 | static void resolve_cases(void) { |
| 66 | ds_model m; ds_opts o={.asof_day=120,.interval_q=0.9}; |
| 67 | label="resolve modes/provenance"; |
| 68 | for(int mode=-1;mode<=1;mode++) for(int prov=0;prov<=4;prov++) for(int where=0;where<3;where++) { |
| 69 | init(&m); o.mode=mode; |
| 70 | if(where==0) m.res[1].prov=prov; |
| 71 | if(where==1) m.ops[0].t_prov=prov; |
| 72 | if(where==2) { m.ops[0].t_prov=prov; m.ops[0].src[0]=0; } |
| 73 | packcheck(&m,&o); |
| 74 | } |
| 75 | label="resolve calibration boundary"; |
| 76 | for(int has=0;has<=1;has++) for(int day=119;day<=121;day++) for(int via=0;via<2;via++) { |
| 77 | init(&m); m.has_calibration=has; o.asof_day=day; |
| 78 | if(via) m.ops[0].t_prov=DS_PROV_MEASURED; else m.res[2].prov=DS_PROV_FITTED; |
| 79 | packcheck(&m,&o); |
| 80 | } |
| 81 | label="resolve host clock"; init(&m); m.ops[0].t_prov=DS_PROV_MEASURED; |
| 82 | for(int day=-1;day<=0;day++) { o.asof_day=day; packcheck(&m,&o); } |
| 83 | o.asof_day=120; |
| 84 | label="resolve engine refusals"; |
| 85 | for(int kind=1;kind<=6;kind++) for(int launcher=-1;launcher<=0;launcher++) { |
| 86 | init(&m); m.ops[0].kind=kind; m.ops[0].engine=-1; m.ops[0].launcher=launcher; packcheck(&m,&o); |
| 87 | } |
| 88 | label="resolve prediction branches"; |
| 89 | double qs[]={-1.0,0.0,0.5,0.9,1.0,2.0,NAN}; |
| 90 | for(int mode=-1;mode<=1;mode++) for(int f=0;f<=5;f++) for(unsigned q=0;q<sizeof qs/sizeof *qs;q++) { |
| 91 | init(&m); m.ops[0].kind=DS_OP_MATVEC; m.ops[0].has_t=0; o.mode=mode; o.interval_q=qs[q]; |
| 92 | fault=f; o.costmodel=(void *)1; packcheck(&m,&o); |
| 93 | } |
| 94 | init(&m); m.ops[0].kind=DS_OP_MATVEC; m.ops[0].has_t=0; o.costmodel=NULL; packcheck(&m,&o); |
| 95 | fault=0; o.costmodel=(void *)1; |
| 96 | label="resolve u32 shape boundaries"; |
| 97 | uint32_t limits[]={0,1,UINT32_MAX-1,UINT32_MAX}; |
| 98 | for(int fld=0;fld<4;fld++) for(unsigned v=0;v<sizeof limits/sizeof *limits;v++) { |
| 99 | init(&m); m.ops[0].kind=DS_OP_MATVEC; m.ops[0].has_t=0; |
| 100 | if(fld==0)m.ops[0].M=limits[v]; if(fld==1)m.ops[0].N=limits[v]; |
| 101 | if(fld==2)m.ops[0].core=limits[v]; if(fld==3)m.ops[0].arm=(uint32_t)v; |
| 102 | packcheck(&m,&o); |
| 103 | } |
| 104 | o.costmodel=NULL; o.mode=0; |
| 105 | label="resolve count boundaries"; |
| 106 | int counts[]={0,1,DS_MAX_OPS-1,DS_MAX_OPS}; |
| 107 | for(unsigned k=0;k<sizeof counts/sizeof *counts;k++) { |
| 108 | init(&m); ds_op tmpl=m.ops[0]; m.n_ops=counts[k]; |
| 109 | for(int i=0;i<m.n_ops;i++) {m.ops[i]=tmpl; snprintf(m.ops[i].id,DS_ID,"op%d",i);} |
| 110 | packcheck(&m,&o); |
| 111 | } |
| 112 | label="resolve resource/dependency/buffer ABI boundaries"; |
| 113 | init(&m); |
| 114 | ds_resource rt=m.res[0];m.n_res=DS_MAX_RES; |
| 115 | for(int i=0;i<m.n_res;i++){m.res[i]=rt;snprintf(m.res[i].id,DS_ID,"r%d",i);} |
| 116 | m.ops[0].bw=DS_MAX_RES-1;m.n_deps=DS_MAX_DEPS;m.n_buf=DS_MAX_BUF; |
| 117 | for(int i=0;i<m.n_deps;i++)m.deps[i]=(ds_dep){0,1}; |
| 118 | for(int i=0;i<m.n_buf;i++){m.buf[i].bytes=4;m.buf[i].cap=2;m.buf[i].first=0;m.buf[i].last=1;} |
| 119 | packcheck(&m,&o); |
| 120 | for(int i=0;i<m.n_res;i++) { |
| 121 | CK(ds_find_res(&m,m.res[i].id)==oracle_ds_find_res(&m,m.res[i].id),"max resource lookup"); |
| 122 | } |
| 123 | init(&m);m.n_res=0;m.ops[0].engine=m.ops[1].engine=-1; |
| 124 | m.ops[0].launcher=m.ops[1].launcher=-1;m.ops[0].bw=m.ops[1].bw=-1; |
| 125 | m.ops[0].kind=m.ops[1].kind=DS_OP_BARRIER;packcheck(&m,&o); |
| 126 | label="resolve absent bandwidth and energy"; |
| 127 | for(int energy=0;energy<2;energy++) for(int instr=0;instr<2;instr++) { |
| 128 | init(&m); m.ops[0].bw=-1; m.ops[0].bytes=0; m.has_energy=energy; |
| 129 | m.joules=2.0; m.power_w=4.0; m.energy_prov=DS_PROV_MEASURED; |
| 130 | if(instr)strcpy(m.instrument,"meter"); packcheck(&m,&o); |
| 131 | } |
| 132 | label="lookup scans"; init(&m); |
| 133 | const char *ids[]={"op0","op1","missing","","op","op00","r0","r3","r4","1234567890123456789012345678901234567890"}; |
| 134 | for(unsigned i=0;i<sizeof ids/sizeof *ids;i++) { |
| 135 | CK(ds_find_op(&m,ids[i])==oracle_ds_find_op(&m,ids[i]),"op lookup"); |
| 136 | CK(ds_find_res(&m,ids[i])==oracle_ds_find_res(&m,ids[i]),"resource lookup"); |
| 137 | } |
| 138 | label="counterfactual kinds/index"; |
| 139 | double demands[]={0.0,8.0,128.0}; |
| 140 | for(unsigned k=0;k<sizeof demands/sizeof *demands;k++) for(int r=-1;r<=4;r++) { |
| 141 | m.ops[0].bytes=m.ops[1].bytes=demands[k]; |
| 142 | double x=ds_counterfactual(&m,&o,r), y=oracle_ds_counterfactual(&m,&o,r); |
| 143 | CK(!memcmp(&x,&y,sizeof x),"counterfactual bit equality"); |
| 144 | } |
| 145 | label="counterfactual unmatched launcher/refusal/default kind"; |
| 146 | init(&m);m.ops[0].launcher=-1; |
| 147 | for(int kind=0;kind<=5;kind++) { |
| 148 | m.res[3].kind=kind; |
| 149 | double x=ds_counterfactual(&m,&o,3),y=oracle_ds_counterfactual(&m,&o,3); |
| 150 | CK(!memcmp(&x,&y,sizeof x),"counterfactual unmatched/default"); |
| 151 | } |
| 152 | m.res[0].prov=DS_PROV_UNKNOWN; |
| 153 | double x=ds_counterfactual(&m,&o,0),y=oracle_ds_counterfactual(&m,&o,0); |
| 154 | CK(!memcmp(&x,&y,sizeof x),"counterfactual refused evaluation"); |
| 155 | label="counterfactual allocation refusal";init(&m);fail_alloc=1; |
| 156 | x=ds_counterfactual(&m,&o,0);y=oracle_ds_counterfactual(&m,&o,0);fail_alloc=0; |
| 157 | CK(!memcmp(&x,&y,sizeof x),"counterfactual allocation failure"); |
| 158 | } |
| 159 | static void diff_cases(void) { |
| 160 | ds_model b,t; ds_opts o={.asof_day=120}; |
| 161 | label="diff removal evidence partitions"; |
| 162 | for(int reason=0;reason<=7;reason++) for(int ref=0;ref<4;ref++) for(int proof=0;proof<2;proof++) for(int consumer=0;consumer<4;consumer++) for(int changed=0;changed<3;changed++) { |
| 163 | init(&b); init(&t); t.ops[0]=t.ops[1]; t.n_ops=1; |
| 164 | t.n_removed=1; strcpy(t.removed[0].op,"op0"); t.removed[0].reason=reason; |
| 165 | const char *refs[]={"","missing","op1","op0"}; strcpy(t.removed[0].ref,refs[ref]); |
| 166 | if(proof)strcpy(t.removed[0].proof,"proof"); |
| 167 | if(consumer==1){b.n_deps=1;b.deps[0]=(ds_dep){0,1};} |
| 168 | if(consumer==2){b.n_deps=1;b.deps[0]=(ds_dep){1,0};} |
| 169 | if(consumer==3){ |
| 170 | b.n_ops=3;b.ops[2]=b.ops[1];strcpy(b.ops[2].id,"op2"); |
| 171 | b.n_deps=1;b.deps[0]=(ds_dep){0,2};t.n_removed=2; |
| 172 | strcpy(t.removed[1].op,"op2");t.removed[1].reason=DS_RM_ELIMINATED; |
| 173 | } |
| 174 | const char *lists[]={NULL,"op1","x,op10,op0,,"}; o.changed=lists[changed]; diffcheck(&b,&t,&o); |
| 175 | } |
| 176 | label="diff nullable options";init(&b);init(&t);t.n_ops=1;t.ops[0]=t.ops[1];t.n_removed=1; |
| 177 | strcpy(t.removed[0].op,"op0");t.removed[0].reason=DS_RM_UNCHANGED_DEP; |
| 178 | strcpy(t.removed[0].ref,"op1");diffcheck(&b,&t,NULL);t.removed[0].ref[0]=0;diffcheck(&b,&t,NULL); |
| 179 | label="diff duplicate missing fabricated"; |
| 180 | for(int records=0;records<=2;records++) { |
| 181 | init(&b); init(&t); t.n_ops=1; t.ops[0]=t.ops[1]; t.n_removed=records; |
| 182 | for(int i=0;i<records;i++){strcpy(t.removed[i].op,"op0");t.removed[i].reason=DS_RM_ELIMINATED;} |
| 183 | diffcheck(&b,&t,&o); |
| 184 | } |
| 185 | init(&b); init(&t); t.n_removed=1; strcpy(t.removed[0].op,"fabricated"); diffcheck(&b,&t,&o); |
| 186 | label="diff shared double count"; init(&b); init(&t); t.n_ops=1; t.ops[0]=t.ops[1]; t.n_removed=2; |
| 187 | strcpy(t.removed[0].op,"op0"); t.removed[0].reason=DS_RM_SHARED; strcpy(t.removed[0].ref,"op1"); |
| 188 | strcpy(t.removed[1].op,"op1"); t.removed[1].reason=DS_RM_ELIMINATED; diffcheck(&b,&t,&o); |
| 189 | label="diff kept changed and added"; |
| 190 | init(&b); init(&t); t.ops[0].t_ns=17; t.ops[1].bytes=9; diffcheck(&b,&t,&o); |
| 191 | strcpy(t.ops[0].id,"added"); t.n_removed=1;strcpy(t.removed[0].op,"op0");t.removed[0].reason=1;diffcheck(&b,&t,&o); |
| 192 | label="diff accounting bandwidth branches"; |
| 193 | for(int bw=-1;bw<=1;bw+=2) for(int bytes=0;bytes<=32;bytes+=8) { |
| 194 | init(&b);init(&t); b.ops[0].bw=bw; b.ops[0].bytes=bytes;t.n_ops=1;t.ops[0]=t.ops[1]; |
| 195 | t.n_removed=1;strcpy(t.removed[0].op,"op0");t.removed[0].reason=1;diffcheck(&b,&t,&o); |
| 196 | } |
| 197 | label="diff streaming changed list"; |
| 198 | char list[2048]; |
| 199 | for(int n=0;n<=1024;n++) { |
| 200 | memset(list,'x',n); strcpy(list+n,",op1,"); |
| 201 | init(&b);init(&t);t.n_ops=1;t.ops[0]=t.ops[1];t.n_removed=1;strcpy(t.removed[0].op,"op0"); |
| 202 | t.removed[0].reason=DS_RM_UNCHANGED_DEP;strcpy(t.removed[0].ref,"op1");o.changed=list;diffcheck(&b,&t,&o); |
| 203 | } |
| 204 | label="diff zero/max counts";o.changed=NULL; |
| 205 | int counts[]={0,1,DS_MAX_OPS-1,DS_MAX_OPS}; |
| 206 | for(unsigned k=0;k<sizeof counts/sizeof *counts;k++) { |
| 207 | init(&b);b.n_ops=counts[k];ds_op tmpl=b.ops[0]; |
| 208 | for(int i=0;i<b.n_ops;i++){b.ops[i]=tmpl;snprintf(b.ops[i].id,DS_ID,"op%d",i);} |
| 209 | t=b;diffcheck(&b,&t,&o);t.n_ops=0;t.n_removed=b.n_ops; |
| 210 | for(int i=0;i<b.n_ops;i++){strcpy(t.removed[i].op,b.ops[i].id);t.removed[i].reason=1;} |
| 211 | diffcheck(&b,&t,&o); |
| 212 | } |
| 213 | } |
| 214 | int main(void) { |
| 215 | resolve_cases();diff_cases(); |
| 216 | printf("resolve differential: %u checks, %u failures\n",checks,failed); |
| 217 | return failed ? 1 : 0; |
| 218 | } |