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 org.openjdk.jmh.annotations.Benchmark;
 26 import org.openjdk.jmh.annotations.CompilerControl;
 27 import org.openjdk.jmh.annotations.Setup;
 28 
 29 import java.util.concurrent.ThreadLocalRandom;
 30 
 31 public class Primitive extends MatrixBase {
 32 
 33     public static class PrimState extends SizeState {
 34         double[][] A;
 35         double[][] B;
 36 
 37         @Setup
 38         public void setup() {
 39             A = populate(new double[size][size * 2]);
 40             B = populate(new double[size][size * 2]);
 41         }
 42 
 43         private double[][] populate(double[][] m) {
 44             int size = m.length;
 45             for (int i = 0; i < size; i++) {
 46                 for (int j = 0; j < size; j++) {
 47                     m[i][j * 2] = ThreadLocalRandom.current().nextDouble();
 48                     m[i][j * 2 + 1] = ThreadLocalRandom.current().nextDouble();
 49                 }
 50             }
 51             return m;
 52         }
 53 
 54     }
 55 
 56     @Benchmark
 57     @CompilerControl(CompilerControl.Mode.DONT_INLINE)
 58     public double[][] multiply(PrimState st) {
 59         double[][] A = st.A;
 60         double[][] B = st.B;
 61         int size = st.size;
 62         double[][] R = new double[size][size * 2];
 63         for (int i = 0; i < size; i++) {
 64             for (int j = 0; j < size; j++) {
 65                 double s_re = 0;
 66                 double s_im = 0;
 67                 for (int k = 0; k < size; k++) {
 68                     double are = A[i][k * 2];
 69                     double aim = A[i][k * 2 + 1];
 70                     double bre = B[k][j * 2];
 71                     double bim = B[k][j * 2 + 1];
 72                     s_re += are * bre - aim * bim;
 73                     s_im += are * bim + bre * aim;
 74                 }
 75                 R[i][j * 2] = s_re;
 76                 R[i][j * 2 + 1] = s_im;
 77             }
 78         }
 79         return R;
 80     }
 81 
 82 //    @Benchmark
 83 //    public double[][] multiplyCacheFriendly() {
 84 //        int size = A.length;
 85 //        double[][] R = new double[size][size * 2];
 86 //        for (int i = 0; i < size; i++) {
 87 //            for (int k = 0; k < size; k++) {
 88 //                double are = A[i][k * 2 + 0];
 89 //                double aim = A[i][k * 2 + 1];
 90 //                for (int j = 0; j < size; j++) {
 91 //                    double bre = B[k][j * 2 + 0];
 92 //                    double bim = B[k][j * 2 + 1];
 93 //                    R[i][j * 2 + 0] += are * bre - aim * bim;
 94 //                    R[i][j * 2 + 1] += are * bim + bre * aim;
 95 //                }
 96 //            }
 97 //        }
 98 //        return R;
 99 //    }
100 
101 
102 }