1 /*
  2  * Copyright (c) 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 package org.openjdk.bench.valhalla.matrix;
 24 
 25 import org.openjdk.bench.valhalla.types.Complex;
 26 import org.openjdk.bench.valhalla.types.RComplex;
 27 import org.openjdk.jmh.annotations.Benchmark;
 28 import org.openjdk.jmh.annotations.CompilerControl;
 29 import org.openjdk.jmh.annotations.Setup;
 30 
 31 import java.util.concurrent.ThreadLocalRandom;
 32 
 33 public abstract class Identity extends Base {
 34 
 35     public static final Complex IZERO = new RComplex(0,0);
 36     public static final RComplex RZERO = new RComplex(0,0);
 37 
 38     private static void populate(Complex[][] m) {
 39         int size = m.length;
 40         for (int i = 0; i < size; i++) {
 41             for (int j = 0; j < size; j++) {
 42                 m[i][j] = new RComplex(ThreadLocalRandom.current().nextDouble(), ThreadLocalRandom.current().nextDouble());
 43             }
 44         }
 45     }
 46 
 47     public static class Ref extends Identity {
 48         RComplex[][] A;
 49         RComplex[][] B;
 50 
 51         @Setup
 52         public void setup() {
 53             A = new RComplex[size][size];
 54             populate(A);
 55             B = new RComplex[size][size];
 56             populate(B);
 57         }
 58 
 59         @Benchmark
 60         @CompilerControl(CompilerControl.Mode.DONT_INLINE)
 61         public RComplex[][] multiply() {
 62             int size = A.length;
 63             RComplex[][] R = new RComplex[size][size];
 64             for (int i = 0; i < size; i++) {
 65                 for (int j = 0; j < size; j++) {
 66                     RComplex s = RZERO;
 67                     for (int k = 0; k < size; k++) {
 68                         s = s.add(A[i][k].mul(B[k][j]));
 69                     }
 70                     R[i][j] = s;
 71                 }
 72             }
 73             return R;
 74         }
 75     }
 76 
 77     public static class Int extends Identity {
 78         Complex[][] A;
 79         Complex[][] B;
 80 
 81         @Setup
 82         public void setup() {
 83             A = new Complex[size][size];
 84             populate(A);
 85             B = new Complex[size][size];
 86             populate(B);
 87         }
 88 
 89         @Benchmark
 90         @CompilerControl(CompilerControl.Mode.DONT_INLINE)
 91         public Complex[][] multiply() {
 92             int size = A.length;
 93             Complex[][] R = new Complex[size][size];
 94             for (int i = 0; i < size; i++) {
 95                 for (int j = 0; j < size; j++) {
 96                     Complex s = IZERO;
 97                     for (int k = 0; k < size; k++) {
 98                         s = s.add(A[i][k].mul(B[k][j]));
 99                     }
100                     R[i][j] = s;
101                 }
102             }
103             return R;
104         }
105     }
106 
107 }