-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkernel.java
More file actions
292 lines (226 loc) · 5.62 KB
/
Copy pathkernel.java
File metadata and controls
292 lines (226 loc) · 5.62 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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
package jnt.scimark2;
public class kernel
{
// each measurement returns approx Mflops
public static double measureFFT(int N, double mintime, Random R)
{
// initialize FFT data as complex (N real/img pairs)
double x[] = RandomVector(2*N, R);
double oldx[] = NewVectorCopy(x);
long cycles = 1;
Stopwatch Q = new Stopwatch();
while(true)
{
Q.start();
for (int i=0; i<cycles; i++)
{
FFT.transform(x); // forward transform
FFT.inverse(x); // backward transform
}
Q.stop();
if (Q.read() >= mintime)
break;
cycles *= 2;
}
// approx Mflops
final double EPS = 1.0e-10;
if ( FFT.test(x) / N > EPS )
return 0.0;
return FFT.num_flops(N)*cycles/ Q.read() * 1.0e-6;
}
public static double measureSOR(int N, double min_time, Random R)
{
double G[][] = RandomMatrix(N, N, R);
Stopwatch Q = new Stopwatch();
int cycles=1;
while(true)
{
Q.start();
SOR.execute(1.25, G, cycles);
Q.stop();
if (Q.read() >= min_time) break;
cycles *= 2;
}
// approx Mflops
return SOR.num_flops(N, N, cycles) / Q.read() * 1.0e-6;
}
public static double measureMonteCarlo(double min_time, Random R)
{
Stopwatch Q = new Stopwatch();
int cycles=1;
while(true)
{
Q.start();
MonteCarlo.integrate(cycles);
Q.stop();
if (Q.read() >= min_time) break;
cycles *= 2;
}
// approx Mflops
return MonteCarlo.num_flops(cycles) / Q.read() * 1.0e-6;
}
public static double measureSparseMatmult(int N, int nz,
double min_time, Random R)
{
// initialize vector multipliers and storage for result
// y = A*y;
double x[] = RandomVector(N, R);
double y[] = new double[N];
// initialize square sparse matrix
//
// for this test, we create a sparse matrix wit M/nz nonzeros
// per row, with spaced-out evenly between the begining of the
// row to the main diagonal. Thus, the resulting pattern looks
// like
// +-----------------+
// +* +
// +*** +
// +* * * +
// +** * * +
// +** * * +
// +* * * * +
// +* * * * +
// +* * * * +
// +-----------------+
//
// (as best reproducible with integer artihmetic)
// Note that the first nr rows will have elements past
// the diagonal.
int nr = nz/N; // average number of nonzeros per row
int anz = nr *N; // _actual_ number of nonzeros
double val[] = RandomVector(anz, R);
int col[] = new int[anz];
int row[] = new int[N+1];
row[0] = 0;
for (int r=0; r<N; r++)
{
// initialize elements for row r
int rowr = row[r];
row[r+1] = rowr + nr;
int step = r/ nr;
if (step < 1) step = 1; // take at least unit steps
for (int i=0; i<nr; i++)
col[rowr+i] = i*step;
}
Stopwatch Q = new Stopwatch();
int cycles=1;
while(true)
{
Q.start();
SparseCompRow.matmult(y, val, row, col, x, cycles);
Q.stop();
if (Q.read() >= min_time) break;
cycles *= 2;
}
// approx Mflops
return SparseCompRow.num_flops(N, nz, cycles) / Q.read() * 1.0e-6;
}
public static double measureLU(int N, double min_time, Random R)
{
// compute approx Mlfops, or O if LU yields large errors
double A[][] = RandomMatrix(N, N, R);
double lu[][] = new double[N][N];
int pivot[] = new int[N];
Stopwatch Q = new Stopwatch();
int cycles=1;
while(true)
{
Q.start();
for (int i=0; i<cycles; i++)
{
CopyMatrix(lu, A);
LU.factor(lu, pivot);
}
Q.stop();
if (Q.read() >= min_time) break;
cycles *= 2;
}
// verify that LU is correct
double b[] = RandomVector(N, R);
double x[] = NewVectorCopy(b);
LU.solve(lu, pivot, x);
final double EPS = 1.0e-12;
if ( normabs(b, matvec(A,x)) / N > EPS )
return 0.0;
// else return approx Mflops
//
return LU.num_flops(N) * cycles / Q.read() * 1.0e-6;
}
private static double[] NewVectorCopy(double x[])
{
int N = x.length;
double y[] = new double[N];
for (int i=0; i<N; i++)
y[i] = x[i];
return y;
}
private static void CopyVector(double B[], double A[])
{
int N = A.length;
for (int i=0; i<N; i++)
B[i] = A[i];
}
private static double normabs(double x[], double y[])
{
int N = x.length;
double sum = 0.0;
for (int i=0; i<N; i++)
sum += Math.abs(x[i]-y[i]);
return sum;
}
private static void CopyMatrix(double B[][], double A[][])
{
int M = A.length;
int N = A[0].length;
int remainder = N & 3; // N mod 4;
for (int i=0; i<M; i++)
{
double Bi[] = B[i];
double Ai[] = A[i];
for (int j=0; j<remainder; j++)
Bi[j] = Ai[j];
for (int j=remainder; j<N; j+=4)
{
Bi[j] = Ai[j];
Bi[j+1] = Ai[j+1];
Bi[j+2] = Ai[j+2];
Bi[j+3] = Ai[j+3];
}
}
}
private static double[][] RandomMatrix(int M, int N, Random R)
{
double A[][] = new double[M][N];
for (int i=0; i<N; i++)
for (int j=0; j<N; j++)
A[i][j] = R.nextDouble();
return A;
}
private static double[] RandomVector(int N, Random R)
{
double A[] = new double[N];
for (int i=0; i<N; i++)
A[i] = R.nextDouble();
return A;
}
private static double[] matvec(double A[][], double x[])
{
int N = x.length;
double y[] = new double[N];
matvec(A, x, y);
return y;
}
private static void matvec(double A[][], double x[], double y[])
{
int M = A.length;
int N = A[0].length;
for (int i=0; i<M; i++)
{
double sum = 0.0;
double Ai[] = A[i];
for (int j=0; j<N; j++)
sum += Ai[j] * x[j];
y[i] = sum;
}
}
}