Search in sources :

Example 11 with MemoryWorkspace

use of org.nd4j.linalg.api.memory.MemoryWorkspace in project nd4j by deeplearning4j.

the class OperationProfilerTests method testScopePanic3.

@Test
public void testScopePanic3() throws Exception {
    Nd4j.getExecutioner().setProfilingMode(OpExecutioner.ProfilingMode.SCOPE_PANIC);
    INDArray array;
    try (MemoryWorkspace workspace = Nd4j.getWorkspaceManager().getAndActivateWorkspace("WS121")) {
        array = Nd4j.create(10);
        assertTrue(array.isAttached());
        assertEquals(1, workspace.getGenerationId());
        try (MemoryWorkspace workspaceInner = Nd4j.getWorkspaceManager().getAndActivateWorkspace("WS122")) {
            array.add(1.0);
        }
    }
}
Also used : INDArray(org.nd4j.linalg.api.ndarray.INDArray) MemoryWorkspace(org.nd4j.linalg.api.memory.MemoryWorkspace) Test(org.junit.Test)

Example 12 with MemoryWorkspace

use of org.nd4j.linalg.api.memory.MemoryWorkspace 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)

Example 13 with MemoryWorkspace

use of org.nd4j.linalg.api.memory.MemoryWorkspace in project nd4j by deeplearning4j.

the class BasicWorkspaceTests method testLoop4.

@Test
public void testLoop4() 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);
        INDArray array2 = Nd4j.create(100);
    }
    assertEquals(0, workspace.getHostOffset());
    assertEquals(200 * Nd4j.sizeOfDataType(), workspace.getCurrentSize());
    try (MemoryWorkspace cW = workspace.notifyScopeEntered()) {
        INDArray array1 = Nd4j.create(100);
        assertEquals(100 * Nd4j.sizeOfDataType(), workspace.getHostOffset());
    }
    assertEquals(0, 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)

Example 14 with MemoryWorkspace

use of org.nd4j.linalg.api.memory.MemoryWorkspace in project nd4j by deeplearning4j.

the class BasicWorkspaceTests method testMmap1.

@Test
public void testMmap1() throws Exception {
    // we don't support MMAP on cuda yet
    if (Nd4j.getExecutioner().getClass().getName().toLowerCase().contains("cuda"))
        return;
    WorkspaceConfiguration mmap = WorkspaceConfiguration.builder().initialSize(1000000).policyLocation(LocationPolicy.MMAP).build();
    MemoryWorkspace ws = Nd4j.getWorkspaceManager().getAndActivateWorkspace(mmap, "M2");
    INDArray mArray = Nd4j.create(100);
    mArray.assign(10f);
    assertEquals(1000f, mArray.sumNumber().floatValue(), 1e-5);
    ws.close();
    ws.notifyScopeEntered();
    INDArray mArrayR = Nd4j.createUninitialized(100);
    assertEquals(1000f, mArrayR.sumNumber().floatValue(), 1e-5);
    ws.close();
}
Also used : INDArray(org.nd4j.linalg.api.ndarray.INDArray) WorkspaceConfiguration(org.nd4j.linalg.api.memory.conf.WorkspaceConfiguration) MemoryWorkspace(org.nd4j.linalg.api.memory.MemoryWorkspace) Test(org.junit.Test) BaseNd4jTest(org.nd4j.linalg.BaseNd4jTest)

Example 15 with MemoryWorkspace

use of org.nd4j.linalg.api.memory.MemoryWorkspace in project nd4j by deeplearning4j.

the class BasicWorkspaceTests method testMmap2.

@Test
public void testMmap2() throws Exception {
    // we don't support MMAP on cuda yet
    if (Nd4j.getExecutioner().getClass().getName().toLowerCase().contains("cuda"))
        return;
    File tmp = File.createTempFile("tmp", "fdsfdf");
    tmp.deleteOnExit();
    Nd4jWorkspace.fillFile(tmp, 100000);
    WorkspaceConfiguration mmap = WorkspaceConfiguration.builder().policyLocation(LocationPolicy.MMAP).tempFilePath(tmp.getAbsolutePath()).build();
    MemoryWorkspace ws = Nd4j.getWorkspaceManager().getAndActivateWorkspace(mmap, "M3");
    INDArray mArray = Nd4j.create(100);
    mArray.assign(10f);
    assertEquals(1000f, mArray.sumNumber().floatValue(), 1e-5);
    ws.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) File(java.io.File) Test(org.junit.Test) BaseNd4jTest(org.nd4j.linalg.BaseNd4jTest)

Aggregations

MemoryWorkspace (org.nd4j.linalg.api.memory.MemoryWorkspace)62 Test (org.junit.Test)39 BaseNd4jTest (org.nd4j.linalg.BaseNd4jTest)35 INDArray (org.nd4j.linalg.api.ndarray.INDArray)35 Nd4jWorkspace (org.nd4j.linalg.memory.abstracts.Nd4jWorkspace)18 WorkspaceConfiguration (org.nd4j.linalg.api.memory.conf.WorkspaceConfiguration)14 DataBuffer (org.nd4j.linalg.api.buffer.DataBuffer)6 AtomicLong (java.util.concurrent.atomic.AtomicLong)5 AllocationPoint (org.nd4j.jita.allocator.impl.AllocationPoint)4 ND4JIllegalStateException (org.nd4j.linalg.exception.ND4JIllegalStateException)3 CudaContext (org.nd4j.linalg.jcublas.context.CudaContext)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 DataInputStream (java.io.DataInputStream)2 DataOutputStream (java.io.DataOutputStream)2 File (java.io.File)2 ArrayList (java.util.ArrayList)2 Ignore (org.junit.Ignore)2 IOException (java.io.IOException)1 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)1