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 24 package org.openjdk.bench.jdk.incubator.foreign; 25 26 import static jdk.incubator.foreign.MemoryAccess.*; 27 import jdk.incubator.foreign.*; 28 import org.openjdk.jmh.annotations.*; 29 import org.openjdk.jmh.runner.Runner; 30 import org.openjdk.jmh.runner.options.Options; 31 import org.openjdk.jmh.runner.options.OptionsBuilder; 32 import sun.misc.Unsafe; 33 import java.util.concurrent.TimeUnit; 34 35 import java.lang.invoke.VarHandle; 36 37 @BenchmarkMode(Mode.AverageTime) 38 @Warmup(iterations = 5, time = 500, timeUnit = TimeUnit.MILLISECONDS) 39 @Measurement(iterations = 10, time = 500, timeUnit = TimeUnit.MILLISECONDS) 40 @State(org.openjdk.jmh.annotations.Scope.Thread) 41 @OutputTimeUnit(TimeUnit.MICROSECONDS) 42 @Fork(3) 43 public class UnrolledAccess { 44 45 static final Unsafe U = Utils.unsafe; 46 47 final static int SIZE = 1024; 48 49 static final VarHandle LONG_HANDLE = MemoryLayout.sequenceLayout(SIZE, MemoryLayouts.JAVA_LONG) 50 .varHandle(long.class, MemoryLayout.PathElement.sequenceElement()); 51 52 @State(Scope.Benchmark) 53 public static class Data { 54 55 final double[] inputArray; 56 final double[] outputArray; 57 final long inputAddress; 58 final long outputAddress; 59 final MemorySegment inputSegment; 60 final MemorySegment outputSegment; 61 62 Data()63 public Data() { 64 this.inputArray = new double[SIZE]; 65 this.outputArray = new double[SIZE]; 66 this.inputAddress = U.allocateMemory(8 * SIZE); 67 this.outputAddress = U.allocateMemory(8 * SIZE); 68 this.inputSegment = MemoryAddress.ofLong(inputAddress).asSegment(8*SIZE, ResourceScope.globalScope()); 69 this.outputSegment = MemoryAddress.ofLong(outputAddress).asSegment(8*SIZE, ResourceScope.globalScope()); 70 } 71 } 72 73 @Benchmark unsafe_loop(Data state)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 handle_loop(Data state)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 LONG_HANDLE.set(os, (long) (i), (long) LONG_HANDLE.get(is, (long) (i)) + (long) LONG_HANDLE.get(os, (long) (i))); 92 LONG_HANDLE.set(os, (long) (i+1), (long) LONG_HANDLE.get(is, (long) (i+1)) + (long) LONG_HANDLE.get(os, (long) (i+1))); 93 LONG_HANDLE.set(os, (long) (i+2), (long) LONG_HANDLE.get(is, (long) (i+2)) + (long) LONG_HANDLE.get(os, (long) (i+2))); 94 LONG_HANDLE.set(os, (long) (i+3), (long) LONG_HANDLE.get(is, (long) (i+3)) + (long) LONG_HANDLE.get(os, (long) (i+3))); 95 } 96 } 97 98 @Benchmark static_handle_loop(Data state)99 public void static_handle_loop(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 setLongAtIndex(os, i,getLongAtIndex(is, i) + MemoryAccess.getLongAtIndex(os, i)); 105 setLongAtIndex(os, i+1,getLongAtIndex(is, i+1) + getLongAtIndex(os, i+1)); 106 setLongAtIndex(os, i+2,getLongAtIndex(is, i+2) + getLongAtIndex(os, i+2)); 107 setLongAtIndex(os, i+3,getLongAtIndex(is, i+3) + getLongAtIndex(os, i+3)); 108 } 109 } 110 } 111