@Override public boolean checkIfWorkspaceExistsAndActive(@NonNull String id) { boolean exists = checkIfWorkspaceExists(id); if (!exists) return false; return backingMap.get().get(id).isScopeActive(); }
@Override public boolean anyWorkspaceActiveForCurrentThread(){ ensureThreadExistense(); boolean anyActive = false; for(MemoryWorkspace ws : backingMap.get().values()){ if(ws.isScopeActive()){ anyActive = true; break; } } return anyActive; } }
/** * This method checks, if given attached INDArray is still in scope of its parent Workspace * <p> * PLEASE NOTE: if this INDArray isn't attached to any Workspace, this method will return true * * @return */ @Override public boolean isInScope() { if (!isAttached()) return true; return parentWorkspace.isScopeActive(); }
private static List<String> allOpenWorkspaces(){ List<MemoryWorkspace> l = Nd4j.getWorkspaceManager().getAllWorkspacesForCurrentThread(); List<String> workspaces = new ArrayList<>(l.size()); for( MemoryWorkspace ws : l){ if(ws.isScopeActive()) { workspaces.add(ws.getId()); } } return workspaces; } }
private static List<String> allOpenWorkspaces(){ List<MemoryWorkspace> l = Nd4j.getWorkspaceManager().getAllWorkspacesForCurrentThread(); List<String> workspaces = new ArrayList<>(l.size()); for( MemoryWorkspace ws : l){ if(ws.isScopeActive()) { workspaces.add(ws.getId()); } } return workspaces; }
/** * Assert that no workspaces are currently open * * @param msg Message to include in the exception, if required */ public static void assertNoWorkspacesOpen(String msg) throws ND4JWorkspaceException { if (Nd4j.getWorkspaceManager().anyWorkspaceActiveForCurrentThread()) { List<MemoryWorkspace> l = Nd4j.getWorkspaceManager().getAllWorkspacesForCurrentThread(); List<String> workspaces = new ArrayList<>(l.size()); for (MemoryWorkspace ws : l) { if(ws.isScopeActive()) { workspaces.add(ws.getId()); } } throw new ND4JWorkspaceException(msg + " - Open/active workspaces: " + workspaces); } }
protected void checkWorkspace(String opName, INDArray array) { if (array.isAttached()) { val ws = array.data().getParentWorkspace(); if (ws.getWorkspaceType() != MemoryWorkspace.Type.CIRCULAR) { if (!ws.isScopeActive()) { throw new ND4JIllegalStateException("Op [" + opName + "] X argument uses leaked workspace pointer from workspace [" + ws.getId() + "]\nAll open workspaces: " + allOpenWorkspaces() + "\n" + SCOPE_PANIC_MSG); } if (ws.getGenerationId() != array.data().getGenerationId()) throw new ND4JIllegalStateException("Op [" + opName + "] X argument uses outdated workspace pointer from workspace [" + ws.getId() + "]\nAll open workspaces: " + allOpenWorkspaces() + "\n" + SCOPE_PANIC_MSG); } } }
/** * Assert that the specified array is valid, in terms of workspaces: i.e., if it is attached (and not in a circular * workspace), assert that the workspace is open, and that the data is not from an old generation. * @param array Array to check * @param msg Message (prefix) to include in the exception, if required. May be null */ public static void assertValidArray(INDArray array, String msg){ if(array == null || !array.isAttached()){ return; } val ws = array.data().getParentWorkspace(); if (ws.getWorkspaceType() != MemoryWorkspace.Type.CIRCULAR) { if (!ws.isScopeActive()) { throw new ND4JWorkspaceException( (msg == null ? "" : msg + ": ") + "Array uses leaked workspace pointer " + "from workspace " + ws.getId() + "\nAll open workspaces: " + allOpenWorkspaces()); } if (ws.getGenerationId() != array.data().getGenerationId()) { throw new ND4JWorkspaceException( (msg == null ? "" : msg + ": ") + "Array outdated workspace pointer " + "from workspace " + ws.getId() + " (array generation " + array.data().getGenerationId() + ", current workspace generation " + ws.getGenerationId() + ")\nAll open workspaces: " + allOpenWorkspaces()); } } }