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

The following examples show how to use org.nd4j.linalg.util.ArrayUtil#calcStridesFortran() . 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: OpExecutionerUtil.java    From nd4j with Apache License 2.0 6 votes vote down vote up
/** Can we do the op (X = Op(X)) directly on the arrays without breaking X up into 1d tensors first?
 * In general, this is possible if the elements of X are contiguous in the buffer, OR if every element
 * of X is at position offset+i*elementWiseStride in the buffer
 * */
public static boolean canDoOpDirectly(INDArray x) {
    if (x.elementWiseStride() < 1)
        return false;
    if (x.isVector())
        return true;

    //For a single NDArray all we require is that the elements are contiguous in the buffer or every nth element

    //Full buffer -> implies all elements are contiguous (and match)
    long l1 = x.lengthLong();
    long dl1 = x.data().length();
    if (l1 == dl1)
        return true;

    //Strides are same as a zero offset NDArray -> all elements are contiguous (even if not offset 0)
    long[] shape1 = x.shape();
    long[] stridesAsInit =
                    (x.ordering() == 'c' ? ArrayUtil.calcStrides(shape1) : ArrayUtil.calcStridesFortran(shape1));
    boolean stridesSameAsInit = Arrays.equals(x.stride(), stridesAsInit);
    return stridesSameAsInit;
}
 
Example 2
Source File: Shape.java    From nd4j with Apache License 2.0 6 votes vote down vote up
/** Are the elements in the buffer contiguous for this NDArray? */
public static boolean isContiguousInBuffer(INDArray in) {
    long length = in.length();
    long dLength = in.data().length();
    if (length == dLength)
        return true; //full buffer, always contiguous

    char order = in.ordering();

    long[] shape = in.shape();
    long[] stridesIfContiguous;
    if (order == 'f') {
        stridesIfContiguous = ArrayUtil.calcStridesFortran(shape);
    } else if (order == 'c') {
        stridesIfContiguous = ArrayUtil.calcStrides(shape);
    } else if (order == 'a') {
        stridesIfContiguous = new long[] {1, 1};
    } else {
        throw new RuntimeException("Invalid order: not c or f (is: " + order + ")");
    }

    return Arrays.equals(in.stride(), stridesIfContiguous);
}
 
Example 3
Source File: JCublasNDArrayFactory.java    From nd4j with Apache License 2.0 5 votes vote down vote up
/**
 * Creates an ndarray with the specified shape
 *
 * @param list
 * @param shape the shape of the ndarray
 * @return the instance
 */
@Override
public INDArray create(List<INDArray> list, int[] shape) {
    if (order == FORTRAN)
        return new JCublasNDArray(list, shape, ArrayUtil.calcStridesFortran(shape));
    else
        return new JCublasNDArray(list, shape);
}
 
Example 4
Source File: OpExecutionerUtil.java    From nd4j with Apache License 2.0 5 votes vote down vote up
/** Can we do the transform op (X = Op(X,Y)) directly on the arrays without breaking them up into 1d tensors first? */
public static boolean canDoOpDirectly(INDArray x, INDArray y) {
    if (x.isVector())
        return true;
    if (x.ordering() != y.ordering())
        return false; //other than vectors, elements in f vs. c NDArrays will never line up
    if (x.elementWiseStride() < 1 || y.elementWiseStride() < 1)
        return false;
    //Full buffer + matching strides -> implies all elements are contiguous (and match)
    //Need strides to match, otherwise elements in buffer won't line up (i.e., c vs. f order arrays)
    long l1 = x.lengthLong();
    long dl1 = x.data().length();
    long l2 = y.lengthLong();
    long dl2 = y.data().length();
    long[] strides1 = x.stride();
    long[] strides2 = y.stride();
    boolean equalStrides = Arrays.equals(strides1, strides2);
    if (l1 == dl1 && l2 == dl2 && equalStrides)
        return true;

    //Strides match + are same as a zero offset NDArray -> all elements are contiguous (and match)
    if (equalStrides) {
        long[] shape1 = x.shape();
        long[] stridesAsInit = (x.ordering() == 'c' ? ArrayUtil.calcStrides(shape1)
                        : ArrayUtil.calcStridesFortran(shape1));
        boolean stridesSameAsInit = Arrays.equals(strides1, stridesAsInit);
        return stridesSameAsInit;
    }

    return false;
}
 
Example 5
Source File: OpExecutionerUtil.java    From nd4j with Apache License 2.0 5 votes vote down vote up
/** Can we do the transform op (Z = Op(X,Y)) directly on the arrays without breaking them up into 1d tensors first? */
public static boolean canDoOpDirectly(INDArray x, INDArray y, INDArray z) {
    if (x.isVector())
        return true;
    if (x.ordering() != y.ordering() || x.ordering() != z.ordering())
        return false; //other than vectors, elements in f vs. c NDArrays will never line up
    if (x.elementWiseStride() < 1 || y.elementWiseStride() < 1)
        return false;
    //Full buffer + matching strides -> implies all elements are contiguous (and match)
    long l1 = x.lengthLong();
    long dl1 = x.data().length();
    long l2 = y.lengthLong();
    long dl2 = y.data().length();
    long l3 = z.lengthLong();
    long dl3 = z.data().length();
    long[] strides1 = x.stride();
    long[] strides2 = y.stride();
    long[] strides3 = z.stride();
    boolean equalStrides = Arrays.equals(strides1, strides2) && Arrays.equals(strides1, strides3);
    if (l1 == dl1 && l2 == dl2 && l3 == dl3 && equalStrides)
        return true;

    //Strides match + are same as a zero offset NDArray -> all elements are contiguous (and match)
    if (equalStrides) {
        long[] shape1 = x.shape();
        long[] stridesAsInit = (x.ordering() == 'c' ? ArrayUtil.calcStrides(shape1)
                        : ArrayUtil.calcStridesFortran(shape1));
        boolean stridesSameAsInit = Arrays.equals(strides1, stridesAsInit);
        return stridesSameAsInit;
    }

    return false;
}
 
Example 6
Source File: Shape.java    From nd4j with Apache License 2.0 5 votes vote down vote up
public static boolean hasDefaultStridesForShape(INDArray input){
    if(!strideDescendingCAscendingF(input)){
        return false;
    }
    char order = input.ordering();
    long[] defaultStrides;
    if(order == 'f'){
        defaultStrides = ArrayUtil.calcStridesFortran(input.shape());
    } else {
        defaultStrides = ArrayUtil.calcStrides(input.shape());
    }
    return Arrays.equals(input.stride(), defaultStrides);
}