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);
}
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;
}
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!");
}
}
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);
}
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);
}
}
Aggregations