Search in sources :

Example 36 with Nd4jWorkspace

use of org.nd4j.linalg.memory.abstracts.Nd4jWorkspace in project nd4j by deeplearning4j.

the class BasicWorkspaceTests method testAllocation3.

@Test
public void testAllocation3() throws Exception {
    Nd4jWorkspace workspace = (Nd4jWorkspace) Nd4j.getWorkspaceManager().getAndActivateWorkspace(basicConfig, "testAllocation2");
    Nd4j.getMemoryManager().setCurrentWorkspace(workspace);
    assertNotEquals(null, Nd4j.getMemoryManager().getCurrentWorkspace());
    assertEquals(0, workspace.getHostOffset());
    INDArray array = Nd4j.create(new int[] { 1, 5 }, 'c');
    // checking if allocation actually happened
    long reqMem = 5 * Nd4j.sizeOfDataType();
    assertEquals(reqMem + reqMem % 8, workspace.getHostOffset());
    array.assign(1.0f);
    assertEquals(5, array.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)

Example 37 with Nd4jWorkspace

use of org.nd4j.linalg.memory.abstracts.Nd4jWorkspace in project nd4j by deeplearning4j.

the class BasicWorkspaceTests method testScope2.

@Test
public void testScope2() throws Exception {
    INDArray array = null;
    try (Nd4jWorkspace wsI = (Nd4jWorkspace) Nd4j.getWorkspaceManager().getAndActivateWorkspace(loopFirstConfig, "ITER")) {
        array = Nd4j.create(100);
        // despite we're allocating this array in workspace, it's empty yet, so it's external allocation
        assertTrue(array.isInScope());
        assertEquals(0, wsI.getCurrentSize());
    }
    try (Nd4jWorkspace wsI = (Nd4jWorkspace) Nd4j.getWorkspaceManager().getAndActivateWorkspace(loopFirstConfig, "ITER")) {
        array = Nd4j.create(100);
        assertTrue(array.isInScope());
        assertEquals(100 * Nd4j.sizeOfDataType(), wsI.getHostOffset());
    }
    assertFalse(array.isInScope());
}
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 38 with Nd4jWorkspace

use of org.nd4j.linalg.memory.abstracts.Nd4jWorkspace in project nd4j by deeplearning4j.

the class BasicWorkspaceTests method testCreateDetached1.

@Test
public void testCreateDetached1() throws Exception {
    try (Nd4jWorkspace wsI = (Nd4jWorkspace) Nd4j.getWorkspaceManager().getAndActivateWorkspace(basicConfig, "ITER")) {
        INDArray array1 = Nd4j.create(new float[] { 1f, 2f, 3f, 4f, 5f });
        INDArray array2 = Nd4j.createUninitializedDetached(5);
        array2.assign(array1);
        long reqMemory = 5 * Nd4j.sizeOfDataType();
        assertEquals(reqMemory + reqMemory % 8, wsI.getHostOffset());
        assertEquals(array1, array2);
    }
}
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 39 with Nd4jWorkspace

use of org.nd4j.linalg.memory.abstracts.Nd4jWorkspace in project nd4j by deeplearning4j.

the class SpecialWorkspaceTests method testViewDetach_1.

@Test
public void testViewDetach_1() throws Exception {
    WorkspaceConfiguration configuration = WorkspaceConfiguration.builder().initialSize(10000000).overallocationLimit(3.0).policyAllocation(AllocationPolicy.OVERALLOCATE).policySpill(SpillPolicy.REALLOCATE).policyLearning(LearningPolicy.FIRST_LOOP).policyReset(ResetPolicy.BLOCK_LEFT).build();
    Nd4jWorkspace workspace = (Nd4jWorkspace) Nd4j.getWorkspaceManager().getWorkspaceForCurrentThread(configuration, "WS109");
    INDArray row = Nd4j.linspace(1, 10, 10);
    INDArray exp = Nd4j.create(1, 10).assign(2.0);
    INDArray result = null;
    try (MemoryWorkspace ws = Nd4j.getWorkspaceManager().getAndActivateWorkspace(configuration, "WS109")) {
        INDArray matrix = Nd4j.create(10, 10);
        for (int e = 0; e < matrix.rows(); e++) matrix.getRow(e).assign(row);
        INDArray column = matrix.getColumn(1);
        assertTrue(column.isView());
        assertTrue(column.isAttached());
        result = column.detach();
    }
    assertFalse(result.isView());
    assertFalse(result.isAttached());
    assertEquals(exp, result);
}
Also used : INDArray(org.nd4j.linalg.api.ndarray.INDArray) WorkspaceConfiguration(org.nd4j.linalg.api.memory.conf.WorkspaceConfiguration) MemoryWorkspace(org.nd4j.linalg.api.memory.MemoryWorkspace) Nd4jWorkspace(org.nd4j.linalg.memory.abstracts.Nd4jWorkspace) Test(org.junit.Test) BaseNd4jTest(org.nd4j.linalg.BaseNd4jTest)

Example 40 with Nd4jWorkspace

use of org.nd4j.linalg.memory.abstracts.Nd4jWorkspace in project nd4j by deeplearning4j.

the class SpecialWorkspaceTests method testVariableTimeSeries2.

@Test
public void testVariableTimeSeries2() throws Exception {
    WorkspaceConfiguration configuration = WorkspaceConfiguration.builder().initialSize(0).overallocationLimit(3.0).policyAllocation(AllocationPolicy.OVERALLOCATE).policySpill(SpillPolicy.REALLOCATE).policyLearning(LearningPolicy.FIRST_LOOP).policyReset(ResetPolicy.ENDOFBUFFER_REACHED).build();
    Nd4jWorkspace workspace = (Nd4jWorkspace) Nd4j.getWorkspaceManager().getWorkspaceForCurrentThread(configuration, "WS1");
    workspace.enableDebug(true);
    try (MemoryWorkspace ws = Nd4j.getWorkspaceManager().getAndActivateWorkspace(configuration, "WS1")) {
        Nd4j.create(500);
        Nd4j.create(500);
    }
    assertEquals(0, workspace.getStepNumber());
    long requiredMemory = 1000 * Nd4j.sizeOfDataType();
    long shiftedSize = ((long) (requiredMemory * 1.3)) + (8 - (((long) (requiredMemory * 1.3)) % 8));
    assertEquals(requiredMemory, workspace.getSpilledSize());
    assertEquals(shiftedSize, workspace.getInitialBlockSize());
    assertEquals(workspace.getInitialBlockSize() * 4, workspace.getCurrentSize());
    for (int i = 0; i < 100; i++) {
        try (MemoryWorkspace ws = Nd4j.getWorkspaceManager().getAndActivateWorkspace(configuration, "WS1")) {
            Nd4j.create(500);
            Nd4j.create(500);
            Nd4j.create(500);
        }
    }
    assertEquals(workspace.getInitialBlockSize() * 4, workspace.getCurrentSize());
    assertEquals(0, workspace.getNumberOfPinnedAllocations());
    assertEquals(0, workspace.getNumberOfExternalAllocations());
    assertEquals(0, workspace.getSpilledSize());
    assertEquals(0, workspace.getPinnedSize());
}
Also used : WorkspaceConfiguration(org.nd4j.linalg.api.memory.conf.WorkspaceConfiguration) 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