Search in sources :

Example 1 with BlasDoubleMatrix

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

the class MatrixUtils method rbCompDense2Blas.

public static BlasDoubleMatrix rbCompDense2Blas(RBCompIntDoubleMatrix mat) {
    assert mat != null;
    int dim = (int) mat.getDim();
    int subDim = mat.getSubDim();
    CompIntDoubleVector[] rows = mat.getRows();
    double[] data = new double[rows.length * dim];
    int rowId = 0;
    for (CompIntDoubleVector row : rows) {
        IntDoubleVector[] partitions = row.getPartitions();
        int partId = 0;
        for (IntDoubleVector part : partitions) {
            assert part.isDense();
            double[] src = part.getStorage().getValues();
            System.arraycopy(src, 0, data, rowId * dim + partId * subDim, src.length);
            partId += 1;
        }
        rowId += 1;
    }
    return MFactory.denseDoubleMatrix(rows.length, dim, data);
}
Also used : CompIntDoubleVector(com.tencent.angel.ml.math2.vector.CompIntDoubleVector) CompIntDoubleVector(com.tencent.angel.ml.math2.vector.CompIntDoubleVector) IntDoubleVector(com.tencent.angel.ml.math2.vector.IntDoubleVector)

Example 2 with BlasDoubleMatrix

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

the class DotMatrixExecutor method applyParallel.

private static Matrix applyParallel(BlasDoubleMatrix mat1, boolean trans1, BlasDoubleMatrix mat2, boolean trans2) {
    int m = mat1.getNumRows(), n = mat1.getNumCols();
    int p = mat2.getNumRows(), q = mat2.getNumCols();
    double[] resBlas;
    BlasDoubleMatrix retMat;
    BlasDoubleMatrix transMat1;
    MatrixExecutors executors = MatrixExecutors.getInstance();
    if (trans1) {
        if (trans2) {
            assert m == q;
            resBlas = new double[n * p];
            retMat = new BlasDoubleMatrix(mat1.getMatrixId(), mat1.getClock(), n, p, resBlas);
        } else {
            assert m == p;
            resBlas = new double[n * q];
            retMat = new BlasDoubleMatrix(mat1.getMatrixId(), mat1.getClock(), n, q, resBlas);
        }
        // Transform mat1, generate a new matrix
        transMat1 = new BlasDoubleMatrix(mat1.getMatrixId(), mat1.getClock(), n, m, transform(mat1));
    } else {
        if (trans2) {
            assert n == q;
            resBlas = new double[m * p];
            retMat = new BlasDoubleMatrix(mat1.getMatrixId(), mat1.getClock(), m, p, resBlas);
        } else {
            assert n == p;
            resBlas = new double[m * q];
            retMat = new BlasDoubleMatrix(mat1.getMatrixId(), mat1.getClock(), m, q, resBlas);
        }
        transMat1 = mat1;
    }
    // Split the row indices of mat1Trans
    int subM = Math.max(1, transMat1.getNumRows() / executors.getParallel());
    int[] leftRowOffIndices = splitRowIds(transMat1.getNumRows(), subM);
    // Parallel execute use fork-join
    DotForkJoinOp op = new DotForkJoinOp(transMat1, mat2, retMat, leftRowOffIndices, 0, leftRowOffIndices.length, subM, trans2);
    executors.execute(op);
    op.join();
    return retMat;
}
Also used : MatrixExecutors(com.tencent.angel.ml.math2.MatrixExecutors) BlasDoubleMatrix(com.tencent.angel.ml.math2.matrix.BlasDoubleMatrix)

Example 3 with BlasDoubleMatrix

use of com.tencent.angel.ml.math2.matrix.BlasDoubleMatrix 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 4 with BlasDoubleMatrix

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

the class DotMatrixExecutor method apply.

private static Vector apply(BlasDoubleMatrix mat, boolean trans, IntIntVector v) {
    int m = mat.getNumRows(), n = mat.getNumCols();
    double[] resArr;
    if (trans) {
        assert m == v.getDim();
        resArr = new double[n];
    } else {
        assert n == v.getDim();
        resArr = new double[m];
    }
    int r = mat.getNumRows(), c = mat.getNumCols();
    double[] data = mat.getData();
    if (v.isDense()) {
        double[] tempArray = ArrayCopy.copy(v.getStorage().getValues(), new double[v.getDim()]);
        if (trans) {
            blas.dgemv("N", c, r, 1.0, data, c, tempArray, 1, 0.0, resArr, 1);
        } else {
            blas.dgemv("T", c, r, 1.0, data, c, tempArray, 1, 0.0, 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];
                }
            }
        }
    }
    IntDoubleDenseVectorStorage storage = new IntDoubleDenseVectorStorage(resArr);
    return new IntDoubleVector(v.getMatrixId(), v.getClock(), 0, resArr.length, storage);
}
Also used : IntDoubleDenseVectorStorage(com.tencent.angel.ml.math2.storage.IntDoubleDenseVectorStorage) Int2IntMap(it.unimi.dsi.fastutil.ints.Int2IntMap) ObjectIterator(it.unimi.dsi.fastutil.objects.ObjectIterator) IntDoubleVector(com.tencent.angel.ml.math2.vector.IntDoubleVector)

Example 5 with BlasDoubleMatrix

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

the class DotMatrixExecutor method apply.

private static Matrix apply(BlasDoubleMatrix mat1, boolean trans1, BlasDoubleMatrix mat2, boolean trans2) {
    double alpha = 1.0, beta = 0.0;
    double[] resBlas;
    int m = mat1.getNumRows(), n = mat1.getNumCols();
    int p = mat2.getNumRows(), q = mat2.getNumCols();
    if (trans1 && trans2) {
        // M1^T * M2^T
        assert m == q;
        resBlas = new double[n * p];
        blas.dgemm("T", "T", p, n, m, alpha, mat2.getData(), q, mat1.getData(), n, beta, resBlas, p);
        return new BlasDoubleMatrix(mat1.getMatrixId(), mat1.getClock(), n, p, resBlas);
    } else if (!trans1 && trans2) {
        // M1 * M2^T
        assert n == q;
        resBlas = new double[m * p];
        blas.dgemm("T", "N", p, m, n, alpha, mat2.getData(), q, mat1.getData(), n, beta, resBlas, p);
        return new BlasDoubleMatrix(mat1.getMatrixId(), mat1.getClock(), m, p, resBlas);
    } else if (trans1 && !trans2) {
        // M1^T * M2
        assert m == p;
        resBlas = new double[n * q];
        blas.dgemm("N", "T", q, n, m, alpha, mat2.getData(), q, mat1.getData(), n, beta, resBlas, q);
        return new BlasDoubleMatrix(mat1.getMatrixId(), mat1.getClock(), n, q, resBlas);
    } else {
        // M1 * M2
        assert n == p;
        resBlas = new double[m * q];
        blas.dgemm("N", "N", q, m, n, alpha, mat2.getData(), q, mat1.getData(), n, beta, resBlas, q);
        return new BlasDoubleMatrix(mat1.getMatrixId(), mat1.getClock(), m, q, resBlas);
    }
}
Also used : BlasDoubleMatrix(com.tencent.angel.ml.math2.matrix.BlasDoubleMatrix)

Aggregations

BlasDoubleMatrix (com.tencent.angel.ml.math2.matrix.BlasDoubleMatrix)15 ObjectIterator (it.unimi.dsi.fastutil.objects.ObjectIterator)12 IntDoubleVector (com.tencent.angel.ml.math2.vector.IntDoubleVector)9 IntDoubleDenseVectorStorage (com.tencent.angel.ml.math2.storage.IntDoubleDenseVectorStorage)5 AngelException (com.tencent.angel.exception.AngelException)3 IntDummyVector (com.tencent.angel.ml.math2.vector.IntDummyVector)3 IntFloatVector (com.tencent.angel.ml.math2.vector.IntFloatVector)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 Int2DoubleMap (it.unimi.dsi.fastutil.ints.Int2DoubleMap)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 MatrixExecutors (com.tencent.angel.ml.math2.MatrixExecutors)1 BlasFloatMatrix (com.tencent.angel.ml.math2.matrix.BlasFloatMatrix)1 RBIntDoubleMatrix (com.tencent.angel.ml.math2.matrix.RBIntDoubleMatrix)1 RBIntFloatMatrix (com.tencent.angel.ml.math2.matrix.RBIntFloatMatrix)1