Skip to content
Newer
Older
100644 75 lines (58 sloc) 2.35 KB
bafa3ba job: 7045
Peter Hinely authored
1 module: matrix
2 synopsis: implementation of "Matrix Multiplication" benchmark
3 author: Peter Hinely
4 copyright: public domain
292272b job: 7045
Peter Hinely authored
5
6
bafa3ba job: 7045
Peter Hinely authored
7 define constant <vector-of-integers> = limited(<simple-vector>, of: <integer>);
292272b job: 7045
Peter Hinely authored
8
fd0922b job: 7045
Peter Hinely authored
9 define constant $empty-vector-of-integers = make(<vector-of-integers>);
10
bafa3ba job: 7045
Peter Hinely authored
11 define class <matrix> (<object>)
fd0922b job: 7045
Peter Hinely authored
12 constant slot rows :: <integer>, required-init-keyword: rows:;
13 constant slot columns :: <integer>, required-init-keyword: columns:;
14 slot elements :: <vector-of-integers>, init-value: $empty-vector-of-integers;
bafa3ba job: 7045
Peter Hinely authored
15 end class;
292272b job: 7045
Peter Hinely authored
16
bafa3ba job: 7045
Peter Hinely authored
17 define sealed domain make (singleton(<matrix>));
292272b job: 7045
Peter Hinely authored
18
4eea5be job: 7045
Peter Hinely authored
19 define sealed method initialize (matrix :: <matrix>, #key rows :: <integer>, columns :: <integer>) => ()
bafa3ba job: 7045
Peter Hinely authored
20 matrix.elements := make(<vector-of-integers>, size: (rows * columns), fill: 0);
21 end method;
292272b job: 7045
Peter Hinely authored
22
bafa3ba job: 7045
Peter Hinely authored
23 define inline function ele (matrix :: <matrix>, row :: <integer>, column :: <integer>) => value :: <integer>;
24 matrix.elements[(row * matrix.columns) + column];
25 end function;
292272b job: 7045
Peter Hinely authored
26
bafa3ba job: 7045
Peter Hinely authored
27 define inline function ele-setter (new-value :: <integer>, matrix :: <matrix>, row :: <integer>, column :: <integer>) => new-value :: <integer>;
28 matrix.elements[(row * matrix.columns) + column] := new-value;
29 end function;
292272b job: 7045
Peter Hinely authored
30
bafa3ba job: 7045
Peter Hinely authored
31 define function create-matrix (rows :: <integer>, columns :: <integer>) => m :: <matrix>;
32 let m = make(<matrix>, rows: rows, columns: columns);
33 for (i from 0 below m.elements.size)
34 m.elements[i] := i + 1;
292272b job: 7045
Peter Hinely authored
35 end for;
36 m;
bafa3ba job: 7045
Peter Hinely authored
37 end function;
38
39 define sealed method \* (m1 :: <matrix>, m2 :: <matrix>) => m :: <matrix>;
40 unless (m1.columns == m2.rows)
41 error("the number of columns of %= must equals the number of rows of %m=", m1, m2);
42 end;
292272b job: 7045
Peter Hinely authored
43
bafa3ba job: 7045
Peter Hinely authored
44 let rows = m1.rows;
45 let columns = m2.columns;
46 let m3 = make(<matrix>, rows: rows, columns: columns);
292272b job: 7045
Peter Hinely authored
47
48 for (i from 0 below rows)
49 for (j from 0 below columns)
50 let value = 0;
51 for (k from 0 below columns)
bafa3ba job: 7045
Peter Hinely authored
52 value := value + ele(m1, i, k) * ele(m2, k, j);
292272b job: 7045
Peter Hinely authored
53 end;
bafa3ba job: 7045
Peter Hinely authored
54 ele(m3, i, j) := value;
292272b job: 7045
Peter Hinely authored
55 end for;
56 end for;
57 m3;
bafa3ba job: 7045
Peter Hinely authored
58 end method;
292272b job: 7045
Peter Hinely authored
59
bafa3ba job: 7045
Peter Hinely authored
60 define function main () => ()
292272b job: 7045
Peter Hinely authored
61 let arg = string-to-integer(element(application-arguments(), 0, default: "1"));
bafa3ba job: 7045
Peter Hinely authored
62
292272b job: 7045
Peter Hinely authored
63 let size = 30;
bafa3ba job: 7045
Peter Hinely authored
64 let m1 = create-matrix(size, size);
65 let m2 = create-matrix(size, size);
66
292272b job: 7045
Peter Hinely authored
67 let mm = #f;
68 for (i from 1 to arg)
bafa3ba job: 7045
Peter Hinely authored
69 mm := m1 * m2;
292272b job: 7045
Peter Hinely authored
70 end;
bafa3ba job: 7045
Peter Hinely authored
71 format-out("%= %= %= %=\n", ele(mm, 0, 0), ele(mm, 2, 3), ele(mm, 3, 2), ele(mm, 4, 4));
72 end function;
73
74 main();
Something went wrong with that request. Please try again.