Search in sources :

Example 91 with DataBuffer

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));
}
Also used : DataBuffer(org.nd4j.linalg.api.buffer.DataBuffer)

Example 92 with DataBuffer

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;
}
Also used : DataBuffer(org.nd4j.linalg.api.buffer.DataBuffer)

Example 93 with DataBuffer

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;
    }
}
Also used : MemoryWorkspace(org.nd4j.linalg.api.memory.MemoryWorkspace) DataBuffer(org.nd4j.linalg.api.buffer.DataBuffer)

Example 94 with DataBuffer

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;
}
Also used : MemoryWorkspace(org.nd4j.linalg.api.memory.MemoryWorkspace) DataBuffer(org.nd4j.linalg.api.buffer.DataBuffer)

Example 95 with DataBuffer

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;
    }
}
Also used : DataBuffer(org.nd4j.linalg.api.buffer.DataBuffer)

Aggregations

DataBuffer (org.nd4j.linalg.api.buffer.DataBuffer)186 INDArray (org.nd4j.linalg.api.ndarray.INDArray)79 Test (org.junit.Test)47 CompressedDataBuffer (org.nd4j.linalg.compression.CompressedDataBuffer)44 CudaContext (org.nd4j.linalg.jcublas.context.CudaContext)39 CudaPointer (org.nd4j.jita.allocator.pointers.CudaPointer)30 AllocationPoint (org.nd4j.jita.allocator.impl.AllocationPoint)25 ND4JIllegalStateException (org.nd4j.linalg.exception.ND4JIllegalStateException)23 BaseDataBuffer (org.nd4j.linalg.api.buffer.BaseDataBuffer)19 Pointer (org.bytedeco.javacpp.Pointer)18 BaseNd4jTest (org.nd4j.linalg.BaseNd4jTest)16 CudaDoubleDataBuffer (org.nd4j.linalg.jcublas.buffer.CudaDoubleDataBuffer)16 IntPointer (org.bytedeco.javacpp.IntPointer)13 PagedPointer (org.nd4j.linalg.api.memory.pointers.PagedPointer)13 CudaIntDataBuffer (org.nd4j.linalg.jcublas.buffer.CudaIntDataBuffer)13 DoublePointer (org.bytedeco.javacpp.DoublePointer)12 FloatPointer (org.bytedeco.javacpp.FloatPointer)12 GridExecutioner (org.nd4j.linalg.api.ops.executioner.GridExecutioner)12 LongPointerWrapper (org.nd4j.nativeblas.LongPointerWrapper)11 CUstream_st (org.bytedeco.javacpp.cuda.CUstream_st)10