-
Notifications
You must be signed in to change notification settings - Fork 255
/
matmul.scala
78 lines (65 loc) · 1.65 KB
/
matmul.scala
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
object MatMul {
type Matrix = Array[Array[Double]]
def matgen(n: Int, seed: Double): Matrix = {
var a = Array.ofDim[Double](n, n)
val tmp = seed / n / n
for (i <- 0 until n) {
for (j <- 0 until n) {
a(i)(j) = tmp * (i - j) * (i + j)
}
}
a
}
def matmul(a: Matrix, b: Matrix): Matrix = {
val m = a.length
val n = a(0).length
val p = b(0).length
// transpose
var b2 = Array.ofDim[Double](n, p)
for (i <- 0 until n)
for (j <- 0 until p)
b2(j)(i) = b(i)(j)
// multiplication
var c = Array.ofDim[Double](m, p)
for (i <- 0 until m) {
for (j <- 0 until p) {
var s = 0.0
val ai = a(i)
val b2j = b2(j)
for (k <- 0 until n) s += ai(k) * b2j(k)
c(i)(j) = s
}
}
c
}
def notify(msg: String): Unit = {
scala.util.Using(
(new java.net.Socket("localhost", 9001)).getOutputStream()
) {
_.write(msg.getBytes())
}
}
def calc(n: Int): Double = {
val size = n / 2 * 2
val a = matgen(size, 1.0)
val b = matgen(size, 2.0)
val x = matmul(a, b)
x(size / 2)(size / 2)
}
def main(args: Array[String]): Unit = {
val n = if (args.length > 0) args(0).toInt else 100
val left = calc(101)
val right = -18.67
if (Math.abs(left - right) > 0.1) {
System.err.println(s"${left} != ${right}")
System.exit(1)
}
notify(s"Scala\t${ProcessHandle.current().pid()}")
val start_time = System.nanoTime
val results = calc(n)
val elapsed = (System.nanoTime - start_time) / 1e9
notify("stop")
println(results)
println("time: " + elapsed + "s")
}
}