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 executingKernelimplementations.
TheKernelRunneris the real workhorse for Aparapi. EachKernelinstance creates a singleKernelRunnerto encapsulate state and to help coordinate interactions between theKerneland it's execution logic.
TheKernelRunneris created lazily as a result of callingKernel.execute(). A this time theExecutionModeis consulted to determine the default requested mode. This will dictate how theKernelRunnerwill attempt to execute theKernel- See Also:
Kernel.execute(int _globalSize)
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static classKernelRunner.ExecutionSettingsprivate static classKernelRunner.FJSafeBarrierprivate classKernelRunner.ThreadDiedHandlerprivate static interfaceKernelRunner.ThreadIdSetter
-
Field Summary
Fields Modifier and Type Field Description private intargcprivate KernelArg[]argsstatic booleanBINARY_CACHING_DISABLEDstatic intCANCEL_STATUS_FALSEstatic intCANCEL_STATUS_TRUEprivate java.util.Set<java.lang.String>capabilitiesSetprivate static java.lang.StringCODE_GEN_ERROR_MARKERprivate EntrypointentryPointprivate booleanexecutingprivate booleanexplicitprivate KernelRunner.ThreadDiedHandlerhandlerprivate java.nio.ByteBufferinBufferRemoteA direct ByteBuffer used for asynchronous intercommunication between java and JNI C code.private java.nio.IntBufferinBufferRemoteIntprivate booleanisFallBackprivate longjniContextHandleprivate Kernelkernelprivate java.util.Hashtable<Device,java.lang.Boolean>kernelIsCompiledForDeviceHashprivate java.util.Hashtable<Device,java.lang.Boolean>kernelNeverExecutedForDeviceHashprivate static java.util.logging.Loggerloggerprivate static java.util.concurrent.ForkJoinPool.ForkJoinWorkerThreadFactorylowPriorityThreadFactoryprivate static intMINIMUM_ARRAY_SIZEprivate static java.util.HashMap<java.lang.Class<? extends Kernel>,java.lang.String>openCLCacheprivate java.nio.ByteBufferoutBufferRemoteA direct ByteBuffer used for asynchronous intercommunication between java and JNI C code.private java.nio.IntBufferoutBufferRemoteIntstatic intPASS_ID_COMPLETED_EXECUTIONstatic intPASS_ID_PREPARING_EXECUTIONprivate intpassIdprivate java.util.Set<java.lang.Object>putsprivate static java.util.LinkedHashSet<java.lang.String>seenBinaryKeysprivate java.util.concurrent.ForkJoinPoolthreadPoolprivate booleanusesOopConversion-
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 booleanallocateArrayBufferIfFirstTimeOrArrayChanged(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.voidcancelMultiPass()voidcleanUpArrays()private voidclearCancelMultiPass()Kernelcompile(java.lang.String _entrypoint, Device device)private java.lang.StringdescribeDevice()voiddispose()Kernel.dispose()delegates toKernelRunner.dispose()which delegates todisposeJNI()to actually close JNI data structures.Kernelexecute(java.lang.String _entrypoint, Range _range, int _passes)private KernelexecuteInternalInner(KernelRunner.ExecutionSettings _settings, Device aparapiDevice, boolean compileOnly)private KernelexecuteInternalOuter(KernelRunner.ExecutionSettings _settings)protected voidexecuteJava(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 KernelexecuteOpenCL(Device device, KernelRunner.ExecutionSettings _settings)private voidextractAtomicIntegerConversionBuffer(KernelArg arg)private voidextractOopConversionBuffer(KernelArg arg)private KernelfallBackByExecutionMode(KernelRunner.ExecutionSettings _settings)private KernelfallBackToNextDevice(Device device, KernelRunner.ExecutionSettings _settings, java.lang.Exception _exception)private KernelfallBackToNextDevice(Device device, KernelRunner.ExecutionSettings _settings, java.lang.Exception _exception, boolean _silently)private KernelfallBackToNextDevice(Device device, KernelRunner.ExecutionSettings _settings, java.lang.String _reason)voidget(java.lang.Object array)Enqueue a request to return this array from the GPU.intgetCancelState()private ClassModelgetClassModelFromArg(KernelArg arg, java.lang.Class<?> arrayClass)Helper method to retrieve the class model from a kernel argument.intgetCurrentPass()Returns the index of the current pass, or one of two special constants with negative values to indicate special progress states.private intgetCurrentPassLocal()protected intgetCurrentPassRemote()int[]getKernelCompileWorkGroupSize(Device device)longgetKernelLocalMemSizeInUse(Device device)intgetKernelMaxWorkGroupSize(Device device)longgetKernelMinimumPrivateMemSizeInUsePerWorkItem(Device device)intgetKernelPreferredWorkGroupSizeMultiple(Device device)private intgetPrimitiveSize(int type)java.util.List<ProfileInfo>getProfileInfo()(package private) booleanhas3DImageWritesSupport()(package private) booleanhasByteAddressableStoreSupport()(package private) booleanhasFP16Support()(package private) booleanhasFP64Support()(package private) booleanhasGlobalInt32BaseAtomicsSupport()(package private) booleanhasGlobalInt32ExtendedAtomicsSupport()(package private) booleanhasGLSharingSupport()(package private) booleanhasInt64BaseAtomicsSupport()(package private) booleanhasInt64ExtendedAtomicsSupport()(package private) booleanhasLocalInt32BaseAtomicsSupport()(package private) booleanhasLocalInt32ExtendedAtomicsSupport()(package private) booleanhasSelectFPRoundingModeSupport()private booleanisDeviceCompatible(Device device)booleanisExecuting()True while any of theexecute()methods are in progress.booleanisExplicit()private voidmaybeReportProfile(KernelRunner.ExecutionSettings _settings)private booleanprepareAtomicIntegerConversionBuffer(KernelArg arg)private booleanprepareOopConversionBuffer(KernelArg arg)voidput(java.lang.Object array)Tag this array so that it is explicitly enqueued before the kernel is executed.private voidrecreateRange(KernelRunner.ExecutionSettings _settings)private voidrestoreObjects()voidsetExplicit(boolean _explicit)private voidsetMultiArrayType(KernelArg arg, java.lang.Class<?> type)java.lang.StringtoString()private booleanupdateKernelArrayRefs()-
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:
toStringin 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_EXECUTIONto indicate that the Kernel has started executing but not reached the initial pass, orPASS_ID_COMPLETED_EXECUTIONto indicate that execution is complete (possibly due to early termination viacancelMultiPass()), i.e. the Kernel is idle.PASS_ID_COMPLETED_EXECUTIONis 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()
-
-