More tests and minor changes.
authorNot Zed <notzed@gmail.com>
Sat, 25 Jan 2020 23:36:19 +0000 (10:06 +1030)
committerNot Zed <notzed@gmail.com>
Sat, 25 Jan 2020 23:36:19 +0000 (10:06 +1030)
src/notzed.zcl/classes/api/Native.java
src/notzed.zcl/classes/au/notzed/zcl/CLContext.java
src/notzed.zcl/classes/au/notzed/zcl/CLMemory.java
src/notzed.zcl/classes/module-info.java
src/notzed.zcl/tests/au/notzed/zcl/CLBufferTest.java [new file with mode: 0644]
src/notzed.zcl/tests/au/notzed/zcl/CLCommandQueueTest.java [new file with mode: 0644]
src/notzed.zcl/tests/au/notzed/zcl/CLProgramTest.java

index 364d262..4260da2 100644 (file)
@@ -101,7 +101,7 @@ public class Native {
                byteHandle.set(p, v);
        }
 
-       public static void setByte(MemoryAddress p, int i, byte v) {
+       public static void setByte(MemoryAddress p, long i, byte v) {
                byteVHandle.set(p, i, v);
        }
 
index fe2779c..564964c 100644 (file)
@@ -346,7 +346,19 @@ public class CLContext extends CLExtendable {
        }
 
        /**
-        * Wraps a full buffer.
+        * Call clCreateBuffer() with sized buffer.
+        *
+        * @param flags
+        * @param hostp must not be null.
+        * @return
+        * @throws CLRuntimeException
+        */
+       public CLBuffer createBuffer(long flags, MemorySegment hostp) throws CLRuntimeException {
+               return createBuffer(flags, hostp.byteSize(), hostp);
+       }
+
+       /**
+        * Call clCreateBuffer() with sized buffer.
         *
         * @param flags
         * @param hostp must not be null. only capacity is honoured.
@@ -403,6 +415,8 @@ public class CLContext extends CLExtendable {
 
        //native public CLBuffer createBuffer(long flags, double[] hostp) throws CLRuntimeException;
 
+       // FIXME: clCreateSubBuffer
+
        /* ********************************************************************** */
 
        public CLImage createImage(long flags, CLImageFormat fmt, CLImageDesc desc) throws CLRuntimeException, UnsupportedOperationException {
index 31f27b8..575d59c 100644 (file)
@@ -172,7 +172,8 @@ public abstract class CLMemory extends CLObject {
        }
 
        /**
-        * Get CL_MEM_TYPE
+        * Get CL_MEM_TYPE.
+        * @returns One of CL_MEM_OBJECT_* types.
         */
        public int getType() {
                return getInfoInt(CL_MEM_TYPE);
@@ -181,7 +182,7 @@ public abstract class CLMemory extends CLObject {
        /**
         * Get CL_MEM_FLAGS.
         *
-        * @return
+        * @return One of CL_MEM_* flags.
         */
        public long getFlags() {
                return getInfoLong(CL_MEM_FLAGS);
@@ -190,7 +191,7 @@ public abstract class CLMemory extends CLObject {
        /**
         * Get CL_MEM_SIZE.
         *
-        * @return
+        * @return byte size.
         */
        public long getSize() {
                return getInfoSizeT(CL_MEM_SIZE);
index 3123d2c..2f7089f 100644 (file)
@@ -25,4 +25,6 @@ module notzed.zcl {
 
        exports au.notzed.zcl;
        //exports au.notzed.zcl.khr;
+       
+       exports api to notzed.zcl.demo;
 }
diff --git a/src/notzed.zcl/tests/au/notzed/zcl/CLBufferTest.java b/src/notzed.zcl/tests/au/notzed/zcl/CLBufferTest.java
new file mode 100644 (file)
index 0000000..6cf19a7
--- /dev/null
@@ -0,0 +1,300 @@
+
+package au.notzed.zcl;
+
+import org.junit.*;
+import static org.junit.Assert.*;
+import static au.notzed.zcl.CL.*;
+import jdk.incubator.foreign.*;
+import api.Native;
+import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
+
+/*
+  TODO: USE_HOST_PTR and what it does
+
+  Most of these tests are buffer related interfaces on clcontext or
+  clcommandqueue.  Maybe some should be there or in an IT.  Does it
+  really matter?
+
+ */
+public class CLBufferTest {
+       boolean haveCL() {
+               CLPlatform[] list = CLPlatform.getPlatforms();
+               return list != null && list.length > 0;
+       }
+
+       CLPlatform plat;
+       CLDevice devs[];
+       CLContext cl;
+       CLCommandQueue q;
+
+       @Before
+       public void setup() {
+               org.junit.Assume.assumeTrue(haveCL());
+
+               plat = CLPlatform.getPlatforms()[0];
+               devs = new CLDevice[] { plat.getDevices(CL_DEVICE_TYPE_ALL)[0] };
+               cl = CLContext.createContext(null, devs);
+               q = cl.createCommandQueue(devs[0], 0);
+       }
+       @After
+       public void shutdown() {
+               CLObject.release(q, cl);
+       }
+
+       @Test
+       public void testCreate1() {
+               System.out.println("createBuffer");
+
+               CLBuffer b = cl.createBuffer(0, 1024);
+
+               assertNotNull(b);
+
+               b.release();
+       }
+
+       @Test
+       public void testCreate2() {
+               System.out.println("createBuffer fields");
+
+               CLBuffer b = cl.createBuffer(0, 1024);
+
+               assertNotNull(b);
+
+               assertEquals(1024L, b.getSize());
+               assertEquals(CL_MEM_READ_WRITE, b.getFlags());
+               assertEquals(CL_MEM_OBJECT_BUFFER, b.getType());
+
+               assertEquals(cl, b.getContext());
+               assertNull(b.getAssociatedMemObject());
+               assertEquals(0, b.getMemOffset());
+
+               b.release();
+       }
+
+       @Test
+       public void testCreate3() {
+               System.out.println("createBuffer flags");
+
+               long[] flags = {
+                       CL_MEM_READ_WRITE,
+                       CL_MEM_WRITE_ONLY,
+                       CL_MEM_READ_ONLY,
+               };
+
+               for (long flag: flags) {
+                       CLBuffer b = cl.createBuffer(flag, 1024);
+
+                       assertNotNull(b);
+
+                       assertEquals(flag, b.getFlags());
+
+                       b.release();
+               }
+       }
+
+       byte[] filla = "opencl ftw!".getBytes();
+
+       static void fillSegment(MemorySegment seg, byte[] seq) {
+               MemoryAddress add = seg.baseAddress();
+               for (long i=0;i<seg.byteSize();i++) {
+                       Native.setByte(add, i, seq[(int)(i % seq.length)]);
+               }
+       }
+
+       static boolean equalSegment(MemorySegment a, MemorySegment b) {
+               boolean same = true;
+               MemoryAddress c = a.baseAddress();
+               MemoryAddress d = b.baseAddress();
+
+               for (long i=0;same && i<a.byteSize();i++) {
+                       same &= Native.getByte(c, i) == Native.getByte(d, i);
+               }
+               return same;
+       }
+
+       static boolean equal(ByteBuffer a, MemorySegment b) {
+               boolean same = true;
+               MemoryAddress d = b.baseAddress();
+
+               for (long i=0;same && i<a.capacity();i++) {
+                       same &= a.get((int)i) == Native.getByte(d, i);
+               }
+               return same;
+       }
+
+       @Test
+       public void testCreate4() throws Exception {
+               System.out.println("createBuffer COPY_HOST_PTR");
+
+               try (MemorySegment sega = MemorySegment.allocateNative(1024, 8);
+                       MemorySegment segb = MemorySegment.allocateNative(1024, 8)) {
+                       fillSegment(sega, filla);
+
+                       CLBuffer b = cl.createBuffer(CL_MEM_COPY_HOST_PTR, 1024, sega);
+
+                       q.enqueueReadBuffer(b, true, 0, segb.byteSize(), segb, null, null);
+
+                       assertTrue(equalSegment(sega, segb));
+                       b.release();
+               }
+       }
+
+       @Test
+       public void testCreate5() throws Exception {
+               Throwable x;
+               System.out.println("createBuffer invalid flags");
+
+               try {
+                       x = null;
+                       cl.createBuffer(CL_MEM_COPY_HOST_PTR, 1024, (MemorySegment)null);
+               } catch (Throwable t) {
+                       x = t;
+               }
+               assertEquals(CLRuntimeException.class, x.getClass());
+               assertEquals(CL_INVALID_HOST_PTR, ((CLRuntimeException)x).err);
+
+               try {
+                       x = null;
+                       cl.createBuffer(CL_MEM_USE_HOST_PTR, 1024, (MemorySegment)null);
+               } catch (Throwable t) {
+                       x = t;
+               }
+               assertEquals(CLRuntimeException.class, x.getClass());
+               assertEquals(CL_INVALID_HOST_PTR, ((CLRuntimeException)x).err);
+
+               try (MemorySegment seg = MemorySegment.allocateNative(8, 8)) {
+                       x = null;
+                       cl.createBuffer(0, 8, seg);
+               } catch (Throwable t) {
+                       x = t;
+               }
+               assertEquals(CLRuntimeException.class, x.getClass());
+               assertEquals(CL_INVALID_HOST_PTR, ((CLRuntimeException)x).err);
+
+               try (MemorySegment seg = MemorySegment.allocateNative(8, 8)) {
+                       x = null;
+                       cl.createBuffer(0, seg);
+               } catch (Throwable t) {
+                       x = t;
+               }
+               assertEquals(CLRuntimeException.class, x.getClass());
+               assertEquals(CL_INVALID_HOST_PTR, ((CLRuntimeException)x).err);
+
+               try {
+                       ByteBuffer bb = ByteBuffer.allocateDirect(8);
+                       x = null;
+                       cl.createBuffer(0, 8, bb);
+               } catch (Throwable t) {
+                       x = t;
+               }
+               assertEquals(CLRuntimeException.class, x.getClass());
+               assertEquals(CL_INVALID_HOST_PTR, ((CLRuntimeException)x).err);
+
+               try {
+                       ByteBuffer bb = ByteBuffer.allocateDirect(8);
+                       x = null;
+                       cl.createBuffer(0, bb);
+               } catch (Throwable t) {
+                       x = t;
+               }
+               assertEquals(CLRuntimeException.class, x.getClass());
+               assertEquals(CL_INVALID_HOST_PTR, ((CLRuntimeException)x).err);
+       }
+
+       @Test
+       public void testCreate6() throws Exception {
+               Throwable x;
+               System.out.println("createBuffer sized segment");
+
+               try (MemorySegment seg = MemorySegment.allocateNative(720, 8)) {
+                       CLBuffer b = cl.createBuffer(CL_MEM_COPY_HOST_PTR, seg);
+
+                       assertEquals(seg.byteSize(), b.getSize());
+
+                       b.release();
+               }
+       }
+
+       @Test
+       public void testCreate7() throws Exception {
+               Throwable x;
+               System.out.println("createBuffer sized buffer");
+
+               ByteBuffer bb = ByteBuffer.allocateDirect(768);
+               CLBuffer b = cl.createBuffer(CL_MEM_COPY_HOST_PTR, bb);
+
+               assertEquals(bb.capacity(), b.getSize());
+
+               b.release();
+       }
+
+       @Test
+       public void testMap1() throws Exception {
+               System.out.println("mapBuffer");
+               try (MemorySegment sega = MemorySegment.allocateNative(1024, 8)) {
+                       fillSegment(sega, filla);
+
+                       CLBuffer b = cl.createBuffer(CL_MEM_COPY_HOST_PTR, 1024, sega);
+                       ByteBuffer bb = q.enqueueMapBuffer(b, true, CL_MAP_READ, 0, sega.byteSize(), null, null);
+
+                       assertNotNull(bb);
+                       assertEquals(sega.byteSize(), bb.capacity());
+
+                       q.enqueueUnmapMemObject(b, bb, null, null);
+
+                       b.release();
+               }
+       }
+
+       @Test
+       public void testMap2() throws Exception {
+               System.out.println("mapBuffer double unmap");
+               try (MemorySegment sega = MemorySegment.allocateNative(1024, 8)) {
+                       fillSegment(sega, filla);
+
+                       CLBuffer b = cl.createBuffer(CL_MEM_COPY_HOST_PTR, 1024, sega);
+                       ByteBuffer bb = q.enqueueMapBuffer(b, true, CL_MAP_READ, 0, sega.byteSize(), null, null);
+
+                       assertNotNull(bb);
+                       assertEquals(sega.byteSize(), bb.capacity());
+
+                       q.enqueueUnmapMemObject(b, bb, null, null);
+
+                       Throwable x;
+                       try {
+                               bb.limit(1); // ensure hash changes
+
+                               x = null;
+                               q.enqueueUnmapMemObject(b, bb, null, null);
+                       } catch (Throwable t) {
+                               x = t;
+                       }
+                       assertEquals(IllegalArgumentException.class, x.getClass());
+
+                       b.release();
+               }
+       }
+
+       @Test
+       public void testFill1() throws Exception {
+               org.junit.Assume.assumeTrue(plat.getAPIVersion() >= CLPlatform.VERSION_1_2);
+
+               byte[] data = new byte[] { 1, 2, 3, 4 };
+
+               try (MemorySegment seg = MemorySegment.allocateNative(1024, 8)) {
+                       CLBuffer b = cl.createBuffer(0, 1024);
+
+                       q.enqueueFillBuffer(b, data, 0, 256, null, null);
+                       q.enqueueReadBuffer(b, true, 0, seg.byteSize(), seg, null, null);
+
+                       boolean same = true;
+                       MemoryAddress add = seg.baseAddress();
+                       for (int i=0;same && i<1024;i++) {
+                               same = Native.getByte(add, i) == data[i % data.length];
+                       }
+
+                       assertTrue(same);
+               }
+       }
+}
diff --git a/src/notzed.zcl/tests/au/notzed/zcl/CLCommandQueueTest.java b/src/notzed.zcl/tests/au/notzed/zcl/CLCommandQueueTest.java
new file mode 100644 (file)
index 0000000..f5895fb
--- /dev/null
@@ -0,0 +1,57 @@
+
+package au.notzed.zcl;
+
+import org.junit.*;
+import static org.junit.Assert.*;
+import static au.notzed.zcl.CL.*;
+import jdk.incubator.foreign.*;
+import api.Native;
+import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
+
+public class CLCommandQueueTest {
+       boolean haveCL() {
+               CLPlatform[] list = CLPlatform.getPlatforms();
+               return list != null && list.length > 0;
+       }
+
+       CLPlatform plat;
+       CLDevice devs[];
+       CLContext cl;
+
+       @Before
+       public void setup() {
+               org.junit.Assume.assumeTrue(haveCL());
+
+               plat = CLPlatform.getPlatforms()[0];
+               devs = new CLDevice[] { plat.getDevices(CL_DEVICE_TYPE_ALL)[0] };
+               cl = CLContext.createContext(null, devs);
+       }
+       @After
+       public void shutdown() {
+               CLObject.release(cl);
+       }
+
+       @Test
+       public void testCreate1() {
+               System.out.println("createCommandQueue old");
+
+               CLCommandQueue q = cl.createCommandQueue(devs[0], 0);
+
+               assertNotNull(q);
+
+               q.release();
+       }
+
+       @Test
+       public void testCreate2() {
+               org.junit.Assume.assumeTrue(plat.getAPIVersion() >= CLPlatform.VERSION_2_0);
+               System.out.println("createCommandQueue new");
+
+               CLCommandQueue q = cl.createCommandQueue(devs[0], new CLQueueProperty[0]);
+
+               assertNotNull(q);
+
+               q.release();
+       }
+}
index d23b5c1..e9c83a5 100644 (file)
@@ -34,12 +34,14 @@ public class CLProgramTest {
        @Test
        public void testCreate1() {
                System.out.println("createProgramWithSource - null");
+               Throwable x = null;
 
                try {
                        cl.createProgramWithSource((String[])null);
                } catch (Throwable t) {
-                       assertEquals(t.getClass(), NullPointerException.class);
+                       x = t;
                }
+               assertEquals(x.getClass(), NullPointerException.class);
                //assertThrows(NullPointerException.class,
                //      () -> cl.createProgramWithSource(null));
        }