Skip to content

Commit 77da7e9

Browse files
committed
[GR-34746] Add SignalCorrelation microbench
PullRequest: graal/10176
2 parents fe9ee5e + 4906cfe commit 77da7e9

File tree

1 file changed

+136
-0
lines changed

1 file changed

+136
-0
lines changed
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
/*
2+
* Copyright (c) 2021, 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. Oracle designates this
8+
* particular file as subject to the "Classpath" exception as provided
9+
* by Oracle in the LICENSE file that accompanied this code.
10+
*
11+
* This code is distributed in the hope that it will be useful, but WITHOUT
12+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14+
* version 2 for more details (a copy is included in the LICENSE file that
15+
* accompanied this code).
16+
*
17+
* You should have received a copy of the GNU General Public License version
18+
* 2 along with this work; if not, write to the Free Software Foundation,
19+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20+
*
21+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22+
* or visit www.oracle.com if you need additional information or have any
23+
* questions.
24+
*/
25+
package micro.benchmarks;
26+
27+
import org.openjdk.jmh.annotations.Benchmark;
28+
import org.openjdk.jmh.infra.Blackhole;
29+
30+
public class SignalCorrelationBenchmark {
31+
/** Approximate {@code sin(i / 1000)}. */
32+
public static double sin(int i) {
33+
double x = i / 1000.0;
34+
double term3 = (x * x * x) / (3 * 2 * 1);
35+
double term5 = (x * x * x * x * x) / (5 * 4 * 3 * 2 * 1);
36+
return x - term3 + term5;
37+
}
38+
39+
public static double[] sinTable(int n) {
40+
double[] table = new double[n];
41+
for (int i = 0; i < n; i++) {
42+
table[i] = sin(i);
43+
}
44+
return table;
45+
}
46+
47+
/** Approximate {@code cos(i / 1000)}. */
48+
public static double cos(int i) {
49+
double x = i / 1000.0;
50+
double term2 = (x * x) / (2 * 1);
51+
double term4 = (x * x * x * x) / (4 * 3 * 2 * 1);
52+
return 1 - term2 + term4;
53+
}
54+
55+
public static double[] cosTable(int n) {
56+
double[] table = new double[n];
57+
for (int i = 0; i < n; i++) {
58+
table[i] = cos(i);
59+
}
60+
return table;
61+
}
62+
63+
public static double correlate(double[] a, double[] b) {
64+
double correlation = 0.0;
65+
for (int i = 0; i < a.length; i++) {
66+
correlation += a[i] * b[i];
67+
}
68+
return correlation;
69+
}
70+
71+
public static double correlateUnrolled(double[] a, double[] b) {
72+
if (a.length != b.length) {
73+
throw new IllegalArgumentException();
74+
}
75+
double[] partial = new double[4];
76+
for (int i = 0; i + 3 < a.length; i += 4) {
77+
partial[0] += a[i + 0] * b[i + 0];
78+
partial[1] += a[i + 1] * b[i + 1];
79+
partial[2] += a[i + 2] * b[i + 2];
80+
partial[3] += a[i + 3] * b[i + 3];
81+
}
82+
for (int i = a.length & ~0b11; i < a.length; ++i) {
83+
partial[0] += a[i] * b[i];
84+
}
85+
double correlation = partial[0] + partial[1] + partial[2] + partial[3];
86+
return correlation;
87+
}
88+
89+
@Benchmark
90+
public void correlateBench(Blackhole bh) {
91+
int n = 10_000_000;
92+
for (int j = 0; j < 10; j++) {
93+
double[] sin = sinTable(n);
94+
double[] cos = cosTable(n);
95+
96+
double sinSin = correlate(sin, sin);
97+
double sinCos = correlate(sin, cos);
98+
double cosSin = correlate(cos, sin);
99+
double cosCos = correlate(cos, cos);
100+
101+
double sinSinNormalized = sinSin / sinSin;
102+
double sinCosNormalized = sinCos / Math.sqrt(sinSin * cosCos);
103+
double cosSinNormalized = cosSin / Math.sqrt(cosCos * sinSin);
104+
double cosCosNormalized = cosCos / cosCos;
105+
106+
bh.consume(sinSinNormalized);
107+
bh.consume(sinCosNormalized);
108+
bh.consume(cosSinNormalized);
109+
bh.consume(cosCosNormalized);
110+
}
111+
}
112+
113+
@Benchmark
114+
public void unrolledCorrelateBench(Blackhole bh) {
115+
int n = 10_000_000;
116+
for (int j = 0; j < 10; j++) {
117+
double[] sin = sinTable(n);
118+
double[] cos = cosTable(n);
119+
120+
double sinSin = correlateUnrolled(sin, sin);
121+
double sinCos = correlateUnrolled(sin, cos);
122+
double cosSin = correlateUnrolled(cos, sin);
123+
double cosCos = correlateUnrolled(cos, cos);
124+
125+
double sinSinNormalized = sinSin / sinSin;
126+
double sinCosNormalized = sinCos / Math.sqrt(sinSin * cosCos);
127+
double cosSinNormalized = cosSin / Math.sqrt(cosCos * sinSin);
128+
double cosCosNormalized = cosCos / cosCos;
129+
130+
bh.consume(sinSinNormalized);
131+
bh.consume(sinCosNormalized);
132+
bh.consume(cosSinNormalized);
133+
bh.consume(cosCosNormalized);
134+
}
135+
}
136+
}

0 commit comments

Comments
 (0)