sourceextensions/gpu/demos/offload.xtl
1⍝!/usr/bin/env xetal
2⍝ Offload: the same products and the same elementwise work computed by
3⍝ the X_eTaL evaluator and, through the gpu extension, on opencl:0;
4⍝ compared, and timed. Products gain on the GPU even through the text
5⍝ bridge; elementwise work does not (docs/macros-and-extensions.md).
6⍝ Prints its timings, so it has no baseline. Run with
7⍝ "scripts/xx run extensions/gpu/demos/offload.xtl" (or "just offload").
8ᵍᵖ⁼u̲se< "Gpu"
9ᵃᶜ⁼u̲se< "Accel"
10ᶜᵏ⁼u̲se< "Clock"
11
12"A 512 by 512 product:"
13a ← 512 512 r̲eshape (r̲ange 262144) ÷ 262144
14cpu ← '{ m → m ᵃᶜm̲atmul m } ᶜᵏt̲ime a
15gpu ← '{ m → m ᵍᵖp̲roduct m } ᶜᵏt̲ime a
16(a ᵃᶜm̲atmul a) ᵍᵖn̲ear a ᵍᵖp̲roduct a
17" evaluator (ms), GPU through the extension (ms), how many times faster:"
18f̲loor cpu
19f̲loor gpu
20f̲loor cpu ÷ gpu m̲ax 1.0
21
22"Where the GPU's time goes: load and bind (ms), run (ms), read back (ms):"
23f̲loor '{ m → l ← ᵍᵖl̲oad (s̲hape m) ᵍᵖp̲roductProgram s̲hape m◆ b ← 1 ᵍᵖb̲ind m◆ 2 ᵍᵖb̲ind m } ᶜᵏt̲ime a ⍝ lint: allow unused-binding
24f̲loor 'ᵍᵖr̲un ᶜᵏt̲ime 0
25f̲loor 'ᵍᵖo̲utput ᶜᵏt̲ime 1
26
27"A million items, elementwise and summed, x + x * w, positive parts:"
28x ← (r̲ange 1048576) ÷ 1048576
29w ← 1048576 r̲eshape 1.0 -1.0
30cpuE ← '{ v → v ᵃᶜp̲ipeline w } ᶜᵏt̲ime x
31p ← "%x = input f64 [1048576]\n%w = input f64 [1048576]\n%xw = map mul %x %w\n%s = map add %x %xw\n%z = const f64 [] 0.0\n%r = map max %s %z\n%t = reduce add %r\noutput %t\n"
32gpuE ← '{ v → l ← ᵍᵖl̲oad p◆ b1 ← 1 ᵍᵖb̲ind v◆ b2 ← 2 ᵍᵖb̲ind w◆ r ← ᵍᵖr̲un 0◆ ᵍᵖo̲utput 1 } ᶜᵏt̲ime x ⍝ lint: allow unused-binding
33(x ᵃᶜp̲ipeline w) ᵍᵖn̲ear ᵍᵖo̲utput 1
34" evaluator (ms), GPU through the extension (ms): the bridge's text costs more than the work"
35f̲loor cpuE
36f̲loor gpuE