Search in sources :

Example 11 with BlasFloatMatrix

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

the class DotMatrixExecutor method apply.

public static Matrix apply(Matrix mat1, boolean trans1, Matrix mat2, boolean trans2, Boolean parallel) {
    if (mat1 instanceof BlasDoubleMatrix && mat2 instanceof BlasDoubleMatrix) {
        if (parallel) {
            return applyParallel((BlasDoubleMatrix) mat1, trans1, (BlasDoubleMatrix) mat2, trans2);
        } else {
            return apply((BlasDoubleMatrix) mat1, trans1, (BlasDoubleMatrix) mat2, trans2);
        }
    } else if (mat1 instanceof BlasFloatMatrix && mat2 instanceof BlasFloatMatrix) {
        if (parallel) {
            return applyParallel((BlasFloatMatrix) mat1, trans1, (BlasFloatMatrix) mat2, trans2);
        } else {
            return apply((BlasFloatMatrix) mat1, trans1, (BlasFloatMatrix) mat2, trans2);
        }
    } else if (mat1 instanceof BlasDoubleMatrix && mat2 instanceof RBIntDoubleMatrix) {
        return apply((BlasDoubleMatrix) mat1, trans1, (RBIntDoubleMatrix) mat2, trans2);
    } else if (mat1 instanceof BlasDoubleMatrix && mat2 instanceof RBLongDoubleMatrix) {
        return apply((BlasDoubleMatrix) mat1, trans1, (RBLongDoubleMatrix) mat2, trans2);
    } else if (mat1 instanceof BlasFloatMatrix && mat2 instanceof RBIntFloatMatrix) {
        return apply((BlasFloatMatrix) mat1, trans1, (RBIntFloatMatrix) mat2, trans2);
    } else if (mat1 instanceof BlasFloatMatrix && mat2 instanceof RBLongFloatMatrix) {
        return apply((BlasFloatMatrix) mat1, trans1, (RBLongFloatMatrix) mat2, trans2);
    } else if (mat1 instanceof RBIntDoubleMatrix && mat2 instanceof BlasDoubleMatrix) {
        return apply((RBIntDoubleMatrix) mat1, trans1, (BlasDoubleMatrix) mat2, trans2);
    } else if (mat1 instanceof RBIntFloatMatrix && mat2 instanceof BlasFloatMatrix) {
        return apply((RBIntFloatMatrix) mat1, trans1, (BlasFloatMatrix) mat2, trans2);
    } else if (mat1 instanceof RowBasedMatrix && mat2 instanceof RowBasedMatrix) {
        if (!trans1 && trans2) {
            int outputRow = mat1.getNumRows();
            int outputCol = mat2.getNumRows();
            RowType type1 = mat1.getRow(0).getStorage().getType();
            RowType type2 = mat2.getRow(0).getStorage().getType();
            if (type1.isDouble() && type2.isDouble()) {
                BlasDoubleMatrix res = MFactory.denseDoubleMatrix(outputRow, outputCol);
                for (int i = 0; i < outputCol; i++) {
                    Vector row = mat2.getRow(i);
                    Vector col = mat1.dot(row);
                    res.setCol(i, col);
                }
                return res;
            } else if (type1.isFloat() && type2.isFloat()) {
                BlasFloatMatrix res = MFactory.denseFloatMatrix(outputRow, outputCol);
                for (int i = 0; i < outputCol; i++) {
                    Vector row = mat2.getRow(i);
                    Vector col = mat1.dot(row);
                    res.setCol(i, col);
                }
                return res;
            } else {
                throw new AngelException("the operation is not supported!");
            }
        } else {
            throw new AngelException("the operation is not supported!");
        }
    } else {
        throw new AngelException("the operation is not supported!");
    }
}
Also used : RBLongDoubleMatrix(com.tencent.angel.ml.math2.matrix.RBLongDoubleMatrix) AngelException(com.tencent.angel.exception.AngelException) RBLongFloatMatrix(com.tencent.angel.ml.math2.matrix.RBLongFloatMatrix) RBIntFloatMatrix(com.tencent.angel.ml.math2.matrix.RBIntFloatMatrix) RowBasedMatrix(com.tencent.angel.ml.math2.matrix.RowBasedMatrix) RBIntDoubleMatrix(com.tencent.angel.ml.math2.matrix.RBIntDoubleMatrix) RowType(com.tencent.angel.ml.matrix.RowType) BlasDoubleMatrix(com.tencent.angel.ml.math2.matrix.BlasDoubleMatrix) BlasFloatMatrix(com.tencent.angel.ml.math2.matrix.BlasFloatMatrix) 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)

Example 12 with BlasFloatMatrix

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

the class DotMatrixExecutor method apply.

private static Vector apply(BlasFloatMatrix mat, boolean trans, IntIntVector v) {
    int m = mat.getNumRows(), n = mat.getNumCols();
    float[] resArr;
    if (trans) {
        assert m == v.getDim();
        resArr = new float[n];
    } else {
        assert n == v.getDim();
        resArr = new float[m];
    }
    int r = mat.getNumRows(), c = mat.getNumCols();
    float[] data = mat.getData();
    if (v.isDense()) {
        float[] tempArray = ArrayCopy.copy(v.getStorage().getValues(), new float[v.getDim()]);
        if (trans) {
            blas.sgemv("N", c, r, 1.0f, data, c, tempArray, 1, 0.0f, resArr, 1);
        } else {
            blas.sgemv("T", c, r, 1.0f, data, c, tempArray, 1, 0.0f, resArr, 1);
        }
    } else if (v.isSparse()) {
        if (trans) {
            for (int j = 0; j < c; j++) {
                ObjectIterator<Int2IntMap.Entry> iter = v.getStorage().entryIterator();
                while (iter.hasNext()) {
                    Int2IntMap.Entry entry = iter.next();
                    int i = entry.getIntKey();
                    resArr[j] += data[i * c + j] * entry.getIntValue();
                }
            }
        } else {
            for (int i = 0; i < r; i++) {
                ObjectIterator<Int2IntMap.Entry> iter = v.getStorage().entryIterator();
                while (iter.hasNext()) {
                    Int2IntMap.Entry entry = iter.next();
                    int j = entry.getIntKey();
                    resArr[i] += data[i * c + j] * entry.getIntValue();
                }
            }
        }
    } else {
        // sorted
        if (trans) {
            for (int j = 0; j < r; j++) {
                int[] idxs = v.getStorage().getIndices();
                int[] vals = v.getStorage().getValues();
                for (int k = 0; k < idxs.length; k++) {
                    resArr[j] += data[idxs[k] * c + j] * vals[k];
                }
            }
        } else {
            for (int i = 0; i < r; i++) {
                int[] idxs = v.getStorage().getIndices();
                int[] vals = v.getStorage().getValues();
                for (int k = 0; k < idxs.length; k++) {
                    resArr[i] += data[i * c + idxs[k]] * vals[k];
                }
            }
        }
    }
    IntFloatDenseVectorStorage storage = new IntFloatDenseVectorStorage(resArr);
    return new IntFloatVector(v.getMatrixId(), v.getClock(), 0, resArr.length, storage);
}
Also used : IntFloatDenseVectorStorage(com.tencent.angel.ml.math2.storage.IntFloatDenseVectorStorage) Int2IntMap(it.unimi.dsi.fastutil.ints.Int2IntMap) IntFloatVector(com.tencent.angel.ml.math2.vector.IntFloatVector) ObjectIterator(it.unimi.dsi.fastutil.objects.ObjectIterator)

Example 13 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, RBLongFloatMatrix mat2, boolean trans2) {
    if (trans1 && !trans2) {
        int outputRows = mat1.getNumCols();
        LongFloatVector[] rows = new LongFloatVector[outputRows];
        for (int i = 0; i < outputRows; i++) {
            Vector col = mat1.getCol(i);
            rows[i] = (LongFloatVector) mat2.transDot(col);
        }
        return MFactory.rbLongFloatMatrix(rows);
    } else if (!trans1 && !trans2) {
        int outputRows = mat1.getNumRows();
        LongFloatVector[] rows = new LongFloatVector[outputRows];
        for (int i = 0; i < outputRows; i++) {
            Vector row = mat1.getRow(i);
            rows[i] = (LongFloatVector) mat2.transDot(row);
        }
        return MFactory.rbLongFloatMatrix(rows);
    } else {
        throw new AngelException("the operation is not supported!");
    }
}
Also used : AngelException(com.tencent.angel.exception.AngelException) LongFloatVector(com.tencent.angel.ml.math2.vector.LongFloatVector) 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)

Example 14 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, IntIntVector 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()) {
            int[] 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<Int2IntMap.Entry> iter = v.getStorage().entryIterator();
            while (iter.hasNext()) {
                Int2IntMap.Entry entry = iter.next();
                int i = entry.getIntKey();
                flag[i] = 1;
                data[i * n + idx] = op.apply(data[i * n + idx], entry.getIntValue());
            }
        } else {
            // sorted
            int[] idxs = v.getStorage().getIndices();
            int[] 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()) {
            int[] 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<Int2IntMap.Entry> iter = v.getStorage().entryIterator();
            while (iter.hasNext()) {
                Int2IntMap.Entry entry = iter.next();
                int i = entry.getIntKey();
                flag[i] = 1;
                newData[i * n + idx] = op.apply(data[i * n + idx], entry.getIntValue());
            }
        } else {
            // sorted
            int[] idxs = v.getStorage().getIndices();
            int[] 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()) {
            int[] 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<Int2IntMap.Entry> iter = v.getStorage().entryIterator();
            while (iter.hasNext()) {
                Int2IntMap.Entry entry = iter.next();
                int j = entry.getIntKey();
                flag[j] = 1;
                data[idx * n + j] = op.apply(data[idx * n + j], entry.getIntValue());
            }
        } else {
            // sorted
            int[] idxs = v.getStorage().getIndices();
            int[] 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()) {
            int[] 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<Int2IntMap.Entry> iter = v.getStorage().entryIterator();
            while (iter.hasNext()) {
                Int2IntMap.Entry entry = iter.next();
                int j = entry.getIntKey();
                flag[j] = 1;
                newData[idx * n + j] = op.apply(data[idx * n + j], entry.getIntValue());
            }
        } else {
            // sorted
            int[] idxs = v.getStorage().getIndices();
            int[] 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 : BlasFloatMatrix(com.tencent.angel.ml.math2.matrix.BlasFloatMatrix) Int2IntMap(it.unimi.dsi.fastutil.ints.Int2IntMap) ObjectIterator(it.unimi.dsi.fastutil.objects.ObjectIterator)

Example 15 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, int idx, 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;
            data[i * n + idx] = op.apply(data[i * n + idx], 1);
        }
        if (op.getOpType() == INTERSECTION) {
            for (int i = 0; i < m; i++) {
                if (flag[i] == 0) {
                    data[i * n + idx] = 0;
                }
            }
        } else if (op.getOpType() == 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 = ArrayCopy.copy(data);
        int[] idxs = v.getIndices();
        for (int k = 0; k < size; k++) {
            int i = idxs[k];
            flag[i] = 1;
            newData[i * n + idx] = op.apply(data[i * n + idx], 1);
        }
        if (op.getOpType() == INTERSECTION) {
            for (int i = 0; i < m; i++) {
                if (flag[i] == 0) {
                    newData[i * n + idx] = 0;
                }
            }
        } else if (op.getOpType() == ALL) {
            for (int i = 0; i < m; i++) {
                if (flag[i] == 0) {
                    newData[i * n + idx] = op.apply(newData[i * n + idx], 0);
                }
            }
        }
        return new BlasFloatMatrix(mat.getMatrixId(), mat.getClock(), m, n, newData);
    } else if (!onCol && op.isInplace()) {
        int[] idxs = v.getIndices();
        for (int k = 0; k < size; k++) {
            int j = idxs[k];
            flag[j] = 1;
            data[idx * n + j] = op.apply(data[idx * n + j], 1);
        }
        if (op.getOpType() == INTERSECTION) {
            for (int j = 0; j < n; j++) {
                if (flag[j] == 0) {
                    data[idx * n + j] = 0;
                }
            }
        } else if (op.getOpType() == 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 = ArrayCopy.copy(data);
        int[] idxs = v.getIndices();
        for (int k = 0; k < size; k++) {
            int j = idxs[k];
            flag[j] = 1;
            newData[idx * n + j] = op.apply(data[idx * n + j], 1);
        }
        if (op.getOpType() == INTERSECTION) {
            for (int j = 0; j < n; j++) {
                if (flag[j] == 0) {
                    newData[idx * n + j] = 0;
                }
            }
        } else if (op.getOpType() == ALL) {
            for (int j = 0; j < n; j++) {
                if (flag[j] == 0) {
                    newData[idx * n + j] = op.apply(newData[idx * n + j], 0);
                }
            }
        }
        return new BlasFloatMatrix(mat.getMatrixId(), mat.getClock(), m, n, newData);
    }
}
Also used : BlasFloatMatrix(com.tencent.angel.ml.math2.matrix.BlasFloatMatrix)

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