use of org.nd4j.linalg.api.buffer.DataBuffer in project nd4j by deeplearning4j.
the class NDArrayTestsFortran method testColumnMmul.
@Test
public void testColumnMmul() {
DataBuffer data = Nd4j.linspace(1, 10, 18).data();
INDArray x2 = Nd4j.create(data, new int[] { 2, 3, 3 });
data = Nd4j.linspace(1, 12, 9).data();
INDArray y2 = Nd4j.create(data, new int[] { 3, 3 });
INDArray z2 = Nd4j.create(new int[] { 3, 2 }, 'f');
z2.putColumn(0, y2.getColumn(0));
z2.putColumn(1, y2.getColumn(1));
INDArray nofOffset = Nd4j.create(new int[] { 3, 3 }, 'f');
nofOffset.assign(x2.slice(0));
assertEquals(nofOffset, x2.slice(0));
INDArray slice = x2.slice(0);
INDArray zeroOffsetResult = slice.mmul(z2);
INDArray offsetResult = nofOffset.mmul(z2);
assertEquals(zeroOffsetResult, offsetResult);
INDArray slice1 = x2.slice(1);
INDArray noOffset2 = Nd4j.create(slice1.shape());
noOffset2.assign(slice1);
assertEquals(slice1, noOffset2);
INDArray noOffsetResult = noOffset2.mmul(z2);
INDArray slice1OffsetResult = slice1.mmul(z2);
assertEquals(noOffsetResult, slice1OffsetResult);
}
use of org.nd4j.linalg.api.buffer.DataBuffer in project nd4j by deeplearning4j.
the class ProtectedCudaConstantHandler method ensureMaps.
private void ensureMaps(Integer deviceId) {
if (!buffersCache.containsKey(deviceId)) {
if (flowController == null)
flowController = AtomicAllocator.getInstance().getFlowController();
try {
synchronized (this) {
if (!buffersCache.containsKey(deviceId)) {
// TODO: this op call should be checked
// nativeOps.setDevice(new CudaPointer(deviceId));
buffersCache.put(deviceId, new ConcurrentHashMap<ArrayDescriptor, DataBuffer>());
constantOffsets.put(deviceId, new AtomicLong(0));
deviceLocks.put(deviceId, new Semaphore(1));
Pointer cAddr = NativeOpsHolder.getInstance().getDeviceNativeOps().getConstantSpace();
// logger.info("constant pointer: {}", cAddr.address() );
deviceAddresses.put(deviceId, cAddr);
}
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
use of org.nd4j.linalg.api.buffer.DataBuffer in project nd4j by deeplearning4j.
the class ProtectedCudaConstantHandler method getConstantBuffer.
/**
* This method returns DataBuffer with contant equal to input array.
*
* PLEASE NOTE: This method assumes that you'll never ever change values within result DataBuffer
*
* @param array
* @return
*/
@Override
public DataBuffer getConstantBuffer(int[] array) {
// logger.info("getConstantBuffer(int[]) called");
ArrayDescriptor descriptor = new ArrayDescriptor(array);
Integer deviceId = AtomicAllocator.getInstance().getDeviceId();
ensureMaps(deviceId);
if (!buffersCache.get(deviceId).containsKey(descriptor)) {
// we create new databuffer
// logger.info("Creating new constant buffer...");
DataBuffer buffer = Nd4j.createBufferDetached(array);
if (constantOffsets.get(deviceId).get() + (array.length * 4) < MAX_CONSTANT_LENGTH) {
buffer.setConstant(true);
// now we move data to constant memory, and keep happy
moveToConstantSpace(buffer);
buffersCache.get(deviceId).put(descriptor, buffer);
bytes.addAndGet(array.length * 4);
}
return buffer;
}
return buffersCache.get(deviceId).get(descriptor);
}
use of org.nd4j.linalg.api.buffer.DataBuffer in project nd4j by deeplearning4j.
the class SynchronousFlowController method prepareDelayedMemory.
protected void prepareDelayedMemory(INDArray array) {
if (configuration.getMemoryModel() == Configuration.MemoryModel.DELAYED) {
AllocationPoint pointData = allocator.getAllocationPoint(array.shapeInfoDataBuffer());
AllocationPoint pointShape = allocator.getAllocationPoint(array.shapeInfoDataBuffer());
if (pointData.getAllocationStatus() != AllocationStatus.DEVICE)
prepareDelayedMemory(array.data());
if (pointShape.getAllocationStatus() == AllocationStatus.HOST) {
DataBuffer oShape = array.shapeInfoDataBuffer();
DataBuffer nShape = Nd4j.getConstantHandler().relocateConstantSpace(oShape);
if (nShape == oShape)
Nd4j.getConstantHandler().moveToConstantSpace(nShape);
((JCublasNDArray) array).setShapeInfoDataBuffer(nShape);
}
}
}
use of org.nd4j.linalg.api.buffer.DataBuffer in project nd4j by deeplearning4j.
the class SynchronousFlowController method prepareActionAllWrite.
@Override
public CudaContext prepareActionAllWrite(INDArray... operands) {
CudaContext context = (CudaContext) allocator.getDeviceContext().getContext();
int cId = allocator.getDeviceId();
for (INDArray operand : operands) {
if (operand == null)
continue;
Nd4j.getCompressor().autoDecompress(operand);
AllocationPoint pointData = allocator.getAllocationPoint(operand);
AllocationPoint pointShape = allocator.getAllocationPoint(operand.shapeInfoDataBuffer());
pointData.acquireLock();
if (pointData.getDeviceId() != cId && pointData.getDeviceId() >= 0) {
DataBuffer buffer = operand.data().originalDataBuffer() == null ? operand.data() : operand.data().originalDataBuffer();
allocator.getMemoryHandler().relocateObject(buffer);
}
if (pointShape.getDeviceId() != cId && pointShape.getDeviceId() >= 0) {
((JCublasNDArray) operand).setShapeInfoDataBuffer(Nd4j.getConstantHandler().relocateConstantSpace(operand.shapeInfoDataBuffer()));
}
prepareDelayedMemory(operand);
allocator.getAllocationPoint(operand).setCurrentContext(context);
}
return context;
}
Aggregations