diff options
author | Michael Bien <[email protected]> | 2010-01-15 16:45:29 +0100 |
---|---|---|
committer | Michael Bien <[email protected]> | 2010-01-15 16:45:29 +0100 |
commit | 64f212c105fd0f23e8116307b64ee4b87928823d (patch) | |
tree | 02adbfa7a3ef1a9b0c5379ae86a9fe1187b7314c /src | |
parent | d446e57f45811c23da651f7a5f481ddaed4c01c6 (diff) |
CLInfoAccessor uses now threadlocal direct nio buffers for all operations.
javadoc improvements.
added event based profiling functionality.
Diffstat (limited to 'src')
-rw-r--r-- | src/com/mbien/opencl/CLCommandQueue.java | 31 | ||||
-rw-r--r-- | src/com/mbien/opencl/CLDevice.java | 5 | ||||
-rw-r--r-- | src/com/mbien/opencl/CLEvent.java | 83 | ||||
-rw-r--r-- | src/com/mbien/opencl/CLInfoAccessor.java | 48 | ||||
-rw-r--r-- | src/com/mbien/opencl/CLSampler.java | 5 |
5 files changed, 137 insertions, 35 deletions
diff --git a/src/com/mbien/opencl/CLCommandQueue.java b/src/com/mbien/opencl/CLCommandQueue.java index 4e88ff1d..06308f08 100644 --- a/src/com/mbien/opencl/CLCommandQueue.java +++ b/src/com/mbien/opencl/CLCommandQueue.java @@ -12,9 +12,9 @@ import static com.mbien.opencl.CL.*; * The command-queue can be used to queue a set of operations in order. Having multiple * command-queues allows applications to queue multiple independent commands without * requiring synchronization. Note that this should work as long as these objects are - * not being shared.<b/> + * not being shared.<br/> * Sharing of objects across multiple command-queues or using a CLCommandQueue - * form multiple Threads will require the application to perform appropriate synchronization.<b/> + * form multiple Threads will require the application to perform appropriate synchronization. * @author Michael Bien */ public class CLCommandQueue implements CLResource { @@ -123,6 +123,20 @@ public class CLCommandQueue implements CLResource { return this; } + + /* + public CLCommandQueue putWriteImage(CLBuffer<?> writeBuffer, boolean blockingWrite, CLEventList events) { + + int ret = cl.clEnqueueWriteImage(ID, writeBuffer.ID, blockingWrite ? CL_TRUE : CL_FALSE, + null,null,0,0,null, 0, null,events==null ? null : events.IDs); + checkForError(ret, "can not write Image"); + + if(events != null) { + events.createEvent(context); + } + return this; + } + */ //TODO implement remaining methods /* @@ -139,11 +153,6 @@ public class CLCommandQueue implements CLResource { return this; } - public CLCommandQueue putWriteImage() { - - return this; - } - public CLCommandQueue putReadImage() { return this; @@ -389,11 +398,15 @@ public class CLCommandQueue implements CLResource { */ public enum Mode { /** - * CL_DEVICE_TYPE_CPU + * If set, the commands in the command-queue are + * executed out-of-order. Otherwise, commands are executed in-order. */ OUT_OF_ORDER_EXEC_MODE(CL_QUEUE_OUT_OF_ORDER_EXEC_MODE_ENABLE), + /** - * CL_DEVICE_TYPE_GPU + * Enables profiling of commands in the command-queue. + * If set, the profiling of commands is enabled. Otherwise profiling of + * commands is disabled. See {@link com.mbien.opencl.CLEvent} for more information. */ PROFILING_MODE(CL_QUEUE_PROFILING_ENABLE); diff --git a/src/com/mbien/opencl/CLDevice.java b/src/com/mbien/opencl/CLDevice.java index ed745e80..7a676fe6 100644 --- a/src/com/mbien/opencl/CLDevice.java +++ b/src/com/mbien/opencl/CLDevice.java @@ -1,5 +1,6 @@ package com.mbien.opencl; +import com.sun.gluegen.runtime.PointerBuffer; import java.nio.Buffer; import java.util.ArrayList; import java.util.Collections; @@ -324,8 +325,8 @@ public final class CLDevice { private class CLDeviceInfoAccessor extends CLInfoAccessor { @Override - protected int getInfo(int name, long valueSize, Buffer value, long[] valueSizeRet, int valueSizeRetOffset) { - return cl.clGetDeviceInfo(ID, name, valueSize, value, valueSizeRet, valueSizeRetOffset); + protected int getInfo(int name, long valueSize, Buffer value, PointerBuffer valueSizeRet) { + return cl.clGetDeviceInfo(ID, name, valueSize, value, valueSizeRet); } } diff --git a/src/com/mbien/opencl/CLEvent.java b/src/com/mbien/opencl/CLEvent.java index 0855ec7e..1855fea9 100644 --- a/src/com/mbien/opencl/CLEvent.java +++ b/src/com/mbien/opencl/CLEvent.java @@ -1,12 +1,17 @@ package com.mbien.opencl; +import com.sun.gluegen.runtime.PointerBuffer; import java.nio.Buffer; import static com.mbien.opencl.CL.*; import static com.mbien.opencl.CLException.*; /** - * + * Event objects can be used for synchronizing command queues, e.g you can wait until a + * event accures or they can also be used to capture profiling information that + * measure execution time of a command. + * Profiling of OpenCL commands can be enabled by using a {@link com.mbien.opencl.CLCommandQueue} created with + * {@link com.mbien.opencl.CLCommandQueue.Mode#PROFILING_MODE}. * @author Michael Bien */ public class CLEvent implements CLResource { @@ -17,12 +22,14 @@ public class CLEvent implements CLResource { private final CL cl; private final CLEventInfoAccessor eventInfo; + private final CLEventProfilingInfoAccessor eventProfilingInfo; CLEvent(CLContext context, long id) { this.context = context; this.cl = context.cl; this.ID = id; this.eventInfo = new CLEventInfoAccessor(); + this.eventProfilingInfo = new CLEventProfilingInfoAccessor(); } public void release() { @@ -40,6 +47,11 @@ public class CLEvent implements CLResource { return CommandType.valueOf(status); } + public long getProfilingInfo(ProfilingCommand command) { + return eventProfilingInfo.getLong(command.COMMAND); + } + + @Override public String toString() { return "CLEvent [id: " + ID @@ -78,12 +90,77 @@ public class CLEvent implements CLResource { private class CLEventInfoAccessor extends CLInfoAccessor { @Override - protected int getInfo(int name, long valueSize, Buffer value, long[] valueSizeRet, int valueSizeRetOffset) { - return cl.clGetEventInfo(ID, name, valueSize, value, valueSizeRet, valueSizeRetOffset); + protected int getInfo(int name, long valueSize, Buffer value, PointerBuffer valueSizeRet) { + return cl.clGetEventInfo(ID, name, valueSize, value, valueSizeRet); + } + + } + + private class CLEventProfilingInfoAccessor extends CLInfoAccessor { + + @Override + protected int getInfo(int name, long valueSize, Buffer value, PointerBuffer valueSizeRet) { + return cl.clGetEventProfilingInfo(ID, name, valueSize, value, valueSizeRet); + } + + } + + // TODO merge with ExecutionStatus? + + public enum ProfilingCommand { + + /** + * A 64-bit value that describes the current device time counter in nanoseconds + * when the command identified by event is enqueued in a command-queue by the host. + */ + QUEUED(CL_PROFILING_COMMAND_QUEUED), + + /** + * A 64-bit value that describes the current device time counter in nanoseconds when + * the command identified by event that has been enqueued is submitted by the host to + * the device associated with the commandqueue. + */ + SUBMIT(CL_PROFILING_COMMAND_SUBMIT), + + /** + * A 64-bit value that describes the current device time counter in nanoseconds when + * the command identified by event starts execution on the device. + */ + START(CL_PROFILING_COMMAND_START), + + /** + * A 64-bit value that describes the current device time counter in nanoseconds when + * the command identified by event has finished execution on the device. + */ + END(CL_PROFILING_COMMAND_END); + + /** + * Value of wrapped OpenCL profiling command. + */ + public final int COMMAND; + + private ProfilingCommand(int command) { + this.COMMAND = command; + } + + public static ProfilingCommand valueOf(int status) { + switch(status) { + case(CL_PROFILING_COMMAND_QUEUED): + return QUEUED; + case(CL_PROFILING_COMMAND_SUBMIT): + return SUBMIT; + case(CL_PROFILING_COMMAND_START): + return START; + case(CL_PROFILING_COMMAND_END): + return END; + } + return null; } } + + public enum ExecutionStatus { /** diff --git a/src/com/mbien/opencl/CLInfoAccessor.java b/src/com/mbien/opencl/CLInfoAccessor.java index e366f7ea..7a303dc3 100644 --- a/src/com/mbien/opencl/CLInfoAccessor.java +++ b/src/com/mbien/opencl/CLInfoAccessor.java @@ -1,5 +1,6 @@ package com.mbien.opencl; +import com.sun.gluegen.runtime.PointerBuffer; import java.nio.Buffer; import java.nio.ByteBuffer; import java.nio.ByteOrder; @@ -8,44 +9,53 @@ import static com.mbien.opencl.CLException.*; /** * Internal utility for common OpenCL clGetFooInfo calls. + * Threadsafe. * @author Michael Bien */ abstract class CLInfoAccessor { - private final static ByteBuffer buffer; - private final static long[] longBuffer; + private final static ThreadLocal<ByteBuffer> localBB = new ThreadLocal<ByteBuffer>() { - // TODO revisit for command queue concurrency - // TODO use direct memory code path as soon gluegen is fixed - static{ - buffer = ByteBuffer.allocate(512); - buffer.order(ByteOrder.nativeOrder()); - longBuffer = new long[1]; - } + @Override + protected ByteBuffer initialValue() { + return ByteBuffer.allocateDirect(512).order(ByteOrder.nativeOrder()); + } - public CLInfoAccessor() { - } + }; + private final static ThreadLocal<PointerBuffer> localPB = new ThreadLocal<PointerBuffer>() { + + @Override + protected PointerBuffer initialValue() { + return PointerBuffer.allocateDirect(1); + } + + }; final long getLong(int key) { - buffer.rewind(); - int ret = getInfo(key, 8, buffer, null, 0); + ByteBuffer buffer = localBB.get(); + int ret = getInfo(key, 8, buffer, null); checkForError(ret, "error while asking for info value"); - return buffer.getLong(); + return buffer.getLong(0); } final String getString(int key) { - - buffer.rewind(); - int ret = getInfo(key, buffer.capacity(), buffer, longBuffer, 0); + + ByteBuffer buffer = localBB.get(); + PointerBuffer pbuffer = localPB.get(); + int ret = getInfo(key, buffer.capacity(), buffer, pbuffer); checkForError(ret, "error while asking for info string"); - return CLUtils.clString2JavaString(buffer.array(), (int)longBuffer[0]); + int clSize = (int)pbuffer.get(0); + byte[] array = new byte[clSize-1]; // last char is always null + buffer.get(array).rewind(); + + return CLUtils.clString2JavaString(array, clSize); } - protected abstract int getInfo(int name, long valueSize, Buffer value, long[] valueSizeRet, int valueSizeRetOffset); + protected abstract int getInfo(int name, long valueSize, Buffer value, PointerBuffer valueSizeRet); } diff --git a/src/com/mbien/opencl/CLSampler.java b/src/com/mbien/opencl/CLSampler.java index 6977e2bd..6732abaf 100644 --- a/src/com/mbien/opencl/CLSampler.java +++ b/src/com/mbien/opencl/CLSampler.java @@ -1,5 +1,6 @@ package com.mbien.opencl; +import com.sun.gluegen.runtime.PointerBuffer; import java.nio.Buffer; import static com.mbien.opencl.CLException.*; @@ -55,8 +56,8 @@ public class CLSampler implements CLResource { private class CLSamplerInfoAccessor extends CLInfoAccessor { @Override - protected int getInfo(int name, long valueSize, Buffer value, long[] valueSizeRet, int valueSizeRetOffset) { - return cl.clGetSamplerInfo(ID, name, valueSize, value, valueSizeRet, valueSizeRetOffset); + protected int getInfo(int name, long valueSize, Buffer value, PointerBuffer valueSizeRet) { + return cl.clGetSamplerInfo(ID, name, valueSize, value, valueSizeRet); } } |