aboutsummaryrefslogtreecommitdiffstats
path: root/src/com/mbien/opencl/CLContext.java
diff options
context:
space:
mode:
authorMichael Bien <[email protected]>2009-10-19 02:56:04 +0200
committerMichael Bien <[email protected]>2009-10-19 02:56:04 +0200
commit9abfd00399e4ca5c351df9cdf25cd85c960b3d44 (patch)
tree0b44ac0da5c03fdd08a4039b25238c240bda1857 /src/com/mbien/opencl/CLContext.java
parent01ae874925c4471d76ee6b18f15bf201c6f12f48 (diff)
initial import of CLProgram. Updated JUnit Test accordingly.
Diffstat (limited to 'src/com/mbien/opencl/CLContext.java')
-rw-r--r--src/com/mbien/opencl/CLContext.java107
1 files changed, 85 insertions, 22 deletions
diff --git a/src/com/mbien/opencl/CLContext.java b/src/com/mbien/opencl/CLContext.java
index 203172b0..601a2b79 100644
--- a/src/com/mbien/opencl/CLContext.java
+++ b/src/com/mbien/opencl/CLContext.java
@@ -1,18 +1,28 @@
package com.mbien.opencl;
import com.mbien.opencl.impl.CLImpl;
-import com.sun.gluegen.runtime.PointerBuffer;
+import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
import java.nio.IntBuffer;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import static com.mbien.opencl.CLException.*;
+
/**
*
* @author Michael Bien
*/
public final class CLContext {
- private final static CL cl;
+ final static CL cl;
public final long contextID;
+ private CLDevice[] devices;
+
+ private final List<CLProgram> programs;
+
static{
System.loadLibrary("gluegen-rt");
System.loadLibrary("jocl");
@@ -21,45 +31,66 @@ public final class CLContext {
private CLContext(long contextID) {
this.contextID = contextID;
+ this.programs = new ArrayList<CLProgram>();
}
/**
* Creates a default context on all available devices.
*/
- public static CLContext create() {
- IntBuffer ib = IntBuffer.allocate(1);
- long context = cl.clCreateContextFromType(null, 0, CL.CL_DEVICE_TYPE_ALL, null, null, null, 0);
-
-// int errorCode = ib.get();
-// if(errorCode != CL.CL_SUCCESS)
-// throw new CLException(errorCode, "can not create CL context");
-
- return new CLContext(context);
+ public static final CLContext create() {
+ return createContext(CL.CL_DEVICE_TYPE_ALL);
}
/**
* Creates a default context on the specified device types.
*/
- public static CLContext create(CLDevice.Type... deviceTypes) {
+ public static final CLContext create(CLDevice.Type... deviceTypes) {
int type = deviceTypes[0].CL_TYPE;
for (int i = 1; i < deviceTypes.length; i++) {
type |= deviceTypes[i].CL_TYPE;
}
- long ctxID = cl.clCreateContextFromType(null, 0, type, null, null, null, 0);
- return new CLContext(ctxID);
+ return createContext(type);
+ }
+
+ private static final CLContext createContext(long deviceType) {
+
+ IntBuffer error = IntBuffer.allocate(1);
+ long context = cl.clCreateContextFromType(null, 0, deviceType, null, null, error, 0);
+
+ checkForError(error.get(), "can not create CL context");
+
+ return new CLContext(context);
+ }
+
+ public CLProgram createProgram(String src) {
+ CLProgram program = new CLProgram(this, src, contextID);
+ programs.add(program);
+ return program;
+ }
+
+ void programReleased(CLProgram program) {
+ programs.remove(program);
}
/**
* Releases the context and all resources.
*/
- public void release() {
+ public CLContext release() {
int ret = cl.clReleaseContext(contextID);
- if(CL.CL_SUCCESS != ret)
- throw new CLException(ret, "error releasing context");
+ checkForError(ret, "error releasing context");
+ return this;
+ }
+
+ /**
+ * Returns a read only view of all programs associated with this context.
+ */
+ public List<CLProgram> getPrograms() {
+ return Collections.unmodifiableList(programs);
}
+
/**
* Gets the device with maximal FLOPS from this context.
*/
@@ -95,12 +126,45 @@ public final class CLContext {
return null;
}
+*/
+ /**
+ * Returns all devices associated with this CLContext.
+ */
public CLDevice[] getCLDevices() {
+ if(devices == null) {
+
+ int sizeofDeviceID = 8; // TODO doublechek deviceID size on 32 bit systems
+
+ long[] longBuffer = new long[1];
+
+ int ret;
+ ret = cl.clGetContextInfo(contextID, CL.CL_CONTEXT_DEVICES, 0, null, longBuffer, 0);
+ checkForError(ret, "can not enumerate devices");
+
+ ByteBuffer deviceIDs = ByteBuffer.allocate((int)longBuffer[0]).order(ByteOrder.nativeOrder());
+
+ ret = cl.clGetContextInfo(contextID, CL.CL_CONTEXT_DEVICES, deviceIDs.capacity(), deviceIDs, null, 0);
+ checkForError(ret, "can not enumerate devices");
+
+ devices = new CLDevice[deviceIDs.capacity()/sizeofDeviceID];
+ for (int i = 0; i < devices.length; i++)
+ devices[i] = new CLDevice(cl, deviceIDs.getLong()); // TODO doublechek deviceID size on 32 bit systems
+
+ }
+
+ return devices;
}
-*/
+ CLDevice getCLDevices(long dID) {
+ CLDevice[] deviceArray = getCLDevices();
+ for (int i = 0; i < deviceArray.length; i++) {
+ if(dID == deviceArray[i].deviceID)
+ return deviceArray[i];
+ }
+ return null;
+ }
/**
* Lists all available OpenCL implementaitons.
@@ -111,14 +175,12 @@ public final class CLContext {
int[] intBuffer = new int[1];
// find all available OpenCL platforms
int ret = cl.clGetPlatformIDs(0, null, 0, intBuffer, 0);
- if(CL.CL_SUCCESS != ret)
- throw new CLException(ret, "can not enumerate platforms");
+ checkForError(ret, "can not enumerate platforms");
// receive platform ids
long[] platformId = new long[intBuffer[0]];
ret = cl.clGetPlatformIDs(platformId.length, platformId, 0, null, 0);
- if(CL.CL_SUCCESS != ret)
- throw new CLException(ret, "can not enumerate platforms");
+ checkForError(ret, "can not enumerate platforms");
CLPlatform[] platforms = new CLPlatform[platformId.length];
@@ -135,4 +197,5 @@ public final class CLContext {
return cl;
}
+
}