From 1dc400ad4371a9900f7c8c0d16aceff10f4ce28c Mon Sep 17 00:00:00 2001 From: Not Zed Date: Tue, 28 Jan 2020 14:39:13 +1030 Subject: [PATCH] memory tests --- Makefile | 8 +- .../au/notzed/zcl/test/TestMemory.java | 17 +-- .../au/notzed/zcl/test/TestMemoryLong.java | 110 ++++++++++++++++++ 3 files changed, 117 insertions(+), 18 deletions(-) create mode 100644 src/notzed.zcl.demo/classes/au/notzed/zcl/test/TestMemoryLong.java diff --git a/Makefile b/Makefile index 7acaae1..0145faf 100644 --- 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 diff --git a/src/notzed.zcl.demo/classes/au/notzed/zcl/test/TestMemory.java b/src/notzed.zcl.demo/classes/au/notzed/zcl/test/TestMemory.java index 24a2414..9a5145a 100644 --- a/src/notzed.zcl.demo/classes/au/notzed/zcl/test/TestMemory.java +++ b/src/notzed.zcl.demo/classes/au/notzed/zcl/test/TestMemory.java @@ -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 index 0000000..55e1c50 --- /dev/null +++ b/src/notzed.zcl.demo/classes/au/notzed/zcl/test/TestMemoryLong.java @@ -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(); + } + + } +} -- 2.39.5