1 /*
  2  * Copyright (c) 2020, 2022, 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 
 24 package org.openjdk.bench.java.lang.foreign;
 25 
 26 import java.lang.foreign.*;
 27 import java.lang.foreign.Arena;
 28 import java.lang.invoke.VarHandle;
 29 import org.openjdk.jmh.annotations.*;
 30 import org.openjdk.jmh.runner.Runner;
 31 import org.openjdk.jmh.runner.options.Options;
 32 import org.openjdk.jmh.runner.options.OptionsBuilder;
 33 import sun.misc.Unsafe;
 34 import java.util.concurrent.TimeUnit;
 35 
 36 import static java.lang.foreign.ValueLayout.*;
 37 
 38 @BenchmarkMode(Mode.AverageTime)
 39 @Warmup(iterations = 5, time = 500, timeUnit = TimeUnit.MILLISECONDS)
 40 @Measurement(iterations = 10, time = 500, timeUnit = TimeUnit.MILLISECONDS)
 41 @State(org.openjdk.jmh.annotations.Scope.Thread)
 42 @OutputTimeUnit(TimeUnit.MICROSECONDS)
 43 @Fork(value = 3, jvmArgsAppend = { "--enable-native-access=ALL-UNNAMED" })
 44 public class UnrolledAccess extends JavaLayouts {
 45 
 46     static final Unsafe U = Utils.unsafe;
 47 
 48     final static int SIZE = 1024;
 49 
 50     @State(Scope.Benchmark)
 51     public static class Data {
 52 
 53         final double[] inputArray;
 54         final double[] outputArray;
 55         final long inputAddress;
 56         final long outputAddress;
 57         final MemorySegment inputSegment;
 58         final MemorySegment outputSegment;
 59 
 60 
 61         public Data() {
 62             this.inputArray = new double[SIZE];
 63             this.outputArray = new double[SIZE];
 64             this.inputAddress = U.allocateMemory(8 * SIZE);
 65             this.outputAddress = U.allocateMemory(8 * SIZE);
 66             this.inputSegment = MemorySegment.ofAddress(inputAddress)
 67                     .reinterpret(8*SIZE);
 68             this.outputSegment = MemorySegment.ofAddress(outputAddress)
 69                     .reinterpret(8*SIZE);
 70         }
 71     }
 72 
 73     @Benchmark
 74     public void unsafe_loop(Data state) {
 75         final long ia = state.inputAddress;
 76         final long oa = state.outputAddress;
 77         for(int i = 0; i < SIZE; i+=4) {
 78             U.putLong(oa + 8*i, U.getLong(ia + 8*i) + U.getLong(oa + 8*i));
 79             U.putLong(oa + 8*(i+1), U.getLong(ia + 8*(i+1)) + U.getLong(oa + 8*(i+1)));
 80             U.putLong(oa + 8*(i+2), U.getLong(ia + 8*(i+2)) + U.getLong(oa + 8*(i+2)));
 81             U.putLong(oa + 8*(i+3), U.getLong(ia + 8*(i+3)) + U.getLong(oa + 8*(i+3)));
 82         }
 83     }
 84 
 85     @Benchmark
 86     public void handle_loop(Data state) {
 87         final MemorySegment is = state.inputSegment;
 88         final MemorySegment os = state.outputSegment;
 89 
 90         for(int i = 0; i < SIZE; i+=4) {
 91             VH_LONG.set(os, (long) (i),   (long) VH_LONG.get(is, (long) (i))   + (long) VH_LONG.get(os, (long) (i)));
 92             VH_LONG.set(os, (long) (i+1), (long) VH_LONG.get(is, (long) (i+1)) + (long) VH_LONG.get(os, (long) (i+1)));
 93             VH_LONG.set(os, (long) (i+2), (long) VH_LONG.get(is, (long) (i+2)) + (long) VH_LONG.get(os, (long) (i+2)));
 94             VH_LONG.set(os, (long) (i+3), (long) VH_LONG.get(is, (long) (i+3)) + (long) VH_LONG.get(os, (long) (i+3)));
 95         }
 96     }
 97 
 98     @Benchmark
 99     public void handle_loop_instance(Data state) {
100         final MemorySegment is = state.inputSegment;
101         final MemorySegment os = state.outputSegment;
102 
103         for(int i = 0; i < SIZE; i+=4) {
104             os.setAtIndex(JAVA_LONG, i, is.getAtIndex(JAVA_LONG, i) + os.getAtIndex(JAVA_LONG, i));
105             os.setAtIndex(JAVA_LONG, i+1, is.getAtIndex(JAVA_LONG, i+1) + os.getAtIndex(JAVA_LONG, i+1));
106             os.setAtIndex(JAVA_LONG, i+2, is.getAtIndex(JAVA_LONG, i+2) + os.getAtIndex(JAVA_LONG, i+2));
107             os.setAtIndex(JAVA_LONG, i+3, is.getAtIndex(JAVA_LONG, i+3) + os.getAtIndex(JAVA_LONG, i+3));
108         }
109     }
110 
111     @Benchmark
112     public void handle_loop_unaligned(Data state) {
113         final MemorySegment is = state.inputSegment;
114         final MemorySegment os = state.outputSegment;
115 
116         for(int i = 0; i < SIZE; i+=4) {
117             VH_LONG_UNALIGNED.set(os, (long) (i),   (long) VH_LONG_UNALIGNED.get(is, (long) (i))   + (long) VH_LONG_UNALIGNED.get(os, (long) (i)));
118             VH_LONG_UNALIGNED.set(os, (long) (i+1), (long) VH_LONG_UNALIGNED.get(is, (long) (i+1)) + (long) VH_LONG_UNALIGNED.get(os, (long) (i+1)));
119             VH_LONG_UNALIGNED.set(os, (long) (i+2), (long) VH_LONG_UNALIGNED.get(is, (long) (i+2)) + (long) VH_LONG_UNALIGNED.get(os, (long) (i+2)));
120             VH_LONG_UNALIGNED.set(os, (long) (i+3), (long) VH_LONG_UNALIGNED.get(is, (long) (i+3)) + (long) VH_LONG_UNALIGNED.get(os, (long) (i+3)));
121         }
122     }
123 
124     @Benchmark
125     public void handle_loop_instance_unaligned(Data state) {
126         final MemorySegment is = state.inputSegment;
127         final MemorySegment os = state.outputSegment;
128 
129         for(int i = 0; i < SIZE; i+=4) {
130             os.setAtIndex(JAVA_LONG_UNALIGNED, i, is.getAtIndex(JAVA_LONG_UNALIGNED, i) + os.getAtIndex(JAVA_LONG_UNALIGNED, i));
131             os.setAtIndex(JAVA_LONG_UNALIGNED, i+1, is.getAtIndex(JAVA_LONG_UNALIGNED, i+1) + os.getAtIndex(JAVA_LONG_UNALIGNED, i+1));
132             os.setAtIndex(JAVA_LONG_UNALIGNED, i+2, is.getAtIndex(JAVA_LONG_UNALIGNED, i+2) + os.getAtIndex(JAVA_LONG_UNALIGNED, i+2));
133             os.setAtIndex(JAVA_LONG_UNALIGNED, i+3, is.getAtIndex(JAVA_LONG_UNALIGNED, i+3) + os.getAtIndex(JAVA_LONG_UNALIGNED, i+3));
134         }
135     }
136 }