Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/stress/numeric/numeric005.java
41159 views
1
/*
2
* Copyright (c) 1999, 2020, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation.
8
*
9
* This code is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12
* version 2 for more details (a copy is included in the LICENSE file that
13
* accompanied this code).
14
*
15
* You should have received a copy of the GNU General Public License version
16
* 2 along with this work; if not, write to the Free Software Foundation,
17
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18
*
19
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
21
* questions.
22
*/
23
24
/*
25
* @test
26
* @key stress randomness
27
*
28
* @summary converted from VM testbase nsk/stress/numeric/numeric005.
29
* VM testbase keywords: [stress, slow, nonconcurrent, quick]
30
* VM testbase readme:
31
* DESCRIPTION
32
* This test calculates the product A*A for a square matrix A of the type
33
* double[][]. Elements of the matrix A are initiated with random numbers,
34
* so that optimizing compiler could not eliminate any essential portion
35
* of calculations.
36
* Calculation of the product A*A is iterated three times, and result of
37
* the 1st iteration is compared to result of the 3rd iteration. HotSpot
38
* releases 1.0 and 1.3 seem to fail to adjust itself for better performance
39
* in 1st iteration, while 3rd iteration usually runs much faster. So, the
40
* 1st iteration is probably executed by HotSpot interpreter, and HotSpot
41
* compiler is probably involved to execute the 3rd iteration. The test
42
* just tries to check if HotSpot compiler produces the same results as the
43
* HotSpot interpreter.
44
* By the way, the test checks JVM performance. The test is treated failed
45
* due to poor performance, if 1st iteration is essentially slower than the
46
* 3rd iteration. The calculations algorithm is encoded as compact 3-levels
47
* cycle like:
48
* for (int line=0; line<N; line++)
49
* for (int column=0; column<N; column++) {
50
* double sum = 0;
51
* for (int k=0; k<N; k++)
52
* sum += A[line][k] * A[k][column];
53
* AA[line][column] = sum;
54
* }
55
* In this test, N=300, so that A is 300x300 matrix; and multiplication
56
* A[line][k]*A[k][column] is executed 300**3=27 millions times in each
57
* execution of this cycle. I believe, that this is HotSpot bug to do not
58
* adjust itself for best performance during such a huge series of executions
59
* of the same portion of program code.
60
* COMMENTS
61
* See the bug-report:
62
* #4242172 (P3/S5) 2.0: poor performance in matrix calculations
63
*
64
* @library /test/lib
65
* @run main/othervm nsk.stress.numeric.numeric005.numeric005 300 3
66
*/
67
68
package nsk.stress.numeric.numeric005;
69
70
import java.io.PrintStream;
71
import java.util.Random;
72
import jdk.test.lib.Utils;
73
74
/**
75
* This test calculates the product <code>A<sup>.</sup>A</code> for
76
* a square matrix <code>A</code> of the type <code>double[][]</code>.
77
* Elements of the matrix <code>A</code> are initiated with random numbers,
78
* so that optimizing compiler could not eliminate any essential portion
79
* of calculations.
80
* <p>
81
* <p>Calculation of the product <code>A<sup>.</sup>A</code> is iterated three
82
* times, and result of the 1<sup>st</sup> iteration is compared to result of
83
* the 3<sup>rd</sup> iteration. HotSpot 1.0 and 1.3 seem to fail to adjust
84
* itself for better performance in 1<sup>st</sup> iteration, while 3<sup>rd</sup>
85
* iteration usually runs much faster. So, 1<sup>st</sup> iteration is probably
86
* executed by HotSpot interpreter, and HotSpot compiler is probably involved to
87
* execute the 3<sup>rd</sup> iteration. The test just tries to check if HotSpot
88
* compiler produces the same results as the HotSpot interpreter.
89
* <p>
90
* <p>By the way, the test checks JVM performance. The test is treated failed
91
* due to poor performance, if 1<sup>st</sup> iteration is essentially slower
92
* than the 3<sup>rd</sup> iteration. The calculations algorithm is encoded
93
* as compact ``canonical'' 3-levels cycle like:
94
* <pre>
95
* for (int line=0; line&lt;N; line++)
96
* for (int column=0; column&lt;N; column++) {
97
* double sum = 0;
98
* for (int k=0; k&lt;N; k++)
99
* sum += A[line][k] * A[k][column];
100
* AA[line][column] = sum;
101
* }
102
* </pre>
103
* <p>
104
* In this test, <code>N</code>=300, so that <code>A</code> is 300x300 matrix;
105
* and multiplication <code>A[line][k]*A[k][column]</code> is executed
106
* 300<sup>3</sup>=27 millions times in each iteration of execution of this
107
* cycle. I believe, that this is HotSpot bug to do not adjust itself for best
108
* performance during such a huge series of executions of the same portion of
109
* program code.
110
* <p>
111
* <p>See the bug-report:
112
* <br>&nbsp;&nbsp;
113
* #4242172 (P3/S5) 2.0: poor performance in matrix calculations
114
*/
115
public class numeric005 {
116
private static final Random RNG = Utils.getRandomInstance();
117
/**
118
* When testing performance, single thread calculation is allowed to
119
* be 10% slower than multi-threads calculation (<code>TOLERANCE</code>
120
* is assigned to 10 now).
121
*/
122
public static final double TOLERANCE = 100; // 10;
123
124
/**
125
* Re-assign this value to <code>true</code> for better
126
* diagnostics.
127
*
128
* @see #print(Object)
129
* @see #println(Object)
130
*/
131
private static boolean verbose = false;
132
133
/**
134
* Stream to print execution trace and/or error messages.
135
* This stream usually equals to <code>System.out</code>
136
*/
137
private static PrintStream out = null;
138
139
/**
140
* Print error-message.
141
*
142
* @see #out
143
*/
144
private static void complain(Object x) {
145
out.println("# " + x);
146
}
147
148
/**
149
* Print to execution trace, if mode is <code>verbose</code>.
150
*
151
* @see #verbose
152
* @see #out
153
*/
154
private static void print(Object x) {
155
if (verbose)
156
out.print(x);
157
}
158
159
/**
160
* Print line to execution trace, if mode is <code>verbose</code>.
161
*
162
* @see #verbose
163
* @see #out
164
*/
165
private static void println(Object x) {
166
print(x + "\n");
167
}
168
169
/**
170
* Re-invoke <code>run(args,out)</code> in order to simulate
171
* JCK-like test interface.
172
*/
173
public static void main(String args[]) {
174
int exitCode = run(args, System.out);
175
System.exit(exitCode + 95);
176
// JCK-like exit status
177
}
178
179
/**
180
* Parse command-line parameters stored in <code>args[]</code> and run
181
* the test.
182
* <p>
183
* <p>Command-line parameters are:
184
* <br>&nbsp;&nbsp;
185
* <code>java numeric005 [-verbose] [-performance] <i>matrixSize</i>
186
* <i>iterations</i></code>
187
* <p>
188
* <p>Here:
189
* <br>&nbsp;&nbsp;<code>-verbose</code> -
190
* keyword, which alows to print execution trace
191
* <br>&nbsp;&nbsp;<code>-performance</code> -
192
* keyword, which alows performance testing
193
* <br>&nbsp;&nbsp;<code><i>matrixSize</i></code> -
194
* number of rows (and columns) in square matrix <code>A</code>
195
* <br>&nbsp;&nbsp;<code><i>iterations</i></code> -
196
* compute <code>A*A</code> several times
197
*
198
* @param args strings array containing command-line parameters
199
* @param out the test log, usually <code>System.out</code>
200
*/
201
public static int run(String args[], PrintStream out) {
202
numeric005.out = out;
203
204
boolean testPerformance = false;
205
int numberOfCPU = 1;
206
207
// Parse parameters starting with "-" (like: "-verbose"):
208
209
int argsShift = 0;
210
for (; argsShift < args.length; argsShift++) {
211
String argument = args[argsShift];
212
213
if (!argument.startsWith("-"))
214
break;
215
216
if (argument.equals("-performance")) {
217
testPerformance = true;
218
continue;
219
}
220
221
if (argument.equals("-verbose")) {
222
verbose = true;
223
continue;
224
}
225
226
complain("Cannot recognize argument: args[" + argsShift + "]: " + argument);
227
return 2; // failure
228
}
229
230
if (args.length != argsShift + 2) {
231
complain("Illegal arguments. Execute:");
232
complain(
233
" java numeric005 [-verbose] [-performance] [-CPU:number] " +
234
"matrixSize iterations");
235
return 2; // failure
236
}
237
238
int size = Integer.parseInt(args[argsShift]);
239
if ((size < 100) || (size > 10000)) {
240
complain("Matrix size should be 100 to 1000 lines & columns.");
241
return 2; // failure
242
}
243
244
int iterations = Integer.parseInt(args[argsShift + 1]);
245
if ((iterations < 1) || (iterations > 100)) {
246
complain("Iterations number should be 1 to 100.");
247
return 2; // failure
248
}
249
250
print("Preparing A[" + size + "," + size + "]:");
251
double[][] A = newMatrix(size);
252
double[][] A1 = new double[size][size];
253
double[][] Ai = new double[size][size];
254
println(" done.");
255
256
println("Should try " + iterations + " iteration(s):");
257
println("==========================" +
258
((iterations > 99) ? "==" : (iterations > 9) ? "=" : ""));
259
println("");
260
261
double overallTime = 0;
262
double firstTime = 0;
263
double lastTime = 0;
264
265
for (int i = 1; i <= iterations; i++) {
266
double seconds;
267
268
if (i == 1) {
269
seconds = elapsedTime(i, A, A1);
270
firstTime = seconds;
271
} else {
272
seconds = elapsedTime(i, A, Ai);
273
lastTime = seconds;
274
}
275
276
overallTime += seconds;
277
}
278
279
double averageTime = overallTime / iterations;
280
double averagePerformance = size * size * (size + size) / averageTime / 1e6;
281
282
println("");
283
println("=======================" +
284
((iterations > 99) ? "==" : (iterations > 9) ? "=" : ""));
285
println("Overall iteration(s): " + iterations);
286
println("Overall elapsed time: " + overallTime + " seconds.");
287
println("Average elapsed time: " + averageTime + " seconds.");
288
println("Average performance: " + averagePerformance + " MFLOPS");
289
290
println("========================");
291
print("Checking accuracy:");
292
for (int line = 0; line < size; line++)
293
for (int column = 0; column < size; column++)
294
if (A1[line][column] != Ai[line][column]) {
295
println("");
296
complain("Test failed:");
297
complain("Different results in 1st and last iterations:");
298
complain(" line=" + line + ", column=" + column);
299
return 2; // FAILED
300
}
301
println(" done.");
302
303
if (testPerformance) {
304
print("Checking performance: ");
305
if (firstTime > lastTime * (1 + TOLERANCE / 100)) {
306
println("");
307
complain("Test failed:");
308
complain("1st iterartion is essentially slower:");
309
complain("Calculation time elapsed (seconds):");
310
complain(" 1-st iteration: " + firstTime);
311
complain(" last iteration: " + lastTime);
312
complain(" tolerance: " + TOLERANCE + "%");
313
return 2; // FAILED
314
}
315
println("done.");
316
}
317
318
println("Test passed.");
319
return 0; // PASSED
320
}
321
322
private static double elapsedTime(int i, double[][] A, double[][] AA) {
323
int size = A.length;
324
325
if (i > 1)
326
println("");
327
println("Iteration #" + i + ":");
328
329
print("Computing A*A:");
330
long mark1 = System.currentTimeMillis();
331
setSquare(A, AA);
332
long mark2 = System.currentTimeMillis();
333
println(" done.");
334
335
double sec = (mark2 - mark1) / 1000.0;
336
double perf = size * size * (size + size) / sec;
337
println("Elapsed time: " + sec + " seconds");
338
println("Performance: " + perf / 1e6 + " MFLOPS");
339
340
return sec;
341
}
342
343
/**
344
* Compute <code>A*A</code> for the given square matrix <code>A</code>.
345
*/
346
private static void setSquare(double[][] A, double[][] AA) {
347
if (A.length != A[0].length)
348
throw new IllegalArgumentException(
349
"the argument matrix A should be square matrix");
350
if (AA.length != AA[0].length)
351
throw new IllegalArgumentException(
352
"the resulting matrix AA should be square matrix");
353
if (A.length != AA.length)
354
throw new IllegalArgumentException(
355
"the matrices A and AA should have equal size");
356
357
int size = A.length;
358
359
for (int line = 0; line < size; line++)
360
for (int column = 0; column < size; column++) {
361
double sum = 0;
362
for (int k = 0; k < size; k++)
363
sum += A[line][k] * A[k][line];
364
AA[line][column] = sum;
365
}
366
}
367
368
/**
369
* Generate new square matrix of the given <code>size</code>
370
* and with elements initiated with random numbers.
371
*/
372
private static double[][] newMatrix(int size) {
373
if ((size < 1) || (size > 1000))
374
throw new IllegalArgumentException(
375
"matrix size should be 1 to 1000");
376
377
double[][] A = new double[size][size];
378
379
for (int line = 0; line < size; line++)
380
for (int column = 0; column < size; column++)
381
A[line][column] = (1 - 2 * RNG.nextDouble()) * size;
382
383
return A;
384
}
385
386
}
387
388