1 /*
  2  * Copyright (c) 2020, 2021, 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.jdk.incubator.foreign;
 25 
 26 import jdk.incubator.foreign.*;
 27 import org.openjdk.jmh.annotations.*;
 28 import org.openjdk.jmh.runner.Runner;
 29 import org.openjdk.jmh.runner.options.Options;
 30 import org.openjdk.jmh.runner.options.OptionsBuilder;
 31 import sun.misc.Unsafe;
 32 import java.util.concurrent.TimeUnit;
 33 
 34 import java.lang.invoke.VarHandle;
 35 
 36 import static jdk.incubator.foreign.ValueLayout.JAVA_LONG;
 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 {
 45 
 46     static final Unsafe U = Utils.unsafe;
 47 
 48     final static int SIZE = 1024;
 49 
 50     static final VarHandle LONG_HANDLE = MemoryLayout.sequenceLayout(SIZE, JAVA_LONG)
 51             .varHandle(MemoryLayout.PathElement.sequenceElement());
 52 
 53     @State(Scope.Benchmark)
 54     public static class Data {
 55 
 56         final double[] inputArray;
 57         final double[] outputArray;
 58         final long inputAddress;
 59         final long outputAddress;
 60         final MemorySegment inputSegment;
 61         final MemorySegment outputSegment;
 62 
 63 
 64         public Data() {
 65             this.inputArray = new double[SIZE];
 66             this.outputArray = new double[SIZE];
 67             this.inputAddress = U.allocateMemory(8 * SIZE);
 68             this.outputAddress = U.allocateMemory(8 * SIZE);
 69             this.inputSegment = MemorySegment.ofAddressNative(MemoryAddress.ofLong(inputAddress), 8*SIZE, ResourceScope.globalScope());
 70             this.outputSegment = MemorySegment.ofAddressNative(MemoryAddress.ofLong(outputAddress), 8*SIZE, ResourceScope.globalScope());
 71         }
 72     }
 73 
 74     @Benchmark
 75     public void unsafe_loop(Data state) {
 76         final long ia = state.inputAddress;
 77         final long oa = state.outputAddress;
 78         for(int i = 0; i < SIZE; i+=4) {
 79             U.putLong(oa + 8*i, U.getLong(ia + 8*i) + U.getLong(oa + 8*i));
 80             U.putLong(oa + 8*(i+1), U.getLong(ia + 8*(i+1)) + U.getLong(oa + 8*(i+1)));
 81             U.putLong(oa + 8*(i+2), U.getLong(ia + 8*(i+2)) + U.getLong(oa + 8*(i+2)));
 82             U.putLong(oa + 8*(i+3), U.getLong(ia + 8*(i+3)) + U.getLong(oa + 8*(i+3)));
 83         }
 84     }
 85 
 86     @Benchmark
 87     public void handle_loop(Data state) {
 88         final MemorySegment is = state.inputSegment;
 89         final MemorySegment os = state.outputSegment;
 90 
 91         for(int i = 0; i < SIZE; i+=4) {
 92             LONG_HANDLE.set(os, (long) (i),   (long) LONG_HANDLE.get(is, (long) (i))   + (long) LONG_HANDLE.get(os, (long) (i)));
 93             LONG_HANDLE.set(os, (long) (i+1), (long) LONG_HANDLE.get(is, (long) (i+1)) + (long) LONG_HANDLE.get(os, (long) (i+1)));
 94             LONG_HANDLE.set(os, (long) (i+2), (long) LONG_HANDLE.get(is, (long) (i+2)) + (long) LONG_HANDLE.get(os, (long) (i+2)));
 95             LONG_HANDLE.set(os, (long) (i+3), (long) LONG_HANDLE.get(is, (long) (i+3)) + (long) LONG_HANDLE.get(os, (long) (i+3)));
 96         }
 97     }
 98 
 99     @Benchmark
100     public void handle_loop_instance(Data state) {
101         final MemorySegment is = state.inputSegment;
102         final MemorySegment os = state.outputSegment;
103 
104         for(int i = 0; i < SIZE; i+=4) {
105             os.setAtIndex(JAVA_LONG, i, is.getAtIndex(JAVA_LONG, i) + os.get(JAVA_LONG, i));
106             os.setAtIndex(JAVA_LONG, i+1, is.getAtIndex(JAVA_LONG, i+1) + os.get(JAVA_LONG, i+1));
107             os.setAtIndex(JAVA_LONG, i+2, is.getAtIndex(JAVA_LONG, i+2) + os.get(JAVA_LONG, i+2));
108             os.setAtIndex(JAVA_LONG, i+3, is.getAtIndex(JAVA_LONG, i+3) + os.get(JAVA_LONG, i+3));
109         }
110     }
111 }