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/ampere/math_device.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 / ampere / math_device.c` 160 lines · 8706 bytes Raw Blame History 1 /* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */ 2 /* Device-only verifier: full binary32 input sweep for the four emitted MUFU functions. 3 * Independent host oracle: system long-double libm, not another CUDA++ kernel. 4 * Bounds: NVIDIA PTX ISA 9.4 sections 9.7.3.13,16,20,21. This checks intended 5 * non-FTZ PTX-level numerical contracts; raw MUFU equivalence is NOT yet qualified. 6 * No GPU is opened without the explicit --device flag. Shards are never exhaustive PASS. */ 7 #include "omega_ampere_codegen.h" 8 #include "omega_gpu_session.h" 9 #include 10 #include 11 #include 12 #include 13 #include 14 #include 15 #include 16 17 #define BATCH 65536u 18 #define NOREG (-1) 19 #define FULL_DOMAIN UINT64_C(0x100000000) 20 #define CTRL(stall,wbar,rbar,wait) \ 21 (((uint32_t)(stall)<<9)|((uint32_t)(wbar)<<14)|((uint32_t)(rbar)<<17)|((uint32_t)(wait)<<20)) 22 #define ALU CTRL(15,7,7,0) 23 #define LOAD_CTRL (CTRL(4,0,7,0)|0x2000u) 24 static uint32_t bits(float x) { uint32_t u; memcpy(&u,&x,sizeof u); return u; } 25 static float number(uint32_t u) { float x; memcpy(&x,&u,sizeof x); return x; } 26 static int append(BlackwellIRProgram *p, BlackwellIROpcode op, int d, int a, int b, int c, uint32_t imm, uint32_t control, int uniform, int pred) { 27 BlackwellIRInsn i = {0}; 28 i.op=op; i.dst_vreg=d; i.src1_vreg=a; i.src2_vreg=b; i.src3_vreg=c; i.ureg=NOREG; 29 i.imm=imm; i.control=control; i.is_uniform=uniform; i.predicate_p0=pred; 30 return omega_bw_ir_append(p,&i)<0 ? -1 : 0; 31 } 32 static int kernel(BlackwellIRProgram *p, BlackwellIROpcode math) { 33 omega_bw_ir_init(p); 34 int src=omega_bw_ir_alloc_vreg64(p), dst=omega_bw_ir_alloc_vreg64(p); 35 int tid=omega_bw_ir_alloc_vreg(p), cta=omega_bw_ir_alloc_vreg(p), n=omega_bw_ir_alloc_vreg(p); 36 int i=omega_bw_ir_alloc_vreg(p), address=omega_bw_ir_alloc_vreg64(p); 37 int x=omega_bw_ir_alloc_vreg(p), y=omega_bw_ir_alloc_vreg(p); 38 /* Conservative scheduling: no dependent instruction is issued before its load barrier. 39 * Each producer allocates barrier zero; the next instruction waits on barrier zero. */ 40 uint32_t pending=0; 41 #define EMIT(OP,D,A,B,C,IMM,BASE,PRED) do { if(append(p,OP,D,A,B,C,IMM,(BASE)|(pending<<20),0,PRED))return -1; pending=0; } while(0) 42 #define LOAD(OP,D,A,IMM) do { EMIT(OP,D,A,NOREG,NOREG,IMM,LOAD_CTRL,0); pending=1u; } while(0) 43 LOAD(BW_IR_LDC64,src,NOREG,0x380); 44 LOAD(BW_IR_LDC64,dst,NOREG,0x388); 45 LOAD(BW_IR_LDC,n,NOREG,0x390); 46 LOAD(BW_IR_S2R,tid,NOREG,BW_SR_TID_X); 47 LOAD(BW_IR_S2R,cta,NOREG,BW_SR_CTAID_X); 48 EMIT(BW_IR_IMAD,i,cta,NOREG,tid,256,ALU,0); 49 EMIT(BW_IR_ISETP_GE_U32,NOREG,i,n,NOREG,0,ALU,0); 50 EMIT(BW_IR_EXIT,NOREG,NOREG,NOREG,NOREG,0,ALU,1); 51 EMIT(BW_IR_IMAD_WIDE,address,i,NOREG,src,4,ALU,0); 52 LOAD(BW_IR_LDG_E,x,address,0); 53 /* MUFU completes through a scoreboard, not a fixed arithmetic delay. */ 54 EMIT(math,y,x,NOREG,NOREG,0,LOAD_CTRL,0); pending=1u; 55 EMIT(BW_IR_IMAD_WIDE,address,i,NOREG,dst,4,ALU,0); 56 EMIT(BW_IR_STG_E,NOREG,address,y,NOREG,0,CTRL(4,7,1,0)|0x2000u,0); pending=2u; 57 EMIT(BW_IR_EXIT,NOREG,NOREG,NOREG,NOREG,0,ALU,0); 58 EMIT(BW_IR_BRA,NOREG,NOREG,NOREG,NOREG,0,0x000fc000u,0); 59 #undef EMIT 60 #undef LOAD 61 return omega_bw_regalloc_solve(p); 62 } 63 static int agrees(unsigned op,uint32_t input,uint32_t output) { 64 float x=number(input), got=number(output); 65 long double ref; 66 switch(op) { 67 case 0: ref=1.0L/(long double)x; break; 68 case 1: ref=1.0L/sqrtl((long double)x); break; 69 case 2: ref=exp2l((long double)x); break; 70 default: ref=log2l((long double)x); break; 71 } 72 if (isnan(ref)) return isnan(got); 73 float rounded=(float)ref; 74 if (isinf(rounded)||rounded==0.0f) return output==bits(rounded); 75 if (!isfinite(got)) return 0; 76 if (op==0||op==2) { 77 uint32_t want=bits(rounded); 78 if ((output>>31)!=(want>>31)) return 0; 79 uint32_t distance=output>want?output-want:want-output; 80 return distance <= (op==0?1u:2u); 81 } 82 long double error=fabsl((long double)got-ref); 83 if(op==1) return error<=fabsl(ref)*exp2l(-22.9L); 84 if(x>0.5f && x<2.0f) return error<=0x1p-22L; 85 return error<=fabsl(ref)*0x1p-22L; 86 } 87 static int parse_u64(const char *s,uint64_t *v) { 88 char *end; errno=0; *v=strtoull(s,&end,0); 89 return errno||!s[0]||s[0]=='-'||*end ? -1:0; 90 } 91 int main(int argc,char **argv) { 92 uint64_t start=0,count=FULL_DOMAIN; int device=0,inject=0,rc=1,emit=0,selftest=0; 93 for(int i=1;i=FULL_DOMAIN||!count||count>FULL_DOMAIN-start)return 64; 103 static const BlackwellIROpcode ops[]={BW_IR_MUFU_RCP,BW_IR_MUFU_RSQ,BW_IR_MUFU_EX2,BW_IR_MUFU_LG2}; 104 static const char *names[]={"rcp","rsqrt","exp2","log2"}; 105 if (selftest) { 106 for(unsigned op=0;op<4;op++) { 107 uint32_t correct=op==3?0xff800000u:op==2?0x3f800000u:0x7f800000u; 108 if(!agrees(op,0,correct)||agrees(op,0,correct^0x80000000u))return 1; 109 if(!agrees(op,0x7fc00000u,0x7fc12345u)||agrees(op,0x7fc00000u,0x3f800000u))return 1; 110 } 111 puts("AMPERE MATH HOST ORACLE CONTROLS PASS; DEVICE NOT_RUN");return 0; 112 } 113 if (emit) { 114 for(unsigned op=0;op<4;op++) { 115 BlackwellIRProgram program; uint8_t code[16384];size_t written=0; 116 if(kernel(&program,ops[op])||omega_bw_encode_program(&program,code,sizeof code,&written)|| 117 omega_gpu_encode_program_target(&program,86,code,sizeof code,&written))return 1; 118 } 119 puts("AMPERE MATH KERNEL EMISSION PASS; DEVICE NOT_RUN");return 0; 120 } 121 if(!device) { puts("AMPERE MATH DEVICE NOT_RUN (explicit --device required)");return 2; } 122 if(inject&&(start!=0||count!=BATCH))return 64; 123 OmegaGpuMem in={0},out={0},code={0}; 124 omega_gpu_session_lock(); 125 if(!omega_gpu_session_open()||omega_gpu_session_target_sm()!=86) { 126 fprintf(stderr,"AMPERE MATH NOT_RUN: target/open: %s\n",omega_gpu_session_last_error());rc=2;goto done; 127 } 128 if(omega_gpu_session_alloc(BATCH*sizeof(uint32_t),&in)||omega_gpu_session_alloc(BATCH*sizeof(uint32_t),&out))goto done; 129 for(unsigned op=0;opBATCH?BATCH:(uint32_t)(count-offset); 137 for(uint32_t j=0;j>32),(uint32_t)out.va,(uint32_t)(out.va>>32),n}; 141 OmegaGpuLaunch launch={.code_va=code.va,.gpr_count=64,.threads_x=256,.threads_y=1,.grid_x=(n+255)/256,.grid_y=1, 142 .num_elements=n,.args=args,.n_args=32,.timeout_ms=10000,.target_sm=86}; 143 if(omega_gpu_session_launch(&launch,NULL,NULL)||omega_gpu_session_sync_to_cpu(&out))goto done; 144 if(inject&&offset==0)((uint32_t *)out.cpu)[0]^=0x80000000u; 145 for(uint32_t j=0;j