diff options
Diffstat (limited to 'src/com/mbien')
-rw-r--r-- | src/com/mbien/opencl/CLContext.java | 45 | ||||
-rw-r--r-- | src/com/mbien/opencl/CLDevice.java | 42 | ||||
-rw-r--r-- | src/com/mbien/opencl/CLException.java | 134 | ||||
-rw-r--r-- | src/com/mbien/opencl/CLPlatform.java | 96 |
4 files changed, 317 insertions, 0 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()+"]"; + } + + + + +} |