-
Notifications
You must be signed in to change notification settings - Fork 255
/
matmul.vala
101 lines (81 loc) · 2.15 KB
/
matmul.vala
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
//valac --pkg gio-2.0 --pkg posix -X -O3 -D GCC_TEST matmul.vala
class Prog
{
static void notify(string msg)
{
try
{
var socket = new SocketClient();
var conn = socket.connect_to_host("localhost",9001,null);
conn.output_stream.write (msg.data);
}
catch (Error e)
{
// standalone usage
}
}
static double[,] MatGen(int n, double seed)
{
var tmp = seed / n / n;
var a = new double[n, n];
for (var i = 0; i < n; ++i)
for (var j = 0; j < n; ++j)
a[i,j] = tmp * (i - j) * (i + j);
return a;
}
static double[,] MatMul(ref double[,] a, ref double[,] b)
{
var m = a.length[0];
var n = a.length[1];
var p = b.length[1];
var x = new double[m, p];
var c = new double[p, n];
// transpose
for (var i = 0; i < n; ++i)
for (var j = 0; j < p; ++j)
c[j,i] = b[i, j];
for (var i = 0; i < m; ++i)
for (var j = 0; j < p; ++j)
{
var s = 0.0;
for (var k = 0; k < n; ++k)
s += a[i, k] * c[j, k];
x[i, j] = s;
}
return x;
}
private static double Calc(int n) {
n = n / 2 * 2;
var a = MatGen(n, 1.0);
var b = MatGen(n, 2.0);
var x = MatMul(ref a, ref b);
return x[n / 2, n / 2];
}
static void main(string[] args)
{
var n = (args.length > 1) ? (int.parse(args[1])) : 100;
var left = Calc(101);
var right = -18.67;
if ((left - right).abs() > 0.1) {
stderr.printf(@"$(left) != $(right)");
Process.exit(1);
}
var msg = "Vala/";
#if GCC_TEST
msg += "gcc";
#elif CLANG_TEST
msg += "clang";
#else
// The preprocessor directive for the test should be specified
Process.exit(-1);
#endif
msg += @"\t $((int)Posix.getpid())";
notify(msg);
var timer = new Timer();
var results = Calc(n);
timer.stop();
notify("stop");
stdout.printf(results.to_string()+"\n");
stdout.printf(@"time: $(timer.elapsed())s\n");
}
}