From 557a0a94f42cb2d3d1fa77933af19a422d2f681c Mon Sep 17 00:00:00 2001 From: Michael Bien Date: Sat, 23 Apr 2011 16:10:47 +0200 Subject: switched from PointerBuffer to NativeSizeBuffer. --- src/com/jogamp/opencl/CLProgram.java | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) (limited to 'src/com/jogamp/opencl/CLProgram.java') diff --git a/src/com/jogamp/opencl/CLProgram.java b/src/com/jogamp/opencl/CLProgram.java index bc3727f8..3293fe57 100644 --- a/src/com/jogamp/opencl/CLProgram.java +++ b/src/com/jogamp/opencl/CLProgram.java @@ -32,6 +32,7 @@ import com.jogamp.common.nio.CachedBufferFactory; import com.jogamp.opencl.util.CLProgramConfiguration; import com.jogamp.opencl.util.CLUtil; import com.jogamp.common.os.Platform; +import com.jogamp.common.nio.NativeSizeBuffer; import com.jogamp.common.nio.PointerBuffer; import com.jogamp.opencl.impl.BuildProgramCallback; import com.jogamp.opencl.util.CLBuildListener; @@ -77,7 +78,7 @@ public class CLProgram extends CLObject implements CLResource { IntBuffer status = newDirectIntBuffer(1); - PointerBuffer length = PointerBuffer.allocateDirect(1).put(0, src.length()); + NativeSizeBuffer length = NativeSizeBuffer.allocateDirect(1).put(0, src.length()); String[] srcArray = new String[] {src}; // Create the program @@ -101,13 +102,13 @@ public class CLProgram extends CLObject implements CLResource { binarySize += entry.getValue().length; } - int pbSize = PointerBuffer.elementSize(); + int pbSize = NativeSizeBuffer.elementSize(); int deviceCount = binaries.size(); CachedBufferFactory bf = CachedBufferFactory.create(binarySize + pbSize*deviceCount*3 + 4, true); - PointerBuffer devices = PointerBuffer.wrap(bf.newDirectByteBuffer(deviceCount*pbSize)); + NativeSizeBuffer devices = NativeSizeBuffer.wrap(bf.newDirectByteBuffer(deviceCount*pbSize)); PointerBuffer codeBuffers = PointerBuffer.wrap(bf.newDirectByteBuffer(deviceCount*pbSize)); - PointerBuffer lengths = PointerBuffer.wrap(bf.newDirectByteBuffer(deviceCount*pbSize)); + NativeSizeBuffer lengths = NativeSizeBuffer.wrap(bf.newDirectByteBuffer(deviceCount*pbSize)); int i = 0; for (Map.Entry entry : entries) { @@ -164,7 +165,7 @@ public class CLProgram extends CLObject implements CLResource { return ""; } - PointerBuffer size = PointerBuffer.allocateDirect(1); + NativeSizeBuffer size = NativeSizeBuffer.allocateDirect(1); int ret = cl.clGetProgramBuildInfo(ID, device.ID, flag, 0, null, size); if(ret != CL_SUCCESS) { @@ -187,7 +188,7 @@ public class CLProgram extends CLObject implements CLResource { return ""; } - PointerBuffer size = PointerBuffer.allocateDirect(1); + NativeSizeBuffer size = NativeSizeBuffer.allocateDirect(1); int ret = cl.clGetProgramInfo(ID, flag, 0, null, size); checkForError(ret, "on clGetProgramInfo"); @@ -328,10 +329,10 @@ public class CLProgram extends CLObject implements CLResource { releaseKernels(); } - PointerBuffer deviceIDs = null; + NativeSizeBuffer deviceIDs = null; int count = 0; if(devices != null && devices.length != 0) { - deviceIDs = PointerBuffer.allocateDirect(devices.length); + deviceIDs = NativeSizeBuffer.allocateDirect(devices.length); for (int i = 0; i < devices.length; i++) { deviceIDs.put(i, devices[i].ID); } @@ -430,7 +431,7 @@ public class CLProgram extends CLObject implements CLResource { if(numKernels.get(0) > 0) { - PointerBuffer kernelIDs = PointerBuffer.allocateDirect(numKernels.get(0)); + NativeSizeBuffer kernelIDs = NativeSizeBuffer.allocateDirect(numKernels.get(0)); ret = cl.clCreateKernelsInProgram(ID, kernelIDs.capacity(), kernelIDs, null); if(ret != CL_SUCCESS) { throw newException(ret, "can not create "+kernelIDs.capacity()+" kernels for "+this); @@ -494,7 +495,7 @@ public class CLProgram extends CLObject implements CLResource { if(released) { return new CLDevice[0]; } - PointerBuffer size = PointerBuffer.allocateDirect(1); + NativeSizeBuffer size = NativeSizeBuffer.allocateDirect(1); int ret = cl.clGetProgramInfo(ID, CL_PROGRAM_DEVICES, 0, null, size); if(ret != CL_SUCCESS) { throw newException(ret, "on clGetProgramInfo of "+this); @@ -604,8 +605,8 @@ public class CLProgram extends CLObject implements CLResource { CLDevice[] devices = getCLDevices(); - PointerBuffer sizes = PointerBuffer.allocateDirect(devices.length); - int ret = cl.clGetProgramInfo(ID, CL_PROGRAM_BINARY_SIZES, sizes.capacity()*PointerBuffer.elementSize(), sizes.getBuffer(), null); + NativeSizeBuffer sizes = NativeSizeBuffer.allocateDirect(devices.length); + int ret = cl.clGetProgramInfo(ID, CL_PROGRAM_BINARY_SIZES, sizes.capacity()*NativeSizeBuffer.elementSize(), sizes.getBuffer(), null); if(ret != CL_SUCCESS) { throw newException(ret, "on clGetProgramInfo(CL_PROGRAM_BINARY_SIZES) of "+this); } @@ -619,14 +620,14 @@ public class CLProgram extends CLObject implements CLResource { long address = InternalBufferUtil.getDirectBufferAddress(binaries); - PointerBuffer addresses = PointerBuffer.allocateDirect(sizes.capacity()); + NativeSizeBuffer addresses = NativeSizeBuffer.allocateDirect(sizes.capacity()); sizes.rewind(); while(sizes.remaining() != 0) { addresses.put(address); address += sizes.get(); } - ret = cl.clGetProgramInfo(ID, CL_PROGRAM_BINARIES, addresses.capacity()*PointerBuffer.elementSize(), addresses.getBuffer(), null); + ret = cl.clGetProgramInfo(ID, CL_PROGRAM_BINARIES, addresses.capacity()*NativeSizeBuffer.elementSize(), addresses.getBuffer(), null); if(ret != CL_SUCCESS) { throw newException(ret, "on clGetProgramInfo(CL_PROGRAM_BINARIES) of "+this); } -- cgit v1.2.3 From b1a2492a2d816e7d8518a460798d1faac35a8b9c Mon Sep 17 00:00:00 2001 From: Michael Bien Date: Wed, 27 Apr 2011 16:51:49 +0200 Subject: missing rewind() --- src/com/jogamp/opencl/CLProgram.java | 1 + 1 file changed, 1 insertion(+) (limited to 'src/com/jogamp/opencl/CLProgram.java') diff --git a/src/com/jogamp/opencl/CLProgram.java b/src/com/jogamp/opencl/CLProgram.java index 3293fe57..1b1dd1d5 100644 --- a/src/com/jogamp/opencl/CLProgram.java +++ b/src/com/jogamp/opencl/CLProgram.java @@ -626,6 +626,7 @@ public class CLProgram extends CLObject implements CLResource { addresses.put(address); address += sizes.get(); } + addresses.rewind(); ret = cl.clGetProgramInfo(ID, CL_PROGRAM_BINARIES, addresses.capacity()*NativeSizeBuffer.elementSize(), addresses.getBuffer(), null); if(ret != CL_SUCCESS) { -- cgit v1.2.3 From f12e3a9d7ac644abc98a51dc51786cf7c5b67851 Mon Sep 17 00:00:00 2001 From: Michael Bien Date: Sun, 15 May 2011 22:42:47 +0200 Subject: CLKernel code review. - optimized create from name path - putArg should not increment the index if setting the argument fails - added putArg() test --- src/com/jogamp/opencl/CLKernel.java | 68 +++++++++++++++++++++---------- src/com/jogamp/opencl/CLProgram.java | 2 +- test/com/jogamp/opencl/CLProgramTest.java | 19 ++++++++- 3 files changed, 65 insertions(+), 24 deletions(-) (limited to 'src/com/jogamp/opencl/CLProgram.java') diff --git a/src/com/jogamp/opencl/CLKernel.java b/src/com/jogamp/opencl/CLKernel.java index 78c5fc93..4c898b4b 100644 --- a/src/com/jogamp/opencl/CLKernel.java +++ b/src/com/jogamp/opencl/CLKernel.java @@ -62,28 +62,36 @@ public class CLKernel extends CLObject implements CLResource, Cloneable { private boolean force32BitArgs; CLKernel(CLProgram program, long id) { + this(program, null, id); + } + + CLKernel(CLProgram program, String name, long id) { super(program.getContext(), id); + this.program = program; this.buffer = Buffers.newDirectByteBuffer((is32Bit()?4:8)*3); - NativeSizeBuffer size = NativeSizeBuffer.allocateDirect(1); - - // get function name - int ret = cl.clGetKernelInfo(ID, CL_KERNEL_FUNCTION_NAME, 0, null, size); - checkForError(ret, "error while asking for kernel function name"); + if(name == null) { + // get function name + NativeSizeBuffer size = NativeSizeBuffer.wrap(buffer); + int ret = cl.clGetKernelInfo(ID, CL_KERNEL_FUNCTION_NAME, 0, null, size); + checkForError(ret, "error while asking for kernel function name"); - ByteBuffer bb = Buffers.newDirectByteBuffer((int)size.get(0)); + ByteBuffer bb = Buffers.newDirectByteBuffer((int)size.get(0)); - ret = cl.clGetKernelInfo(ID, CL_KERNEL_FUNCTION_NAME, bb.capacity(), bb, null); - checkForError(ret, "error while asking for kernel function name"); - - this.name = CLUtil.clString2JavaString(bb, bb.capacity()); + ret = cl.clGetKernelInfo(ID, CL_KERNEL_FUNCTION_NAME, bb.capacity(), bb, null); + checkForError(ret, "error while asking for kernel function name"); + + this.name = CLUtil.clString2JavaString(bb, bb.capacity()); + }else{ + this.name = name; + } // get number of arguments - ret = cl.clGetKernelInfo(ID, CL_KERNEL_NUM_ARGS, bb.capacity(), bb, null); + int ret = cl.clGetKernelInfo(ID, CL_KERNEL_NUM_ARGS, buffer.capacity(), buffer, null); checkForError(ret, "error while asking for number of function arguments."); - numArgs = bb.getInt(0); + numArgs = buffer.getInt(0); } @@ -93,32 +101,38 @@ public class CLKernel extends CLObject implements CLResource, Cloneable { // } public CLKernel putArg(CLMemory value) { - setArg(argIndex++, value); + setArg(argIndex, value); + argIndex++; return this; } public CLKernel putArg(int value) { - setArg(argIndex++, value); + setArg(argIndex, value); + argIndex++; return this; } public CLKernel putArg(long value) { - setArg(argIndex++, value); + setArg(argIndex, value); + argIndex++; return this; } public CLKernel putArg(float value) { - setArg(argIndex++, value); + setArg(argIndex, value); + argIndex++; return this; } public CLKernel putArg(double value) { - setArg(argIndex++, value); + setArg(argIndex, value); + argIndex++; return this; } public CLKernel putNullArg(int size) { - setNullArg(argIndex++, size); + setNullArg(argIndex, size); + argIndex++; return this; } @@ -128,11 +142,21 @@ public class CLKernel extends CLObject implements CLResource, Cloneable { return this; } + /** + * Resets the argument index to 0. + */ public CLKernel rewind() { argIndex = 0; return this; } + /** + * Returns the argument index used in the relative putArt(...) methods. + */ + public int position() { + return argIndex; + } + // public CLKernel setArg(int argumentIndex, Buffer value) { // setArgument(argumentIndex, CLMemory.sizeOfBufferElem(value)*value.capacity(), value); // return this; @@ -224,19 +248,19 @@ public class CLKernel extends CLObject implements CLResource, Cloneable { } private Buffer wrap(float value) { - return buffer.putFloat(value).rewind(); + return buffer.putFloat(0, value); } private Buffer wrap(double value) { - return buffer.putDouble(value).rewind(); + return buffer.putDouble(0, value); } private Buffer wrap(int value) { - return buffer.putInt(value).rewind(); + return buffer.putInt(0, value); } private Buffer wrap(long value) { - return buffer.putLong(value).rewind(); + return buffer.putLong(0, value); } /** diff --git a/src/com/jogamp/opencl/CLProgram.java b/src/com/jogamp/opencl/CLProgram.java index 1b1dd1d5..e8706a73 100644 --- a/src/com/jogamp/opencl/CLProgram.java +++ b/src/com/jogamp/opencl/CLProgram.java @@ -407,7 +407,7 @@ public class CLProgram extends CLObject implements CLResource { throw newException(err[0], "unable to create Kernel with name: "+kernelName); } - CLKernel kernel = new CLKernel(this, id); + CLKernel kernel = new CLKernel(this, kernelName, id); kernels.add(kernel); return kernel; } diff --git a/test/com/jogamp/opencl/CLProgramTest.java b/test/com/jogamp/opencl/CLProgramTest.java index 49c25180..a3ee26f3 100644 --- a/test/com/jogamp/opencl/CLProgramTest.java +++ b/test/com/jogamp/opencl/CLProgramTest.java @@ -268,7 +268,7 @@ public class CLProgramTest { @Test public void kernelTest() { - String source = "__attribute__((reqd_work_group_size(1, 1, 1))) kernel void foo(void) { }\n"; + String source = "__attribute__((reqd_work_group_size(1, 1, 1))) kernel void foo(float a, int b) { }\n"; CLContext context = CLContext.create(); @@ -287,6 +287,23 @@ public class CLProgramTest { assertEquals(1, wgs[1]); assertEquals(1, wgs[2]); + // put args test + assertEquals(0, kernel.position()); + + kernel.putArg(1.0f); + assertEquals(1, kernel.position()); + + kernel.putArg(2); + assertEquals(2, kernel.position()); + + try{ + kernel.putArg(3); + fail("exception not thrown"); + }catch (IndexOutOfBoundsException expected){ } + + assertEquals(2, kernel.position()); + assertEquals(0, kernel.rewind().position()); + }finally{ context.release(); } -- cgit v1.2.3