libraryextensions/blender/demos/Matmul.xtl

Matmul.xtl -- a matrix product, as two videos (Video.xtl): a small one a step at a time, C = A x B for X_eTaL-gpu's matmul demo ([3 4] x [4 2]), and a large one as a fly-through, 512 x 512 x 512. The products and sums are X_eTaL-gpu's Accel library (ac:m_atmul, the inner product its GPU runs); what is new here is only how they are shown: every cell's place, color and timing, every label and the camera's path, worked out from those arrays. Shared by the demos (demos/matmul-steps.xtl, demos/matmul-fly.xtl), their tests and the literate document (docs/literate/matmul.org). Import: "mm:" u_se< "Matmul"

source · imports ac: work/gpu/libs/Accel/src/Accel.xtl; vd: extensions/blender/lib/Video.xtl

The arrays (Accel)

ˡa : Int

value · line 18

a: X_eTaL-gpu's matmul demo's left matrix, 1 to 12 (3 by 4)

ˡa ← 3 4 r̲eshape r̲ange 12

ˡb : Int

value · line 21

b: its right matrix (4 by 2)

ˡb ← 4 2 r̲eshape 1 0 0 1 1 1 2 -1

ˡc : Int

value · line 30

c: their product, ac:m_atmul (3 by 2): each item the sum of a row of a times a column of b

      "mm:" u̲se< "Matmul"
      ᵐᵐc
12 1
28 5
44 9
ˡc ← ˡa ᵃᶜm̲atmul ˡb

Every product, one at a time

ˡitemI : Int

value · line 36

the items of c in the order they are worked out, row by row: i and j from 0 (6 each)

ˡitemI ← ((r̲ange 6) − 1) d̲iv 2

ˡitemJ : Int

value · line 38

j

ˡitemJ ← ((r̲ange 6) − 1) m̲od 2

ˡprodQ : Int

value · line 42

for each of the 24 products, item by item, k from 0 to 3 within each: its item (from 0), i, j and k

ˡprodQ ← ((r̲ange 24) − 1) d̲iv 4

ˡprodI : Int

value · line 44

i

ˡprodI ← (ˡprodQ + 1) s̲elect ˡitemI

ˡprodJ : Int

value · line 46

j

ˡprodJ ← (ˡprodQ + 1) s̲elect ˡitemJ

ˡprodK : Int

value · line 48

k

ˡprodK ← ((r̲ange 24) − 1) m̲od 4

ˡproducts : Int

value · line 54

the products: a[i;k] times b[k;j], each (24)

      "mm:" u̲se< "Matmul"
      4 t̲ake ᵐᵐproducts
1 0 3 8
ˡproducts ← ((1 + ˡprodK + 4 × ˡprodI) s̲elect r̲avel ˡa) × (1 + ˡprodJ + 2 × ˡprodK) s̲elect r̲avel ˡb
Used in: ˡsums, ˡi̲tems

ˡsums : Int

value · line 60

the products added up, four at a time, are c, item by item

      "mm:" u̲se< "Matmul"
      (r̲avel ᵐᵐc) = '+ r̲/₂ 6 4 r̲eshape ᵐᵐproducts
1 1 1 1 1 1
ˡsums ← '+ r̲/₂ 6 4 r̲eshape ˡproducts

Where things stand

g : Float

value (private) · line 65
g ← 1.3

d : Float

value (private) · line 66
d ← 0.86

ʰx̲yz : (Num a, Num b, Num c, Any d) => (a, b, c) -> d -> Float

function (private) · line 69
ʰx̲yz ← { (x, y, zz) q →
  n ← (t̲ally x) m̲ax (t̲ally y) m̲ax t̲ally zz
  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
}

ˡa̲t : (Num a, Num b, Any c) => (a, b) -> c -> Float

function · line 78

The cube: x is j (across, b's and c's columns), y is k (into the picture, the sum's index), z is -i (down, a's and c's rows). a stands on the left wall, b on the ceiling, c on the back wall, and the products fill the cube between them. (i, k) mm:a_t 0: a's cells

ˡa̲t ← { (i, k) z → (0.0 − g, (f̲loat k) + 0.5, 0.0 − (f̲loat i) + 0.5) ʰx̲yz 0 }

ˡb̲t : (Num a, Num b, Any c) => (a, b) -> c -> Float

function · line 80

(k, j) mm:b_t 0: b's cells

ˡb̲t ← { (k, j) z → ((f̲loat j) + 0.5, (f̲loat k) + 0.5, g) ʰx̲yz 0 }

ˡc̲t : (Num a, Num b, Any c) => (a, b) -> c -> Float

function · line 82

(i, j) mm:c_t 0: c's cells

ˡc̲t ← { (i, j) z → ((f̲loat j) + 0.5, 4.0 + g, 0.0 − (f̲loat i) + 0.5) ʰx̲yz 0 }

ˡp̲t : (Num a, Num b, Num c, Any d) => (a, b, c) -> d -> Float

function · line 84

(i, j, k) mm:p_t 0: a product's place in the cube

ˡ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 }
Used in: ˡi̲tems

ʰs̲ized : Any b => (a, a) -> b -> a

function (private) · line 87
ʰs̲ized ← { (at, size) z → at c̲at₂ ((t̲ally at) c̲at 3) r̲eshape size }

ʰo̲f : Int -> a -> a

function (private) · line 90
ʰo̲f ← { n c → (n c̲at 3) r̲eshape c }

When

ˡdurations : Float

value · line 95

each item's time: slow at first, then quicker (seconds, 6)

ˡdurations ← 3.0 2.2 1.6 1.3 1.1 1.0

ˡstarts : Float

value · line 97

when each item starts: 1.5 s, then one after another

ˡstarts ← 1.5 + 0.0 c̲at '+ s̲\ -1 d̲rop ˡdurations

ˡlast : Float

value · line 99

when the last item ends

ˡlast ← 6 s̲elect ˡstarts + ˡdurations

ˡtogether : Float

value · line 101

all six worked out at once: from 0.3 s after the last, for 2.5 s

ˡtogether ← (0.3 + ˡlast) c̲at 2.8 + ˡlast

ˡlength : Float

value · line 103

the video's length: a second after that

ˡlength ← 3.8 + ˡlast

ˡappear : Float

value · line 107

for each product: when it appears (one after another in the first half of its item's time), when it flies to c, when it lands (24 each)

ˡappear ← ((ˡprodQ + 1) s̲elect ˡstarts) + ((ˡprodQ + 1) s̲elect ˡdurations) × 0.12 + 0.45 × (f̲loat ˡprodK) ÷ 4.0
Used in: ˡi̲tems

ˡflies : Float

value · line 109

when it flies

ˡflies ← ((ˡprodQ + 1) s̲elect ˡstarts) + 0.68 × (ˡprodQ + 1) s̲elect ˡdurations
Used in: ˡi̲tems

ˡlands : Float

value · line 111

when it lands, and its item of c is shown

ˡlands ← ((ˡprodQ + 1) s̲elect ˡstarts) + 0.88 × (ˡprodQ + 1) s̲elect ˡdurations
Used in: ˡi̲tems

The steps video

white : Int

value (private) · line 116
white ← 255 255 255

yellow : Int

value (private) · line 117
yellow ← 255 216 77

empty : Int

value (private) · line 118
empty ← 42 46 54
Used in: ˡw̲alls

edge : Int

value (private) · line 119
edge ← 138 147 163
Used in: ˡw̲alls

ˡsky : Int

value · line 121

the sky (0 to 255)

ˡsky ← 7 8 10
Used in: ˡs̲teps

ʰg̲roup : (Num a, Any b) => (Char, Float, Float, a, Float) -> b -> Char

function (private) · line 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 }

ʰo̲utlined : (Num a, Any b) => (Char, Float, Float, a, Float) -> b -> Char

function (private) · line 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 }

ʰw̲ords : (Num a, Any b) => (Char, Char, Float, Float, a) -> b -> Char

function (private) · line 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 }

ʰs̲pans : (Num a, Any b) => (Char, a, a, Int) -> b -> Char

function (private) · line 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 }
Used in: ˡi̲tems

ˡw̲alls : Any a => a -> Char

function · line 138

the walls: a, b and c's cells colored by their values, labeled, and the cube's edges; c's cells start empty; each wall's name and shape by it (A's and B's above, C's to its right)

ˡw̲alls ← { z →
  ai ← ((r̲ange 12) − 1) d̲iv 4
  ak ← ((r̲ange 12) − 1) m̲od 4
  bk ← ((r̲ange 8) − 1) d̲iv 2
  bj ← ((r̲ange 8) − 1) m̲od 2
  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
    (("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
    (("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
    (("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
    (("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
    ("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)
  names ← ("A" ʰs̲hape ˡa) c̲at "|" c̲at ("B" ʰs̲hape ˡb) c̲at "|" c̲at "C" ʰs̲hape ˡc
  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
}
Used in: ˡs̲teps

ʰs̲hape : Char -> Int -> Char

function (private) · line 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 "]" }

ˡi̲tems : Any a => a -> Char

function · line 160

one item at a time: its row of a, its column of b and its cell of c outlined; its four products appear in the cube, each with guide lines from its two factors, fly into c's cell and shrink there; then c's cell shows its sum

ˡi̲tems ← { z →
  i ← ˡitemI
  j ← ˡitemJ
  ends ← ˡstarts + ˡdurations
  pi ← ˡprodI
  pj ← ˡprodJ
  pk ← ˡprodK
  at ← (pi, pj, pk) ˡp̲t 0
  into ← (pi, pj) ˡc̲t 0
  colors ← (ˡproducts, -4.0, 12.0) ᵛᵈt̲one 0
  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
  fa ← (pi, pk) ˡa̲t 0
  fb ← (pk, pj) ˡb̲t 0
  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
  done ← (4 × r̲ange 6) s̲elect ˡlands
  ((("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
    (("rows", ˡstarts, ends, 6) ʰs̲pans 0) c̲at
    (("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
    (("columns", ˡstarts, ends, 6) ʰs̲pans 0) c̲at
    (("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
    (("targets", ˡstarts, ends, 6) ʰs̲pans 0) c̲at
    (("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
    (("products", 2 1 t̲ranspose 2 24 r̲eshape ˡflies c̲at ˡlands, keys) ᵛᵈm̲ovesEach 0) c̲at
    (("products", ˡappear, ˡlands, 24) ʰs̲pans 0) c̲at
    (("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
    (("product values", ˡappear, ˡflies, 24) ʰs̲pans 0) c̲at
    (("guides", guides) ᵛᵈl̲ines 0) c̲at
    (("guides", ˡappear c̲at ˡappear, ˡflies c̲at ˡflies, 48) ʰs̲pans 0) c̲at
    (("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
    (("C", done, ˡlength, 6) ʰs̲pans 0) c̲at
    (("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
    (("C values", done, ˡlength, 6) ʰs̲pans 0) c̲at
    (("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
    ("together", 1 s̲elect ˡtogether, 2 s̲elect ˡtogether, 6) ʰs̲pans 0)
}
Used in: ˡs̲teps

ˡstepTexts : Char

value · line 197

the captions, | between them, their shapes the arrays'

ˡ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
  "one item of C: a row of A times a column of B|" c̲at
  (f̲ormat 2 s̲elect s̲hape ˡa) c̲at " products, added: one item of C|" c̲at
  "then the next item, and the next|" c̲at
  "on the GPU: all " c̲at (f̲ormat t̲ally r̲avel ˡc) c̲at " at once, one work-item each")
Used in: ˡs̲teps

ˡs̲tepCaptions : Any a => a -> Float

function · line 204

when each caption shows (from, to) and its color

ˡs̲tepCaptions ← { z →
  s ← ˡstarts
  e ← ˡstarts + ˡdurations
  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
  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
  (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
}
Used in: ˡs̲teps

ˡs̲tepCamera : Any a => a -> (Float, Float, Float)

function · line 214

the camera: three keys about the cube, from the left and in front, drifting

ˡs̲tepCamera ← { z →
  center ← 0.35 2.6 -1.3
  ts ← 0.0 c̲at (ˡlength ÷ 2.0) c̲at ˡlength
  (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)
}
Used in: ˡs̲teps

ˡs̲teps : Any c => (a, b) -> c -> Char

function · line 223

(width, height) mm:s_teps 0: the whole steps script but its output: the stage (a 40 mm lens), the walls, the items, the captions, the camera

ˡs̲teps ← { (w, h) z →
  ((ᵛᵈhead c̲at ((w, h, 30, ˡlength, ˡsky, 40) ᵛᵈs̲tage 0) c̲at (ˡw̲alls 0) c̲at ˡi̲tems 0) c̲at
    ((ˡstepTexts, ˡs̲tepCaptions 0) ᵛᵈc̲aptions 0) c̲at
    (ˡs̲tepCamera 0) ᵛᵈc̲amera 0)
}
Used in: script