Module notzed.zcl
Package au.notzed.zcl

Class CLMemory

java.lang.Object
api.Native
au.notzed.zcl.CLObject
au.notzed.zcl.CLMemory
Direct Known Subclasses:
CLBuffer, CLImage, CLPipe

public abstract class CLMemory
extends CLObject
Interface for cl_mem.

Memory Transfers

Subclasses of CLMemory define various read, write, and map operations. These are performed via ByteBuffer objects or primitive arrays.

Primtive Arrays

Where available the primitive transfers will take the primitive array and an offset. Offsets are treated as units of the primitive type.

Due to the Java memory design primitive arrays only allow for synchronous transfers. This is a trade-off between ease of use, efficiency, and performance and may change in the future.

ByteBuffers

All transfer and memory operations support the use of ByteBuffer objects. These are initialised to the endianness of host (ByteOrder.nativeOrder()).

ByteBuffer objects allow access to native (malloc) memory from Java and hence support both synchronous (blocking) asynchronous (nonblocking) memory transfers.

When a user-created (i.e. not mapped) ByteBuffer is accepted as an argument then the position() and limit() of the buffer is honoured at the time the function is invoked. Note that the position itself is never modified by zcl. This is to simplify use of the common case and to avoid the need to track asnchronous operations.

It is unclear if this api is the correct one as it comes at a small performance penalty over simply passing the byte offset as with the array methods. It may change (again) in the future?

  • Method Details

    • create

      public static CLMemory create​(jdk.incubator.foreign.MemoryAddress p)
    • release

      public void release()
      If CL_MEM_USE_HOST_PTR was used at creation then this must be invoked to avoid a memory leak. It also must be invoked on the original thread of creation.
      Overrides:
      release in class Native
    • setMemObjectDestructorCallback

      public void setMemObjectDestructorCallback​(CLNotify<CLMemory> notify) throws CLException, java.lang.UnsupportedOperationException
      Call clSetMemObjectDestructorCallback.
      Parameters:
      notify -
      Throws:
      CLException
      java.lang.UnsupportedOperationException
      Since:
      OpenCL 1.1
    • getType

      public int getType()
      Get CL_MEM_TYPE.
      Returns:
      One of CL_MEM_OBJECT_* types.
    • getFlags

      public long getFlags()
      Get CL_MEM_FLAGS.
      Returns:
      One of CL_MEM_* flags.
    • getSize

      public long getSize()
      Get CL_MEM_SIZE.
      Returns:
      byte size.
    • getHostPtr

      public java.nio.ByteBuffer getHostPtr() throws CLRuntimeException
      Get CL_MEM_HOST_PTR. This recreates a bytebuffer from the memory segment used at creation time. It is always in native order.
      Returns:
      Throws:
      CLRuntimeException
    • getContext

      public CLContext getContext()
      Get CL_MEM_CONTEXT.
      Returns:
      An interface to the context this memory was created on.
    • getAssociatedMemObject

      public CLBuffer getAssociatedMemObject()
      Get CL_MEM_ASSOCIATED_MEMOBJECT for a sub-buffer.
      Returns:
    • getMemOffset

      public long getMemOffset()
      Get CL_MEM_OFFSET for a sub-buffer.
      Returns:
    • alloc

      public static java.nio.ByteBuffer alloc​(int size)
      Allocates a buffer suitable for opencl use - sets the byte order.
      Parameters:
      size -
      Returns:
    • alloc

      public static java.nio.ByteBuffer alloc​(float[] data)
      Copies the array to a direct buffer.
      Parameters:
      data -
      Returns:
    • alloc

      public static java.nio.ByteBuffer alloc​(int[] data)
      Copies the array to a direct buffer.
      Parameters:
      data -
      Returns:
    • alloc

      public static java.nio.ByteBuffer alloc​(short[] data)
      Copies the array to a direct buffer.
      Parameters:
      data -
      Returns:
    • alloc

      public static java.nio.ByteBuffer alloc​(byte[] data)
      Copies the array to a direct buffer.
      Parameters:
      data -
      Returns:
    • toHalf

      public static short[] toHalf​(short[] dst, int doff, float[] src, int soff, int len)
      Utility function for converting a buffer from java float to opencl half
      Parameters:
      dst -
      doff -
      src -
      soff -
      len -
      Returns:
    • toHalf

      public static short[] toHalf​(float[] src)
    • toFloat

      public static float[] toFloat​(float[] dst, int doff, short[] src, int soff, int len)
    • toFloat

      public static float[] toFloat​(short[] src)
    • toFloat

      public static float toFloat​(short hbits)
    • toHalf

      public static short toHalf​(float fval)