interplane.Get Omega
Read in your language

omega-helix

This project has not described itself yet.

Brought by drakestapleton

branch main Branches Tags History

omega-helix / native / mio.om

441 lines · 7425 bytes Raw Blame History
1note SPDX-License-Identifier: AGPL-3.0-only
2note omegallm lio.om (aien-train#52): the Linux platform unit, the only unit that uses host words.
3note It is concatenated last for targets aarch64 and x86linux; eio.om replaces it for x86efi. Both
4note define the same functions: oargc, oargr, oflush, ocwopen, ocwput, ocwend, ofsz, ogradw, ocsinit,
5note ocsg, ocsp, odread, owflush, owopen, owclose, ooffrd, oprd, obmsave and main. Every other unit is
6note free of host words.
7global owfd int 2
8global opth uchar 2100
9global opk uchar 4200
10global ocfd int 1
11global opp uchar 8
12global ost uchar 8
13global owrf int 64
14global owpd int 64
15global ommap_sz_g ulong 1
16
17note the argument count (argv[0] included)
18fn oargc to ulong
19ret ulong argcnt
20end
21
22note argv[i] into oarg (at most 4096 bytes); returns the argread result (negative: no such argument)
23fn oargr i ulong to long
24ret argread i oarg 0 4096
25end
26
27fn main to int
28if lt oargc 2
29exit 2
30end
31ret ocmd
32end
33
34note onsnow: monotonic nanoseconds (kvcmp's step timing in cli.om)
35fn onsnow to ulong
36ret ulong monons
37end
38
39fn oflush to void
40n = get oon 0
41p = ulong 0
42while lt p n bound 4097
43w = write 1 oout p sub n p
44if le w 0
45exit 70
46end
47p = add p ulong w
48end
49put oon 0 0
50end
51
52note v1 checkpoint stream (ckpt.om): open PATH.tmp for the path in argv[ai] (created, emptied)
53fn ocwopen ai ulong to void
54pl = oargp ai
55if gt pl 2000
56exit 2
57end
58for i ulong pl
59put opth i get oarg i
60end
61put opth pl 46
62put opth add pl 1 116
63put opth add pl 2 109
64put opth add pl 3 112
65fd = int fopen opth 0 add pl 4 577 420
66if lt fd 0
67exit 70
68end
69put ocfd 0 fd
70end
71
72note write ock[0 .. n) at byte position pos of the open PATH.tmp
73fn ocwput pos ulong n ulong to void
74fd = get ocfd 0
75p = ulong 0
76while lt p n bound 999999
77w = pwrite fd ock p sub n p long add pos p
78if le w 0
79exit 70
80end
81p = add p ulong w
82end
83end
84
85note fsync and close PATH.tmp, then rename it onto the path in argv[ai]
86fn ocwend ai ulong to void
87fd = get ocfd 0
88if ne fsync fd 0
89exit 70
90end
91if ne fclose fd 0
92exit 70
93end
94ocrename oargp ai
95end
96
97note rename opth[0 .. pl+4) to opth[0 .. pl) via a packed two-path list
98fn ocrename pl ulong to void
99put opk 0 2
100put opk 1 0
101put opk 2 uchar and add pl 4 255
102put opk 3 uchar shr add pl 4 8
103for i ulong add pl 4
104put opk add 4 i get opth i
105end
106q = add 8 pl
107put opk q uchar and pl 255
108put opk add q 1 uchar shr pl 8
109for j ulong pl
110put opk add add q 2 j get opth j
111end
112r = renames opk 0 add add q 2 pl 0
113if ne r 0
114exit 70
115end
116end
117
118note size in bytes of the file named by argv[ai] (seek to its end)
119fn ofsz ai ulong to ulong
120n = oargp ai
121fd = int fopen oarg 0 n 0 0
122if lt fd 0
123exit 70
124end
125sz = fseek fd 0 2
126if lt sz 0
127exit 70
128end
129if ne fclose fd 0
130exit 70
131end
132ret ulong sz
133end
134
135note cold store (CONTRACT-V3 section 2): GRD, Adam moments, and solver/specialist scratch up to OCSN
136note f64 values in a dynamic om2 pool grown on demand via enlarge (zero-filled on growth; failed
137note enlarge is an I/O refusal, exit 70; indices >= OCSN trap BOUNDS, exit 103).
138const OCSN ulong 1500000000
139const OCSMIN ulong 65536
140global ocs f64 *
141global ocsf ulong 1
142
143fn ocs_ensure need ulong to bool
144if gt need OCSN
145ret false
146end
147cur = get ocsf 0
148if le need cur
149ret true
150end
151cap = cur
152if lt cap OCSMIN
153cap = OCSMIN
154end
155while lt cap need bound 64
156cap = mul cap 2
157end
158if gt cap OCSN
159cap = OCSN
160end
161r = enlarge ocs cap
162if ne r 0
163ret false
164end
165put ocsf 0 cap
166ret true
167end
168
169fn ocsinit to ulong
170need = mul 3 get og 8
171if gt need OCSN
172ret OCSN
173end
174if lt need OCSMIN
175need = OCSMIN
176end
177if not ocs_ensure need
178exit 70
179end
180ret OCSN
181end
182
183fn ocsgrow need ulong to bool
184ret ocs_ensure need
185end
186
187fn ocsg i ulong to f64
188if lt i get ocsf 0
189ret get ocs i
190end
191if lt i OCSN
192ret 0.0f64
193end
194ret get ocs i
195end
196
197fn ocsp i ulong v f64 to void
198if ge i get ocsf 0
199if ge i OCSN
200put ocs i v
201ret
202end
203if not ocs_ensure add i 1
204exit 70
205end
206end
207put ocs i v
208end
209
210fn ogradw to f64
211k = get og 45
212if gt k 1
213exit 70
214end
215ret ograd
216end
217
218fn odread i ulong to ulong
219n = oargp i
220fd = int fopen oarg 0 n 0 0
221if lt fd 0
222exit 70
223end
224p = ulong 0
225dn = false
226for o ulong 64
227q = ulong 0
228while and not dn lt q 999999 bound 999999
229q = add q 1
230r = long 0
231if lt p ODHALF
232r = read fd od0 p sub ODHALF p
233else
234if lt p ODCAP
235r = read fd od1 sub p ODHALF sub ODCAP p
236else
237r = read fd ochk 0 1
238end
239end
240if lt r 0
241exit 70
242end
243if eq r 0
244dn = true
245else
246p = add p ulong r
247if gt p ODCAP
248exit 21
249end
250end
251end
252if dn
253break
254end
255end
256if not dn
257exit 70
258end
259c = fclose fd
260put odn 0 p
261ret p
262end
263
264note prep output: two buffered writers (k = 0 TRAIN, k = 1 HELD).
265fn owflush k ulong to void
266n = get owbn k
267fd = get owfd k
268p = ulong 0
269while lt p n bound 65537
270w = long 0
271if eq k 0
272w = write fd owb0 p sub n p
273else
274w = write fd owb1 p sub n p
275end
276if le w 0
277exit 70
278end
279p = add p ulong w
280end
281put owbn k 0
282end
283
284fn owopen k ulong i ulong to void
285n = oargp i
286fd = int fopen oarg 0 n 577 420
287if lt fd 0
288exit 70
289end
290put owfd k fd
291put owbn k 0
292end
293
294fn owclose k ulong to void
295owflush k
296fd = get owfd k
297if ne fsync fd 0
298exit 70
299end
300if ne fclose fd 0
301exit 70
302end
303end
304
305note read argv[ai] (B little-endian u64 offsets) into ochk; exit 21 unless it holds exactly 8 B bytes
306fn ooffrd ai ulong to void
307n = oargp ai
308fd = int fopen oarg 0 n 0 0
309if lt fd 0
310exit 70
311end
312p = ulong 0
313dn = false
314while not dn bound 70000
315r = read fd ochk p sub 65536 p
316if lt r 0
317exit 70
318end
319if eq r 0
320dn = true
321else
322p = add p ulong r
323if ge p 65536
324exit 21
325end
326end
327end
328c = fclose fd
329if ne p mul 8 get og 6
330exit 21
331end
332end
333
334
335note v1 positioned read (CONTRACT-V1 section 3): read up to n bytes of the file named by argv[ai],
336note from byte pos, into opw[off .. off + n). The file is opened and closed on every call. Returns
337note the bytes read: n, fewer only when the file ends first, 0 at or after its end. Exit 70 on an
338note I/O error, 2 on a bad argument; off + n over len opw traps 3. Each pread pass adds at least one
339note byte or ends, so n + 1 <= 2097153 passes suffice: 3 outer passes of at most 999999 inner ones.
340fn oprd ai ulong pos ulong off ulong n ulong to ulong
341pl = oargp ai
342fd = int fopen oarg 0 pl 0 0
343if lt fd 0
344exit 70
345end
346p = ulong 0
347dn = false
348for o ulong 3
349while and not dn lt p n bound 999999
350r = pread fd opw add off p sub n p long add pos p
351if lt r 0
352exit 70
353end
354if eq r 0
355dn = true
356else
357p = add p ulong r
358end
359end
360end
361if ne fclose fd 0
362exit 70
363end
364ret p
365end
366
367note v1 (CONTRACT-V1 section 7): write obm[0 .. n) (bpe.om) to the path in argv[ai] as ocwrite does:
368note PATH.tmp, fsync, rename
369fn obmsave ai ulong n ulong to void
370pl = oargp ai
371if gt pl 2000
372exit 2
373end
374for i ulong pl
375put opth i get oarg i
376end
377put opth pl 46
378put opth add pl 1 116
379put opth add pl 2 109
380put opth add pl 3 112
381fd = int fopen opth 0 add pl 4 577 420
382if lt fd 0
383exit 70
384end
385p = ulong 0
386while lt p n bound 999999
387w = write fd obm p sub n p
388if le w 0
389exit 70
390end
391p = add p ulong w
392end
393if ne fsync fd 0
394exit 70
395end
396if ne fclose fd 0
397exit 70
398end
399ocrename pl
400end
401
402fn ocwput_opw pos ulong n ulong to void
403fd = get ocfd 0
404p = ulong 0
405while lt p n bound 999999
406w = pwrite fd opw p sub n p long add pos p
407if le w 0
408exit 70
409end
410p = add p ulong w
411end
412end
413
414fn ommap_open ai ulong to long
415put ommap_sz_g 0 ofsz ai
416ret neg long 1
417end
418
419fn ommap_sz to ulong
420ret get ommap_sz_g 0
421end
422
423fn ommap_read h long off ulong n ulong to void
424exit 70
425end
426
427fn ommap_drop h long to void
428ret
429end
430
431fn ocswrite off ulong n ulong to bool
432ret false
433end
434
435fn ocsread off ulong n ulong to bool
436ret false
437end
438
439fn ommap_fnv64 h long off ulong nw ulong init_h ulong to ulong
440ret init_h
441end