use of org.nd4j.linalg.memory.abstracts.Nd4jWorkspace in project nd4j by deeplearning4j.
the class WorkspaceProviderTests method testNestedWorkspaces1.
@Test
public void testNestedWorkspaces1() throws Exception {
Nd4j.getWorkspaceManager().setDefaultWorkspaceConfiguration(basicConfiguration);
try (Nd4jWorkspace ws1 = (Nd4jWorkspace) Nd4j.getWorkspaceManager().getWorkspaceForCurrentThread("WS1").notifyScopeEntered()) {
INDArray array1 = Nd4j.create(100);
assertEquals(100 * Nd4j.sizeOfDataType(), ws1.getHostOffset());
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());
}
}
assertNull(Nd4j.getMemoryManager().getCurrentWorkspace());
log.info("---------------");
Nd4j.getWorkspaceManager().destroyAllWorkspacesForCurrentThread();
}
use of org.nd4j.linalg.memory.abstracts.Nd4jWorkspace in project nd4j by deeplearning4j.
the class WorkspaceProviderTests method testNestedWorkspaces4.
@Test
public void testNestedWorkspaces4() 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("WS2").notifyScopeEntered()) {
INDArray array2 = Nd4j.create(100);
try (Nd4jWorkspace ws3 = (Nd4jWorkspace) Nd4j.getWorkspaceManager().getWorkspaceForCurrentThread("WS3").notifyScopeEntered()) {
INDArray array3 = Nd4j.create(100);
assertEquals(100 * Nd4j.sizeOfDataType(), ws1.getHostOffset());
assertEquals(100 * Nd4j.sizeOfDataType(), ws2.getHostOffset());
assertEquals(100 * Nd4j.sizeOfDataType(), ws3.getHostOffset());
}
INDArray array2b = Nd4j.create(100);
assertEquals(200 * Nd4j.sizeOfDataType(), ws2.getHostOffset());
}
INDArray array1b = Nd4j.create(100);
assertEquals(200 * Nd4j.sizeOfDataType(), ws1.getHostOffset());
}
Nd4jWorkspace ws1 = (Nd4jWorkspace) Nd4j.getWorkspaceManager().getWorkspaceForCurrentThread("WS1");
Nd4jWorkspace ws2 = (Nd4jWorkspace) Nd4j.getWorkspaceManager().getWorkspaceForCurrentThread("WS2");
Nd4jWorkspace ws3 = (Nd4jWorkspace) Nd4j.getWorkspaceManager().getWorkspaceForCurrentThread("WS3");
assertEquals(0 * Nd4j.sizeOfDataType(), ws1.getHostOffset());
assertEquals(0 * Nd4j.sizeOfDataType(), ws2.getHostOffset());
assertEquals(0 * Nd4j.sizeOfDataType(), ws3.getHostOffset());
assertNull(Nd4j.getMemoryManager().getCurrentWorkspace());
}
use of org.nd4j.linalg.memory.abstracts.Nd4jWorkspace in project nd4j by deeplearning4j.
the class WorkspaceProviderTests method testWorkspacesSerde3.
@Test
public void testWorkspacesSerde3() 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")) {
try (MemoryWorkspace wsO = Nd4j.getMemoryManager().scopeOutOfWorkspaces()) {
workspace.enableDebug(true);
ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
DataInputStream dis = new DataInputStream(bis);
restored = Nd4j.read(dis);
assertEquals(0, 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 testUnboundedLoop1.
@Test
public void testUnboundedLoop1() throws Exception {
WorkspaceConfiguration configuration = WorkspaceConfiguration.builder().initialSize(100 * 100 * Nd4j.sizeOfDataType()).policyReset(ResetPolicy.ENDOFBUFFER_REACHED).policyAllocation(AllocationPolicy.STRICT).build();
for (int x = 0; x < 100; x++) {
try (Nd4jWorkspace ws1 = (Nd4jWorkspace) Nd4j.getWorkspaceManager().getWorkspaceForCurrentThread(configuration, "ITER").notifyScopeEntered()) {
INDArray array = Nd4j.create(100);
}
Nd4jWorkspace ws1 = (Nd4jWorkspace) Nd4j.getWorkspaceManager().getWorkspaceForCurrentThread(configuration, "ITER");
assertEquals((x + 1) * 100 * Nd4j.sizeOfDataType(), ws1.getHostOffset());
}
Nd4jWorkspace ws1 = (Nd4jWorkspace) Nd4j.getWorkspaceManager().getWorkspaceForCurrentThread(configuration, "ITER");
assertEquals(100 * 100 * Nd4j.sizeOfDataType(), ws1.getHostOffset());
// just to trigger reset
ws1.notifyScopeEntered();
// confirming reset
// assertEquals(0, ws1.getHostOffset());
ws1.notifyScopeLeft();
assertNull(Nd4j.getMemoryManager().getCurrentWorkspace());
}
use of org.nd4j.linalg.memory.abstracts.Nd4jWorkspace in project nd4j by deeplearning4j.
the class BasicWorkspaceTests method testToggle1.
@Test
public void testToggle1() throws Exception {
Nd4jWorkspace workspace = (Nd4jWorkspace) Nd4j.getWorkspaceManager().createNewWorkspace(loopFirstConfig);
Nd4j.getMemoryManager().setCurrentWorkspace(workspace);
assertNotEquals(null, Nd4j.getMemoryManager().getCurrentWorkspace());
assertEquals(0, workspace.getHostOffset());
try (MemoryWorkspace cW = workspace.notifyScopeEntered()) {
INDArray array1 = Nd4j.create(100);
cW.toggleWorkspaceUse(false);
INDArray arrayDetached = Nd4j.create(100);
arrayDetached.assign(1.0f);
double sum = arrayDetached.sumNumber().doubleValue();
assertEquals(100f, sum, 0.01);
cW.toggleWorkspaceUse(true);
INDArray array2 = Nd4j.create(100);
}
assertEquals(0, workspace.getHostOffset());
assertEquals(200 * Nd4j.sizeOfDataType(), workspace.getCurrentSize());
log.info("--------------------------");
try (MemoryWorkspace cW = workspace.notifyScopeEntered()) {
INDArray array1 = Nd4j.create(100);
cW.toggleWorkspaceUse(false);
INDArray arrayDetached = Nd4j.create(100);
arrayDetached.assign(1.0f);
double sum = arrayDetached.sumNumber().doubleValue();
assertEquals(100f, sum, 0.01);
cW.toggleWorkspaceUse(true);
assertEquals(100 * Nd4j.sizeOfDataType(), workspace.getHostOffset());
INDArray array2 = Nd4j.create(100);
assertEquals(200 * Nd4j.sizeOfDataType(), workspace.getHostOffset());
}
}
Aggregations