1 /*
 2  * Copyright (c) 2016, 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.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 Base {
32 
33     double[][] A;
34     double[][] B;
35 
36     @Setup
37     public void setup() {
38         A = populate(new double[size][size*2]);
39         B = populate(new double[size][size*2]);
40     }
41 
42     private double[][] populate(double[][] m) {
43         int size = m.length;
44         for (int i = 0; i < size; i++) {
45             for (int j = 0; j < size; j++) {
46                 m[i][j*2+0] = ThreadLocalRandom.current().nextDouble();
47                 m[i][j*2+1] = ThreadLocalRandom.current().nextDouble();
48             }
49         }
50         return m;
51     }
52 
53     @Benchmark
54     @CompilerControl(CompilerControl.Mode.DONT_INLINE)
55     public double[][] multiply() {
56         int size = A.length;
57         double[][] R = new double[size][size * 2];
58         for (int i = 0; i < size; i++) {
59             for (int j = 0; j < size; j++) {
60                 double s_re = 0;
61                 double s_im = 0;
62                 for (int k = 0; k < size; k++) {
63                     double are = A[i][k * 2 + 0];
64                     double aim = A[i][k * 2 + 1];
65                     double bre = B[k][j * 2 + 0];
66                     double bim = B[k][j * 2 + 1];
67                     s_re += are * bre - aim * bim;
68                     s_im += are * bim + bre * aim;
69                 }
70                 R[i][j * 2 + 0] = s_re;
71                 R[i][j * 2 + 1] = s_im;
72             }
73         }
74         return R;
75     }
76 
77 //    @Benchmark
78 //    public double[][] multiplyCacheFriendly() {
79 //        int size = A.length;
80 //        double[][] R = new double[size][size * 2];
81 //        for (int i = 0; i < size; i++) {
82 //            for (int k = 0; k < size; k++) {
83 //                double are = A[i][k * 2 + 0];
84 //                double aim = A[i][k * 2 + 1];
85 //                for (int j = 0; j < size; j++) {
86 //                    double bre = B[k][j * 2 + 0];
87 //                    double bim = B[k][j * 2 + 1];
88 //                    R[i][j * 2 + 0] += are * bre - aim * bim;
89 //                    R[i][j * 2 + 1] += are * bim + bre * aim;
90 //                }
91 //            }
92 //        }
93 //        return R;
94 //    }
95 
96 
97 }