Package org.nd4j.linalg.workspace
Class BaseWorkspaceMgr<T extends Enum<T>>
- java.lang.Object
-
- org.nd4j.linalg.workspace.BaseWorkspaceMgr<T>
-
- All Implemented Interfaces:
WorkspaceMgr<T>
public abstract class BaseWorkspaceMgr<T extends Enum<T>> extends Object implements WorkspaceMgr<T>
-
-
Field Summary
Fields Modifier and Type Field Description protected Map<T,WorkspaceConfiguration>
configMap
protected Set<T>
scopeOutOfWs
protected Map<T,String>
workspaceNames
-
Constructor Summary
Constructors Modifier Constructor Description protected
BaseWorkspaceMgr()
protected
BaseWorkspaceMgr(Set<T> scopeOutOfWs, Map<T,WorkspaceConfiguration> configMap, Map<T,String> workspaceNames)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
assertCurrentWorkspace(T arrayType, String msg)
Assert that the current workspace is the one for the specified array type.void
assertNotOpen(T arrayType, @NonNull String msg)
Assert thath the workspace for the specified array type is not open.void
assertOpen(T arrayType, String msg)
Assert thath the workspace for the specified array type is open.INDArray
castTo(T arrayType, @NonNull DataType dataType, @NonNull INDArray toCast, boolean dupIfCorrectType)
Cast the specified array to the specified datatype.
If the array is already the correct type, the bahaviour depends on the 'dupIfCorrectType' argument.
dupIfCorrectType = false && toCast.dataType() == dataType: return input array as-is (unless workspace is wrong)
dupIfCorrectType = true && toCast.dataType() == dataType: duplicate the array into the specified workspaceINDArray
create(T arrayType, @NonNull DataType dataType, @lombok.NonNull long... shape)
Create an array in the specified array type's workspace (or detached if none is specified).INDArray
create(T arrayType, @NonNull DataType dataType, @lombok.NonNull long[] shape, @lombok.NonNull char order)
Create an array in the specified array type's workspace (or detached if none is specified).INDArray
createUninitialized(T arrayType, @NonNull DataType dataType, @lombok.NonNull long[] shape, char order)
Create an uninitialized array in the specified array type's workspace (or detached if none is specified).INDArray
createUninitialized(T arrayType, DataType dataType, long... shape)
Create an uninitialized array in the specified array type's workspace (or detached if none is specified).INDArray
dup(T arrayType, @NonNull INDArray toDup)
Duplicate the array, where the array is put into the specified array type's workspace (if applicable)INDArray
dup(T arrayType, @NonNull INDArray toDup, char order)
Duplicate the array, where the array is put into the specified array type's workspace (if applicable)WorkspaceConfiguration
getConfiguration(T arrayType)
String
getWorkspaceName(T arrayType)
boolean
hasConfiguration(T arrayType)
Has the specified array type been configured in this workspace manager?boolean
isScopedOut(T arrayType)
boolean
isWorkspaceOpen(T arrayType)
Check if the workspace for the specified array type is open.INDArray
leverageTo(T arrayType, @NonNull INDArray array)
Leverage the array to the specified array type's workspace (or detach if required).MemoryWorkspace
notifyScopeBorrowed(T arrayType)
Borrow the scope for the specified array typeWorkspacesCloseable
notifyScopeEntered(@NonNull T... arrayTypes)
Open/enter multiple workspaces.MemoryWorkspace
notifyScopeEntered(T arrayType)
void
setConfiguration(T arrayType, WorkspaceConfiguration configuration)
Set the workspace configuration for the specified array typevoid
setScopedOutFor(T arrayType)
Set arrays to be scoped out (not in any workspace) for the specified array type.void
setWorkspace(T forEnum, @NonNull String wsName, @NonNull WorkspaceConfiguration configuration)
Seth the workspace name and configuration for the specified array typevoid
setWorkspaceName(T arrayType, @NonNull String name)
Set the workspace name for the specified array typeINDArray
validateArrayLocation(T arrayType, @NonNull INDArray array, boolean migrateIfInvalid, boolean exceptionIfDetached)
Validate that the specified array type is actually in the workspace it's supposed to be in
-
-
-
Method Detail
-
setConfiguration
public void setConfiguration(@NonNull T arrayType, WorkspaceConfiguration configuration)
Description copied from interface:WorkspaceMgr
Set the workspace configuration for the specified array type- Specified by:
setConfiguration
in interfaceWorkspaceMgr<T extends Enum<T>>
- Parameters:
arrayType
- Type of array to set the configuration forconfiguration
- Configuration for the specified array type
-
getConfiguration
public WorkspaceConfiguration getConfiguration(@NonNull T arrayType)
- Specified by:
getConfiguration
in interfaceWorkspaceMgr<T extends Enum<T>>
- Parameters:
arrayType
- Array type to get the workspace configuration for- Returns:
- Workspace configuration for the specified array type (or note, if no configuration has been set)
-
setScopedOutFor
public void setScopedOutFor(@NonNull T arrayType)
Description copied from interface:WorkspaceMgr
Set arrays to be scoped out (not in any workspace) for the specified array type. This means that create, dup, leverage etc methods will return result arrays that are not attached to any workspace- Specified by:
setScopedOutFor
in interfaceWorkspaceMgr<T extends Enum<T>>
- Parameters:
arrayType
- Array type to set scoped out for
-
isScopedOut
public boolean isScopedOut(@NonNull T arrayType)
- Specified by:
isScopedOut
in interfaceWorkspaceMgr<T extends Enum<T>>
- Parameters:
arrayType
- Array type- Returns:
- True if the specified array type is set to be scoped out
-
hasConfiguration
public boolean hasConfiguration(@NonNull T arrayType)
Description copied from interface:WorkspaceMgr
Has the specified array type been configured in this workspace manager?- Specified by:
hasConfiguration
in interfaceWorkspaceMgr<T extends Enum<T>>
- Parameters:
arrayType
- Array type to check- Returns:
- True if the array type has been configured (either scoped out, or a workspace has been set for this array type)
-
notifyScopeEntered
public MemoryWorkspace notifyScopeEntered(@NonNull T arrayType)
- Specified by:
notifyScopeEntered
in interfaceWorkspaceMgr<T extends Enum<T>>
- Parameters:
arrayType
- Array type to enter the scope for- Returns:
- Workspace for the specified array type
-
notifyScopeEntered
public WorkspacesCloseable notifyScopeEntered(@NonNull @NonNull T... arrayTypes)
Description copied from interface:WorkspaceMgr
Open/enter multiple workspaces. This is equivalent to nested opening of the specified workspaces- Specified by:
notifyScopeEntered
in interfaceWorkspaceMgr<T extends Enum<T>>
- Parameters:
arrayTypes
- Open the specified workspaces- Returns:
- Closeable for the specified workspaces
-
notifyScopeBorrowed
public MemoryWorkspace notifyScopeBorrowed(@NonNull T arrayType)
Description copied from interface:WorkspaceMgr
Borrow the scope for the specified array type- Specified by:
notifyScopeBorrowed
in interfaceWorkspaceMgr<T extends Enum<T>>
- Parameters:
arrayType
- Array type to borrow the scope for- Returns:
- Workspace
-
setWorkspaceName
public void setWorkspaceName(@NonNull T arrayType, @NonNull @NonNull String name)
Description copied from interface:WorkspaceMgr
Set the workspace name for the specified array type- Specified by:
setWorkspaceName
in interfaceWorkspaceMgr<T extends Enum<T>>
- Parameters:
arrayType
- Array type to set the workspace name forname
- Workspace name to set
-
getWorkspaceName
public String getWorkspaceName(@NonNull T arrayType)
- Specified by:
getWorkspaceName
in interfaceWorkspaceMgr<T extends Enum<T>>
- Parameters:
arrayType
- Array type to get the workspace name for (if set)- Returns:
- The workspace name for the specified array type (or null, if none has been set)
-
setWorkspace
public void setWorkspace(@NonNull T forEnum, @NonNull @NonNull String wsName, @NonNull @NonNull WorkspaceConfiguration configuration)
Description copied from interface:WorkspaceMgr
Seth the workspace name and configuration for the specified array type- Specified by:
setWorkspace
in interfaceWorkspaceMgr<T extends Enum<T>>
- Parameters:
forEnum
- Array typewsName
- Workspace nameconfiguration
- Workspace configuration
-
isWorkspaceOpen
public boolean isWorkspaceOpen(@NonNull T arrayType)
Description copied from interface:WorkspaceMgr
Check if the workspace for the specified array type is open. If the array type is set to be scoped out, this will return true- Specified by:
isWorkspaceOpen
in interfaceWorkspaceMgr<T extends Enum<T>>
- Parameters:
arrayType
- Array type- Returns:
- True if the workspace is open (or array type is set to scoped out)
-
assertOpen
public void assertOpen(T arrayType, String msg) throws ND4JWorkspaceException
Description copied from interface:WorkspaceMgr
Assert thath the workspace for the specified array type is open. For array types that are set to scoped out, this will be treated as a no-op- Specified by:
assertOpen
in interfaceWorkspaceMgr<T extends Enum<T>>
- Parameters:
arrayType
- Array type to checkmsg
- May be null. If non-null: include this in the exception- Throws:
ND4JWorkspaceException
- If the specified workspace is not open
-
assertNotOpen
public void assertNotOpen(@NonNull T arrayType, @NonNull @NonNull String msg)
Description copied from interface:WorkspaceMgr
Assert thath the workspace for the specified array type is not open. For array types that are set to scoped out, this will be treated as a no-op- Specified by:
assertNotOpen
in interfaceWorkspaceMgr<T extends Enum<T>>
- Parameters:
arrayType
- Array type to checkmsg
- May be null. If non-null: include this in the exception
-
assertCurrentWorkspace
public void assertCurrentWorkspace(@NonNull T arrayType, String msg)
Description copied from interface:WorkspaceMgr
Assert that the current workspace is the one for the specified array type. As perWorkspaceMgr.isWorkspaceOpen(Enum)
scoped out array types are ignored here.- Specified by:
assertCurrentWorkspace
in interfaceWorkspaceMgr<T extends Enum<T>>
- Parameters:
arrayType
- Array type to checkmsg
- May be null. Message to include in the exception
-
leverageTo
public INDArray leverageTo(@NonNull T arrayType, @NonNull @NonNull INDArray array)
Description copied from interface:WorkspaceMgr
Leverage the array to the specified array type's workspace (or detach if required). If the array is not attached (not defined in a workspace) - array is returned unmodified- Specified by:
leverageTo
in interfaceWorkspaceMgr<T extends Enum<T>>
- Parameters:
arrayType
- Array type's workspace to move the array toarray
- Array to leverage- Returns:
- Leveraged array (if leveraged, or original array otherwise)
-
validateArrayLocation
public INDArray validateArrayLocation(@NonNull T arrayType, @NonNull @NonNull INDArray array, boolean migrateIfInvalid, boolean exceptionIfDetached)
Description copied from interface:WorkspaceMgr
Validate that the specified array type is actually in the workspace it's supposed to be in- Specified by:
validateArrayLocation
in interfaceWorkspaceMgr<T extends Enum<T>>
- Parameters:
arrayType
- Array type of the arrayarray
- Array to checkmigrateIfInvalid
- if true, and array is in the wrong WS: migrate the array and return. If false and in the wrong WS: exceptionexceptionIfDetached
- If true: if the workspace is detached, but is expected to be in a workspace: should an exception be thrown?- Returns:
- The original array, or (if required, and if migrateIfInvalid == true) the migrated array
-
create
public INDArray create(@NonNull T arrayType, @NonNull @NonNull DataType dataType, @NonNull @lombok.NonNull long... shape)
Description copied from interface:WorkspaceMgr
Create an array in the specified array type's workspace (or detached if none is specified). Equivalent toNd4j.create(int...)
, other than the array location- Specified by:
create
in interfaceWorkspaceMgr<T extends Enum<T>>
- Parameters:
arrayType
- Array typedataType
- Data type for the created arrayshape
- Shape- Returns:
- Created arary
-
create
public INDArray create(@NonNull T arrayType, @NonNull @NonNull DataType dataType, @NonNull @lombok.NonNull long[] shape, @NonNull @lombok.NonNull char order)
Description copied from interface:WorkspaceMgr
Create an array in the specified array type's workspace (or detached if none is specified). Equivalent toNd4j.create(int[],char)
, other than the array location- Specified by:
create
in interfaceWorkspaceMgr<T extends Enum<T>>
- Parameters:
arrayType
- Array typedataType
- Data type for the created arrayshape
- Shapeorder
- Order of the array- Returns:
- Created arary
-
createUninitialized
public INDArray createUninitialized(T arrayType, DataType dataType, long... shape)
Description copied from interface:WorkspaceMgr
Create an uninitialized array in the specified array type's workspace (or detached if none is specified). Equivalent toorg.nd4j.linalg.factory.Nd4j#createUninitialized(int)
(int...)}, other than the array location- Specified by:
createUninitialized
in interfaceWorkspaceMgr<T extends Enum<T>>
- Parameters:
arrayType
- Array typedataType
- Data type of the created arrayshape
- Shape- Returns:
- Created array
-
createUninitialized
public INDArray createUninitialized(@NonNull T arrayType, @NonNull @NonNull DataType dataType, @NonNull @lombok.NonNull long[] shape, char order)
Description copied from interface:WorkspaceMgr
Create an uninitialized array in the specified array type's workspace (or detached if none is specified). Equivalent toNd4j.createUninitialized(int[], char)
}, other than the array location- Specified by:
createUninitialized
in interfaceWorkspaceMgr<T extends Enum<T>>
- Parameters:
arrayType
- Array typedataType
- Data type of the returned arrayshape
- Shapeorder
- Order of the array- Returns:
- Created array
-
dup
public INDArray dup(@NonNull T arrayType, @NonNull @NonNull INDArray toDup, char order)
Description copied from interface:WorkspaceMgr
Duplicate the array, where the array is put into the specified array type's workspace (if applicable)- Specified by:
dup
in interfaceWorkspaceMgr<T extends Enum<T>>
- Parameters:
arrayType
- Array type for the resulttoDup
- Array to duplicateorder
- Order for the duplicated array- Returns:
- Duplicated array in the specified array type's workspace
-
dup
public INDArray dup(@NonNull T arrayType, @NonNull @NonNull INDArray toDup)
Description copied from interface:WorkspaceMgr
Duplicate the array, where the array is put into the specified array type's workspace (if applicable)- Specified by:
dup
in interfaceWorkspaceMgr<T extends Enum<T>>
- Parameters:
arrayType
- Array type for the resulttoDup
- Array to duplicate- Returns:
- Duplicated array in the specified array type's workspace
-
castTo
public INDArray castTo(@NonNull T arrayType, @NonNull @NonNull DataType dataType, @NonNull @NonNull INDArray toCast, boolean dupIfCorrectType)
Description copied from interface:WorkspaceMgr
Cast the specified array to the specified datatype.
If the array is already the correct type, the bahaviour depends on the 'dupIfCorrectType' argument.
dupIfCorrectType = false && toCast.dataType() == dataType: return input array as-is (unless workspace is wrong)
dupIfCorrectType = true && toCast.dataType() == dataType: duplicate the array into the specified workspace- Specified by:
castTo
in interfaceWorkspaceMgr<T extends Enum<T>>
- Parameters:
arrayType
- Array typedataType
- Data typetoCast
- Array to castdupIfCorrectType
- False: return array as-is if correct type and in the correct workspace. True: dup if already correct type- Returns:
- Cast (or duplicated) array
-
-