public abstract class CLMemory extends CLObject
Memory Transfers
Subclasses of CLMemory define various read, write, and map operations. These are performed via ByteBuffer objects or primitive arrays.
Primtive ArraysWhere 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.
ByteBuffersAll 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 Summary
Modifier and Type Method Description static java.nio.ByteBuffer
alloc(byte[] data)
Copies the array to a direct buffer.static java.nio.ByteBuffer
alloc(float[] data)
Copies the array to a direct buffer.static java.nio.ByteBuffer
alloc(int size)
Allocates a buffer suitable for opencl use - sets the byte order.static java.nio.ByteBuffer
alloc(int[] data)
Copies the array to a direct buffer.static java.nio.ByteBuffer
alloc(short[] data)
Copies the array to a direct buffer.static CLMemory
create(jdk.incubator.foreign.MemoryAddress p)
CLBuffer
getAssociatedMemObject()
Get CL_MEM_ASSOCIATED_MEMOBJECT for a sub-buffer.CLContext
getContext()
Get CL_MEM_CONTEXT.long
getFlags()
Get CL_MEM_FLAGS.java.nio.ByteBuffer
getHostPtr()
Get CL_MEM_HOST_PTR.long
getMemOffset()
Get CL_MEM_OFFSET for a sub-buffer.long
getSize()
Get CL_MEM_SIZE.int
getType()
Get CL_MEM_TYPE.void
release()
If CL_MEM_USE_HOST_PTR was used at creation then this must be invoked to avoid a memory leak.void
setMemObjectDestructorCallback(CLNotify<CLMemory> notify)
Call clSetMemObjectDestructorCallback.static float[]
toFloat(float[] dst, int doff, short[] src, int soff, int len)
static float
toFloat(short hbits)
static float[]
toFloat(short[] src)
static short
toHalf(float fval)
static short[]
toHalf(float[] src)
static short[]
toHalf(short[] dst, int doff, float[] src, int soff, int len)
Utility function for converting a buffer from java float to opencl halfMethods inherited from class au.notzed.zcl.CLObject
getInfo, getInfo, getInfo, getInfoAny, getInfoAny, getInfoAny, getInfoAny, getInfoAnyV, getInfoByteV, getInfoByteV, getInfoInt, getInfoInt, getInfoInt, getInfoInt, getInfoJava, getInfoLong, getInfoLong, getInfoLong, getInfoLong, getInfoLongV, getInfoLongV, getInfoPropertyV, getInfoSizeT, getInfoSizeT, getInfoSizeT, getInfoSizeTA, getInfoString, getInfoString, getInfoString, getInfoString, release, release
Methods inherited from class api.Native
addr, addr, addr, debugDumpReachable, debugFlushAll, downcallHandle, downcallHandle, dumpSignature, freeUpcallStub, getAddr, getAddr, getByte, getByte, getDouble, getDouble, getFloat, getFloat, getInt, getInt, getLong, getLong, getShort, loadLibraries, parseStruct, parseUnion, release, release, resolve, setAddr, setAddr, setByte, setByte, setDouble, setDouble, setFloat, setFloat, setInt, setInt, setLong, setLong, setShort, setShort, toAddrV, toAddrV, toAddrV, toByteV, toByteV, toLongV, toLongV, toObjectV, toObjectV, toString, upcallStub
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Method Details
-
create
-
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. -
setMemObjectDestructorCallback
public void setMemObjectDestructorCallback(CLNotify<CLMemory> notify) throws CLException, java.lang.UnsupportedOperationExceptionCall 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
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
Get CL_MEM_CONTEXT.- Returns:
- An interface to the context this memory was created on.
-
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)
-