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 package org.openjdk.bench.java.lang.foreign; 24 25 import java.lang.foreign.Arena; 26 import java.lang.foreign.MemorySegment; 27 28 import org.openjdk.jmh.annotations.Benchmark; 29 import org.openjdk.jmh.annotations.BenchmarkMode; 30 import org.openjdk.jmh.annotations.Fork; 31 import org.openjdk.jmh.annotations.Measurement; 32 import org.openjdk.jmh.annotations.Mode; 33 import org.openjdk.jmh.annotations.OutputTimeUnit; 34 import org.openjdk.jmh.annotations.Setup; 35 import org.openjdk.jmh.annotations.State; 36 import org.openjdk.jmh.annotations.TearDown; 37 import org.openjdk.jmh.annotations.Warmup; 38 import sun.misc.Unsafe; 39 40 import java.nio.ByteBuffer; 41 import java.nio.ByteOrder; 42 import java.util.concurrent.TimeUnit; 43 44 import static java.lang.foreign.ValueLayout.JAVA_DOUBLE; 45 46 @BenchmarkMode(Mode.AverageTime) 47 @Warmup(iterations = 5, time = 500, timeUnit = TimeUnit.MILLISECONDS) 48 @Measurement(iterations = 10, time = 500, timeUnit = TimeUnit.MILLISECONDS) 49 @State(org.openjdk.jmh.annotations.Scope.Thread) 50 @OutputTimeUnit(TimeUnit.MILLISECONDS) 51 @Fork(3) 52 public class LoopOverNonConstantFP { 53 54 static final Unsafe unsafe = Utils.unsafe; 55 56 static final int ELEM_SIZE = 1_000_000; 57 static final int CARRIER_SIZE = (int)JAVA_DOUBLE.byteSize(); 58 static final int ALLOC_SIZE = ELEM_SIZE * CARRIER_SIZE; 59 60 Arena arena; 61 MemorySegment segmentIn, segmentOut; 62 long unsafe_addrIn, unsafe_addrOut; 63 ByteBuffer byteBufferIn, byteBufferOut; 64 65 @Setup 66 public void setup() { 67 unsafe_addrIn = unsafe.allocateMemory(ALLOC_SIZE); 68 unsafe_addrOut = unsafe.allocateMemory(ALLOC_SIZE); 69 for (int i = 0; i < ELEM_SIZE; i++) { 70 unsafe.putDouble(unsafe_addrIn + (i * CARRIER_SIZE), i); 71 } 72 for (int i = 0; i < ELEM_SIZE; i++) { 73 unsafe.putDouble(unsafe_addrOut + (i * CARRIER_SIZE), i); 74 } 75 arena = Arena.ofConfined(); 76 segmentIn = arena.allocate(ALLOC_SIZE, 1); 77 segmentOut = arena.allocate(ALLOC_SIZE, 1); 78 for (int i = 0; i < ELEM_SIZE; i++) { 79 segmentIn.setAtIndex(JAVA_DOUBLE, i, i); 80 } 81 for (int i = 0; i < ELEM_SIZE; i++) { 82 segmentOut.setAtIndex(JAVA_DOUBLE, i, i); 83 } 84 byteBufferIn = ByteBuffer.allocateDirect(ALLOC_SIZE).order(ByteOrder.nativeOrder()); 85 byteBufferOut = ByteBuffer.allocateDirect(ALLOC_SIZE).order(ByteOrder.nativeOrder()); 86 for (int i = 0; i < ELEM_SIZE; i++) { 87 byteBufferIn.putDouble(i * CARRIER_SIZE , i); 88 } 89 for (int i = 0; i < ELEM_SIZE; i++) { 90 byteBufferOut.putDouble(i * CARRIER_SIZE , i); 91 } 92 } 93 94 @TearDown 95 public void tearDown() { 96 arena.close(); 97 unsafe.invokeCleaner(byteBufferIn); 98 unsafe.invokeCleaner(byteBufferOut); 99 unsafe.freeMemory(unsafe_addrIn); 100 unsafe.freeMemory(unsafe_addrOut); 101 } 102 103 @Benchmark 104 public void unsafe_loop() { 105 for (int i = 0; i < ELEM_SIZE; i ++) { 106 unsafe.putDouble(unsafe_addrOut + (i * CARRIER_SIZE), 107 unsafe.getDouble(unsafe_addrIn + (i * CARRIER_SIZE)) + 108 unsafe.getDouble(unsafe_addrOut + (i * CARRIER_SIZE))); 109 } 110 } 111 112 @Benchmark 113 public void segment_loop() { 114 for (int i = 0; i < ELEM_SIZE; i ++) { 115 segmentOut.setAtIndex(JAVA_DOUBLE, i, 116 segmentIn.getAtIndex(JAVA_DOUBLE, i) + 117 segmentOut.getAtIndex(JAVA_DOUBLE, i)); 118 } 119 } 120 121 @Benchmark 122 public void BB_loop() { 123 for (int i = 0; i < ELEM_SIZE; i++) { 124 byteBufferOut.putDouble(i * CARRIER_SIZE, 125 byteBufferIn.getDouble(i * CARRIER_SIZE) + 126 byteBufferOut.getDouble(i * CARRIER_SIZE)); 127 } 128 } 129 }