Search in sources :

Example 6 with PointersPair

use of org.nd4j.jita.allocator.pointers.PointersPair in project nd4j by deeplearning4j.

the class CudaFullCachingProvider method malloc.

/**
 * This method provides PointersPair to memory chunk specified by AllocationShape
 *
 * PLEASE NOTE: This method can actually ignore malloc request, and give out previously cached free memory chunk with equal shape.
 *
 * @param shape shape of desired memory chunk
 * @param point target AllocationPoint structure
 * @param location either HOST or DEVICE
 * @return
 */
@Override
public PointersPair malloc(AllocationShape shape, AllocationPoint point, AllocationStatus location) {
    long reqMemory = AllocationUtils.getRequiredMemory(shape);
    if (location == AllocationStatus.DEVICE && reqMemory < CudaEnvironment.getInstance().getConfiguration().getMaximumDeviceAllocation()) {
        int deviceId = AtomicAllocator.getInstance().getDeviceId();
        ensureDeviceCacheHolder(deviceId, shape);
        CacheHolder cache = deviceCache.get(deviceId).get(shape);
        if (cache != null) {
            Pointer pointer = cache.poll();
            if (pointer != null) {
                cacheDeviceHit.incrementAndGet();
                deviceCachedAmount.get(deviceId).addAndGet(-1 * reqMemory);
                PointersPair pair = new PointersPair();
                pair.setDevicePointer(pointer);
                point.setAllocationStatus(AllocationStatus.DEVICE);
                point.setDeviceId(deviceId);
                return pair;
            }
        }
        cacheDeviceMiss.incrementAndGet();
        return super.malloc(shape, point, location);
    }
    return super.malloc(shape, point, location);
}
Also used : PointersPair(org.nd4j.jita.allocator.pointers.PointersPair) Pointer(org.bytedeco.javacpp.Pointer) CudaPointer(org.nd4j.jita.allocator.pointers.CudaPointer) AllocationPoint(org.nd4j.jita.allocator.impl.AllocationPoint)

Example 7 with PointersPair

use of org.nd4j.jita.allocator.pointers.PointersPair in project nd4j by deeplearning4j.

the class DelayedMemoryTest method testDelayedAllocation2.

/**
 * This test should be run manually
 *
 * @throws Exception
 */
@Test
public void testDelayedAllocation2() throws Exception {
    AtomicAllocator allocator = AtomicAllocator.getInstance();
    INDArray array = Nd4j.create(10, 10);
    AllocationPoint pointer = allocator.getAllocationPoint(array);
    PointersPair pair = pointer.getPointers();
    // pointers should be equal, device memory wasn't allocated yet
    assertEquals(pair.getDevicePointer(), pair.getHostPointer());
    // ////////////
    AllocationPoint shapePointer = allocator.getAllocationPoint(array.shapeInfoDataBuffer());
    // pointers should be equal, device memory wasn't allocated yet
    assertEquals(shapePointer.getPointers().getDevicePointer(), shapePointer.getPointers().getHostPointer());
    assertEquals(pointer.getAllocationStatus(), AllocationStatus.HOST);
    assertEquals(shapePointer.getAllocationStatus(), AllocationStatus.HOST);
    float sum = array.sumNumber().floatValue();
    assertEquals(0.0f, sum, 0.0001f);
    shapePointer = allocator.getAllocationPoint(array.shapeInfoDataBuffer());
    pointer = allocator.getAllocationPoint(array);
    assertEquals(AllocationStatus.CONSTANT, shapePointer.getAllocationStatus());
    assertEquals(AllocationStatus.DEVICE, pointer.getAllocationStatus());
    // at this point all pointers show be different, since we've used OP (sumNumber)
    assertNotEquals(shapePointer.getPointers().getDevicePointer(), shapePointer.getPointers().getHostPointer());
}
Also used : INDArray(org.nd4j.linalg.api.ndarray.INDArray) PointersPair(org.nd4j.jita.allocator.pointers.PointersPair) AtomicAllocator(org.nd4j.jita.allocator.impl.AtomicAllocator) AllocationPoint(org.nd4j.jita.allocator.impl.AllocationPoint) Test(org.junit.Test)

Example 8 with PointersPair

use of org.nd4j.jita.allocator.pointers.PointersPair in project nd4j by deeplearning4j.

the class DelayedMemoryTest method testDelayedAllocation3.

@Test
public void testDelayedAllocation3() throws Exception {
    INDArray array = Nd4j.create(new float[] { 1f, 2f, 3f, 4f, 5f });
    AllocationPoint pointer = AtomicAllocator.getInstance().getAllocationPoint(array);
    PointersPair pair = pointer.getPointers();
    // pointers should be equal, device memory wasn't allocated yet
    assertEquals(pair.getDevicePointer(), pair.getHostPointer());
    assertEquals(2.0f, array.getFloat(1), 0.001f);
    assertEquals(pair.getDevicePointer(), pair.getHostPointer());
}
Also used : INDArray(org.nd4j.linalg.api.ndarray.INDArray) PointersPair(org.nd4j.jita.allocator.pointers.PointersPair) AllocationPoint(org.nd4j.jita.allocator.impl.AllocationPoint) Test(org.junit.Test)

Aggregations

PointersPair (org.nd4j.jita.allocator.pointers.PointersPair)8 AllocationPoint (org.nd4j.jita.allocator.impl.AllocationPoint)5 Test (org.junit.Test)3 INDArray (org.nd4j.linalg.api.ndarray.INDArray)3 Pointer (org.bytedeco.javacpp.Pointer)2 CudaPointer (org.nd4j.jita.allocator.pointers.CudaPointer)2 DataInputStream (java.io.DataInputStream)1 DataOutputStream (java.io.DataOutputStream)1 FileInputStream (java.io.FileInputStream)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1 lombok.val (lombok.val)1 GarbageBufferReference (org.nd4j.jita.allocator.garbage.GarbageBufferReference)1 AtomicAllocator (org.nd4j.jita.allocator.impl.AtomicAllocator)1 CudaWorkspace (org.nd4j.jita.workspace.CudaWorkspace)1 PagedPointer (org.nd4j.linalg.api.memory.pointers.PagedPointer)1 ND4JIllegalStateException (org.nd4j.linalg.exception.ND4JIllegalStateException)1 CudaContext (org.nd4j.linalg.jcublas.context.CudaContext)1