Package com.aparapi.internal.kernel
Class KernelRunner
- java.lang.Object
-
- com.aparapi.internal.jni.KernelRunnerJNI
-
- com.aparapi.internal.kernel.KernelRunner
-
public class KernelRunner extends KernelRunnerJNI
The class is responsible for executingKernel
implementations.
TheKernelRunner
is the real workhorse for Aparapi. EachKernel
instance creates a singleKernelRunner
to encapsulate state and to help coordinate interactions between theKernel
and it's execution logic.
TheKernelRunner
is created lazily as a result of callingKernel.execute()
. A this time theExecutionMode
is consulted to determine the default requested mode. This will dictate how theKernelRunner
will attempt to execute theKernel
- See Also:
Kernel.execute(int _globalSize)
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static class
KernelRunner.ExecutionSettings
private static class
KernelRunner.FJSafeBarrier
private class
KernelRunner.ThreadDiedHandler
private static interface
KernelRunner.ThreadIdSetter
-
Field Summary
Fields Modifier and Type Field Description private int
argc
private KernelArg[]
args
static boolean
BINARY_CACHING_DISABLED
static int
CANCEL_STATUS_FALSE
static int
CANCEL_STATUS_TRUE
private java.util.Set<java.lang.String>
capabilitiesSet
private static java.lang.String
CODE_GEN_ERROR_MARKER
private Entrypoint
entryPoint
private boolean
executing
private boolean
explicit
private KernelRunner.ThreadDiedHandler
handler
private java.nio.ByteBuffer
inBufferRemote
A direct ByteBuffer used for asynchronous intercommunication between java and JNI C code.private java.nio.IntBuffer
inBufferRemoteInt
private boolean
isFallBack
private long
jniContextHandle
private Kernel
kernel
private java.util.Hashtable<Device,java.lang.Boolean>
kernelIsCompiledForDeviceHash
private java.util.Hashtable<Device,java.lang.Boolean>
kernelNeverExecutedForDeviceHash
private static java.util.logging.Logger
logger
private static java.util.concurrent.ForkJoinPool.ForkJoinWorkerThreadFactory
lowPriorityThreadFactory
private static int
MINIMUM_ARRAY_SIZE
private static java.util.HashMap<java.lang.Class<? extends Kernel>,java.lang.String>
openCLCache
private java.nio.ByteBuffer
outBufferRemote
A direct ByteBuffer used for asynchronous intercommunication between java and JNI C code.private java.nio.IntBuffer
outBufferRemoteInt
static int
PASS_ID_COMPLETED_EXECUTION
static int
PASS_ID_PREPARING_EXECUTION
private int
passId
private java.util.Set<java.lang.Object>
puts
private static java.util.LinkedHashSet<java.lang.String>
seenBinaryKeys
private java.util.concurrent.ForkJoinPool
threadPool
private boolean
usesOopConversion
-
Fields inherited from class com.aparapi.internal.jni.KernelRunnerJNI
ARG_APARAPI_BUFFER, ARG_ARRAY, ARG_ARRAYLENGTH, ARG_BOOLEAN, ARG_BYTE, ARG_CHAR, ARG_CONSTANT, ARG_DOUBLE, ARG_EXPLICIT, ARG_EXPLICIT_WRITE, ARG_FLOAT, ARG_GLOBAL, ARG_INT, ARG_LOCAL, ARG_LONG, ARG_OBJ_ARRAY_STRUCT, ARG_PRIMITIVE, ARG_READ, ARG_SHORT, ARG_STATIC, ARG_WRITE, JNI_FLAG_USE_ACC, JNI_FLAG_USE_GPU
-
-
Constructor Summary
Constructors Constructor Description KernelRunner(Kernel _kernel)
Create a KernelRunner for a specific Kernel instance.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
allocateArrayBufferIfFirstTimeOrArrayChanged(KernelArg arg, java.lang.Object newRef, int objArraySize, int totalStructSize, int totalBufferSize)
Helper method that manages the memory allocation for storing the kernel argument data, so that the data can be exchanged between the host and the OpenCL device.void
cancelMultiPass()
void
cleanUpArrays()
private void
clearCancelMultiPass()
Kernel
compile(java.lang.String _entrypoint, Device device)
private java.lang.String
describeDevice()
void
dispose()
Kernel.dispose()
delegates toKernelRunner.dispose()
which delegates todisposeJNI()
to actually close JNI data structures.Kernel
execute(java.lang.String _entrypoint, Range _range, int _passes)
private Kernel
executeInternalInner(KernelRunner.ExecutionSettings _settings, Device aparapiDevice, boolean compileOnly)
private Kernel
executeInternalOuter(KernelRunner.ExecutionSettings _settings)
protected void
executeJava(KernelRunner.ExecutionSettings _settings, Device device)
Execute using a Java thread pool, or sequentially, or using an alternative algorithm, usually as a result of failing to compile or execute OpenCLprivate Kernel
executeOpenCL(Device device, KernelRunner.ExecutionSettings _settings)
private void
extractAtomicIntegerConversionBuffer(KernelArg arg)
private void
extractOopConversionBuffer(KernelArg arg)
private Kernel
fallBackByExecutionMode(KernelRunner.ExecutionSettings _settings)
private Kernel
fallBackToNextDevice(Device device, KernelRunner.ExecutionSettings _settings, java.lang.Exception _exception)
private Kernel
fallBackToNextDevice(Device device, KernelRunner.ExecutionSettings _settings, java.lang.Exception _exception, boolean _silently)
private Kernel
fallBackToNextDevice(Device device, KernelRunner.ExecutionSettings _settings, java.lang.String _reason)
void
get(java.lang.Object array)
Enqueue a request to return this array from the GPU.int
getCancelState()
private ClassModel
getClassModelFromArg(KernelArg arg, java.lang.Class<?> arrayClass)
Helper method to retrieve the class model from a kernel argument.int
getCurrentPass()
Returns the index of the current pass, or one of two special constants with negative values to indicate special progress states.private int
getCurrentPassLocal()
protected int
getCurrentPassRemote()
int[]
getKernelCompileWorkGroupSize(Device device)
long
getKernelLocalMemSizeInUse(Device device)
int
getKernelMaxWorkGroupSize(Device device)
long
getKernelMinimumPrivateMemSizeInUsePerWorkItem(Device device)
int
getKernelPreferredWorkGroupSizeMultiple(Device device)
private int
getPrimitiveSize(int type)
java.util.List<ProfileInfo>
getProfileInfo()
(package private) boolean
has3DImageWritesSupport()
(package private) boolean
hasByteAddressableStoreSupport()
(package private) boolean
hasFP16Support()
(package private) boolean
hasFP64Support()
(package private) boolean
hasGlobalInt32BaseAtomicsSupport()
(package private) boolean
hasGlobalInt32ExtendedAtomicsSupport()
(package private) boolean
hasGLSharingSupport()
(package private) boolean
hasInt64BaseAtomicsSupport()
(package private) boolean
hasInt64ExtendedAtomicsSupport()
(package private) boolean
hasLocalInt32BaseAtomicsSupport()
(package private) boolean
hasLocalInt32ExtendedAtomicsSupport()
(package private) boolean
hasSelectFPRoundingModeSupport()
private boolean
isDeviceCompatible(Device device)
boolean
isExecuting()
True while any of theexecute()
methods are in progress.boolean
isExplicit()
private void
maybeReportProfile(KernelRunner.ExecutionSettings _settings)
private boolean
prepareAtomicIntegerConversionBuffer(KernelArg arg)
private boolean
prepareOopConversionBuffer(KernelArg arg)
void
put(java.lang.Object array)
Tag this array so that it is explicitly enqueued before the kernel is executed.private void
recreateRange(KernelRunner.ExecutionSettings _settings)
private void
restoreObjects()
void
setExplicit(boolean _explicit)
private void
setMultiArrayType(KernelArg arg, java.lang.Class<?> type)
java.lang.String
toString()
private boolean
updateKernelArrayRefs()
-
Methods inherited from class com.aparapi.internal.jni.KernelRunnerJNI
buildProgramJNI, disposeJNI, getExtensionsJNI, getJNI, getKernelCompileWorkGroupSizeJNI, getKernelLocalMemSizeInUseJNI, getKernelMaxWorkGroupSizeJNI, getKernelMinimumPrivateMemSizeInUsePerWorkItemJNI, getKernelPreferredWorkGroupSizeMultipleJNI, getProfileInfoJNI, initJNI, runKernelJNI, setArgsJNI
-
-
-
-
Field Detail
-
BINARY_CACHING_DISABLED
public static boolean BINARY_CACHING_DISABLED
-
MINIMUM_ARRAY_SIZE
private static final int MINIMUM_ARRAY_SIZE
- See Also:
- Constant Field Values
-
PASS_ID_PREPARING_EXECUTION
public static final int PASS_ID_PREPARING_EXECUTION
- See Also:
getCurrentPass()
, Constant Field Values
-
PASS_ID_COMPLETED_EXECUTION
public static final int PASS_ID_COMPLETED_EXECUTION
- See Also:
getCurrentPass()
, Constant Field Values
-
CANCEL_STATUS_FALSE
public static final int CANCEL_STATUS_FALSE
- See Also:
- Constant Field Values
-
CANCEL_STATUS_TRUE
public static final int CANCEL_STATUS_TRUE
- See Also:
- Constant Field Values
-
CODE_GEN_ERROR_MARKER
private static final java.lang.String CODE_GEN_ERROR_MARKER
-
logger
private static java.util.logging.Logger logger
-
jniContextHandle
private long jniContextHandle
-
kernel
private final Kernel kernel
-
entryPoint
private Entrypoint entryPoint
-
argc
private int argc
-
executing
private volatile boolean executing
-
passId
private volatile int passId
-
inBufferRemote
private final java.nio.ByteBuffer inBufferRemote
A direct ByteBuffer used for asynchronous intercommunication between java and JNI C code.At present this is a 4 byte buffer to be interpreted as an int[1], used for passing from java to C a single integer interpreted as a cancellation indicator.
-
inBufferRemoteInt
private final java.nio.IntBuffer inBufferRemoteInt
-
outBufferRemote
private final java.nio.ByteBuffer outBufferRemote
A direct ByteBuffer used for asynchronous intercommunication between java and JNI C code.At present this is a 4 byte buffer to be interpreted as an int[1], used for passing from C to java a single integer interpreted as a the current pass id.
-
outBufferRemoteInt
private final java.nio.IntBuffer outBufferRemoteInt
-
isFallBack
private boolean isFallBack
-
lowPriorityThreadFactory
private static final java.util.concurrent.ForkJoinPool.ForkJoinWorkerThreadFactory lowPriorityThreadFactory
-
handler
private final KernelRunner.ThreadDiedHandler handler
-
threadPool
private final java.util.concurrent.ForkJoinPool threadPool
-
openCLCache
private static java.util.HashMap<java.lang.Class<? extends Kernel>,java.lang.String> openCLCache
-
seenBinaryKeys
private static java.util.LinkedHashSet<java.lang.String> seenBinaryKeys
-
kernelIsCompiledForDeviceHash
private final java.util.Hashtable<Device,java.lang.Boolean> kernelIsCompiledForDeviceHash
-
kernelNeverExecutedForDeviceHash
private final java.util.Hashtable<Device,java.lang.Boolean> kernelNeverExecutedForDeviceHash
-
capabilitiesSet
private java.util.Set<java.lang.String> capabilitiesSet
-
args
private KernelArg[] args
-
usesOopConversion
private boolean usesOopConversion
-
puts
private final java.util.Set<java.lang.Object> puts
-
explicit
private boolean explicit
-
-
Constructor Detail
-
KernelRunner
public KernelRunner(Kernel _kernel)
Create a KernelRunner for a specific Kernel instance.- Parameters:
_kernel
-
-
-
Method Detail
-
cleanUpArrays
public void cleanUpArrays()
-
dispose
public void dispose()
Kernel.dispose()
delegates toKernelRunner.dispose()
which delegates todisposeJNI()
to actually close JNI data structures.- See Also:
KernelRunnerJNI.disposeJNI(long)
-
getKernelMinimumPrivateMemSizeInUsePerWorkItem
public long getKernelMinimumPrivateMemSizeInUsePerWorkItem(Device device) throws QueryFailedException
- Throws:
QueryFailedException
-
getKernelLocalMemSizeInUse
public long getKernelLocalMemSizeInUse(Device device) throws QueryFailedException
- Throws:
QueryFailedException
-
getKernelPreferredWorkGroupSizeMultiple
public int getKernelPreferredWorkGroupSizeMultiple(Device device) throws QueryFailedException
- Throws:
QueryFailedException
-
getKernelMaxWorkGroupSize
public int getKernelMaxWorkGroupSize(Device device) throws QueryFailedException
- Throws:
QueryFailedException
-
getKernelCompileWorkGroupSize
public int[] getKernelCompileWorkGroupSize(Device device) throws QueryFailedException
- Throws:
QueryFailedException
-
hasFP64Support
boolean hasFP64Support()
-
hasSelectFPRoundingModeSupport
boolean hasSelectFPRoundingModeSupport()
-
hasGlobalInt32BaseAtomicsSupport
boolean hasGlobalInt32BaseAtomicsSupport()
-
hasGlobalInt32ExtendedAtomicsSupport
boolean hasGlobalInt32ExtendedAtomicsSupport()
-
hasLocalInt32BaseAtomicsSupport
boolean hasLocalInt32BaseAtomicsSupport()
-
hasLocalInt32ExtendedAtomicsSupport
boolean hasLocalInt32ExtendedAtomicsSupport()
-
hasInt64BaseAtomicsSupport
boolean hasInt64BaseAtomicsSupport()
-
hasInt64ExtendedAtomicsSupport
boolean hasInt64ExtendedAtomicsSupport()
-
has3DImageWritesSupport
boolean has3DImageWritesSupport()
-
hasByteAddressableStoreSupport
boolean hasByteAddressableStoreSupport()
-
hasFP16Support
boolean hasFP16Support()
-
hasGLSharingSupport
boolean hasGLSharingSupport()
-
executeJava
protected void executeJava(KernelRunner.ExecutionSettings _settings, Device device)
Execute using a Java thread pool, or sequentially, or using an alternative algorithm, usually as a result of failing to compile or execute OpenCL
-
getClassModelFromArg
private ClassModel getClassModelFromArg(KernelArg arg, java.lang.Class<?> arrayClass)
Helper method to retrieve the class model from a kernel argument.- Parameters:
arg
- the kernel argumentarrayClass
- the array Java class for the argument- Returns:
- the Aparapi ClassModel instance.
-
allocateArrayBufferIfFirstTimeOrArrayChanged
public boolean allocateArrayBufferIfFirstTimeOrArrayChanged(KernelArg arg, java.lang.Object newRef, int objArraySize, int totalStructSize, int totalBufferSize)
Helper method that manages the memory allocation for storing the kernel argument data, so that the data can be exchanged between the host and the OpenCL device.- Parameters:
arg
- the kernel argumentnewRef
- the actual Java data instanceobjArraySize
- the number of elements in the Java arraytotalStructSize
- the size of each target array elementtotalBufferSize
- the total buffer size including memory alignment- Returns:
- true, if internal buffer had to be allocated or reallocated holding the data
- false, if buffer didn't change and is already allocated
-
prepareOopConversionBuffer
private boolean prepareOopConversionBuffer(KernelArg arg) throws AparapiException
- Parameters:
arg
-- Returns:
- Throws:
AparapiException
-
extractOopConversionBuffer
private void extractOopConversionBuffer(KernelArg arg) throws AparapiException
- Throws:
AparapiException
-
restoreObjects
private void restoreObjects() throws AparapiException
- Throws:
AparapiException
-
prepareAtomicIntegerConversionBuffer
private boolean prepareAtomicIntegerConversionBuffer(KernelArg arg) throws AparapiException
- Throws:
AparapiException
-
extractAtomicIntegerConversionBuffer
private void extractAtomicIntegerConversionBuffer(KernelArg arg) throws AparapiException
- Throws:
AparapiException
-
updateKernelArrayRefs
private boolean updateKernelArrayRefs() throws AparapiException
- Throws:
AparapiException
-
executeOpenCL
private Kernel executeOpenCL(Device device, KernelRunner.ExecutionSettings _settings) throws AparapiException
- Throws:
AparapiException
-
fallBackByExecutionMode
private Kernel fallBackByExecutionMode(KernelRunner.ExecutionSettings _settings)
-
recreateRange
private void recreateRange(KernelRunner.ExecutionSettings _settings)
-
fallBackToNextDevice
private Kernel fallBackToNextDevice(Device device, KernelRunner.ExecutionSettings _settings, java.lang.String _reason)
-
fallBackToNextDevice
private Kernel fallBackToNextDevice(Device device, KernelRunner.ExecutionSettings _settings, java.lang.Exception _exception)
-
fallBackToNextDevice
private Kernel fallBackToNextDevice(Device device, KernelRunner.ExecutionSettings _settings, java.lang.Exception _exception, boolean _silently)
-
compile
public Kernel compile(java.lang.String _entrypoint, Device device) throws CompileFailedException
- Throws:
CompileFailedException
-
executeInternalOuter
private Kernel executeInternalOuter(KernelRunner.ExecutionSettings _settings)
-
executeInternalInner
private Kernel executeInternalInner(KernelRunner.ExecutionSettings _settings, Device aparapiDevice, boolean compileOnly) throws CompileFailedException
- Throws:
CompileFailedException
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
describeDevice
private java.lang.String describeDevice()
-
maybeReportProfile
private void maybeReportProfile(KernelRunner.ExecutionSettings _settings)
-
isDeviceCompatible
private boolean isDeviceCompatible(Device device)
-
getCancelState
public int getCancelState()
-
cancelMultiPass
public void cancelMultiPass()
-
clearCancelMultiPass
private void clearCancelMultiPass()
-
getCurrentPass
public int getCurrentPass()
Returns the index of the current pass, or one of two special constants with negative values to indicate special progress states. Those constants arePASS_ID_PREPARING_EXECUTION
to indicate that the Kernel has started executing but not reached the initial pass, orPASS_ID_COMPLETED_EXECUTION
to indicate that execution is complete (possibly due to early termination viacancelMultiPass()
), i.e. the Kernel is idle.PASS_ID_COMPLETED_EXECUTION
is also returned before the first execution has been invoked.This can be used, for instance, to update a visual progress bar.
- See Also:
execute(String, Range, int)
-
isExecuting
public boolean isExecuting()
True while any of theexecute()
methods are in progress.
-
getCurrentPassRemote
protected int getCurrentPassRemote()
-
getCurrentPassLocal
private int getCurrentPassLocal()
-
getPrimitiveSize
private int getPrimitiveSize(int type)
-
setMultiArrayType
private void setMultiArrayType(KernelArg arg, java.lang.Class<?> type) throws AparapiException
- Throws:
AparapiException
-
get
public void get(java.lang.Object array)
Enqueue a request to return this array from the GPU. This method blocks until the array is available.
Note thatKernel.put(type [])
calls will delegate to this call.
Package public- Parameters:
array
- It is assumed that this parameter is indeed an array (of int, float, short etc).- See Also:
Kernel.get(int[] arr)
,Kernel.get(float[] arr)
,Kernel.get(double[] arr)
,Kernel.get(long[] arr)
,Kernel.get(char[] arr)
,Kernel.get(boolean[] arr)
-
getProfileInfo
public java.util.List<ProfileInfo> getProfileInfo()
-
put
public void put(java.lang.Object array)
Tag this array so that it is explicitly enqueued before the kernel is executed.
Note thatKernel.put(type [])
calls will delegate to this call.
Package public- Parameters:
array
- It is assumed that this parameter is indeed an array (of int, float, short etc).- See Also:
Kernel.put(int[] arr)
,Kernel.put(float[] arr)
,Kernel.put(double[] arr)
,Kernel.put(long[] arr)
,Kernel.put(char[] arr)
,Kernel.put(boolean[] arr)
-
setExplicit
public void setExplicit(boolean _explicit)
-
isExplicit
public boolean isExplicit()
-
-