From 12359a375d629c39dd8844ab7fd5e05e8d7d6fea Mon Sep 17 00:00:00 2001 From: Not Zed Date: Sun, 26 Jan 2020 10:06:19 +1030 Subject: [PATCH] More tests and minor changes. --- src/notzed.zcl/classes/api/Native.java | 2 +- .../classes/au/notzed/zcl/CLContext.java | 16 +- .../classes/au/notzed/zcl/CLMemory.java | 7 +- src/notzed.zcl/classes/module-info.java | 2 + .../tests/au/notzed/zcl/CLBufferTest.java | 300 ++++++++++++++++++ .../au/notzed/zcl/CLCommandQueueTest.java | 57 ++++ .../tests/au/notzed/zcl/CLProgramTest.java | 4 +- 7 files changed, 382 insertions(+), 6 deletions(-) create mode 100644 src/notzed.zcl/tests/au/notzed/zcl/CLBufferTest.java create mode 100644 src/notzed.zcl/tests/au/notzed/zcl/CLCommandQueueTest.java diff --git a/src/notzed.zcl/classes/api/Native.java b/src/notzed.zcl/classes/api/Native.java index 364d262..4260da2 100644 --- a/src/notzed.zcl/classes/api/Native.java +++ b/src/notzed.zcl/classes/api/Native.java @@ -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); } diff --git a/src/notzed.zcl/classes/au/notzed/zcl/CLContext.java b/src/notzed.zcl/classes/au/notzed/zcl/CLContext.java index fe2779c..564964c 100644 --- a/src/notzed.zcl/classes/au/notzed/zcl/CLContext.java +++ b/src/notzed.zcl/classes/au/notzed/zcl/CLContext.java @@ -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 { diff --git a/src/notzed.zcl/classes/au/notzed/zcl/CLMemory.java b/src/notzed.zcl/classes/au/notzed/zcl/CLMemory.java index 31f27b8..575d59c 100644 --- a/src/notzed.zcl/classes/au/notzed/zcl/CLMemory.java +++ b/src/notzed.zcl/classes/au/notzed/zcl/CLMemory.java @@ -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); diff --git a/src/notzed.zcl/classes/module-info.java b/src/notzed.zcl/classes/module-info.java index 3123d2c..2f7089f 100644 --- a/src/notzed.zcl/classes/module-info.java +++ b/src/notzed.zcl/classes/module-info.java @@ -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 index 0000000..6cf19a7 --- /dev/null +++ b/src/notzed.zcl/tests/au/notzed/zcl/CLBufferTest.java @@ -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= 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 index 0000000..f5895fb --- /dev/null +++ b/src/notzed.zcl/tests/au/notzed/zcl/CLCommandQueueTest.java @@ -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(); + } +} diff --git a/src/notzed.zcl/tests/au/notzed/zcl/CLProgramTest.java b/src/notzed.zcl/tests/au/notzed/zcl/CLProgramTest.java index d23b5c1..e9c83a5 100644 --- a/src/notzed.zcl/tests/au/notzed/zcl/CLProgramTest.java +++ b/src/notzed.zcl/tests/au/notzed/zcl/CLProgramTest.java @@ -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)); } -- 2.39.5