interplane.Get Omega
Read in your language

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

branch main Branches Tags History

omega-mv / research / m15 / spbm / load.c

73 lines · 2803 bytes Raw Blame History
1/* SPDX-License-Identifier: GPL-2.0-only */
2/* Bounded CPU calibration load. Does not access firmware or device controls. */
3#define _GNU_SOURCE
4#include <arm_neon.h>
5#include <pthread.h>
6#include <sched.h>
7#include <stdatomic.h>
8#include <stdint.h>
9#include <stdio.h>
10#include <stdlib.h>
11#include <string.h>
12#include <time.h>
13#include <unistd.h>
14
15static atomic_int stop;
16struct worker { int cpu, error; uint64_t cpu_ns, iterations; float result; };
17static uint64_t cpu_time(void)
18{
19 struct timespec t;
20 if(clock_gettime(CLOCK_THREAD_CPUTIME_ID,&t)) { perror("thread clock"); exit(1); }
21 return (uint64_t)t.tv_sec*1000000000+t.tv_nsec;
22}
23static void *run(void *arg)
24{
25 struct worker *w=arg;
26 cpu_set_t set;
27 uint64_t start, n=0;
28 float32x4_t a[8], scale=vdupq_n_f32(.99999f), bias=vdupq_n_f32(.00001f);
29 CPU_ZERO(&set); CPU_SET(w->cpu,&set);
30 w->error=pthread_setaffinity_np(pthread_self(),sizeof(set),&set);
31 if(w->error) return NULL;
32 for(int i=0;i<8;i++) a[i]=vdupq_n_f32((float)i+2.0f);
33 start=cpu_time();
34 while(!atomic_load_explicit(&stop,memory_order_relaxed)) {
35 for(int i=0;i<4096;i++) {
36 for(int j=0;j<8;j++) a[j]=vfmaq_f32(bias,a[j],scale);
37 /* Keep all independent vectors live, including when rounded to a fixed point. */
38 __asm__ volatile("" : "+w"(a[0]), "+w"(a[1]), "+w"(a[2]), "+w"(a[3]),
39 "+w"(a[4]), "+w"(a[5]), "+w"(a[6]), "+w"(a[7]));
40 }
41 n+=4096;
42 }
43 w->cpu_ns=cpu_time()-start; w->iterations=n; w->result=0;
44 for(int i=0;i<8;i++) w->result+=vaddvq_f32(a[i]);
45 return NULL;
46}
47int main(int argc,char **argv)
48{
49 pthread_t threads[10]; struct worker w[10]={0};
50 char *end; unsigned long seconds;
51 int p,failed=0,n=10;
52 /* Optional third argument: worker count 1..10 (default 10, as in
53 * collections 01-03). */
54 if((argc!=3 && argc!=4) || (strcmp(argv[1],"p") && strcmp(argv[1],"e"))) return 2;
55 if(argc==4) { n=atoi(argv[3]); if(n<1 || n>10) return 2; }
56 seconds=strtoul(argv[2],&end,10); if(*end || seconds<1 || seconds>120) return 2;
57 p=!strcmp(argv[1],"p");
58 for(int i=0;i<n;i++) {
59 char path[160]; unsigned long long midr; FILE *f;
60 w[i].cpu=i<5?i+(p?5:0):i+5+(p?5:0);
61 snprintf(path,sizeof(path),"/sys/devices/system/cpu/cpu%d/regs/identification/midr_el1",w[i].cpu);
62 f=fopen(path,"r"); if(!f || fscanf(f,"%llx",&midr)!=1) return 1; fclose(f);
63 if(((midr>>4)&0xfff)!=(unsigned int)(p?0xd85:0xd87)) return 1;
64 if(pthread_create(&threads[i],NULL,run,&w[i])) exit(1);
65 }
66 sleep(seconds); atomic_store(&stop,1);
67 for(int i=0;i<n;i++) {
68 pthread_join(threads[i],NULL);
69 printf("{\"cpu\":%d,\"cpu_ns\":%llu,\"iterations\":%llu,\"result\":%.6f,\"affinity_error\":%d}\n",w[i].cpu,(unsigned long long)w[i].cpu_ns,(unsigned long long)w[i].iterations,w[i].result,w[i].error);
70 if(w[i].error || w[i].cpu_ns<seconds*UINT64_C(700000000)) failed=1;
71 }
72 return failed;
73}