1 /*
   2  * Copyright (c) 2018, 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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  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
  23  * questions.
  24  */
  25 package benchmark.jdk.incubator.vector;
  26 
  27 import jdk.incubator.vector.*;
  28 import jdk.incubator.vector.VectorSpecies;
  29 import org.openjdk.jmh.annotations.*;
  30 
  31 import java.util.concurrent.TimeUnit;
  32 
  33 import static org.junit.jupiter.api.Assertions.*;
  34 
  35 @BenchmarkMode(Mode.Throughput)
  36 @Warmup(iterations = 3, time = 1)
  37 @Measurement(iterations = 5, time = 1)
  38 @OutputTimeUnit(TimeUnit.MILLISECONDS)
  39 @State(Scope.Benchmark)
  40 @Fork(value = 1, jvmArgsPrepend = {"--add-modules=jdk.incubator.vector"})
  41 public class Merge extends AbstractVectorBenchmark {
  42 
  43     @Param({"64", "1024", "65536"})
  44     int size;
  45 
  46     int[] in, out;
  47 
  48     @Setup
  49     public void setup() {
  50         size = size + (size % 64); // FIXME: process tails
  51         in  = new int[size];
  52         out = new int[size];
  53         for (int i = 0; i < size; i++) {
  54             in[i] = i;
  55         }
  56     }
  57 
  58     @Benchmark
  59     public void merge64_128() {
  60         merge(I64, I128);
  61     }
  62 
  63     @Benchmark
  64     public void merge128_256() {
  65         merge(I128, I256);
  66     }
  67 
  68     @Benchmark
  69     public void merge256_512() {
  70         merge(I256, I512);
  71     }
  72 
  73     @Benchmark
  74     public void merge64_256() {
  75         merge(I64, I256);
  76     }
  77 
  78     @Benchmark
  79     public void merge128_512() {
  80         merge(I128, I512);
  81     }
  82 
  83     @Benchmark
  84     public void merge64_512() {
  85         merge(I64, I256);
  86     }
  87 
  88     IntVector merge(VectorSpecies<Integer> from, VectorSpecies<Integer> to, int idx) {
  89         assert from.length() <= to.length();
  90 
  91         int vlenFrom = from.length();
  92         int vlenTo   =   to.length();
  93 
  94         if (vlenFrom == vlenTo) {
  95             return IntVector.fromArray(from, in, idx);
  96         } else {
  97             var stepDown = narrow(to);
  98             int mid = stepDown.length();
  99             var lo = merge(from, stepDown, idx);
 100             var hi = merge(from, stepDown, idx + mid);
 101             return join(stepDown, to, lo, hi);
 102         }
 103     }
 104 
 105 
 106     void merge(VectorSpecies<Integer> from, VectorSpecies<Integer> to) {
 107         int vlenTo = to.length();
 108         for (int i = 0; i < in.length; i += vlenTo) {
 109             var r = merge(from, to, i);
 110             r.intoArray(out, i);
 111         }
 112     }
 113 
 114     @TearDown
 115     public void tearDown() {
 116         assertArrayEquals(in, out);
 117     }
 118 }