use of com.tencent.angel.ml.math2.vector.IntDoubleVector in project angel by Tencent.
the class BinaryMatrixExecutor method apply.
private static Matrix apply(BlasDoubleMatrix mat, IntDoubleVector v, int idx, boolean onCol, Binary op) {
double[] 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()) {
double[] 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<Int2DoubleMap.Entry> iter = v.getStorage().entryIterator();
while (iter.hasNext()) {
Int2DoubleMap.Entry entry = iter.next();
int i = entry.getIntKey();
flag[i] = 1;
data[i * n + idx] = op.apply(data[i * n + idx], entry.getDoubleValue());
}
} else {
// sorted
int[] idxs = v.getStorage().getIndices();
double[] 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()) {
double[] newData;
if (op.getOpType() == INTERSECTION) {
newData = new double[m * n];
} else {
newData = ArrayCopy.copy(data);
}
if (v.isDense()) {
double[] 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<Int2DoubleMap.Entry> iter = v.getStorage().entryIterator();
while (iter.hasNext()) {
Int2DoubleMap.Entry entry = iter.next();
int i = entry.getIntKey();
flag[i] = 1;
newData[i * n + idx] = op.apply(data[i * n + idx], entry.getDoubleValue());
}
} else {
// sorted
int[] idxs = v.getStorage().getIndices();
double[] 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 BlasDoubleMatrix(mat.getMatrixId(), mat.getClock(), m, n, newData);
} else if (!onCol && op.isInplace()) {
if (v.isDense()) {
double[] 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<Int2DoubleMap.Entry> iter = v.getStorage().entryIterator();
while (iter.hasNext()) {
Int2DoubleMap.Entry entry = iter.next();
int j = entry.getIntKey();
flag[j] = 1;
data[idx * n + j] = op.apply(data[idx * n + j], entry.getDoubleValue());
}
} else {
// sorted
int[] idxs = v.getStorage().getIndices();
double[] 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 {
double[] newData;
if (op.getOpType() == INTERSECTION) {
newData = new double[m * n];
} else {
newData = ArrayCopy.copy(data);
}
if (v.isDense()) {
double[] 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<Int2DoubleMap.Entry> iter = v.getStorage().entryIterator();
while (iter.hasNext()) {
Int2DoubleMap.Entry entry = iter.next();
int j = entry.getIntKey();
flag[j] = 1;
newData[idx * n + j] = op.apply(data[idx * n + j], entry.getDoubleValue());
}
} else {
// sorted
int[] idxs = v.getStorage().getIndices();
double[] 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 BlasDoubleMatrix(mat.getMatrixId(), mat.getClock(), m, n, newData);
}
}
use of com.tencent.angel.ml.math2.vector.IntDoubleVector in project angel by Tencent.
the class UpdatePSFTest method testSparseDoubleUDF.
public void testSparseDoubleUDF() throws Exception {
Worker worker = LocalClusterContext.get().getWorker(workerAttempt0Id).getWorker();
MatrixClient client1 = worker.getPSAgent().getMatrixClient(SPARSE_DOUBLE_MAT, 0);
int matrixW1Id = client1.getMatrixId();
// genIndexs(feaNum, nnz);
int[] index = new int[feaNum];
for (int i = 0; i < index.length; i++) {
index[i] = i;
}
IntDoubleVector deltaVec = new IntDoubleVector(feaNum, new IntDoubleSparseVectorStorage(feaNum, nnz));
for (int i = 0; i < index.length; i++) {
deltaVec.set(index[i], index[i]);
}
// for (int i = 0; i < feaNum; i++) {
// deltaVec.set(i, i);
// }
deltaVec.setRowId(0);
Vector[] updates = new Vector[1];
updates[0] = deltaVec;
client1.asyncUpdate(new IncrementRows(new IncrementRowsParam(matrixW1Id, updates))).get();
IntDoubleVector row = (IntDoubleVector) client1.getRow(0);
for (int id : index) {
// System.out.println("id=" + id + ", value=" + row.get(id));
Assert.assertEquals(row.get(id), deltaVec.get(id), 0);
}
Assert.assertTrue(index.length == row.size());
}
use of com.tencent.angel.ml.math2.vector.IntDoubleVector in project angel by Tencent.
the class IndexGetRowTask method run.
@Override
public void run(TaskContext taskContext) throws AngelException {
try {
MatrixClient matrixClient = taskContext.getMatrix(IndexGetRowTest.DENSE_DOUBLE_MAT);
MatrixMeta matrixMeta = PSAgentContext.get().getMatrixMetaManager().getMatrixMeta(IndexGetRowTest.DENSE_DOUBLE_MAT);
int[] indices = genIndexs((int) matrixMeta.getColNum(), IndexGetRowTest.nnz);
IntDoubleVector delta = VFactory.sparseDoubleVector((int) matrixMeta.getColNum(), IndexGetRowTest.nnz);
for (int i = 0; i < IndexGetRowTest.nnz; i++) {
delta.set(indices[i], indices[i]);
}
IntDoubleVector delta1 = VFactory.denseDoubleVector((int) matrixMeta.getColNum());
for (int i = 0; i < IndexGetRowTest.colNum; i++) {
delta1.set(i, i);
}
LOG.info("delta use " + delta.getType() + " type storage");
int testNum = 500;
long startTs = System.currentTimeMillis();
LOG.info("for sparse delta type");
conf.setBoolean("use.new.split", false);
for (int i = 0; i < testNum; i++) {
matrixClient.increment(0, delta, true);
if (i > 0 && i % 10 == 0) {
LOG.info("increment old use time = " + (System.currentTimeMillis() - startTs) / i);
}
}
conf.setBoolean("use.new.split", true);
startTs = System.currentTimeMillis();
for (int i = 0; i < testNum; i++) {
matrixClient.increment(0, delta, true);
// IntDoubleVector vector = (IntDoubleVector) ((GetRowResult) matrixClient.get(func)).getRow();
if (i > 0 && i % 10 == 0) {
LOG.info("increment new use time = " + (System.currentTimeMillis() - startTs) / i);
}
}
LOG.info("for dense delta type");
conf.setBoolean("use.new.split", false);
for (int i = 0; i < testNum; i++) {
matrixClient.increment(0, delta1, true);
if (i > 0 && i % 10 == 0) {
LOG.info("increment old use time = " + (System.currentTimeMillis() - startTs) / i);
}
}
conf.setBoolean("use.new.split", true);
startTs = System.currentTimeMillis();
for (int i = 0; i < testNum; i++) {
matrixClient.increment(0, delta1, true);
// IntDoubleVector vector = (IntDoubleVector) ((GetRowResult) matrixClient.get(func)).getRow();
if (i > 0 && i % 10 == 0) {
LOG.info("increment new use time = " + (System.currentTimeMillis() - startTs) / i);
}
}
} catch (InvalidParameterException e) {
LOG.error("get matrix failed ", e);
throw new AngelException(e);
}
}
use of com.tencent.angel.ml.math2.vector.IntDoubleVector in project angel by Tencent.
the class RBCompLongDoubleMatrix method diag.
@Override
public Vector diag() {
double[] resArr = new double[rows.length];
for (int i = 0; i < rows.length; i++) {
if (null == rows[i]) {
resArr[i] = 0;
} else {
resArr[i] = rows[i].get(i);
}
}
IntDoubleDenseVectorStorage storage = new IntDoubleDenseVectorStorage(resArr);
return new IntDoubleVector(getMatrixId(), 0, getClock(), resArr.length, storage);
}
use of com.tencent.angel.ml.math2.vector.IntDoubleVector in project angel by Tencent.
the class RBIntDoubleMatrix method diag.
@Override
public Vector diag() {
double[] resArr = new double[rows.length];
for (int i = 0; i < rows.length; i++) {
if (null == rows[i]) {
resArr[i] = 0;
} else {
resArr[i] = rows[i].get(i);
}
}
IntDoubleDenseVectorStorage storage = new IntDoubleDenseVectorStorage(resArr);
return new IntDoubleVector(getMatrixId(), 0, getClock(), resArr.length, storage);
}
Aggregations