use of org.nd4j.linalg.memory.abstracts.Nd4jWorkspace in project nd4j by deeplearning4j.
the class WorkspaceProviderTests method testNestedWorkspaces5.
@Test
public void testNestedWorkspaces5() throws Exception {
Nd4j.getWorkspaceManager().setDefaultWorkspaceConfiguration(basicConfiguration);
try (Nd4jWorkspace ws1 = (Nd4jWorkspace) Nd4j.getWorkspaceManager().getWorkspaceForCurrentThread("WS1").notifyScopeEntered()) {
INDArray array1 = Nd4j.create(100);
try (Nd4jWorkspace ws2 = (Nd4jWorkspace) Nd4j.getWorkspaceManager().getWorkspaceForCurrentThread("WS1").notifyScopeEntered()) {
INDArray array2 = Nd4j.create(100);
}
long reqMem = 200 * Nd4j.sizeOfDataType();
assertEquals(reqMem + reqMem % 8, ws1.getHostOffset());
INDArray array3 = Nd4j.create(100);
reqMem = 300 * Nd4j.sizeOfDataType();
assertEquals(reqMem + reqMem % 8, ws1.getHostOffset());
}
assertNull(Nd4j.getMemoryManager().getCurrentWorkspace());
}
use of org.nd4j.linalg.memory.abstracts.Nd4jWorkspace in project nd4j by deeplearning4j.
the class WorkspaceProviderTests method testNestedWorkspaces3.
@Test
public void testNestedWorkspaces3() throws Exception {
Nd4j.getWorkspaceManager().setDefaultWorkspaceConfiguration(basicConfiguration);
// We open top-level workspace
try (Nd4jWorkspace ws1 = (Nd4jWorkspace) Nd4j.getWorkspaceManager().getWorkspaceForCurrentThread("WS1").notifyScopeEntered()) {
INDArray array1 = Nd4j.create(100);
assertEquals(100 * Nd4j.sizeOfDataType(), ws1.getHostOffset());
// we open first nested workspace
try (Nd4jWorkspace ws2 = (Nd4jWorkspace) Nd4j.getWorkspaceManager().getWorkspaceForCurrentThread("WS2").notifyScopeEntered()) {
assertEquals(0 * Nd4j.sizeOfDataType(), ws2.getHostOffset());
INDArray array2 = Nd4j.create(100);
assertEquals(100 * Nd4j.sizeOfDataType(), ws1.getHostOffset());
assertEquals(100 * Nd4j.sizeOfDataType(), ws2.getHostOffset());
}
// and second nexted workspace
try (Nd4jWorkspace ws3 = (Nd4jWorkspace) Nd4j.getWorkspaceManager().getWorkspaceForCurrentThread("WS3").notifyScopeEntered()) {
assertEquals(0 * Nd4j.sizeOfDataType(), ws3.getHostOffset());
INDArray array2 = Nd4j.create(100);
assertEquals(100 * Nd4j.sizeOfDataType(), ws1.getHostOffset());
assertEquals(100 * Nd4j.sizeOfDataType(), ws3.getHostOffset());
}
// this allocation should happen within top-level workspace
INDArray array1b = Nd4j.create(100);
assertEquals(200 * Nd4j.sizeOfDataType(), ws1.getHostOffset());
}
assertEquals(null, Nd4j.getMemoryManager().getCurrentWorkspace());
assertNull(Nd4j.getMemoryManager().getCurrentWorkspace());
}
use of org.nd4j.linalg.memory.abstracts.Nd4jWorkspace in project nd4j by deeplearning4j.
the class WorkspaceProviderTests method testWorkspacesSerde2.
@Test
public void testWorkspacesSerde2() throws Exception {
INDArray array = Nd4j.create(10).assign(1.0);
INDArray restored = null;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(bos);
Nd4j.write(array, dos);
try (Nd4jWorkspace workspace = (Nd4jWorkspace) Nd4j.getWorkspaceManager().getAndActivateWorkspace(basicConfiguration, "WS_1")) {
workspace.enableDebug(true);
ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
DataInputStream dis = new DataInputStream(bis);
restored = Nd4j.read(dis);
long requiredMemory = 10 * Nd4j.sizeOfDataType();
assertEquals(requiredMemory + requiredMemory % 8, workspace.getHostOffset());
assertEquals(array.length(), restored.length());
assertEquals(1.0f, restored.meanNumber().floatValue(), 1.0f);
// we want to ensure it's the same cached shapeInfo used here
assertTrue(array.shapeInfoDataBuffer() == restored.shapeInfoDataBuffer());
}
}
use of org.nd4j.linalg.memory.abstracts.Nd4jWorkspace in project nd4j by deeplearning4j.
the class WorkspaceProviderTests method testReallocate1.
@Test
public void testReallocate1() throws Exception {
try (MemoryWorkspace ws = Nd4j.getWorkspaceManager().getAndActivateWorkspace(reallocateConfiguration, "WS_1")) {
INDArray array = Nd4j.create(100);
}
Nd4jWorkspace workspace = (Nd4jWorkspace) Nd4j.getWorkspaceManager().getWorkspaceForCurrentThread(reallocateConfiguration, "WS_1");
workspace.initializeWorkspace();
assertEquals(100 * Nd4j.sizeOfDataType(), workspace.getCurrentSize());
try (MemoryWorkspace ws = Nd4j.getWorkspaceManager().getAndActivateWorkspace(reallocateConfiguration, "WS_1")) {
INDArray array = Nd4j.create(1000);
}
assertEquals(1000 * Nd4j.sizeOfDataType(), workspace.getMaxCycleAllocations());
workspace.initializeWorkspace();
assertEquals(1000 * Nd4j.sizeOfDataType(), workspace.getCurrentSize());
// now we're working on reallocated array, that should be able to hold >100 elements
try (MemoryWorkspace ws = Nd4j.getWorkspaceManager().getAndActivateWorkspace(reallocateConfiguration, "WS_1")) {
INDArray array = Nd4j.create(500).assign(1.0);
assertEquals(1.0, array.meanNumber().doubleValue(), 0.01);
}
}
use of org.nd4j.linalg.memory.abstracts.Nd4jWorkspace in project nd4j by deeplearning4j.
the class WorkspaceProviderTests method testCircularBufferReset1.
@Test
public void testCircularBufferReset1() throws Exception {
Nd4jWorkspace workspace = (Nd4jWorkspace) Nd4j.getWorkspaceManager().getWorkspaceForCurrentThread(circularConfiguration, "WSR_1");
try (MemoryWorkspace ws = Nd4j.getWorkspaceManager().getAndActivateWorkspace("WSR_1")) {
Nd4j.create(10000);
assertEquals(0, workspace.getCurrentSize());
assertEquals(1, workspace.getNumberOfExternalAllocations());
}
assertEquals(10 * 1024L * 1024L, workspace.getCurrentSize());
assertEquals(0, workspace.getHostOffset());
assertEquals(1, workspace.getNumberOfExternalAllocations());
for (int i = 0; i < 11 * 1024 * 1024; i += 10000 * Nd4j.sizeOfDataType()) {
try (MemoryWorkspace ws = Nd4j.getWorkspaceManager().getAndActivateWorkspace("WSR_1")) {
Nd4j.create(10000);
}
/*
if (i < 10480000)
assertEquals("I: " + i,1, workspace.getNumberOfExternalAllocations());
else
assertEquals(0, workspace.getNumberOfExternalAllocations());
*/
}
assertEquals(0, workspace.getNumberOfExternalAllocations());
}
Aggregations