Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/stress/numeric/numeric007.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/numeric007.
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
* long[][]. 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
* long 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
* @library /test/lib
64
* @run main/othervm nsk.stress.numeric.numeric007.numeric007 300 3
65
*/
66
67
package nsk.stress.numeric.numeric007;
68
69
import java.io.PrintStream;
70
import java.util.Random;
71
import jdk.test.lib.Utils;
72
73
/**
74
* This test calculates the product <code>A<sup>.</sup>A</code> for
75
* a square matrix <code>A</code> of the type <code>long[][]</code>.
76
* Elements of the matrix <code>A</code> are initiated with random numbers,
77
* so that optimizing compiler could not eliminate any essential portion
78
* of calculations.
79
* <p>
80
* <p>Calculation of the product <code>A<sup>.</sup>A</code> is iterated three
81
* times, and result of the 1<sup>st</sup> iteration is compared to result of
82
* the 3<sup>rd</sup> iteration. HotSpot 1.0 and 1.3 seem to fail to adjust
83
* itself for better performance in 1<sup>st</sup> iteration, while 3<sup>rd</sup>
84
* iteration usually runs much faster. So, 1<sup>st</sup> iteration is probably
85
* executed by HotSpot interpreter, and HotSpot compiler is probably involved to
86
* execute the 3<sup>rd</sup> iteration. The test just tries to check if HotSpot
87
* compiler produces the same results as the HotSpot interpreter.
88
* <p>
89
* <p>By the way, the test checks JVM performance. The test is treated failed
90
* due to poor performance, if 1<sup>st</sup> iteration is essentially slower
91
* than the 3<sup>rd</sup> iteration. The calculations algorithm is encoded
92
* as compact ``canonical'' 3-levels cycle like:
93
* <pre>
94
* for (int line=0; line&lt;N; line++)
95
* for (int column=0; column&lt;N; column++) {
96
* long sum = 0;
97
* for (int k=0; k&lt;N; k++)
98
* sum += A[line][k] * A[k][column];
99
* AA[line][column] = sum;
100
* }
101
* </pre>
102
* <p>
103
* In this test, <code>N</code>=300, so that <code>A</code> is 300x300 matrix;
104
* and multiplication <code>A[line][k]*A[k][column]</code> is executed
105
* 300<sup>3</sup>=27 millions times in each iteration of execution of this
106
* cycle. I believe, that this is HotSpot bug to do not adjust itself for best
107
* performance during such a huge series of executions of the same portion of
108
* program code.
109
* <p>
110
* <p>See the bug-report:
111
* <br>&nbsp;&nbsp;
112
* #4242172 (P3/S5) 2.0: poor performance in matrix calculations
113
*/
114
public class numeric007 {
115
private static final Random RNG = Utils.getRandomInstance();
116
/**
117
* When testing performance, single thread calculation is allowed to
118
* be 10% slower than multi-threads calculation (<code>TOLERANCE</code>
119
* is assigned to 10 now).
120
*/
121
public static final double TOLERANCE = 100; // 10;
122
123
/**
124
* Re-assign this value to <code>true</code> for better
125
* diagnostics.
126
*
127
* @see #print(Object)
128
* @see #println(Object)
129
*/
130
private static boolean verbose = false;
131
132
/**
133
* Stream to print execution trace and/or error messages.
134
* This stream usually equals to <code>System.out</code>
135
*/
136
private static PrintStream out = null;
137
138
/**
139
* Print error-message.
140
*
141
* @see #out
142
*/
143
private static void complain(Object x) {
144
out.println("# " + x);
145
}
146
147
/**
148
* Print to execution trace, if mode is <code>verbose</code>.
149
*
150
* @see #verbose
151
* @see #out
152
*/
153
private static void print(Object x) {
154
if (verbose)
155
out.print(x);
156
}
157
158
/**
159
* Print line to execution trace, if mode is <code>verbose</code>.
160
*
161
* @see #verbose
162
* @see #out
163
*/
164
private static void println(Object x) {
165
print(x + "\n");
166
}
167
168
/**
169
* Re-invoke <code>run(args,out)</code> in order to simulate
170
* JCK-like test interface.
171
*/
172
public static void main(String args[]) {
173
int exitCode = run(args, System.out);
174
System.exit(exitCode + 95);
175
// JCK-like exit status
176
}
177
178
/**
179
* Parse command-line parameters stored in <code>args[]</code> and run
180
* the test.
181
* <p>
182
* <p>Command-line parameters are:
183
* <br>&nbsp;&nbsp;
184
* <code>java numeric007 [-verbose] [-performance] <i>matrixSize</i>
185
* <i>iterations</i></code>
186
* <p>
187
* <p>Here:
188
* <br>&nbsp;&nbsp;<code>-verbose</code> -
189
* keyword, which alows to print execution trace
190
* <br>&nbsp;&nbsp;<code>-performance</code> -
191
* keyword, which alows performance testing
192
* <br>&nbsp;&nbsp;<code><i>matrixSize</i></code> -
193
* number of rows (and columns) in square matrix <code>A</code>
194
* <br>&nbsp;&nbsp;<code><i>iterations</i></code> -
195
* compute <code>A*A</code> several times
196
*
197
* @param args strings array containing command-line parameters
198
* @param out the test log, usually <code>System.out</code>
199
*/
200
public static int run(String args[], PrintStream out) {
201
numeric007.out = out;
202
203
boolean testPerformance = false;
204
int numberOfCPU = 1;
205
206
// Parse parameters starting with "-" (like: "-verbose"):
207
208
int argsShift = 0;
209
for (; argsShift < args.length; argsShift++) {
210
String argument = args[argsShift];
211
212
if (!argument.startsWith("-"))
213
break;
214
215
if (argument.equals("-performance")) {
216
testPerformance = true;
217
continue;
218
}
219
220
if (argument.equals("-verbose")) {
221
verbose = true;
222
continue;
223
}
224
225
complain("Cannot recognize argument: args[" + argsShift + "]: " + argument);
226
return 2; // failure
227
}
228
229
if (args.length != argsShift + 2) {
230
complain("Illegal arguments. Execute:");
231
complain(
232
" java numeric007 [-verbose] [-performance] [-CPU:number] " +
233
"matrixSize iterations");
234
return 2; // failure
235
}
236
237
int size = Integer.parseInt(args[argsShift]);
238
if ((size < 100) || (size > 10000)) {
239
complain("Matrix size should be 100 to 1000 lines & columns.");
240
return 2; // failure
241
}
242
243
int iterations = Integer.parseInt(args[argsShift + 1]);
244
if ((iterations < 1) || (iterations > 100)) {
245
complain("Iterations number should be 1 to 100.");
246
return 2; // failure
247
}
248
249
print("Preparing A[" + size + "," + size + "]:");
250
long[][] A = newMatrix(size);
251
long[][] A1 = new long[size][size];
252
long[][] Ai = new long[size][size];
253
println(" done.");
254
255
println("Should try " + iterations + " iteration(s):");
256
println("==========================" +
257
((iterations > 99) ? "==" : (iterations > 9) ? "=" : ""));
258
println("");
259
260
double overallTime = 0;
261
double firstTime = 0;
262
double lastTime = 0;
263
264
for (int i = 1; i <= iterations; i++) {
265
double seconds;
266
267
if (i == 1) {
268
seconds = elapsedTime(i, A, A1);
269
firstTime = seconds;
270
} else {
271
seconds = elapsedTime(i, A, Ai);
272
lastTime = seconds;
273
}
274
275
overallTime += seconds;
276
}
277
278
double averageTime = overallTime / iterations;
279
double averagePerformance = size * size * (size + size) / averageTime / 1e6;
280
281
println("");
282
println("=======================" +
283
((iterations > 99) ? "==" : (iterations > 9) ? "=" : ""));
284
println("Overall iteration(s): " + iterations);
285
println("Overall elapsed time: " + overallTime + " seconds.");
286
println("Average elapsed time: " + averageTime + " seconds.");
287
println("Average performance: " + averagePerformance + " MFLOPS");
288
289
println("========================");
290
print("Checking accuracy:");
291
for (int line = 0; line < size; line++)
292
for (int column = 0; column < size; column++)
293
if (A1[line][column] != Ai[line][column]) {
294
println("");
295
complain("Test failed:");
296
complain("Different results in 1st and last iterations:");
297
complain(" line=" + line + ", column=" + column);
298
return 2; // FAILED
299
}
300
println(" done.");
301
302
if (testPerformance) {
303
print("Checking performance: ");
304
if (firstTime > lastTime * (1 + TOLERANCE / 100)) {
305
println("");
306
complain("Test failed:");
307
complain("1st iterartion is essentially slower:");
308
complain("Calculation time elapsed (seconds):");
309
complain(" 1-st iteration: " + firstTime);
310
complain(" last iteration: " + lastTime);
311
complain(" tolerance: " + TOLERANCE + "%");
312
return 2; // FAILED
313
}
314
println("done.");
315
}
316
317
println("Test passed.");
318
return 0; // PASSED
319
}
320
321
private static double elapsedTime(int i, long[][] A, long[][] AA) {
322
int size = A.length;
323
324
if (i > 1)
325
println("");
326
println("Iteration #" + i + ":");
327
328
print("Computing A*A:");
329
long mark1 = System.currentTimeMillis();
330
setSquare(A, AA);
331
long mark2 = System.currentTimeMillis();
332
println(" done.");
333
334
double sec = (mark2 - mark1) / 1000.0;
335
double perf = size * size * (size + size) / sec;
336
println("Elapsed time: " + sec + " seconds");
337
println("Performance: " + perf / 1e6 + " MFLOPS");
338
339
return sec;
340
}
341
342
/**
343
* Compute <code>A*A</code> for the given square matrix <code>A</code>.
344
*/
345
private static void setSquare(long[][] A, long[][] AA) {
346
if (A.length != A[0].length)
347
throw new IllegalArgumentException(
348
"the argument matrix A should be square matrix");
349
if (AA.length != AA[0].length)
350
throw new IllegalArgumentException(
351
"the resulting matrix AA should be square matrix");
352
if (A.length != AA.length)
353
throw new IllegalArgumentException(
354
"the matrices A and AA should have equal size");
355
356
int size = A.length;
357
358
for (int line = 0; line < size; line++)
359
for (int column = 0; column < size; column++) {
360
long sum = 0;
361
for (int k = 0; k < size; k++)
362
sum += A[line][k] * A[k][line];
363
AA[line][column] = sum;
364
}
365
}
366
367
/**
368
* Generate new square matrix of the given <code>size</code>
369
* and with elements initiated with random numbers.
370
*/
371
private static long[][] newMatrix(int size) {
372
if ((size < 1) || (size > 1000))
373
throw new IllegalArgumentException(
374
"matrix size should be 1 to 1000");
375
376
long[][] A = new long[size][size];
377
378
for (int line = 0; line < size; line++)
379
for (int column = 0; column < size; column++)
380
A[line][column] = Math.round((1 - 2 * RNG.nextDouble()) * size);
381
382
return A;
383
}
384
385
}
386
387