From: Not Zed Date: Fri, 24 Jan 2020 06:35:39 +0000 (+1030) Subject: Mostly documentation updates with a couple more api functions implemented. X-Git-Url: https://code.zedzone.au/cvs?a=commitdiff_plain;h=c495e1fdd7a23d9fd1ffd935c2e209f7ace92017;p=zcl Mostly documentation updates with a couple more api functions implemented. --- diff --git a/src/notzed.zcl.demo/classes/module-info.java b/src/notzed.zcl.demo/classes/module-info.java index 8cfc6f5..d9922ca 100644 --- a/src/notzed.zcl.demo/classes/module-info.java +++ b/src/notzed.zcl.demo/classes/module-info.java @@ -16,6 +16,12 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ + +/** + * Some basic demonstration examples for OpenCL using zcl. + */ module notzed.zcl.demo { requires notzed.zcl; + + exports au.notzed.zcl.tools; } diff --git a/src/notzed.zcl/classes/api/Native.java b/src/notzed.zcl/classes/api/Native.java index ef599df..f8a4dc7 100644 --- a/src/notzed.zcl/classes/api/Native.java +++ b/src/notzed.zcl/classes/api/Native.java @@ -34,6 +34,13 @@ import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; import java.lang.System.Logger.Level; +/** + * Base class for all native objects. + * + * Handles instantiation and provides helper functions for native access. + * + * Work in progress. + */ public class Native { private final MemoryAddress p; @@ -185,18 +192,22 @@ public class Native { } /* helpers - java to native */ - public static MemoryAddress toAddrV(Allocator frame, T[]array) { - MemoryAddress list = frame.alloca(8 * array.length); + public static MemoryAddress toAddrV(Allocator frame, T[]array, int len) { + MemoryAddress list = frame.alloca(8 * len); - for (int i=0;i MemoryAddress toAddrV(Allocator frame, T[]array) { + return toAddrV(frame, array, array.length); + } + public static MemoryAddress toAddrV(Allocator frame, String[]array) { if (array != null) { - MemoryAddress list = frame.alloca(8 * array.length); + MemoryAddress list = frame.alloca(8 * array.length); for (int i=0;i MemoryAddress toLongV(Allocator frame, long[]array) { - MemoryAddress list = frame.alloca(8 * array.length); + MemoryAddress list = frame.alloca(8 * array.length); for (int i=0;i */ public class CLBuffer extends CLMemory { - /** - * Create an interface for a native pointer of type cl_mem that refers to a - * buffer object. - * - * @param p Native pointer. - */ public CLBuffer(MemoryAddress p) { this(p, null); } - public CLBuffer(MemoryAddress p, MemorySegment seg) { + CLBuffer(MemoryAddress p, MemorySegment seg) { super(p, seg); } diff --git a/src/notzed.zcl/classes/au/notzed/zcl/CLCommandQueue.java b/src/notzed.zcl/classes/au/notzed/zcl/CLCommandQueue.java index c73cfa7..6de70f5 100644 --- a/src/notzed.zcl/classes/au/notzed/zcl/CLCommandQueue.java +++ b/src/notzed.zcl/classes/au/notzed/zcl/CLCommandQueue.java @@ -30,7 +30,6 @@ import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.nio.BufferOverflowException; import java.nio.BufferUnderflowException; -//import static au.au.notzed.zcl.CL.*; //import au.notzed.zcl.khr.GLSharing; import java.lang.invoke.MethodHandle; @@ -55,16 +54,11 @@ import java.util.function.Function; */ public class CLCommandQueue extends CLExtendable { - /** - * Create an interface for a native pointer of type cl_command_queue. - * - * @param p Native pointer. - */ - CLCommandQueue(MemoryAddress p) { + public CLCommandQueue(MemoryAddress p) { super(p); } - public static CLCommandQueue create(MemoryAddress p) { + static CLCommandQueue create(MemoryAddress p) { return Native.resolve(p, CLCommandQueue::new); } @@ -314,8 +308,8 @@ public class CLCommandQueue extends CLExtendable { * @param host_row_pitch * @param host_slice_pitch * @param buffer - * @param waiters - * @param events + * @param wait + * @param event * @throws CLException * @throws UnsupportedOperationException * @since OpenCL 1.1 @@ -552,8 +546,8 @@ public class CLCommandQueue extends CLExtendable { * @param host_row_pitch * @param host_slice_pitch * @param buffer - * @param waiters - * @param events + * @param wait + * @param event * @throws CLException * @throws UnsupportedOperationException * @since OpenCL 1.1 @@ -659,8 +653,8 @@ public class CLCommandQueue extends CLExtendable { * @param pattern pattern to fill * @param offset offset in multiples of the pattern size. * @param size number of elements in multiples of the pattern size. - * @param waiters - * @param events + * @param wait + * @param event * @since OpenCL 1.2 * @throws CLException */ @@ -713,8 +707,8 @@ public class CLCommandQueue extends CLExtendable { * @param pattern pattern to fill * @param offset offset in multiples of the pattern size. * @param size number of elements in multiples of the pattern size. - * @param waiters - * @param events + * @param wait + * @param event * @since OpenCL 1.2 * @throws CLException */ @@ -741,8 +735,8 @@ public class CLCommandQueue extends CLExtendable { * @param pattern pattern to fill * @param offset offset in multiples of the pattern size. * @param size number of elements in multiples of the pattern size. - * @param waiters - * @param events + * @param wait + * @param event * @since OpenCL 1.2 * @throws CLException */ @@ -769,8 +763,8 @@ public class CLCommandQueue extends CLExtendable { * @param pattern pattern to fill * @param offset offset in multiples of the pattern size. * @param size number of elements in multiples of the pattern size. - * @param waiters - * @param events + * @param wait + * @param event * @since OpenCL 1.2 * @throws CLException */ @@ -816,8 +810,8 @@ public class CLCommandQueue extends CLExtendable { * @param pattern pattern to fill * @param offset offset in multiples of the pattern size in floats. * @param size number of elements in multiples of the pattern size in floats. - * @param waiters - * @param events + * @param wait + * @param event * @since OpenCL 1.2 * @throws CLException */ @@ -1024,8 +1018,8 @@ public class CLCommandQueue extends CLExtendable { * @param row_pitch * @param slice_pitch * @param buffer the position is honoured and updated - * @param waiters - * @param events + * @param wait + * @param event * @throws CLException */ public void enqueueReadImage(CLImage image, boolean blocking, @@ -1069,8 +1063,8 @@ public class CLCommandQueue extends CLExtendable { * @param slice_pitch in number of array elements * @param buffer * @param buff_offset in number of array elements - * @param waiters - * @param events + * @param wait + * @param event * @throws CLException */ public void enqueueReadImage(CLImage image, boolean blocking, diff --git a/src/notzed.zcl/classes/au/notzed/zcl/CLContext.java b/src/notzed.zcl/classes/au/notzed/zcl/CLContext.java index 34d89d0..fe2779c 100644 --- a/src/notzed.zcl/classes/au/notzed/zcl/CLContext.java +++ b/src/notzed.zcl/classes/au/notzed/zcl/CLContext.java @@ -23,10 +23,7 @@ import api.Memory; import api.Allocator; import api.Native; import api.Callback; -import java.util.stream.Stream; - import java.lang.invoke.MethodHandle; - import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.io.ByteArrayOutputStream; @@ -34,9 +31,6 @@ import java.io.IOException; import java.io.InputStream; import java.nio.charset.Charset; -import java.util.function.Function; -import java.util.function.IntFunction; - /** * Interface for cl_context */ @@ -47,11 +41,6 @@ public class CLContext extends CLExtendable { */ final Callback notify; - /** - * Creates an interface for a native pointer of type cl_context. - * - * @param p Native pointer. - */ public CLContext(MemoryAddress p) { super(p); this.notify = null; @@ -294,7 +283,7 @@ public class CLContext extends CLExtendable { * * @param flags CL_MEM_* flags. * @param size Size in bytes. - * @param hostp Optional host memory pointer. + * @param hostseg Optional host memory pointer. * @return Newly allocated buffer. * @throws CLRuntimeException */ @@ -426,7 +415,7 @@ public class CLContext extends CLExtendable { * @param flags CL_MEM_* flags. * @param fmt Image format. * @param desc Image descriptor. For OpenCL 1.2, only CL_MEM_OBJECT_IMAGE2D and CL_MEM_OBJECT_IMAGE3D are allowed. - * @param hostp + * @param hostseg * @return Newly allocated image.

* release() should be called when it is no longer needed. * @throws CLRuntimeException diff --git a/src/notzed.zcl/classes/au/notzed/zcl/CLContextNotify.java b/src/notzed.zcl/classes/au/notzed/zcl/CLContextNotify.java index e8f18f7..c005742 100644 --- a/src/notzed.zcl/classes/au/notzed/zcl/CLContextNotify.java +++ b/src/notzed.zcl/classes/au/notzed/zcl/CLContextNotify.java @@ -17,8 +17,6 @@ package au.notzed.zcl; import java.nio.ByteBuffer; -import java.util.function.Function; -import jdk.incubator.foreign.MemoryAddress; import jdk.incubator.foreign.MemorySegment; import api.Native; import api.Callback; @@ -44,6 +42,4 @@ public interface CLContextNotify { return Callback.NULL; } } - - } diff --git a/src/notzed.zcl/classes/au/notzed/zcl/CLDevice.java b/src/notzed.zcl/classes/au/notzed/zcl/CLDevice.java index f6e474c..c97d51c 100644 --- a/src/notzed.zcl/classes/au/notzed/zcl/CLDevice.java +++ b/src/notzed.zcl/classes/au/notzed/zcl/CLDevice.java @@ -16,9 +16,7 @@ */ package au.notzed.zcl; -import api.Native; import java.lang.invoke.MethodHandle; -import java.util.function.Function; import jdk.incubator.foreign.*; import static au.notzed.zcl.CL.*; import static au.notzed.zcl.CLLib.*; @@ -28,11 +26,6 @@ import static au.notzed.zcl.CLLib.*; */ public class CLDevice extends CLExtendable { - /** - * Create an interface for a native pointer of type cl_device_id. - * - * @param p Native pointer. - */ public CLDevice(MemoryAddress p) { super(p); } @@ -44,8 +37,6 @@ public class CLDevice extends CLExtendable { } } - //private native static void release(long p); - @Override public String toString() { return getName(); @@ -408,15 +399,15 @@ public class CLDevice extends CLExtendable { return getInfoInt(CL_DEVICE_PARTITION_MAX_SUB_DEVICES); } - //public native CLDeviceProperty[] getPartitionProperties(); + public CLDeviceProperty[] getPartitionProperties() { + return getInfoPropertyV(CL_DEVICE_PARTITION_PROPERTIES, clGetDeviceInfo, CLDeviceProperty.TagValue::new, CLDeviceProperty[]::new); + } public long getPartitionAffinityDomain() { return getInfoLong(CL_DEVICE_PARTITION_AFFINITY_DOMAIN); } - //public native CLDeviceProperty[] getPartitionType(); - - //public static CLDevice[] newArray(int n) { - // return new CLDevice[n]; - //} + public CLDeviceProperty[] getPartitionType() { + return getInfoPropertyV(CL_DEVICE_PARTITION_TYPE, clGetDeviceInfo, CLDeviceProperty.TagValue::new, CLDeviceProperty[]::new); + } } diff --git a/src/notzed.zcl/classes/au/notzed/zcl/CLDeviceProperty.java b/src/notzed.zcl/classes/au/notzed/zcl/CLDeviceProperty.java index f3f1afb..08b8813 100644 --- a/src/notzed.zcl/classes/au/notzed/zcl/CLDeviceProperty.java +++ b/src/notzed.zcl/classes/au/notzed/zcl/CLDeviceProperty.java @@ -47,25 +47,15 @@ public interface CLDeviceProperty extends CLProperty { } @Override - public int toInt(int[] dst, int o) { - dst[o++] = CL.CL_DEVICE_PARTITION_BY_COUNTS; - for (int c : counts) { - dst[o++] = c; - } - dst[o++] = CL.CL_DEVICE_PARTITION_BY_COUNTS_LIST_END; + public int toInt(MemoryAddress dst, int o) { + Native.setInt(dst, o++, CL.CL_DEVICE_PARTITION_BY_COUNTS); + for (int c : counts) + Native.setInt(dst, o++, c); + Native.setInt(dst, o++, CL.CL_DEVICE_PARTITION_BY_COUNTS_LIST_END); return o; } - + @Override - public int toLong(long[] dst, int o) { - dst[o++] = CL.CL_DEVICE_PARTITION_BY_COUNTS; - for (int c : counts) { - dst[o++] = c; - } - dst[o++] = CL.CL_DEVICE_PARTITION_BY_COUNTS_LIST_END; - return o; - } - public int toLong(MemoryAddress dst, int o) { Native.setLong(dst, o++, CL.CL_DEVICE_PARTITION_BY_COUNTS); for (int c : counts) diff --git a/src/notzed.zcl/classes/au/notzed/zcl/CLEvent.java b/src/notzed.zcl/classes/au/notzed/zcl/CLEvent.java index 6e63126..c378cfe 100644 --- a/src/notzed.zcl/classes/au/notzed/zcl/CLEvent.java +++ b/src/notzed.zcl/classes/au/notzed/zcl/CLEvent.java @@ -29,20 +29,22 @@ import java.lang.invoke.MethodHandle; public class CLEvent extends CLObject { /** - * This is used to retain a reference for any callback set + * Cached and used for api version check */ - Callback callback; + final int apiVersion; /** - * Create an interface for a native pointer of type cl_event. - * - * @param p Native pointer. + * This is used to retain a reference for any callback set */ + Callback callback; + public CLEvent(MemoryAddress p) { super(p); + + apiVersion = getContext().getAPIVersion(); } - public static CLEvent create(MemoryAddress p) { + static CLEvent create(MemoryAddress p) { return Native.resolve(p, CLEvent::new); } @@ -54,6 +56,7 @@ public class CLEvent extends CLObject { } } + @Override public void release() { Native.release(callback); callback = null; @@ -75,8 +78,7 @@ public class CLEvent extends CLObject { * @since OpenCL 1.1 */ public void setUserEventStatus(int status) throws CLRuntimeException { - // avoid platform lookup for costs - //requiredAPIVersion(CLPlatform.VERSION_1_1); + CLPlatform.requireAPIVersion(apiVersion, CLPlatform.VERSION_1_1); try { int res = clSetUserEventStatus(addr(), status); @@ -98,7 +100,7 @@ public class CLEvent extends CLObject { * @since OpenCL 1.1 */ public void setEventCallback(int type, CLEventNotify notify) throws CLRuntimeException { - //requiredAPIVersion(CLPlatform.VERSION_1_1); + CLPlatform.requireAPIVersion(apiVersion, CLPlatform.VERSION_1_1); Native.release(callback); diff --git a/src/notzed.zcl/classes/au/notzed/zcl/CLEventList.java b/src/notzed.zcl/classes/au/notzed/zcl/CLEventList.java index bb38677..df0d398 100644 --- a/src/notzed.zcl/classes/au/notzed/zcl/CLEventList.java +++ b/src/notzed.zcl/classes/au/notzed/zcl/CLEventList.java @@ -29,7 +29,6 @@ import static au.notzed.zcl.CLLib.*; * this API binding and provides a much cleaner, efficient, and simple interface. *

* See {@link au.notzed.zcl.CLCommandQueue} for more information on usage. - *

*

Internal Details

*

* Internally the CLEventList is maintained as an array of long values which hold @@ -39,7 +38,6 @@ import static au.notzed.zcl.CLLib.*; * The JNI code directly reads from the events list and index in order to build * a list of wait events, and calls the internal add(long) method to append to * the events array. - *

*/ public class CLEventList { @@ -52,7 +50,7 @@ public class CLEventList { /** * Creates a new event list. * - * @param capacity Sets the event list capacity. This + * @param capacity Sets the event list capacity. */ public CLEventList(int capacity) { this.jevents = new CLEvent[capacity]; @@ -65,11 +63,16 @@ public class CLEventList { */ public void reset() { for (int i = 0; i < index; i++) { + jevents[i].release(); jevents[i] = null; } index = 0; } + public void release() { + reset(); + } + /** * Creates an interface to the given event. * @@ -98,19 +101,6 @@ public class CLEventList { return index; } - static MemoryAddress toWaitList(Allocator frame, CLEventList list) { - if (list != null) { - MemoryAddress addr = frame.alloca(list.index * 8); - - for (int i=0;i 0) { try (Allocator frame = Memory.stack()) { - MemoryAddress events = toWaitList(frame, this); + MemoryAddress events = Native.toAddrV(frame, jevents, index); int res; res = clWaitForEvents(size(), events); diff --git a/src/notzed.zcl/classes/au/notzed/zcl/CLExtendable.java b/src/notzed.zcl/classes/au/notzed/zcl/CLExtendable.java index 6686295..44837df 100644 --- a/src/notzed.zcl/classes/au/notzed/zcl/CLExtendable.java +++ b/src/notzed.zcl/classes/au/notzed/zcl/CLExtendable.java @@ -54,8 +54,7 @@ public abstract class CLExtendable extends CLObject { } public void requireAPIVersion(int version) throws UnsupportedOperationException { - if (apiVersion < version) - throw new UnsupportedOperationException("Requires version " + ((apiVersion >> 8) & 0xff) + "." + (apiVersion & 0xff)); + CLPlatform.requireAPIVersion(apiVersion, version); } public boolean haveAPIVersion(int version) { diff --git a/src/notzed.zcl/classes/au/notzed/zcl/CLImage.java b/src/notzed.zcl/classes/au/notzed/zcl/CLImage.java index 60d4509..b255444 100644 --- a/src/notzed.zcl/classes/au/notzed/zcl/CLImage.java +++ b/src/notzed.zcl/classes/au/notzed/zcl/CLImage.java @@ -19,9 +19,6 @@ package au.notzed.zcl; import static au.notzed.zcl.CL.*; import static au.notzed.zcl.CLLib.*; import jdk.incubator.foreign.*; -import api.Native; - -//import static au.au.notzed.zcl.CL.*; import java.nio.ByteBuffer; import java.lang.invoke.MethodHandle; @@ -32,13 +29,6 @@ import java.lang.invoke.MethodHandle; */ public class CLImage extends CLMemory { - /** - * Create an interface for a native pointer of type cl_mem that refers to an - * image object. - * - * @param p Native pointer. - * @param seg A segment backing the image on the host. - */ public CLImage(MemoryAddress p) { this(p, null); } diff --git a/src/notzed.zcl/classes/au/notzed/zcl/CLKernel.java b/src/notzed.zcl/classes/au/notzed/zcl/CLKernel.java index aaade1e..5565491 100644 --- a/src/notzed.zcl/classes/au/notzed/zcl/CLKernel.java +++ b/src/notzed.zcl/classes/au/notzed/zcl/CLKernel.java @@ -22,8 +22,6 @@ import jdk.incubator.foreign.*; import api.Native; import api.Allocator; import api.Memory; - -import java.nio.Buffer; import java.nio.ByteBuffer; import java.lang.invoke.MethodHandle; @@ -35,13 +33,12 @@ import java.lang.invoke.MethodHandle; */ public class CLKernel extends CLObject { - /** - * Create an interface for a native pointer of type cl_kernel. - * - * @param p Native pointer. - */ + int apiVersion; + CLKernel(MemoryAddress p) { super(p); + + apiVersion = getContext().getAPIVersion(); } static CLKernel create(MemoryAddress p) { @@ -68,10 +65,22 @@ public class CLKernel extends CLObject { * @since OpenCL 2.1 */ public CLKernel cloneKernel() throws CLRuntimeException { - if (clCloneKernel != null) { - // ?? + CLPlatform.requireAPIVersion(apiVersion, CLPlatform.VERSION_2_1); + + try (Allocator a = Memory.stack()) { + MemoryAddress cres = a.alloca(8); + MemoryAddress ck = clCloneKernel(addr(), cres); + int res = getInt(cres); + + if (res != 0) + throw new CLRuntimeException(res); + + return Native.resolve(ck, CLKernel::new); + } catch (CLRuntimeException | Error t) { + throw t; + } catch (Throwable t) { + throw new RuntimeException(t); } - throw new UnsupportedOperationException(); } /** @@ -86,8 +95,7 @@ public class CLKernel extends CLObject { if (res != 0) throw new CLRuntimeException(res); - } catch (CLRuntimeException t) { - // oh joy + } catch (CLRuntimeException | Error t) { throw t; } catch (Throwable t) { throw new RuntimeException(t); @@ -442,12 +450,13 @@ public class CLKernel extends CLObject { throw new UnsupportedOperationException(); } - // /** - // * @since OpenCL 2.1 - // */ - // public long[] getLocalSizeForSubGroupCount(CLDevice device, long count) { - // return getSubGroupInfoAnyV(device, CTYPE_SIZE_T, CL_KERNEL_LOCAL_SIZE_FOR_SUB_GROUP_COUNT, new long[]{count}); - // } + /** + * @since OpenCL 2.1 + */ + public long[] getLocalSizeForSubGroupCount(CLDevice device, long count) { + //return getSubGroupInfoAnyV(device, CTYPE_SIZE_T, CL_KERNEL_LOCAL_SIZE_FOR_SUB_GROUP_COUNT, new long[]{count}); + throw new UnsupportedOperationException(); + } public long getMaxNumSubGroups(CLDevice device) { throw new UnsupportedOperationException(); diff --git a/src/notzed.zcl/classes/au/notzed/zcl/CLMemory.java b/src/notzed.zcl/classes/au/notzed/zcl/CLMemory.java index 0a2923b..31f27b8 100644 --- a/src/notzed.zcl/classes/au/notzed/zcl/CLMemory.java +++ b/src/notzed.zcl/classes/au/notzed/zcl/CLMemory.java @@ -26,14 +26,11 @@ import api.Allocator; import java.lang.invoke.MethodHandle; import java.nio.ByteBuffer; -//import static au.au.notzed.zcl.CL.*; import java.nio.ByteOrder; -import jdk.incubator.foreign.*; /** * Interface for cl_mem. - *

- *

Memory Transfers

+ *

Memory Transfers

*

* Subclasses of CLMemory define various read, write, and map operations. These * are performed via ByteBuffer objects or primitive arrays. diff --git a/src/notzed.zcl/classes/au/notzed/zcl/CLObject.java b/src/notzed.zcl/classes/au/notzed/zcl/CLObject.java index a7eb75d..1a8fc21 100644 --- a/src/notzed.zcl/classes/au/notzed/zcl/CLObject.java +++ b/src/notzed.zcl/classes/au/notzed/zcl/CLObject.java @@ -24,29 +24,24 @@ import jdk.incubator.foreign.*; import api.Native; import api.Allocator; import api.Memory; -import java.nio.ByteBuffer; -import java.nio.ByteOrder; /** - * Base class for all OpenCL types which can be represented as objects - i.e. - * those that are defined by anonymous pointer types in cl.h. + * Base class for all OpenCL types which can be represented as objects. + * That is, those that are defined by anonymous pointer types in cl.h. *

- * Each object has a single memory pointer which is stored in the p field. A - * long is used for all platforms for code simplicity. For most objects this is - * the only value retained and all get methods defer to the OpenCL runtime. - * CLObjects are not guaranteed to map unique instances to a given OpenCL object - * although in many cases the API doesn't allow for any other behaviour. Objects - * are considered equal if their p value is identical. + * Each object has a single memory pointer which is stored in the p field. + * For most objects this is the only value retained and all get methods defer + * to the OpenCL runtime. OpenCL objects with the same pointer are guarnateed + * to map to the same Java instance. Objects are considered equal if their p + * value is identical. *

* To reduce the code-size, retain(), release(), getInfo() and object-specific - * getInfo() variants have been implemented in a polymorphic fashion. The TYPE_* - * fields and getType() method are used to implement this mechanism. get() - * methods which return CLObjects will normally return a new instance for each - * call although each instance will point to the same underlying native - * resource. + * getInfo() variants have been implemented in a polymorphic fashion. For + * getInfo(), the getInfoFunc() returns the method handle of the native + * function used to retrieve the information. *

- * Memory management is explicit and objects must be released when no longer - * needed. + * Memory management is automatic but can also be explicit at application + * discretion. Use the release() methods for explicit deallocation. */ public abstract class CLObject extends Native { @@ -54,55 +49,6 @@ public abstract class CLObject extends Native { super(p); } - // These are not really public - for access by extensions - public static final int CTYPE_PLATFORM = 0; - public static final int CTYPE_DEVICE = 1; - public static final int CTYPE_CONTEXT = 2; - public static final int CTYPE_COMMAND_QUEUE = 3; - public static final int CTYPE_PIPE = 4; - public static final int CTYPE_BUFFER = 5; - public static final int CTYPE_IMAGE = 6; - public static final int CTYPE_SAMPLER = 7; - public static final int CTYPE_PROGRAM = 8; - public static final int CTYPE_KERNEL = 9; - public static final int CTYPE_EVENT = 10; - public static final int CTYPE_SIZEOF = 11; // just a count - - /* - These type codes are used for getInfoX() queries. - Various values in zcl-jni.c must be synchronised with this manually. - */ - public static final int CTYPE_SIZE_T = 16; - public static final int CTYPE_BYTE = 17; - public static final int CTYPE_SHORT = 18; - public static final int CTYPE_INT = 19; - public static final int CTYPE_LONG = 20; - public static final int CTYPE_FLOAT = 21; - public static final int CTYPE_DOUBLE = 22; - // Range of primitives - protected static final int CTYPE_PRIMITIVE = 16; - protected static final int CTYPE_PRIMITIVE_SIZEOF = 23 - 16; - //protected static final int CTYPE_ARRAY = 128; - - - /* Info types */ - final static int TYPE_PLATFORM = 0; - final static int TYPE_DEVICE = 1; - final static int TYPE_CONTEXT = 2; - final static int TYPE_COMMAND_QUEUE = 3; - final static int TYPE_MEM_OBJECT = 4; - final static int TYPE_IMAGE = 5; - final static int TYPE_PIPE = 6; - final static int TYPE_SAMPLER = 7; - final static int TYPE_PROGRAM = 8; - final static int TYPE_PROGRAM_BUILD = 9; - final static int TYPE_KERNEL = 10; - final static int TYPE_KERNEL_ARG = 11; - final static int TYPE_KERNEL_WORK_GROUP = 12; - final static int TYPE_EVENT = 13; - final static int TYPE_EVENT_PROFILING = 14; - final static int TYPE_KERNEL_SUB_GROUP = 15; - // new 5-param version // this one is static so it can be accessed at creation time protected static MemoryAddress getInfo(MemoryAddress self, int id, MethodHandle getInfo, Allocator frame, long size) throws CLRuntimeException { @@ -122,7 +68,6 @@ public abstract class CLObject extends Native { } } - protected int getInfoInt(int id, MethodHandle getInfo) { try (Allocator a = Memory.stack()) { return getInt(getInfo(addr(), id, getInfo, a, 4)); @@ -318,10 +263,6 @@ public abstract class CLObject extends Native { } } - - // check the rest - - // indexed version protected MemoryAddress getInfo(int index, int id, MethodHandle getInfo, Allocator a, long size) throws CLRuntimeException { try { @@ -376,7 +317,6 @@ public abstract class CLObject extends Native { } } - protected String getInfoString(int index, int id, MethodHandle getInfo) { try (Allocator a = Memory.stack()) { MemorySegment valp = getInfoAny(index, id, getInfo, a); diff --git a/src/notzed.zcl/classes/au/notzed/zcl/CLPipe.java b/src/notzed.zcl/classes/au/notzed/zcl/CLPipe.java index b2e753a..5f2c613 100644 --- a/src/notzed.zcl/classes/au/notzed/zcl/CLPipe.java +++ b/src/notzed.zcl/classes/au/notzed/zcl/CLPipe.java @@ -28,12 +28,6 @@ import java.lang.invoke.MethodHandle; */ public class CLPipe extends CLMemory { - /** - * Create an interface for a native pointer of type cl_mem that refers to a - * pipe object. - * - * @param p Native pointer. - */ public CLPipe(MemoryAddress p) { this(p, null); } diff --git a/src/notzed.zcl/classes/au/notzed/zcl/CLPlatform.java b/src/notzed.zcl/classes/au/notzed/zcl/CLPlatform.java index be8dcb1..2151e7d 100644 --- a/src/notzed.zcl/classes/au/notzed/zcl/CLPlatform.java +++ b/src/notzed.zcl/classes/au/notzed/zcl/CLPlatform.java @@ -18,16 +18,12 @@ package au.notzed.zcl; import static au.notzed.zcl.CL.*; import static au.notzed.zcl.CLLib.*; -//import au.notzed.zcl.khr.GLSharing; import java.util.function.ToDoubleFunction; import jdk.incubator.foreign.*; import api.Native; import api.Memory; import api.Allocator; - -// hack test import java.lang.invoke.*; -import jdk.incubator.foreign.unsafe.ForeignUnsafe; /** * Interface for cl_platform_id @@ -43,11 +39,6 @@ public class CLPlatform extends CLObject { final int apiVersion; - /** - * Create an interface for a native pointer of type cl_platform_id. - * - * @param p Native pointer. - */ public CLPlatform(MemoryAddress p) { super(p); @@ -82,6 +73,11 @@ public class CLPlatform extends CLObject { return clGetPlatformInfo; } + public static void requireAPIVersion(int apiVersion, int required) throws UnsupportedOperationException { + if (apiVersion < required) + throw new UnsupportedOperationException("Requires version " + ((apiVersion >> 8) & 0xff) + "." + (apiVersion & 0xff)); + } + /** * Calls clGetPlatformIDs. * @@ -96,13 +92,17 @@ public class CLPlatform extends CLObject { int res; res = (int)clGetPlatformIDs.invokeExact(0, MemoryAddress.NULL, lenp); - + if (res != 0) + throw new CLRuntimeException(res); + len = Native.getInt(lenp); list = frame.alloca(8 * len); res = (int)clGetPlatformIDs.invokeExact(len, list, lenp); return toObjectV(list, new CLPlatform[len], CLPlatform::new); + } catch (RuntimeException | Error t) { + throw t; } catch (Throwable t) { throw new RuntimeException(t); } @@ -136,6 +136,8 @@ public class CLPlatform extends CLObject { res = (int)clGetDeviceIDs.invokeExact(addr(), type, len, list, lenp); return toObjectV(list, new CLDevice[len], CLDevice::new); + } catch (RuntimeException | Error t) { + throw t; } catch (Throwable t) { throw new RuntimeException(); } @@ -185,8 +187,30 @@ public class CLPlatform extends CLObject { * * @throws CLRuntimeException */ - //public native void unloadPlatformCompiler() throws CLRuntimeException; + public void unloadPlatformCompiler() throws CLRuntimeException { + try { + int res = clUnloadCompiler(); + if (res != 0) + throw new CLRuntimeException(res); + } catch (RuntimeException | Error t) { + throw t; + } catch (Throwable t) { + throw new RuntimeException(t); + } + } + /** + * Get the platform api versiom. + * + * This may be compared to the version constants + * {@link #VERSION_1_0}, {@link #VERSION_1_1}, and so on. + * + * @return platform api version. + */ + public int getAPIVersion() { + return apiVersion; + } + /** * get CL_PLATFORM_PROFILE. * diff --git a/src/notzed.zcl/classes/au/notzed/zcl/CLProgram.java b/src/notzed.zcl/classes/au/notzed/zcl/CLProgram.java index 43b97a0..ef55f67 100644 --- a/src/notzed.zcl/classes/au/notzed/zcl/CLProgram.java +++ b/src/notzed.zcl/classes/au/notzed/zcl/CLProgram.java @@ -31,11 +31,6 @@ import java.util.stream.LongStream; */ public class CLProgram extends CLObject { - /** - * Create an interface for a native pointer of type cl_program. - * - * @param p Native pointer. - */ CLProgram(MemoryAddress p) { super(p); } diff --git a/src/notzed.zcl/classes/au/notzed/zcl/CLProperty.java b/src/notzed.zcl/classes/au/notzed/zcl/CLProperty.java index 02e1906..3b38ac1 100644 --- a/src/notzed.zcl/classes/au/notzed/zcl/CLProperty.java +++ b/src/notzed.zcl/classes/au/notzed/zcl/CLProperty.java @@ -39,20 +39,18 @@ public interface CLProperty { * * @param dst destination * @param o offset - * @return updated o (o + getSize()) + * @return updated o = (o + getSize()) */ - int toInt(int dst[], int o); + int toInt(MemoryAddress dst, int o); /** - * Encode this property as a long array. + * Encode this property as a long poke. * * @param dst destination * @param o offset - * @return updated o (o + getSize()) + * @return updated o = (o + getSize())) */ - int toLong(long dst[], int o); - - int toLong(MemoryAddress addr, int i); + int toLong(MemoryAddress dst, int o); /** * A simple tag/value property type. @@ -73,19 +71,13 @@ public interface CLProperty { } @Override - public int toInt(int[] dst, int o) { - dst[o++] = (int) tag; - dst[o++] = (int) value; + public int toInt(MemoryAddress dst, int o) { + Native.setInt(dst, o++, (int)tag); + Native.setInt(dst, o++, (int)value); return o; } @Override - public int toLong(long[] dst, int o) { - dst[o++] = tag; - dst[o++] = value; - return o; - } - public int toLong(MemoryAddress dst, int o) { Native.setLong(dst, o++, tag); Native.setLong(dst, o++, value); @@ -113,65 +105,7 @@ public interface CLProperty { return size + 1; } - /** - * Convert to 32-bit intptr_t array. A terminating 0 is included. - * - * @param props - * @return - */ - /* - public static int[] toInt(Object[] props) { - int[] dst = new int[getSize(props)]; - int o = 0; - - //System.out.printf("CLProperty toInt(props=%d) size=%d\n", props.length, dst.length); - - for (Object p : props) { - o = ((CLProperty) p).toInt(dst, o); - } - dst[o] = 0; - - return dst; - } - */ - /** - * Convert to 64-bit intptr_t array. A terminating 0L is included. - * - * @param props - * @return - */ - /* - public static long[] toLong(Object[] props) { - long[] dst = new long[getSize(props)]; - int o = 0; - - //System.out.printf("CLProperty toLong(props=%d) size=%d\n", props.length, dst.length); - - for (Object p : props) { - o = ((CLProperty) p).toLong(dst, o); - } - dst[o] = 0; - - return dst; - } - */ - /* - public static Pointer toLong(T[] properties) { - if (properties != null && properties.length > 0) { - Pointer props = Pointer.alloc(getSize(properties), 8, Long.class); - int o = 0; - - for (CLProperty p : properties) - o = p.toLong(props, o); - props.set(o, 0L); - - return props; - } else { - return Pointer.NULL; - } - }*/ - - public static MemoryAddress toNative(Allocator frame, T[] properties) { + static MemoryAddress toNative(Allocator frame, T[] properties) { if (properties != null && properties.length > 0) { MemoryAddress addr = frame.alloca(getSize(properties) * 8); int i = 0; @@ -189,7 +123,7 @@ public interface CLProperty { } } - public static T[] fromNative(MemorySegment seg, BiFunction create, IntFunction createArray) { + static T[] fromNative(MemorySegment seg, BiFunction create, IntFunction createArray) { MemoryAddress add = seg.baseAddress(); ArrayList list = new ArrayList<>(); long tag; diff --git a/src/notzed.zcl/classes/au/notzed/zcl/CLSampler.java b/src/notzed.zcl/classes/au/notzed/zcl/CLSampler.java index cacbbb7..736f9d1 100644 --- a/src/notzed.zcl/classes/au/notzed/zcl/CLSampler.java +++ b/src/notzed.zcl/classes/au/notzed/zcl/CLSampler.java @@ -19,7 +19,6 @@ package au.notzed.zcl; import static au.notzed.zcl.CL.*; import static au.notzed.zcl.CLLib.*; import jdk.incubator.foreign.*; -import api.Native; import java.lang.invoke.MethodHandle; /** @@ -27,11 +26,6 @@ import java.lang.invoke.MethodHandle; */ public class CLSampler extends CLObject { - /** - * Create an interface for a native pointer of type cl_sampler. - * - * @param p Native pointer. - */ public CLSampler(MemoryAddress p) { super(p); } @@ -61,7 +55,6 @@ public class CLSampler extends CLObject { return new CLSamplerProperty.TagValue(CL_SAMPLER_FILTER_MODE, mode); } - public CLContext getContext() { return getInfoAny(CL_SAMPLER_CONTEXT, clGetSamplerInfo, CLContext::new); } diff --git a/src/notzed.zcl/classes/au/notzed/zcl/khr/GLSharing.java b/src/notzed.zcl/classes/au/notzed/zcl/khr/GLSharing.java index 665d19b..245c907 100644 --- a/src/notzed.zcl/classes/au/notzed/zcl/khr/GLSharing.java +++ b/src/notzed.zcl/classes/au/notzed/zcl/khr/GLSharing.java @@ -20,12 +20,10 @@ import au.notzed.zcl.CLBuffer; import au.notzed.zcl.CLCommandQueue; import au.notzed.zcl.CLContext; import au.notzed.zcl.CLContextProperty; -import au.notzed.zcl.CLDevice; import au.notzed.zcl.CLEventList; import au.notzed.zcl.CLExtension; import au.notzed.zcl.CLImage; import au.notzed.zcl.CLMemory; -import au.notzed.zcl.CLObject; import au.notzed.zcl.CLRuntimeException; import jdk.incubator.foreign.MemoryAddress; @@ -160,12 +158,13 @@ public class GLSharing extends CLExtension { int ctype, int param_name) throws CLRuntimeException; + /* public CLDevice getCurrendDeviceForGLConextKHR(CLContextProperty[] properties) throws CLRuntimeException { return getGLContextInfoKHRAny(properties, CLObject.CTYPE_DEVICE, CL_CURRENT_DEVICE_FOR_GL_CONTEXT_KHR); } public CLDevice[] getDevicesForGLConextKHR(CLContextProperty[] properties) throws CLRuntimeException { return getGLContextInfoKHRAnyV(properties, CLObject.CTYPE_DEVICE, CL_DEVICES_FOR_GL_CONTEXT_KHR); - } + }*/ } diff --git a/src/notzed.zcl/classes/au/notzed/zcl/package-info.java b/src/notzed.zcl/classes/au/notzed/zcl/package-info.java index 35fcc94..a1e97df 100644 --- a/src/notzed.zcl/classes/au/notzed/zcl/package-info.java +++ b/src/notzed.zcl/classes/au/notzed/zcl/package-info.java @@ -14,7 +14,8 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ + /** - * Provides a Java binding for OpenCL 2.0 and earlier. + * Provides a Java binding for OpenCL 2.1 and earlier. */ package au.notzed.zcl; diff --git a/src/notzed.zcl/classes/module-info.java b/src/notzed.zcl/classes/module-info.java index 67454d8..e2aac4b 100644 --- a/src/notzed.zcl/classes/module-info.java +++ b/src/notzed.zcl/classes/module-info.java @@ -16,6 +16,10 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ + +/** + * Implements a java-friendly binding to OpenCL. + */ module notzed.zcl { requires jdk.incubator.foreign; diff --git a/src/notzed.zcl/gen/gen.make b/src/notzed.zcl/gen/gen.make index 62508ae..2be9c5f 100644 --- a/src/notzed.zcl/gen/gen.make +++ b/src/notzed.zcl/gen/gen.make @@ -18,9 +18,6 @@ notzed.zcl_generated = \ notzed.zcl_JAVA_GENERATED = \ $(notzed.zcl_generated) -$(info $(notzed.zcl_genjavadir)) -$(info $(addprefix $(notzed.zcl_genjavadir),$(notzed.zcl_generated)): $(generate_api) $(opencl_pm)) - $(notzed.zcl_genjavadir)/au/notzed/zcl/CL.java: src/notzed.zcl/include/CL/cl.h $(export_defines) @install -d $(@D) perl $(export_defines) $< > $@ || rm -f $@ diff --git a/src/notzed.zcl/gen/generate-api b/src/notzed.zcl/gen/generate-api index ca3d95f..7e2dee0 100755 --- a/src/notzed.zcl/gen/generate-api +++ b/src/notzed.zcl/gen/generate-api @@ -900,7 +900,7 @@ $importPoineter END } - print $dst "public class $class{name} {\n"; + print $dst "class $class{name} {\n"; print $dst "\tstatic final String[] libraries = {"; print $dst join(",", map { "\"$_\"" } @libs); @@ -1123,7 +1123,7 @@ END } print $dst "\@FunctionalInterface\n"; - print $dst "public interface $name {\n"; + print $dst "interface $name {\n"; print $dst "\tpublic $result fn("; for $pi (@params) {