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.nio.ByteOrder;
 27 import java.util.concurrent.TimeUnit;
 28 
 29 import jdk.incubator.foreign.MemoryAddress;
 30 import jdk.incubator.foreign.MemorySegment;
 31 import jdk.incubator.foreign.ResourceScope;
 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.foreign,jdk.incubator.vector",
 55     "-Dforeign.restricted=permit",
 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 
 75   private ResourceScope implicitScope;
 76 
 77   private MemorySegment srcSegment;
 78 
 79   private MemorySegment dstSegment;
 80 
 81 
 82   private MemoryAddress srcAddress;
 83 
 84   private MemoryAddress dstAddress;
 85 
 86   private short[] a, b, c;
 87 
 88   @Setup
 89   public void setup() {
 90     var longSize = size / Short.BYTES;
 91     srcArray = new short[longSize];
 92     dstArray = srcArray.clone();
 93     for (int i = 0; i < srcArray.length; i++) {
 94       srcArray[i] = (short) i;
 95     }
 96 
 97     srcSegmentHeap = MemorySegment.ofArray(new byte[size]);
 98     dstSegmentHeap = MemorySegment.ofArray(new byte[size]);
 99 
100     implicitScope = ResourceScope.newImplicitScope();
101     srcSegment = MemorySegment.allocateNative(size, SPECIES.vectorByteSize(), implicitScope);
102     dstSegment = MemorySegment.allocateNative(size, SPECIES.vectorByteSize(), implicitScope);
103 
104     srcAddress = srcSegment.address();
105     dstAddress = dstSegment.address();
106 
107     this.longSize = longSize;
108 
109     a = new short[size];
110     b = new short[size];
111     c = new short[size];
112   }
113 
114   @Benchmark
115   public void array() {
116     for (int i = 0; i < SPECIES.loopBound(srcArray.length); i += SPECIES.length()) {
117       var v = ShortVector.fromArray(SPECIES, srcArray, i);
118       v.intoArray(dstArray, i);
119     }
120   }
121 
122   @Benchmark
123   public void vectAdd1() {
124     var a = this.a;
125     var b = this.b;
126     var c = this.c;
127 
128     for (int i = 0; i < a.length; i += SPECIES.length()) {
129       ShortVector av = ShortVector.fromArray(SPECIES, a, i);
130       ShortVector bv = ShortVector.fromArray(SPECIES, b, i);
131       av.lanewise(VectorOperators.ADD, bv).intoArray(c, i);
132     }
133   }
134 
135   @Benchmark
136   public void vectAdd2() {
137     var a = this.a;
138     var b = this.b;
139     var c = this.c;
140 
141     for (int i = 0; i < a.length/SPECIES.length(); i++) {
142       ShortVector av = ShortVector.fromArray(SPECIES, a, (i*SPECIES.length()));
143       ShortVector bv = ShortVector.fromArray(SPECIES, b, (i*SPECIES.length()));
144       av.lanewise(VectorOperators.ADD, bv).intoArray(c, (i*SPECIES.length()));
145     }
146   }
147 
148   @Benchmark
149   public void arrayAdd() {
150     for (int i = 0; i < SPECIES.loopBound(srcArray.length); i += SPECIES.length()) {
151       var v = ShortVector.fromArray(SPECIES, srcArray, i);
152       v = v.add(v);
153       v.intoArray(dstArray, i);
154     }
155   }
156 
157   @Benchmark
158   public void heapSegment() {
159     for (long i = 0; i < SPECIES.loopBound(longSize); i += SPECIES.length()) {
160       var v = ShortVector.fromMemorySegment(SPECIES, srcSegmentHeap, i, ByteOrder.nativeOrder());
161       v.intoMemorySegment(dstSegmentHeap, i, ByteOrder.nativeOrder());
162     }
163   }
164 
165   @Benchmark
166   public void segmentNativeImplicit() {
167     for (long i = 0; i < SPECIES.loopBound(srcArray.length); i += SPECIES.length()) {
168       var v = ShortVector.fromMemorySegment(SPECIES, srcSegment, i, ByteOrder.nativeOrder());
169       v.intoMemorySegment(dstSegment, i, ByteOrder.nativeOrder());
170     }
171   }
172 
173   @Benchmark
174   public void segmentNativeConfined() {
175     try (final var scope = ResourceScope.newConfinedScope()) {
176       final var srcSegmentConfined = MemorySegment.ofAddress(srcAddress, size, scope);
177       final var dstSegmentConfined = MemorySegment.ofAddress(dstAddress, size, scope);
178 
179       for (long i = 0; i < SPECIES.loopBound(srcArray.length); i += SPECIES.length()) {
180         var v = ShortVector.fromMemorySegment(SPECIES, srcSegmentConfined, i, ByteOrder.nativeOrder());
181         v.intoMemorySegment(dstSegmentConfined, i, ByteOrder.nativeOrder());
182       }
183     }
184   }
185 }