public GarbageWorkspaceReference(MemoryWorkspace referent, ReferenceQueue<? super MemoryWorkspace> queue) { super(referent, queue); this.pointersPair = ((Nd4jWorkspace) referent).workspace; this.id = referent.getId(); this.threadId = referent.getThreadId(); this.pinnedPointers = ((Nd4jWorkspace) referent).pinnedAllocations; this.externalPointers = ((Nd4jWorkspace) referent).externalAllocations; this.key = id + "_" + threadId; } }
@Override public void close() { for( int i=workspaces.length-1; i>=0; i-- ){ workspaces[i].close(); } } }
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 void initWorkspace() { workspace = Nd4j.getWorkspaceManager().createNewWorkspace( WorkspaceConfiguration.builder() .initialSize(memoryForGraph()) .policyAllocation(AllocationPolicy.OVERALLOCATE) .policyLearning(LearningPolicy.FIRST_LOOP) .build()); Nd4j.getWorkspaceManager().setWorkspaceForCurrentThread(workspace); }
@Override public MemoryWorkspace notifyScopeEntered(@NonNull T arrayType) { validateConfig(arrayType); if(isScopedOut(arrayType)){ return Nd4j.getWorkspaceManager().scopeOutOfWorkspaces(); } else { MemoryWorkspace ws = Nd4j.getWorkspaceManager().getWorkspaceForCurrentThread( getConfiguration(arrayType), getWorkspaceName(arrayType)); return ws.notifyScopeEntered(); } }
@Override public MemoryWorkspace notifyScopeBorrowed(@NonNull T arrayType) { validateConfig(arrayType); enforceExistsAndActive(arrayType); if(scopeOutOfWs.contains(arrayType)){ return Nd4j.getWorkspaceManager().scopeOutOfWorkspaces(); } else { MemoryWorkspace ws = Nd4j.getWorkspaceManager().getWorkspaceForCurrentThread( getConfiguration(arrayType), getWorkspaceName(arrayType)); return ws.notifyScopeBorrowed(); } }
/** * 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); } }
/** * This method gets & activates default workspace * * @return */ @Override public MemoryWorkspace getAndActivateWorkspace() { return getWorkspaceForCurrentThread().notifyScopeEntered(); }
/** * 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 destroys given workspace * * @param workspace */ @Override public void destroyWorkspace(MemoryWorkspace workspace) { if (workspace == null || workspace instanceof DummyWorkspace) return; //workspace.destroyWorkspace(); backingMap.get().remove(workspace.getId()); }
/** * 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(); }
/** * 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); } }
/** * This method temporary opens block out of any workspace scope. * <p> * PLEASE NOTE: Do not forget to close this block. * * @return */ @Override public MemoryWorkspace scopeOutOfWorkspaces() { MemoryWorkspace workspace = Nd4j.getMemoryManager().getCurrentWorkspace(); if (workspace == null) return new DummyWorkspace(); else { Nd4j.getMemoryManager().setCurrentWorkspace(null); return workspace.tagOutOfScopeUse(); } }
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; } }
/** * This method gets & activates default with a given configuration and Id * * @param configuration * @param id * @return */ @Override public MemoryWorkspace getAndActivateWorkspace(@NonNull WorkspaceConfiguration configuration, @NonNull String id) { return getWorkspaceForCurrentThread(configuration, id).notifyScopeEntered(); }
@Override public boolean anyWorkspaceActiveForCurrentThread(){ ensureThreadExistense(); boolean anyActive = false; for(MemoryWorkspace ws : backingMap.get().values()){ if(ws.isScopeActive()){ anyActive = true; break; } } return anyActive; } }
@Override public boolean isWorkspaceOpen(@NonNull T arrayType) { validateConfig(arrayType); if(!scopeOutOfWs.contains(arrayType)) { return Nd4j.getWorkspaceManager().checkIfWorkspaceExistsAndActive(getWorkspaceName(arrayType)); } return true; }
/** * This method gets & activates workspace with a given Id * * @param id * @return */ @Override public MemoryWorkspace getAndActivateWorkspace(@NonNull String id) { return getWorkspaceForCurrentThread(id).notifyScopeEntered(); }
@Override public boolean checkIfWorkspaceExistsAndActive(@NonNull String id) { boolean exists = checkIfWorkspaceExists(id); if (!exists) return false; return backingMap.get().get(id).isScopeActive(); }
/** * 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); }