# ######################################################################
# 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
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();
}
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();
}
}
--- /dev/null
+/*
+ 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();
+ }
+
+ }
+}