summaryrefslogtreecommitdiffstats
path: root/src/com/mbien/opencl/CLKernel.java
diff options
context:
space:
mode:
authorMichael Bien <[email protected]>2009-10-19 19:26:15 +0200
committerMichael Bien <[email protected]>2009-10-19 19:26:15 +0200
commitcb7fa23952a10795215eda50848530828b92895e (patch)
treee9e34a7cf55505fe3a381e474ddeb9d224caf545 /src/com/mbien/opencl/CLKernel.java
parent9abfd00399e4ca5c351df9cdf25cd85c960b3d44 (diff)
initial import of CLBuffer and CLKernel.
added hashCode(), equals() and toString() methods. updated JUnit test to test new classes.
Diffstat (limited to 'src/com/mbien/opencl/CLKernel.java')
-rw-r--r--src/com/mbien/opencl/CLKernel.java93
1 files changed, 93 insertions, 0 deletions
diff --git a/src/com/mbien/opencl/CLKernel.java b/src/com/mbien/opencl/CLKernel.java
new file mode 100644
index 00000000..be5e03b6
--- /dev/null
+++ b/src/com/mbien/opencl/CLKernel.java
@@ -0,0 +1,93 @@
+package com.mbien.opencl;
+
+import com.sun.gluegen.runtime.BufferFactory;
+import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
+import static com.mbien.opencl.CLException.*;
+
+/**
+ *
+ * @author Michael Bien
+ */
+public class CLKernel {
+
+ public final long kernelID;
+ public final String name;
+
+ private final CLProgram program;
+ private final CL cl;
+
+ CLKernel(CLProgram program, long id) {
+ this.kernelID = 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);
+ 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);
+ checkForError(ret, "error while asking for kernel function name");
+
+ this.name = new String(bb.array(), 0, (int)longArray[0]).trim();
+
+ }
+
+ public CLKernel setArg(int argumentIndex, int argumentSize, CLBuffer value) {
+ int ret = cl.clSetKernelArg(kernelID, argumentIndex, argumentSize, wrapLong(value.bufferID));
+ 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));
+ checkForError(ret, "error on clSetKernelArg");
+ return this;
+ }
+
+ private final ByteBuffer wrapLong(long value) {
+ return (ByteBuffer) BufferFactory.newDirectByteBuffer(8).putLong(value).rewind();
+ }
+
+ public CLKernel release() {
+ cl.clReleaseKernel(kernelID);
+ program.kernelReleased(this);
+ return this;
+ }
+
+ @Override
+ public String toString() {
+ return "CLKernel [id: " + kernelID
+ + " name: " + name+"]";
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if (obj == null) {
+ return false;
+ }
+ if (getClass() != obj.getClass()) {
+ return false;
+ }
+ final CLKernel other = (CLKernel) obj;
+ if (this.kernelID != other.kernelID) {
+ return false;
+ }
+ if (!this.program.equals(other.program)) {
+ return false;
+ }
+ return true;
+ }
+
+ @Override
+ public int hashCode() {
+ int hash = 7;
+ hash = 43 * hash + (int) (this.kernelID ^ (this.kernelID >>> 32));
+ hash = 43 * hash + (this.program != null ? this.program.hashCode() : 0);
+ return hash;
+ }
+
+}