diff options
Diffstat (limited to 'src/com/mbien/opencl')
-rw-r--r-- | src/com/mbien/opencl/CLBuffer.java | 13 | ||||
-rw-r--r-- | src/com/mbien/opencl/CLCommandQueue.java | 77 | ||||
-rw-r--r-- | src/com/mbien/opencl/CLContext.java | 25 | ||||
-rw-r--r-- | src/com/mbien/opencl/CLDevice.java | 36 | ||||
-rw-r--r-- | src/com/mbien/opencl/CLKernel.java | 23 | ||||
-rw-r--r-- | src/com/mbien/opencl/CLPlatform.java | 14 | ||||
-rw-r--r-- | src/com/mbien/opencl/CLProgram.java | 45 |
7 files changed, 163 insertions, 70 deletions
diff --git a/src/com/mbien/opencl/CLBuffer.java b/src/com/mbien/opencl/CLBuffer.java index 0f6e34a4..74df71e6 100644 --- a/src/com/mbien/opencl/CLBuffer.java +++ b/src/com/mbien/opencl/CLBuffer.java @@ -10,7 +10,7 @@ import static com.mbien.opencl.CLException.*; public class CLBuffer { public final ByteBuffer buffer; - public final long bufferID; + public final long ID; private final CLContext context; private final CL cl; @@ -26,16 +26,15 @@ public class CLBuffer { int[] intArray = new int[1]; - this.bufferID = cl.clCreateBuffer(context.contextID, flags, directBuffer.capacity(), null, intArray, 0); + this.ID = cl.clCreateBuffer(context.ID, flags, directBuffer.capacity(), null, intArray, 0); checkForError(intArray[0], "can not create cl buffer"); } - public CLBuffer release() { - cl.clReleaseMemObject(bufferID); + public void release() { + cl.clReleaseMemObject(ID); context.bufferReleased(this); - return this; } @Override @@ -50,7 +49,7 @@ public class CLBuffer { if (this.buffer != other.buffer && (this.buffer == null || !this.buffer.equals(other.buffer))) { return false; } - if (this.context.contextID != other.context.contextID) { + if (this.context.ID != other.context.ID) { return false; } return true; @@ -60,7 +59,7 @@ public class CLBuffer { public int hashCode() { int hash = 3; hash = 29 * hash + (this.buffer != null ? this.buffer.hashCode() : 0); - hash = 29 * hash + (int) (this.context.contextID ^ (this.context.contextID >>> 32)); + hash = 29 * hash + (int) (this.context.ID ^ (this.context.ID >>> 32)); return hash; } diff --git a/src/com/mbien/opencl/CLCommandQueue.java b/src/com/mbien/opencl/CLCommandQueue.java new file mode 100644 index 00000000..ff91131b --- /dev/null +++ b/src/com/mbien/opencl/CLCommandQueue.java @@ -0,0 +1,77 @@ +package com.mbien.opencl; + +import static com.mbien.opencl.CLException.*; + +/** + * + * @author Michael Bien + */ +public class CLCommandQueue { + + public final long ID; + private final CLContext context; + private final CL cl; + + CLCommandQueue(CLContext context, CLDevice device, long properties) { + this.context = context; + this.cl = context.cl; + + int[] status = new int[1]; + this.ID = cl.clCreateCommandQueue(context.ID, device.ID, properties, status, 0); + + if(status[0] != CL.CL_SUCCESS) + throw new CLException(status[0], "can not create command queue on "+device); + } + + public CLCommandQueue putWriteBuffer(CLBuffer writeBuffer, boolean blockingWrite) { + + int ret = cl.clEnqueueWriteBuffer( + ID, writeBuffer.ID, blockingWrite ? CL.CL_TRUE : CL.CL_FALSE, + 0, writeBuffer.buffer.capacity(), writeBuffer.buffer, + 0, null, 0, + null, 0 ); + + if(ret != CL.CL_SUCCESS) + throw new CLException(ret, "can not enqueue WriteBuffer: " + writeBuffer); + + return this; + } + + public CLCommandQueue putReadBuffer(CLBuffer readBuffer, boolean blockingRead) { + + int ret = cl.clEnqueueReadBuffer( + ID, readBuffer.ID, blockingRead ? CL.CL_TRUE : CL.CL_FALSE, + 0, readBuffer.buffer.capacity(), readBuffer.buffer, + 0, null, 0, + null, 0 ); + + if(ret != CL.CL_SUCCESS) + throw new CLException(ret, "can not enqueue ReadBuffer: " + readBuffer); + + return this; + } + + public CLCommandQueue putNDRangeKernel(CLKernel kernel, int workDimension, long[] globalWorkOffset, long[] globalWorkSize, long[] localWorkSize) { + + int ret = cl.clEnqueueNDRangeKernel( + ID, kernel.ID, 1, + null, 0, + globalWorkSize, 0, + localWorkSize, 0, + 0, + null, 0, + null, 0 ); + + if(ret != CL.CL_SUCCESS) + throw new CLException(ret, "can not enqueue NDRangeKernel: " + kernel); + + return this; + } + + public void release() { + int ret = cl.clReleaseCommandQueue(ID); + checkForError(ret, "can not release command queue"); + } + + +} diff --git a/src/com/mbien/opencl/CLContext.java b/src/com/mbien/opencl/CLContext.java index 9cb649d7..fcbb8041 100644 --- a/src/com/mbien/opencl/CLContext.java +++ b/src/com/mbien/opencl/CLContext.java @@ -17,7 +17,7 @@ import static com.mbien.opencl.CLException.*; public final class CLContext { final static CL cl; - public final long contextID; + public final long ID; private CLDevice[] devices; @@ -31,7 +31,7 @@ public final class CLContext { } private CLContext(long contextID) { - this.contextID = contextID; + this.ID = contextID; this.programs = new ArrayList<CLProgram>(); this.buffers = new ArrayList<CLBuffer>(); } @@ -67,7 +67,7 @@ public final class CLContext { } public CLProgram createProgram(String src) { - CLProgram program = new CLProgram(this, src, contextID); + CLProgram program = new CLProgram(this, src, ID); programs.add(program); return program; } @@ -89,7 +89,7 @@ public final class CLContext { /** * Releases the context and all resources. */ - public CLContext release() { + public void release() { //release all resources while(!programs.isEmpty()) @@ -98,9 +98,8 @@ public final class CLContext { while(!buffers.isEmpty()) buffers.get(0).release(); - int ret = cl.clReleaseContext(contextID); + int ret = cl.clReleaseContext(ID); checkForError(ret, "error releasing context"); - return this; } /** @@ -167,17 +166,17 @@ public final class CLContext { long[] longBuffer = new long[1]; int ret; - ret = cl.clGetContextInfo(contextID, CL.CL_CONTEXT_DEVICES, 0, null, longBuffer, 0); + ret = cl.clGetContextInfo(ID, 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); + ret = cl.clGetContextInfo(ID, 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 + devices[i] = new CLDevice(this, deviceIDs.getLong()); // TODO doublechek deviceID size on 32 bit systems } @@ -187,7 +186,7 @@ public final class CLContext { CLDevice getCLDevice(long dID) { CLDevice[] deviceArray = getCLDevices(); for (int i = 0; i < deviceArray.length; i++) { - if(dID == deviceArray[i].deviceID) + if(dID == deviceArray[i].ID) return deviceArray[i]; } return null; @@ -227,7 +226,7 @@ public final class CLContext { @Override public String toString() { - return "CLContext [id: " + contextID + return "CLContext [id: " + ID + " #devices: " + getCLDevices().length + "]"; } @@ -241,7 +240,7 @@ public final class CLContext { return false; } final CLContext other = (CLContext) obj; - if (this.contextID != other.contextID) { + if (this.ID != other.ID) { return false; } return true; @@ -250,7 +249,7 @@ public final class CLContext { @Override public int hashCode() { int hash = 7; - hash = 23 * hash + (int) (this.contextID ^ (this.contextID >>> 32)); + hash = 23 * hash + (int) (this.ID ^ (this.ID >>> 32)); return hash; } diff --git a/src/com/mbien/opencl/CLDevice.java b/src/com/mbien/opencl/CLDevice.java index dcc9ee97..c69afef5 100644 --- a/src/com/mbien/opencl/CLDevice.java +++ b/src/com/mbien/opencl/CLDevice.java @@ -61,15 +61,37 @@ public final class CLDevice { } private final CL cl; + private CLContext context; /** * OpenCL device id for this device. */ - public final long deviceID; + public final long ID; CLDevice(CL cl, long id) { this.cl = cl; - this.deviceID = id; + this.ID = id; + } + + CLDevice(CLContext context, long id) { + this.context = context; + this.cl = context.cl; + this.ID = id; + } + + public CLCommandQueue createCommandQueue() { + return createCommandQueue(0); + } + + public CLCommandQueue createCommandQueue(long properties) { + if(context == null) + throw new IllegalStateException("this device is not associated with a context"); + return new CLCommandQueue(context, this, properties); + } + + /*keep this package private for now, may be null*/ + CLContext getContext() { + return context; } /** @@ -160,7 +182,7 @@ public final class CLDevice { ByteBuffer bb = ByteBuffer.allocate(8); bb.order(ByteOrder.nativeOrder()); - int ret = cl.clGetDeviceInfo(deviceID, key, bb.capacity(), bb, null, 0); + int ret = cl.clGetDeviceInfo(ID, key, bb.capacity(), bb, null, 0); checkForError(ret, "can not receive device info"); @@ -172,7 +194,7 @@ public final class CLDevice { long[] longBuffer = new long[1]; ByteBuffer bb = ByteBuffer.allocate(512); - int ret = cl.clGetDeviceInfo(deviceID, key, bb.capacity(), bb, longBuffer, 0); + int ret = cl.clGetDeviceInfo(ID, key, bb.capacity(), bb, longBuffer, 0); checkForError(ret, "can not receive device info string"); @@ -183,7 +205,7 @@ public final class CLDevice { @Override public String toString() { - return "CLDevice [id: " + deviceID + return "CLDevice [id: " + ID + " name: " + getName() + " type: " + getType() + " profile: " + getProfile()+"]"; @@ -198,7 +220,7 @@ public final class CLDevice { return false; } final CLDevice other = (CLDevice) obj; - if (this.deviceID != other.deviceID) { + if (this.ID != other.ID) { return false; } return true; @@ -207,7 +229,7 @@ public final class CLDevice { @Override public int hashCode() { int hash = 3; - hash = 79 * hash + (int) (this.deviceID ^ (this.deviceID >>> 32)); + hash = 79 * hash + (int) (this.ID ^ (this.ID >>> 32)); return hash; } diff --git a/src/com/mbien/opencl/CLKernel.java b/src/com/mbien/opencl/CLKernel.java index be5e03b6..8f470719 100644 --- a/src/com/mbien/opencl/CLKernel.java +++ b/src/com/mbien/opencl/CLKernel.java @@ -11,25 +11,25 @@ import static com.mbien.opencl.CLException.*; */ public class CLKernel { - public final long kernelID; + public final long ID; public final String name; private final CLProgram program; private final CL cl; CLKernel(CLProgram program, long id) { - this.kernelID = id; + this.ID = id; this.program = program; this.cl = program.context.cl; long[] longArray = new long[1]; - int ret = cl.clGetKernelInfo(kernelID, CL.CL_KERNEL_FUNCTION_NAME, 0, null, longArray, 0); + int ret = cl.clGetKernelInfo(ID, CL.CL_KERNEL_FUNCTION_NAME, 0, null, longArray, 0); checkForError(ret, "error while asking for kernel function name"); ByteBuffer bb = ByteBuffer.allocate((int)longArray[0]).order(ByteOrder.nativeOrder()); - ret = cl.clGetKernelInfo(kernelID, CL.CL_KERNEL_FUNCTION_NAME, bb.capacity(), bb, null, 0); + ret = cl.clGetKernelInfo(ID, CL.CL_KERNEL_FUNCTION_NAME, bb.capacity(), bb, null, 0); checkForError(ret, "error while asking for kernel function name"); this.name = new String(bb.array(), 0, (int)longArray[0]).trim(); @@ -37,13 +37,13 @@ public class CLKernel { } public CLKernel setArg(int argumentIndex, int argumentSize, CLBuffer value) { - int ret = cl.clSetKernelArg(kernelID, argumentIndex, argumentSize, wrapLong(value.bufferID)); + int ret = cl.clSetKernelArg(ID, argumentIndex, argumentSize, wrapLong(value.ID)); checkForError(ret, "error on clSetKernelArg"); return this; } public CLKernel setArg(int argumentIndex, int argumentSize, long value) { - int ret = cl.clSetKernelArg(kernelID, argumentIndex, argumentSize, wrapLong(value)); + int ret = cl.clSetKernelArg(ID, argumentIndex, argumentSize, wrapLong(value)); checkForError(ret, "error on clSetKernelArg"); return this; } @@ -52,15 +52,14 @@ public class CLKernel { return (ByteBuffer) BufferFactory.newDirectByteBuffer(8).putLong(value).rewind(); } - public CLKernel release() { - cl.clReleaseKernel(kernelID); + public void release() { + cl.clReleaseKernel(ID); program.kernelReleased(this); - return this; } @Override public String toString() { - return "CLKernel [id: " + kernelID + return "CLKernel [id: " + ID + " name: " + name+"]"; } @@ -73,7 +72,7 @@ public class CLKernel { return false; } final CLKernel other = (CLKernel) obj; - if (this.kernelID != other.kernelID) { + if (this.ID != other.ID) { return false; } if (!this.program.equals(other.program)) { @@ -85,7 +84,7 @@ public class CLKernel { @Override public int hashCode() { int hash = 7; - hash = 43 * hash + (int) (this.kernelID ^ (this.kernelID >>> 32)); + hash = 43 * hash + (int) (this.ID ^ (this.ID >>> 32)); hash = 43 * hash + (this.program != null ? this.program.hashCode() : 0); return hash; } diff --git a/src/com/mbien/opencl/CLPlatform.java b/src/com/mbien/opencl/CLPlatform.java index d9f8dd25..dde9994b 100644 --- a/src/com/mbien/opencl/CLPlatform.java +++ b/src/com/mbien/opencl/CLPlatform.java @@ -11,12 +11,12 @@ public final class CLPlatform { /** * OpenCL platform id for this platform. */ - public final long platformID; + public final long ID; private final CL cl; CLPlatform(CL cl, long id) { - this.platformID = id; + this.ID = id; this.cl = cl; } @@ -28,11 +28,11 @@ public final class CLPlatform { int[] intBuffer = new int[1]; //find all devices - int ret = cl.clGetDeviceIDs(platformID, CL.CL_DEVICE_TYPE_ALL, 0, null, 0, intBuffer, 0); + int ret = cl.clGetDeviceIDs(ID, CL.CL_DEVICE_TYPE_ALL, 0, null, 0, intBuffer, 0); checkForError(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); + ret = cl.clGetDeviceIDs(ID, CL.CL_DEVICE_TYPE_ALL, deviceIDs.length, deviceIDs, 0, null, 0); checkForError(ret, "error while enumerating devices"); CLDevice[] devices = new CLDevice[deviceIDs.length]; @@ -80,7 +80,7 @@ public final class CLPlatform { long[] longBuffer = new long[1]; ByteBuffer bb = ByteBuffer.allocate(512); - int ret = cl.clGetPlatformInfo(platformID, key, bb.capacity(), bb, longBuffer, 0); + int ret = cl.clGetPlatformInfo(ID, key, bb.capacity(), bb, longBuffer, 0); checkForError(ret, "can not receive info string"); return new String(bb.array(), 0, (int)longBuffer[0]); @@ -103,7 +103,7 @@ public final class CLPlatform { return false; } final CLPlatform other = (CLPlatform) obj; - if (this.platformID != other.platformID) { + if (this.ID != other.ID) { return false; } return true; @@ -112,7 +112,7 @@ public final class CLPlatform { @Override public int hashCode() { int hash = 7; - hash = 71 * hash + (int) (this.platformID ^ (this.platformID >>> 32)); + hash = 71 * hash + (int) (this.ID ^ (this.ID >>> 32)); return hash; } diff --git a/src/com/mbien/opencl/CLProgram.java b/src/com/mbien/opencl/CLProgram.java index 73fe8cac..0404e070 100644 --- a/src/com/mbien/opencl/CLProgram.java +++ b/src/com/mbien/opencl/CLProgram.java @@ -5,7 +5,6 @@ import java.nio.ByteOrder; import java.util.Collections; import java.util.HashMap; import java.util.Map; -import java.util.Set; import static com.mbien.opencl.CLException.*; /** @@ -15,7 +14,7 @@ import static com.mbien.opencl.CLException.*; public class CLProgram { public final CLContext context; - public final long programID; + public final long ID; private final CL cl; @@ -64,13 +63,13 @@ public class CLProgram { int[] intArray = new int[1]; // Create the program - programID = cl.clCreateProgramWithSource(contextID, 1, new String[] {src}, new long[]{src.length()}, 0, intArray, 0); + ID = cl.clCreateProgramWithSource(contextID, 1, new String[] {src}, new long[]{src.length()}, 0, intArray, 0); checkForError(intArray[0], "can not create program with source"); } /** - * Builds this program for all devices accosiated with the context and implementation specific build options. + * Builds this program for all devices associated with the context and implementation specific build options. * @return this */ public CLProgram build() { @@ -89,12 +88,12 @@ public class CLProgram { if(devices != null) { deviceIDs = new long[devices.length]; for (int i = 0; i < deviceIDs.length; i++) { - deviceIDs[i] = devices[i].deviceID; + deviceIDs[i] = devices[i].ID; } } // Build the program - int ret = cl.clBuildProgram(programID, deviceIDs, options, null, null); + int ret = cl.clBuildProgram(ID, deviceIDs, options, null, null); checkForError(ret, "error building program"); return this; @@ -108,11 +107,11 @@ public class CLProgram { if(kernels.isEmpty()) { int[] intArray = new int[1]; - int ret = cl.clCreateKernelsInProgram(programID, 0, null, 0, intArray, 0); + int ret = cl.clCreateKernelsInProgram(ID, 0, null, 0, intArray, 0); checkForError(ret, "can not create kernels for program"); long[] kernelIDs = new long[intArray[0]]; - ret = cl.clCreateKernelsInProgram(programID, kernelIDs.length, kernelIDs, 0, null, 0); + ret = cl.clCreateKernelsInProgram(ID, kernelIDs.length, kernelIDs, 0, null, 0); checkForError(ret, "can not create kernels for program"); for (int i = 0; i < intArray[0]; i++) { @@ -130,9 +129,8 @@ public class CLProgram { /** * Releases this program. - * @return this */ - public CLProgram release() { + public void release() { if(!kernels.isEmpty()) { String[] names = kernels.keySet().toArray(new String[kernels.size()]); @@ -141,11 +139,10 @@ public class CLProgram { } } - int ret = cl.clReleaseProgram(programID); + int ret = cl.clReleaseProgram(ID); checkForError(ret, "can not release program"); context.programReleased(this); - - return this; + } /** @@ -154,11 +151,11 @@ public class CLProgram { public CLDevice[] getCLDevices() { long[] longArray = new long[1]; - int ret = cl.clGetProgramInfo(programID, CL.CL_PROGRAM_DEVICES, 0, null, longArray, 0); + int ret = cl.clGetProgramInfo(ID, CL.CL_PROGRAM_DEVICES, 0, null, longArray, 0); checkForError(ret, "on clGetProgramInfo"); ByteBuffer bb = ByteBuffer.allocate((int) longArray[0]).order(ByteOrder.nativeOrder()); - ret = cl.clGetProgramInfo(programID, CL.CL_PROGRAM_DEVICES, bb.capacity(), bb, null, 0); + ret = cl.clGetProgramInfo(ID, CL.CL_PROGRAM_DEVICES, bb.capacity(), bb, null, 0); checkForError(ret, "on clGetProgramInfo"); int count = bb.capacity() / 8; // TODO sizeof cl_device @@ -172,11 +169,11 @@ public class CLProgram { } public String getBuildLog(CLDevice device) { - return getBuildInfoString(device.deviceID, CL.CL_PROGRAM_BUILD_LOG); + return getBuildInfoString(device.ID, CL.CL_PROGRAM_BUILD_LOG); } public Status getBuildStatus(CLDevice device) { - int clStatus = getBuildInfoInt(device.deviceID, CL.CL_PROGRAM_BUILD_STATUS); + int clStatus = getBuildInfoInt(device.ID, CL.CL_PROGRAM_BUILD_STATUS); return Status.valueOf(clStatus); } @@ -193,12 +190,12 @@ public class CLProgram { long[] longArray = new long[1]; - int ret = cl.clGetProgramBuildInfo(programID, device, flag, 0, null, longArray, 0); + int ret = cl.clGetProgramBuildInfo(ID, device, flag, 0, null, longArray, 0); checkForError(ret, "on clGetProgramBuildInfo"); ByteBuffer bb = ByteBuffer.allocate((int)longArray[0]).order(ByteOrder.nativeOrder()); - ret = cl.clGetProgramBuildInfo(programID, device, flag, bb.capacity(), bb, null, 0); + ret = cl.clGetProgramBuildInfo(ID, device, flag, bb.capacity(), bb, null, 0); checkForError(ret, "on clGetProgramBuildInfo"); return new String(bb.array(), 0, (int)longArray[0]); @@ -208,12 +205,12 @@ public class CLProgram { long[] longArray = new long[1]; - int ret = cl.clGetProgramInfo(programID, flag, 0, null, longArray, 0); + int ret = cl.clGetProgramInfo(ID, flag, 0, null, longArray, 0); checkForError(ret, "on clGetProgramInfo"); ByteBuffer bb = ByteBuffer.allocate((int)longArray[0]).order(ByteOrder.nativeOrder()); - ret = cl.clGetProgramInfo(programID, flag, bb.capacity(), bb, null, 0); + ret = cl.clGetProgramInfo(ID, flag, bb.capacity(), bb, null, 0); checkForError(ret, "on clGetProgramInfo"); return new String(bb.array(), 0, (int)longArray[0]); @@ -233,7 +230,7 @@ public class CLProgram { ByteBuffer bb = ByteBuffer.allocate(4).order(ByteOrder.nativeOrder()); - int ret = cl.clGetProgramBuildInfo(programID, device, flag, bb.capacity(), bb, null, 0); + int ret = cl.clGetProgramBuildInfo(ID, device, flag, bb.capacity(), bb, null, 0); checkForError(ret, "error on clGetProgramBuildInfo"); return bb.getInt(); @@ -248,7 +245,7 @@ public class CLProgram { return false; } final CLProgram other = (CLProgram) obj; - if (this.programID != other.programID) { + if (this.ID != other.ID) { return false; } if (!this.context.equals(other.context)) { @@ -261,7 +258,7 @@ public class CLProgram { public int hashCode() { int hash = 7; hash = 37 * hash + (this.context != null ? this.context.hashCode() : 0); - hash = 37 * hash + (int) (this.programID ^ (this.programID >>> 32)); + hash = 37 * hash + (int) (this.ID ^ (this.ID >>> 32)); return hash; } |