tobogganSe connecter

louise

The Louise language: compiler, runtime, std, tooling

Clone avec HTTPS, un jeton personnel en mot de passe, ou avec SSH, une clé ajoutée dans vos paramètres.

Dernier runEn cours#127 · main
0Étoiles
0Forks
40Issues
121 lignes · 1928 octetsBrut · Historique
1// A matrix product (issue #190's float-heavy kernel): C = A·B over flat
2// arrays. The element loop's arithmetic runs on raw doubles (ADR 0045):
3// it allocates nothing per iteration.
4
5import std/array.{type Array}
6import std/float
7import std/io
8
9/// C[i][j] = Σ_k A[i][k]·B[k][j].
10fn dot ::
11 a: Array<Float>
12 b: Array<Float>
13 i: Int
14 j: Int
15 k: Int
16 n: Int
17 acc: Float
18 -> Float
19{
20 if k == n
21 {
22 acc
23 }
24 else
25 {
26 match (array.get a (i * n + k), array.get b (k * n + j))
27 { (Some(x), Some(y)) -> dot a b i j (k + 1) n (acc + x * y)
28 , _ -> panic "matmul: index"
29 }
30 }
31}
32
33fn cols ::
34 a: Array<Float>
35 b: Array<Float>
36 c: Array<Float>
37 i: Int
38 j: Int
39 n: Int
40 -> Array<Float>
41{
42 if j == n
43 {
44 c
45 }
46 else
47 {
48 let d = dot a b i j 0 n 0.0
49 cols a b (array.set c (i * n + j) d) i (j + 1) n
50 }
51}
52
53fn rows ::
54 a: Array<Float>
55 b: Array<Float>
56 c: Array<Float>
57 i: Int
58 n: Int
59 -> Array<Float>
60{
61 if i == n
62 {
63 c
64 }
65 else
66 {
67 rows a b (cols a b c i 0 n) (i + 1) n
68 }
69}
70
71/// x·(3/7)+1/4, so the matrices are not all ones.
72fn fill :: n: Int off: Int i: Int acc: Array<Float> -> Array<Float>
73{
74 if i == n * n
75 {
76 acc
77 }
78 else
79 {
80 fill
81 n
82 off
83 (i + 1)
84 (array.push acc (float.from_int ((i + off) % 7) * 0.428571 + 0.25))
85 }
86}
87
88pub fn main :: -> () / <io>
89{
90 let n = 64
91 let a = fill n 0 0 (array.empty ())
92 let b = fill n 3 0 (array.empty ())
93 let c = rows a b (zeros n 0 (array.empty ())) 0 n
94 io.println (show (trace c 0 n 0.0))
95}
96//> 8153.808265755265
97
98fn zeros :: n: Int i: Int acc: Array<Float> -> Array<Float>
99{
100 if i == n * n
101 {
102 acc
103 }
104 else
105 {
106 zeros n (i + 1) (array.push acc 0.0)
107 }
108}
109
110fn trace :: c: Array<Float> i: Int n: Int acc: Float -> Float
111{
112 if i == n
113 {
114 acc
115 }
116 else
117 {
118 match array.get c (i * n + i)
119 { Some(x) -> trace c (i + 1) n (acc + x), None -> panic "trace: index" }
120 }
121}