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