aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMichael Bien <[email protected]>2009-09-30 17:59:22 +0200
committerMichael Bien <[email protected]>2009-09-30 17:59:22 +0200
commita550876d23b84427667111c5e2700766752b6040 (patch)
treede61e1605ebb782625437f1b8e5888ca54ae6862
parent5db5f6ac97894bcb9804e4bfcc4607cfdae637a6 (diff)
started with high level abstraction.
-rw-r--r--src/com/mbien/opencl/CLContext.java45
-rw-r--r--src/com/mbien/opencl/CLDevice.java42
-rw-r--r--src/com/mbien/opencl/CLException.java134
-rw-r--r--src/com/mbien/opencl/CLPlatform.java96
-rw-r--r--test/com/mbien/opencl/JOCLTest.java90
5 files changed, 378 insertions, 29 deletions
diff --git a/src/com/mbien/opencl/CLContext.java b/src/com/mbien/opencl/CLContext.java
new file mode 100644
index 00000000..dc3c7279
--- /dev/null
+++ b/src/com/mbien/opencl/CLContext.java
@@ -0,0 +1,45 @@
+package com.mbien.opencl;
+
+import com.mbien.opencl.impl.CLImpl;
+
+/**
+ *
+ * @author Michael Bien
+ */
+public class CLContext {
+
+ private final static CL cl;
+
+ static{
+ System.loadLibrary("gluegen-rt");
+ System.loadLibrary("jocl");
+ cl = new CLImpl();
+ }
+
+ /**
+ * Lists all available OpenCL implementaitons.
+ * @throws CLException if something went wrong initializing OpenCL
+ */
+ public static CLPlatform[] listCLPlatforms() {
+
+ 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");
+
+ // 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");
+
+ CLPlatform[] platforms = new CLPlatform[platformId.length];
+
+ for (int i = 0; i < platformId.length; i++)
+ platforms[i] = new CLPlatform(cl, platformId[i]);
+
+ return platforms;
+ }
+
+}
diff --git a/src/com/mbien/opencl/CLDevice.java b/src/com/mbien/opencl/CLDevice.java
new file mode 100644
index 00000000..58d35fa9
--- /dev/null
+++ b/src/com/mbien/opencl/CLDevice.java
@@ -0,0 +1,42 @@
+package com.mbien.opencl;
+
+import java.nio.ByteBuffer;
+
+/**
+ *
+ * @author Michael Bien
+ */
+public class CLDevice {
+
+ private final CL cl;
+ private final long deviceID;
+
+ CLDevice(CL cl, long id) {
+ this.cl = cl;
+ this.deviceID = id;
+ }
+
+ /**
+ * Returns the name of this device.
+ */
+ public String getName() {
+ return getInfoString(CL.CL_DEVICE_NAME);
+ }
+
+ public String getInfoString(int key) {
+
+ long[] longBuffer = new long[1];
+ ByteBuffer bb = ByteBuffer.allocate(512);
+
+ int ret = cl.clGetDeviceInfo(deviceID, key, bb.capacity(), bb, longBuffer, 0);
+
+ if(CL.CL_SUCCESS != ret)
+ throw new CLException(ret, "can not receive info string");
+
+ return new String(bb.array(), 0, (int)longBuffer[0]);
+
+ }
+
+// ret = cl.clGetDeviceInfo(device, CL.CL_DEVICE_TYPE, bb.capacity(), bb, longBuffer, 0);
+// assertEquals(CL.CL_SUCCESS, ret);
+}
diff --git a/src/com/mbien/opencl/CLException.java b/src/com/mbien/opencl/CLException.java
new file mode 100644
index 00000000..c81dc039
--- /dev/null
+++ b/src/com/mbien/opencl/CLException.java
@@ -0,0 +1,134 @@
+package com.mbien.opencl;
+
+/**
+ * Main Exception type for runtime OpenCL errors and unsuccessfull function calls (e.g. returning other values than CL_SUCCESS).
+ * @author Michael Bien
+ */
+public class CLException extends RuntimeException {
+
+ public CLException(Throwable cause) {
+ super(cause);
+ }
+
+ public CLException(String message, Throwable cause) {
+ super(message, cause);
+ }
+
+ public CLException(String message) {
+ super(message);
+ }
+
+ public CLException(int error, String message) {
+ super(resolveError(error) + ": " + message);
+ }
+
+
+ private static final String resolveError(int error) {
+
+ switch (error) {
+ case CL.CL_INVALID_VALUE:
+ return "CL_INVALID_VALUE";
+
+ case CL.CL_INVALID_DEVICE_TYPE:
+ return "CL_INVALID_DEVICE_TYPE";
+
+ case CL.CL_INVALID_PLATFORM:
+ return "CL_INVALID_PLATFORM";
+
+ case CL.CL_INVALID_DEVICE:
+ return "CL_INVALID_DEVICE";
+
+ case CL.CL_INVALID_CONTEXT:
+ return "CL_INVALID_CONTEXT";
+
+ case CL.CL_INVALID_QUEUE_PROPERTIES:
+ return "CL_INVALID_QUEUE_PROPERTIES";
+
+ case CL.CL_INVALID_COMMAND_QUEUE:
+ return "CL_INVALID_COMMAND_QUEUE";
+
+ case CL.CL_INVALID_HOST_PTR:
+ return "CL_INVALID_HOST_PTR";
+
+ case CL.CL_INVALID_MEM_OBJECT:
+ return "CL_INVALID_MEM_OBJECT";
+
+ case CL.CL_INVALID_IMAGE_FORMAT_DESCRIPTOR:
+ return "CL_INVALID_IMAGE_FORMAT_DESCRIPTOR";
+
+ case CL.CL_INVALID_IMAGE_SIZE:
+ return "CL_INVALID_IMAGE_SIZE";
+
+ case CL.CL_INVALID_SAMPLER:
+ return "CL_INVALID_SAMPLER";
+
+ case CL.CL_INVALID_BINARY:
+ return "CL_INVALID_BINARY";
+
+ case CL.CL_INVALID_BUILD_OPTIONS:
+ return "CL_INVALID_BUILD_OPTIONS";
+
+ case CL.CL_INVALID_PROGRAM:
+ return "CL_INVALID_PROGRAM";
+
+ case CL.CL_INVALID_PROGRAM_EXECUTABLE:
+ return "CL_INVALID_PROGRAM_EXECUTABLE";
+
+ case CL.CL_INVALID_KERNEL_NAME:
+ return "CL_INVALID_KERNEL_NAME";
+
+ case CL.CL_INVALID_KERNEL_DEFINITION:
+ return "CL_INVALID_KERNEL_DEFINITION";
+
+ case CL.CL_INVALID_KERNEL:
+ return "CL_INVALID_KERNEL";
+
+ case CL.CL_INVALID_ARG_INDEX:
+ return "CL_INVALID_ARG_INDEX";
+
+ case CL.CL_INVALID_ARG_VALUE:
+ return "CL_INVALID_ARG_VALUE";
+
+ case CL.CL_INVALID_ARG_SIZE:
+ return "CL_INVALID_ARG_SIZE";
+
+ case CL.CL_INVALID_KERNEL_ARGS:
+ return "CL_INVALID_KERNEL_ARGS";
+
+ case CL.CL_INVALID_WORK_DIMENSION:
+ return "CL_INVALID_WORK_DIMENSION";
+
+ case CL.CL_INVALID_WORK_GROUP_SIZE:
+ return "CL_INVALID_WORK_GROUP_SIZE";
+
+ case CL.CL_INVALID_WORK_ITEM_SIZE:
+ return "CL_INVALID_WORK_ITEM_SIZE";
+
+ case CL.CL_INVALID_GLOBAL_OFFSET:
+ return "CL_INVALID_GLOBAL_OFFSET";
+
+ case CL.CL_INVALID_EVENT_WAIT_LIST:
+ return "CL_INVALID_EVENT_WAIT_LIST";
+
+ case CL.CL_INVALID_EVENT:
+ return "CL_INVALID_EVENT";
+
+ case CL.CL_INVALID_OPERATION:
+ return "CL_INVALID_OPERATION";
+
+ case CL.CL_INVALID_GL_OBJECT:
+ return "CL_INVALID_GL_OBJECT";
+
+ case CL.CL_INVALID_BUFFER_SIZE:
+ return "CL_INVALID_BUFFER_SIZE";
+
+ case CL.CL_INVALID_MIP_LEVEL:
+ return "CL_INVALID_MIP_LEVEL";
+
+ default:
+ return "unknown cause";
+ }
+ }
+
+
+}
diff --git a/src/com/mbien/opencl/CLPlatform.java b/src/com/mbien/opencl/CLPlatform.java
new file mode 100644
index 00000000..582e8abc
--- /dev/null
+++ b/src/com/mbien/opencl/CLPlatform.java
@@ -0,0 +1,96 @@
+package com.mbien.opencl;
+
+import java.nio.ByteBuffer;
+
+/**
+ *
+ * @author Michael Bien
+ */
+public class CLPlatform {
+
+ private final long platformID;
+ private final CL cl;
+
+ CLPlatform(CL cl, long id) {
+ this.platformID = id;
+ this.cl = cl;
+ }
+
+ public CLDevice[] listCLDevices() {
+
+ int[] intBuffer = new int[1];
+
+ //find all devices
+ int ret = cl.clGetDeviceIDs(platformID, CL.CL_DEVICE_TYPE_ALL, 0, null, 0, intBuffer, 0);
+ if(CL.CL_SUCCESS != ret)
+ throw new CLException(ret, "error while enumerating devices");
+
+ long[] deviceIDs = new long[intBuffer[0]];
+ ret = cl.clGetDeviceIDs(platformID, CL.CL_DEVICE_TYPE_ALL, deviceIDs.length, deviceIDs, 0, null, 0);
+ if(CL.CL_SUCCESS != ret)
+ throw new CLException(ret, "error while enumerating devices");
+
+ CLDevice[] devices = new CLDevice[deviceIDs.length];
+
+ //print device info
+ for (int i = 0; i < deviceIDs.length; i++)
+ devices[i] = new CLDevice(cl, deviceIDs[i]);
+
+ return devices;
+
+ }
+
+ /**
+ * Returns the platform name.
+ */
+ public String getName() {
+ return getInfoString(CL.CL_PLATFORM_NAME);
+ }
+
+ /**
+ * Returns the platform version.
+ */
+ public String getVersion() {
+ return getInfoString(CL.CL_PLATFORM_VERSION);
+ }
+
+ /**
+ * Returns the platform profile.
+ */
+ public String getProfile() {
+ return getInfoString(CL.CL_PLATFORM_PROFILE);
+ }
+
+ /**
+ * Returns the platform vendor.
+ */
+ public String getVendor() {
+ return getInfoString(CL.CL_PLATFORM_VENDOR);
+ }
+
+ /**
+ * Returns a info string in exchange for a key (CL_PLATFORM_*).
+ */
+ public String getInfoString(int key) {
+ long[] longBuffer = new long[1];
+ ByteBuffer bb = ByteBuffer.allocate(512);
+
+ int ret = cl.clGetPlatformInfo(platformID, key, bb.capacity(), bb, longBuffer, 0);
+ if(CL.CL_SUCCESS != ret)
+ throw new CLException(ret, "can not receive info string");
+
+ return new String(bb.array(), 0, (int)longBuffer[0]);
+ }
+
+ @Override
+ public String toString() {
+ return "CLPlatform [name:" + getName()
+ +" vendor:"+getVendor()
+ +" profile:"+getProfile()
+ +" version:"+getVersion()+"]";
+ }
+
+
+
+
+}
diff --git a/test/com/mbien/opencl/JOCLTest.java b/test/com/mbien/opencl/JOCLTest.java
index d0b19204..3fdd4536 100644
--- a/test/com/mbien/opencl/JOCLTest.java
+++ b/test/com/mbien/opencl/JOCLTest.java
@@ -28,12 +28,37 @@ public class JOCLTest {
}
@Test
- public void basicLowLevelTest() {
+ public void highLevelTest() {
+ System.out.println(" - - - highLevelTest - - - ");
+
+ CLPlatform[] clPlatforms = CLContext.listCLPlatforms();
+
+ for (CLPlatform platform : clPlatforms) {
+
+ System.out.println("platform info:");
+ System.out.println(platform.getName());
+ System.out.println(platform.getProfile());
+ System.out.println(platform.getVersion());
+ System.out.println(platform.getVendor());
+
+ CLDevice[] clDevices = platform.listCLDevices();
+ for (CLDevice device : clDevices) {
+ System.out.println("device info:");
+ System.out.println(device.getName());
+ }
+ }
+
+ }
- System.out.print("loading native libs...");
- System.loadLibrary("gluegen-rt");
- System.loadLibrary("jocl");
- System.out.println("done");
+ @Test
+ public void lowLevelTest() {
+ System.out.println(" - - - lowLevelTest - - - ");
+
+ // already loaded
+// System.out.print("loading native libs...");
+// System.loadLibrary("gluegen-rt");
+// System.loadLibrary("jocl");
+// System.out.println("done");
CreateContextCallback cb = new CreateContextCallback() {
@Override
@@ -49,6 +74,7 @@ public class JOCLTest {
CL cl = new CLImpl();
int[] intBuffer = new int[1];
+ // find all available OpenCL platforms
ret = cl.clGetPlatformIDs(0, null, 0, intBuffer, 0);
assertEquals(CL.CL_SUCCESS, ret);
System.out.println("#platforms: "+intBuffer[0]);
@@ -56,51 +82,57 @@ public class JOCLTest {
long[] platformId = new long[intBuffer[0]];
ret = cl.clGetPlatformIDs(platformId.length, platformId, 0, null, 0);
assertEquals(CL.CL_SUCCESS, ret);
-
- long[] longBuffer = new long[1];
+ // print platform info
+ long[] longBuffer = new long[1];
ByteBuffer bb = ByteBuffer.allocate(128);
- byte[] str = new byte[128];
for (int i = 0; i < platformId.length; i++) {
long platform = platformId[i];
System.out.println("platform id: "+platform);
- ret = cl.clGetPlatformInfo(platform, CL.CL_PLATFORM_PROFILE, bb.capacity(), bb, null, 0);
+ ret = cl.clGetPlatformInfo(platform, CL.CL_PLATFORM_PROFILE, bb.capacity(), bb, longBuffer, 0);
assertEquals(CL.CL_SUCCESS, ret);
- bb.get(str);
+ System.out.println(" profile: "+new String(bb.array(), 0, (int)longBuffer[0]));
- System.out.println(" profile: "+new String(str));
- Arrays.fill(str, (byte)0);
- bb.rewind();
+ ret = cl.clGetPlatformInfo(platform, CL.CL_PLATFORM_VERSION, bb.capacity(), bb, longBuffer, 0);
+ assertEquals(CL.CL_SUCCESS, ret);
+ System.out.println(" version: "+new String(bb.array(), 0, (int)longBuffer[0]));
- ret = cl.clGetPlatformInfo(platform, CL.CL_PLATFORM_VERSION, bb.capacity(), bb, null, 0);
+ ret = cl.clGetPlatformInfo(platform, CL.CL_PLATFORM_NAME, bb.capacity(), bb, longBuffer, 0);
assertEquals(CL.CL_SUCCESS, ret);
- bb.get(str);
- System.out.println(" version: "+new String(str));
- Arrays.fill(str, (byte)0);
- bb.rewind();
+ System.out.println(" name: "+new String(bb.array(), 0, (int)longBuffer[0]));
- ret = cl.clGetPlatformInfo(platform, CL.CL_PLATFORM_NAME, bb.capacity(), bb, null, 0);
+ ret = cl.clGetPlatformInfo(platform, CL.CL_PLATFORM_VENDOR, bb.capacity(), bb, longBuffer, 0);
assertEquals(CL.CL_SUCCESS, ret);
- bb.get(str);
- System.out.println(" name: "+new String(str));
- Arrays.fill(str, (byte)0);
- bb.rewind();
+ System.out.println(" vendor: "+new String(bb.array(), 0, (int)longBuffer[0]));
- ret = cl.clGetPlatformInfo(platform, CL.CL_PLATFORM_VENDOR, bb.capacity(), bb, null, 0);
+ //find all devices
+ ret = cl.clGetDeviceIDs(platform, CL.CL_DEVICE_TYPE_ALL, 0, null, 0, intBuffer, 0);
assertEquals(CL.CL_SUCCESS, ret);
- bb.get(str);
- System.out.println(" vendor: "+new String(str));
- Arrays.fill(str, (byte)0);
- bb.rewind();
+ System.out.println("#devices: "+intBuffer[0]);
+
+ long[] devices = new long[intBuffer[0]];
+ ret = cl.clGetDeviceIDs(platform, CL.CL_DEVICE_TYPE_ALL, devices.length, devices, 0, null, 0);
+
+ //print device info
+ for (int j = 0; j < devices.length; j++) {
+ long device = devices[j];
+ ret = cl.clGetDeviceInfo(device, CL.CL_DEVICE_NAME, bb.capacity(), bb, longBuffer, 0);
+ assertEquals(CL.CL_SUCCESS, ret);
+ System.out.println(" device: "+new String(bb.array(), 0, (int)longBuffer[0]));
+
+// ret = cl.clGetDeviceInfo(device, CL.CL_DEVICE_TYPE, bb.capacity(), bb, longBuffer, 0);
+// assertEquals(CL.CL_SUCCESS, ret);
+
+
+ }
}
Arrays.fill(longBuffer, 0);
-
long context = cl.clCreateContextFromType(null, CL.CL_DEVICE_TYPE_ALL, cb, null, null);
System.out.println("context handle: "+context);