29package com.jogamp.opencl;
31import com.jogamp.opencl.util.CLUtil;
32import com.jogamp.common.nio.Buffers;
33import com.jogamp.common.nio.PointerBuffer;
34import com.jogamp.opencl.llb.CL;
35import java.nio.Buffer;
36import java.nio.ByteBuffer;
38import static com.jogamp.opencl.CLException.*;
39import static com.jogamp.opencl.llb.CL.*;
40import static com.jogamp.common.os.Platform.*;
53public class CLKernel extends CLObjectResource implements Cloneable {
59 private final CL binding;
61 private final ByteBuffer buffer;
64 private boolean force32BitArgs;
67 this(program,
null, id);
73 this.program = program;
74 this.buffer = Buffers.newDirectByteBuffer((is32Bit()?4:8)*3);
80 final PointerBuffer size = PointerBuffer.wrap(buffer);
82 checkForError(ret,
"error while asking for kernel function name");
84 final ByteBuffer bb = Buffers.newDirectByteBuffer((
int)size.get(0));
86 ret = binding.
clGetKernelInfo(
ID, CL_KERNEL_FUNCTION_NAME, bb.capacity(), bb,
null);
87 checkForError(ret,
"error while asking for kernel function name");
95 final int ret = binding.
clGetKernelInfo(
ID, CL_KERNEL_NUM_ARGS, buffer.capacity(), buffer,
null);
96 checkForError(ret,
"error while asking for number of function arguments.");
151 argIndex += values.length;
176 setArgument(argumentIndex, is32Bit()?4:8, wrap(value.
ID));
181 setArgument(argumentIndex, 2, wrap(value));
186 setArgument(argumentIndex, 4, wrap(value));
192 setArgument(argumentIndex, 4, wrap((
int)value));
194 setArgument(argumentIndex, 8, wrap(value));
200 setArgument(argumentIndex, 4, wrap(value));
206 setArgument(argumentIndex, 4, wrap((
float)value));
208 setArgument(argumentIndex, 8, wrap(value));
214 setArgument(argumentIndex, size,
null);
224 if(values ==
null || values.length == 0) {
225 throw new IllegalArgumentException(
"values array was empty or null.");
227 for (
int i = 0; i < values.length; i++) {
228 final Object value = values[i];
231 }
else if(value instanceof Short) {
233 }
else if(value instanceof Integer) {
234 setArg(i, (Integer)value);
235 }
else if(value instanceof Long) {
237 }
else if(value instanceof Float) {
239 }
else if(value instanceof Double) {
242 throw new IllegalArgumentException(value +
" is not a valid argument.");
249 for (
int i = 0; i < values.length; i++) {
250 setArg(i+startIndex, values[i]);
254 private void setArgument(
final int argumentIndex,
final int size,
final Buffer value) {
255 if(argumentIndex >=
numArgs || argumentIndex < 0) {
256 throw new IndexOutOfBoundsException(
"kernel "+
this +
" has "+
numArgs+
257 " arguments, can not set argument with index "+argumentIndex);
260 throw new IllegalStateException(
"can not set program" +
261 " arguments for a not executable program. "+program);
265 if(ret != CL_SUCCESS) {
266 throw newException(ret,
"error setting arg "+argumentIndex+
" to value "+value+
" of size "+size+
" of "+
this);
275 this.force32BitArgs = force;
287 return force32BitArgs;
290 private Buffer wrap(
final float value) {
291 return buffer.putFloat(0, value);
294 private Buffer wrap(
final double value) {
295 return buffer.putDouble(0, value);
298 private Buffer wrap(
final short value) {
299 return buffer.putShort(0, value);
302 private Buffer wrap(
final int value) {
303 return buffer.putInt(0, value);
306 private Buffer wrap(
final long value) {
307 return buffer.putLong(0, value);
321 return getWorkGroupInfo(device, CL_KERNEL_LOCAL_MEM_SIZE);
333 return getWorkGroupInfo(device, CL_KERNEL_WORK_GROUP_SIZE);
344 if(ret != CL_SUCCESS) {
345 throw newException(ret,
"error while asking for CL_KERNEL_COMPILE_WORK_GROUP_SIZE of "+
this+
" on "+device);
349 return new long[] { buffer.getInt(0), buffer.getInt(4), buffer.getInt(8) };
351 return new long[] { buffer.getLong(0), buffer.getLong(8), buffer.getLong(16) };
361 return getWorkGroupInfo(device, CL_KERNEL_PREFERRED_WORK_GROUP_SIZE_MULTIPLE);
371 return getWorkGroupInfo(device, CL_KERNEL_PRIVATE_MEM_SIZE);
374 private long getWorkGroupInfo(
final CLDevice device,
final int flag) {
376 if(ret != CL_SUCCESS) {
377 throw newException(ret,
"error while asking for clGetKernelWorkGroupInfo of "+
this+
" on "+device);
379 return buffer.getLong(0);
389 program.onKernelReleased(
this);
390 if(ret != CL_SUCCESS) {
391 throw newException(ret,
"can not release "+
this);
397 return "CLKernel [id: " +
ID
398 +
" name: " +
name+
"]";
402 public boolean equals(
final Object obj) {
406 if (getClass() != obj.getClass()) {
410 if (this.
ID != other.
ID) {
413 if (!this.program.
equals(other.program)) {
422 hash = 43 * hash + (int) (this.
ID ^ (this.
ID >>> 32));
423 hash = 43 * hash + (this.program !=
null ? this.program.
hashCode() : 0);
This object represents an OpenCL device.
High level abstraction for an OpenCL Kernel.
void release()
Releases all resources of this kernel from its context.
boolean isForce32BitArgsEnabled()
CLKernel putArgs(final CLMemory<?>... values)
CLKernel rewind()
Resets the argument index to 0.
long[] getCompileWorkGroupSize(final CLDevice device)
Returns the work-group size specified by the attribute((reqd_work_group_size(X, Y,...
int position()
Returns the argument index used in the relative putArt(...) methods.
CLKernel setArg(final int argumentIndex, final CLMemory<?> value)
CLKernel setArg(final int argumentIndex, final long value)
CLKernel putArg(final int value)
CLKernel putArg(final CLMemory<?> value)
CLKernel clone()
Returns a new instance of this kernel with uninitialized arguments.
CLKernel putArg(final short value)
boolean equals(final Object obj)
CLKernel setArg(final int argumentIndex, final float value)
long getWorkGroupSize(final CLDevice device)
Returns the work group size for this kernel on the given device.
long getPrivateMemSize(final CLDevice device)
Returns the number of bytes of private memory used by each work item in the kernel.
CLKernel setArgs(final CLMemory<?>... values)
CLKernel putNullArg(final int size)
long getPreferredWorkGroupSizeMultiple(final CLDevice device)
Returns the preferred multiple of workgroup size to use for kernel launch.
CLKernel setForce32BitArgs(final boolean force)
Forces double and long arguments to be passed as float and int to the OpenCL kernel.
CLKernel putArg(final long value)
CLKernel putArg(final float value)
CLKernel putArg(final double value)
CLKernel setArg(final int argumentIndex, final int value)
CLKernel setArg(final int argumentIndex, final double value)
CLKernel setArg(final int argumentIndex, final short value)
long getLocalMemorySize(final CLDevice device)
Returns the amount of local memory in bytes being used by a kernel.
CLKernel setArgs(final Object... values)
CLKernel setNullArg(final int argumentIndex, final int size)
Common superclass for all OpenCL memory types.
CLContext getContext()
Returns the context for this OpenCL object.
final long ID
The OpenCL object handle.
CLPlatform getPlatform()
Returns the platform for this OpenCL object.
Represents a OpenCL program executed on one or more CLDevices.
boolean isExecutable()
Returns true if the build status 'BUILD_SUCCESS' for at least one device of this program exists.
boolean equals(final Object obj)
CLKernel createCLKernel(final String kernelName)
Creates a kernel with the specified kernel name.
static String clString2JavaString(final byte[] chars, int clLength)
Java bindings to OpenCL, the Open Computing Language.
int clGetKernelInfo(long kernel, int param_name, long param_value_size, Buffer param_value, PointerBuffer param_value_size_ret)
Interface to C language function: cl_int {@native clGetKernelInfo}(cl_kernel kernel,...
int clReleaseKernel(long kernel)
Interface to C language function: cl_int {@native clReleaseKernel}(cl_kernel kernel)
int clSetKernelArg(long kernel, int arg_index, long arg_size, Buffer arg_value)
Interface to C language function: cl_int {@native clSetKernelArg}(cl_kernel kernel,...
int clGetKernelWorkGroupInfo(long kernel, long device, int param_name, long param_value_size, Buffer param_value, PointerBuffer param_value_size_ret)
Interface to C language function: cl_int {@native clGetKernelWorkGroupInfo}(cl_kernel kernel,...