SerranoEngine

public class SerranoEngine

This class is supposed be initialized and configured at the very beggining of your app. It is responsible for setting up computation envirionment involving with iOS’s GPU device. It will initialize serveral instances which will involve with heavy GPU evaluation and are reconmmended reusing by Apple documentation.

The configuredEngine is a singleton instance and should be used everywhere you need to access GPUDevice (an instance of MTLDevice),\ serranoCommandQueue (an instance of MTLCommandQueue).

Initial and configure a GPU engine:

   let configuredEngine = SerranoEngine.configuredEngine
   let (success, message) = configuredEngine.configureEngine(computationMode: .GPU, serranoCommandQueue: nil, serranoMTLLibrary: nil, systemDefaultGPUDevice: nil)
   if !success {
       // Failed to congiure GPU device
       print(message)
   }

Initial and configure a CPU engine:

    let configuredEngine = SerranoEngine.configuredEngine
    configuredEngine.configureEngine(computationMode: .CPU, serranoCommandQueue: nil, serranoMTLLibrary: nil, systemDefaultGPUDevice: nil)
  • Instance

    Declaration

    Swift

    public static let configuredEngine = SerranoEngine()
  • GPU device.

    Declaration

    Swift

    public var  GPUDevice: MTLDevice?
  • Metal Command Queue for serrano.

    Declaration

    Swift

    public var serranoCommandQueue: MTLCommandQueue?
  • Engine computation mode

    Declaration

    Swift

    public var computationMode: OperatorComputationMode
  • Loaded MTLCompute​Pipeline​States. A dictionary with label as key and corresponding MTLComputePipelineState as value.

    Declaration

    Swift

    public var loadedGPUKernels: [String : MTLComputePipelineState]
  • METAL library

    Declaration

    Swift

    public var metalLibrary: MTLLibrary?
  • User defined Metal libarary

    Declaration

    Swift

    public var userMetalLibarary: [MTLLibrary] = [MTLLibrary]()
  • Default operator computation Mode

    Declaration

    Swift

    public var defaultComputationMode: OperatorComputationMode = .Auto
  • Setup GPUDevice, serranoCommandQueue and computationMode of engine.

    Note

    When user gives no device instance and method fails to initialized a GPU device instance, \ Serrano will automatically set up computationMode to OperatorComputationMode.CPU.

    Warning

    This method must be called before doing any GPU related computation.

    Declaration

    Swift

    public func configureEngine(computationMode mode: OperatorComputationMode,
                                serranoCommandQueue commandQueue: MTLCommandQueue? = nil,
                                serranoMTLLibrary library: MTLLibrary? = nil,
                                systemDefaultGPUDevice gpu: MTLDevice? = nil) -> (result: Bool, message: String)

    Parameters

    computationMode

    one of choies in enum OperatorComputationMode (GPU or CPU).

    serranoCommandQueue

    Optional. If it is nil, method will initialize a command queue in GPU mode.

    serranoMTLLibrary

    Optional. If it is nil, method will initialize a default MTLLibrary.

    systemDefaultGPUDevice

    Optional. If this is nil and computationMode is GPU,\ method will try to create a instance calling MTLCreate​System​Default​Device().\ If failed to initialize a GPU device instance, will return false.

    Return Value

    • result: If configure engine successfully.
    • message: Message information of configure.
  • Reset all attributes of engine to default values.

    Declaration

    Swift

    public func resetEngine()
  • Check if current configured engine has available GPU device.

    Declaration

    Swift

    public func hasAvailableGPU() -> Bool

    Return Value

    true if has available device.

  • Get a kernel in loadedGPUKernels, if not in loaded kernels, return nil

    Declaration

    Swift

    internal func getGPUKernelFromLoadedKernels(kenelLabel label: String) -> MTLComputePipelineState?
  • Load GPU compute kernel from Serrano’s default Metal library. Before loading method will check if already loaded this kernel, if loaded just return the kernel If not found in loadedGPUKernels, method will create a new MTLCompute​Pipeline​State instance for function with target label and return the kernel. When failed to find the function, will return a nil kernel with error information.

    Note

    User must configure the engine first before loading any GPU kernels. If method could find GPUDevice or ‘metalLibrary’ is nil, it will raise a fatal error.

    Declaration

    Swift

    public func loadGPUKernel(kernelLabel label: String) -> (result: MTLComputePipelineState?, message: String)

    Parameters

    label

    Kernel function name in Metal file.

    Return Value

    kernel: Optional. Target kernel. Will be nil if fails to load; message: message information.