diff options
Diffstat (limited to 'src/com/mbien/opencl')
-rw-r--r-- | src/com/mbien/opencl/CLBuffer.java | 31 | ||||
-rw-r--r-- | src/com/mbien/opencl/CLCommandQueue.java | 45 | ||||
-rw-r--r-- | src/com/mbien/opencl/CLKernel.java | 8 | ||||
-rw-r--r-- | src/com/mbien/opencl/CLProgram.java | 10 |
4 files changed, 86 insertions, 8 deletions
diff --git a/src/com/mbien/opencl/CLBuffer.java b/src/com/mbien/opencl/CLBuffer.java index 223b074b..42b9586d 100644 --- a/src/com/mbien/opencl/CLBuffer.java +++ b/src/com/mbien/opencl/CLBuffer.java @@ -1,6 +1,12 @@ package com.mbien.opencl; +import com.sun.gluegen.runtime.BufferFactory; import java.nio.Buffer; +import java.nio.ByteBuffer; +import java.nio.DoubleBuffer; +import java.nio.FloatBuffer; +import java.nio.IntBuffer; +import java.nio.ShortBuffer; import static com.mbien.opencl.CLException.*; /** @@ -31,7 +37,8 @@ public class CLBuffer<B extends Buffer> { int[] intArray = new int[1]; if(glBuffer == 0) { - this.ID = cl.clCreateBuffer(context.ID, flags, directBuffer.capacity(), null, intArray, 0); + this.ID = cl.clCreateBuffer(context.ID, flags, + sizeOfBufferElem(directBuffer)*directBuffer.capacity(), null, intArray, 0); }else{ CLGLI clgli = (CLGLI)cl; this.ID = clgli.clCreateFromGLBuffer(context.ID, flags, glBuffer, intArray, 0); @@ -46,6 +53,28 @@ public class CLBuffer<B extends Buffer> { checkForError(ret, "can not release mem object"); } + //stolen from JOGL project... think about merging + private final int sizeOfBufferElem(Buffer buffer) { + + if (buffer instanceof ByteBuffer) { + return BufferFactory.SIZEOF_BYTE; + } else if (buffer instanceof IntBuffer) { + return BufferFactory.SIZEOF_INT; + } else if (buffer instanceof ShortBuffer) { + return BufferFactory.SIZEOF_SHORT; + } else if (buffer instanceof FloatBuffer) { + return BufferFactory.SIZEOF_FLOAT; + } else if (buffer instanceof DoubleBuffer) { + return BufferFactory.SIZEOF_DOUBLE; + } + throw new RuntimeException("Unexpected buffer type " + buffer.getClass().getName()); + } + + int getSizeInBytes() { + return sizeOfBufferElem(buffer)*buffer.capacity(); + } + + @Override public boolean equals(Object obj) { if (obj == null) { diff --git a/src/com/mbien/opencl/CLCommandQueue.java b/src/com/mbien/opencl/CLCommandQueue.java index d58cc162..991b0088 100644 --- a/src/com/mbien/opencl/CLCommandQueue.java +++ b/src/com/mbien/opencl/CLCommandQueue.java @@ -1,5 +1,6 @@ package com.mbien.opencl; +import java.nio.Buffer; import java.util.ArrayList; import java.util.EnumSet; import java.util.List; @@ -37,7 +38,7 @@ public class CLCommandQueue { int ret = cl.clEnqueueWriteBuffer( ID, writeBuffer.ID, blockingWrite ? CL.CL_TRUE : CL.CL_FALSE, - 0, writeBuffer.buffer.capacity(), writeBuffer.buffer, + 0, writeBuffer.getSizeInBytes(), writeBuffer.buffer, // 0, null, null); //TODO solve NPE in gluegen when PointerBuffer == null (fast dircet memory path) 0, null, 0, null, 0); //TODO events @@ -51,7 +52,7 @@ public class CLCommandQueue { int ret = cl.clEnqueueReadBuffer( ID, readBuffer.ID, blockingRead ? CL.CL_TRUE : CL.CL_FALSE, - 0, readBuffer.buffer.capacity(), readBuffer.buffer, + 0, readBuffer.getSizeInBytes(), readBuffer.buffer, // 0, null, null); //TODO solve NPE in gluegen when PointerBuffer == null (fast dircet memory path) 0, null, 0, null, 0); //TODO events @@ -61,6 +62,20 @@ public class CLCommandQueue { return this; } + public CLCommandQueue putReadBuffer(CLBuffer<?> readBuffer, Buffer buffer, boolean blockingRead) { + + int ret = cl.clEnqueueReadBuffer( + ID, readBuffer.ID, blockingRead ? CL.CL_TRUE : CL.CL_FALSE, + 0, readBuffer.getSizeInBytes(), buffer, +// 0, null, null); //TODO solve NPE in gluegen when PointerBuffer == null (fast dircet memory path) + 0, null, 0, null, 0); //TODO events + + if(ret != CL.CL_SUCCESS) + throw new CLException(ret, "can not enqueue ReadBuffer: " + readBuffer); + + return this; + } + public CLCommandQueue putBarrier() { int ret = cl.clEnqueueBarrier(ID); checkForError(ret, "can not enqueue Barrier"); @@ -130,7 +145,7 @@ public class CLCommandQueue { return this; } */ - + public CLCommandQueue putNDRangeKernel(CLKernel kernel, int workDimension, long globalWorkOffset, long globalWorkSize, long localWorkSize) { return this.putNDRangeKernel( kernel, workDimension, @@ -142,7 +157,7 @@ public class CLCommandQueue { public CLCommandQueue putNDRangeKernel(CLKernel kernel, int workDimension, long[] globalWorkOffset, long[] globalWorkSize, long[] localWorkSize) { int ret = cl.clEnqueueNDRangeKernel( - ID, kernel.ID, 1, + ID, kernel.ID, workDimension, globalWorkOffset, 0, globalWorkSize, 0, localWorkSize, 0, @@ -156,6 +171,28 @@ public class CLCommandQueue { return this; } + + public CLCommandQueue putAcquireGLObject(long glObject) { + CLGLI xl = (CLGLI) cl; + int ret = xl.clEnqueueAcquireGLObjects(ID, 1, new long[] {glObject}, 0, 0, null, 0, null, 0); + + if(ret != CL.CL_SUCCESS) + throw new CLException(ret, "can not aquire GLObject: " + glObject); + + return this; + } + + public CLCommandQueue putReleaseGLObject(long glObject) { + CLGLI xl = (CLGLI) cl; + int ret = xl.clEnqueueReleaseGLObjects(ID, 1, new long[] {glObject}, 0, 0, null, 0, null, 0); + + if(ret != CL.CL_SUCCESS) + throw new CLException(ret, "can not release GLObject: " + glObject); + + return this; + } + + public CLCommandQueue finish() { int ret = cl.clFinish(ID); checkForError(ret, "can not finish command queue"); diff --git a/src/com/mbien/opencl/CLKernel.java b/src/com/mbien/opencl/CLKernel.java index 21b176c7..a4ee9f89 100644 --- a/src/com/mbien/opencl/CLKernel.java +++ b/src/com/mbien/opencl/CLKernel.java @@ -68,10 +68,18 @@ public class CLKernel { return this; } + private final Buffer wrap(float value) { + return BufferFactory.newDirectByteBuffer(4).putFloat(value).rewind(); + } + private final Buffer wrap(double value) { return BufferFactory.newDirectByteBuffer(8).putDouble(value).rewind(); } + private final Buffer wrap(int value) { + return BufferFactory.newDirectByteBuffer(4).putInt(value).rewind(); + } + private final Buffer wrap(long value) { return BufferFactory.newDirectByteBuffer(8).putLong(value).rewind(); } diff --git a/src/com/mbien/opencl/CLProgram.java b/src/com/mbien/opencl/CLProgram.java index da0a0447..5b00898f 100644 --- a/src/com/mbien/opencl/CLProgram.java +++ b/src/com/mbien/opencl/CLProgram.java @@ -141,7 +141,10 @@ public class CLProgram { // Build the program int ret = cl.clBuildProgram(ID, deviceIDs, options, null, null); - checkForError(ret, "error building program"); + + if(ret != CL.CL_SUCCESS) { + checkForError(ret, "\n"+getBuildLog()); + } return this; } @@ -218,8 +221,9 @@ public class CLProgram { CLDevice[] devices = getCLDevices(); for (int i = 0; i < devices.length; i++) { CLDevice device = devices[i]; - sb.append(device).append(" build log:"); - sb.append(getBuildLog(device)); + sb.append(device).append(" build log:\n"); + String log = getBuildLog(device).trim(); + sb.append(log.isEmpty()?" <empty>":log); if(i != devices.length-1) sb.append("\n"); } |