use of org.nd4j.linalg.api.buffer.DataBuffer in project nd4j by deeplearning4j.
the class BaseSparseNDArrayCOO method subArray.
@Override
public INDArray subArray(ShapeOffsetResolution resolution) {
long[] offsets = resolution.getOffsets();
int[] shape = LongUtils.toInts(resolution.getShapes());
int[] stride = LongUtils.toInts(resolution.getStrides());
int[] flags = resolution.getFixed();
flags = updateFlags(flags, shape);
long offset = (int) (offset() + resolution.getOffset());
int newRank = shape.length;
long[] sparseOffsets = createSparseOffsets(offset);
int[] newAxis = createHiddenDimensions(resolution.getPrependAxis());
if (offset() + resolution.getOffset() >= Integer.MAX_VALUE)
throw new IllegalArgumentException("Offset of array can not be >= Integer.MAX_VALUE");
if (offsets.length != newRank)
throw new IllegalArgumentException("Invalid offset " + Arrays.toString(offsets));
if (stride.length != newRank)
throw new IllegalArgumentException("Invalid stride " + Arrays.toString(stride));
if (shape.length == rank() && Shape.contentEquals(shape, shapeOf())) {
if (ArrayUtil.isZero(offsets)) {
return this;
} else {
throw new IllegalArgumentException("Invalid subArray offsets");
}
}
DataBuffer newSparseInformation = Nd4j.getSparseInfoProvider().createSparseInformation(flags, sparseOffsets, newAxis, underlyingRank());
return create(values, indices, newSparseInformation, Arrays.copyOf(shape, shape.length));
}
use of org.nd4j.linalg.api.buffer.DataBuffer in project nd4j by deeplearning4j.
the class BaseSparseNDArrayCSR method addAtPosition.
private DataBuffer addAtPosition(DataBuffer buf, long dataSize, int pos, double value) {
DataBuffer buffer = (buf.length() == dataSize) ? reallocate(buf) : buf;
double[] tail = buffer.getDoublesAt(pos, (int) dataSize - pos);
buffer.put(pos, value);
for (int i = 0; i < tail.length; i++) {
buffer.put(i + pos + 1, tail[i]);
}
return buffer;
}
use of org.nd4j.linalg.api.buffer.DataBuffer in project nd4j by deeplearning4j.
the class BaseNDArray method detach.
/**
* This metod detaches INDArray from Workspace, returning copy. Basically it's dup() into new memory chunk.
* <p>
* PLEASE NOTE: If this INDArray instance is NOT attached - it will be returned unmodified.
*
* @return
*/
@Override
public INDArray detach() {
if (!isAttached())
return this;
Nd4j.getExecutioner().commit();
/*
two options here
1) we're within some workspace
2) we're out of any workspace
*/
if (Nd4j.getMemoryManager().getCurrentWorkspace() == null) {
if (!isView()) {
Nd4j.getExecutioner().commit();
DataBuffer buffer = Nd4j.createBuffer(this.lengthLong(), false);
Nd4j.getMemoryManager().memcpy(buffer, this.data());
return Nd4j.createArrayFromShapeBuffer(buffer, this.shapeInfoDataBuffer());
} else {
INDArray copy = Nd4j.createUninitialized(this.shape(), this.ordering());
copy.assign(this);
Nd4j.getExecutioner().commit();
return copy;
}
} else {
MemoryWorkspace workspace = Nd4j.getMemoryManager().getCurrentWorkspace();
Nd4j.getMemoryManager().setCurrentWorkspace(null);
INDArray copy = null;
if (!isView()) {
Nd4j.getExecutioner().commit();
DataBuffer buffer = Nd4j.createBuffer(this.lengthLong(), false);
// Pointer.memcpy(buffer.pointer(), this.data.pointer(), this.lengthLong() * Nd4j.sizeOfDataType(this.data.dataType()));
Nd4j.getMemoryManager().memcpy(buffer, this.data());
// this.dup(this.ordering());
copy = Nd4j.createArrayFromShapeBuffer(buffer, this.shapeInfoDataBuffer());
} else {
copy = Nd4j.createUninitialized(this.shape(), this.ordering());
copy.assign(this);
Nd4j.getExecutioner().commit();
}
Nd4j.getMemoryManager().setCurrentWorkspace(workspace);
return copy;
}
}
use of org.nd4j.linalg.api.buffer.DataBuffer in project nd4j by deeplearning4j.
the class BaseNDArray method migrate.
/**
* This method pulls this INDArray into current Workspace, or optionally detaches if no workspace is present.<br>
* That is:<br>
* If current workspace is present/active, INDArray is migrated to it.<br>
* If no current workspace is present/active, one of two things occur:
* 1. If detachOnNoWs arg is true: if there is no current workspace, INDArray is detached
* 2. If detachOnNoWs arg is false: this INDArray is returned as-is (no-op) - equivalent to {@link #migrate()}
*
* @param detachOnNoWs If true: detach on no WS. If false and no workspace: return this.
* @return Migrated INDArray
*/
@Override
public INDArray migrate(boolean detachOnNoWs) {
MemoryWorkspace current = Nd4j.getMemoryManager().getCurrentWorkspace();
if (current == null) {
if (detachOnNoWs) {
return detach();
} else {
return this;
}
}
INDArray copy = null;
if (!this.isView()) {
Nd4j.getExecutioner().commit();
DataBuffer buffer = Nd4j.createBuffer(this.lengthLong(), false);
Nd4j.getMemoryManager().memcpy(buffer, this.data());
copy = Nd4j.createArrayFromShapeBuffer(buffer, this.shapeInfoDataBuffer());
} else {
copy = this.dup(this.ordering());
Nd4j.getExecutioner().commit();
}
return copy;
}
use of org.nd4j.linalg.api.buffer.DataBuffer in project nd4j by deeplearning4j.
the class Shape method isVector.
/**
* Returns whether the given shape is a vector
*
* @param shapeInfo the shapeinfo to test
* @return whether the given shape is a vector
*/
public static boolean isVector(DataBuffer shapeInfo) {
int rank = Shape.rank(shapeInfo);
if (rank > 2 || rank < 1)
return false;
else {
int len = Shape.length(shapeInfo);
DataBuffer shape = Shape.shapeOf(shapeInfo);
return shape.getInt(0) == len || shape.getInt(1) == len;
}
}
Aggregations