Search in sources :

Example 41 with Nd4jWorkspace

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

the class WorkspaceProviderTests method testMemcpy1.

@Ignore
@Test
public void testMemcpy1() {
    INDArray warmUp = Nd4j.create(100000);
    for (int x = 0; x < 5000; x++) {
        warmUp.addi(0.1);
    }
    WorkspaceConfiguration configuration = WorkspaceConfiguration.builder().policyMirroring(MirroringPolicy.HOST_ONLY).initialSize(1024L * 1024L * 1024L).policyLearning(LearningPolicy.NONE).build();
    INDArray array = Nd4j.createUninitialized(150000000);
    MemoryWorkspace workspace = (Nd4jWorkspace) Nd4j.getWorkspaceManager().createNewWorkspace(configuration, "HOST");
    workspace.notifyScopeEntered();
    INDArray memcpy = array.unsafeDuplication(false);
    workspace.notifyScopeLeft();
}
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) Ignore(org.junit.Ignore) Test(org.junit.Test) BaseNd4jTest(org.nd4j.linalg.BaseNd4jTest)

Example 42 with Nd4jWorkspace

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

the class WorkspaceProviderTests method testNestedWorkspaces9.

@Test
public void testNestedWorkspaces9() throws Exception {
    for (int x = 1; x < 10; x++) {
        try (MemoryWorkspace ws = Nd4j.getWorkspaceManager().getAndActivateWorkspace(delayedConfiguration, "WS_1")) {
            INDArray array = Nd4j.create(100 * x);
        }
    }
    Nd4jWorkspace workspace = (Nd4jWorkspace) Nd4j.getWorkspaceManager().getWorkspaceForCurrentThread(delayedConfiguration, "WS_1");
    workspace.initializeWorkspace();
    assertEquals(300 * Nd4j.sizeOfDataType(), workspace.getCurrentSize());
}
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 43 with Nd4jWorkspace

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

the class WorkspaceProviderTests method testNestedWorkspaces2.

@Test
public void testNestedWorkspaces2() 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());
        for (int x = 1; x <= 100; x++) {
            try (Nd4jWorkspace ws2 = (Nd4jWorkspace) Nd4j.getWorkspaceManager().getWorkspaceForCurrentThread(loopConfiguration, "WS2").notifyScopeEntered()) {
                INDArray array2 = Nd4j.create(x);
            }
            Nd4jWorkspace ws2 = (Nd4jWorkspace) Nd4j.getWorkspaceManager().getWorkspaceForCurrentThread("WS2");
            long reqMemory = x * Nd4j.sizeOfDataType();
            assertEquals(reqMemory + reqMemory % 8, ws2.getLastCycleAllocations());
        }
        Nd4j.getWorkspaceManager().getWorkspaceForCurrentThread("WS2").initializeWorkspace();
        assertEquals(100 * Nd4j.sizeOfDataType(), ((Nd4jWorkspace) Nd4j.getWorkspaceManager().getWorkspaceForCurrentThread("WS2")).getCurrentSize());
    }
    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 44 with Nd4jWorkspace

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

the class WorkspaceProviderTests method testNestedWorkspaces6.

@Test
public void testNestedWorkspaces6() throws Exception {
    try (Nd4jWorkspace wsExternal = (Nd4jWorkspace) Nd4j.getWorkspaceManager().getAndActivateWorkspace(firstConfiguration, "External")) {
        INDArray array1 = Nd4j.create(10);
        INDArray array2 = null;
        INDArray array3 = null;
        INDArray array4 = null;
        try (Nd4jWorkspace wsFeedForward = (Nd4jWorkspace) Nd4j.getWorkspaceManager().getAndActivateWorkspace(firstConfiguration, "FeedForward")) {
            array2 = Nd4j.create(10);
            assertEquals(true, array2.isAttached());
            try (Nd4jWorkspace borrowed = (Nd4jWorkspace) Nd4j.getWorkspaceManager().getWorkspaceForCurrentThread("External").notifyScopeBorrowed()) {
                array3 = Nd4j.create(10);
                assertTrue(wsExternal == array3.data().getParentWorkspace());
            }
            assertEquals(true, array3.isAttached());
            try (MemoryWorkspace ws = Nd4j.getMemoryManager().scopeOutOfWorkspaces()) {
                array4 = Nd4j.create(10);
            }
            assertEquals(false, array4.isAttached());
        }
        assertEquals(0, wsExternal.getCurrentSize());
        log.info("------");
    }
}
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 45 with Nd4jWorkspace

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

the class WorkspaceProviderTests method testVariableInput1.

@Test
public void testVariableInput1() throws Exception {
    Nd4jWorkspace workspace = (Nd4jWorkspace) Nd4j.getWorkspaceManager().getWorkspaceForCurrentThread(adsiConfiguration, "ADSI");
    INDArray array1 = null;
    INDArray array2 = null;
    try (MemoryWorkspace ws = Nd4j.getWorkspaceManager().getAndActivateWorkspace(adsiConfiguration, "ADSI")) {
        // we allocate first element smaller then subsequent;
        array1 = Nd4j.create(8, 128, 100);
    }
    long requiredMemory = 8 * 128 * 100 * Nd4j.sizeOfDataType();
    long shiftedSize = ((long) (requiredMemory * 1.3)) + (8 - (((long) (requiredMemory * 1.3)) % 8));
    assertEquals(shiftedSize, workspace.getInitialBlockSize());
    assertEquals(shiftedSize * 4, workspace.getCurrentSize());
    assertEquals(0, workspace.getHostOffset());
    assertEquals(0, workspace.getDeviceOffset());
    assertEquals(1, workspace.getCyclesCount());
    assertEquals(0, workspace.getStepNumber());
    try (MemoryWorkspace ws = Nd4j.getWorkspaceManager().getAndActivateWorkspace(adsiConfiguration, "ADSI")) {
        // allocating same shape
        array1 = Nd4j.create(8, 128, 100);
    }
    assertEquals(workspace.getInitialBlockSize(), workspace.getHostOffset());
    assertEquals(workspace.getInitialBlockSize(), workspace.getDeviceOffset());
    assertEquals(2, workspace.getCyclesCount());
    assertEquals(0, workspace.getStepNumber());
    try (MemoryWorkspace ws = Nd4j.getWorkspaceManager().getAndActivateWorkspace(adsiConfiguration, "ADSI")) {
        // allocating bigger shape
        array1 = Nd4j.create(8, 128, 200).assign(1.0);
    }
    // offsets should be intact, allocation happened as pinned
    assertEquals(workspace.getInitialBlockSize(), workspace.getHostOffset());
    assertEquals(workspace.getInitialBlockSize(), workspace.getDeviceOffset());
    assertEquals(1, workspace.getNumberOfPinnedAllocations());
    assertEquals(3, workspace.getCyclesCount());
    assertEquals(0, workspace.getStepNumber());
    try (MemoryWorkspace ws = Nd4j.getWorkspaceManager().getAndActivateWorkspace(adsiConfiguration, "ADSI")) {
        // allocating same shape
        array1 = Nd4j.create(8, 128, 100);
    }
    assertEquals(2, workspace.getNumberOfPinnedAllocations());
    assertEquals(0, workspace.getStepNumber());
    assertEquals(4, workspace.getCyclesCount());
    try (MemoryWorkspace ws = Nd4j.getWorkspaceManager().getAndActivateWorkspace(adsiConfiguration, "ADSI")) {
        // allocating same shape
        array1 = Nd4j.create(8, 128, 100);
    }
    assertEquals(3, workspace.getNumberOfPinnedAllocations());
    assertEquals(1, workspace.getStepNumber());
    assertEquals(5, workspace.getCyclesCount());
    for (int i = 0; i < 12; i++) {
        try (MemoryWorkspace ws = Nd4j.getWorkspaceManager().getAndActivateWorkspace(adsiConfiguration, "ADSI")) {
            // allocating same shape
            array1 = Nd4j.create(8, 128, 100);
        }
    }
    // Now we know that workspace was reallocated and offset was shifted to the end of workspace
    assertEquals(4, workspace.getStepNumber());
    requiredMemory = 8 * 128 * 200 * Nd4j.sizeOfDataType();
    shiftedSize = ((long) (requiredMemory * 1.3)) + (8 - (((long) (requiredMemory * 1.3)) % 8));
    assertEquals(shiftedSize * 4, workspace.getCurrentSize());
    assertEquals(workspace.getCurrentSize(), workspace.getHostOffset());
    assertEquals(workspace.getCurrentSize(), workspace.getDeviceOffset());
}
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