use of org.nd4j.linalg.memory.abstracts.Nd4jWorkspace in project nd4j by deeplearning4j.
the class BasicWorkspaceTests method testOverallocation1.
@Test
public void testOverallocation1() throws Exception {
WorkspaceConfiguration overallocationConfig = WorkspaceConfiguration.builder().initialSize(1024).maxSize(10 * 1024 * 1024).overallocationLimit(1.0).policyAllocation(AllocationPolicy.OVERALLOCATE).policyLearning(LearningPolicy.NONE).policyMirroring(MirroringPolicy.FULL).policySpill(SpillPolicy.EXTERNAL).build();
Nd4jWorkspace workspace = (Nd4jWorkspace) Nd4j.getWorkspaceManager().createNewWorkspace(overallocationConfig);
assertEquals(2048, workspace.getCurrentSize());
}
use of org.nd4j.linalg.memory.abstracts.Nd4jWorkspace in project nd4j by deeplearning4j.
the class BasicWorkspaceTests method testLeverage1.
@Test
public void testLeverage1() throws Exception {
try (Nd4jWorkspace wsOne = (Nd4jWorkspace) Nd4j.getWorkspaceManager().getAndActivateWorkspace(basicConfig, "EXT")) {
assertEquals(0, wsOne.getHostOffset());
try (Nd4jWorkspace wsTwo = (Nd4jWorkspace) Nd4j.getWorkspaceManager().getAndActivateWorkspace(basicConfig, "INT")) {
INDArray array = Nd4j.create(new float[] { 1f, 2f, 3f, 4f, 5f });
assertEquals(0, wsOne.getHostOffset());
long reqMemory = 5 * Nd4j.sizeOfDataType();
assertEquals(reqMemory + reqMemory % 8, wsTwo.getHostOffset());
INDArray copy = array.leverage();
assertEquals(reqMemory + reqMemory % 8, wsTwo.getHostOffset());
assertEquals(reqMemory + reqMemory % 8, wsOne.getHostOffset());
assertNotEquals(null, copy);
assertTrue(copy.isAttached());
assertEquals(15.0f, copy.sumNumber().floatValue(), 0.01f);
}
}
}
use of org.nd4j.linalg.memory.abstracts.Nd4jWorkspace in project nd4j by deeplearning4j.
the class BasicWorkspaceTests method testLeverageTo1.
@Test
public void testLeverageTo1() throws Exception {
try (Nd4jWorkspace wsOne = (Nd4jWorkspace) Nd4j.getWorkspaceManager().getAndActivateWorkspace(basicConfig, "EXT")) {
INDArray array1 = Nd4j.create(5);
try (Nd4jWorkspace wsTwo = (Nd4jWorkspace) Nd4j.getWorkspaceManager().getAndActivateWorkspace(basicConfig, "INT")) {
INDArray array2 = Nd4j.create(new float[] { 1f, 2f, 3f, 4f, 5f });
long reqMemory = 5 * Nd4j.sizeOfDataType();
assertEquals(reqMemory + reqMemory % 8, wsOne.getHostOffset());
array2.leverageTo("EXT");
assertEquals((reqMemory + reqMemory % 8) * 2, wsOne.getHostOffset());
}
}
}
use of org.nd4j.linalg.memory.abstracts.Nd4jWorkspace in project nd4j by deeplearning4j.
the class BasicWorkspaceTests method testDetach1.
@Test
public void testDetach1() throws Exception {
INDArray array = null;
INDArray copy = null;
try (Nd4jWorkspace wsI = (Nd4jWorkspace) Nd4j.getWorkspaceManager().getAndActivateWorkspace(basicConfig, "ITER")) {
array = Nd4j.create(new float[] { 1f, 2f, 3f, 4f, 5f });
// despite we're allocating this array in workspace, it's empty yet, so it's external allocation
assertTrue(array.isInScope());
assertTrue(array.isAttached());
long reqMemory = 5 * Nd4j.sizeOfDataType();
assertEquals(reqMemory + reqMemory % 8, wsI.getHostOffset());
copy = array.detach();
assertTrue(array.isInScope());
assertTrue(array.isAttached());
assertEquals(reqMemory + reqMemory % 8, wsI.getHostOffset());
assertFalse(copy.isAttached());
assertTrue(copy.isInScope());
assertEquals(reqMemory + reqMemory % 8, wsI.getHostOffset());
}
assertEquals(15.0f, copy.sumNumber().floatValue(), 0.01f);
assertFalse(array == copy);
}
use of org.nd4j.linalg.memory.abstracts.Nd4jWorkspace in project nd4j by deeplearning4j.
the class BasicWorkspaceTests method testAllocation6.
@Test
public void testAllocation6() throws Exception {
Nd4jWorkspace workspace = (Nd4jWorkspace) Nd4j.getWorkspaceManager().getAndActivateWorkspace(basicConfig, "testAllocation6");
Nd4j.getMemoryManager().setCurrentWorkspace(workspace);
assertNotEquals(null, Nd4j.getMemoryManager().getCurrentWorkspace());
assertEquals(0, workspace.getHostOffset());
INDArray array = Nd4j.rand(100, 10, 10);
// checking if allocation actually happened
assertEquals(1000 * Nd4j.sizeOfDataType(), workspace.getHostOffset());
INDArray dup = array.dup();
assertEquals(2000 * Nd4j.sizeOfDataType(), workspace.getHostOffset());
// assertEquals(5, dup.sumNumber().doubleValue(), 0.01);
workspace.close();
}
Aggregations