This repository was archived by the owner on Nov 10, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 106
Expand file tree
/
Copy pathLinkedSparseMatrixTest.java
More file actions
218 lines (195 loc) · 8.26 KB
/
LinkedSparseMatrixTest.java
File metadata and controls
218 lines (195 loc) · 8.26 KB
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
package no.uib.cipr.matrix.sparse;
import au.com.bytecode.opencsv.CSVWriter;
import com.google.common.base.Stopwatch;
import com.google.common.collect.Lists;
import lombok.Cleanup;
import lombok.extern.java.Log;
import no.uib.cipr.matrix.DenseMatrix;
import no.uib.cipr.matrix.Matrix;
import no.uib.cipr.matrix.MatrixEntry;
import no.uib.cipr.matrix.Utilities;
import org.junit.Test;
import java.io.File;
import java.io.FileWriter;
import java.util.concurrent.TimeUnit;
import static org.junit.Assert.assertEquals;
/**
* @author Sam Halliday
*/
@Log
public class LinkedSparseMatrixTest extends SparseMatrixTestAbstract {
@Override
protected void createPrimary() throws Exception {
int n = Utilities.getInt(1, max);
int m = Utilities.getInt(1, max);
int b = Utilities.getInt(Math.min(bmax, m));
int[][] nz = Utilities.getRowPattern(n, m, b);
A = new LinkedSparseMatrix(n, m);
Ad = Utilities.rowPopulate(A, nz);
for (MatrixEntry e : A) {
int row = e.row();
int col = e.column();
double expect = Ad[row][col];
assertEquals(expect, e.get(), 0);
assertEquals(expect, A.get(row, col), 0);
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
double expect = Ad[i][j];
assertEquals(expect, A.get(i, j), 0);
}
}
LinkedSparseMatrix.Node head = ((LinkedSparseMatrix) A).links.head;
LinkedSparseMatrix.Node node = head;
while (node != null) {
// log.info(node.toString());
assertEquals(Ad[node.row][node.col], node.val, 0.0);
node = node.rowTail;
}
node = head;
while (node != null) {
// log.info(node.toString());
assertEquals(Ad[node.row][node.col], node.val, 0.0);
node = node.colTail;
}
}
public void ignoredTimedMult() {
Stopwatch watch = Stopwatch.createUnstarted();
DenseMatrix dense = new DenseMatrix(1000, 1000);
int[][] nz = Utilities.getRowPattern(dense.numRows(),
dense.numColumns(), 100);
Utilities.rowPopulate(dense, nz);
log.info("created matrices");
Matrix sparse = new LinkedSparseMatrix(dense.numRows(),
dense.numColumns());
sparse.set(dense);
for (Matrix m : Lists.newArrayList(dense, sparse)) {
log.info("starting " + m.getClass());
Matrix t = new DenseMatrix(m);
t.transpose();
Matrix o = new DenseMatrix(dense.numRows(), dense.numColumns());
log.info("warming up " + m.getClass() + " " + o.getClass());
for (int i = 0; i < 10; i++)
m.mult(t, o);
log.info("starting " + m.getClass() + " " + o.getClass());
watch.start();
for (int i = 0; i < 100; i++)
m.mult(t, o);
watch.stop();
log.info(m.getClass() + " " + o.getClass() + " " + watch);
}
}
public void ignoredTimedTransMult() {
Stopwatch watch = Stopwatch.createUnstarted();
DenseMatrix dense = new DenseMatrix(1000, 1000);
int[][] nz = Utilities.getRowPattern(dense.numRows(),
dense.numColumns(), 100);
Utilities.rowPopulate(dense, nz);
log.info("created matrices");
Matrix sparse = new LinkedSparseMatrix(dense.numRows(),
dense.numColumns());
sparse.set(dense);
for (Matrix m : Lists.newArrayList(dense, sparse)) {
log.info("starting " + m.getClass());
Matrix t = new DenseMatrix(m);
Matrix o = new DenseMatrix(dense.numRows(), dense.numColumns());
log.info("warming up " + m.getClass() + " " + o.getClass());
for (int i = 0; i < 10; i++)
m.transAmult(t, o);
log.info("starting " + m.getClass() + " " + o.getClass());
watch.start();
for (int i = 0; i < 100; i++)
m.transAmult(t, o);
watch.stop();
log.info(m.getClass() + " " + o.getClass() + " " + watch);
}
}
@Test
@Override
public void testIteratorSet() {
}
@Test
@Override
public void testIteratorSetGet() {
}
/**
* Does a naive perf test against DenseMatrix, outputting CSV that we plot
* in R.
* <p/>
* Generate n x n matrix with m entries, on left, and an n x n dense matrix
* with m entries on the right, using the same population algo. Then we
* multiply them and output into a dense matrix. We compare dense vs linked
* sparse from the left, and also look at memory usage. We repeat for 10
* different values of m (10,000 to 100,000), and in both cases vary m from
* 1,000 to 10,000. This requires about 8GB heap to be on the safe side.
* <p/>
* -Xms8g -Xmx8g -Djava.util.logging.config.file=logging.properties
*
* @param args
*/
public static void main(String[] args) throws Exception {
File file = new File("LinkedSparseMatrixPerf.csv");
log.info("writing to " + file);
@Cleanup
CSVWriter csv = new CSVWriter(new FileWriter(file));
for (int r = 0; r < 10; r++) {
for (int m = 10000; m <= 100000; m = m + 10000) {
for (int n = 1000; n <= 10000; n = n + 1000) {
int[][] patternA = Utilities.getRowPattern(n, n, m / n);
DenseMatrix origA = new DenseMatrix(n, n);
Utilities.rowPopulate(origA, patternA);
int[][] patternB = Utilities.getRowPattern(n, n, m / n);
DenseMatrix origB = new DenseMatrix(n, n);
Utilities.rowPopulate(origB, patternB);
// to be fair, we reuse the same matrix values
long denseMem, denseInitTime, denseMultTime, sparseMem, sparseInitTime, sparseMultTime;
Stopwatch timer = Stopwatch.createUnstarted();
{
timer.reset();
timer.start();
DenseMatrix A = new DenseMatrix(origA);
timer.stop();
// all attempts to measure memory usage failed
denseMem = n * n * 8;
denseInitTime = timer.elapsed(TimeUnit.NANOSECONDS);
timer.reset();
DenseMatrix B = origB.copy();
DenseMatrix C = new DenseMatrix(n, n);
timer.start();
A.mult(B, C);
timer.stop();
denseMultTime = timer.elapsed(TimeUnit.NANOSECONDS);
}
{
timer.reset();
timer.start();
LinkedSparseMatrix A = new LinkedSparseMatrix(origA);
timer.stop();
// using compressedooms
sparseMem = m * 28 + 16 * n;
sparseInitTime = timer.elapsed(TimeUnit.NANOSECONDS);
timer.reset();
DenseMatrix B = origB.copy();
DenseMatrix C = new DenseMatrix(n, n);
timer.start();
A.mult(B, C);
timer.stop();
sparseMultTime = timer.elapsed(TimeUnit.NANOSECONDS);
}
String[] line = new String[]{Integer.toString(n),
Integer.toString(m), Long.toString(denseMem),
Long.toString(denseInitTime),
Long.toString(denseMultTime),
Long.toString(sparseMem),
Long.toString(sparseInitTime),
Long.toString(sparseMultTime)};
log.info(java.util.Arrays.toString(line));
csv.writeNext(line);
// these are to keep lots of refs above alive from GC
log.finest(origA.numRows() + " " + origB.numColumns() + " "
+ patternA.length + " " + patternB.length);
}
}
}
}
}