1 /*
  2  *  Copyright (c) 2021, 2022, Oracle and/or its affiliates. All rights reserved.
  3  *  Copyright (c) 2021, Rado Smogura. All rights reserved.
  4  *
  5  *  DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  6  *
  7  *  This code is free software; you can redistribute it and/or modify it
  8  *  under the terms of the GNU General Public License version 2 only, as
  9  *  published by the Free Software Foundation.
 10  *
 11  *  This code is distributed in the hope that it will be useful, but WITHOUT
 12  *  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 13  *  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 14  *  version 2 for more details (a copy is included in the LICENSE file that
 15  *  accompanied this code).
 16  *
 17  *  You should have received a copy of the GNU General Public License version
 18  *  2 along with this work; if not, write to the Free Software Foundation,
 19  *  Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 20  *
 21  *  Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 22  *  or visit www.oracle.com if you need additional information or have any
 23  *  questions.
 24  *
 25  */
 26 package org.openjdk.bench.jdk.incubator.vector;
 27 
 28 import java.lang.foreign.MemorySegment;
 29 import java.lang.foreign.Arena;
 30 import java.nio.ByteOrder;
 31 import java.util.concurrent.TimeUnit;
 32 import jdk.incubator.vector.ByteVector;
 33 import jdk.incubator.vector.VectorSpecies;
 34 import org.openjdk.jmh.annotations.Benchmark;
 35 import org.openjdk.jmh.annotations.BenchmarkMode;
 36 import org.openjdk.jmh.annotations.CompilerControl;
 37 import org.openjdk.jmh.annotations.Fork;
 38 import org.openjdk.jmh.annotations.Measurement;
 39 import org.openjdk.jmh.annotations.Mode;
 40 import org.openjdk.jmh.annotations.OutputTimeUnit;
 41 import org.openjdk.jmh.annotations.Param;
 42 import org.openjdk.jmh.annotations.Setup;
 43 import org.openjdk.jmh.annotations.State;
 44 import org.openjdk.jmh.annotations.Warmup;
 45 
 46 @BenchmarkMode(Mode.AverageTime)
 47 @Warmup(iterations = 5, time = 500, timeUnit = TimeUnit.MILLISECONDS)
 48 @Measurement(iterations = 10, time = 500, timeUnit = TimeUnit.MILLISECONDS)
 49 @State(org.openjdk.jmh.annotations.Scope.Thread)
 50 @OutputTimeUnit(TimeUnit.NANOSECONDS)
 51 @Fork(value = 1, jvmArgsAppend = {
 52     "--add-modules=jdk.incubator.vector",
 53     "--enable-preview",
 54     "--enable-native-access", "ALL-UNNAMED"})
 55 public class MemorySegmentVectorAccess {
 56   private static final VectorSpecies<Byte> SPECIES = VectorSpecies.ofLargestShape(byte.class);
 57 
 58   @Param("1024")
 59   private int size;
 60 
 61   byte[] byteIn;
 62   byte[] byteOut;
 63 
 64   MemorySegment nativeIn, nativeOut;
 65   MemorySegment heapIn, heapOut;
 66 
 67   MemorySegment nativeInRo, nativeOutRo;
 68   MemorySegment heapInRo, heapOutRo;
 69 
 70   @Setup
 71   public void setup() {
 72       Arena scope1 = Arena.ofAuto();
 73       nativeIn = scope1.allocate(size, 1);
 74       Arena scope = Arena.ofAuto();
 75       nativeOut = scope.allocate(size, 1);
 76 
 77     byteIn = new byte[size];
 78     byteOut = new byte[size];
 79 
 80     heapIn = MemorySegment.ofArray(byteIn);
 81     heapOut = MemorySegment.ofArray(byteOut);
 82 
 83     nativeInRo = nativeIn.asReadOnly();
 84     nativeOutRo = nativeOut.asReadOnly();
 85 
 86     heapInRo = heapIn.asReadOnly();
 87     heapOutRo = heapOut.asReadOnly();
 88   }
 89 
 90   @Benchmark
 91   public void directSegments() {
 92     copyMemory(nativeIn, nativeOut);
 93   }
 94 
 95   @Benchmark
 96   public void heapSegments() {
 97     copyMemory(heapIn, heapOut);
 98   }
 99 
100   @Benchmark
101   public void pollutedSegments2() {
102     copyIntoNotInlined(nativeIn, nativeOut);
103     copyIntoNotInlined(heapIn, heapOut);
104   }
105 
106   @Benchmark
107   public void pollutedSegments3() {
108     copyIntoNotInlined(nativeIn, nativeOut);
109     copyIntoNotInlined(heapIn, heapOut);
110 
111     copyIntoNotInlined(nativeInRo, nativeOut);
112     copyIntoNotInlined(heapInRo, heapOut);
113   }
114 
115   @Benchmark
116   public void pollutedSegments4() {
117     copyIntoNotInlined(nativeIn, heapOut); // Pollute if unswitch on 2nd param
118     copyIntoNotInlined(heapIn, heapOut);
119 
120     copyIntoNotInlined(heapIn, nativeIn); // Pollute if unswitch on 1st param
121     copyIntoNotInlined(heapIn, nativeOut);
122   }
123 
124 
125   boolean readOnlyException;
126 
127   @Benchmark
128   public void pollutedSegments5() {
129     copyIntoNotInlined(nativeIn, heapOut);
130     copyIntoNotInlined(heapIn, heapOut);
131 
132     copyIntoNotInlined(heapIn, nativeIn);
133     copyIntoNotInlined(heapIn, nativeOut);
134 
135     if (readOnlyException) {
136       try {
137         copyIntoNotInlined(heapIn, nativeOutRo);
138       } catch (Exception ignored) {}
139       readOnlyException = !readOnlyException;
140     }
141   }
142 
143   @Benchmark
144   public void arrayCopy() {
145     byte[] in = byteIn;
146     byte[] out = byteOut;
147 
148     for (int i = 0; i < SPECIES.loopBound(in.length); i += SPECIES.vectorByteSize()) {
149       final var v = ByteVector.fromArray(SPECIES, in, i);
150       v.intoArray(out, i);
151     }
152   }
153 
154   @CompilerControl(CompilerControl.Mode.DONT_INLINE)
155   protected void copyIntoNotInlined(MemorySegment in, MemorySegment out) {
156     copyMemory(in, out);
157   }
158 
159   @CompilerControl(CompilerControl.Mode.INLINE)
160   protected void copyMemory(MemorySegment in, MemorySegment out) {
161     for (long i = 0; i < SPECIES.loopBound(in.byteSize()); i += SPECIES.vectorByteSize()) {
162       final var v = ByteVector.fromMemorySegment(SPECIES, in, i, ByteOrder.nativeOrder());
163       v.intoMemorySegment(out, i, ByteOrder.nativeOrder());
164     }
165   }
166 }