/** * Assert that the specified workspace is open and active * * @param ws Name of the workspace to assert open and active * @param errorMsg Message to include in the exception, if required */ public static void assertOpenAndActive(@NonNull String ws, @NonNull String errorMsg) throws ND4JWorkspaceException { if (!Nd4j.getWorkspaceManager().checkIfWorkspaceExistsAndActive(ws)) { throw new ND4JWorkspaceException(errorMsg); } }
private void enforceExistsAndActive(@NonNull T arrayType){ validateConfig(arrayType); if(scopeOutOfWs.contains(arrayType)){ return; } if(!Nd4j.getWorkspaceManager().checkIfWorkspaceExistsAndActive(workspaceNames.get(arrayType))){ throw new ND4JWorkspaceException("Workspace \"" + workspaceNames.get(arrayType) + "\" for array type " + arrayType + " is not open"); } } }
@Override public boolean isWorkspaceOpen(@NonNull T arrayType) { validateConfig(arrayType); if(!scopeOutOfWs.contains(arrayType)) { return Nd4j.getWorkspaceManager().checkIfWorkspaceExistsAndActive(getWorkspaceName(arrayType)); } return true; }
/** * Assert that the specified workspace is open, active, and is the current workspace * * @param ws Name of the workspace to assert open/active/current * @param errorMsg Message to include in the exception, if required */ public static void assertOpenActiveAndCurrent(@NonNull String ws, @NonNull String errorMsg) throws ND4JWorkspaceException { if (!Nd4j.getWorkspaceManager().checkIfWorkspaceExistsAndActive(ws)) { throw new ND4JWorkspaceException(errorMsg + " - workspace is not open and active"); } MemoryWorkspace currWs = Nd4j.getMemoryManager().getCurrentWorkspace(); if (currWs == null || !ws.equals(currWs.getId())) { throw new ND4JWorkspaceException(errorMsg + " - not the current workspace (current workspace: " + (currWs == null ? null : currWs.getId())); } }
/** * This method detaches INDArray from current Workspace, and attaches it to Workspace with a given Id, if a workspace * with the given ID is open and active. * * If the workspace does not exist, or is not active, the array is detached from any workspaces. * * @param id ID of the workspace to leverage to * @return The INDArray, leveraged to the specified workspace (if it exists and is active) otherwise the detached array * @see #leverageTo(String) */ public INDArray leverageOrDetach(String id){ if(!isAttached()){ return this; } if(!Nd4j.getWorkspaceManager().checkIfWorkspaceExistsAndActive(id)){ return detach(); } return leverageTo(id); }