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-preview", "--enable-native-access=ALL-UNNAMED" }) 44 public class UnrolledAccess extends JavaLayouts { 45 46 static final Unsafe U = Utils.unsafe; 47 48 static final VarHandle VH_LONG_UNALIGNED = JAVA_LONG_UNALIGNED.arrayElementVarHandle(); 49 50 static final VarHandle VH_LONG = JAVA_LONG.arrayElementVarHandle(); 51 52 final static int SIZE = 1024; 53 54 @State(Scope.Benchmark) 55 public static class Data { 56 57 final double[] inputArray; 58 final double[] outputArray; 59 final long inputAddress; 60 final long outputAddress; 61 final MemorySegment inputSegment; 62 final MemorySegment outputSegment; 63 64 65 public Data() { 66 this.inputArray = new double[SIZE]; 67 this.outputArray = new double[SIZE]; 68 this.inputAddress = U.allocateMemory(8 * SIZE); 69 this.outputAddress = U.allocateMemory(8 * SIZE); 70 this.inputSegment = MemorySegment.ofAddress(inputAddress) 71 .reinterpret(8*SIZE); 72 this.outputSegment = MemorySegment.ofAddress(outputAddress) 73 .reinterpret(8*SIZE); 74 } 75 } 76 77 @Benchmark 78 public void unsafe_loop(Data state) { 79 final long ia = state.inputAddress; 80 final long oa = state.outputAddress; 81 for(int i = 0; i < SIZE; i+=4) { 82 U.putLong(oa + 8*i, U.getLong(ia + 8*i) + U.getLong(oa + 8*i)); 83 U.putLong(oa + 8*(i+1), U.getLong(ia + 8*(i+1)) + U.getLong(oa + 8*(i+1))); 84 U.putLong(oa + 8*(i+2), U.getLong(ia + 8*(i+2)) + U.getLong(oa + 8*(i+2))); 85 U.putLong(oa + 8*(i+3), U.getLong(ia + 8*(i+3)) + U.getLong(oa + 8*(i+3))); 86 } 87 } 88 89 @Benchmark 90 public void handle_loop(Data state) { 91 final MemorySegment is = state.inputSegment; 92 final MemorySegment os = state.outputSegment; 93 94 for(int i = 0; i < SIZE; i+=4) { 95 VH_LONG.set(os, (long) (i), (long) VH_LONG.get(is, (long) (i)) + (long) VH_LONG.get(os, (long) (i))); 96 VH_LONG.set(os, (long) (i+1), (long) VH_LONG.get(is, (long) (i+1)) + (long) VH_LONG.get(os, (long) (i+1))); 97 VH_LONG.set(os, (long) (i+2), (long) VH_LONG.get(is, (long) (i+2)) + (long) VH_LONG.get(os, (long) (i+2))); 98 VH_LONG.set(os, (long) (i+3), (long) VH_LONG.get(is, (long) (i+3)) + (long) VH_LONG.get(os, (long) (i+3))); 99 } 100 } 101 102 @Benchmark 103 public void handle_loop_instance(Data state) { 104 final MemorySegment is = state.inputSegment; 105 final MemorySegment os = state.outputSegment; 106 107 for(int i = 0; i < SIZE; i+=4) { 108 os.setAtIndex(JAVA_LONG, i, is.getAtIndex(JAVA_LONG, i) + os.getAtIndex(JAVA_LONG, i)); 109 os.setAtIndex(JAVA_LONG, i+1, is.getAtIndex(JAVA_LONG, i+1) + os.getAtIndex(JAVA_LONG, i+1)); 110 os.setAtIndex(JAVA_LONG, i+2, is.getAtIndex(JAVA_LONG, i+2) + os.getAtIndex(JAVA_LONG, i+2)); 111 os.setAtIndex(JAVA_LONG, i+3, is.getAtIndex(JAVA_LONG, i+3) + os.getAtIndex(JAVA_LONG, i+3)); 112 } 113 } 114 115 @Benchmark 116 public void handle_loop_unaligned(Data state) { 117 final MemorySegment is = state.inputSegment; 118 final MemorySegment os = state.outputSegment; 119 120 for(int i = 0; i < SIZE; i+=4) { 121 VH_LONG_UNALIGNED.set(os, (long) (i), (long) VH_LONG_UNALIGNED.get(is, (long) (i)) + (long) VH_LONG_UNALIGNED.get(os, (long) (i))); 122 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))); 123 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))); 124 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))); 125 } 126 } 127 128 @Benchmark 129 public void handle_loop_instance_unaligned(Data state) { 130 final MemorySegment is = state.inputSegment; 131 final MemorySegment os = state.outputSegment; 132 133 for(int i = 0; i < SIZE; i+=4) { 134 os.setAtIndex(JAVA_LONG_UNALIGNED, i, is.getAtIndex(JAVA_LONG_UNALIGNED, i) + os.getAtIndex(JAVA_LONG_UNALIGNED, i)); 135 os.setAtIndex(JAVA_LONG_UNALIGNED, i+1, is.getAtIndex(JAVA_LONG_UNALIGNED, i+1) + os.getAtIndex(JAVA_LONG_UNALIGNED, i+1)); 136 os.setAtIndex(JAVA_LONG_UNALIGNED, i+2, is.getAtIndex(JAVA_LONG_UNALIGNED, i+2) + os.getAtIndex(JAVA_LONG_UNALIGNED, i+2)); 137 os.setAtIndex(JAVA_LONG_UNALIGNED, i+3, is.getAtIndex(JAVA_LONG_UNALIGNED, i+3) + os.getAtIndex(JAVA_LONG_UNALIGNED, i+3)); 138 } 139 } 140 }