< prev index next >

test/micro/org/openjdk/bench/jdk/incubator/vector/TestLoadStoreBytes.java

Print this page

  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.vector;
 25 
 26 import java.nio.ByteBuffer;
 27 import java.nio.ByteOrder;
 28 import java.util.concurrent.TimeUnit;
 29 import jdk.incubator.foreign.CLinker;
 30 import jdk.incubator.foreign.MemoryAccess;
 31 import jdk.incubator.foreign.MemoryAddress;
 32 import jdk.incubator.foreign.MemorySegment;
 33 import jdk.incubator.foreign.ResourceScope;
 34 import jdk.incubator.vector.ByteVector;
 35 import jdk.incubator.vector.VectorOperators;
 36 import jdk.incubator.vector.VectorSpecies;
 37 import org.openjdk.jmh.annotations.Benchmark;
 38 import org.openjdk.jmh.annotations.BenchmarkMode;
 39 import org.openjdk.jmh.annotations.CompilerControl;
 40 import org.openjdk.jmh.annotations.Fork;
 41 import org.openjdk.jmh.annotations.Measurement;
 42 import org.openjdk.jmh.annotations.Mode;
 43 import org.openjdk.jmh.annotations.OutputTimeUnit;
 44 import org.openjdk.jmh.annotations.Param;
 45 import org.openjdk.jmh.annotations.Setup;
 46 import org.openjdk.jmh.annotations.State;
 47 import org.openjdk.jmh.annotations.Warmup;
 48 


 49 @BenchmarkMode(Mode.AverageTime)
 50 @Warmup(iterations = 5, time = 500, timeUnit = TimeUnit.MILLISECONDS)
 51 @Measurement(iterations = 10, time = 500, timeUnit = TimeUnit.MILLISECONDS)
 52 @State(org.openjdk.jmh.annotations.Scope.Thread)
 53 @OutputTimeUnit(TimeUnit.NANOSECONDS)
 54 @Fork(value = 1, jvmArgsAppend = {
 55     "--add-modules=jdk.incubator.foreign,jdk.incubator.vector",
 56     "-Dforeign.restricted=permit",
 57     "--enable-native-access", "ALL-UNNAMED",
 58     "-Djdk.incubator.vector.VECTOR_ACCESS_OOB_CHECK=1"})
 59 public class TestLoadStoreBytes {
 60   private static final VectorSpecies<Byte> SPECIES = VectorSpecies.ofLargestShape(byte.class);
 61 
 62   @Param("1024")
 63   private int size;
 64 
 65   private byte[] srcArray;
 66 
 67   private byte[] dstArray;
 68 

 99     dstArray = srcArray.clone();
100     for (int i = 0; i < srcArray.length; i++) {
101       srcArray[i] = (byte) i;
102     }
103 
104 
105     srcBufferHeap = ByteBuffer.allocate(size);
106     dstBufferHeap = ByteBuffer.allocate(size);
107 
108     srcBufferNative = ByteBuffer.allocateDirect(size);
109     dstBufferNative = ByteBuffer.allocateDirect(size);
110 
111 
112     implicitScope = ResourceScope.newImplicitScope();
113     srcSegmentImplicit = MemorySegment.allocateNative(size, SPECIES.vectorByteSize(), implicitScope);
114     srcBufferSegmentImplicit = srcSegmentImplicit.asByteBuffer();
115     dstSegmentImplicit = MemorySegment.allocateNative(size, SPECIES.vectorByteSize(), implicitScope);
116     dstBufferSegmentImplicit = dstSegmentImplicit.asByteBuffer();
117 
118 
119     srcAddress = CLinker.allocateMemory(size);
120     dstAddress = CLinker.allocateMemory(size);
121 
122     a = new byte[size];
123     b = new byte[size];
124     c = new byte[size];
125   }
126 
127 
128   @Benchmark
129   public void array() {
130 //    final var srcArray = this.srcArray;
131     for (int i = 0; i < SPECIES.loopBound(srcArray.length); i += SPECIES.length()) {
132       var v = ByteVector.fromArray(SPECIES, srcArray, i);
133       v.intoArray(dstArray, i);
134     }
135   }
136 
137   @Benchmark
138   public void array2() {
139 //    final var srcArray = this.srcArray;
140     for (int i = 0; i < SPECIES.loopBound(srcArray.length); i += SPECIES.length()) {

215   public void bufferNativeScalar() {
216     for (int i = 0; i < SPECIES.loopBound(srcArray.length); i++) {
217       var v = srcBufferNative.get(i);
218       dstBufferNative.put(i, v);
219     }
220   }
221 
222 
223   @Benchmark
224   public void bufferSegmentImplicit() {
225     for (int i = 0; i < SPECIES.loopBound(srcArray.length); i += SPECIES.length()) {
226       var v = ByteVector.fromByteBuffer(SPECIES, srcBufferSegmentImplicit, i, ByteOrder.nativeOrder());
227       v.intoByteBuffer(dstBufferSegmentImplicit, i, ByteOrder.nativeOrder());
228     }
229   }
230 
231   @Benchmark
232   @CompilerControl(CompilerControl.Mode.PRINT)
233   public void segmentImplicitScalar() {
234     for (int i = 0; i < SPECIES.loopBound(srcArray.length); i++) {
235       var v = MemoryAccess.getByteAtOffset(srcSegmentImplicit, i);
236       MemoryAccess.setByteAtOffset(dstSegmentImplicit, i, v);
237     }
238   }
239 
240   @Benchmark
241   public void bufferSegmentConfined() {
242     try (final var scope = ResourceScope.newConfinedScope()) {
243       final var srcBufferSegmentConfined = srcAddress.asSegment(size, scope).asByteBuffer();
244       final var dstBufferSegmentConfined = dstAddress.asSegment(size, scope).asByteBuffer();
245 
246       for (int i = 0; i < SPECIES.loopBound(srcArray.length); i += SPECIES.length()) {
247         var v = ByteVector.fromByteBuffer(SPECIES, srcBufferSegmentConfined, i, ByteOrder.nativeOrder());
248         v.intoByteBuffer(dstBufferSegmentConfined, i, ByteOrder.nativeOrder());
249       }
250     }
251   }
252 }

  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.vector;
 25 
 26 import java.nio.ByteBuffer;
 27 import java.nio.ByteOrder;
 28 import java.util.concurrent.TimeUnit;


 29 import jdk.incubator.foreign.MemoryAddress;
 30 import jdk.incubator.foreign.MemorySegment;
 31 import jdk.incubator.foreign.ResourceScope;
 32 import jdk.incubator.vector.ByteVector;
 33 import jdk.incubator.vector.VectorOperators;
 34 import jdk.incubator.vector.VectorSpecies;
 35 import org.openjdk.jmh.annotations.Benchmark;
 36 import org.openjdk.jmh.annotations.BenchmarkMode;
 37 import org.openjdk.jmh.annotations.CompilerControl;
 38 import org.openjdk.jmh.annotations.Fork;
 39 import org.openjdk.jmh.annotations.Measurement;
 40 import org.openjdk.jmh.annotations.Mode;
 41 import org.openjdk.jmh.annotations.OutputTimeUnit;
 42 import org.openjdk.jmh.annotations.Param;
 43 import org.openjdk.jmh.annotations.Setup;
 44 import org.openjdk.jmh.annotations.State;
 45 import org.openjdk.jmh.annotations.Warmup;
 46 
 47 import static jdk.incubator.foreign.ValueLayout.JAVA_BYTE;
 48 
 49 @BenchmarkMode(Mode.AverageTime)
 50 @Warmup(iterations = 5, time = 500, timeUnit = TimeUnit.MILLISECONDS)
 51 @Measurement(iterations = 10, time = 500, timeUnit = TimeUnit.MILLISECONDS)
 52 @State(org.openjdk.jmh.annotations.Scope.Thread)
 53 @OutputTimeUnit(TimeUnit.NANOSECONDS)
 54 @Fork(value = 1, jvmArgsAppend = {
 55     "--add-modules=jdk.incubator.foreign,jdk.incubator.vector",
 56     "-Dforeign.restricted=permit",
 57     "--enable-native-access", "ALL-UNNAMED",
 58     "-Djdk.incubator.vector.VECTOR_ACCESS_OOB_CHECK=1"})
 59 public class TestLoadStoreBytes {
 60   private static final VectorSpecies<Byte> SPECIES = VectorSpecies.ofLargestShape(byte.class);
 61 
 62   @Param("1024")
 63   private int size;
 64 
 65   private byte[] srcArray;
 66 
 67   private byte[] dstArray;
 68 

 99     dstArray = srcArray.clone();
100     for (int i = 0; i < srcArray.length; i++) {
101       srcArray[i] = (byte) i;
102     }
103 
104 
105     srcBufferHeap = ByteBuffer.allocate(size);
106     dstBufferHeap = ByteBuffer.allocate(size);
107 
108     srcBufferNative = ByteBuffer.allocateDirect(size);
109     dstBufferNative = ByteBuffer.allocateDirect(size);
110 
111 
112     implicitScope = ResourceScope.newImplicitScope();
113     srcSegmentImplicit = MemorySegment.allocateNative(size, SPECIES.vectorByteSize(), implicitScope);
114     srcBufferSegmentImplicit = srcSegmentImplicit.asByteBuffer();
115     dstSegmentImplicit = MemorySegment.allocateNative(size, SPECIES.vectorByteSize(), implicitScope);
116     dstBufferSegmentImplicit = dstSegmentImplicit.asByteBuffer();
117 
118 
119     srcAddress = MemorySegment.allocateNative(size, implicitScope).address();
120     dstAddress = MemorySegment.allocateNative(size, implicitScope).address();
121 
122     a = new byte[size];
123     b = new byte[size];
124     c = new byte[size];
125   }
126 
127 
128   @Benchmark
129   public void array() {
130 //    final var srcArray = this.srcArray;
131     for (int i = 0; i < SPECIES.loopBound(srcArray.length); i += SPECIES.length()) {
132       var v = ByteVector.fromArray(SPECIES, srcArray, i);
133       v.intoArray(dstArray, i);
134     }
135   }
136 
137   @Benchmark
138   public void array2() {
139 //    final var srcArray = this.srcArray;
140     for (int i = 0; i < SPECIES.loopBound(srcArray.length); i += SPECIES.length()) {

215   public void bufferNativeScalar() {
216     for (int i = 0; i < SPECIES.loopBound(srcArray.length); i++) {
217       var v = srcBufferNative.get(i);
218       dstBufferNative.put(i, v);
219     }
220   }
221 
222 
223   @Benchmark
224   public void bufferSegmentImplicit() {
225     for (int i = 0; i < SPECIES.loopBound(srcArray.length); i += SPECIES.length()) {
226       var v = ByteVector.fromByteBuffer(SPECIES, srcBufferSegmentImplicit, i, ByteOrder.nativeOrder());
227       v.intoByteBuffer(dstBufferSegmentImplicit, i, ByteOrder.nativeOrder());
228     }
229   }
230 
231   @Benchmark
232   @CompilerControl(CompilerControl.Mode.PRINT)
233   public void segmentImplicitScalar() {
234     for (int i = 0; i < SPECIES.loopBound(srcArray.length); i++) {
235       var v = srcSegmentImplicit.get(JAVA_BYTE, i);
236       dstSegmentImplicit.set(JAVA_BYTE, i, v);
237     }
238   }
239 
240   @Benchmark
241   public void bufferSegmentConfined() {
242     try (final var scope = ResourceScope.newConfinedScope()) {
243       final var srcBufferSegmentConfined = MemorySegment.ofAddressNative(srcAddress, size, scope).asByteBuffer();
244       final var dstBufferSegmentConfined = MemorySegment.ofAddressNative(dstAddress, size, scope).asByteBuffer();
245 
246       for (int i = 0; i < SPECIES.loopBound(srcArray.length); i += SPECIES.length()) {
247         var v = ByteVector.fromByteBuffer(SPECIES, srcBufferSegmentConfined, i, ByteOrder.nativeOrder());
248         v.intoByteBuffer(dstBufferSegmentConfined, i, ByteOrder.nativeOrder());
249       }
250     }
251   }
252 }
< prev index next >