-
Notifications
You must be signed in to change notification settings - Fork 258
/
Copy pathmatmul.v
79 lines (73 loc) · 1.35 KB
/
matmul.v
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import os
import math
import net
fn make_matrix(n int, m int) [][]f64 {
return [][]f64{len: m, init: []f64{len: n}}
}
fn matgen(n int, seed f64) [][]f64 {
mut a := make_matrix(n, n)
tmp := seed / n / n
for i := 0; i < n; i++ {
for j := 0; j < n; j++ {
v := tmp * f64(i - j) * f64(i + j)
a[i][j] = v
}
}
return a
}
fn matmul(a [][]f64, b [][]f64) [][]f64 {
m := a.len
n := a[0].len
p := b[0].len
// transpose
mut b2 := make_matrix(n, p)
for i := 0; i < n; i++ {
for j := 0; j < p; j++ {
b2[j][i] = b[i][j]
}
}
// mul
mut c := make_matrix(m, p)
for i := 0; i < m; i++ {
for j := 0; j < p; j++ {
mut s := 0.0
ai := a[i]
b2j := b2[j]
for k := 0; k < n; k++ {
s += ai[k] * b2j[k]
}
c[i][j] = s
}
}
return c
}
fn notify(msg string) {
mut sock := net.dial_tcp('127.0.0.1:9001') or { return }
defer {
sock.close() or {}
}
sock.write_string(msg) or {}
}
fn calc(n int) f64 {
size := n / 2 * 2
a := matgen(size, 1.0)
b := matgen(size, 2.0)
c := matmul(a, b)
return c[size / 2][size / 2]
}
fn main() {
n := if os.args.len > 1 { os.args[1].int() } else { 100 }
left := calc(101)
right := -18.67
if math.abs(left - right) > 0.1 {
panic('${left} != ${right}')
}
mut lang := 'V/gcc'
$if clang {
lang = 'V/clang'
}
notify('${lang}\t${C.getpid()}')
results := calc(n)
notify('stop')
println(results)
}