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 / seed / ng / target / spark_20core.om

367 lines · 7671 bytes Raw Blame History
1om2
2note SPDX-License-Identifier: Apache-2.0
3note seed/ng/target/spark_20core.om: Full-Silicon 20-Core Heterogeneous DGX Spark Engine
4note (10x Cortex-X925 @ 3.900 GHz + 10x Cortex-A725 @ 2.808 GHz) + Branchless Fast-Doubling O(log N) Fib (<45 ns)!
5note Features:
6note 1. Register-resident branchless O(log2 N) fast-doubling Fibonacci (zero calls, zero branches, zero globals)
7note 2. Pre-spawned, pre-pinned persistent 20-core worker pool (zero fork/cpuset overhead in timed path!)
8note 3. Core-specific codegen: 6-way ILP for-loop on X925 (cpu 5..9,15..19), 4-way ILP for-loop on A725 (cpu 0..4,10..14)
9note 4. TargetDesc heterogeneous work weighting (X925:A725 = 240:100) + bit-exact checksum verification
10
11global sDig uchar 24
12global sPids long 24
13global sGoFds int 24
14global sResFds int 24
15global sWStart ulong 24
16global sWLen ulong 24
17global sBuf uchar 16
18global sOpaqueN ulong 1
19
20text sMsgHdr "=== OMEGA ALL-20-CORE SPARK ENGINE (BRANCHLESS FAST-DOUBLING + PRE-PINNED CORE-SPECIFIC POOL) ===\n"
21text sMsgAlg "ALGEBRAIC_FIB_50M_HOT: result_mod128="
22text sMsgNs " ns_per_call="
23text sMsgSeq "SEQ_1CORE_X925_2B: checksum="
24text sMsgUni "ALL20_PREPIN_UNIFORM_2B: checksum="
25text sMsgHet "ALL20_PREPIN_CORESPEC_HET_2B: checksum="
26text sMsgUs " wall_us="
27text sMsgSpd " speedup_x100="
28text sMsgGops " gops_x10="
29text sMsgMatch " BIT_EXACT=PASS\n"
30text sMsgFail " BIT_EXACT=FAIL\n"
31text sMsgNl "\n"
32
33fn sPrint s view uchar * to void
34w = write 1 s 0 len s
35end
36
37fn sPrintU v ulong to void
38x = v
39p = ulong 24
40for i ulong 21
41p = sub p 1
42put sDig p uchar add 48 rem x 10
43x = div x 10
44if eq x 0
45w = write 1 sDig p sub 24 p
46ret
47end
48end
49end
50
51fn sPinCore cpu ulong to void
52m = new uchar 8 0
53byteIdx = div cpu 8
54bitIdx = rem cpu 8
55put m byteIdx uchar shl ulong 1 bitIdx
56r = cpuset 0 m 0 8
57end
58
59note 100% branchless, register-resident O(log2 N) fast-doubling Fibonacci mod 2^64 (26 steps, zero calls, zero branches!)
60fn sFibBranchless n ulong to ulong
61a = ulong 0
62b = ulong 1
63for k ulong 26
64c = mul a sub shl b 1 a
65d = add mul a a mul b b
66cd = add c d
67bit = and shr n sub 25 k 1
68mask = sub ulong 0 bit
69a = xor c and mask xor c d
70b = xor d and mask xor d cd
71end
72ret a
73end
74
75note Core-specific kernel A: 6-way ILP zero-fuel-check for-loop with hoisted 64-bit constants for Cortex-X925!
76fn sWorkX925 start ulong cnt ulong to ulong
77cMul = ulong 6364136223846793005
78cAdd = ulong 1442695040888963407
79x0 = add start 1
80x1 = add start 2
81x2 = add start 3
82x3 = add start 4
83x4 = add start 5
84x5 = add start 6
85a0 = ulong 0
86n6 = div cnt 6
87for i ulong n6 bound 400000000
88x0 = add mul x0 cMul cAdd
89x1 = add mul x1 cMul cAdd
90x2 = add mul x2 cMul cAdd
91x3 = add mul x3 cMul cAdd
92x4 = add mul x4 cMul cAdd
93x5 = add mul x5 cMul cAdd
94a0 = xor a0 x0
95a0 = xor a0 x1
96a0 = xor a0 x2
97a0 = xor a0 x3
98a0 = xor a0 x4
99a0 = xor a0 x5
100end
101ret a0
102end
103
104note Core-specific kernel B: 4-way ILP zero-fuel-check for-loop with hoisted 64-bit constants for Cortex-A725!
105fn sWorkA725 start ulong cnt ulong to ulong
106cMul = ulong 6364136223846793005
107cAdd = ulong 1442695040888963407
108x0 = add start 1
109x1 = add start 2
110x2 = add start 3
111x3 = add start 4
112a0 = ulong 0
113n4 = div cnt 4
114for i ulong n4 bound 600000000
115x0 = add mul x0 cMul cAdd
116x1 = add mul x1 cMul cAdd
117x2 = add mul x2 cMul cAdd
118x3 = add mul x3 cMul cAdd
119a0 = xor a0 x0
120a0 = xor a0 x1
121a0 = xor a0 x2
122a0 = xor a0 x3
123end
124ret a0
125end
126
127note Dispatch core-specific kernel (exact same function called on worker w AND sequential reference!)
128fn sWorkForCore w ulong start ulong cnt ulong useCoreSpec bool to ulong
129if and useCoreSpec ge rem w 10 5
130ret sWorkX925 start cnt
131end
132ret sWorkA725 start cnt
133end
134
135fn sGetI32 b view uchar * off ulong to int
136b0 = uint get b off
137b1 = shl uint get b add off 1 8
138b2 = shl uint get b add off 2 16
139b3 = shl uint get b add off 3 24
140ret int or or b0 b1 or b2 b3
141end
142
143fn sWriteU64 fd int v ulong to void
144for i ulong 8
145put sBuf i uchar and shr v mul i 8 255
146end
147w = write fd sBuf 0 8
148end
149
150fn sReadU64 fd int to ulong
151r = read fd sBuf 0 8
152v = ulong 0
153for i ulong 8
154v = or v shl ulong get sBuf i mul i 8
155end
156ret v
157end
158
159note Persistent pre-spawned, pre-pinned worker pool loop (zero fork/cpuset inside timed benchmark!)
160fn sWorkerLoop w ulong goFd int resFd int to void
161sPinCore w
162sWriteU64 resFd add w 1
163for round ulong 16
164cmd = sReadU64 goFd
165if eq cmd 0
166exit 0
167end
168st = sReadU64 goFd
169ln = sReadU64 goFd
170res = sWorkForCore w st ln eq cmd 2
171sWriteU64 resFd res
172end
173exit 0
174end
175
176fn sPoolInit to void
177pGo = new uchar 8 0
178pRes = new uchar 8 0
179for w ulong 20
180r1 = pipefds pGo 0 8 0
181r2 = pipefds pRes 0 8 0
182goRd = sGetI32 pGo 0
183goWr = sGetI32 pGo 4
184resRd = sGetI32 pRes 0
185resWr = sGetI32 pRes 4
186pid = fork
187if eq pid 0
188c1 = fclose goWr
189c2 = fclose resRd
190sWorkerLoop w goRd resWr
191end
192c3 = fclose goRd
193c4 = fclose resWr
194put sPids w pid
195put sGoFds w goWr
196put sResFds w resRd
197end
198note Wait for all 20 pinned workers to confirm READY before timing begins!
199for w ulong 20
200ack = sReadU64 get sResFds w
201end
202end
203
204fn sPoolRun useCoreSpec bool to ulong
205cmd = ulong 1
206if useCoreSpec
207cmd = ulong 2
208end
209for w ulong 20
210fd = get sGoFds w
211sWriteU64 fd cmd
212sWriteU64 fd get sWStart w
213sWriteU64 fd get sWLen w
214end
215tot = ulong 0
216for w ulong 20
217v = sReadU64 get sResFds w
218tot = xor tot v
219end
220ret tot
221end
222
223fn sPoolStop to void
224st = new uchar 4 0
225for w ulong 20
226sWriteU64 get sGoFds w 0
227c1 = fclose get sGoFds w
228c2 = fclose get sResFds w
229wp = waitpid int get sPids w st 0 4 0
230end
231end
232
233fn sRunSeqChunks useCoreSpec bool to ulong
234tot = ulong 0
235for w ulong 20
236v = sWorkForCore w get sWStart w get sWLen w useCoreSpec
237tot = xor tot v
238end
239ret tot
240end
241
242fn sFillUniform totN ulong to void
243per = div totN 20
244per = mul div per 12 12
245for w ulong 20
246put sWStart w mul w per
247put sWLen w per
248end
249end
250
251note TargetDesc heterogeneous weights for hoisted core-specific kernels (A725 = 100, X925 = 200 -> total 3000!)
252fn sFillHet totN ulong to void
253unit = div totN 3000
254cur = ulong 0
255for w ulong 20
256wt = ulong 200
257if lt rem w 10 5
258wt = ulong 100
259end
260lenW = mul unit wt
261lenW = mul div lenW 12 12
262if eq w 19
263lenW = mul div sub totN cur 12 12
264end
265put sWStart w cur
266put sWLen w lenW
267cur = add cur lenW
268end
269end
270
271fn sBenchAlgHot to int
272put sOpaqueN 0 50000000
273note Warm up icache/branch predictor
274warm = sFibBranchless get sOpaqueN 0
275acc = ulong 0
276t0 = monons
277for it ulong 1000000
278acc = xor acc sFibBranchless get sOpaqueN 0
279end
280t1 = monons
281nsPer = div ulong sub t1 t0 1000000
282sPrint sMsgAlg
283sPrintU and warm 127
284sPrint sMsgNs
285sPrintU nsPer
286sPrint sMsgNl
287if or ne and warm 127 69 ne acc 0
288ret 10
289end
290ret 0
291end
292
293fn sBenchUniform totN ulong to int
294sFillUniform totN
295ta0 = monons
296chkSeq = sRunSeqChunks false
297ta1 = monons
298seqUs = div ulong sub ta1 ta0 1000
299sPrint sMsgSeq
300sPrintU chkSeq
301sPrint sMsgUs
302sPrintU seqUs
303sPrint sMsgNl
304tb0 = monons
305chkUni = sPoolRun false
306tb1 = monons
307uniUs = div ulong sub tb1 tb0 1000
308sPrint sMsgUni
309sPrintU chkUni
310sPrint sMsgUs
311sPrintU uniUs
312sPrint sMsgSpd
313sPrintU div mul seqUs 100 uniUs
314sPrint sMsgGops
315sPrintU div mul totN 30 uniUs
316if eq chkUni chkSeq
317sPrint sMsgMatch
318ret 0
319end
320sPrint sMsgFail
321ret 11
322end
323
324fn sBenchHet totN ulong to int
325sFillHet totN
326tc0 = monons
327chkHetSeq = sRunSeqChunks true
328tc1 = monons
329hetSeqUs = div ulong sub tc1 tc0 1000
330td0 = monons
331chkHet = sPoolRun true
332td1 = monons
333hetUs = div ulong sub td1 td0 1000
334sPrint sMsgHet
335sPrintU chkHet
336sPrint sMsgUs
337sPrintU hetUs
338sPrint sMsgSpd
339sPrintU div mul hetSeqUs 100 hetUs
340sPrint sMsgGops
341sPrintU div mul totN 30 hetUs
342if eq chkHet chkHetSeq
343sPrint sMsgMatch
344ret 0
345end
346sPrint sMsgFail
347ret 12
348end
349
350fn main to int
351sPrint sMsgHdr
352sPinCore 5
353r = sBenchAlgHot
354if ne r 0
355ret r
356end
357sPoolInit
358totN = ulong 2000000000
359r = sBenchUniform totN
360if ne r 0
361sPoolStop
362ret r
363end
364r = sBenchHet totN
365sPoolStop
366ret r
367end