Java Code Examples for org.nd4j.linalg.api.memory.MemoryWorkspace#close()

The following examples show how to use org.nd4j.linalg.api.memory.MemoryWorkspace#close() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DoubleDataBufferTest.java    From nd4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testReallocationWorkspace() {
    WorkspaceConfiguration initialConfig = WorkspaceConfiguration.builder().initialSize(10 * 1024L * 1024L)
                    .policyAllocation(AllocationPolicy.STRICT).policyLearning(LearningPolicy.NONE).build();
    MemoryWorkspace workspace = Nd4j.getWorkspaceManager().getAndActivateWorkspace(initialConfig, "SOME_ID");

    DataBuffer buffer = Nd4j.createBuffer(new double[] {1, 2, 3, 4});
    double[] old = buffer.asDouble();
    assertTrue(buffer.isAttached());
    assertEquals(4, buffer.capacity());
    buffer.reallocate(6);
    assertEquals(6, buffer.capacity());
    assertArrayEquals(old, buffer.asDouble(), 1e-1);
    workspace.close();

}
 
Example 2
Source File: FloatDataBufferTest.java    From nd4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testReallocationWorkspace() {
    WorkspaceConfiguration initialConfig = WorkspaceConfiguration.builder().initialSize(10 * 1024L * 1024L)
                    .policyAllocation(AllocationPolicy.STRICT).policyLearning(LearningPolicy.NONE).build();
    MemoryWorkspace workspace = Nd4j.getWorkspaceManager().getAndActivateWorkspace(initialConfig, "SOME_ID");

    DataBuffer buffer = Nd4j.createBuffer(new float[] {1, 2, 3, 4});
    assertTrue(buffer.isAttached());
    float[] old = buffer.asFloat();
    assertEquals(4, buffer.capacity());
    buffer.reallocate(6);
    assertEquals(6, buffer.capacity());
    float[] newBuf = buffer.asFloat();
    assertArrayEquals(old, newBuf, 1e-4F);
    workspace.close();
}
 
Example 3
Source File: IntDataBufferTests.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testReallocationWorkspace() {
    WorkspaceConfiguration initialConfig = WorkspaceConfiguration.builder().initialSize(10 * 1024L * 1024L)
                    .policyAllocation(AllocationPolicy.STRICT).policyLearning(LearningPolicy.NONE).build();
    MemoryWorkspace workspace = Nd4j.getWorkspaceManager().getAndActivateWorkspace(initialConfig, "SOME_ID");

    DataBuffer buffer = Nd4j.createBuffer(new int[] {1, 2, 3, 4});
    val old = buffer.asInt();
    assertTrue(buffer.isAttached());
    assertEquals(4, buffer.capacity());
    buffer.reallocate(6);
    assertEquals(6, buffer.capacity());
    val newContent = buffer.asInt();
    assertEquals(6, newContent.length);
    assertArrayEquals(old, Arrays.copyOf(newContent, old.length));
    workspace.close();
}
 
Example 4
Source File: DoubleDataBufferTest.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testReallocationWorkspace() {
    WorkspaceConfiguration initialConfig = WorkspaceConfiguration.builder().initialSize(10 * 1024L * 1024L)
                    .policyAllocation(AllocationPolicy.STRICT).policyLearning(LearningPolicy.NONE).build();
    MemoryWorkspace workspace = Nd4j.getWorkspaceManager().getAndActivateWorkspace(initialConfig, "SOME_ID");

    DataBuffer buffer = Nd4j.createBuffer(new double[] {1, 2, 3, 4});
    double[] old = buffer.asDouble();
    assertTrue(buffer.isAttached());
    assertEquals(4, buffer.capacity());
    buffer.reallocate(6);
    assertEquals(6, buffer.capacity());
    assertArrayEquals(old, Arrays.copyOf(buffer.asDouble(), 4), 1e-1);
    workspace.close();

}
 
Example 5
Source File: FloatDataBufferTest.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testReallocationWorkspace() {
    WorkspaceConfiguration initialConfig = WorkspaceConfiguration.builder().initialSize(10 * 1024L * 1024L)
                    .policyAllocation(AllocationPolicy.STRICT).policyLearning(LearningPolicy.NONE).build();
    MemoryWorkspace workspace = Nd4j.getWorkspaceManager().getAndActivateWorkspace(initialConfig, "SOME_ID");

    DataBuffer buffer = Nd4j.createBuffer(new float[] {1, 2, 3, 4});
    assertTrue(buffer.isAttached());
    float[] old = buffer.asFloat();
    assertEquals(4, buffer.capacity());
    buffer.reallocate(6);
    assertEquals(6, buffer.capacity());
    float[] newBuf = buffer.asFloat();
    assertArrayEquals(old, newBuf, 1e-4F);
    workspace.close();
}
 
Example 6
Source File: IntDataBufferTests.java    From nd4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testReallocationWorkspace() {
    WorkspaceConfiguration initialConfig = WorkspaceConfiguration.builder().initialSize(10 * 1024L * 1024L)
                    .policyAllocation(AllocationPolicy.STRICT).policyLearning(LearningPolicy.NONE).build();
    MemoryWorkspace workspace = Nd4j.getWorkspaceManager().getAndActivateWorkspace(initialConfig, "SOME_ID");

    DataBuffer buffer = Nd4j.createBuffer(new int[] {1, 2, 3, 4});
    int[] old = buffer.asInt();
    assertTrue(buffer.isAttached());
    assertEquals(4, buffer.capacity());
    buffer.reallocate(6);
    assertEquals(6, buffer.capacity());
    assertArrayEquals(old, buffer.asInt());
    workspace.close();
}
 
Example 7
Source File: BasicWorkspaceTests.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testMmap1() {
    // 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)
            .policyLearning(LearningPolicy.NONE)
            .build();

    MemoryWorkspace ws = Nd4j.getWorkspaceManager().getAndActivateWorkspace(mmap, "M2");

    INDArray mArray = Nd4j.create(DOUBLE, 100);
    mArray.assign(10f);

    assertEquals(1000f, mArray.sumNumber().floatValue(), 1e-5);

    ws.close();


    ws.notifyScopeEntered();

    INDArray mArrayR = Nd4j.createUninitialized(DOUBLE, 100);
    assertEquals(1000f, mArrayR.sumNumber().floatValue(), 1e-5);

    ws.close();
}
 
Example 8
Source File: BasicWorkspaceTests.java    From nd4j with Apache License 2.0 3 votes vote down vote up
@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();
}