Package ai.djl

Class Device


  • public final class Device
    extends java.lang.Object
    The Device class provides the specified assignment for CPU/GPU processing on the NDArray.

    Users can use this to specify whether to load/compute the NDArray on CPU/GPU with deviceType and deviceId provided.

    See Also:
    The D2L chapter on GPU devices
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static interface  Device.Type
      Contains device type string constants.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      static Device cpu()
      Returns the default CPU Device.
      boolean equals​(java.lang.Object o)
      static Device fromName​(java.lang.String deviceName)
      Parses a deviceName string into a device for the default engine.
      static Device fromName​(java.lang.String deviceName, Engine engine)
      Parses a deviceName string into a device.
      int getDeviceId()
      Returns the deviceId of the Device.
      java.lang.String getDeviceType()
      Returns the device type of the Device.
      static Device gpu()
      Returns the default GPU Device.
      static Device gpu​(int deviceId)
      Returns a new instance of GPU Device with the specified deviceId.
      int hashCode()
      boolean isGpu()
      Returns if the Device is GPU.
      static Device of​(java.lang.String deviceType, int deviceId)
      Returns a Device with device type and device id.
      java.lang.String toString()
      • Methods inherited from class java.lang.Object

        clone, finalize, getClass, notify, notifyAll, wait, wait, wait
    • Method Detail

      • of

        public static Device of​(java.lang.String deviceType,
                                int deviceId)
        Returns a Device with device type and device id.
        Parameters:
        deviceType - the device type, typically CPU or GPU
        deviceId - the deviceId on the hardware.
        Returns:
        a Device instance
      • fromName

        public static Device fromName​(java.lang.String deviceName)
        Parses a deviceName string into a device for the default engine.
        Parameters:
        deviceName - deviceName String to parse
        Returns:
        the parsed device
        See Also:
        fromName(String, Engine)
      • fromName

        public static Device fromName​(java.lang.String deviceName,
                                      Engine engine)
        Parses a deviceName string into a device.

        The main format of a device name string is "cpu", "gpu0", or "nc1". This is simply deviceType concatenated with the deviceId. If no deviceId is used, -1 will be assumed.

        There are also several simplified formats. The "-1", deviceNames corresponds to cpu. Non-negative integer deviceNames such as "0", "1", or "2" correspond to gpus with those deviceIds.

        Finally, unspecified deviceNames (null or "") are parsed into the engine's default device.

        Parameters:
        deviceName - deviceName string
        engine - the engine the devie is for
        Returns:
        the device
      • getDeviceType

        public java.lang.String getDeviceType()
        Returns the device type of the Device.
        Returns:
        the device type of the Device
      • getDeviceId

        public int getDeviceId()
        Returns the deviceId of the Device.
        Returns:
        the deviceId of the Device
      • isGpu

        public boolean isGpu()
        Returns if the Device is GPU.
        Returns:
        if the Device is GPU.
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
      • equals

        public boolean equals​(java.lang.Object o)
        Overrides:
        equals in class java.lang.Object
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class java.lang.Object
      • cpu

        public static Device cpu()
        Returns the default CPU Device.
        Returns:
        the default CPU Device
      • gpu

        public static Device gpu()
        Returns the default GPU Device.
        Returns:
        the default GPU Device
      • gpu

        public static Device gpu​(int deviceId)
        Returns a new instance of GPU Device with the specified deviceId.
        Parameters:
        deviceId - the GPU device ID
        Returns:
        a new instance of GPU Device with specified deviceId