1 /* 2 * Copyright (c) 2025, 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 package org.openjdk.bench.valhalla.matrix; 24 25 import jdk.internal.value.ValueClass; 26 import org.openjdk.jmh.annotations.Benchmark; 27 import org.openjdk.jmh.annotations.CompilerControl; 28 import org.openjdk.jmh.annotations.Fork; 29 import org.openjdk.jmh.annotations.Setup; 30 31 import java.util.concurrent.ThreadLocalRandom; 32 33 34 @Fork(value = 3, jvmArgsAppend = {"--enable-preview", "--add-exports", "java.base/jdk.internal.value=ALL-UNNAMED"}) 35 public class ValueNullFree extends MatrixBase { 36 37 public static ValueComplex[][] create_matrix_val(int size) { 38 ValueComplex[][] x; 39 x = new ValueComplex[size][]; 40 for (int i = 0; i < size; i++) { 41 x[i] = (ValueComplex[]) ValueClass.newNullRestrictedAtomicArray(ValueComplex.class, size, new ValueComplex(0, 0)); 42 } 43 return x; 44 } 45 46 public static Complex[][] create_matrix_int(int size) { 47 return new Complex[size][size]; 48 } 49 50 public static abstract class ValState extends SizeState { 51 ValueComplex[][] A; 52 ValueComplex[][] B; 53 54 static void populate(ValueComplex[][] m) { 55 int size = m.length; 56 for (int i = 0; i < size; i++) { 57 for (int j = 0; j < size; j++) { 58 m[i][j] = new ValueComplex(ThreadLocalRandom.current().nextDouble(), ThreadLocalRandom.current().nextDouble()); 59 } 60 } 61 } 62 } 63 64 public static abstract class IntState extends SizeState { 65 Complex[][] A; 66 Complex[][] B; 67 68 static void populate(Complex[][] m) { 69 int size = m.length; 70 for (int i = 0; i < size; i++) { 71 for (int j = 0; j < size; j++) { 72 m[i][j] = new ValueComplex(ThreadLocalRandom.current().nextDouble(), ThreadLocalRandom.current().nextDouble()); 73 } 74 } 75 } 76 } 77 78 public static class Val_as_Val extends ValState { 79 @Setup 80 public void setup() { 81 populate(A = create_matrix_val(size)); 82 populate(B = create_matrix_val(size)); 83 } 84 } 85 86 public static class Val_as_Int extends IntState { 87 @Setup 88 public void setup() { 89 populate(A = create_matrix_val(size)); 90 populate(B = create_matrix_val(size)); 91 } 92 } 93 94 public static class Int_as_Int extends IntState { 95 @Setup 96 public void setup() { 97 populate(A = create_matrix_int(size)); 98 populate(B = create_matrix_int(size)); 99 } 100 } 101 102 @Benchmark 103 @CompilerControl(CompilerControl.Mode.DONT_INLINE) 104 public ValueComplex[][] mult_val_as_val(Val_as_Val st) { 105 ValueComplex[][] A = st.A; 106 ValueComplex[][] B = st.B; 107 int size = st.size; 108 ValueComplex[][] R = create_matrix_val(size); 109 for (int i = 0; i < size; i++) { 110 for (int j = 0; j < size; j++) { 111 ValueComplex s = new ValueComplex(0,0); 112 for (int k = 0; k < size; k++) { 113 s = s.add(A[i][k].mul(B[k][j])); 114 } 115 R[i][j] = s; 116 } 117 } 118 return R; 119 } 120 121 @Benchmark 122 @CompilerControl(CompilerControl.Mode.DONT_INLINE) 123 public Complex[][] mult_val_as_int(Val_as_Int st) { 124 Complex[][] A = st.A; 125 Complex[][] B = st.B; 126 int size = st.size; 127 Complex[][] R = create_matrix_val(size); 128 for (int i = 0; i < size; i++) { 129 for (int j = 0; j < size; j++) { 130 Complex s = new ValueComplex(0,0); 131 for (int k = 0; k < size; k++) { 132 s = s.add(A[i][k].mul(B[k][j])); 133 } 134 R[i][j] = s; 135 } 136 } 137 return R; 138 } 139 140 @Benchmark 141 @CompilerControl(CompilerControl.Mode.DONT_INLINE) 142 public Complex[][] mult_int_as_int(Int_as_Int st) { 143 Complex[][] A = st.A; 144 Complex[][] B = st.B; 145 int size = st.size; 146 Complex[][] R = create_matrix_int(size); 147 for (int i = 0; i < size; i++) { 148 for (int j = 0; j < size; j++) { 149 Complex s = new ValueComplex(0,0); 150 for (int k = 0; k < size; k++) { 151 s = s.add(A[i][k].mul(B[k][j])); 152 } 153 R[i][j] = s; 154 } 155 } 156 return R; 157 } 158 159 public interface Complex { 160 double re(); 161 double im(); 162 Complex add(Complex that); 163 Complex mul(Complex that); 164 } 165 166 public static value class ValueComplex implements Complex { 167 168 private final double re; 169 private final double im; 170 171 public ValueComplex(double re, double im) { 172 this.re = re; 173 this.im = im; 174 } 175 176 @Override 177 public double re() { return re; } 178 179 @Override 180 public double im() { return im; } 181 182 @Override 183 public ValueComplex add(Complex that) { 184 return new ValueComplex(this.re + that.re(), this.im + that.im()); 185 } 186 187 public ValueComplex add(ValueComplex that) { 188 return new ValueComplex(this.re + that.re, this.im + that.im); 189 } 190 191 @Override 192 public ValueComplex mul(Complex that) { 193 return new ValueComplex(this.re * that.re() - this.im * that.im(), 194 this.re * that.im() + this.im * that.re()); 195 } 196 197 public ValueComplex mul(ValueComplex that) { 198 return new ValueComplex(this.re * that.re - this.im * that.im, 199 this.re * that.im + this.im * that.re); 200 } 201 202 } 203 204 }