1 /* 2 * Copyright (c) 2021, 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 */ 24 package org.openjdk.bench.jdk.incubator.vector; 25 26 import java.lang.foreign.Arena; 27 import java.nio.ByteOrder; 28 import java.util.concurrent.TimeUnit; 29 30 import java.lang.foreign.MemorySegment; 31 32 import jdk.incubator.vector.ShortVector; 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.TearDown; 46 import org.openjdk.jmh.annotations.Warmup; 47 48 @BenchmarkMode(Mode.AverageTime) 49 @Warmup(iterations = 5, time = 500, timeUnit = TimeUnit.MILLISECONDS) 50 @Measurement(iterations = 10, time = 500, timeUnit = TimeUnit.MILLISECONDS) 51 @State(org.openjdk.jmh.annotations.Scope.Thread) 52 @OutputTimeUnit(TimeUnit.NANOSECONDS) 53 @Fork(value = 1, jvmArgsAppend = { 54 "--add-modules=jdk.incubator.vector", 55 "--enable-native-access", "ALL-UNNAMED"}) 56 public class TestLoadStoreShorts { 57 private static final VectorSpecies<Short> SPECIES = VectorSpecies.ofLargestShape(short.class); 58 59 @Param("256") 60 private int size; 61 62 private int longSize; 63 64 private short[] srcArray; 65 66 private short[] dstArray; 67 68 69 private MemorySegment srcSegmentHeap; 70 71 private MemorySegment dstSegmentHeap; 72 73 private MemorySegment srcSegment; 74 75 private MemorySegment dstSegment; 76 77 private short[] a, b, c; 78 79 @Setup 80 public void setup() { 81 var longSize = size / Short.BYTES; 82 srcArray = new short[longSize]; 83 dstArray = srcArray.clone(); 84 for (int i = 0; i < srcArray.length; i++) { 85 srcArray[i] = (short) i; 86 } 87 88 srcSegmentHeap = MemorySegment.ofArray(new byte[size]); 89 dstSegmentHeap = MemorySegment.ofArray(new byte[size]); 90 91 srcSegment = Arena.ofAuto().allocate(size, SPECIES.vectorByteSize()); 92 dstSegment = Arena.ofAuto().allocate(size, SPECIES.vectorByteSize()); 93 94 this.longSize = longSize; 95 96 a = new short[size]; 97 b = new short[size]; 98 c = new short[size]; 99 } 100 101 @Benchmark 102 public void array() { 103 for (int i = 0; i < SPECIES.loopBound(srcArray.length); i += SPECIES.length()) { 104 var v = ShortVector.fromArray(SPECIES, srcArray, i); 105 v.intoArray(dstArray, i); 106 } 107 } 108 109 @Benchmark 110 public void vectAdd1() { 111 var a = this.a; 112 var b = this.b; 113 var c = this.c; 114 115 for (int i = 0; i < a.length; i += SPECIES.length()) { 116 ShortVector av = ShortVector.fromArray(SPECIES, a, i); 117 ShortVector bv = ShortVector.fromArray(SPECIES, b, i); 118 av.lanewise(VectorOperators.ADD, bv).intoArray(c, i); 119 } 120 } 121 122 @Benchmark 123 public void vectAdd2() { 124 var a = this.a; 125 var b = this.b; 126 var c = this.c; 127 128 for (int i = 0; i < a.length/SPECIES.length(); i++) { 129 ShortVector av = ShortVector.fromArray(SPECIES, a, (i*SPECIES.length())); 130 ShortVector bv = ShortVector.fromArray(SPECIES, b, (i*SPECIES.length())); 131 av.lanewise(VectorOperators.ADD, bv).intoArray(c, (i*SPECIES.length())); 132 } 133 } 134 135 @Benchmark 136 public void arrayAdd() { 137 for (int i = 0; i < SPECIES.loopBound(srcArray.length); i += SPECIES.length()) { 138 var v = ShortVector.fromArray(SPECIES, srcArray, i); 139 v = v.add(v); 140 v.intoArray(dstArray, i); 141 } 142 } 143 144 @Benchmark 145 public void heapSegment() { 146 for (long i = 0; i < SPECIES.loopBound(longSize); i += SPECIES.length()) { 147 var v = ShortVector.fromMemorySegment(SPECIES, srcSegmentHeap, i, ByteOrder.nativeOrder()); 148 v.intoMemorySegment(dstSegmentHeap, i, ByteOrder.nativeOrder()); 149 } 150 } 151 152 @Benchmark 153 public void segmentNativeImplicit() { 154 for (long i = 0; i < SPECIES.loopBound(srcArray.length); i += SPECIES.length()) { 155 var v = ShortVector.fromMemorySegment(SPECIES, srcSegment, i, ByteOrder.nativeOrder()); 156 v.intoMemorySegment(dstSegment, i, ByteOrder.nativeOrder()); 157 } 158 } 159 160 @Benchmark 161 public void segmentNativeConfined() { 162 try (final var arena = Arena.ofConfined()) { 163 final var srcSegmentConfined = srcSegment.reinterpret(arena, null); 164 final var dstSegmentConfined = dstSegment.reinterpret(arena, null); 165 166 for (long i = 0; i < SPECIES.loopBound(srcArray.length); i += SPECIES.length()) { 167 var v = ShortVector.fromMemorySegment(SPECIES, srcSegmentConfined, i, ByteOrder.nativeOrder()); 168 v.intoMemorySegment(dstSegmentConfined, i, ByteOrder.nativeOrder()); 169 } 170 } 171 } 172 }