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 package org.openjdk.bench.jdk.incubator.foreign;
 24 
 25 import jdk.incubator.foreign.MemorySegment;
 26 import jdk.incubator.foreign.ResourceScope;
 27 import org.openjdk.jmh.annotations.Benchmark;
 28 import org.openjdk.jmh.annotations.BenchmarkMode;
 29 import org.openjdk.jmh.annotations.Fork;
 30 import org.openjdk.jmh.annotations.Measurement;
 31 import org.openjdk.jmh.annotations.Mode;
 32 import org.openjdk.jmh.annotations.OutputTimeUnit;
 33 import org.openjdk.jmh.annotations.Setup;
 34 import org.openjdk.jmh.annotations.State;
 35 import org.openjdk.jmh.annotations.TearDown;
 36 import org.openjdk.jmh.annotations.Warmup;
 37 import sun.misc.Unsafe;
 38 
 39 import java.nio.ByteBuffer;
 40 import java.nio.ByteOrder;
 41 import java.util.concurrent.TimeUnit;
 42 
 43 import static jdk.incubator.foreign.MemoryLayout.PathElement.sequenceElement;
 44 import static jdk.incubator.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     MemorySegment segmentIn, segmentOut;
 61     long unsafe_addrIn, unsafe_addrOut;
 62     ByteBuffer byteBufferIn, byteBufferOut;
 63 
 64     @Setup
 65     public void setup() {
 66         unsafe_addrIn = unsafe.allocateMemory(ALLOC_SIZE);
 67         unsafe_addrOut = unsafe.allocateMemory(ALLOC_SIZE);
 68         for (int i = 0; i < ELEM_SIZE; i++) {
 69             unsafe.putDouble(unsafe_addrIn + (i * CARRIER_SIZE), i);
 70         }
 71         for (int i = 0; i < ELEM_SIZE; i++) {
 72             unsafe.putDouble(unsafe_addrOut + (i * CARRIER_SIZE), i);
 73         }
 74         segmentIn = MemorySegment.allocateNative(ALLOC_SIZE, ResourceScope.newConfinedScope());
 75         segmentOut = MemorySegment.allocateNative(ALLOC_SIZE, ResourceScope.newConfinedScope());
 76         for (int i = 0; i < ELEM_SIZE; i++) {
 77             segmentIn.setAtIndex(JAVA_DOUBLE, i, i);
 78         }
 79         for (int i = 0; i < ELEM_SIZE; i++) {
 80             segmentOut.setAtIndex(JAVA_DOUBLE, i, i);
 81         }
 82         byteBufferIn = ByteBuffer.allocateDirect(ALLOC_SIZE).order(ByteOrder.nativeOrder());
 83         byteBufferOut = ByteBuffer.allocateDirect(ALLOC_SIZE).order(ByteOrder.nativeOrder());
 84         for (int i = 0; i < ELEM_SIZE; i++) {
 85             byteBufferIn.putDouble(i * CARRIER_SIZE , i);
 86         }
 87         for (int i = 0; i < ELEM_SIZE; i++) {
 88             byteBufferOut.putDouble(i * CARRIER_SIZE , i);
 89         }
 90     }
 91 
 92     @TearDown
 93     public void tearDown() {
 94         segmentIn.scope().close();
 95         segmentOut.scope().close();
 96         unsafe.invokeCleaner(byteBufferIn);
 97         unsafe.invokeCleaner(byteBufferOut);
 98         unsafe.freeMemory(unsafe_addrIn);
 99         unsafe.freeMemory(unsafe_addrOut);
100     }
101 
102     @Benchmark
103     public void unsafe_loop() {
104         for (int i = 0; i < ELEM_SIZE; i ++) {
105             unsafe.putDouble(unsafe_addrOut + (i * CARRIER_SIZE),
106                     unsafe.getDouble(unsafe_addrIn + (i * CARRIER_SIZE)) +
107                     unsafe.getDouble(unsafe_addrOut + (i * CARRIER_SIZE)));
108         }
109     }
110 
111     @Benchmark
112     public void segment_loop() {
113         for (int i = 0; i < ELEM_SIZE; i ++) {
114             segmentOut.setAtIndex(JAVA_DOUBLE, i,
115                     segmentIn.getAtIndex(JAVA_DOUBLE, i) +
116                     segmentOut.getAtIndex(JAVA_DOUBLE, i));
117         }
118     }
119 
120     @Benchmark
121     public void BB_loop() {
122         for (int i = 0; i < ELEM_SIZE; i++) {
123             byteBufferOut.putDouble(i * CARRIER_SIZE,
124                     byteBufferIn.getDouble(i * CARRIER_SIZE) +
125                     byteBufferOut.getDouble(i * CARRIER_SIZE));
126         }
127     }
128 }