Examples
Complete programs you can run as they are. Save one as app.ts and run it with symmetra app.ts, or copy the routes into your own project. Download them all as symmetra-examples-1.4.0.zip.
api-bench.ts
import { Router, serve } from '@symmetra/http'
const app = new Router()
// ── Baseline: pure GPU compute, no I/O ────────────────────────────────────────
app.get('/health', ctx => {
ctx.json({ status: 'ok', runtime: 'symmetra-gpu' })
})
// ── KV write: store Fibonacci lineage (20 entries) then return large JSON ─────
// Demonstrates cooperative CPU-GPU KV store writes.
// Each ctx.kv.set() pauses thread 0 ~1-5μs for a CPU roundtrip.
// Call /api/fib-cached next to read from the cache.
app.get('/api/fib-lineage', ctx => {
ctx.kv.set('fib:0', '0')
ctx.kv.set('fib:1', '1')
ctx.kv.set('fib:2', '1')
ctx.kv.set('fib:3', '2')
ctx.kv.set('fib:4', '3')
ctx.kv.set('fib:5', '5')
ctx.kv.set('fib:6', '8')
ctx.kv.set('fib:7', '13')
ctx.kv.set('fib:8', '21')
ctx.kv.set('fib:9', '34')
ctx.kv.set('fib:10', '55')
ctx.kv.set('fib:11', '89')
ctx.kv.set('fib:12', '144')
ctx.kv.set('fib:13', '233')
ctx.kv.set('fib:14', '377')
ctx.kv.set('fib:15', '610')
ctx.kv.set('fib:16', '987')
ctx.kv.set('fib:17', '1597')
ctx.kv.set('fib:18', '2584')
ctx.kv.set('fib:19', '4181')
// GPU-side: compute fib(30) to add real compute work
let x = 0
let y = 1
for (let i = 0; i < 30; i++) {
const z = x + y
x = y
y = z
}
ctx.json({ stored: 20, fib30: y, next: '/api/fib-cached' })
})
// ── KV read: retrieve fib:19 from store (set by /api/fib-lineage) ─────────────
app.get('/api/fib-cached', ctx => {
const val = ctx.kv.get('fib:19')
if (val !== null) {
ctx.json({ key: 'fib:19', value: val, hit: true })
} else {
ctx.json({ key: 'fib:19', hit: false, msg: 'call /api/fib-lineage first' })
}
})
// ── KV accumulate: increment a counter stored in KV ───────────────────────────
// Demonstrates read-modify-write pattern. Not atomic across concurrent requests
// (illustrative only - for atomic counters use GPU-side atomicAdd).
app.get('/api/kv-counter', ctx => {
ctx.kv.set('counter', '42')
const v = ctx.kv.get('counter')
ctx.json({ counter: v })
})
// ── Roundtrip: outbound HTTP GET to our own /health endpoint ──────────────────
// Demonstrates cooperative CPU-GPU outbound fetch.
// CPU makes a real TCP connection to localhost:8484/health.
// Expected latency: +1-10ms per request (loopback + GPU dispatch).
app.get('/api/roundtrip', ctx => {
const body = fetch('http://localhost:8484/health')
if (body !== null) {
ctx.json({ roundtrip: true, upstream: body })
} else {
ctx.json({ roundtrip: false, error: 'upstream unreachable' })
}
})
serve(app, { port: 8484, streams: 256 })
custom-api.ts
import { Router, serve } from '@symmetra/http'
const app = new Router()
app.get('/health', ctx => {
ctx.json({ status: 'ok', runtime: 'symmetra-gpu' })
})
app.get('/api/location', async ctx => {
const body = await ctx.fetch('http://ip-api.com/json/')
if (body === null) {
ctx.status(502)
ctx.json({ error: 'upstream API unreachable' })
return
}
ctx.json({ source: 'ip-api.com', data: body })
})
serve(app, { port: 8383, streams: 32 })
hello-gpu-test.ts
/**
* hello-gpu.ts - Minimal Symmetra GPU test
*
* Run with: sym examples/hello-gpu.ts
*
* This file exercises the full pipeline:
* TypeScript → esbuild → SymmParser → Codegen → NVRTC → GPU kernel → HTTP server
*
* Every handler below runs as a __device__ function on the GPU.
* No JavaScript or V8 is involved in handling requests.
*/
import { Router, serve } from '@symmetra/http'
const app = new Router()
// Each route compiles to a __device__ function in CUDA C++.
// The codegen generates: __device__ void symm_handler_0(SymmCtx* ctx) { ... }
app.get('/health', ctx => {
ctx.json({ status: 'ok', runtime: 'symmetra-gpu' })
})
app.get('/hello', ctx => {
ctx.json({
message: 'Hello from GPU',
path: ctx.path,
})
})
app.get('/echo/:name', ctx => {
ctx.json({
name: ctx.params.name,
message: `Hello, ${ctx.params.name}! Running on GPU.`,
})
})
app.post('/compute', async ctx => {
const body = await ctx.json<{ values: number[] }>()
// GPU computes in parallel - each value processed by a separate CUDA thread
const sum = body.values.reduce((a, b) => a + b, 0)
ctx.json({ sum, count: body.values.length })
})
// serve() is intercepted by SymmParser - it configures the GpuServer port.
// This call never executes in JavaScript.
serve(app, { port: 8181, streams: 32 })
hello-gpu.ts
/**
* hello-gpu.ts - Minimal Symmetra GPU test
*
* Run with: sym examples/hello-gpu.ts
*
* This file exercises the full pipeline:
* TypeScript → esbuild → SymmParser → Codegen → NVRTC → GPU kernel → HTTP server
*
* Every handler below runs as a __device__ function on the GPU.
* No JavaScript or V8 is involved in handling requests.
*/
import { Router, serve } from '@symmetra/http'
const app = new Router()
// Each route compiles to a __device__ function in CUDA C++.
// The codegen generates: __device__ void symm_handler_0(SymmCtx* ctx) { ... }
app.get('/health', ctx => {
ctx.json({ status: 'ok', runtime: 'symmetra-gpu' })
})
app.get('/hello', ctx => {
ctx.json({
message: 'Hello from GPU',
path: ctx.path,
})
})
app.get('/echo/:name', ctx => {
ctx.json({
name: ctx.params.name,
message: `Hello, ${ctx.params.name}! Running on GPU.`,
})
})
app.post('/compute', async ctx => {
const body = await ctx.json<{ values: number[] }>()
// GPU computes in parallel - each value processed by a separate CUDA thread
const sum = body.values.reduce((a, b) => a + b, 0)
ctx.json({ sum, count: body.values.length })
})
// serve() is intercepted by SymmParser - it configures the GpuServer port.
// This call never executes in JavaScript.
serve(app, { port: 8080, streams: 32 })
probe-coop-limits.ts
import { Router, serve } from '@symmetra/http'
const app = new Router()
// Two keys that are IDENTICAL for the first 255 characters and differ only after.
// If the GPU truncates the key at 255, both land on the same store entry and the
// second set overwrites the first -- so kA reads back "BBB" instead of "AAA".
app.get('/key-collide', async ctx => {
const pad = 'k'.repeat(260)
const kA = pad + 'ALPHA'
const kB = pad + 'BETA'
await ctx.kv.set(kA, 'AAA')
await ctx.kv.set(kB, 'BBB')
const a = await ctx.kv.get(kA)
const b = await ctx.kv.get(kB)
ctx.json({ a: a, b: b, collided: a === b })
})
// A value longer than the 4096-byte slot buffer. Round-trip its length.
app.get('/val-trunc', async ctx => {
const big = 'v'.repeat(5000)
await ctx.kv.set('bigval', big)
const got = await ctx.kv.get('bigval')
ctx.json({ sent: big.length, got: got.length })
})
// A URL past 255 characters. It points at THIS server's own /marker route with a
// long query string. If the URL is truncated the request never reaches /marker.
app.get('/marker', ctx => { ctx.json({ q: ctx.query }) })
// The part that DISTINGUISHES this URL sits past character 255, so a truncated copy
// still matches /marker and still answers 200 -- it just answers about a different URL.
app.get('/url-trunc', async ctx => {
const longUrl = 'http://127.0.0.1:8192/marker?pad=' + 'z'.repeat(240) + '&tail=END'
const r = await ctx.fetch(longUrl)
ctx.json({ urlLen: longUrl.length, sawTail: r.indexOf('tail=END') >= 0, body: r })
})
serve(app, { port: 8192 })
qa-paper.ts
/**
* qa-paper.ts - send questions, get answers back, computed on the GPU.
*
* sym examples/qa-paper.ts # from the repo root
*
* Every handler below compiles to a __device__ function. There is no JavaScript at
* request time - the TypeScript is read once at startup, turned into CUDA C++, and
* JIT-compiled into the persistent kernel.
*
* Three ways to pose a question, in the order you will usually want them:
* GET /answer/:a/:b - question in the path
* GET /answer?a=..&b=.. - question in the query string
* POST /score - a whole answer sheet in the body
*
* Only constructs verified to work on the GPU are used here. See docs/USING-THE-ENGINE.md
* for what is supported and what the engine refuses.
*/
import { Router, serve } from '@symmetra/http'
const app = new Router()
app.get('/health', ctx => {
ctx.json({ status: 'ok', runtime: 'symmetra-gpu' })
})
// ── Question in the path ────────────────────────────────────────────────────
// Path parameters arrive as strings; Number() converts them.
app.get('/answer/:a/:b', ctx => {
const a = Number(ctx.params.a)
const b = Number(ctx.params.b)
ctx.json({ a, b, sum: a + b, product: a * b })
})
// ── Question in the query string ────────────────────────────────────────────
app.get('/answer', ctx => {
const a = Number(ctx.query.a)
const b = Number(ctx.query.b)
ctx.json({ a, b, sum: a + b, difference: a - b })
})
// ── A whole answer sheet, scored in one request ─────────────────────────────
// POST {"answers": [4, 9, 16, 25, 36]}
// The array is reduced on the GPU; every element is handled by the block.
app.post('/score', async ctx => {
const body = await ctx.json<{ answers: number[] }>()
const total = body.answers.reduce((x, y) => x + y, 0)
const count = body.answers.length
ctx.json({ total, count, mean: total / count })
})
serve(app, { port: 8191, streams: 32 })
synth-debug.ts
import { Router, serve } from '@symmetra/http'
const app = new Router()
// Test 1: GET with known computation
app.get('/known', ctx => {
let x = 0
for (let i = 0; i < 5; i++) x = x + i
ctx.json({ x }) // expect x=10
})
// Test 2: POST, read body_len via ctx.bodyLen()
app.post('/blen', ctx => {
const blen = ctx.bodyLen()
ctx.json({ blen })
})
// Test 3: POST, read individual bytes (body as chars cast to int)
app.post('/b0', ctx => {
const v = ctx.i32(0)
ctx.json({ v })
})
serve(app, { port: 8486 })
synth-mini.ts
import { Router, serve } from '@symmetra/http'
const app = new Router()
app.get('/health', ctx => {
ctx.json({ status: 'ok' })
})
app.post('/test', ctx => {
// body: [n_ex=2, has_k=0, n_flat=6, 2,4,6, 1,3,5, 3,3, 0,0, 3,0]
const n_ex = ctx.i32(0)
const has_k = ctx.i32(1)
const n_flat = ctx.i32(2)
const off_lens = 3 + n_flat
const la = ctx.i32(off_lens + 0)
const lb = ctx.i32(off_lens + 1)
const oa = 3
const ob = oa + la
const off_exp = off_lens + n_ex + n_ex
const e0 = ctx.i32(off_exp + 0)
const e1 = ctx.i32(off_exp + 1)
let cnt0 = 0
for (let i = 0; i < la; i++) {
if (ctx.i32(oa + i) % 2 === 0) cnt0 = cnt0 + 1
}
let cnt1 = 0
for (let i = 0; i < lb; i++) {
if (ctx.i32(ob + i) % 2 === 0) cnt1 = cnt1 + 1
}
ctx.json({
n_ex, has_k, n_flat, la, lb, oa, ob, e0, e1,
cnt0, cnt1,
hit: cnt0 === e0 && cnt1 === e1
})
})
serve(app, { port: 8485 })
synth-server.ts
/**
* synth-server.ts - GPU-accelerated template synthesis server
*
* Binary protocol - body is a raw int32 array (little-endian):
* [0] n_ex (number of examples, 2-4)
* [1] has_k (1 if k param exists)
* [2] n_flat (total flat array length)
* [3..3+n_flat-1] flat values
* [3+n_flat..3+n_flat+n_ex-1] lens (per-example array length)
* [3+n_flat+n_ex..3+n_flat+2*n_ex-1] k values
* [3+n_flat+2*n_ex..3+n_flat+3*n_ex-1] expected outputs
*
* Response: JSON { strategy: string | null, hit: boolean }
*
* Run: sym examples/synth-server.ts
*/
import { Router, serve } from '@symmetra/http'
const app = new Router()
app.get('/health', ctx => {
ctx.json({ status: 'ok', engine: 'synth', runtime: 'symmetra-gpu' })
})
app.post('/synth', ctx => {
const n_ex = ctx.i32(0)
const has_k = ctx.i32(1)
const n_flat = ctx.i32(2)
// Per-example array lengths and cumulative offsets into flat[]
const off_lens = 3 + n_flat
const la = ctx.i32(off_lens + 0)
const lb = n_ex > 1 ? ctx.i32(off_lens + 1) : 0
const lc = n_ex > 2 ? ctx.i32(off_lens + 2) : 0
const ld = n_ex > 3 ? ctx.i32(off_lens + 3) : 0
// Cumulative offsets: oa=0, ob=la, oc=la+lb, od=la+lb+lc
const oa = 3
const ob = oa + la
const oc = ob + lb
const od = oc + lc
// k values per example
const off_k = off_lens + n_ex
const k0 = ctx.i32(off_k + 0)
const k1 = n_ex > 1 ? ctx.i32(off_k + 1) : 0
const k2 = n_ex > 2 ? ctx.i32(off_k + 2) : 0
const k3 = n_ex > 3 ? ctx.i32(off_k + 3) : 0
// Expected outputs per example
const off_exp = off_k + n_ex
const e0 = ctx.i32(off_exp + 0)
const e1 = n_ex > 1 ? ctx.i32(off_exp + 1) : 0
const e2 = n_ex > 2 ? ctx.i32(off_exp + 2) : 0
const e3 = n_ex > 3 ? ctx.i32(off_exp + 3) : 0
// Helper macros (as inline expressions) for per-example access:
// flat_at(ex, i) = ctx.i32(base_off[ex] + i)
// len(ex) = la / lb / lc / ld
// kv(ex) = k0 / k1 / k2 / k3
// exp(ex) = e0 / e1 / e2 / e3
// Inline loops unroll over examples by setting ex-specific vars each pass.
// ── NO-K TEMPLATES ──────────────────────────────────────────────────────────
if (has_k === 0) {
// count-even
let ok = 1
let cnt0 = 0; for (let i = 0; i < la; i++) { if (ctx.i32(oa + i) % 2 === 0) cnt0 = cnt0 + 1 }; if (cnt0 !== e0) ok = 0
if (ok === 1 && n_ex > 1) { let c = 0; for (let i = 0; i < lb; i++) { if (ctx.i32(ob + i) % 2 === 0) c = c + 1 }; if (c !== e1) ok = 0 }
if (ok === 1 && n_ex > 2) { let c = 0; for (let i = 0; i < lc; i++) { if (ctx.i32(oc + i) % 2 === 0) c = c + 1 }; if (c !== e2) ok = 0 }
if (ok === 1 && n_ex > 3) { let c = 0; for (let i = 0; i < ld; i++) { if (ctx.i32(od + i) % 2 === 0) c = c + 1 }; if (c !== e3) ok = 0 }
if (ok === 1) { ctx.json({ strategy: 'count-even', hit: true }); return }
// count-odd
ok = 1
cnt0 = 0; for (let i = 0; i < la; i++) { if (ctx.i32(oa + i) % 2 !== 0) cnt0 = cnt0 + 1 }; if (cnt0 !== e0) ok = 0
if (ok === 1 && n_ex > 1) { let c = 0; for (let i = 0; i < lb; i++) { if (ctx.i32(ob + i) % 2 !== 0) c = c + 1 }; if (c !== e1) ok = 0 }
if (ok === 1 && n_ex > 2) { let c = 0; for (let i = 0; i < lc; i++) { if (ctx.i32(oc + i) % 2 !== 0) c = c + 1 }; if (c !== e2) ok = 0 }
if (ok === 1 && n_ex > 3) { let c = 0; for (let i = 0; i < ld; i++) { if (ctx.i32(od + i) % 2 !== 0) c = c + 1 }; if (c !== e3) ok = 0 }
if (ok === 1) { ctx.json({ strategy: 'count-odd', hit: true }); return }
// product
ok = 1
let p0 = 1; for (let i = 0; i < la; i++) p0 = p0 * ctx.i32(oa + i); if (p0 !== e0) ok = 0
if (ok === 1 && n_ex > 1) { let p = 1; for (let i = 0; i < lb; i++) p = p * ctx.i32(ob + i); if (p !== e1) ok = 0 }
if (ok === 1 && n_ex > 2) { let p = 1; for (let i = 0; i < lc; i++) p = p * ctx.i32(oc + i); if (p !== e2) ok = 0 }
if (ok === 1 && n_ex > 3) { let p = 1; for (let i = 0; i < ld; i++) p = p * ctx.i32(od + i); if (p !== e3) ok = 0 }
if (ok === 1) { ctx.json({ strategy: 'product', hit: true }); return }
// sum-all
ok = 1
let s0 = 0; for (let i = 0; i < la; i++) s0 = s0 + ctx.i32(oa + i); if (s0 !== e0) ok = 0
if (ok === 1 && n_ex > 1) { let s = 0; for (let i = 0; i < lb; i++) s = s + ctx.i32(ob + i); if (s !== e1) ok = 0 }
if (ok === 1 && n_ex > 2) { let s = 0; for (let i = 0; i < lc; i++) s = s + ctx.i32(oc + i); if (s !== e2) ok = 0 }
if (ok === 1 && n_ex > 3) { let s = 0; for (let i = 0; i < ld; i++) s = s + ctx.i32(od + i); if (s !== e3) ok = 0 }
if (ok === 1) { ctx.json({ strategy: 'sum-all', hit: true }); return }
// max
ok = 1
let mx0 = ctx.i32(oa); for (let i = 1; i < la; i++) { if (ctx.i32(oa+i) > mx0) mx0 = ctx.i32(oa+i) }; if (mx0 !== e0) ok = 0
if (ok === 1 && n_ex > 1) { let mx = ctx.i32(ob); for (let i = 1; i < lb; i++) { if (ctx.i32(ob+i) > mx) mx = ctx.i32(ob+i) }; if (mx !== e1) ok = 0 }
if (ok === 1 && n_ex > 2) { let mx = ctx.i32(oc); for (let i = 1; i < lc; i++) { if (ctx.i32(oc+i) > mx) mx = ctx.i32(oc+i) }; if (mx !== e2) ok = 0 }
if (ok === 1 && n_ex > 3) { let mx = ctx.i32(od); for (let i = 1; i < ld; i++) { if (ctx.i32(od+i) > mx) mx = ctx.i32(od+i) }; if (mx !== e3) ok = 0 }
if (ok === 1) { ctx.json({ strategy: 'max', hit: true }); return }
// min
ok = 1
let mn0 = ctx.i32(oa); for (let i = 1; i < la; i++) { if (ctx.i32(oa+i) < mn0) mn0 = ctx.i32(oa+i) }; if (mn0 !== e0) ok = 0
if (ok === 1 && n_ex > 1) { let mn = ctx.i32(ob); for (let i = 1; i < lb; i++) { if (ctx.i32(ob+i) < mn) mn = ctx.i32(ob+i) }; if (mn !== e1) ok = 0 }
if (ok === 1 && n_ex > 2) { let mn = ctx.i32(oc); for (let i = 1; i < lc; i++) { if (ctx.i32(oc+i) < mn) mn = ctx.i32(oc+i) }; if (mn !== e2) ok = 0 }
if (ok === 1 && n_ex > 3) { let mn = ctx.i32(od); for (let i = 1; i < ld; i++) { if (ctx.i32(od+i) < mn) mn = ctx.i32(od+i) }; if (mn !== e3) ok = 0 }
if (ok === 1) { ctx.json({ strategy: 'min', hit: true }); return }
// buy-two-cheapest: sum of 2 smallest
ok = 1
let m1 = 2147483647; let m2 = 2147483647
for (let i = 0; i < la; i++) { const v = ctx.i32(oa+i); if (v < m1) { m2 = m1; m1 = v } else if (v < m2) m2 = v }
if (m1 + m2 !== e0) ok = 0
if (ok === 1 && n_ex > 1) { m1 = 2147483647; m2 = 2147483647; for (let i = 0; i < lb; i++) { const v = ctx.i32(ob+i); if (v < m1) { m2 = m1; m1 = v } else if (v < m2) m2 = v }; if (m1 + m2 !== e1) ok = 0 }
if (ok === 1 && n_ex > 2) { m1 = 2147483647; m2 = 2147483647; for (let i = 0; i < lc; i++) { const v = ctx.i32(oc+i); if (v < m1) { m2 = m1; m1 = v } else if (v < m2) m2 = v }; if (m1 + m2 !== e2) ok = 0 }
if (ok === 1 && n_ex > 3) { m1 = 2147483647; m2 = 2147483647; for (let i = 0; i < ld; i++) { const v = ctx.i32(od+i); if (v < m1) { m2 = m1; m1 = v } else if (v < m2) m2 = v }; if (m1 + m2 !== e3) ok = 0 }
if (ok === 1) { ctx.json({ strategy: 'buy-two-cheapest', hit: true }); return }
// semi-ordered-swap-count
ok = 1
let minI = 0; let maxI = 0
for (let i = 1; i < la; i++) { if (ctx.i32(oa+i) < ctx.i32(oa+minI)) minI = i; if (ctx.i32(oa+i) > ctx.i32(oa+maxI)) maxI = i }
let swaps = minI + (la - 1 - maxI); if (maxI < minI) swaps = swaps - 1; if (swaps !== e0) ok = 0
if (ok === 1 && n_ex > 1) { let mi = 0; let ma = 0; for (let i = 1; i < lb; i++) { if (ctx.i32(ob+i) < ctx.i32(ob+mi)) mi = i; if (ctx.i32(ob+i) > ctx.i32(ob+ma)) ma = i }; let sw = mi + (lb - 1 - ma); if (ma < mi) sw = sw - 1; if (sw !== e1) ok = 0 }
if (ok === 1 && n_ex > 2) { let mi = 0; let ma = 0; for (let i = 1; i < lc; i++) { if (ctx.i32(oc+i) < ctx.i32(oc+mi)) mi = i; if (ctx.i32(oc+i) > ctx.i32(oc+ma)) ma = i }; let sw = mi + (lc - 1 - ma); if (ma < mi) sw = sw - 1; if (sw !== e2) ok = 0 }
if (ok === 1 && n_ex > 3) { let mi = 0; let ma = 0; for (let i = 1; i < ld; i++) { if (ctx.i32(od+i) < ctx.i32(od+mi)) mi = i; if (ctx.i32(od+i) > ctx.i32(od+ma)) ma = i }; let sw = mi + (ld - 1 - ma); if (ma < mi) sw = sw - 1; if (sw !== e3) ok = 0 }
if (ok === 1) { ctx.json({ strategy: 'semi-ordered-swap-count', hit: true }); return }
// largest-after-adjacent-merge
ok = 1
let best0 = ctx.i32(oa) + ctx.i32(oa + 1)
for (let i = 1; i + 1 < la; i++) { const v = ctx.i32(oa+i) + ctx.i32(oa+i+1); if (v > best0) best0 = v }; if (best0 !== e0) ok = 0
if (ok === 1 && n_ex > 1) { let b = ctx.i32(ob) + ctx.i32(ob+1); for (let i = 1; i+1 < lb; i++) { const v = ctx.i32(ob+i)+ctx.i32(ob+i+1); if (v > b) b = v }; if (b !== e1) ok = 0 }
if (ok === 1 && n_ex > 2) { let b = ctx.i32(oc) + ctx.i32(oc+1); for (let i = 1; i+1 < lc; i++) { const v = ctx.i32(oc+i)+ctx.i32(oc+i+1); if (v > b) b = v }; if (b !== e2) ok = 0 }
if (ok === 1 && n_ex > 3) { let b = ctx.i32(od) + ctx.i32(od+1); for (let i = 1; i+1 < ld; i++) { const v = ctx.i32(od+i)+ctx.i32(od+i+1); if (v > b) b = v }; if (b !== e3) ok = 0 }
if (ok === 1) { ctx.json({ strategy: 'largest-after-adjacent-merge', hit: true }); return }
// ant-on-boundary-prefix-zero
ok = 1
let ps = 0; cnt0 = 0; for (let i = 0; i < la; i++) { ps = ps + ctx.i32(oa+i); if (ps === 0) cnt0 = cnt0 + 1 }; if (cnt0 !== e0) ok = 0
if (ok === 1 && n_ex > 1) { let s = 0; let c = 0; for (let i = 0; i < lb; i++) { s = s + ctx.i32(ob+i); if (s === 0) c = c + 1 }; if (c !== e1) ok = 0 }
if (ok === 1 && n_ex > 2) { let s = 0; let c = 0; for (let i = 0; i < lc; i++) { s = s + ctx.i32(oc+i); if (s === 0) c = c + 1 }; if (c !== e2) ok = 0 }
if (ok === 1 && n_ex > 3) { let s = 0; let c = 0; for (let i = 0; i < ld; i++) { s = s + ctx.i32(od+i); if (s === 0) c = c + 1 }; if (c !== e3) ok = 0 }
if (ok === 1) { ctx.json({ strategy: 'ant-on-boundary-prefix-zero', hit: true }); return }
// longest-alternating-subarray
ok = 1
let bestA = 1; let curA = 1
for (let i = 1; i < la; i++) { if ((ctx.i32(oa+i) > 0) !== (ctx.i32(oa+i-1) > 0)) { curA = curA + 1; if (curA > bestA) bestA = curA } else curA = 1 }; if (bestA !== e0) ok = 0
if (ok === 1 && n_ex > 1) { let b = 1; let c = 1; for (let i = 1; i < lb; i++) { if ((ctx.i32(ob+i) > 0) !== (ctx.i32(ob+i-1) > 0)) { c = c + 1; if (c > b) b = c } else c = 1 }; if (b !== e1) ok = 0 }
if (ok === 1 && n_ex > 2) { let b = 1; let c = 1; for (let i = 1; i < lc; i++) { if ((ctx.i32(oc+i) > 0) !== (ctx.i32(oc+i-1) > 0)) { c = c + 1; if (c > b) b = c } else c = 1 }; if (b !== e2) ok = 0 }
if (ok === 1 && n_ex > 3) { let b = 1; let c = 1; for (let i = 1; i < ld; i++) { if ((ctx.i32(od+i) > 0) !== (ctx.i32(od+i-1) > 0)) { c = c + 1; if (c > b) b = c } else c = 1 }; if (b !== e3) ok = 0 }
if (ok === 1) { ctx.json({ strategy: 'longest-alternating-subarray', hit: true }); return }
}
// ── WITH-K TEMPLATES ────────────────────────────────────────────────────────
if (has_k === 1) {
// count-ge-k
let ok = 1
let ck0 = 0; for (let i = 0; i < la; i++) { if (ctx.i32(oa+i) >= k0) ck0 = ck0 + 1 }; if (ck0 !== e0) ok = 0
if (ok === 1 && n_ex > 1) { let c = 0; for (let i = 0; i < lb; i++) { if (ctx.i32(ob+i) >= k1) c = c + 1 }; if (c !== e1) ok = 0 }
if (ok === 1 && n_ex > 2) { let c = 0; for (let i = 0; i < lc; i++) { if (ctx.i32(oc+i) >= k2) c = c + 1 }; if (c !== e2) ok = 0 }
if (ok === 1 && n_ex > 3) { let c = 0; for (let i = 0; i < ld; i++) { if (ctx.i32(od+i) >= k3) c = c + 1 }; if (c !== e3) ok = 0 }
if (ok === 1) { ctx.json({ strategy: 'count-ge-k', hit: true }); return }
// count-gt-k
ok = 1
ck0 = 0; for (let i = 0; i < la; i++) { if (ctx.i32(oa+i) > k0) ck0 = ck0 + 1 }; if (ck0 !== e0) ok = 0
if (ok === 1 && n_ex > 1) { let c = 0; for (let i = 0; i < lb; i++) { if (ctx.i32(ob+i) > k1) c = c + 1 }; if (c !== e1) ok = 0 }
if (ok === 1 && n_ex > 2) { let c = 0; for (let i = 0; i < lc; i++) { if (ctx.i32(oc+i) > k2) c = c + 1 }; if (c !== e2) ok = 0 }
if (ok === 1 && n_ex > 3) { let c = 0; for (let i = 0; i < ld; i++) { if (ctx.i32(od+i) > k3) c = c + 1 }; if (c !== e3) ok = 0 }
if (ok === 1) { ctx.json({ strategy: 'count-gt-k', hit: true }); return }
// count-le-k
ok = 1
ck0 = 0; for (let i = 0; i < la; i++) { if (ctx.i32(oa+i) <= k0) ck0 = ck0 + 1 }; if (ck0 !== e0) ok = 0
if (ok === 1 && n_ex > 1) { let c = 0; for (let i = 0; i < lb; i++) { if (ctx.i32(ob+i) <= k1) c = c + 1 }; if (c !== e1) ok = 0 }
if (ok === 1 && n_ex > 2) { let c = 0; for (let i = 0; i < lc; i++) { if (ctx.i32(oc+i) <= k2) c = c + 1 }; if (c !== e2) ok = 0 }
if (ok === 1 && n_ex > 3) { let c = 0; for (let i = 0; i < ld; i++) { if (ctx.i32(od+i) <= k3) c = c + 1 }; if (c !== e3) ok = 0 }
if (ok === 1) { ctx.json({ strategy: 'count-le-k', hit: true }); return }
// count-lt-k
ok = 1
ck0 = 0; for (let i = 0; i < la; i++) { if (ctx.i32(oa+i) < k0) ck0 = ck0 + 1 }; if (ck0 !== e0) ok = 0
if (ok === 1 && n_ex > 1) { let c = 0; for (let i = 0; i < lb; i++) { if (ctx.i32(ob+i) < k1) c = c + 1 }; if (c !== e1) ok = 0 }
if (ok === 1 && n_ex > 2) { let c = 0; for (let i = 0; i < lc; i++) { if (ctx.i32(oc+i) < k2) c = c + 1 }; if (c !== e2) ok = 0 }
if (ok === 1 && n_ex > 3) { let c = 0; for (let i = 0; i < ld; i++) { if (ctx.i32(od+i) < k3) c = c + 1 }; if (c !== e3) ok = 0 }
if (ok === 1) { ctx.json({ strategy: 'count-lt-k', hit: true }); return }
// count-equal-to-k
ok = 1
ck0 = 0; for (let i = 0; i < la; i++) { if (ctx.i32(oa+i) === k0) ck0 = ck0 + 1 }; if (ck0 !== e0) ok = 0
if (ok === 1 && n_ex > 1) { let c = 0; for (let i = 0; i < lb; i++) { if (ctx.i32(ob+i) === k1) c = c + 1 }; if (c !== e1) ok = 0 }
if (ok === 1 && n_ex > 2) { let c = 0; for (let i = 0; i < lc; i++) { if (ctx.i32(oc+i) === k2) c = c + 1 }; if (c !== e2) ok = 0 }
if (ok === 1 && n_ex > 3) { let c = 0; for (let i = 0; i < ld; i++) { if (ctx.i32(od+i) === k3) c = c + 1 }; if (c !== e3) ok = 0 }
if (ok === 1) { ctx.json({ strategy: 'count-equal-to-k', hit: true }); return }
// count-divisible-k
ok = 1
ck0 = 0; for (let i = 0; i < la; i++) { if (ctx.i32(oa+i) % k0 === 0) ck0 = ck0 + 1 }; if (ck0 !== e0) ok = 0
if (ok === 1 && n_ex > 1) { let c = 0; for (let i = 0; i < lb; i++) { if (ctx.i32(ob+i) % k1 === 0) c = c + 1 }; if (c !== e1) ok = 0 }
if (ok === 1 && n_ex > 2) { let c = 0; for (let i = 0; i < lc; i++) { if (ctx.i32(oc+i) % k2 === 0) c = c + 1 }; if (c !== e2) ok = 0 }
if (ok === 1 && n_ex > 3) { let c = 0; for (let i = 0; i < ld; i++) { if (ctx.i32(od+i) % k3 === 0) c = c + 1 }; if (c !== e3) ok = 0 }
if (ok === 1) { ctx.json({ strategy: 'count-divisible-k', hit: true }); return }
// max-beauty-window-2k: sliding window on sorted arr, max window where max-min <= 2k
ok = 1
let l2 = 0; let bW = 0
for (let r = 0; r < la; r++) {
while (ctx.i32(oa+r) - ctx.i32(oa+l2) > 2 * k0) l2 = l2 + 1
if (r - l2 + 1 > bW) bW = r - l2 + 1
}; if (bW !== e0) ok = 0
if (ok === 1 && n_ex > 1) { let l = 0; let b = 0; for (let r = 0; r < lb; r++) { while (ctx.i32(ob+r) - ctx.i32(ob+l) > 2 * k1) l = l + 1; if (r - l + 1 > b) b = r - l + 1 }; if (b !== e1) ok = 0 }
if (ok === 1 && n_ex > 2) { let l = 0; let b = 0; for (let r = 0; r < lc; r++) { while (ctx.i32(oc+r) - ctx.i32(oc+l) > 2 * k2) l = l + 1; if (r - l + 1 > b) b = r - l + 1 }; if (b !== e2) ok = 0 }
if (ok === 1 && n_ex > 3) { let l = 0; let b = 0; for (let r = 0; r < ld; r++) { while (ctx.i32(od+r) - ctx.i32(od+l) > 2 * k3) l = l + 1; if (r - l + 1 > b) b = r - l + 1 }; if (b !== e3) ok = 0 }
if (ok === 1) { ctx.json({ strategy: 'max-beauty-window-2k', hit: true }); return }
// count-subarrays-range-2: count subarrays where max - min <= k
ok = 1
let cntR = 0
for (let i = 0; i < la; i++) { let mn = ctx.i32(oa+i); let mx = ctx.i32(oa+i); for (let j = i; j < la; j++) { if (ctx.i32(oa+j) < mn) mn = ctx.i32(oa+j); if (ctx.i32(oa+j) > mx) mx = ctx.i32(oa+j); if (mx - mn <= k0) cntR = cntR + 1; else break } }; if (cntR !== e0) ok = 0
if (ok === 1 && n_ex > 1) { let c = 0; for (let i = 0; i < lb; i++) { let mn = ctx.i32(ob+i); let mx = ctx.i32(ob+i); for (let j = i; j < lb; j++) { if (ctx.i32(ob+j) < mn) mn = ctx.i32(ob+j); if (ctx.i32(ob+j) > mx) mx = ctx.i32(ob+j); if (mx - mn <= k1) c = c + 1; else break } }; if (c !== e1) ok = 0 }
if (ok === 1 && n_ex > 2) { let c = 0; for (let i = 0; i < lc; i++) { let mn = ctx.i32(oc+i); let mx = ctx.i32(oc+i); for (let j = i; j < lc; j++) { if (ctx.i32(oc+j) < mn) mn = ctx.i32(oc+j); if (ctx.i32(oc+j) > mx) mx = ctx.i32(oc+j); if (mx - mn <= k2) c = c + 1; else break } }; if (c !== e2) ok = 0 }
if (ok === 1 && n_ex > 3) { let c = 0; for (let i = 0; i < ld; i++) { let mn = ctx.i32(od+i); let mx = ctx.i32(od+i); for (let j = i; j < ld; j++) { if (ctx.i32(od+j) < mn) mn = ctx.i32(od+j); if (ctx.i32(od+j) > mx) mx = ctx.i32(od+j); if (mx - mn <= k3) c = c + 1; else break } }; if (c !== e3) ok = 0 }
if (ok === 1) { ctx.json({ strategy: 'count-subarrays-range-2', hit: true }); return }
// count-subarrays-max-k-times: count subarrays where max appears >= k times (sliding)
ok = 1
let maxV = ctx.i32(oa); for (let i = 1; i < la; i++) { if (ctx.i32(oa+i) > maxV) maxV = ctx.i32(oa+i) }
let cntM = 0; let lM = 0; let freqM = 0
for (let r = 0; r < la; r++) {
if (ctx.i32(oa+r) === maxV) freqM = freqM + 1
while (freqM >= k0) { cntM = cntM + (la - r); if (ctx.i32(oa+lM) === maxV) freqM = freqM - 1; lM = lM + 1 }
}; if (cntM !== e0) ok = 0
if (ok === 1 && n_ex > 1) { let mv = ctx.i32(ob); for (let i = 1; i < lb; i++) { if (ctx.i32(ob+i) > mv) mv = ctx.i32(ob+i) }; let c = 0; let l = 0; let f = 0; for (let r = 0; r < lb; r++) { if (ctx.i32(ob+r) === mv) f = f + 1; while (f >= k1) { c = c + (lb - r); if (ctx.i32(ob+l) === mv) f = f - 1; l = l + 1 } }; if (c !== e1) ok = 0 }
if (ok === 1 && n_ex > 2) { let mv = ctx.i32(oc); for (let i = 1; i < lc; i++) { if (ctx.i32(oc+i) > mv) mv = ctx.i32(oc+i) }; let c = 0; let l = 0; let f = 0; for (let r = 0; r < lc; r++) { if (ctx.i32(oc+r) === mv) f = f + 1; while (f >= k2) { c = c + (lc - r); if (ctx.i32(oc+l) === mv) f = f - 1; l = l + 1 } }; if (c !== e2) ok = 0 }
if (ok === 1 && n_ex > 3) { let mv = ctx.i32(od); for (let i = 1; i < ld; i++) { if (ctx.i32(od+i) > mv) mv = ctx.i32(od+i) }; let c = 0; let l = 0; let f = 0; for (let r = 0; r < ld; r++) { if (ctx.i32(od+r) === mv) f = f + 1; while (f >= k3) { c = c + (ld - r); if (ctx.i32(od+l) === mv) f = f - 1; l = l + 1 } }; if (c !== e3) ok = 0 }
if (ok === 1) { ctx.json({ strategy: 'count-subarrays-max-k-times', hit: true }); return }
}
ctx.json({ strategy: null, hit: false })
})
serve(app, { port: 8484 })
synth-test.ts
import { Router, serve } from '@symmetra/http'
const app = new Router()
app.post('/test', ctx => {
const n_ex = ctx.i32(0)
const has_k = ctx.i32(1)
const n_flat = ctx.i32(2)
const off_lens = 3 + n_flat
const la = ctx.i32(off_lens + 0)
const lb = n_ex > 1 ? ctx.i32(off_lens + 1) : 0
const oa = 3
const ob = oa + la
const off_k = off_lens + n_ex
const off_exp = off_k + n_ex
const e0 = ctx.i32(off_exp + 0)
const e1 = n_ex > 1 ? ctx.i32(off_exp + 1) : 0
let ok = 1
let cnt0 = 0
for (let i = 0; i < la; i++) { if (ctx.i32(oa + i) % 2 === 0) cnt0 = cnt0 + 1 }
if (cnt0 !== e0) ok = 0
if (ok === 1 && n_ex > 1) {
let c = 0
for (let i = 0; i < lb; i++) { if (ctx.i32(ob + i) % 2 === 0) c = c + 1 }
if (c !== e1) ok = 0
}
ctx.json({ n_ex, la, lb, oa, ob, e0, e1, cnt0, ok })
})
serve(app, { port: 8487 })