use of com.tencent.angel.ml.math.vector.SparseDoubleVector in project angel by Tencent.
the class GetValueOfIndexTask method sparseDouble.
public void sparseDouble(TaskContext tContext) {
long startGen = System.currentTimeMillis();
int[] index = genIndexs(feaNum, nnz);
long cost = System.currentTimeMillis() - startGen;
LOG.info("Gen index cost: " + cost + " ms.");
try {
MatrixClient sMatClient = tContext.getMatrix(SPARSE_DOUBLE_MAT);
// Set PS Model values
long startInc = System.currentTimeMillis();
SparseDoubleVector delt = new SparseDoubleVector(feaNum);
for (int i = 0; i < feaNum; i++) delt.set(i, (double) i);
sMatClient.increment(0, delt);
sMatClient.clock().get();
long costInc = System.currentTimeMillis() - startInc;
LOG.info("Increment delt cost " + costInc + " ms.");
// Wait for all tasks finish this clock
sMatClient.getTaskContext().globalSync(sMatClient.getMatrixId());
// Get values of index array
long startGet = System.currentTimeMillis();
IndexGetFunc func = new IndexGetFunc(new IndexGetParam(tContext.getMatrix(DENSE_DOUBLE_MAT).getMatrixId(), 0, index));
SparseDoubleVector row = (SparseDoubleVector) ((GetRowResult) sMatClient.get(func)).getRow();
long costGet = System.currentTimeMillis() - startGet;
LOG.info("Get row of indexs cost " + costGet + " ms.");
} catch (Throwable e) {
throw new AngelException(e);
}
}
use of com.tencent.angel.ml.math.vector.SparseDoubleVector in project angel by Tencent.
the class SparseDoubleTask method run.
@Override
public void run(TaskContext taskContext) throws AngelException {
try {
MatrixClient client = taskContext.getMatrix("sparse_double_test");
while (taskContext.getEpoch() < 100) {
long startTs = System.currentTimeMillis();
TVector row = client.getRow(0);
LOG.info("Task " + taskContext.getTaskId() + " in iteration " + taskContext.getEpoch() + " pull use time=" + (System.currentTimeMillis() - startTs) + ", sum=" + sum((SparseDoubleVector) row));
startTs = System.currentTimeMillis();
SparseDoubleVector deltaV = new SparseDoubleVector(2100000000, 150000000);
for (int i = 0; i < 2100000000; i += 20) {
deltaV.set(i, 1.0);
}
deltaV.setMatrixId(client.getMatrixId());
deltaV.setRowId(0);
LOG.info("Task " + taskContext.getTaskId() + " in iteration " + taskContext.getEpoch() + " train use time=" + (System.currentTimeMillis() - startTs));
startTs = System.currentTimeMillis();
client.increment(deltaV);
client.clock().get();
LOG.info("Task " + taskContext.getTaskId() + " in iteration " + taskContext.getEpoch() + " flush use time=" + (System.currentTimeMillis() - startTs));
taskContext.incEpoch();
}
} catch (Throwable x) {
throw new AngelException("run task failed ", x);
}
}
use of com.tencent.angel.ml.math.vector.SparseDoubleVector in project angel by Tencent.
the class DenseDoubleMatrixTest method plusBy1.
@Test
public void plusBy1() throws Exception {
double[][] value = { { 1.0, 2.0 }, { 3.0, 4.0 } };
DenseDoubleMatrix mat = new DenseDoubleMatrix(2, 2, value);
TDoubleVector vec = new DenseDoubleVector(2, new double[] { 1.0, 1.0 });
vec.setRowId(0);
TDoubleVector vec_1 = new SparseDoubleVector(2, new int[] { 1 }, new double[] { 1.0 });
vec_1.setRowId(1);
mat.plusBy(vec);
mat.plusBy(vec_1);
assertEquals(2.0, mat.get(0, 0));
assertEquals(3.0, mat.get(0, 1));
assertEquals(3.0, mat.get(1, 0));
assertEquals(5.0, mat.get(1, 1));
}
use of com.tencent.angel.ml.math.vector.SparseDoubleVector in project angel by Tencent.
the class SparseDoubleMatrixTest method testPlusByGet.
@Test
public void testPlusByGet() {
SparseDoubleMatrix matrix = new SparseDoubleMatrix(2, 2);
matrix.plusBy(0, 0, 1.0);
matrix.plusBy(1, 1, 1.0);
assertEquals(matrix.get(0, 0), 1.0);
assertEquals(matrix.get(0, 1), 0.0);
assertEquals(matrix.get(1, 0), 0.0);
assertEquals(matrix.get(1, 1), 1.0);
matrix.clear();
SparseDoubleVector incVec = new SparseDoubleVector(2);
incVec.set(0, 1);
incVec.set(1, 1);
incVec.setRowId(0);
matrix.plusBy(incVec);
assertEquals(matrix.get(0, 0), 1.0);
assertEquals(matrix.get(0, 1), 1.0);
assertEquals(matrix.get(1, 0), 0.0);
assertEquals(matrix.get(1, 1), 0.0);
matrix.clear();
int[] rowIndexes = { 0, 1 };
int[] colIndexes = { 0, 1 };
double[] values = { 1.0, 1.0 };
matrix.plusBy(rowIndexes, colIndexes, values);
assertEquals(matrix.get(0, 0), 1.0);
assertEquals(matrix.get(0, 1), 0.0);
assertEquals(matrix.get(1, 0), 0.0);
assertEquals(matrix.get(1, 1), 1.0);
matrix.clear();
colIndexes[0] = 0;
colIndexes[1] = 1;
values[0] = 1.0;
values[1] = 1.0;
matrix.plusBy(0, colIndexes, values);
assertEquals(matrix.get(0, 0), 1.0);
assertEquals(matrix.get(0, 1), 1.0);
assertEquals(matrix.get(1, 0), 0.0);
assertEquals(matrix.get(1, 1), 0.0);
SparseDoubleMatrix matrix1 = new SparseDoubleMatrix(2, 2);
matrix.clear();
matrix.plusBy(0, 0, 1.0);
matrix.plusBy(1, 1, 1.0);
matrix1.plusBy(0, 0, 1.0);
matrix1.plusBy(1, 1, 1.0);
matrix.plusBy(matrix1);
assertEquals(matrix.get(0, 0), 2.0);
assertEquals(matrix.get(0, 1), 0.0);
assertEquals(matrix.get(1, 0), 0.0);
assertEquals(matrix.get(1, 1), 2.0);
assertEquals(((SparseDoubleVector) matrix.getRow(0)).get(0), 2.0);
assertEquals(((SparseDoubleVector) matrix.getRow(0)).get(1), 0.0);
assertEquals(((SparseDoubleVector) matrix.getRow(1)).get(0), 0.0);
assertEquals(((SparseDoubleVector) matrix.getRow(1)).get(1), 2.0);
}
use of com.tencent.angel.ml.math.vector.SparseDoubleVector in project angel by Tencent.
the class SparseDoubleMatrix method initVector.
/**
* Init the empty vector
*
* @param rowIndex row index
* @return
*/
@Override
public SparseDoubleVector initVector(int rowIndex) {
SparseDoubleVector ret = new SparseDoubleVector((int) col);
ret.setMatrixId(matrixId);
ret.setRowId(rowIndex);
return ret;
}
Aggregations