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 / attn_views_diff.c

85 lines · 4266 bytes Raw Blame History
1/* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */
2#include "cudapp_native.h"
3#include "attn_views_fixture.h"
4#include <stddef.h>
5#include <stdlib.h>
6#include <stdint.h>
7#include <stdio.h>
8#include <string.h>
9#include <sys/mman.h>
10static void oracle(uint32_t *a,const uint64_t *p,size_t na,size_t nw){
11 memset(a,0,32*sizeof *a);
12 for(size_t i=0;i<na;i++){a[2*i]=(uint32_t)p[i];a[2*i+1]=(uint32_t)(p[i]>>32);}
13 for(size_t i=0;i<nw-2*na;i++)a[2*na+i]=(uint32_t)p[na+i];
14}
15static int floats_equal(const float *a,const float *b,size_t n){
16 for(size_t i=0;i<n;i++){
17 uint32_t x,y;memcpy(&x,a+i,4);memcpy(&y,b+i,4);
18 int xn=(x&0x7f800000u)==0x7f800000u&&(x&0x7fffffu);
19 int yn=(y&0x7f800000u)==0x7f800000u&&(y&0x7fffffu);
20 if(xn||yn){if(!xn||!yn)return 0;}else if(x!=y){
21 fprintf(stderr,"float mismatch at %zu: %08x != %08x\n",i,x,y);return 0;
22 }
23 }
24 return 1;
25}
26static int fixture_equal(const AttnViewsFixture *a,const AttnViewsFixture *b){
27 size_t tail=offsetof(AttnViewsFixture,result);
28 return !memcmp(a,b,offsetof(AttnViewsFixture,ff))&&
29 floats_equal(a->ff,b->ff,AV_N)&&floats_equal(a->rope,b->rope,1024)&&
30 floats_equal(a->fold,b->fold,32)&&
31 !memcmp((const char*)a+tail,(const char*)b+tail,sizeof *a-tail);
32}
33int main(void){
34 uint64_t p[32],r=99;uint32_t got[32],want[32];
35 for(size_t na=0;na<=16;na++)for(size_t nw=2*na;nw<=32;nw++){
36 for(size_t i=0;i<32;i++)p[i]=UINT64_C(0xdeadbeef12345678)^((uint64_t)i<<40);
37 oracle(want,p,na,nw);
38 if(omega_av_pack(&r,p,32,got,32,na,nw)||r||memcmp(got,want,sizeof got))return 1;
39 }
40 if(omega_av_pack(&r,p,32,got,32,17,32)||r!=1)return 2;
41 if(omega_av_pack(&r,p,32,got,32,2,3)||r!=1)return 3;
42 if(omega_av_pack(&r,p,32,got,32,0,33)||r!=1)return 4;
43 if(omega_av_pack(&r,p,31,got,32,0,0)==0)return 5;
44 if(omega_av_pack(&r,p,32,(uint32_t*)p,32,0,0)==0)return 6;
45 oracle(want,p,4,16);want[1]^=1;
46 if(omega_av_pack(&r,p,32,got,32,4,16)||!memcmp(got,want,sizeof got))return 7;
47 AttnViewsFixture *a=calloc(1,sizeof *a),*b=calloc(1,sizeof *b);
48 if(!a||!b)return 8;
49 attn_omega(a);attn_oracle(b);
50 if(!fixture_equal(a,b)){
51 for(size_t i=0;i<sizeof *a;i++)if(((unsigned char*)a)[i]!=((unsigned char*)b)[i]){
52 fprintf(stderr,"first fixture byte mismatch at %zu: %02x != %02x\n",i,((unsigned char*)a)[i],((unsigned char*)b)[i]);break;
53 }
54 for(size_t i=0;i<32;i++)if(a->result[i]!=b->result[i])
55 fprintf(stderr,"result[%zu]: %llu != %llu\n",i,(unsigned long long)a->result[i],(unsigned long long)b->result[i]);
56 fputs("FAIL host-data fixture differential\n",stderr);return 9;
57 }
58 b->args[1]^=1;
59 if(fixture_equal(a,b))return 10;
60 free(a);free(b);
61 unsigned char bytes[32]={0};uint64_t q[16]={0};
62 if(omega_av_copy(&r,NULL,0,NULL,0,0)||r)return 11;
63 if(omega_av_copy(&r,bytes,16,bytes+1,16,16)!=32)return 12;
64 if(omega_av_copy(&r,bytes,16,bytes+16,16,16)||r)return 13;
65 if(omega_av_copy((uint64_t*)bytes,bytes,8,bytes+16,8,8)!=32)return 14;
66 if(omega_av_copy(&r,(void*)(UINTPTR_MAX-1),3,bytes,3,3)!=32)return 15;
67 if(omega_av_copy(&r,bytes,2,bytes+16,2,3)!=3)return 16;
68 if(omega_av_gath(&r,bytes,8,bytes+16,8,q,2)!=3)return 17;
69 /* Admission above the former fixed capacity, backed by a real unread mapping. */
70 size_t large_bytes=(size_t)16777217*sizeof(uint32_t);
71 void *large=mmap(NULL,large_bytes,PROT_NONE,MAP_PRIVATE|MAP_ANONYMOUS,-1,0);
72 if(large==MAP_FAILED)return 18;
73 int large_ok=omega_av_max(&r,large,16777217,0)==0 && r==1;
74 munmap(large,large_bytes);if(!large_ok)return 18;
75 if(omega_av_bfexp(&r,bytes+1,1,got,1,1)!=32)return 19;
76 uint16_t nanbf[2]={0x7f81,0xffc1};float nanout[2],nanwant[2];
77 uint32_t nanbits[2]={0x7fc00000,0xffc00000};
78 memcpy(nanwant,nanbits,sizeof nanbits);
79 if(omega_av_bfexp(&r,nanbf,2,nanout,2,2)||r||!floats_equal(nanout,nanwant,2))return 20;
80 q[0]=0;q[3]=0;
81 if(omega_av_table(&r,got,1,got,1,q,16,0)!=32)return 21;
82 if(omega_av_touch(&r,got,1,got+2,1,got,1,0)!=32)return 22;
83 puts("PASS attention host typed views differential; red check rejected; runtime bounds and aliases refused");
84 return 0;
85}