diff options
author | Michael Bien <[email protected]> | 2010-02-18 01:40:40 +0100 |
---|---|---|
committer | Michael Bien <[email protected]> | 2010-02-18 01:40:40 +0100 |
commit | 074e8a18e8f5f77168bde267ab87f3cf285f82be (patch) | |
tree | 5dfd0063968fc2ed1b809b86daa247f8cda27fcd /src | |
parent | 62d9a63caad9d614a4a4ca90956b38ff623242a5 (diff) |
added putCopyBufferToImage, putCopyImageToBuffer and putMap/UnmapBuffer operations to CLCommandQueue.
added buffer mapping test to CLBufferTest.
Diffstat (limited to 'src')
-rw-r--r-- | src/com/mbien/opencl/CLBuffer.java | 23 | ||||
-rw-r--r-- | src/com/mbien/opencl/CLCommandQueue.java | 252 | ||||
-rw-r--r-- | src/com/mbien/opencl/CLContext.java | 16 | ||||
-rw-r--r-- | src/com/mbien/opencl/CLMemory.java | 53 |
4 files changed, 325 insertions, 19 deletions
diff --git a/src/com/mbien/opencl/CLBuffer.java b/src/com/mbien/opencl/CLBuffer.java index fd8611e5..ebde221c 100644 --- a/src/com/mbien/opencl/CLBuffer.java +++ b/src/com/mbien/opencl/CLBuffer.java @@ -10,15 +10,34 @@ import static com.mbien.opencl.CLException.*; */ public class CLBuffer<B extends Buffer> extends CLMemory<B> { + public CLBuffer(CLContext context, long id) { + super(context, id); + } protected CLBuffer(CLContext context, B directBuffer, long id) { super(context, directBuffer, id); } + @SuppressWarnings("unchecked") + static CLBuffer<?> create(CLContext context, int size, int flags) { + + CL cl = context.cl; + int[] result = new int[1]; + + if(isHostPointerFlag(flags)) { + throw new IllegalArgumentException("no host pointer defined"); + } + + long id = cl.clCreateBuffer(context.ID, flags, size, null, result, 0); + checkForError(result[0], "can not create cl buffer"); + + return new CLBuffer(context, id); + } + static <B extends Buffer> CLBuffer<B> create(CLContext context, B directBuffer, int flags) { - if(directBuffer != null && !directBuffer.isDirect()) - throw new IllegalArgumentException("buffer is not a direct buffer"); + if(!directBuffer.isDirect()) + throw new IllegalArgumentException("buffer is not direct"); B host_ptr = null; CL cl = context.cl; diff --git a/src/com/mbien/opencl/CLCommandQueue.java b/src/com/mbien/opencl/CLCommandQueue.java index 0967a27b..1a98db6e 100644 --- a/src/com/mbien/opencl/CLCommandQueue.java +++ b/src/com/mbien/opencl/CLCommandQueue.java @@ -1,6 +1,8 @@ package com.mbien.opencl; import com.sun.gluegen.runtime.PointerBuffer; +import java.nio.ByteBuffer; +import java.nio.IntBuffer; import java.util.ArrayList; import java.util.EnumSet; import java.util.List; @@ -108,14 +110,18 @@ public class CLCommandQueue implements CLResource { } */ + public CLCommandQueue putCopyBuffer(CLBuffer<?> src, CLBuffer<?> dest) { + return putCopyBuffer(src, dest, 0, 0, src.getCLSize(), null); + } + public CLCommandQueue putCopyBuffer(CLBuffer<?> src, CLBuffer<?> dest, long bytesToCopy) { - return putCopyBuffer(src, dest, bytesToCopy, null); + return putCopyBuffer(src, dest, 0, 0, bytesToCopy, null); } - public CLCommandQueue putCopyBuffer(CLBuffer<?> src, CLBuffer<?> dest, long bytesToCopy, CLEventList events) { + public CLCommandQueue putCopyBuffer(CLBuffer<?> src, CLBuffer<?> dest, int srcOffset, int destOffset, long bytesToCopy, CLEventList events) { int ret = cl.clEnqueueCopyBuffer( - ID, src.ID, dest.ID, src.buffer.position(), dest.buffer.position(), bytesToCopy, + ID, src.ID, dest.ID, srcOffset, destOffset, bytesToCopy, 0, null, events==null ? null : events.IDs); checkForError(ret, "can not copy Buffer"); @@ -253,7 +259,7 @@ public class CLCommandQueue implements CLResource { //2D public CLCommandQueue putCopyImage(CLImage2d<?> srcImage, CLImage2d<?> dstImage) { - return putCopyImage(srcImage, dstImage, 0, 0, 0, 0, srcImage.width, srcImage.height, null); + return putCopyImage(srcImage, dstImage, null); } public CLCommandQueue putCopyImage(CLImage2d<?> srcImage, CLImage2d<?> dstImage, CLEventList events) { @@ -288,7 +294,7 @@ public class CLCommandQueue implements CLResource { //3D public CLCommandQueue putCopyImage(CLImage3d<?> srcImage, CLImage3d<?> dstImage) { - return putCopyImage(srcImage, dstImage, 0, 0, 0, 0, 0, 0, srcImage.width, srcImage.height, srcImage.depth, null); + return putCopyImage(srcImage, dstImage, null); } public CLCommandQueue putCopyImage(CLImage3d<?> srcImage, CLImage3d<?> dstImage, CLEventList events) { @@ -323,32 +329,250 @@ public class CLCommandQueue implements CLResource { return this; } - //TODO implement remaining methods - /* - public CLCommandQueue putCopyBufferToImage() { + //2D + public CLCommandQueue putCopyBufferToImage(CLBuffer<?> srcBuffer, CLImage2d<?> dstImage) { + return putCopyBufferToImage(srcBuffer, dstImage, null); + } + + public CLCommandQueue putCopyBufferToImage(CLBuffer<?> srcBuffer, CLImage2d<?> dstImage, CLEventList events) { + return putCopyBufferToImage(srcBuffer, dstImage, 0, 0, 0, dstImage.width, dstImage.height, events); + } + + public CLCommandQueue putCopyBufferToImage(CLBuffer<?> srcBuffer, CLImage2d<?> dstImage, + long srcOffset, int dstOriginX, int dstOriginY, + int rangeX, int rangeY) { + return putCopyBufferToImage(srcBuffer, dstImage, + srcOffset, dstOriginX, dstOriginY, rangeX, rangeY, null); + } + + public CLCommandQueue putCopyBufferToImage(CLBuffer<?> srcBuffer, CLImage2d<?> dstImage, + long srcOffset, int dstOriginX, int dstOriginY, + int rangeX, int rangeY, CLEventList events) { + + copy2NIO(bufferA, dstOriginX, dstOriginY); + copy2NIO(bufferB, rangeX, rangeY); + + int ret = cl.clEnqueueCopyBufferToImage(ID, srcBuffer.ID, dstImage.ID, + srcOffset, bufferA, bufferB, + 0, null, events==null ? null : events.IDs); + checkForError(ret, "can not copy buffer to image2d"); + if(events != null) { + events.createEvent(context); + } return this; } - public CLCommandQueue putCopyImageToBuffer() { + + //3D + public CLCommandQueue putCopyBufferToImage(CLBuffer<?> srcBuffer, CLImage3d<?> dstImage) { + return putCopyBufferToImage(srcBuffer, dstImage, null); + } + + public CLCommandQueue putCopyBufferToImage(CLBuffer<?> srcBuffer, CLImage3d<?> dstImage, CLEventList events) { + return putCopyBufferToImage(srcBuffer, dstImage, 0, 0, 0, 0, dstImage.width, dstImage.height, dstImage.depth, events); + } + + public CLCommandQueue putCopyBufferToImage(CLBuffer<?> srcBuffer, CLImage3d<?> dstImage, + long srcOffset, int dstOriginX, int dstOriginY, int dstOriginZ, + int rangeX, int rangeY, int rangeZ) { + return putCopyBufferToImage(srcBuffer, dstImage, + srcOffset, dstOriginX, dstOriginY, dstOriginZ, rangeX, rangeY, rangeZ, null); + + } + + public CLCommandQueue putCopyBufferToImage(CLBuffer<?> srcBuffer, CLImage3d<?> dstImage, + long srcOffset, int dstOriginX, int dstOriginY, int dstOriginZ, + int rangeX, int rangeY, int rangeZ, CLEventList events) { + copy2NIO(bufferA, dstOriginX, dstOriginY, dstOriginZ); + copy2NIO(bufferB, rangeX, rangeY, rangeZ); + + int ret = cl.clEnqueueCopyBufferToImage(ID, srcBuffer.ID, dstImage.ID, + srcOffset, bufferA, bufferB, + 0, null, events==null ? null : events.IDs); + checkForError(ret, "can not copy buffer to image3d"); + + if(events != null) { + events.createEvent(context); + } return this; } - public CLBuffer putMapBuffer() { - - return null; + //2D + public CLCommandQueue putCopyImageToBuffer(CLImage2d<?> srcImage, CLBuffer<?> dstBuffer) { + return putCopyImageToBuffer(srcImage, dstBuffer, null); + } + + public CLCommandQueue putCopyImageToBuffer(CLImage2d<?> srcImage, CLBuffer<?> dstBuffer, CLEventList events) { + return putCopyImageToBuffer(srcImage, dstBuffer, 0, 0, srcImage.width, srcImage.height, 0, events); } + + public CLCommandQueue putCopyImageToBuffer(CLImage2d<?> srcImage, CLBuffer<?> dstBuffer, + int srcOriginX, int srcOriginY, + int rangeX, int rangeY, long dstOffset) { + return putCopyImageToBuffer(srcImage, dstBuffer, + srcOriginX, srcOriginY, rangeX, rangeY, dstOffset, null); + } + + public CLCommandQueue putCopyImageToBuffer(CLImage2d<?> srcImage, CLBuffer<?> dstBuffer, + int srcOriginX, int srcOriginY, + int rangeX, int rangeY, long dstOffset, CLEventList events) { + + copy2NIO(bufferA, srcOriginX, srcOriginY); + copy2NIO(bufferB, rangeX, rangeY); - public CLCommandQueue putMapImage() { + int ret = cl.clEnqueueCopyImageToBuffer(ID, dstBuffer.ID, srcImage.ID, + bufferA, bufferB, dstOffset, + 0, null, events==null ? null : events.IDs); + checkForError(ret, "can not copy buffer to image2d"); + if(events != null) { + events.createEvent(context); + } return this; } + + //3D + public CLCommandQueue putCopyImageToBuffer(CLImage3d<?> srcImage, CLBuffer<?> dstBuffer) { + return putCopyImageToBuffer(srcImage, dstBuffer, 0, 0, 0, srcImage.width, srcImage.height, srcImage.depth, 0, null); + } + + public CLCommandQueue putCopyImageToBuffer(CLImage3d<?> srcImage, CLBuffer<?> dstBuffer, CLEventList events) { + return putCopyImageToBuffer(srcImage, dstBuffer, 0, 0, 0, srcImage.width, srcImage.height, srcImage.depth, 0, events); + } + + public CLCommandQueue putCopyImageToBuffer(CLImage3d<?> srcImage, CLBuffer<?> dstBuffer, + int srcOriginX, int srcOriginY, int srcOriginZ, + int rangeX, int rangeY, int rangeZ, long dstOffset) { + return putCopyImageToBuffer(srcImage, dstBuffer, + srcOriginX, srcOriginY, srcOriginZ, rangeX, rangeY, rangeZ, dstOffset, null); + + } + + public CLCommandQueue putCopyImageToBuffer(CLImage3d<?> srcImage, CLBuffer<?> dstBuffer, + int srcOriginX, int srcOriginY, int srcOriginZ, + int rangeX, int rangeY, int rangeZ, long dstOffset, CLEventList events) { + + copy2NIO(bufferA, srcOriginX, srcOriginY, srcOriginZ); + copy2NIO(bufferB, rangeX, rangeY, rangeZ); - public CLCommandQueue putUnmapMemObject() { + int ret = cl.clEnqueueCopyImageToBuffer(ID, dstBuffer.ID, srcImage.ID, + bufferA, bufferB, dstOffset, + 0, null, events==null ? null : events.IDs); + checkForError(ret, "can not copy buffer to image3d"); + if(events != null) { + events.createEvent(context); + } return this; } + + public ByteBuffer putMapBuffer(CLBuffer<?> buffer, CLMemory.Map flag, boolean blockingMap) { + return putMapBuffer(buffer, flag, blockingMap, null); + } + + public ByteBuffer putMapBuffer(CLBuffer<?> buffer, CLMemory.Map flag, boolean blockingMap, CLEventList events) { + return putMapBuffer(buffer, flag, 0, buffer.getCLSize(), blockingMap, events); + } + + public ByteBuffer putMapBuffer(CLBuffer<?> buffer, CLMemory.Map flag, long offset, long length, boolean blockingMap) { + return putMapBuffer(buffer, flag, offset, length, blockingMap, null); + } + + public ByteBuffer putMapBuffer(CLBuffer<?> buffer, CLMemory.Map flag, long offset, long length, boolean blockingMap, CLEventList events) { + IntBuffer error = bufferA.position(0).getBuffer().asIntBuffer(); + ByteBuffer mappedBuffer = cl.clEnqueueMapBuffer(ID, buffer.ID, blockingMap ? CL_TRUE : CL_FALSE, + flag.FLAGS, offset, length, + 0, null, events==null ? null : events.IDs, error); + checkForError(error.get(), "can not map buffer"); + + if(events != null) { + events.createEvent(context); + } + + return mappedBuffer; + } +/* TODO finish putMapImage + // 2D + public ByteBuffer putMapImage(CLImage2d<?> image, CLMemory.Map flag, boolean blockingMap) { + return putMapImage(image, flag, blockingMap, null); + } + + public ByteBuffer putMapImage(CLImage2d<?> image, CLMemory.Map flag, boolean blockingMap, CLEventList events) { + return putMapImage(image, flag, 0, 0, image.width, image.height, blockingMap, events); + } + + public ByteBuffer putMapImage(CLImage2d<?> buffer, CLMemory.Map flag, int offsetX, int offsetY, + int rangeX, int rangeY, boolean blockingMap) { + return putMapImage(buffer, flag, offsetX, offsetY, rangeX, rangeY, blockingMap, null); + } + + public ByteBuffer putMapImage(CLImage2d<?> buffer, CLMemory.Map flag, + int offsetX, int offsetY, + int rangeX, int rangeY, boolean blockingMap, CLEventList events) { + IntBuffer error = bufferA.position(0).getBuffer().asIntBuffer(); + copy2NIO(bufferB, offsetX, offsetY); + copy2NIO(bufferC, rangeX, rangeY); + ByteBuffer mappedImage = cl.clEnqueueMapImage(ID, buffer.ID, blockingMap ? CL_TRUE : CL_FALSE, + flag.FLAGS, bufferB, bufferC, null, null, + 0, null, events==null ? null : events.IDs, error); + checkForError(error.get(), "can not map image2d"); + + if(events != null) { + events.createEvent(context); + } + + return mappedImage; + } + + // 3D + public ByteBuffer putMapImage(CLImage3d<?> image, CLMemory.Map flag, boolean blockingMap) { + return putMapImage(image, flag, blockingMap, null); + } + + public ByteBuffer putMapImage(CLImage3d<?> image, CLMemory.Map flag, boolean blockingMap, CLEventList events) { + return putMapImage(image, flag, 0, 0, 0, image.width, image.height, image.depth, blockingMap, events); + } + + public ByteBuffer putMapImage(CLImage3d<?> image, CLMemory.Map flag, + int offsetX, int offsetY, int offsetZ, + int rangeX, int rangeY, int rangeZ, boolean blockingMap) { + return putMapImage(image, flag, offsetX, offsetY, offsetZ, rangeX, rangeY, rangeZ, blockingMap, null); + } + + public ByteBuffer putMapImage(CLImage3d<?> buffer, CLMemory.Map flag, + int offsetX, int offsetY, int offsetZ, + int rangeX, int rangeY, int rangeZ, boolean blockingMap, CLEventList events) { + IntBuffer error = bufferA.position(0).getBuffer().asIntBuffer(); + copy2NIO(bufferB, offsetX, offsetY, offsetZ); + copy2NIO(bufferC, rangeX, rangeY, rangeZ); + ByteBuffer mappedImage = cl.clEnqueueMapImage(ID, buffer.ID, blockingMap ? CL_TRUE : CL_FALSE, + flag.FLAGS, bufferB, bufferC, null, null, + 0, null, events==null ? null : events.IDs, error); + checkForError(error.get(), "can not map image3d"); + + if(events != null) { + events.createEvent(context); + } + + return mappedImage; + } */ + public CLCommandQueue putUnmapMemory(CLMemory<?> memory) { + return putUnmapMemory(memory, null); + } + + public CLCommandQueue putUnmapMemory(CLMemory<?> memory, CLEventList events) { + int ret = cl.clEnqueueUnmapMemObject(ID, memory.ID, memory.getBuffer(), + 0, null, events==null ? null : events.IDs); + checkForError(ret, "can not unmap memory"); + + if(events != null) { + events.createEvent(context); + } + return this; + } + public CLCommandQueue putMarker(CLEventList events) { int ret = cl.clEnqueueMarker(CL_INT_MIN, events.IDs); checkForError(ret, "can not enqueue marker"); diff --git a/src/com/mbien/opencl/CLContext.java b/src/com/mbien/opencl/CLContext.java index 99322035..f92213fc 100644 --- a/src/com/mbien/opencl/CLContext.java +++ b/src/com/mbien/opencl/CLContext.java @@ -278,6 +278,22 @@ public class CLContext implements CLResource { /** * Creates a CLBuffer with the specified flags. No flags creates a MEM.READ_WRITE buffer. */ + public final CLBuffer<?> createBuffer(int size, Mem... flags) { + return createBuffer(size, Mem.flagsToInt(flags)); + } + + /** + * Creates a CLBuffer with the specified flags. + */ + public final CLBuffer<?> createBuffer(int size, int flags) { + CLBuffer<?> buffer = CLBuffer.create(this, size, flags); + memoryObjects.add(buffer); + return buffer; + } + + /** + * Creates a CLBuffer with the specified flags. No flags creates a MEM.READ_WRITE buffer. + */ public final <B extends Buffer> CLBuffer<B> createBuffer(B directBuffer, Mem... flags) { return createBuffer(directBuffer, Mem.flagsToInt(flags)); } diff --git a/src/com/mbien/opencl/CLMemory.java b/src/com/mbien/opencl/CLMemory.java index 296f16f5..2d12ca31 100644 --- a/src/com/mbien/opencl/CLMemory.java +++ b/src/com/mbien/opencl/CLMemory.java @@ -39,7 +39,9 @@ public abstract class CLMemory <B extends Buffer> implements CLResource { } protected static final boolean isHostPointerFlag(int flags) { - return (flags & CL_MEM_COPY_HOST_PTR) != 0 || (flags & CL_MEM_USE_HOST_PTR) != 0; + return (flags & CL_MEM_COPY_HOST_PTR) != 0 + || (flags & CL_MEM_USE_HOST_PTR) != 0 + || (flags & CL_MEM_ALLOC_HOST_PTR)!= 0; } protected static final int sizeOfBufferElem(Buffer buffer) { @@ -176,7 +178,7 @@ public abstract class CLMemory <B extends Buffer> implements CLResource { * to allocate memory from host accessible memory. * {@link #ALLOC_HOST_PTR} and {@link #USE_BUFFER} are mutually exclusive. */ - ALLOC_HOST_PTR(CL_MEM_ALLOC_HOST_PTR), + ALLOCATE_BUFFER(CL_MEM_ALLOC_HOST_PTR), /** * Enum representing CL_MEM_COPY_HOST_PTR. @@ -207,7 +209,7 @@ public abstract class CLMemory <B extends Buffer> implements CLResource { case CL_MEM_USE_HOST_PTR: return Mem.USE_BUFFER; case(CL_MEM_ALLOC_HOST_PTR): - return ALLOC_HOST_PTR; + return ALLOCATE_BUFFER; case CL_MEM_COPY_HOST_PTR: return Mem.COPY_BUFFER; } @@ -228,6 +230,51 @@ public abstract class CLMemory <B extends Buffer> implements CLResource { } } + /** + * Configures the mapping process of + * {@link com.mbien.opencl.CLCommandQueue#putMapBuffer(CLBuffer, CLMemory.Map, boolean)}. + */ + public enum Map { + + /** + * Enum representing CL_MAP_READ | CL_MAP_WRITE. + * This flag specifies that the memory object will be mapped for read and write operation. + */ + READ_WRITE(CL_MAP_READ | CL_MAP_WRITE), + + /** + * Enum representing CL_MAP_WRITE. + * This flag specifies that the memory object will be mapped for write operation. + */ + WRITE(CL_MAP_WRITE), + + /** + * Enum representing CL_MAP_READ. + * This flag specifies that the memory object will be mapped for read operation. + */ + READ(CL_MAP_READ); + + /** + * Value of wrapped OpenCL flag. + */ + public final int FLAGS; + + private Map(int flags) { + this.FLAGS = flags; + } + + public Map valueOf(int flag) { + if(flag == WRITE.FLAGS) + return WRITE; + else if(flag == READ.FLAGS) + return READ; + else if(flag == READ_WRITE.FLAGS) + return READ_WRITE; + return null; + } + + } + } |