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-native-access", "ALL-UNNAMED"})
 54 public class MemorySegmentVectorAccess {
 55   private static final VectorSpecies<Byte> SPECIES = VectorSpecies.ofLargestShape(byte.class);
 56 
 57   @Param("1024")
 58   private int size;
 59 
 60   byte[] byteIn;
 61   byte[] byteOut;
 62 
 63   MemorySegment nativeIn, nativeOut;
 64   MemorySegment heapIn, heapOut;
 65 
 66   MemorySegment nativeInRo, nativeOutRo;
 67   MemorySegment heapInRo, heapOutRo;
 68 
 69   @Setup
 70   public void setup() {
 71       Arena scope1 = Arena.ofAuto();
 72       nativeIn = scope1.allocate(size, 1);
 73       Arena scope = Arena.ofAuto();
 74       nativeOut = scope.allocate(size, 1);
 75 
 76     byteIn = new byte[size];
 77     byteOut = new byte[size];
 78 
 79     heapIn = MemorySegment.ofArray(byteIn);
 80     heapOut = MemorySegment.ofArray(byteOut);
 81 
 82     nativeInRo = nativeIn.asReadOnly();
 83     nativeOutRo = nativeOut.asReadOnly();
 84 
 85     heapInRo = heapIn.asReadOnly();
 86     heapOutRo = heapOut.asReadOnly();
 87   }
 88 
 89   @Benchmark
 90   public void directSegments() {
 91     copyMemory(nativeIn, nativeOut);
 92   }
 93 
 94   @Benchmark
 95   public void heapSegments() {
 96     copyMemory(heapIn, heapOut);
 97   }
 98 
 99   @Benchmark
100   public void pollutedSegments2() {
101     copyIntoNotInlined(nativeIn, nativeOut);
102     copyIntoNotInlined(heapIn, heapOut);
103   }
104 
105   @Benchmark
106   public void pollutedSegments3() {
107     copyIntoNotInlined(nativeIn, nativeOut);
108     copyIntoNotInlined(heapIn, heapOut);
109 
110     copyIntoNotInlined(nativeInRo, nativeOut);
111     copyIntoNotInlined(heapInRo, heapOut);
112   }
113 
114   @Benchmark
115   public void pollutedSegments4() {
116     copyIntoNotInlined(nativeIn, heapOut); // Pollute if unswitch on 2nd param
117     copyIntoNotInlined(heapIn, heapOut);
118 
119     copyIntoNotInlined(heapIn, nativeIn); // Pollute if unswitch on 1st param
120     copyIntoNotInlined(heapIn, nativeOut);
121   }
122 
123 
124   boolean readOnlyException;
125 
126   @Benchmark
127   public void pollutedSegments5() {
128     copyIntoNotInlined(nativeIn, heapOut);
129     copyIntoNotInlined(heapIn, heapOut);
130 
131     copyIntoNotInlined(heapIn, nativeIn);
132     copyIntoNotInlined(heapIn, nativeOut);
133 
134     if (readOnlyException) {
135       try {
136         copyIntoNotInlined(heapIn, nativeOutRo);
137       } catch (Exception ignored) {}
138       readOnlyException = !readOnlyException;
139     }
140   }
141 
142   @Benchmark
143   public void arrayCopy() {
144     byte[] in = byteIn;
145     byte[] out = byteOut;
146 
147     for (int i = 0; i < SPECIES.loopBound(in.length); i += SPECIES.vectorByteSize()) {
148       final var v = ByteVector.fromArray(SPECIES, in, i);
149       v.intoArray(out, i);
150     }
151   }
152 
153   @CompilerControl(CompilerControl.Mode.DONT_INLINE)
154   protected void copyIntoNotInlined(MemorySegment in, MemorySegment out) {
155     copyMemory(in, out);
156   }
157 
158   @CompilerControl(CompilerControl.Mode.INLINE)
159   protected void copyMemory(MemorySegment in, MemorySegment out) {
160     for (long i = 0; i < SPECIES.loopBound(in.byteSize()); i += SPECIES.vectorByteSize()) {
161       final var v = ByteVector.fromMemorySegment(SPECIES, in, i, ByteOrder.nativeOrder());
162       v.intoMemorySegment(out, i, ByteOrder.nativeOrder());
163     }
164   }
165 }