Skip to content

Commit

Permalink
several small refactorings, api and javadoc improvements.
Browse files Browse the repository at this point in the history
added automatically generated specialized CLExceptions for each known OpenCL error.
added get/set Properties to CLCommandQueue.
  • Loading branch information
mbien committed Feb 22, 2010
1 parent b014104 commit 7c83da1
Show file tree
Hide file tree
Showing 13 changed files with 814 additions and 195 deletions.
1 change: 1 addition & 0 deletions resources/cl-common.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ NioDirectOnly clSetCommandQueueProperty
NioDirectOnly clWaitForEvents
#NioDirectOnly clCreateContext
#NioDirectOnly clBuildProgram
NioDirectOnly clIcdGetPlatformIDsKHR

#extensions
NioDirectOnly clGetGLContextInfoKHR
Expand Down
59 changes: 45 additions & 14 deletions src/com/mbien/opencl/CLCommandQueue.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@
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
* The command queue is used to queue a set of operations for a specific {@link CLDevice}.
* 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.<br/>
* Sharing of objects across multiple command-queues or using a CLCommandQueue
* Sharing of objects across multiple queues or using a CLCommandQueue
* form multiple Threads will require the application to perform appropriate synchronization.
* @see CLDevice#createCommandQueue(com.mbien.opencl.CLCommandQueue.Mode[])
* @author Michael Bien
*/
public class CLCommandQueue implements CLResource {
Expand Down Expand Up @@ -49,7 +50,7 @@ public class CLCommandQueue implements CLResource {
this.ID = cl.clCreateCommandQueue(context.ID, device.ID, properties, status, 0);

if(status[0] != CL_SUCCESS)
throw new CLException(status[0], "can not create command queue on "+device);
throw newException(status[0], "can not create command queue on "+device);
}

public CLCommandQueue putWriteBuffer(CLBuffer<?> writeBuffer, boolean blockingRead) {
Expand All @@ -64,7 +65,7 @@ public CLCommandQueue putWriteBuffer(CLBuffer<?> writeBuffer, boolean blockingWr
0, null, events==null ? null : events.IDs);

if(ret != CL_SUCCESS)
throw new CLException(ret, "can not enqueue WriteBuffer: " + writeBuffer);
throw newException(ret, "can not enqueue WriteBuffer: " + writeBuffer);

if(events != null) {
events.createEvent(context);
Expand All @@ -86,7 +87,7 @@ public CLCommandQueue putReadBuffer(CLBuffer<?> readBuffer, boolean blockingRead
0, null, events==null ? null : events.IDs);

if(ret != CL_SUCCESS)
throw new CLException(ret, "can not enqueue ReadBuffer: " + readBuffer);
throw newException(ret, "can not enqueue ReadBuffer: " + readBuffer);

if(events != null) {
events.createEvent(context);
Expand Down Expand Up @@ -710,7 +711,7 @@ public CLCommandQueue putNDRangeKernel(CLKernel kernel, int workDimension, Point
events==null ? null : events.IDs);

if(ret != CL_SUCCESS)
throw new CLException(ret, "can not enqueue NDRangeKernel: " + kernel);
throw newException(ret, "can not enqueue NDRangeKernel: " + kernel);

if(events != null) {
events.createEvent(context);
Expand All @@ -733,7 +734,7 @@ public CLCommandQueue putAcquireGLObject(long glObject, CLEventList events) {
events==null ? null : events.IDs);

if(ret != CL_SUCCESS)
throw new CLException(ret, "can not aquire GLObject: " + glObject);
throw newException(ret, "can not aquire GLObject: " + glObject);

if(events != null) {
events.createEvent(context);
Expand All @@ -756,7 +757,7 @@ public CLCommandQueue putReleaseGLObject(long glObject, CLEventList events) {
events==null ? null : events.IDs);

if(ret != CL_SUCCESS)
throw new CLException(ret, "can not release GLObject: " + glObject);
throw newException(ret, "can not release GLObject: " + glObject);

if(events != null) {
events.createEvent(context);
Expand All @@ -779,10 +780,10 @@ public boolean isProfilingEnabled() {
}

/**
* Returns true only when {@link Mode#OUT_OF_ORDER_EXEC_MODE} mode has been enabled.
* Returns true only when {@link Mode#OUT_OF_ORDER_MODE} mode has been enabled.
*/
public boolean isOutOfOrderModeEnabled() {
return (Mode.OUT_OF_ORDER_EXEC_MODE.QUEUE_MODE & properties) != 0;
return (Mode.OUT_OF_ORDER_MODE.QUEUE_MODE & properties) != 0;
}

public void release() {
Expand All @@ -803,6 +804,36 @@ private final static PointerBuffer copy2NIO(PointerBuffer buffer, long a, long b
return buffer.rewind().put(a).put(b).put(c).rewind();
}

/**
* Returns the device of this command queue.
*/
public CLDevice getDevice() {
return device;
}

/**
* Returns the command queue properties as EnumSet.
*/
public EnumSet<Mode> getProperties() {
return Mode.valuesOf(properties);
}

/**
* Setting properties after a command queue has been created can be implementation specific,
* please refere to the specification ({@native clSetCommandQueueProperty}) or vendor documentation.
*/
public void setProperty(Mode property, boolean enabled) {
int ret = cl.clSetCommandQueueProperty(ID, property.QUEUE_MODE, enabled ? CL_TRUE : CL_FALSE, null);
if(ret != CL_SUCCESS) {
checkForError(ret, "can not set command queue property: " + property);
}
if(enabled) {
properties |= property.QUEUE_MODE;
}else{
properties &= ~property.QUEUE_MODE;
}
}

@Override
public boolean equals(Object obj) {
if (obj == null) {
Expand Down Expand Up @@ -841,7 +872,7 @@ public enum Mode {
* 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),
OUT_OF_ORDER_MODE(CL_QUEUE_OUT_OF_ORDER_EXEC_MODE_ENABLE),

/**
* Enables profiling of commands in the command-queue.
Expand All @@ -862,14 +893,14 @@ private Mode(int value) {
public static Mode valueOf(int queueMode) {
switch(queueMode) {
case(CL_QUEUE_OUT_OF_ORDER_EXEC_MODE_ENABLE):
return OUT_OF_ORDER_EXEC_MODE;
return OUT_OF_ORDER_MODE;
case(CL_QUEUE_PROFILING_ENABLE):
return PROFILING_MODE;
}
return null;
}

public static EnumSet<Mode> valuesOf(int bitfield) {
public static EnumSet<Mode> valuesOf(long bitfield) {
List<Mode> matching = new ArrayList<Mode>();
Mode[] values = Mode.values();
for (Mode value : values) {
Expand Down
54 changes: 33 additions & 21 deletions src/com/mbien/opencl/CLContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,13 @@ public class CLContext implements CLResource {
protected final List<CLSampler> samplers;
protected final List<CLMemory<? extends Buffer>> memoryObjects;
protected final Map<CLDevice, List<CLCommandQueue>> queuesMap;
protected final CLPlatform platform;


protected CLContext(long contextID) {
this.cl = CLPlatform.getLowLevelBinding();
protected CLContext(CLPlatform platform, long contextID) {
this.cl = CLPlatform.getLowLevelCLInterface();
this.ID = contextID;
this.platform = platform;
this.programs = new ArrayList<CLProgram>();
this.samplers = new ArrayList<CLSampler>();
this.memoryObjects = new ArrayList<CLMemory<? extends Buffer>>();
Expand Down Expand Up @@ -82,8 +84,7 @@ private final void initDevices() {
* The platform to be used is implementation dependent.
*/
public static final CLContext create() {
PointerBuffer properties = setupContextProperties(null);
return new CLContext(createContextFromType(properties, CL.CL_DEVICE_TYPE_ALL));
return create((CLPlatform)null, Type.ALL);
}

/**
Expand Down Expand Up @@ -115,10 +116,14 @@ public static final CLContext create(CLPlatform platform) {
*/
public static final CLContext create(CLPlatform platform, CLDevice.Type... deviceTypes) {

if(platform == null) {
platform = CLPlatform.getDefault();
}

long type = toDeviceBitmap(deviceTypes);

PointerBuffer properties = setupContextProperties(platform);
return new CLContext(createContextFromType(properties, type));
return new CLContext(platform, createContextFromType(properties, type));
}

/**
Expand All @@ -127,8 +132,12 @@ public static final CLContext create(CLPlatform platform, CLDevice.Type... devic
*/
public static final CLContext create(CLPlatform platform, CLDevice... devices) {

if(platform == null) {
platform = CLPlatform.getDefault();
}

PointerBuffer properties = setupContextProperties(platform);
CLContext context = new CLContext(createContext(properties, devices));
CLContext context = new CLContext(platform, createContext(properties, devices));
if(devices != null) {
for (int i = 0; i < devices.length; i++) {
devices[i].setContext(context);
Expand All @@ -140,7 +149,7 @@ public static final CLContext create(CLPlatform platform, CLDevice... devices) {
protected static final long createContextFromType(PointerBuffer properties, long deviceType) {

IntBuffer status = IntBuffer.allocate(1);
long context = CLPlatform.getLowLevelBinding().clCreateContextFromType(properties, deviceType, null, null, status);
long context = CLPlatform.getLowLevelCLInterface().clCreateContextFromType(properties, deviceType, null, null, status);

checkForError(status.get(), "can not create CL context");

Expand All @@ -157,7 +166,7 @@ protected static final long createContext(PointerBuffer properties, CLDevice...
pb.put(i, devices[i].ID);
}
}
long context = CLPlatform.getLowLevelBinding().clCreateContext(properties, pb, null, null, status);
long context = CLPlatform.getLowLevelCLInterface().clCreateContext(properties, pb, null, null, status);

checkForError(status.get(), "can not create CL context");

Expand All @@ -166,10 +175,6 @@ protected static final long createContext(PointerBuffer properties, CLDevice...

private static final PointerBuffer setupContextProperties(CLPlatform platform) {

if(platform == null) {
platform = CLPlatform.getDefault();
}

if(platform == null) {
throw new RuntimeException("no OpenCL installation found");
}
Expand Down Expand Up @@ -375,24 +380,31 @@ public void release() {
checkForError(ret, "error releasing context");
}

/**
* Returns the CLPlatform this context is running on.
*/
public CLPlatform getPlatform() {
return platform;
}

/**
* Returns a read only view of all programs associated with this context.
*/
public List<CLProgram> getCLPrograms() {
public List<CLProgram> getPrograms() {
return Collections.unmodifiableList(programs);
}

/**
* Returns a read only view of all allocated memory objects associated with this context.
*/
public List<CLMemory<? extends Buffer>> getCLMemoryObjects() {
public List<CLMemory<? extends Buffer>> getMemoryObjects() {
return Collections.unmodifiableList(memoryObjects);
}

/**
* Returns a read only view of all samplers associated with this context.
*/
public List<CLSampler> getCLSamplers() {
public List<CLSampler> getSamplers() {
return Collections.unmodifiableList(samplers);
}

Expand All @@ -403,7 +415,7 @@ public List<CLSampler> getCLSamplers() {
* @see #getMaxFlopsDevice(com.mbien.opencl.CLDevice.Type)
*/
public CLDevice getMaxFlopsDevice() {
return CLPlatform.findMaxFlopsDevice(getCLDevices());
return CLPlatform.findMaxFlopsDevice(getDevices());
}

/**
Expand All @@ -412,19 +424,19 @@ public CLDevice getMaxFlopsDevice() {
* MAX_COMPUTE_UNITS and MAX_CLOCK_FREQUENCY.
*/
public CLDevice getMaxFlopsDevice(CLDevice.Type type) {
return CLPlatform.findMaxFlopsDevice(getCLDevices(), type);
return CLPlatform.findMaxFlopsDevice(getDevices(), type);
}

/**
* Returns all devices associated with this CLContext.
*/
public CLDevice[] getCLDevices() {
public CLDevice[] getDevices() {
initDevices();
return devices;
}

CLDevice getCLDevice(long dID) {
CLDevice[] deviceArray = getCLDevices();
CLDevice getDevice(long dID) {
CLDevice[] deviceArray = getDevices();
for (int i = 0; i < deviceArray.length; i++) {
if(dID == deviceArray[i].ID)
return deviceArray[i];
Expand All @@ -445,7 +457,7 @@ protected static long toDeviceBitmap(Type[] deviceTypes) {
@Override
public String toString() {
return "CLContext [id: " + ID
+ " #devices: " + getCLDevices().length
+ " #devices: " + getDevices().length
+ "]";
}

Expand Down
10 changes: 7 additions & 3 deletions src/com/mbien/opencl/CLDevice.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@
import static com.mbien.opencl.CL.*;

/**
*
* This object represents an OpenCL device.
* @see CLPlatform#listCLDevices(com.mbien.opencl.CLDevice.Type)
* @see CLPlatform#getMaxFlopsDevice(com.mbien.opencl.CLDevice.Type)
* @see CLContext#getDevices()
* @see CLContext#getMaxFlopsDevice(com.mbien.opencl.CLDevice.Type)
* @author Michael Bien
*/
public final class CLDevice {
Expand Down Expand Up @@ -68,11 +72,11 @@ public CLCommandQueue createCommandQueue(long properties) {
return context.createCommandQueue(this, properties);
}

/*keep this package private for now, may be null*/
CLContext getContext() {
public CLContext getContext() {
return context;
}

/*keep this package private*/
void setContext(CLContext context) {
this.context = context;
}
Expand Down
Loading

0 comments on commit 7c83da1

Please sign in to comment.