Java Code Examples for org.nd4j.linalg.util.ArrayUtil#copy()

The following examples show how to use org.nd4j.linalg.util.ArrayUtil#copy() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: NdIndexIterator.java    From nd4j with Apache License 2.0 6 votes vote down vote up
/**
 *  Pass in the shape to iterate over.
 *  Defaults to c ordering
 * @param shape the shape to iterate over
 */
public NdIndexIterator(char order, boolean cache, long... shape) {
    this.shape = ArrayUtil.copy(shape);
    this.length = ArrayUtil.prod(shape);
    this.order = order;
    this.cache = cache;
    if (this.cache) {
        LinearIndexLookup lookup = lookupMap.get(new Pair<>(shape, order));
        if (lookup == null) {
            lookup = new LinearIndexLookup(shape, order);
            //warm up the cache
            for (int i = 0; i < length; i++) {
                lookup.lookup(i);
            }
            lookupMap.put(new Pair<>(shape, order), lookup);
            this.lookup = lookup;
        } else {
            this.lookup = lookupMap.get(new Pair<>(shape, order));
        }

    }
}
 
Example 2
Source File: ShapeResolutionTestsC.java    From nd4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testRowVectorShapeOneZeroOffset() {
    INDArray arr = Nd4j.create(2, 2);
    ShapeOffsetResolution resolution = new ShapeOffsetResolution(arr);
    //row 0
    resolution.exec(NDArrayIndex.point(0));
    long[] oneIndexShape = ArrayUtil.copy(resolution.getShapes());
    assertArrayEquals(new long[] {1, 2}, oneIndexShape);
    long[] oneIndexOffsets = ArrayUtil.copy(resolution.getOffsets());
    assertArrayEquals(new long[] {0, 0}, oneIndexOffsets);
    assertEquals(0, resolution.getOffset());
    long[] oneIndexStrides = ArrayUtil.copy(resolution.getStrides());
    assertArrayEquals(new long[] {1, 1}, oneIndexStrides);

}
 
Example 3
Source File: ShapeResolutionTestsC.java    From nd4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testRowVectorShapeOneOneOffset() {
    INDArray arr = Nd4j.create(2, 2);
    ShapeOffsetResolution resolution = new ShapeOffsetResolution(arr);
    //row 0
    resolution.exec(NDArrayIndex.point(1));
    long[] oneIndexShape = ArrayUtil.copy(resolution.getShapes());
    assertArrayEquals(new long[] {1, 2}, oneIndexShape);
    assertEquals(2, resolution.getOffset());
    long[] oneIndexStrides = ArrayUtil.copy(resolution.getStrides());
    assertArrayEquals(new long[] {1, 1}, oneIndexStrides);

}
 
Example 4
Source File: ShapeResolutionTestsC.java    From nd4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testRowVectorShapeTwoOneOffset() {
    INDArray arr = Nd4j.create(2, 2);
    ShapeOffsetResolution resolution = new ShapeOffsetResolution(arr);
    //row 0
    resolution.exec(NDArrayIndex.point(1), NDArrayIndex.all());
    long[] oneIndexShape = ArrayUtil.copy(resolution.getShapes());
    assertArrayEquals(new long[] {1, 2}, oneIndexShape);
    long[] oneIndexOffsets = ArrayUtil.copy(resolution.getOffsets());
    assertArrayEquals(new long[] {0, 0}, oneIndexOffsets);
    assertEquals(2, resolution.getOffset());
    long[] oneIndexStrides = ArrayUtil.copy(resolution.getStrides());
    assertArrayEquals(new long[] {1, 1}, oneIndexStrides);

}
 
Example 5
Source File: JCublasNDArrayFactory.java    From nd4j with Apache License 2.0 4 votes vote down vote up
@Override
public INDArray specialConcat(int dimension, INDArray... toConcat) {
    if (toConcat.length == 1)
        return toConcat[0];

    if (Nd4j.getExecutioner() instanceof GridExecutioner)
        ((GridExecutioner) Nd4j.getExecutioner()).flushQueue();

    PointerPointer shapeInfoPointers = new PointerPointer(toConcat.length);
    PointerPointer dataPointers = new PointerPointer(toConcat.length);

    AtomicAllocator allocator = AtomicAllocator.getInstance();
    CudaContext context = (CudaContext) allocator.getDeviceContext().getContext();


    int sumAlongDim = 0;

    val outputShape = ArrayUtil.copy(toConcat[0].shape());


    for (int i = 0; i < toConcat.length; i++) {
        if (toConcat[i].isCompressed())
            Nd4j.getCompressor().decompressi(toConcat[i]);

        allocator.synchronizeHostData(toConcat[i]);
        shapeInfoPointers.put(i, allocator.getHostPointer(toConcat[i].shapeInfoDataBuffer()));
        dataPointers.put(i, allocator.getHostPointer(toConcat[i].data()));
        sumAlongDim += toConcat[i].size(dimension);

        for (int j = 0; j < toConcat[i].rank(); j++)
            if (j != dimension && toConcat[i].size(j) != outputShape[j]) {
                throw new IllegalArgumentException(
                        "Illegal concatenation at array " + i + " and shape element " + j);
            }
    }

    outputShape[dimension] = sumAlongDim;

    PointerPointer dummy = new PointerPointer(new Pointer[] {null});

    INDArray ret = Nd4j.createUninitialized(outputShape, Nd4j.order());

    if (ret.data().dataType() == DataBuffer.Type.DOUBLE) {
        nativeOps.specialConcatDouble(dummy, dimension, toConcat.length, dataPointers, shapeInfoPointers,
                (DoublePointer) ret.data().addressPointer(),
                (LongPointer) ret.shapeInfoDataBuffer().addressPointer(),
                new PointerPointer(new Pointer[] {null}), new PointerPointer(new Pointer[] {null}));
    } else if (ret.data().dataType() == DataBuffer.Type.FLOAT) {
        nativeOps.specialConcatFloat(dummy, dimension, toConcat.length, dataPointers, shapeInfoPointers,
                (FloatPointer) ret.data().addressPointer(),
                (LongPointer) ret.shapeInfoDataBuffer().addressPointer(),
                new PointerPointer(new Pointer[] {null}), new PointerPointer(new Pointer[] {null}));

    } else if (ret.data().dataType() == DataBuffer.Type.HALF) {
        nativeOps.specialConcatHalf(dummy, dimension, toConcat.length, dataPointers, shapeInfoPointers,
                (ShortPointer) ret.data().addressPointer(),
                (LongPointer) ret.shapeInfoDataBuffer().addressPointer(),
                new PointerPointer(new Pointer[]{null}), new PointerPointer(new Pointer[]{null}));
    } else {
        throw new ND4JIllegalStateException("Unknown dataType: " + ret.data().dataType());
    }

    AllocationPoint point = allocator.getAllocationPoint(ret);

    val perfD = PerformanceTracker.getInstance().helperStartTransaction();

    nativeOps.memcpyAsync(point.getDevicePointer(), point.getHostPointer(), ret.lengthLong() * Nd4j.sizeOfDataType(ret.data().dataType()), CudaConstants.cudaMemcpyHostToDevice, context.getSpecialStream());
    context.getSpecialStream().synchronize();

    PerformanceTracker.getInstance().helperRegisterTransaction(point.getDeviceId(), perfD, point.getNumberOfBytes(), MemcpyDirection.HOST_TO_DEVICE);

    point.tickHostRead();
    point.tickDeviceWrite();

    return ret;
}
 
Example 6
Source File: CpuNDArrayFactory.java    From nd4j with Apache License 2.0 4 votes vote down vote up
/**
 * concatenate ndarrays along a dimension
 *
 * @param dimension the dimension to concatenate along
 * @param toConcat  the ndarrays to concatenate
 * @return the concatenate ndarrays
 */
@Override
public INDArray concat(int dimension, INDArray... toConcat) {
    if (toConcat == null || toConcat.length == 0)
        throw new ND4JIllegalStateException("Can't concatenate 0 arrays");

    if (toConcat.length == 1)
        return toConcat[0];

    // if reusable var wasn't created for this thread, or is smaller then needed - set it to new value
    if (extrazA.get() == null || extrazB.get() == null || extrazSize.get() == null || extrazSize.get() < toConcat.length) {
        extrazA.set(new PointerPointer(toConcat.length));
        extrazB.set(new PointerPointer(toConcat.length));
        extrazSize.set(toConcat.length);
    }

    PointerPointer shapeInfoPointers = extrazA.get();
    PointerPointer dataPointers = extrazB.get();

    int sumAlongDim = 0;

    long[] outputShape = ArrayUtil.copy(toConcat[0].shape());


    for (int i = 0; i < toConcat.length; i++) {
        if (toConcat[i].isCompressed())
            Nd4j.getCompressor().decompressi(toConcat[i]);

        shapeInfoPointers.put(i, toConcat[i].shapeInfoDataBuffer().addressPointer());
        dataPointers.put(i, toConcat[i].data().addressPointer());
        sumAlongDim += toConcat[i].size(dimension);
        for (int j = 0; j < toConcat[i].rank(); j++)
            if (j != dimension && toConcat[i].size(j) != outputShape[j]) {
                throw new IllegalArgumentException(
                        "Illegal concatenation at array " + i + " and shape element " + j);
            }


        //log.info("Shape[{}]: {}", i, Arrays.toString(toConcat[i].shapeInfoDataBuffer().asInt()));
    }

    outputShape[dimension] = sumAlongDim;

    //PointerPointer dummy = new PointerPointer(new Pointer[] {null});

    INDArray ret = Nd4j.createUninitialized(outputShape, Nd4j.order());

    if (ret.data().dataType() == DataBuffer.Type.DOUBLE) {
        nativeOps.concatDouble(null, dimension, toConcat.length, dataPointers, shapeInfoPointers,
                (DoublePointer) ret.data().addressPointer(),
                (LongPointer) ret.shapeInfoDataBuffer().addressPointer(),
                //new PointerPointer(new Pointer[] {null}), new PointerPointer(new Pointer[] {null}));
                null, null);
    } else if (ret.data().dataType() == DataBuffer.Type.FLOAT) {
        nativeOps.concatFloat(null, dimension, toConcat.length, dataPointers, shapeInfoPointers,
                (FloatPointer) ret.data().addressPointer(),
                (LongPointer) ret.shapeInfoDataBuffer().addressPointer(),
                //new PointerPointer(new Pointer[] {null}), new PointerPointer(new Pointer[] {null}));
                null, null);

    } else if (ret.data().dataType() == DataBuffer.Type.HALF) {
        nativeOps.concatHalf(null, dimension, toConcat.length, dataPointers, shapeInfoPointers,
                (ShortPointer) ret.data().addressPointer(),
                (LongPointer) ret.shapeInfoDataBuffer().addressPointer(),
                //new PointerPointer(new Pointer[]{null}), new PointerPointer(new Pointer[]{null}));
                null, null);
    } else {
        throw new ND4JIllegalStateException("Unknown dataType: " + ret.data().dataType());
    }
    return ret;
    // return super.concat(dimension,toConcat);
}
 
Example 7
Source File: BaseNDArray.java    From nd4j with Apache License 2.0 4 votes vote down vote up
@Override
public INDArray reshape(char order, long... newShape) {
    Nd4j.getCompressor().autoDecompress(this);

    if (newShape == null || newShape.length < 1)
        throw new ND4JIllegalStateException(
                "Can't reshape(int...) without shape arguments. Got empty shape instead.");

    // TODO: maybe toFlatten() makes more sense here?
    // reshape(-1) special case
    if (newShape.length == 1 && newShape[0] == -1)
        newShape[0] = this.length();

    int numberNegativesOnes = 0;
    long[] shape = ArrayUtil.copy(newShape);


    for (int i = 0; i < shape.length; i++) {
        if (shape[i] < 0) {
            if (numberNegativesOnes >= 1)
                throw new IllegalArgumentException("Only one dimension can be negative ones. Got shape "
                        + Arrays.toString(newShape));

            numberNegativesOnes++;

            int shapeLength = 1;
            for (int j = 0; j < shape.length; j++)
                if (shape[j] >= 1)
                    shapeLength *= shape[j];
            long realShape = Math.abs(length() / shapeLength);
            long[] thisNewShape = new long[shape.length];
            for (int j = 0; j < shape.length; j++) {
                if (i != j) {
                    thisNewShape[j] = shape[j];
                } else
                    thisNewShape[j] = realShape;
            }

            shape = thisNewShape;
            break;

        }
    }

    long prod = ArrayUtil.prodLong(shape);

    if (prod != this.lengthLong()){
        throw new ND4JIllegalStateException("New shape length doesn't match original length: [" + prod + "] vs [" + this.lengthLong() + "]. Original shape: "+Arrays.toString(this.shape())+" New Shape: "+Arrays.toString(newShape));
    }





    INDArray reshapeAttempt = Shape.newShapeNoCopy(this, shape, order == 'f');
    if (reshapeAttempt != null) {
        // kinda strange get/set usage
        //  reshapeAttempt.setOrder(Shape.getOrder(reshapeAttempt));
        return reshapeAttempt;
    }


    INDArray ret = Nd4j.createUninitialized(shape, order);
    if (order != ordering()) {
        ret.setData(dup(order).data());
    } else
        ret.assign(this);
    return ret;
}