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-preview", 56 "--enable-native-access", "ALL-UNNAMED"}) 57 public class TestLoadStoreShorts { 58 private static final VectorSpecies<Short> SPECIES = VectorSpecies.ofLargestShape(short.class); 59 60 @Param("256") 61 private int size; 62 63 private int longSize; 64 65 private short[] srcArray; 66 67 private short[] dstArray; 68 69 70 private MemorySegment srcSegmentHeap; 71 72 private MemorySegment dstSegmentHeap; 73 74 private MemorySegment srcSegment; 75 76 private MemorySegment dstSegment; 77 78 private short[] a, b, c; 79 80 @Setup 81 public void setup() { 82 var longSize = size / Short.BYTES; 83 srcArray = new short[longSize]; 84 dstArray = srcArray.clone(); 85 for (int i = 0; i < srcArray.length; i++) { 86 srcArray[i] = (short) i; 87 } 88 89 srcSegmentHeap = MemorySegment.ofArray(new byte[size]); 90 dstSegmentHeap = MemorySegment.ofArray(new byte[size]); 91 92 srcSegment = Arena.ofAuto().allocate(size, SPECIES.vectorByteSize()); 93 dstSegment = Arena.ofAuto().allocate(size, SPECIES.vectorByteSize()); 94 95 this.longSize = longSize; 96 97 a = new short[size]; 98 b = new short[size]; 99 c = new short[size]; 100 } 101 102 @Benchmark 103 public void array() { 104 for (int i = 0; i < SPECIES.loopBound(srcArray.length); i += SPECIES.length()) { 105 var v = ShortVector.fromArray(SPECIES, srcArray, i); 106 v.intoArray(dstArray, i); 107 } 108 } 109 110 @Benchmark 111 public void vectAdd1() { 112 var a = this.a; 113 var b = this.b; 114 var c = this.c; 115 116 for (int i = 0; i < a.length; i += SPECIES.length()) { 117 ShortVector av = ShortVector.fromArray(SPECIES, a, i); 118 ShortVector bv = ShortVector.fromArray(SPECIES, b, i); 119 av.lanewise(VectorOperators.ADD, bv).intoArray(c, i); 120 } 121 } 122 123 @Benchmark 124 public void vectAdd2() { 125 var a = this.a; 126 var b = this.b; 127 var c = this.c; 128 129 for (int i = 0; i < a.length/SPECIES.length(); i++) { 130 ShortVector av = ShortVector.fromArray(SPECIES, a, (i*SPECIES.length())); 131 ShortVector bv = ShortVector.fromArray(SPECIES, b, (i*SPECIES.length())); 132 av.lanewise(VectorOperators.ADD, bv).intoArray(c, (i*SPECIES.length())); 133 } 134 } 135 136 @Benchmark 137 public void arrayAdd() { 138 for (int i = 0; i < SPECIES.loopBound(srcArray.length); i += SPECIES.length()) { 139 var v = ShortVector.fromArray(SPECIES, srcArray, i); 140 v = v.add(v); 141 v.intoArray(dstArray, i); 142 } 143 } 144 145 @Benchmark 146 public void heapSegment() { 147 for (long i = 0; i < SPECIES.loopBound(longSize); i += SPECIES.length()) { 148 var v = ShortVector.fromMemorySegment(SPECIES, srcSegmentHeap, i, ByteOrder.nativeOrder()); 149 v.intoMemorySegment(dstSegmentHeap, i, ByteOrder.nativeOrder()); 150 } 151 } 152 153 @Benchmark 154 public void segmentNativeImplicit() { 155 for (long i = 0; i < SPECIES.loopBound(srcArray.length); i += SPECIES.length()) { 156 var v = ShortVector.fromMemorySegment(SPECIES, srcSegment, i, ByteOrder.nativeOrder()); 157 v.intoMemorySegment(dstSegment, i, ByteOrder.nativeOrder()); 158 } 159 } 160 161 @Benchmark 162 public void segmentNativeConfined() { 163 try (final var arena = Arena.ofConfined()) { 164 final var srcSegmentConfined = srcSegment.reinterpret(arena, null); 165 final var dstSegmentConfined = dstSegment.reinterpret(arena, null); 166 167 for (long i = 0; i < SPECIES.loopBound(srcArray.length); i += SPECIES.length()) { 168 var v = ShortVector.fromMemorySegment(SPECIES, srcSegmentConfined, i, ByteOrder.nativeOrder()); 169 v.intoMemorySegment(dstSegmentConfined, i, ByteOrder.nativeOrder()); 170 } 171 } 172 } 173 }