omega-mv
Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.
Brought by drakestapleton
omega-mv / bench / m1 / block1_sched.c
| 1 | /* M1 block 1 (omega-mv#574): time both realization schedules on the Spark. |
| 2 | * Two measurements per program: |
| 3 | * path = omega_exec_native_f3 as Omega runs it (mmap+copy+mprotect+call+munmap per call) |
| 4 | * code = map once, call the function N times (the instruction bytes alone) |
| 5 | * Verifies every schedule against omega_program_eval before timing. */ |
| 6 | #include <stdio.h> |
| 7 | #include <string.h> |
| 8 | #include <stdlib.h> |
| 9 | #include <time.h> |
| 10 | #include <sys/mman.h> |
| 11 | #include <unistd.h> |
| 12 | #include "omega_program.h" |
| 13 | #include "omega_realize.h" |
| 14 | #include "omega_realize_synth.h" |
| 15 | #include "omega_exec.h" |
| 16 | |
| 17 | static double now(void){struct timespec t;clock_gettime(CLOCK_MONOTONIC,&t);return t.tv_sec+t.tv_nsec*1e-9;} |
| 18 | static int make_prog(OmegaProgram *p,const char *name,uint16_t w,const uint8_t *ops,const uint64_t *imms,size_t n){ |
| 19 | omega_program_init(p,name); |
| 20 | p->contract.input_type=p->contract.output_type=TYPE_UNSIGNED_INT; |
| 21 | p->contract.input_width=p->contract.output_width=w; |
| 22 | snprintf(p->contract.precondition,sizeof p->contract.precondition,"true"); |
| 23 | snprintf(p->contract.postcondition,sizeof p->contract.postcondition,"true"); |
| 24 | omega_build_constraint_id(CONST_PRECONDITION,"true",&p->contract.precondition_id); |
| 25 | omega_build_constraint_id(CONST_POSTCONDITION,"true",&p->contract.postcondition_id); |
| 26 | p->body.has_body=true;p->body.step_count=(uint16_t)n; |
| 27 | for(size_t i=0;i<n;++i){p->body.steps[i].op=ops[i];p->body.steps[i].imm=imms[i];} |
| 28 | return omega_program_compute_id(p); |
| 29 | } |
| 30 | static int compose2(OmegaProgram *out,OpCode o1,uint64_t i1,OpCode o2,uint64_t i2){ |
| 31 | OmegaProgram a,b;char err[160]; |
| 32 | if(omega_program_build_unary_op(&a,"a",o1,i1)||omega_program_build_unary_op(&b,"b",o2,i2))return -1; |
| 33 | return omega_program_compose(&a,&b,out,err,sizeof err); |
| 34 | } |
| 35 | typedef uint64_t(*Fn3)(uint64_t,uint64_t,uint64_t); |
| 36 | |
| 37 | int main(int argc,char**argv){ |
| 38 | long NPATH=argc>1?atol(argv[1]):20000, NCODE=argc>2?atol(argv[2]):20000000; |
| 39 | OmegaProgram P[24];const char*L[24];int n=0; |
| 40 | #define ADD(label,rc) do{if((rc)==0){L[n++]=label;}else{fprintf(stderr,"build fail %s\n",label);return 2;}}while(0) |
| 41 | ADD("x+1",omega_program_build_unary_op(&P[n],"f",OP_ADD,1)); |
| 42 | ADD("x*3",omega_program_build_unary_op(&P[n],"f",OP_MUL,3)); |
| 43 | ADD("(x*3)-2",compose2(&P[n],OP_MUL,3,OP_SUB,2)); |
| 44 | ADD("(x+7)*5",compose2(&P[n],OP_ADD,7,OP_MUL,5)); |
| 45 | ADD("x&0xFF00FF",omega_program_build_unary_op(&P[n],"f",OP_AND,0xFF00FF)); |
| 46 | {uint8_t o[]={OP_ADD};uint64_t i[]={0x123456789ABCDEF0ull};ADD("x+0x123456789ABCDEF0",make_prog(&P[n],"w",64,o,i,1));} |
| 47 | {uint8_t o[]={OP_MUL,OP_AND,OP_OR};uint64_t i[]={0x100000001ull,0xFFFF0000FFFF0000ull,0x0001000000000000ull};ADD("3-step wide",make_prog(&P[n],"w",64,o,i,3));} |
| 48 | {uint8_t o[]={OP_MUL,OP_SUB};uint64_t i[]={3,2};ADD("u32 (x*3)-2",make_prog(&P[n],"n",32,o,i,2));} |
| 49 | {uint8_t o[16];uint64_t i[16];for(int k=0;k<16;++k){o[k]=k%2?OP_MUL:OP_ADD;i[k]=0xFEDCBA9876543210ull^(uint64_t)k;}ADD("16-step wide",make_prog(&P[n],"long",64,o,i,16));} |
| 50 | {uint8_t o[64];uint64_t i[64];for(int k=0;k<64;++k){o[k]=k%2?OP_MUL:OP_ADD;i[k]=0xFEDCBA9876543210ull^(uint64_t)k;}ADD("64-step wide",make_prog(&P[n],"long",64,o,i,64));} |
| 51 | |
| 52 | printf("program,schedule,code_bytes,verified,n_path,path_ns_per_call,n_code,code_ns_per_call,rule_pick\n"); |
| 53 | size_t pg=(size_t)sysconf(_SC_PAGESIZE); |
| 54 | for(int k=0;k<n;++k){ |
| 55 | RealizationSynthesisResult rs; int src=omega_synthesize_for_dgx_spark(&P[k],&rs); |
| 56 | const char*pick=src==0?(rs.schedule==OMEGA_SCHED_PRELOAD?"PRELOAD":"SEQUENTIAL"):"synth_refused"; |
| 57 | for(int s=0;s<2;++s){ |
| 58 | RealizationObject r;memset(&r,0,sizeof r); |
| 59 | if(omega_program_emit_schedule(&P[k].body,P[k].contract.input_width,(OmegaRealizeSchedule)s,r.code_bytes,&r.code_len,sizeof r.code_bytes)){printf("%s,%s,0,emit_fail,,,,,%s\n",L[k],s?"PRELOAD":"SEQUENTIAL",pick);continue;} |
| 60 | uint64_t xs[OMEGA_REALIZE_DIFF_INPUTS],ys[OMEGA_REALIZE_DIFF_INPUTS]; |
| 61 | size_t ni=omega_realize_differential_inputs(&P[k],xs,OMEGA_REALIZE_DIFF_INPUTS); |
| 62 | int ok=omega_program_eval(&P[k],xs,ni,ys)==0; |
| 63 | for(size_t i=0;ok&&i<ni;++i){uint64_t g=0;if(omega_exec_native_f3(&r,xs[i],0,0,&g)||g!=ys[i])ok=0;} |
| 64 | if(!ok){printf("%s,%s,%zu,FAIL,,,,,%s\n",L[k],s?"PRELOAD":"SEQUENTIAL",r.code_len,pick);continue;} |
| 65 | /* path: as Omega executes */ |
| 66 | volatile uint64_t sink=0;uint64_t g; |
| 67 | for(int w=0;w<200;++w)omega_exec_native_f3(&r,xs[0],0,0,&g); |
| 68 | double t0=now();for(long i=0;i<NPATH;++i){omega_exec_native_f3(&r,xs[i%ni],0,0,&g);sink+=g;}double t1=now(); |
| 69 | /* code: map once */ |
| 70 | size_t al=(r.code_len+pg-1)&~(pg-1);void*m=mmap(NULL,al,PROT_READ|PROT_WRITE,MAP_ANONYMOUS|MAP_PRIVATE,-1,0); |
| 71 | memcpy(m,r.code_bytes,r.code_len);__builtin___clear_cache((char*)m,(char*)m+r.code_len);mprotect(m,al,PROT_READ|PROT_EXEC); |
| 72 | union{void*p;Fn3 f;}u;u.p=m;uint64_t acc=xs[0]; |
| 73 | for(long i=0;i<1000000;++i)acc=u.f(acc,0,0); |
| 74 | double t2=now();for(long i=0;i<NCODE;++i)acc=u.f(acc,0,0);double t3=now();sink+=acc;munmap(m,al); |
| 75 | printf("%s,%s,%zu,ok,%ld,%.1f,%ld,%.3f,%s\n",L[k],s?"PRELOAD":"SEQUENTIAL",r.code_len,NPATH,(t1-t0)/NPATH*1e9,NCODE,(t3-t2)/NCODE*1e9,pick); |
| 76 | fflush(stdout); |
| 77 | } |
| 78 | } |
| 79 | return 0; |
| 80 | } |