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.MemoryAccess;
 26 import jdk.incubator.foreign.MemorySegment;
 27 import jdk.incubator.foreign.ResourceScope;
 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 jdk.incubator.foreign.MemoryLayout.PathElement.sequenceElement;
 45 import static jdk.incubator.foreign.MemoryLayouts.JAVA_DOUBLE;
 46 
 47 @BenchmarkMode(Mode.AverageTime)
 48 @Warmup(iterations = 5, time = 500, timeUnit = TimeUnit.MILLISECONDS)
 49 @Measurement(iterations = 10, time = 500, timeUnit = TimeUnit.MILLISECONDS)
 50 @State(org.openjdk.jmh.annotations.Scope.Thread)
 51 @OutputTimeUnit(TimeUnit.MILLISECONDS)
 52 @Fork(3)
 53 public class LoopOverNonConstantFP {
 54 
 55     static final Unsafe unsafe = Utils.unsafe;
 56 
 57     static final int ELEM_SIZE = 1_000_000;
 58     static final int CARRIER_SIZE = (int)JAVA_DOUBLE.byteSize();
 59     static final int ALLOC_SIZE = ELEM_SIZE * CARRIER_SIZE;
 60 
 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         segmentIn = MemorySegment.allocateNative(ALLOC_SIZE, ResourceScope.newConfinedScope());
 76         segmentOut = MemorySegment.allocateNative(ALLOC_SIZE, ResourceScope.newConfinedScope());
 77         for (int i = 0; i < ELEM_SIZE; i++) {
 78             MemoryAccess.setDoubleAtIndex(segmentIn, i, i);
 79         }
 80         for (int i = 0; i < ELEM_SIZE; i++) {
 81             MemoryAccess.setDoubleAtIndex(segmentOut, i, i);
 82         }
 83         byteBufferIn = ByteBuffer.allocateDirect(ALLOC_SIZE).order(ByteOrder.nativeOrder());
 84         byteBufferOut = ByteBuffer.allocateDirect(ALLOC_SIZE).order(ByteOrder.nativeOrder());
 85         for (int i = 0; i < ELEM_SIZE; i++) {
 86             byteBufferIn.putDouble(i * CARRIER_SIZE , i);
 87         }
 88         for (int i = 0; i < ELEM_SIZE; i++) {
 89             byteBufferOut.putDouble(i * CARRIER_SIZE , i);
 90         }
 91     }
 92 
 93     @TearDown
 94     public void tearDown() {
 95         segmentIn.scope().close();
 96         segmentOut.scope().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             MemoryAccess.setDoubleAtIndex(segmentOut, i,
116                     MemoryAccess.getDoubleAtIndex(segmentIn, i) +
117                     MemoryAccess.getDoubleAtIndex(segmentOut, 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 }