memory tests foreign-jextract
authorNot Zed <notzed@gmail.com>
Tue, 28 Jan 2020 04:09:13 +0000 (14:39 +1030)
committerNot Zed <notzed@gmail.com>
Tue, 28 Jan 2020 04:09:13 +0000 (14:39 +1030)
Makefile
src/notzed.zcl.demo/classes/au/notzed/zcl/test/TestMemory.java
src/notzed.zcl.demo/classes/au/notzed/zcl/test/TestMemoryLong.java [new file with mode: 0644]

index 7acaae1..0145faf 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -21,10 +21,12 @@ include java.make
 # ######################################################################
 # Work in progress idea for java.make extension to create execution templates
 
+test_demos := $(subst /,.,$(subst .java,,$(shell find src/notzed.zcl.demo/classes -name 'Test*.java' -printf '%P\n')))
+$(info $(test_demos))
+
 notzed.zcl.demo_DEMOS=au.notzed.zcl.tools.clinfo \
-       au.notzed.zcl.test.TestObjects \
-       au.notzed.zcl.test.TestCopies \
-       au.notzed.zcl.test.TestMemory
+       $(test_demos)
+
 notzed.zcl.fxdemo_DEMOS=fxdemo.fract.Mandelbrot fxdemo.fract.Test
 
 DEMOFLAGS=--add-exports jdk.incubator.foreign/jdk.incubator.foreign.unsafe=notzed.zcl
index 24a2414..9a5145a 100644 (file)
@@ -16,7 +16,7 @@ import jdk.incubator.foreign.MemorySegment;
 public class TestMemory {
 
        static void check(MemorySegment seg) {
-               int len = (int)(seg.byteSize() >>> 3);
+               int len = (int)(seg.byteSize() >>> 2);
                float sum = 0;
 
                MemoryAddress add = seg.baseAddress();
@@ -103,20 +103,7 @@ public class TestMemory {
                        }
                        System.out.printf(" %12.9f bb over segment\n", (System.nanoTime() - now) * 1E-9);
 
-                                               now = System.nanoTime();
-                       for (int l = 0; l < 1000; l++) {
-                               for (MemorySegment a: segments)
-                                       check(a);
-                       }
-                       System.out.printf(" %12.9f segment\n", (System.nanoTime() - now) * 1E-9);
-
-                                               now = System.nanoTime();
-                       for (int l = 0; l < 1000; l++) {
-                               for (MemorySegment a: segments)
-                                       check(a);
-                       }
-                       System.out.printf(" %12.9f segment\n", (System.nanoTime() - now) * 1E-9);
-
+                       System.out.println();
                }
 
        }
diff --git a/src/notzed.zcl.demo/classes/au/notzed/zcl/test/TestMemoryLong.java b/src/notzed.zcl.demo/classes/au/notzed/zcl/test/TestMemoryLong.java
new file mode 100644 (file)
index 0000000..55e1c50
--- /dev/null
@@ -0,0 +1,110 @@
+/*
+  License: public domain or equivalent.
+ */
+package au.notzed.zcl.test;
+
+import api.Native;
+import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
+import java.nio.LongBuffer;
+import jdk.incubator.foreign.MemoryAddress;
+import jdk.incubator.foreign.MemorySegment;
+
+/**
+ * Some memory tests.
+ */
+public class TestMemoryLong {
+
+       static void check(MemorySegment seg) {
+               int len = (int)(seg.byteSize() >>> 3);
+               long sum = 0;
+
+               MemoryAddress add = seg.baseAddress();
+               for (int i = 0; i < len; i++)
+                       sum += Native.getLong(add, i);
+               Native.setLong(add, sum);
+       }
+
+       static void check(long[] seg) {
+               long sum = 0;
+               for (int i = 0; i < seg.length; i++)
+                       sum += seg[i];
+               seg[0] = sum;
+       }
+
+       static void check(LongBuffer seg) {
+               long sum = 0;
+
+               while (seg.hasRemaining())
+                       sum += seg.get();
+               seg.rewind();
+               seg.put(0, sum);
+       }
+
+       static void check2(LongBuffer seg) {
+               long sum = 0;
+               int len = seg.limit();
+
+               for (int i = 0; i < len; i++)
+                       sum += seg.get(i);
+               seg.put(0, sum);
+       }
+
+       public static void main(String[] args) {
+               int[] sizes = {
+                       1024,
+                       1024 * 1024,
+                       16,
+                       64,
+                       31,
+                       17
+               };
+               long[][] arrays = new long[sizes.length][];
+               MemorySegment segments[] = new MemorySegment[sizes.length];
+               ByteBuffer[] buffers = new ByteBuffer[sizes.length];
+
+               for (int i = 0; i < sizes.length; i++) {
+                       arrays[i] = new long[sizes[i]];
+                       segments[i] = MemorySegment.allocateNative(sizes[i] * 8, 16);
+                       buffers[i] = ByteBuffer.allocateDirect(sizes[i] * 8).order(ByteOrder.nativeOrder());
+               }
+
+               for (int c = 0; c < 10; c++) {
+                       long now;
+
+                       now = System.nanoTime();
+                       for (int l = 0; l < 1000; l++) {
+                               for (long[] a: arrays)
+                                       check(a);
+                       }
+                       System.out.printf(" %12.9f array\n", (System.nanoTime() - now) * 1E-9);
+                       now = System.nanoTime();
+                       for (int l = 0; l < 1000; l++) {
+                               for (ByteBuffer a: buffers)
+                                       check(a.asLongBuffer());
+                       }
+                       System.out.printf(" %12.9f bb stream\n", (System.nanoTime() - now) * 1E-9);
+                       now = System.nanoTime();
+                       for (int l = 0; l < 1000; l++) {
+                               for (MemorySegment a: segments)
+                                       check(a);
+                       }
+                       System.out.printf(" %12.9f segment\n", (System.nanoTime() - now) * 1E-9);
+                       now = System.nanoTime();
+                       for (int l = 0; l < 1000; l++) {
+                               for (ByteBuffer a: buffers)
+                                       check2(a.asLongBuffer());
+                       }
+                       System.out.printf(" %12.9f bb index\n", (System.nanoTime() - now) * 1E-9);
+                       now = System.nanoTime();
+                       for (int l = 0; l < 1000; l++) {
+                               for (MemorySegment a: segments)
+                                       check(a.asByteBuffer().order(ByteOrder.nativeOrder()).asLongBuffer());
+                       }
+                       System.out.printf(" %12.9f bb over segment\n", (System.nanoTime() - now) * 1E-9);
+
+                       System.out.println();
+               }
+
+       }
+}