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, IntLongVector 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<Int2LongMap.Entry> iter = v.getStorage().entryIterator();
while (iter.hasNext()) {
Int2LongMap.Entry entry = iter.next();
int i = entry.getIntKey();
resArr[j] += data[i * c + j] * entry.getLongValue();
}
}
} else {
for (int i = 0; i < r; i++) {
ObjectIterator<Int2LongMap.Entry> iter = v.getStorage().entryIterator();
while (iter.hasNext()) {
Int2LongMap.Entry entry = iter.next();
int j = entry.getIntKey();
resArr[i] += data[i * c + j] * entry.getLongValue();
}
}
}
} else {
// sorted
if (trans) {
for (int j = 0; j < r; j++) {
int[] idxs = v.getStorage().getIndices();
long[] 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();
long[] 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);
}
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, IntDummyVector 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 (trans) {
for (int j = 0; j < c; j++) {
int[] idxs = v.getIndices();
for (int i : idxs) {
resArr[j] += data[i * c + j];
}
}
} else {
for (int i = 0; i < r; i++) {
int[] idxs = v.getIndices();
for (int j : idxs) {
resArr[i] += data[i * c + j];
}
}
}
IntFloatDenseVectorStorage storage = new IntFloatDenseVectorStorage(resArr);
return new IntFloatVector(v.getMatrixId(), v.getClock(), 0, resArr.length, storage);
}
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, IntFloatVector 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 = v.getStorage().getValues();
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<Int2FloatMap.Entry> iter = v.getStorage().entryIterator();
while (iter.hasNext()) {
Int2FloatMap.Entry entry = iter.next();
int i = entry.getIntKey();
resArr[j] += data[i * c + j] * entry.getFloatValue();
}
}
} else {
for (int i = 0; i < r; i++) {
ObjectIterator<Int2FloatMap.Entry> iter = v.getStorage().entryIterator();
while (iter.hasNext()) {
Int2FloatMap.Entry entry = iter.next();
int j = entry.getIntKey();
resArr[i] += data[i * c + j] * entry.getFloatValue();
}
}
}
} else {
// sorted
if (trans) {
for (int j = 0; j < r; j++) {
int[] idxs = v.getStorage().getIndices();
float[] 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();
float[] 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);
}
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, BlasFloatMatrix mat2, boolean trans2) {
float alpha = 1.0f, beta = 0.0f;
float[] 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 float[n * p];
blas.sgemm("T", "T", p, n, m, alpha, mat2.getData(), q, mat1.getData(), n, beta, resBlas, p);
return new BlasFloatMatrix(mat1.getMatrixId(), mat1.getClock(), n, p, resBlas);
} else if (!trans1 && trans2) {
// M1 * M2^T
assert n == q;
resBlas = new float[m * p];
blas.sgemm("T", "N", p, m, n, alpha, mat2.getData(), q, mat1.getData(), n, beta, resBlas, p);
return new BlasFloatMatrix(mat1.getMatrixId(), mat1.getClock(), m, p, resBlas);
} else if (trans1 && !trans2) {
// M1^T * M2
assert m == p;
resBlas = new float[n * q];
blas.sgemm("N", "T", q, n, m, alpha, mat2.getData(), q, mat1.getData(), n, beta, resBlas, q);
return new BlasFloatMatrix(mat1.getMatrixId(), mat1.getClock(), n, q, resBlas);
} else {
// M1 * M2
assert n == p;
resBlas = new float[m * q];
blas.sgemm("N", "N", q, m, n, alpha, mat2.getData(), q, mat1.getData(), n, beta, resBlas, q);
return new BlasFloatMatrix(mat1.getMatrixId(), mat1.getClock(), m, q, resBlas);
}
}
use of com.tencent.angel.ml.math2.matrix.BlasFloatMatrix in project angel by Tencent.
the class DotMatrixExecutor method applyParallel.
private static Matrix applyParallel(BlasFloatMatrix mat1, boolean trans1, BlasFloatMatrix mat2, boolean trans2) {
int m = mat1.getNumRows(), n = mat1.getNumCols();
int p = mat2.getNumRows(), q = mat2.getNumCols();
float[] resBlas;
BlasFloatMatrix retMat;
BlasFloatMatrix transMat1;
MatrixExecutors executors = MatrixExecutors.getInstance();
if (trans1) {
if (trans2) {
assert m == q;
resBlas = new float[n * p];
retMat = new BlasFloatMatrix(mat1.getMatrixId(), mat1.getClock(), n, p, resBlas);
} else {
assert m == p;
resBlas = new float[n * q];
retMat = new BlasFloatMatrix(mat1.getMatrixId(), mat1.getClock(), n, q, resBlas);
}
// Transform mat1, generate a new matrix
transMat1 = new BlasFloatMatrix(mat1.getMatrixId(), mat1.getClock(), n, m, transform(mat1));
} else {
if (trans2) {
assert n == q;
resBlas = new float[m * p];
retMat = new BlasFloatMatrix(mat1.getMatrixId(), mat1.getClock(), m, p, resBlas);
} else {
assert n == p;
resBlas = new float[m * q];
retMat = new BlasFloatMatrix(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;
}
Aggregations