< prev index next >

test/micro/org/openjdk/bench/jdk/incubator/foreign/ParallelSum.java

Print this page

  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.foreign;
 25 
 26 import jdk.incubator.foreign.MemoryLayout;
 27 import jdk.incubator.foreign.MemoryLayouts;
 28 import jdk.incubator.foreign.ResourceScope;
 29 import jdk.incubator.foreign.SequenceLayout;

 30 import sun.misc.Unsafe;
 31 import org.openjdk.jmh.annotations.Benchmark;
 32 import org.openjdk.jmh.annotations.BenchmarkMode;
 33 import org.openjdk.jmh.annotations.Fork;
 34 import org.openjdk.jmh.annotations.Measurement;
 35 import org.openjdk.jmh.annotations.Mode;
 36 import org.openjdk.jmh.annotations.OutputTimeUnit;
 37 import org.openjdk.jmh.annotations.Setup;
 38 import org.openjdk.jmh.annotations.State;
 39 import org.openjdk.jmh.annotations.TearDown;
 40 import org.openjdk.jmh.annotations.Warmup;
 41 
 42 import jdk.incubator.foreign.MemorySegment;
 43 import java.lang.invoke.VarHandle;
 44 import java.util.LinkedList;
 45 import java.util.List;
 46 import java.util.Optional;
 47 import java.util.Spliterator;
 48 import java.util.concurrent.CountedCompleter;
 49 import java.util.concurrent.RecursiveTask;
 50 import java.util.concurrent.TimeUnit;
 51 import java.util.function.Predicate;
 52 import java.util.function.ToIntFunction;
 53 
 54 import static jdk.incubator.foreign.MemoryLayout.PathElement.sequenceElement;
 55 import static jdk.incubator.foreign.MemoryLayouts.JAVA_INT;
 56 
 57 @BenchmarkMode(Mode.AverageTime)
 58 @Warmup(iterations = 5, time = 500, timeUnit = TimeUnit.MILLISECONDS)
 59 @Measurement(iterations = 10, time = 500, timeUnit = TimeUnit.MILLISECONDS)
 60 @State(org.openjdk.jmh.annotations.Scope.Thread)
 61 @OutputTimeUnit(TimeUnit.MILLISECONDS)
 62 @Fork(value = 3, jvmArgsAppend = { "--add-modules=jdk.incubator.foreign" })
 63 public class ParallelSum {
 64 
 65     final static int CARRIER_SIZE = 4;
 66     final static int ALLOC_SIZE = CARRIER_SIZE * 1024 * 1024 * 256;
 67     final static int ELEM_SIZE = ALLOC_SIZE / CARRIER_SIZE;
 68     static final VarHandle VH_int = MemoryLayout.sequenceLayout(JAVA_INT).varHandle(int.class, sequenceElement());
 69 
 70     final static MemoryLayout ELEM_LAYOUT = MemoryLayouts.JAVA_INT;
 71     final static int BULK_FACTOR = 512;
 72     final static SequenceLayout ELEM_LAYOUT_BULK = MemoryLayout.sequenceLayout(BULK_FACTOR, ELEM_LAYOUT);
 73 
 74     static final Unsafe unsafe = Utils.unsafe;
 75 
 76     MemorySegment segment;
 77     long address;
 78 
 79     @Setup
 80     public void setup() {
 81         address = unsafe.allocateMemory(ALLOC_SIZE);
 82         for (int i = 0; i < ELEM_SIZE; i++) {
 83             unsafe.putInt(address + (i * CARRIER_SIZE), i);
 84         }
 85         segment = MemorySegment.allocateNative(ALLOC_SIZE, CARRIER_SIZE, ResourceScope.newSharedScope());
 86         for (int i = 0; i < ELEM_SIZE; i++) {
 87             VH_int.set(segment, (long) i, i);
 88         }
 89     }
 90 

  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.foreign;
 25 
 26 import jdk.incubator.foreign.MemoryLayout;

 27 import jdk.incubator.foreign.ResourceScope;
 28 import jdk.incubator.foreign.SequenceLayout;
 29 import jdk.incubator.foreign.ValueLayout;
 30 import sun.misc.Unsafe;
 31 import org.openjdk.jmh.annotations.Benchmark;
 32 import org.openjdk.jmh.annotations.BenchmarkMode;
 33 import org.openjdk.jmh.annotations.Fork;
 34 import org.openjdk.jmh.annotations.Measurement;
 35 import org.openjdk.jmh.annotations.Mode;
 36 import org.openjdk.jmh.annotations.OutputTimeUnit;
 37 import org.openjdk.jmh.annotations.Setup;
 38 import org.openjdk.jmh.annotations.State;
 39 import org.openjdk.jmh.annotations.TearDown;
 40 import org.openjdk.jmh.annotations.Warmup;
 41 
 42 import jdk.incubator.foreign.MemorySegment;
 43 import java.lang.invoke.VarHandle;
 44 import java.util.LinkedList;
 45 import java.util.List;
 46 import java.util.Optional;
 47 import java.util.Spliterator;
 48 import java.util.concurrent.CountedCompleter;
 49 import java.util.concurrent.RecursiveTask;
 50 import java.util.concurrent.TimeUnit;
 51 import java.util.function.Predicate;
 52 import java.util.function.ToIntFunction;
 53 
 54 import static jdk.incubator.foreign.MemoryLayout.PathElement.sequenceElement;
 55 import static jdk.incubator.foreign.ValueLayout.JAVA_INT;
 56 
 57 @BenchmarkMode(Mode.AverageTime)
 58 @Warmup(iterations = 5, time = 500, timeUnit = TimeUnit.MILLISECONDS)
 59 @Measurement(iterations = 10, time = 500, timeUnit = TimeUnit.MILLISECONDS)
 60 @State(org.openjdk.jmh.annotations.Scope.Thread)
 61 @OutputTimeUnit(TimeUnit.MILLISECONDS)
 62 @Fork(value = 3, jvmArgsAppend = { "--add-modules=jdk.incubator.foreign" })
 63 public class ParallelSum {
 64 
 65     final static int CARRIER_SIZE = 4;
 66     final static int ALLOC_SIZE = CARRIER_SIZE * 1024 * 1024 * 256;
 67     final static int ELEM_SIZE = ALLOC_SIZE / CARRIER_SIZE;
 68     static final VarHandle VH_int = MemoryLayout.sequenceLayout(JAVA_INT).varHandle(sequenceElement());
 69 
 70     final static MemoryLayout ELEM_LAYOUT = ValueLayout.JAVA_INT;
 71     final static int BULK_FACTOR = 512;
 72     final static SequenceLayout ELEM_LAYOUT_BULK = MemoryLayout.sequenceLayout(BULK_FACTOR, ELEM_LAYOUT);
 73 
 74     static final Unsafe unsafe = Utils.unsafe;
 75 
 76     MemorySegment segment;
 77     long address;
 78 
 79     @Setup
 80     public void setup() {
 81         address = unsafe.allocateMemory(ALLOC_SIZE);
 82         for (int i = 0; i < ELEM_SIZE; i++) {
 83             unsafe.putInt(address + (i * CARRIER_SIZE), i);
 84         }
 85         segment = MemorySegment.allocateNative(ALLOC_SIZE, CARRIER_SIZE, ResourceScope.newSharedScope());
 86         for (int i = 0; i < ELEM_SIZE; i++) {
 87             VH_int.set(segment, (long) i, i);
 88         }
 89     }
 90 
< prev index next >