return this; if (!Nd4j.getWorkspaceManager().checkIfWorkspaceExists(id)) { if(enforceExistence){ throw new Nd4jNoSuchWorkspaceException(id); MemoryWorkspace target = Nd4j.getWorkspaceManager().getWorkspaceForCurrentThread(id);
private void initWorkspace() { workspace = Nd4j.getWorkspaceManager().createNewWorkspace( WorkspaceConfiguration.builder() .initialSize(memoryForGraph()) .policyAllocation(AllocationPolicy.OVERALLOCATE) .policyLearning(LearningPolicy.FIRST_LOOP) .build()); Nd4j.getWorkspaceManager().setWorkspaceForCurrentThread(workspace); }
try(MemoryWorkspace ws = Nd4j.getWorkspaceManager().getAndActivateWorkspace(initialConfig, "SOME_ID")) { try (MemoryWorkspace ws = Nd4j.getWorkspaceManager().getAndActivateWorkspace(learningConfig, "OTHER_ID")) { INDArray array = Nd4j.create(100); try(MemoryWorkspace ws1 = Nd4j.getWorkspaceManager().getAndActivateWorkspace(initialConfig, "SOME_ID")) { INDArray array = Nd4j.create(10, 10).assign(1.0f); INDArray sumRes; try(MemoryWorkspace ws2 = Nd4j.getWorkspaceManager().getAndActivateWorkspace(initialConfig, "THIRD_ID")) { try(MemoryWorkspace ws1 = Nd4j.getWorkspaceManager().getAndActivateWorkspace(initialConfig, "SOME_ID")) { INDArray array1 = Nd4j.create(10, 10).assign(1.0f); INDArray array2; try(MemoryWorkspace ws = Nd4j.getWorkspaceManager().scopeOutOfWorkspaces()) { try (MemoryWorkspace ws1 = Nd4j.getWorkspaceManager().getAndActivateWorkspace(circularConfig, "CIRCULAR_ID")) { INDArray array = Nd4j.create(100);
/** * 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); } }
@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(); } }
try { if (useWorkspace) workspace = Nd4j.getWorkspaceManager().getWorkspaceForCurrentThread(configuration, workspaceId);
if (Nd4j.getWorkspaceManager().checkIfWorkspaceExists(ComputationGraph.workspaceFeedForward)) { try (MemoryWorkspace workspace = Nd4j.getWorkspaceManager() .getAndActivateWorkspace(ComputationGraph.workspaceFeedForward)) { if (isExternal) {
Nd4j.getWorkspaceManager().getAndActivateWorkspace(workspaceConfiguration, "VPTREE_WORSKPACE");
MemoryWorkspace ws = Nd4j.getWorkspaceManager().createNewWorkspace(configuration, "CGA-" + i, cDevice);
@Override public INDArray getActivation(INDArray in, boolean training) { if (training) { try(MemoryWorkspace ws = Nd4j.getWorkspaceManager().scopeOutOfWorkspaces()) { this.alpha = Nd4j.rand(in.shape(), l, u, Nd4j.getRandom()); } INDArray inTimesAlpha = in.mul(alpha); BooleanIndexing.replaceWhere(in, inTimesAlpha, Conditions.lessThan(0)); } else { this.alpha = null; double a = 0.5 * (l + u); return Nd4j.getExecutioner().execAndReturn(new RectifedLinear(in, a)); } return in; }
@Override protected void init() { super.init(); if (currentSize.get() > 0) { //log.info("Allocating {} bytes at DEVICE & HOST space...", currentSize.get()); isInit.set(true); long bytes = currentSize.get(); if (isDebug.get()) log.info("Allocating [{}] workspace on device_{}, {} bytes...", id, Nd4j.getAffinityManager().getDeviceForCurrentThread(), bytes); if (isDebug.get()) { Nd4j.getWorkspaceManager().printAllocationStatisticsForCurrentThread(); } Pointer ptr = memoryManager.allocate((bytes + SAFETY_OFFSET), MemoryKind.HOST, false); if (ptr == null) throw new ND4JIllegalStateException("Can't allocate memory for workspace"); workspace.setHostPointer(new PagedPointer(ptr)); if (workspaceConfiguration.getPolicyMirroring() != MirroringPolicy.HOST_ONLY) workspace.setDevicePointer(new PagedPointer(memoryManager.allocate((bytes + SAFETY_OFFSET), MemoryKind.DEVICE, false))); //log.info("Workspace [{}] initialized successfully", id); } }
} finally { log.debug("Terminating all workspaces for trainer_{}", threadId); Nd4j.getWorkspaceManager().destroyAllWorkspacesForCurrentThread(); isStopped.set(true);
if (Nd4j.getWorkspaceManager().anyWorkspaceActiveForCurrentThread()) { val ws = Nd4j.getMemoryManager().getCurrentWorkspace(); nativeOps.memcpyAsync(target.addressPointer(), dstPtr, target.capacity(), CudaConstants.cudaMemcpyHostToHost, stream); if (Nd4j.getWorkspaceManager().anyWorkspaceActiveForCurrentThread()) { if (Nd4j.getWorkspaceManager().anyWorkspaceActiveForCurrentThread()) {
try { if (useWorkspaces) { workspace = Nd4j.getWorkspaceManager().getWorkspaceForCurrentThread(configuration, workspaceId);
@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(); } }
.getAndActivateWorkspace(appliedConfiguration, "CGA_APPLY")) { INDArray compressed_copy = compressed.unsafeDuplication(true);
protected void initializeWorkspaces(long size) { WorkspaceConfiguration configuration = WorkspaceConfiguration.builder().initialSize(size) .overallocationLimit(bufferSize).policyReset(ResetPolicy.ENDOFBUFFER_REACHED) .policyAllocation(AllocationPolicy.OVERALLOCATE).policySpill(SpillPolicy.EXTERNAL) .policyLearning(LearningPolicy.NONE).build(); int numDevices = Nd4j.getAffinityManager().getNumberOfDevices(); int cDevice = Nd4j.getAffinityManager().getDeviceForCurrentThread(); for (int i = 0; i < numDevices; i++) { Nd4j.getAffinityManager().unsafeSetDevice(i); workspaces.add(Nd4j.getWorkspaceManager().createNewWorkspace(configuration, "IDSC-" + i, i)); } Nd4j.getAffinityManager().unsafeSetDevice(cDevice); numWorkspaces = numDevices; isInitialized = true; }
@Override protected void init() { if (workspaceConfiguration.getPolicyLocation() == LocationPolicy.MMAP) { throw new ND4JIllegalStateException("CUDA do not support MMAP workspaces yet"); } super.init(); if (currentSize.get() > 0) { //log.info("Allocating {} bytes at DEVICE & HOST space...", currentSize.get()); isInit.set(true); long bytes = currentSize.get(); if (isDebug.get()) log.info("Allocating [{}] workspace on device_{}, {} bytes...", id, Nd4j.getAffinityManager().getDeviceForCurrentThread(), bytes); if (isDebug.get()) { Nd4j.getWorkspaceManager().printAllocationStatisticsForCurrentThread(); } Pointer ptr = memoryManager.allocate((bytes + SAFETY_OFFSET), MemoryKind.HOST, false); if (ptr == null) throw new ND4JIllegalStateException("Can't allocate memory for workspace"); workspace.setHostPointer(new PagedPointer(ptr)); if (workspaceConfiguration.getPolicyMirroring() != MirroringPolicy.HOST_ONLY) workspace.setDevicePointer(new PagedPointer(memoryManager.allocate((bytes + SAFETY_OFFSET), MemoryKind.DEVICE, false))); //log.info("Workspace [{}] initialized successfully", id); } }
} finally { log.debug("Terminating all workspaces for trainer_{}", threadId); Nd4j.getWorkspaceManager().destroyAllWorkspacesForCurrentThread(); isStopped.set(true);
try { if (useWorkspace) workspace = Nd4j.getWorkspaceManager().getWorkspaceForCurrentThread(configuration, workspaceId);