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);
}
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());
}
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());
}
Aggregations