Search in sources :

Example 16 with Nd4jWorkspace

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());
}
Also used : WorkspaceConfiguration(org.nd4j.linalg.api.memory.conf.WorkspaceConfiguration) Nd4jWorkspace(org.nd4j.linalg.memory.abstracts.Nd4jWorkspace) Test(org.junit.Test) BaseNd4jTest(org.nd4j.linalg.BaseNd4jTest)

Example 17 with Nd4jWorkspace

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);
        }
    }
}
Also used : INDArray(org.nd4j.linalg.api.ndarray.INDArray) Nd4jWorkspace(org.nd4j.linalg.memory.abstracts.Nd4jWorkspace) Test(org.junit.Test) BaseNd4jTest(org.nd4j.linalg.BaseNd4jTest)

Example 18 with Nd4jWorkspace

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());
        }
    }
}
Also used : INDArray(org.nd4j.linalg.api.ndarray.INDArray) Nd4jWorkspace(org.nd4j.linalg.memory.abstracts.Nd4jWorkspace) Test(org.junit.Test) BaseNd4jTest(org.nd4j.linalg.BaseNd4jTest)

Example 19 with Nd4jWorkspace

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);
}
Also used : INDArray(org.nd4j.linalg.api.ndarray.INDArray) Nd4jWorkspace(org.nd4j.linalg.memory.abstracts.Nd4jWorkspace) Test(org.junit.Test) BaseNd4jTest(org.nd4j.linalg.BaseNd4jTest)

Example 20 with Nd4jWorkspace

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();
}
Also used : INDArray(org.nd4j.linalg.api.ndarray.INDArray) Nd4jWorkspace(org.nd4j.linalg.memory.abstracts.Nd4jWorkspace) Test(org.junit.Test) BaseNd4jTest(org.nd4j.linalg.BaseNd4jTest)

Aggregations

Test (org.junit.Test)51 BaseNd4jTest (org.nd4j.linalg.BaseNd4jTest)51 Nd4jWorkspace (org.nd4j.linalg.memory.abstracts.Nd4jWorkspace)51 INDArray (org.nd4j.linalg.api.ndarray.INDArray)47 MemoryWorkspace (org.nd4j.linalg.api.memory.MemoryWorkspace)18 WorkspaceConfiguration (org.nd4j.linalg.api.memory.conf.WorkspaceConfiguration)11 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 DataInputStream (java.io.DataInputStream)2 DataOutputStream (java.io.DataOutputStream)2 Ignore (org.junit.Ignore)1 ND4JIllegalStateException (org.nd4j.linalg.exception.ND4JIllegalStateException)1