sourceextensions/blender/demos/MatmulFly.xtl
1⍝│ MatmulFly.xtl -- a large matrix product as a 3D construct, a
2⍝│ fly-through video (Video.xtl): C = A x B for n by n matrices (512 in
3⍝│ the demo), by X_eTaL-gpu's Accel library (ac:m_atmul). A stands on
4⍝│ the left wall, B on the ceiling, C on the front wall, every item a
5⍝│ cell colored by its value; the n x n x n multiply-adds fill the cube
6⍝│ between them. The camera starts far out, flies to one work-group (a
7⍝│ 16 by 16 tile of C, as a GPU schedules it), then to one work-item (a
8⍝│ row of A times a column of B): its n products stand as bars, added
9⍝│ one at a time from the far end into one item of C; then C fills in,
10⍝│ a work-group at a time. Every array here is worked out in X_eTaL;
11⍝│ Video.xtl writes them into the script Blender runs. Shared by the
12⍝│ demo (demos/matmul-fly.xtl), its test and the literate document
13⍝│ (docs/literate/matmul.org).
14⍝│ Import: "mf:" u_se< "MatmulFly"
15
16"ac:" u̲se< "Accel"
17"vd:" u̲se< "Video"
18
19⍝━━ The arrays
20
21⍝│ n mf:l_eft 0: the left matrix A (n by n), smooth waves
22⍝│ with a ripple, so its wall shows a pattern at every scale
23⍝│ >> "mf:" u_se< "MatmulFly"
24⍝│ >> s_hape 8 mf:l_eft 0
25⍝│ 8 8
26ˡl̲eft ← { n z →
27 i ← f̲loat (r̲ange n) − 1
28 ((s̲in 0.031 × i) '+ t̲able 0.7 × c̲os 0.017 × i) + (f̲loat ((7 × (r̲ange n) − 1) '+ t̲able 3 × (r̲ange n) − 1) m̲od 23) ÷ 40.0
29}
30
31⍝│ n mf:r_ight 0: the right matrix B (n by n), waves of its own
32ˡr̲ight ← { n z →
33 i ← f̲loat (r̲ange n) − 1
34 ((0.8 × c̲os 0.023 × i) '+ t̲able s̲in 0.041 × i) + (f̲loat ((5 × (r̲ange n) − 1) '+ t̲able 11 × (r̲ange n) − 1) m̲od 19) ÷ 40.0
35}
36
37⍝━━ Places
38
39⍝ a work-group's side, the walls' distance from the cube, the cube's side
40tile ← 16
41gap ← 1.6
42side ← 10.0
43
44⍝│ n mf:w_orkGroup 0: the work-group the camera visits, its first row
45⍝│ and column (from 0): 5/8 of the way down C's tiles, 13/32 across --
46⍝│ for 512, tile 20 13, rows 320 to 335, columns 208 to 223
47⍝│ >> "mf:" u_se< "MatmulFly"
48⍝│ >> 512 mf:w_orkGroup 0
49⍝│ 320 208
50ˡw̲orkGroup ← { n z →
51 t ← n d̲iv tile
52 tile × ((5 × t) d̲iv 8) c̲at (13 × t) d̲iv 32
53}
54
55⍝│ n mf:w_orkItem 0: the work-item the camera visits: row 5 and column 9
56⍝│ of its work-group
57ˡw̲orkItem ← { n z → (n ˡw̲orkGroup 0) + 5 9 }
58
59⍝ a box from its low corner to its high (x y z each): its middle and size
60ʰb̲ox ← { lo hi → (0.5 × lo + hi) c̲at a̲bs hi − lo }
61
62⍝ rows for vd:b_oxes: boxes (each a middle and size), one color, a glow
63ʰb̲oxes ← { (bs, c, g) z → bs c̲at₂ ((1 s̲elect s̲hape bs) c̲at 4) r̲eshape (f̲loat c) c̲at g }
64
65⍝━━ When (seconds; the video is 18)
66
67⍝│ the video's length
68ˡlength ← 18.0
69⍝ a fraction of the video, in seconds
70ʰa̲t ← { f → ˡlength × f }
71
72⍝│ the n products appear, the running sum goes from the far end to the
73⍝│ near (0.56 to 0.78 of the video), C fills in (0.84 to 0.97)
74ˡsumTimes ← ʰa̲t 0.56 0.78
75⍝│ C fills in
76ˡfillTimes ← ʰa̲t 0.84 0.97
77
78⍝│ n mf:f_ill 0: the second each item of C appears, a work-group at a
79⍝│ time, row by row of work-groups (n by n)
80ˡf̲ill ← { n z →
81 t ← n d̲iv tile
82 g ← ((((r̲ange n) − 1) d̲iv tile) × t) '+ t̲able ((r̲ange n) − 1) d̲iv tile
83 f ← ˡfillTimes
84 (1 s̲elect f) + ((2 s̲elect f) − 1 s̲elect f) × ((f̲loat g) + 0.5) ÷ f̲loat t × t
85}
86
87⍝━━ The scene
88
89⍝ colors (0 to 255)
90white ← 255 255 255
91yellow ← 255 216 77
92orange ← 255 122 61
93edge ← 138 147 163
94⍝│ the sky
95ˡsky ← 7 8 10
96⍝│ a positive product's color, and a negative one's
97ˡplus ← 255 179 71
98⍝│ a negative one's
99ˡminus ← 79 168 255
100
101⍝│ n mf:w_alls 0: A, B and C as walls of cells colored by value (C
102⍝│ appearing a work-group at a time, on a pane of glass until then), the
103⍝│ cube's edges, and each wall's name
104ˡw̲alls ← { n z →
105 u ← side ÷ f̲loat n
106 a ← n ˡl̲eft 0
107 b ← n ˡr̲ight 0
108 c ← a ᵃᶜm̲atmul b
109 zero ← (n c̲at n) r̲eshape 0.0
110 t ← ((("A", (0.0 − gap) c̲at 0.0 0.0, 0.0 0.0 c̲at 0.0 − u, 0.0 c̲at u c̲at 0.0, (n c̲at n c̲at 3) r̲eshape (a, 'm̲in r̲/ 'm̲in r̲/ a, 'm̲ax r̲/ 'm̲ax r̲/ a) ᵛᵈt̲one 0, zero, 0.08) ᵛᵈg̲rid 0) c̲at
111 (("B", 0.0 0.0 c̲at gap, 0.0 c̲at u c̲at 0.0, u c̲at 0.0 0.0, (n c̲at n c̲at 3) r̲eshape (b, 'm̲in r̲/ 'm̲in r̲/ b, 'm̲ax r̲/ 'm̲ax r̲/ b) ᵛᵈt̲one 0, zero, 0.08) ᵛᵈg̲rid 0) c̲at
112 (("C", 0.0 c̲at (0.0 − gap) c̲at 0.0, 0.0 0.0 c̲at 0.0 − u, u c̲at 0.0 0.0, (n c̲at n c̲at 3) r̲eshape (c, 'm̲in r̲/ 'm̲in r̲/ c, 'm̲ax r̲/ 'm̲ax r̲/ c) ᵛᵈt̲one 0, n ˡf̲ill 0, 0.08) ᵛᵈg̲rid 0) c̲at
113 (("C empty", (1 6 r̲eshape (0.5 × side) c̲at ((0.0 − gap) + 0.01) c̲at (-0.5 × side) c̲at side c̲at 0.005 c̲at side, 40 48 60, 1.0) ʰb̲oxes 0) ᵛᵈg̲lass 0) c̲at
114 ("volume", (1 6 r̲eshape (0.5 × side) c̲at (0.5 × side) c̲at (-0.5 × side) c̲at side c̲at side c̲at side, edge, 1.0) ʰb̲oxes 0) ᵛᵈo̲utlines 0)
115 sh ← "[" c̲at (f̲ormat n c̲at n) c̲at "]"
116 names ← "A " c̲at sh c̲at "|B " c̲at sh c̲at "|C " c̲at sh
117 at ← 3 3 r̲eshape ((0.0 − gap) − 0.05) c̲at (0.5 × side) c̲at 0.6 c̲at (0.5 × side) c̲at (side + 0.4) c̲at (gap + 0.5) c̲at (0.5 × side) c̲at ((0.0 − gap) − 0.05) c̲at 0.6
118 t c̲at ("names", names, at c̲at₂ (3 4 r̲eshape 1.3 c̲at f̲loat white)) ᵛᵈl̲abels 0
119}
120
121⍝│ n mf:w_ork 0: one work-group and one work-item. The work-group: its
122⍝│ tile of C outlined, the rows of A and columns of B it reads, the slab
123⍝│ of the cube it covers. The work-item: a beam through the cube along
124⍝│ its row of A and column of B, its n products as bars along the beam
125⍝│ (orange positive, blue negative), the running sum -- the products
126⍝│ added from the far end -- as one white bar moving to the near end,
127⍝│ and its item of C, lit
128ˡw̲ork ← { n z →
129 u ← side ÷ f̲loat n
130 a ← n ˡl̲eft 0
131 b ← n ˡr̲ight 0
132 c ← a ᵃᶜm̲atmul b
133 (i0, j0) ← (1 s̲elect n ˡw̲orkGroup 0, 2 s̲elect n ˡw̲orkGroup 0)
134 (wi, wj) ← (1 s̲elect n ˡw̲orkItem 0, 2 s̲elect n ˡw̲orkItem 0)
135 x0 ← u × f̲loat j0
136 x1 ← u × f̲loat j0 + tile
137 z0 ← 0.0 − u × f̲loat i0 + tile
138 z1 ← 0.0 − u × f̲loat i0
139 bands ← 4 6 r̲eshape ((x0 c̲at ((0.0 − gap) − 0.03) c̲at z0) ʰb̲ox x1 c̲at ((0.0 − gap) − 0.01) c̲at z1) c̲at ((((0.0 − gap) − 0.03) c̲at 0.0 c̲at z0) ʰb̲ox ((0.0 − gap) − 0.01) c̲at side c̲at z1) c̲at ((x0 c̲at 0.0 c̲at gap + 0.01) ʰb̲ox x1 c̲at side c̲at gap + 0.03) c̲at (x0 c̲at 0.0 c̲at z0) ʰb̲ox x1 c̲at side c̲at z1
140 bx ← u × 0.5 + f̲loat wj
141 bz ← 0.0 − u × 0.5 + f̲loat wi
142 wx0 ← u × f̲loat wj
143 wx1 ← u × f̲loat wj + 1
144 wz0 ← 0.0 − u × f̲loat wi + 1
145 wz1 ← 0.0 − u × f̲loat wi
146 item ← 4 6 r̲eshape ((wx0 c̲at 0.0 c̲at wz0) ʰb̲ox wx1 c̲at side c̲at wz1) c̲at ((((0.0 − gap) − 0.03) c̲at 0.0 c̲at wz0) ʰb̲ox ((0.0 − gap) − 0.02) c̲at side c̲at wz1) c̲at ((wx0 c̲at 0.0 c̲at gap + 0.02) ʰb̲ox wx1 c̲at side c̲at gap + 0.03) c̲at (wx0 c̲at ((0.0 − gap) − 0.04) c̲at wz0) ʰb̲ox wx1 c̲at ((0.0 − gap) − 0.02) c̲at wz1
147 p ← ((wi + 1) s̲elect a) × (wj + 1) s̲elect₂ b
148 ky ← u × 0.5 + f̲loat (r̲ange n) − 1
149 h ← 0.06 × p
150 bars ← 2 1 t̲ranspose (6 c̲at n) r̲eshape (n r̲eshape bx) c̲at ky c̲at (bz + h ÷ 2.0) c̲at (n r̲eshape 0.7 × u) c̲at (n r̲eshape 0.7 × u) c̲at 0.002 m̲ax a̲bs h
151 sign ← f̲loat p ≥ 0.0
152 colors ← (sign '× t̲able f̲loat ˡplus) + (1.0 − sign) '× t̲able f̲loat ˡminus
153 partial ← r̲ev '+ s̲\ r̲ev p
154 ks ← ((n − 1) − 8 × (r̲ange n d̲iv 8) − 1) c̲at 0
155 m ← t̲ally ks
156 hs ← 0.5 ÷ 'm̲ax r̲/ a̲bs partial
157 ph ← hs × (ks + 1) s̲elect partial
158 kk ← 2 1 t̲ranspose (6 c̲at m) r̲eshape (m r̲eshape bx) c̲at (u × 0.5 + f̲loat ks) c̲at (bz + ph ÷ 2.0) c̲at (m r̲eshape 1.4 × u) c̲at (m r̲eshape 1.4 × u) c̲at 0.002 m̲ax a̲bs ph
159 st ← ˡsumTimes
160 ts ← (1 s̲elect st) + ((2 s̲elect st) − 1 s̲elect st) × (f̲loat (r̲ange m) − 1) ÷ f̲loat m − 1
161 v ← (wj + 1) s̲elect (wi + 1) s̲elect c
162 rc ← (v c̲at ('m̲in r̲/ 'm̲in r̲/ c) c̲at 'm̲ax r̲/ 'm̲ax r̲/ c, 'm̲in r̲/ 'm̲in r̲/ c, 'm̲ax r̲/ 'm̲ax r̲/ c) ᵛᵈt̲one 0
163 cell ← (bx c̲at ((0.0 − gap) − 0.03) c̲at bz) c̲at u c̲at (0.028) c̲at u
164 ((("work-group", bands c̲at₂ 4 4 r̲eshape 255.0 255.0 255.0 3.0 255.0 216.0 77.0 2.0 255.0 216.0 77.0 2.0 255.0 216.0 77.0 2.0) ᵛᵈo̲utlines 0) c̲at
165 (("work-item", item c̲at₂ 4 4 r̲eshape 255.0 122.0 61.0 4.0 255.0 122.0 61.0 4.0 255.0 122.0 61.0 4.0 255.0 255.0 255.0 6.0) ᵛᵈb̲oxes 0) c̲at
166 (("products", bars c̲at₂ colors c̲at₂ n r̲eshape 1.5) ᵛᵈb̲oxes 0) c̲at
167 (("products", (n c̲at 2) r̲eshape ((1 s̲elect ˡsumTimes) − 0.72) c̲at ˡlength) ᵛᵈs̲hown 0) c̲at
168 (("sum", (1 6 r̲eshape 1 s̲elect kk, white, 4.0) ʰb̲oxes 0) ᵛᵈb̲oxes 0) c̲at
169 (("sum", ts, (1 c̲at m c̲at 6) r̲eshape kk) ᵛᵈm̲oves 0) c̲at
170 (("sum", 1 2 r̲eshape st) ᵛᵈs̲hown 0) c̲at
171 (("result", (1 6 r̲eshape cell, 1 s̲elect rc, 3.0) ʰb̲oxes 0) ᵛᵈb̲oxes 0) c̲at
172 ("result", 1 2 r̲eshape (2 s̲elect st) c̲at ˡlength) ᵛᵈs̲hown 0)
173}
174
175⍝│ n mf:c_ameraKeys 0: the camera's path, eight keys (seconds, eyes,
176⍝│ aims): far out to see the whole cube, closer, at the work-group's
177⍝│ tile, at the work-item's cell, beside its beam looking along it
178⍝│ (while the sum runs), back at the cell, then far out again while C
179⍝│ fills in
180ˡc̲ameraKeys ← { n z →
181 u ← side ÷ f̲loat n
182 (i0, j0) ← (1 s̲elect n ˡw̲orkGroup 0, 2 s̲elect n ˡw̲orkGroup 0)
183 (wi, wj) ← (1 s̲elect n ˡw̲orkItem 0, 2 s̲elect n ˡw̲orkItem 0)
184 center ← (0.5 × side) c̲at (0.5 × side) c̲at -0.5 × side
185 tc ← (u × (f̲loat j0) + 0.5 × f̲loat tile) c̲at (0.0 − gap) c̲at 0.0 − u × (f̲loat i0) + 0.5 × f̲loat tile
186 cc ← (u × 0.5 + f̲loat wj) c̲at (0.0 − gap) c̲at 0.0 − u × 0.5 + f̲loat wi
187 sd ← (1 s̲elect cc) c̲at 0.0 c̲at 3 s̲elect cc
188 ts ← ʰa̲t 0.0 0.16 0.32 0.44 0.54 0.70 0.80 1.0
189 eyes ← 8 3 r̲eshape (center + -17.0 -27.0 13.0) c̲at (center + -9.0 -24.0 9.0) c̲at (tc + -0.9 -2.6 0.8) c̲at (cc + -0.12 -0.55 0.12) c̲at (sd + -0.75 -0.9 0.45) c̲at (sd + -0.6 -0.7 0.35) c̲at (cc + -0.35 -1.1 0.3) c̲at center + -5.0 -27.0 5.0
190 aims ← 8 3 r̲eshape center c̲at center c̲at tc c̲at cc c̲at (sd + 0.0 2.2 0.0) c̲at (sd + 0.0 1.2 0.0) c̲at cc c̲at center + 0.0 c̲at (0.0 − gap) c̲at 0.0
191 (ts, eyes, aims)
192}
193
194⍝│ n mf:c_aptionTexts 0: the captions, | between them, their numbers
195⍝│ the arrays'
196ˡc̲aptionTexts ← { n z →
197 ns ← f̲ormat n
198 ("C = A x B|" c̲at ns c̲at " x " c̲at ns c̲at " x " c̲at ns c̲at " = " c̲at (f̲ormat n × n × n) c̲at " multiply-adds|" c̲at
199 "one work-group: a " c̲at (f̲ormat tile) c̲at " x " c̲at (f̲ormat tile) c̲at " tile of C|" c̲at
200 "one work-item: a row of A times a column of B|" c̲at
201 ns c̲at " products, added one at a time from the far end|" c̲at
202 "the sum is one item of C|" c̲at
203 "every item of C: " c̲at (f̲ormat n × n) c̲at " sums, a work-group at a time")
204}
205
206⍝│ the captions' spans (fractions of the video) and colors
207ˡc̲aptionTable ← { z →
208 from ← ʰa̲t 0.0 0.08 0.29 0.43 0.56 0.79 0.86
209 to ← ʰa̲t 0.07 0.27 0.42 0.55 0.78 0.85 1.0
210 (2 1 t̲ranspose 2 7 r̲eshape from c̲at to) c̲at₂ f̲loat 7 3 r̲eshape white c̲at yellow c̲at yellow c̲at 255 154 106 c̲at white c̲at white c̲at yellow
211}
212
213⍝│ (width, height, n) mf:s_cene 0: the whole fly-through script but its
214⍝│ output: the stage (a 35 mm lens), the walls, the work-group and
215⍝│ work-item, the captions, the camera
216ˡs̲cene ← { (w, h, n) z →
217 ((ᵛᵈhead c̲at ((w, h, 30, ˡlength, ˡsky, 35) ᵛᵈs̲tage 0) c̲at (n ˡw̲alls 0) c̲at n ˡw̲ork 0) c̲at
218 ((n ˡc̲aptionTexts 0, ˡc̲aptionTable 0) ᵛᵈc̲aptions 0) c̲at
219 (n ˡc̲ameraKeys 0) ᵛᵈc̲amera 0)
220}