Search in sources :

Example 46 with Nd4jWorkspace

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

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

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

Example 49 with Nd4jWorkspace

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);
    }
}
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)

Example 50 with Nd4jWorkspace

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());
}
Also used : 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