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