libraryextensions/blender/demos/MatmulFly.xtl
MatmulFly.xtl -- a large matrix product as a 3D construct, a fly-through video (Video.xtl): C = A x B for n by n matrices (512 in the demo), by X_eTaL-gpu's Accel library (ac:m_atmul). A stands on the left wall, B on the ceiling, C on the front wall, every item a cell colored by its value; the n x n x n multiply-adds fill the cube between them. The camera starts far out, flies to one work-group (a 16 by 16 tile of C, as a GPU schedules it), then to one work-item (a row of A times a column of B): its n products stand as bars, added one at a time from the far end into one item of C; then C fills in, a work-group at a time. Every array here is worked out in X_eTaL; Video.xtl writes them into the script Blender runs. Shared by the demo (demos/matmul-fly.xtl), its test and the literate document (docs/literate/matmul.org). Import: "mf:" u_se< "MatmulFly"
The arrays
ˡl̲eft : Any a => Int -> a -> Float
n mf:l_eft 0: the left matrix A (n by n), smooth waves with a ripple, so its wall shows a pattern at every scale
"mf:" u̲se< "MatmulFly"
s̲hape 8 ᵐᶠl̲eft 0 8 8
ˡl̲eft ← { n z → i ← f̲loat (r̲ange n) − 1 ((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 }
ˡr̲ight : Any a => Int -> a -> Float
n mf:r_ight 0: the right matrix B (n by n), waves of its own
ˡr̲ight ← { n z → i ← f̲loat (r̲ange n) − 1 ((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 }
Places
ˡw̲orkGroup : Any a => Int -> a -> Int
n mf:w_orkGroup 0: the work-group the camera visits, its first row and column (from 0): 5/8 of the way down C's tiles, 13/32 across -- for 512, tile 20 13, rows 320 to 335, columns 208 to 223
"mf:" u̲se< "MatmulFly"
512 ᵐᶠw̲orkGroup 0 320 208
ˡw̲orkGroup ← { n z → t ← n d̲iv tile tile × ((5 × t) d̲iv 8) c̲at (13 × t) d̲iv 32 }
ˡw̲orkItem : Any a => Int -> a -> Int
n mf:w_orkItem 0: the work-item the camera visits: row 5 and column 9 of its work-group
ˡw̲orkItem ← { n z → (n ˡw̲orkGroup 0) + 5 9 }
ʰb̲ox : Float -> Float -> Float
ʰb̲ox ← { lo hi → (0.5 × lo + hi) c̲at a̲bs hi − lo }
ʰb̲oxes : (Num a, Any b) => (Float, a, Float) -> b -> Float
ʰ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 }
When (seconds; the video is 18)
ˡsumTimes : Float
the n products appear, the running sum goes from the far end to the near (0.56 to 0.78 of the video), C fills in (0.84 to 0.97)
ˡsumTimes ← ʰa̲t 0.56 0.78
ˡf̲ill : Any a => Int -> a -> Float
n mf:f_ill 0: the second each item of C appears, a work-group at a time, row by row of work-groups (n by n)
ˡf̲ill ← { n z → t ← n d̲iv tile g ← ((((r̲ange n) − 1) d̲iv tile) × t) '+ t̲able ((r̲ange n) − 1) d̲iv tile f ← ˡfillTimes (1 s̲elect f) + ((2 s̲elect f) − 1 s̲elect f) × ((f̲loat g) + 0.5) ÷ f̲loat t × t }
The scene
orange : Int
orange ← 255 122 61
ˡw̲alls : Any a => Int -> a -> Char
n mf:w_alls 0: A, B and C as walls of cells colored by value (C appearing a work-group at a time, on a pane of glass until then), the cube's edges, and each wall's name
ˡw̲alls ← { n z → u ← side ÷ f̲loat n a ← n ˡl̲eft 0 b ← n ˡr̲ight 0 c ← a ᵃᶜm̲atmul b zero ← (n c̲at n) r̲eshape 0.0 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 (("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 (("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 (("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 ("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) sh ← "[" c̲at (f̲ormat n c̲at n) c̲at "]" names ← "A " c̲at sh c̲at "|B " c̲at sh c̲at "|C " c̲at sh 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 t c̲at ("names", names, at c̲at₂ (3 4 r̲eshape 1.3 c̲at f̲loat white)) ᵛᵈl̲abels 0 }
ˡw̲ork : Any a => Int -> a -> Char
n mf:w_ork 0: one work-group and one work-item. The work-group: its tile of C outlined, the rows of A and columns of B it reads, the slab of the cube it covers. The work-item: a beam through the cube along its row of A and column of B, its n products as bars along the beam (orange positive, blue negative), the running sum -- the products added from the far end -- as one white bar moving to the near end, and its item of C, lit
ˡw̲ork ← { n z → u ← side ÷ f̲loat n a ← n ˡl̲eft 0 b ← n ˡr̲ight 0 c ← a ᵃᶜm̲atmul b (i0, j0) ← (1 s̲elect n ˡw̲orkGroup 0, 2 s̲elect n ˡw̲orkGroup 0) (wi, wj) ← (1 s̲elect n ˡw̲orkItem 0, 2 s̲elect n ˡw̲orkItem 0) x0 ← u × f̲loat j0 x1 ← u × f̲loat j0 + tile z0 ← 0.0 − u × f̲loat i0 + tile z1 ← 0.0 − u × f̲loat i0 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 bx ← u × 0.5 + f̲loat wj bz ← 0.0 − u × 0.5 + f̲loat wi wx0 ← u × f̲loat wj wx1 ← u × f̲loat wj + 1 wz0 ← 0.0 − u × f̲loat wi + 1 wz1 ← 0.0 − u × f̲loat wi 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 p ← ((wi + 1) s̲elect a) × (wj + 1) s̲elect₂ b ky ← u × 0.5 + f̲loat (r̲ange n) − 1 h ← 0.06 × p 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 sign ← f̲loat p ≥ 0.0 colors ← (sign '× t̲able f̲loat ˡplus) + (1.0 − sign) '× t̲able f̲loat ˡminus partial ← r̲ev '+ s̲\ r̲ev p ks ← ((n − 1) − 8 × (r̲ange n d̲iv 8) − 1) c̲at 0 m ← t̲ally ks hs ← 0.5 ÷ 'm̲ax r̲/ a̲bs partial ph ← hs × (ks + 1) s̲elect partial 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 st ← ˡsumTimes ts ← (1 s̲elect st) + ((2 s̲elect st) − 1 s̲elect st) × (f̲loat (r̲ange m) − 1) ÷ f̲loat m − 1 v ← (wj + 1) s̲elect (wi + 1) s̲elect c 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 cell ← (bx c̲at ((0.0 − gap) − 0.03) c̲at bz) c̲at u c̲at (0.028) c̲at u ((("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 (("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 (("products", bars c̲at₂ colors c̲at₂ n r̲eshape 1.5) ᵛᵈb̲oxes 0) c̲at (("products", (n c̲at 2) r̲eshape ((1 s̲elect ˡsumTimes) − 0.72) c̲at ˡlength) ᵛᵈs̲hown 0) c̲at (("sum", (1 6 r̲eshape 1 s̲elect kk, white, 4.0) ʰb̲oxes 0) ᵛᵈb̲oxes 0) c̲at (("sum", ts, (1 c̲at m c̲at 6) r̲eshape kk) ᵛᵈm̲oves 0) c̲at (("sum", 1 2 r̲eshape st) ᵛᵈs̲hown 0) c̲at (("result", (1 6 r̲eshape cell, 1 s̲elect rc, 3.0) ʰb̲oxes 0) ᵛᵈb̲oxes 0) c̲at ("result", 1 2 r̲eshape (2 s̲elect st) c̲at ˡlength) ᵛᵈs̲hown 0) }
ˡc̲ameraKeys : Any a => Int -> a -> (Float, Float, Float)
n mf:c_ameraKeys 0: the camera's path, eight keys (seconds, eyes, aims): far out to see the whole cube, closer, at the work-group's tile, at the work-item's cell, beside its beam looking along it (while the sum runs), back at the cell, then far out again while C fills in
ˡc̲ameraKeys ← { n z → u ← side ÷ f̲loat n (i0, j0) ← (1 s̲elect n ˡw̲orkGroup 0, 2 s̲elect n ˡw̲orkGroup 0) (wi, wj) ← (1 s̲elect n ˡw̲orkItem 0, 2 s̲elect n ˡw̲orkItem 0) center ← (0.5 × side) c̲at (0.5 × side) c̲at -0.5 × side 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 cc ← (u × 0.5 + f̲loat wj) c̲at (0.0 − gap) c̲at 0.0 − u × 0.5 + f̲loat wi sd ← (1 s̲elect cc) c̲at 0.0 c̲at 3 s̲elect cc ts ← ʰa̲t 0.0 0.16 0.32 0.44 0.54 0.70 0.80 1.0 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 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 (ts, eyes, aims) }
ˡc̲aptionTexts : (Num a, Any b) => a -> b -> Char
n mf:c_aptionTexts 0: the captions, | between them, their numbers the arrays'
ˡc̲aptionTexts ← { n z → ns ← f̲ormat n ("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 "one work-group: a " c̲at (f̲ormat tile) c̲at " x " c̲at (f̲ormat tile) c̲at " tile of C|" c̲at "one work-item: a row of A times a column of B|" c̲at ns c̲at " products, added one at a time from the far end|" c̲at "the sum is one item of C|" c̲at "every item of C: " c̲at (f̲ormat n × n) c̲at " sums, a work-group at a time") }
ˡc̲aptionTable : Any a => a -> Float
the captions' spans (fractions of the video) and colors
ˡc̲aptionTable ← { z → from ← ʰa̲t 0.0 0.08 0.29 0.43 0.56 0.79 0.86 to ← ʰa̲t 0.07 0.27 0.42 0.55 0.78 0.85 1.0 (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 }
ˡs̲cene : Any c => (a, b, Int) -> c -> Char
(width, height, n) mf:s_cene 0: the whole fly-through script but its output: the stage (a 35 mm lens), the walls, the work-group and work-item, the captions, the camera
ˡs̲cene ← { (w, h, n) z → ((ᵛᵈ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 ((n ˡc̲aptionTexts 0, ˡc̲aptionTable 0) ᵛᵈc̲aptions 0) c̲at (n ˡc̲ameraKeys 0) ᵛᵈc̲amera 0) }