Search in sources :

Example 1 with BlasFloatMatrix

use of com.tencent.angel.ml.math2.matrix.BlasFloatMatrix in project angel by Tencent.

the class BinaryMatrixExecutor method apply.

private static Matrix apply(BlasFloatMatrix mat, IntLongVector v, int idx, boolean onCol, Binary op) {
    float[] data = mat.getData();
    int m = mat.getNumRows(), n = mat.getNumCols();
    int size = v.size();
    byte[] flag = null;
    if (!v.isDense()) {
        flag = new byte[v.getDim()];
    }
    if (onCol && op.isInplace()) {
        if (v.isDense()) {
            long[] values = v.getStorage().getValues();
            for (int i = 0; i < m; i++) {
                data[i * n + idx] = op.apply(data[i * n + idx], values[i]);
            }
        } else if (v.isSparse()) {
            ObjectIterator<Int2LongMap.Entry> iter = v.getStorage().entryIterator();
            while (iter.hasNext()) {
                Int2LongMap.Entry entry = iter.next();
                int i = entry.getIntKey();
                flag[i] = 1;
                data[i * n + idx] = op.apply(data[i * n + idx], entry.getLongValue());
            }
        } else {
            // sorted
            int[] idxs = v.getStorage().getIndices();
            long[] values = v.getStorage().getValues();
            for (int k = 0; k < size; k++) {
                int i = idxs[k];
                flag[i] = 1;
                data[i * n + idx] = op.apply(data[i * n + idx], values[k]);
            }
        }
        if (!v.isDense()) {
            switch(op.getOpType()) {
                case INTERSECTION:
                    for (int i = 0; i < m; i++) {
                        if (flag[i] == 0) {
                            data[i * n + idx] = 0;
                        }
                    }
                case UNION:
                    break;
                case ALL:
                    for (int i = 0; i < m; i++) {
                        if (flag[i] == 0) {
                            data[i * n + idx] = op.apply(data[i * n + idx], 0);
                        }
                    }
            }
        }
        return mat;
    } else if (onCol && !op.isInplace()) {
        float[] newData;
        if (op.getOpType() == INTERSECTION) {
            newData = new float[m * n];
        } else {
            newData = ArrayCopy.copy(data);
        }
        if (v.isDense()) {
            long[] values = v.getStorage().getValues();
            for (int i = 0; i < m; i++) {
                newData[i * n + idx] = op.apply(data[i * n + idx], values[i]);
            }
        } else if (v.isSparse()) {
            ObjectIterator<Int2LongMap.Entry> iter = v.getStorage().entryIterator();
            while (iter.hasNext()) {
                Int2LongMap.Entry entry = iter.next();
                int i = entry.getIntKey();
                flag[i] = 1;
                newData[i * n + idx] = op.apply(data[i * n + idx], entry.getLongValue());
            }
        } else {
            // sorted
            int[] idxs = v.getStorage().getIndices();
            long[] values = v.getStorage().getValues();
            for (int k = 0; k < size; k++) {
                int i = idxs[k];
                flag[i] = 1;
                newData[i * n + idx] = op.apply(data[i * n + idx], values[k]);
            }
        }
        if (!v.isDense()) {
            switch(op.getOpType()) {
                case INTERSECTION:
                    break;
                case UNION:
                    break;
                case ALL:
                    for (int i = 0; i < m; i++) {
                        if (flag[i] == 0) {
                            newData[i * n + idx] = op.apply(data[i * n + idx], 0);
                        }
                    }
            }
        }
        return new BlasFloatMatrix(mat.getMatrixId(), mat.getClock(), m, n, newData);
    } else if (!onCol && op.isInplace()) {
        if (v.isDense()) {
            long[] values = v.getStorage().getValues();
            for (int j = 0; j < n; j++) {
                data[idx * n + j] = op.apply(data[idx * n + j], values[j]);
            }
        } else if (v.isSparse()) {
            ObjectIterator<Int2LongMap.Entry> iter = v.getStorage().entryIterator();
            while (iter.hasNext()) {
                Int2LongMap.Entry entry = iter.next();
                int j = entry.getIntKey();
                flag[j] = 1;
                data[idx * n + j] = op.apply(data[idx * n + j], entry.getLongValue());
            }
        } else {
            // sorted
            int[] idxs = v.getStorage().getIndices();
            long[] values = v.getStorage().getValues();
            for (int k = 0; k < size; k++) {
                int j = idxs[k];
                flag[j] = 1;
                data[idx * n + j] = op.apply(data[idx * n + j], values[k]);
            }
        }
        if (!v.isDense()) {
            switch(op.getOpType()) {
                case INTERSECTION:
                    for (int j = 0; j < n; j++) {
                        if (flag[j] == 0) {
                            data[idx * n + j] = 0;
                        }
                    }
                case UNION:
                    break;
                case ALL:
                    for (int j = 0; j < n; j++) {
                        if (flag[j] == 0) {
                            data[idx * n + j] = op.apply(data[idx * n + j], 0);
                        }
                    }
            }
        }
        return mat;
    } else {
        float[] newData;
        if (op.getOpType() == INTERSECTION) {
            newData = new float[m * n];
        } else {
            newData = ArrayCopy.copy(data);
        }
        if (v.isDense()) {
            long[] values = v.getStorage().getValues();
            for (int j = 0; j < n; j++) {
                newData[idx * n + j] = op.apply(data[idx * n + j], values[j]);
            }
        } else if (v.isSparse()) {
            ObjectIterator<Int2LongMap.Entry> iter = v.getStorage().entryIterator();
            while (iter.hasNext()) {
                Int2LongMap.Entry entry = iter.next();
                int j = entry.getIntKey();
                flag[j] = 1;
                newData[idx * n + j] = op.apply(data[idx * n + j], entry.getLongValue());
            }
        } else {
            // sorted
            int[] idxs = v.getStorage().getIndices();
            long[] values = v.getStorage().getValues();
            for (int k = 0; k < size; k++) {
                int j = idxs[k];
                flag[j] = 1;
                newData[idx * n + j] = op.apply(data[idx * n + j], values[k]);
            }
        }
        if (!v.isDense()) {
            switch(op.getOpType()) {
                case INTERSECTION:
                    break;
                case UNION:
                    break;
                case ALL:
                    for (int j = 0; j < n; j++) {
                        if (flag[j] == 0) {
                            newData[idx * n + j] = op.apply(data[idx * n + j], 0);
                        }
                    }
            }
        }
        return new BlasFloatMatrix(mat.getMatrixId(), mat.getClock(), m, n, newData);
    }
}
Also used : Int2LongMap(it.unimi.dsi.fastutil.ints.Int2LongMap) BlasFloatMatrix(com.tencent.angel.ml.math2.matrix.BlasFloatMatrix) ObjectIterator(it.unimi.dsi.fastutil.objects.ObjectIterator)

Example 2 with BlasFloatMatrix

use of com.tencent.angel.ml.math2.matrix.BlasFloatMatrix in project angel by Tencent.

the class BinaryMatrixExecutor method apply.

private static Matrix apply(BlasFloatMatrix mat, IntDummyVector v, boolean onCol, Binary op) {
    float[] data = mat.getData();
    int m = mat.getNumRows(), n = mat.getNumCols();
    int size = v.size();
    byte[] flag = new byte[v.getDim()];
    if (onCol && op.isInplace()) {
        int[] idxs = v.getIndices();
        for (int k = 0; k < size; k++) {
            int i = idxs[k];
            flag[i] = 1;
            for (int j = 0; j < n; j++) {
                data[i * n + j] = op.apply(data[i * n + j], 1);
            }
        }
        if (op.getOpType() == INTERSECTION) {
            for (int i = 0; i < m; i++) {
                if (flag[i] == 0) {
                    for (int j = 0; j < n; j++) {
                        data[i * n + j] = 0;
                    }
                }
            }
        } else if (op.getOpType() == ALL) {
            for (int i = 0; i < m; i++) {
                if (flag[i] == 0) {
                    for (int j = 0; j < n; j++) {
                        data[i * n + j] = op.apply(data[i * n + j], 0);
                    }
                }
            }
        }
        return mat;
    } else if (onCol && !op.isInplace()) {
        float[] newData;
        if (op.getOpType() == INTERSECTION) {
            newData = new float[m * n];
        } else {
            newData = ArrayCopy.copy(data);
        }
        int[] idxs = v.getIndices();
        for (int k = 0; k < size; k++) {
            int i = idxs[k];
            flag[i] = 1;
            for (int j = 0; j < n; j++) {
                newData[i * n + j] = op.apply(data[i * n + j], 1);
            }
        }
        if (op.getOpType() == ALL) {
            for (int i = 0; i < m; i++) {
                if (flag[i] == 0) {
                    for (int j = 0; j < n; j++) {
                        newData[i * n + j] = op.apply(newData[i * n + j], 0);
                    }
                }
            }
        }
        return new BlasFloatMatrix(mat.getMatrixId(), mat.getClock(), m, n, newData);
    } else if (!onCol && op.isInplace()) {
        int[] idxs = v.getIndices();
        for (int i = 0; i < n; i++) {
            for (int k = 0; k < size; k++) {
                int j = idxs[k];
                flag[j] = 1;
                data[i * n + j] = op.apply(data[i * n + j], 1);
            }
        }
        if (op.getOpType() == INTERSECTION) {
            for (int j = 0; j < n; j++) {
                if (flag[j] == 0) {
                    for (int i = 0; i < m; i++) {
                        data[i * n + j] = 0;
                    }
                }
            }
        } else if (op.getOpType() == ALL) {
            for (int j = 0; j < n; j++) {
                if (flag[j] == 0) {
                    for (int i = 0; i < m; i++) {
                        data[i * n + j] = op.apply(data[i * n + j], 0);
                    }
                }
            }
        }
        return mat;
    } else {
        float[] newData;
        if (op.getOpType() == INTERSECTION) {
            newData = new float[m * n];
        } else {
            newData = ArrayCopy.copy(data);
        }
        int[] idxs = v.getIndices();
        for (int i = 0; i < n; i++) {
            for (int k = 0; k < size; k++) {
                int j = idxs[k];
                flag[j] = 1;
                newData[i * n + j] = op.apply(data[i * n + j], 1);
            }
        }
        if (op.getOpType() == ALL) {
            for (int j = 0; j < n; j++) {
                if (flag[j] == 0) {
                    for (int i = 0; i < m; i++) {
                        newData[i * n + j] = op.apply(newData[i * n + j], 0);
                    }
                }
            }
        }
        return new BlasFloatMatrix(mat.getMatrixId(), mat.getClock(), m, n, newData);
    }
}
Also used : BlasFloatMatrix(com.tencent.angel.ml.math2.matrix.BlasFloatMatrix)

Example 3 with BlasFloatMatrix

use of com.tencent.angel.ml.math2.matrix.BlasFloatMatrix in project angel by Tencent.

the class BinaryMatrixExecutor method apply.

private static Matrix apply(BlasFloatMatrix mat1, boolean trans1, BlasFloatMatrix mat2, boolean trans2, Binary op) {
    float[] mat1Data = mat1.getData();
    float[] mat2Data = mat2.getData();
    int m = mat1.getNumRows(), n = mat1.getNumCols();
    int p = mat2.getNumRows(), q = mat2.getNumCols();
    int size = m * n;
    if (trans1 && trans2) {
        // TT
        float[] newData = new float[size];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                newData[i * m + j] = op.apply(mat1Data[j * n + i], mat2Data[j * q + i]);
            }
        }
        return new BlasFloatMatrix(mat1.getMatrixId(), mat1.getClock(), n, m, newData);
    } else if (!trans1 && trans2) {
        // _T
        if (op.isInplace()) {
            for (int i = 0; i < m; i++) {
                for (int j = 0; j < n; j++) {
                    mat1Data[i * n + j] = op.apply(mat1Data[i * n + j], mat2Data[j * q + i]);
                }
            }
            return mat1;
        } else {
            float[] newData = new float[size];
            for (int i = 0; i < m; i++) {
                for (int j = 0; j < n; j++) {
                    newData[i * n + j] = op.apply(mat1Data[i * n + j], mat2Data[j * q + i]);
                }
            }
            return new BlasFloatMatrix(mat1.getMatrixId(), mat1.getClock(), m, n, newData);
        }
    } else if (trans1 && !trans2) {
        // T_
        float[] newData = new float[size];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                newData[i * m + j] = op.apply(mat1Data[j * n + i], mat2Data[i * q + j]);
            }
        }
        return new BlasFloatMatrix(mat1.getMatrixId(), mat1.getClock(), m, n, newData);
    } else {
        if (op.isInplace()) {
            for (int i = 0; i < size; i++) {
                mat1Data[i] = op.apply(mat1Data[i], mat2Data[i]);
            }
            return mat1;
        } else {
            float[] newData = new float[size];
            for (int i = 0; i < size; i++) {
                newData[i] = op.apply(mat1Data[i], mat2Data[i]);
            }
            return new BlasFloatMatrix(mat1.getMatrixId(), mat1.getClock(), m, n, newData);
        }
    }
}
Also used : BlasFloatMatrix(com.tencent.angel.ml.math2.matrix.BlasFloatMatrix)

Example 4 with BlasFloatMatrix

use of com.tencent.angel.ml.math2.matrix.BlasFloatMatrix in project angel by Tencent.

the class BinaryMatrixExecutor method apply.

private static Matrix apply(BlasFloatMatrix mat, IntLongVector v, boolean onCol, Binary op) {
    float[] data = mat.getData();
    int m = mat.getNumRows(), n = mat.getNumCols();
    int size = v.size();
    byte[] flag = null;
    if (!v.isDense()) {
        flag = new byte[v.getDim()];
    }
    if (onCol && op.isInplace()) {
        if (v.isDense()) {
            long[] values = v.getStorage().getValues();
            for (int i = 0; i < m; i++) {
                long value = values[i];
                for (int j = 0; j < n; j++) {
                    data[i * n + j] = op.apply(data[i * n + j], value);
                }
            }
        } else if (v.isSparse()) {
            ObjectIterator<Int2LongMap.Entry> iter = v.getStorage().entryIterator();
            while (iter.hasNext()) {
                Int2LongMap.Entry entry = iter.next();
                int i = entry.getIntKey();
                flag[i] = 1;
                float value = entry.getLongValue();
                for (int j = 0; j < n; j++) {
                    data[i * n + j] = op.apply(data[i * n + j], value);
                }
            }
        } else {
            // sorted
            int[] idxs = v.getStorage().getIndices();
            long[] values = v.getStorage().getValues();
            for (int k = 0; k < size; k++) {
                int i = idxs[k];
                flag[i] = 1;
                long value = values[k];
                for (int j = 0; j < n; j++) {
                    data[i * n + j] = op.apply(data[i * n + j], value);
                }
            }
        }
        if (!v.isDense()) {
            switch(op.getOpType()) {
                case INTERSECTION:
                    for (int i = 0; i < m; i++) {
                        if (flag[i] == 0) {
                            for (int j = 0; j < n; j++) {
                                data[i * n + j] = 0;
                            }
                        }
                    }
                case UNION:
                    break;
                case ALL:
                    for (int i = 0; i < m; i++) {
                        if (flag[i] == 0) {
                            for (int j = 0; j < n; j++) {
                                data[i * n + j] = op.apply(data[i * n + j], 0);
                            }
                        }
                    }
            }
        }
        return mat;
    } else if (onCol && !op.isInplace()) {
        float[] newData;
        if (op.getOpType() == INTERSECTION) {
            newData = new float[m * n];
        } else {
            newData = ArrayCopy.copy(data);
        }
        if (v.isDense()) {
            long[] values = v.getStorage().getValues();
            for (int i = 0; i < m; i++) {
                long value = values[i];
                for (int j = 0; j < n; j++) {
                    newData[i * n + j] = op.apply(data[i * n + j], value);
                }
            }
        } else if (v.isSparse()) {
            ObjectIterator<Int2LongMap.Entry> iter = v.getStorage().entryIterator();
            while (iter.hasNext()) {
                Int2LongMap.Entry entry = iter.next();
                int i = entry.getIntKey();
                flag[i] = 1;
                float value = entry.getLongValue();
                for (int j = 0; j < n; j++) {
                    newData[i * n + j] = op.apply(data[i * n + j], value);
                }
            }
        } else {
            // sorted
            int[] idxs = v.getStorage().getIndices();
            long[] values = v.getStorage().getValues();
            for (int k = 0; k < size; k++) {
                int i = idxs[k];
                flag[i] = 1;
                long value = values[k];
                for (int j = 0; j < n; j++) {
                    newData[i * n + j] = op.apply(data[i * n + j], value);
                }
            }
        }
        if (!v.isDense()) {
            switch(op.getOpType()) {
                case INTERSECTION:
                    break;
                case UNION:
                    break;
                case ALL:
                    for (int i = 0; i < m; i++) {
                        if (flag[i] == 0) {
                            for (int j = 0; j < n; j++) {
                                newData[i * n + j] = op.apply(data[i * n + j], 0);
                            }
                        }
                    }
            }
        }
        return new BlasFloatMatrix(mat.getMatrixId(), mat.getClock(), m, n, newData);
    } else if (!onCol && op.isInplace()) {
        if (v.isDense()) {
            long[] values = v.getStorage().getValues();
            for (int i = 0; i < m; i++) {
                for (int j = 0; j < n; j++) {
                    data[i * n + j] = op.apply(data[i * n + j], values[j]);
                }
            }
        } else if (v.isSparse()) {
            ObjectIterator<Int2LongMap.Entry> iter = v.getStorage().entryIterator();
            while (iter.hasNext()) {
                Int2LongMap.Entry entry = iter.next();
                int j = entry.getIntKey();
                float value = entry.getLongValue();
                flag[j] = 1;
                for (int i = 0; i < m; i++) {
                    data[i * n + j] = op.apply(data[i * n + j], value);
                }
            }
        } else {
            // sorted
            int[] idxs = v.getStorage().getIndices();
            long[] values = v.getStorage().getValues();
            for (int k = 0; k < size; k++) {
                int j = idxs[k];
                long value = values[k];
                flag[j] = 1;
                for (int i = 0; i < m; i++) {
                    data[i * n + j] = op.apply(data[i * n + j], value);
                }
            }
        }
        if (!v.isDense()) {
            switch(op.getOpType()) {
                case INTERSECTION:
                    for (int j = 0; j < n; j++) {
                        if (flag[j] == 0) {
                            for (int i = 0; i < m; i++) {
                                data[i * n + j] = 0;
                            }
                        }
                    }
                case UNION:
                    break;
                case ALL:
                    for (int j = 0; j < n; j++) {
                        if (flag[j] == 0) {
                            for (int i = 0; i < m; i++) {
                                data[i * n + j] = op.apply(data[i * n + j], 0);
                            }
                        }
                    }
            }
        }
        return mat;
    } else {
        float[] newData;
        if (op.getOpType() == INTERSECTION) {
            newData = new float[m * n];
        } else {
            newData = ArrayCopy.copy(data);
        }
        if (v.isDense()) {
            long[] values = v.getStorage().getValues();
            for (int j = 0; j < n; j++) {
                long value = values[j];
                for (int i = 0; i < m; i++) {
                    newData[i * n + j] = op.apply(data[i * n + j], value);
                }
            }
        } else if (v.isSparse()) {
            ObjectIterator<Int2LongMap.Entry> iter = v.getStorage().entryIterator();
            while (iter.hasNext()) {
                Int2LongMap.Entry entry = iter.next();
                int j = entry.getIntKey();
                flag[j] = 1;
                float value = entry.getLongValue();
                for (int i = 0; i < m; i++) {
                    newData[i * n + j] = op.apply(data[i * n + j], value);
                }
            }
        } else {
            // sorted
            int[] idxs = v.getStorage().getIndices();
            long[] values = v.getStorage().getValues();
            for (int k = 0; k < size; k++) {
                int j = idxs[k];
                flag[j] = 1;
                long value = values[k];
                for (int i = 0; i < m; i++) {
                    newData[i * n + j] = op.apply(data[i * n + j], value);
                }
            }
        }
        if (!v.isDense()) {
            switch(op.getOpType()) {
                case INTERSECTION:
                    break;
                case UNION:
                    break;
                case ALL:
                    for (int j = 0; j < n; j++) {
                        if (flag[j] == 0) {
                            for (int i = 0; i < m; i++) {
                                newData[i * n + j] = op.apply(data[i * n + j], 0);
                            }
                        }
                    }
            }
        }
        return new BlasFloatMatrix(mat.getMatrixId(), mat.getClock(), m, n, newData);
    }
}
Also used : ObjectIterator(it.unimi.dsi.fastutil.objects.ObjectIterator) Int2LongMap(it.unimi.dsi.fastutil.ints.Int2LongMap) BlasFloatMatrix(com.tencent.angel.ml.math2.matrix.BlasFloatMatrix)

Example 5 with BlasFloatMatrix

use of com.tencent.angel.ml.math2.matrix.BlasFloatMatrix in project angel by Tencent.

the class DotMatrixExecutor method apply.

private static Matrix apply(BlasFloatMatrix mat1, boolean trans1, RBIntFloatMatrix mat2, boolean trans2) {
    if (trans1 && !trans2) {
        int outputRows = mat1.getNumCols();
        IntFloatVector[] rows = new IntFloatVector[outputRows];
        for (int i = 0; i < outputRows; i++) {
            Vector col = mat1.getCol(i);
            rows[i] = (IntFloatVector) mat2.transDot(col);
        }
        return MFactory.rbIntFloatMatrix(rows);
    } else if (!trans1 && !trans2) {
        int outputRows = mat1.getNumRows();
        IntFloatVector[] rows = new IntFloatVector[outputRows];
        for (int i = 0; i < outputRows; i++) {
            Vector row = mat1.getRow(i);
            rows[i] = (IntFloatVector) mat2.transDot(row);
        }
        return MFactory.rbIntFloatMatrix(rows);
    } else {
        throw new AngelException("the operation is not supported!");
    }
}
Also used : AngelException(com.tencent.angel.exception.AngelException) IntLongVector(com.tencent.angel.ml.math2.vector.IntLongVector) IntFloatVector(com.tencent.angel.ml.math2.vector.IntFloatVector) LongDoubleVector(com.tencent.angel.ml.math2.vector.LongDoubleVector) Vector(com.tencent.angel.ml.math2.vector.Vector) LongFloatVector(com.tencent.angel.ml.math2.vector.LongFloatVector) IntDoubleVector(com.tencent.angel.ml.math2.vector.IntDoubleVector) IntIntVector(com.tencent.angel.ml.math2.vector.IntIntVector) IntDummyVector(com.tencent.angel.ml.math2.vector.IntDummyVector) IntFloatVector(com.tencent.angel.ml.math2.vector.IntFloatVector)

Aggregations

BlasFloatMatrix (com.tencent.angel.ml.math2.matrix.BlasFloatMatrix)12 ObjectIterator (it.unimi.dsi.fastutil.objects.ObjectIterator)9 IntFloatVector (com.tencent.angel.ml.math2.vector.IntFloatVector)8 IntFloatDenseVectorStorage (com.tencent.angel.ml.math2.storage.IntFloatDenseVectorStorage)4 AngelException (com.tencent.angel.exception.AngelException)3 IntDoubleVector (com.tencent.angel.ml.math2.vector.IntDoubleVector)3 IntDummyVector (com.tencent.angel.ml.math2.vector.IntDummyVector)3 IntIntVector (com.tencent.angel.ml.math2.vector.IntIntVector)3 IntLongVector (com.tencent.angel.ml.math2.vector.IntLongVector)3 LongDoubleVector (com.tencent.angel.ml.math2.vector.LongDoubleVector)3 LongFloatVector (com.tencent.angel.ml.math2.vector.LongFloatVector)3 Vector (com.tencent.angel.ml.math2.vector.Vector)3 Int2FloatMap (it.unimi.dsi.fastutil.ints.Int2FloatMap)3 Int2IntMap (it.unimi.dsi.fastutil.ints.Int2IntMap)3 Int2LongMap (it.unimi.dsi.fastutil.ints.Int2LongMap)3 BlasDoubleMatrix (com.tencent.angel.ml.math2.matrix.BlasDoubleMatrix)2 MatrixExecutors (com.tencent.angel.ml.math2.MatrixExecutors)1 RBIntDoubleMatrix (com.tencent.angel.ml.math2.matrix.RBIntDoubleMatrix)1 RBIntFloatMatrix (com.tencent.angel.ml.math2.matrix.RBIntFloatMatrix)1 RBLongDoubleMatrix (com.tencent.angel.ml.math2.matrix.RBLongDoubleMatrix)1