sourceextensions/blender/demos/Matmul.xtl
1⍝│ Matmul.xtl -- a matrix product, as two videos (Video.xtl): a small one
2⍝│ a step at a time, C = A x B for X_eTaL-gpu's matmul demo ([3 4] x
3⍝│ [4 2]), and a large one as a fly-through, 512 x 512 x 512. The
4⍝│ products and sums are X_eTaL-gpu's Accel library (ac:m_atmul, the
5⍝│ inner product its GPU runs); what is new here is only how they are
6⍝│ shown: every cell's place, color and timing, every label and the
7⍝│ camera's path, worked out from those arrays. Shared by the demos
8⍝│ (demos/matmul-steps.xtl, demos/matmul-fly.xtl), their tests and the
9⍝│ literate document (docs/literate/matmul.org).
10⍝│ Import: "mm:" u_se< "Matmul"
11
12"ac:" u̲se< "Accel"
13"vd:" u̲se< "Video"
14
15⍝━━ The arrays (Accel)
16
17⍝│ a: X_eTaL-gpu's matmul demo's left matrix, 1 to 12 (3 by 4)
18ˡa ← 3 4 r̲eshape r̲ange 12
19
20⍝│ b: its right matrix (4 by 2)
21ˡb ← 4 2 r̲eshape 1 0 0 1 1 1 2 -1
22
23⍝│ c: their product, ac:m_atmul (3 by 2): each item the sum of a row of a
24⍝│ times a column of b
25⍝│ >> "mm:" u_se< "Matmul"
26⍝│ >> mm:c
27⍝│ 12 1
28⍝│ 28 5
29⍝│ 44 9
30ˡc ← ˡa ᵃᶜm̲atmul ˡb
31
32⍝━━ Every product, one at a time
33
34⍝│ the items of c in the order they are worked out, row by row: i and j
35⍝│ from 0 (6 each)
36ˡitemI ← ((r̲ange 6) − 1) d̲iv 2
37⍝│ j
38ˡitemJ ← ((r̲ange 6) − 1) m̲od 2
39
40⍝│ for each of the 24 products, item by item, k from 0 to 3 within each:
41⍝│ its item (from 0), i, j and k
42ˡprodQ ← ((r̲ange 24) − 1) d̲iv 4
43⍝│ i
44ˡprodI ← (ˡprodQ + 1) s̲elect ˡitemI
45⍝│ j
46ˡprodJ ← (ˡprodQ + 1) s̲elect ˡitemJ
47⍝│ k
48ˡprodK ← ((r̲ange 24) − 1) m̲od 4
49
50⍝│ the products: a[i;k] times b[k;j], each (24)
51⍝│ >> "mm:" u_se< "Matmul"
52⍝│ >> 4 t_ake mm:products
53⍝│ 1 0 3 8
54ˡproducts ← ((1 + ˡprodK + 4 × ˡprodI) s̲elect r̲avel ˡa) × (1 + ˡprodJ + 2 × ˡprodK) s̲elect r̲avel ˡb
55
56⍝│ the products added up, four at a time, are c, item by item
57⍝│ >> "mm:" u_se< "Matmul"
58⍝│ >> (r_avel mm:c) = '+ r_/_2 6 4 r_eshape mm:products
59⍝│ 1 1 1 1 1 1
60ˡsums ← '+ r̲/₂ 6 4 r̲eshape ˡproducts
61
62⍝━━ Where things stand
63
64⍝ the walls' distance from the cube; a cell's size
65g ← 1.3
66d ← 0.86
67
68⍝ a row of x y z for each of the vectors (or numbers) x, y and z
69ʰx̲yz ← { (x, y, zz) q →
70 n ← (t̲ally x) m̲ax (t̲ally y) m̲ax t̲ally zz
71 2 1 t̲ranspose (3 c̲at n) r̲eshape (n r̲eshape f̲loat x) c̲at (n r̲eshape f̲loat y) c̲at n r̲eshape f̲loat zz
72}
73
74⍝│ The cube: x is j (across, b's and c's columns), y is k (into the
75⍝│ picture, the sum's index), z is -i (down, a's and c's rows). a stands
76⍝│ on the left wall, b on the ceiling, c on the back wall, and the
77⍝│ products fill the cube between them. (i, k) mm:a_t 0: a's cells
78ˡa̲t ← { (i, k) z → (0.0 − g, (f̲loat k) + 0.5, 0.0 − (f̲loat i) + 0.5) ʰx̲yz 0 }
79⍝│ (k, j) mm:b_t 0: b's cells
80ˡb̲t ← { (k, j) z → ((f̲loat j) + 0.5, (f̲loat k) + 0.5, g) ʰx̲yz 0 }
81⍝│ (i, j) mm:c_t 0: c's cells
82ˡc̲t ← { (i, j) z → ((f̲loat j) + 0.5, 4.0 + g, 0.0 − (f̲loat i) + 0.5) ʰx̲yz 0 }
83⍝│ (i, j, k) mm:p_t 0: a product's place in the cube
84ˡp̲t ← { (i, j, k) z → ((f̲loat j) + 0.5, (f̲loat k) + 0.5, 0.0 − (f̲loat i) + 0.5) ʰx̲yz 0 }
85
86⍝ rows x y z w d h for places (n by 3) of one size
87ʰs̲ized ← { (at, size) z → at c̲at₂ ((t̲ally at) c̲at 3) r̲eshape size }
88
89⍝ n by 3 rows of one color
90ʰo̲f ← { n c → (n c̲at 3) r̲eshape c }
91
92⍝━━ When
93
94⍝│ each item's time: slow at first, then quicker (seconds, 6)
95ˡdurations ← 3.0 2.2 1.6 1.3 1.1 1.0
96⍝│ when each item starts: 1.5 s, then one after another
97ˡstarts ← 1.5 + 0.0 c̲at '+ s̲\ -1 d̲rop ˡdurations
98⍝│ when the last item ends
99ˡlast ← 6 s̲elect ˡstarts + ˡdurations
100⍝│ all six worked out at once: from 0.3 s after the last, for 2.5 s
101ˡtogether ← (0.3 + ˡlast) c̲at 2.8 + ˡlast
102⍝│ the video's length: a second after that
103ˡlength ← 3.8 + ˡlast
104
105⍝│ for each product: when it appears (one after another in the first
106⍝│ half of its item's time), when it flies to c, when it lands (24 each)
107ˡappear ← ((ˡprodQ + 1) s̲elect ˡstarts) + ((ˡprodQ + 1) s̲elect ˡdurations) × 0.12 + 0.45 × (f̲loat ˡprodK) ÷ 4.0
108⍝│ when it flies
109ˡflies ← ((ˡprodQ + 1) s̲elect ˡstarts) + 0.68 × (ˡprodQ + 1) s̲elect ˡdurations
110⍝│ when it lands, and its item of c is shown
111ˡlands ← ((ˡprodQ + 1) s̲elect ˡstarts) + 0.88 × (ˡprodQ + 1) s̲elect ˡdurations
112
113⍝━━ The steps video
114
115⍝ colors (0 to 255): white, yellow, an empty cell, the cube's edges, the sky
116white ← 255 255 255
117yellow ← 255 216 77
118empty ← 42 46 54
119edge ← 138 147 163
120⍝│ the sky (0 to 255)
121ˡsky ← 7 8 10
122
123⍝ a group of boxes: places (n by 3), one size, colors (n by 3), glow
124ʰg̲roup ← { (name, at, size, c, gl) z → (name, ((at, size) ʰs̲ized 0) c̲at₂ (f̲loat c) c̲at₂ (t̲ally at) r̲eshape gl) ᵛᵈb̲oxes 0 }
125
126⍝ a group of outlines, the same rows
127ʰo̲utlined ← { (name, at, size, c, gl) z → (name, ((at, size) ʰs̲ized 0) c̲at₂ (f̲loat c) c̲at₂ (t̲ally at) r̲eshape gl) ᵛᵈo̲utlines 0 }
128
129⍝ a group of words at places, one height, one color
130ʰw̲ords ← { (name, texts, at, size, c) z → (name, texts, at c̲at₂ ((t̲ally at) r̲eshape size) c̲at₂ (t̲ally at) ʰo̲f f̲loat c) ᵛᵈl̲abels 0 }
131
132⍝ spans for every object of a group: from and to (vectors or numbers)
133ʰs̲pans ← { (name, a, b, n) z → (name, 2 1 t̲ranspose (2 c̲at n) r̲eshape (n r̲eshape a) c̲at n r̲eshape b) ᵛᵈs̲hown 0 }
134
135⍝│ the walls: a, b and c's cells colored by their values, labeled, and
136⍝│ the cube's edges; c's cells start empty; each wall's name and shape
137⍝│ by it (A's and B's above, C's to its right)
138ˡw̲alls ← { z →
139 ai ← ((r̲ange 12) − 1) d̲iv 4
140 ak ← ((r̲ange 12) − 1) m̲od 4
141 bk ← ((r̲ange 8) − 1) d̲iv 2
142 bj ← ((r̲ange 8) − 1) m̲od 2
143 t ← ((("A", (ai, ak) ˡa̲t 0, 0.12 c̲at d c̲at d, (r̲avel ˡa, 0.0, 12.0) ᵛᵈt̲one 0, 0.9) ʰg̲roup 0) c̲at
144 (("A values", 0 ᵛᵈf̲ixed f̲loat ˡa, ((ai, ak) ˡa̲t 0) + 12 ʰo̲f -0.14 0.0 0.0, 0.42, white) ʰw̲ords 0) c̲at
145 (("B", (bk, bj) ˡb̲t 0, d c̲at d c̲at 0.12, (r̲avel ˡb, -1.0, 2.0) ᵛᵈt̲one 0, 0.9) ʰg̲roup 0) c̲at
146 (("B values", 0 ᵛᵈf̲ixed f̲loat ˡb, ((bk, bj) ˡb̲t 0) + 8 ʰo̲f 0.0 0.0 0.14, 0.42, white) ʰw̲ords 0) c̲at
147 (("C empty", (ˡitemI, ˡitemJ) ˡc̲t 0, d c̲at 0.12 c̲at d, 6 ʰo̲f empty, 1.0) ʰg̲roup 0) c̲at
148 ("cube", 1 3 r̲eshape 1.0 2.0 -1.5, 2.0 4.0 3.0, 1 ʰo̲f edge, 1.0) ʰo̲utlined 0)
149 names ← ("A" ʰs̲hape ˡa) c̲at "|" c̲at ("B" ʰs̲hape ˡb) c̲at "|" c̲at "C" ʰs̲hape ˡc
150 t c̲at ("names", names, 3 3 r̲eshape (0.0 − g) c̲at 2.0 0.7 1.0 4.6 1.5 3.6 5.3 -1.5, 0.5, yellow) ʰw̲ords 0
151}
152
153⍝ an array's name and shape, as [3 4]
154ʰs̲hape ← { name x → name c̲at ((0 < t̲ally name) t̲ake " ") c̲at "[" c̲at (f̲ormat s̲hape x) c̲at "]" }
155
156⍝│ one item at a time: its row of a, its column of b and its cell of c
157⍝│ outlined; its four products appear in the cube, each with guide lines
158⍝│ from its two factors, fly into c's cell and shrink there; then c's
159⍝│ cell shows its sum
160ˡi̲tems ← { z →
161 i ← ˡitemI
162 j ← ˡitemJ
163 ends ← ˡstarts + ˡdurations
164 pi ← ˡprodI
165 pj ← ˡprodJ
166 pk ← ˡprodK
167 at ← (pi, pj, pk) ˡp̲t 0
168 into ← (pi, pj) ˡc̲t 0
169 colors ← (ˡproducts, -4.0, 12.0) ᵛᵈt̲one 0
170 keys ← 2 1 3 t̲ranspose 2 24 6 r̲eshape ((at, 0.5 0.5 0.5) ʰs̲ized 0) c̲at (into, 0.05 0.05 0.05) ʰs̲ized 0
171 fa ← (pi, pk) ˡa̲t 0
172 fb ← (pk, pj) ˡb̲t 0
173 guides ← ((fa c̲at fb) c̲at₂ at c̲at at) c̲at₂ (48 ʰo̲f f̲loat yellow) c̲at₂ 48 r̲eshape 0.015
174 done ← (4 × r̲ange 6) s̲elect ˡlands
175 ((("rows", (0.0 − g, 2.0, 0.0 − (f̲loat i) + 0.5) ʰx̲yz 0, 0.2 c̲at 4.0 c̲at d + 0.08, 6 ʰo̲f white, 3.0) ʰo̲utlined 0) c̲at
176 (("rows", ˡstarts, ends, 6) ʰs̲pans 0) c̲at
177 (("columns", ((f̲loat j) + 0.5, 2.0, g) ʰx̲yz 0, (d + 0.08) c̲at 4.0 0.2, 6 ʰo̲f white, 3.0) ʰo̲utlined 0) c̲at
178 (("columns", ˡstarts, ends, 6) ʰs̲pans 0) c̲at
179 (("targets", (i, j) ˡc̲t 0, (d + 0.1) c̲at 0.22 c̲at d + 0.1, 6 ʰo̲f white, 3.0) ʰo̲utlined 0) c̲at
180 (("targets", ˡstarts, ends, 6) ʰs̲pans 0) c̲at
181 (("products", ((at, 0.5 0.5 0.5) ʰs̲ized 0) c̲at₂ colors c̲at₂ 24 r̲eshape 1.6) ᵛᵈb̲oxes 0) c̲at
182 (("products", 2 1 t̲ranspose 2 24 r̲eshape ˡflies c̲at ˡlands, keys) ᵛᵈm̲ovesEach 0) c̲at
183 (("products", ˡappear, ˡlands, 24) ʰs̲pans 0) c̲at
184 (("product values", 0 ᵛᵈf̲ixed f̲loat ˡproducts, at + 24 ʰo̲f 0.0 0.0 0.55, 0.36, white) ʰw̲ords 0) c̲at
185 (("product values", ˡappear, ˡflies, 24) ʰs̲pans 0) c̲at
186 (("guides", guides) ᵛᵈl̲ines 0) c̲at
187 (("guides", ˡappear c̲at ˡappear, ˡflies c̲at ˡflies, 48) ʰs̲pans 0) c̲at
188 (("C", (i, j) ˡc̲t 0, (d × 1.001) c̲at 0.14 c̲at d × 1.001, (r̲avel ˡc, 0.0, 44.0) ᵛᵈt̲one 0, 1.0) ʰg̲roup 0) c̲at
189 (("C", done, ˡlength, 6) ʰs̲pans 0) c̲at
190 (("C values", 0 ᵛᵈf̲ixed f̲loat ˡc, ((i, j) ˡc̲t 0) + 6 ʰo̲f 0.0 -0.14 0.0, 0.42, white) ʰw̲ords 0) c̲at
191 (("C values", done, ˡlength, 6) ʰs̲pans 0) c̲at
192 (("together", (i, j) ˡc̲t 0, (d + 0.1) c̲at 0.22 c̲at d + 0.1, 6 ʰo̲f yellow, 3.0) ʰo̲utlined 0) c̲at
193 ("together", 1 s̲elect ˡtogether, 2 s̲elect ˡtogether, 6) ʰs̲pans 0)
194}
195
196⍝│ the captions, | between them, their shapes the arrays'
197ˡstepTexts ← ("C = A x B: " c̲at ("" ʰs̲hape ˡa) c̲at " x " c̲at ("" ʰs̲hape ˡb) c̲at " -> " c̲at ("" ʰs̲hape ˡc) c̲at "|" c̲at
198 "one item of C: a row of A times a column of B|" c̲at
199 (f̲ormat 2 s̲elect s̲hape ˡa) c̲at " products, added: one item of C|" c̲at
200 "then the next item, and the next|" c̲at
201 "on the GPU: all " c̲at (f̲ormat t̲ally r̲avel ˡc) c̲at " at once, one work-item each")
202
203⍝│ when each caption shows (from, to) and its color
204ˡs̲tepCaptions ← { z →
205 s ← ˡstarts
206 e ← ˡstarts + ˡdurations
207 from ← 0.0 c̲at (1 s̲elect s) c̲at (2 s̲elect s) c̲at (4 s̲elect s) c̲at 1 s̲elect ˡtogether
208 to ← ((1 s̲elect s) − 0.05) c̲at (1 s̲elect e) c̲at (3 s̲elect e) c̲at (6 s̲elect e) c̲at ˡlength
209 (2 1 t̲ranspose 2 5 r̲eshape from c̲at to) c̲at₂ f̲loat 5 3 r̲eshape white c̲at yellow c̲at white c̲at white c̲at yellow
210}
211
212⍝│ the camera: three keys about the cube, from the left and in front,
213⍝│ drifting
214ˡs̲tepCamera ← { z →
215 center ← 0.35 2.6 -1.3
216 ts ← 0.0 c̲at (ˡlength ÷ 2.0) c̲at ˡlength
217 (ts, 3 3 r̲eshape (center + -7.5 -11.5 6.0) c̲at (center + -5.5 -12.5 5.0) c̲at center + -8.0 -11.0 6.5, 3 3 r̲eshape center)
218}
219
220⍝│ (width, height) mm:s_teps 0: the whole steps script but its output:
221⍝│ the stage (a 40 mm lens), the walls, the items, the captions, the
222⍝│ camera
223ˡs̲teps ← { (w, h) z →
224 ((ᵛᵈhead c̲at ((w, h, 30, ˡlength, ˡsky, 40) ᵛᵈs̲tage 0) c̲at (ˡw̲alls 0) c̲at ˡi̲tems 0) c̲at
225 ((ˡstepTexts, ˡs̲tepCaptions 0) ᵛᵈc̲aptions 0) c̲at
226 (ˡs̲tepCamera 0) ᵛᵈc̲amera 0)
227}