Search in sources :

Example 6 with Nd4jWorkspace

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();
}
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 7 with Nd4jWorkspace

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());
}
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 8 with Nd4jWorkspace

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

Example 9 with Nd4jWorkspace

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());
}
Also used : INDArray(org.nd4j.linalg.api.ndarray.INDArray) 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 10 with Nd4jWorkspace

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());
    }
}
Also used : INDArray(org.nd4j.linalg.api.ndarray.INDArray) MemoryWorkspace(org.nd4j.linalg.api.memory.MemoryWorkspace) 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