From 6ebcd3d033a8124289662890293a985884aef1ce Mon Sep 17 00:00:00 2001 From: Yang Zhang Date: Fri, 26 Aug 2016 13:52:30 +0800 Subject: Add samples for basic SIMD instructions performance Add the test cases as follows: 1. Vector copy for int/short/byte 2. Vector add for int/short/byte 3. Vector mul for int/short/byte Change-Id: I2b0f9324c029efeb61387f0620d59792c8aeb5aa --- .../java/org/linaro/benchmarks/TestSimdAdd.java | 74 ++++++++++++++++++++++ .../java/org/linaro/benchmarks/TestSimdCopy.java | 68 ++++++++++++++++++++ .../java/org/linaro/benchmarks/TestSimdMul.java | 74 ++++++++++++++++++++++ 3 files changed, 216 insertions(+) create mode 100644 src/main/java/org/linaro/benchmarks/TestSimdAdd.java create mode 100644 src/main/java/org/linaro/benchmarks/TestSimdCopy.java create mode 100644 src/main/java/org/linaro/benchmarks/TestSimdMul.java diff --git a/src/main/java/org/linaro/benchmarks/TestSimdAdd.java b/src/main/java/org/linaro/benchmarks/TestSimdAdd.java new file mode 100644 index 0000000..18082d0 --- /dev/null +++ b/src/main/java/org/linaro/benchmarks/TestSimdAdd.java @@ -0,0 +1,74 @@ +package org.linaro.benchmarks; + +import org.openjdk.jmh.annotations.*; +import java.util.concurrent.TimeUnit; + +@BenchmarkMode(Mode.AverageTime) +@OutputTimeUnit(TimeUnit.MICROSECONDS) +@State(Scope.Benchmark) + +public class TestSimdAdd { + static final int LENGTH = 1024 * 256; + static int [] a = new int[LENGTH]; + static int [] b = new int[LENGTH]; + static int [] c = new int[LENGTH]; + static short [] sa = new short[LENGTH]; + static short [] sb = new short[LENGTH]; + static short [] sc = new short[LENGTH]; + static byte [] ba = new byte[LENGTH]; + static byte [] bb = new byte[LENGTH]; + static byte [] bc = new byte[LENGTH]; + public static void TestSimdAddInit() { + for (int i = 0; i < LENGTH; i++) { + a[i] = i + 3; + b[i] = i + 2; + c[i] = i + 1; + sa[i] = (short)(i + 3); + sb[i] = (short)(i + 2); + sc[i] = (short)(i + 1); + ba[i] = (byte)(i + 3); + bb[i] = (byte)(i + 2); + bc[i] = (byte)(i + 1); + } + } + + public static void vectAddInt() { + for (int i = 0; i < LENGTH; i++) { + c[i] = a[i] + b[i]; + } + } + + public static void vectAddShort() { + for (int i = 0; i < LENGTH; i++) { + sc[i] = (short)(sa[i] + sb[i]); + } + } + + public static void vectAddByte() { + for (int i = 0; i < LENGTH; i++) { + bc[i] = (byte)(ba[i] + bb[i]); + } + } + + @Setup + public void setup() + { + TestSimdAddInit(); + } + + @Benchmark + public void testVectAddByte() { + vectAddByte(); + } + + @Benchmark + public void testVectAddShort() { + vectAddShort(); + } + + @Benchmark + public void testVectAddInt() { + vectAddInt(); + } + +} diff --git a/src/main/java/org/linaro/benchmarks/TestSimdCopy.java b/src/main/java/org/linaro/benchmarks/TestSimdCopy.java new file mode 100644 index 0000000..43fe27c --- /dev/null +++ b/src/main/java/org/linaro/benchmarks/TestSimdCopy.java @@ -0,0 +1,68 @@ +package org.linaro.benchmarks; + +import org.openjdk.jmh.annotations.*; +import java.util.concurrent.TimeUnit; + +@BenchmarkMode(Mode.AverageTime) +@OutputTimeUnit(TimeUnit.MICROSECONDS) +@State(Scope.Benchmark) + +public class TestSimdCopy { + static final int LENGTH = 1024 * 256; + static int [] a = new int[LENGTH]; + static int [] c = new int[LENGTH]; + static short [] sa = new short[LENGTH]; + static short [] sc = new short[LENGTH]; + static byte [] ba = new byte[LENGTH]; + static byte [] bc = new byte[LENGTH]; + public static void TestSimdCopyInit() { + for (int i = 0; i < LENGTH; i++) { + a[i] = i + 3; + c[i] = i + 1; + sa[i] = (short)(i + 3); + sc[i] = (short)(i + 1); + ba[i] = (byte)(i + 3); + bc[i] = (byte)(i + 1); + } + } + + public static void vectCopyInt() { + for (int i = 0; i < LENGTH; i++) { + c[i] = a[i]; + } + } + + public static void vectCopyShort() { + for (int i = 0; i < LENGTH; i++) { + sc[i] = sa[i]; + } + } + + public static void vectCopyByte() { + for (int i = 0; i < LENGTH; i++) { + bc[i] = ba[i]; + } + } + + @Setup + public void setup() + { + TestSimdCopyInit(); + } + + @Benchmark + public void testVectCopyByte() { + vectCopyByte(); + } + + @Benchmark + public void testVectCopyShort() { + vectCopyShort(); + } + + @Benchmark + public void testVectCopyInt() { + vectCopyInt(); + } + +} diff --git a/src/main/java/org/linaro/benchmarks/TestSimdMul.java b/src/main/java/org/linaro/benchmarks/TestSimdMul.java new file mode 100644 index 0000000..e12b204 --- /dev/null +++ b/src/main/java/org/linaro/benchmarks/TestSimdMul.java @@ -0,0 +1,74 @@ +package org.linaro.benchmarks; + +import org.openjdk.jmh.annotations.*; +import java.util.concurrent.TimeUnit; + +@BenchmarkMode(Mode.AverageTime) +@OutputTimeUnit(TimeUnit.MICROSECONDS) +@State(Scope.Benchmark) + +public class TestSimdMul { + static final int LENGTH = 1024 * 256; + static int [] a = new int[LENGTH]; + static int [] b = new int[LENGTH]; + static int [] c = new int[LENGTH]; + static short [] sa = new short[LENGTH]; + static short [] sb = new short[LENGTH]; + static short [] sc = new short[LENGTH]; + static byte [] ba = new byte[LENGTH]; + static byte [] bb = new byte[LENGTH]; + static byte [] bc = new byte[LENGTH]; + public static void TestSimdMulInit() { + for (int i = 0; i < LENGTH; i++) { + a[i] = i + 3; + b[i] = i + 2; + c[i] = i + 1; + sa[i] = (short)(i + 3); + sb[i] = (short)(i + 2); + sc[i] = (short)(i + 1); + ba[i] = (byte)(i + 3); + bb[i] = (byte)(i + 2); + bc[i] = (byte)(i + 1); + } + } + + public static void vectMulInt() { + for (int i = 0; i < LENGTH; i++) { + c[i] = a[i] * b[i]; + } + } + + public static void vectMulShort() { + for (int i = 0; i < LENGTH; i++) { + sc[i] = (short)(sa[i] * sb[i]); + } + } + + public static void vectMulByte() { + for (int i = 0; i < LENGTH; i++) { + bc[i] = (byte)(ba[i] * bb[i]); + } + } + + @Setup + public void setup() + { + TestSimdMulInit(); + } + + @Benchmark + public void testVectMulByte() { + vectMulByte(); + } + + @Benchmark + public void testVectMulShort() { + vectMulShort(); + } + + @Benchmark + public void tesVectMulInt() { + vectMulInt(); + } + +} -- cgit v1.2.3