use of com.tencent.angel.ml.math2.vector.LongFloatVector in project angel by Tencent.
the class MixedBinaryOutNonZAExecutor method apply.
private static Vector apply(CompLongFloatVector v1, LongIntVector v2, Binary op) {
LongFloatVector[] parts = v1.getPartitions();
Storage[] resParts = StorageSwitch.applyComp(v1, v2, op);
if (v2.isSparse()) {
if (!op.isKeepStorage()) {
for (int i = 0; i < parts.length; i++) {
if (parts[i].getStorage() instanceof LongFloatSortedVectorStorage) {
resParts[i] = new LongFloatSparseVectorStorage(parts[i].getDim(), parts[i].getStorage().getIndices(), parts[i].getStorage().getValues());
}
}
}
long subDim = (v1.getDim() + v1.getNumPartitions() - 1) / v1.getNumPartitions();
ObjectIterator<Long2IntMap.Entry> iter = v2.getStorage().entryIterator();
while (iter.hasNext()) {
Long2IntMap.Entry entry = iter.next();
long gidx = entry.getLongKey();
int pidx = (int) (gidx / subDim);
long subidx = gidx % subDim;
((LongFloatVectorStorage) resParts[pidx]).set(subidx, op.apply(parts[pidx].get(subidx), entry.getIntValue()));
}
} else {
// sorted
if (!op.isKeepStorage()) {
for (int i = 0; i < parts.length; i++) {
if (parts[i].getStorage() instanceof LongFloatSortedVectorStorage) {
resParts[i] = new LongFloatSparseVectorStorage(parts[i].getDim(), parts[i].getStorage().getIndices(), parts[i].getStorage().getValues());
}
}
}
long subDim = (v1.getDim() + v1.getNumPartitions() - 1) / v1.getNumPartitions();
long[] v2Indices = v2.getStorage().getIndices();
int[] v2Values = v2.getStorage().getValues();
for (int i = 0; i < v2Indices.length; i++) {
long gidx = v2Indices[i];
int pidx = (int) (gidx / subDim);
long subidx = gidx % subDim;
((LongFloatVectorStorage) resParts[pidx]).set(subidx, op.apply(parts[pidx].get(subidx), v2Values[i]));
}
}
LongFloatVector[] res = new LongFloatVector[parts.length];
int i = 0;
for (LongFloatVector part : parts) {
res[i] = new LongFloatVector(part.getMatrixId(), part.getRowId(), part.getClock(), part.getDim(), (LongFloatVectorStorage) resParts[i]);
i++;
}
return new CompLongFloatVector(v1.getMatrixId(), v1.getRowId(), v1.getClock(), v1.getDim(), res, v1.getSubDim());
}
use of com.tencent.angel.ml.math2.vector.LongFloatVector in project angel by Tencent.
the class CompReduceExecutor method apply.
private static UnionEle apply(CompLongFloatVector v, ReduceOP op, int start, int end) {
UnionEle res = new UnionEle();
LongFloatVector[] parts = v.getPartitions();
switch(op) {
case Sum:
for (int i = start; i <= end; i++) {
res.setDouble1(res.getDouble1() + parts[i].sum());
}
break;
case Avg:
for (int i = start; i <= end; i++) {
res.setDouble1(res.getDouble1() + parts[i].sum());
res.setLong1(res.getLong1() + parts[i].getDim());
}
break;
case Std:
for (int i = start; i <= end; i++) {
res.setDouble1(res.getDouble1() + parts[i].sum());
double norm = parts[i].norm();
res.setDouble2(res.getDouble2() + norm * norm);
res.setLong1(res.getLong1() + parts[i].getDim());
}
break;
case Norm:
for (int i = start; i <= end; i++) {
double norm = parts[i].norm();
res.setDouble2(res.getDouble2() + norm * norm);
}
break;
case Min:
res.setDouble1(Double.MAX_VALUE);
for (int i = start; i <= end; i++) {
res.setDouble1(Math.min(res.getDouble1(), parts[i].min()));
}
break;
case Max:
res.setDouble1(Double.MIN_VALUE);
for (int i = start; i <= end; i++) {
res.setDouble1(Math.max(res.getDouble1(), parts[i].max()));
}
break;
case Size:
for (int i = start; i <= end; i++) {
res.setLong1(res.getLong1() + parts[i].size());
}
break;
case Numzeros:
for (int i = start; i <= end; i++) {
res.setLong1(res.getLong1() + parts[i].numZeros());
}
break;
}
return res;
}
use of com.tencent.angel.ml.math2.vector.LongFloatVector in project angel by Tencent.
the class UpdatePSFTest method testSparseFloatLongKeyUDF.
public void testSparseFloatLongKeyUDF() throws Exception {
Worker worker = LocalClusterContext.get().getWorker(workerAttempt0Id).getWorker();
MatrixClient client1 = worker.getPSAgent().getMatrixClient(SPARSE_FLOAT_LONG_MAT, 0);
int matrixW1Id = client1.getMatrixId();
long[] index = genLongIndexs(feaNum, nnz);
LongFloatVector deltaVec = new LongFloatVector(feaNum, new LongFloatSparseVectorStorage(feaNum, nnz));
for (int i = 0; i < nnz; i++) {
deltaVec.set(index[i], index[i]);
}
deltaVec.setRowId(0);
Vector[] updates = new Vector[1];
updates[0] = deltaVec;
client1.asyncUpdate(new IncrementRows(new IncrementRowsParam(matrixW1Id, updates))).get();
// client1.clock().get();
LongFloatVector row = (LongFloatVector) client1.getRow(0);
for (long id : index) {
// System.out.println("id=" + id + ", value=" + row.get(id));
Assert.assertTrue(row.get(id) == deltaVec.get(id));
}
// Assert.assertTrue(index.length == row.size());
}
use of com.tencent.angel.ml.math2.vector.LongFloatVector in project angel by Tencent.
the class MatrixRowGetTask method run.
@Override
public void run(TaskContext taskContext) throws AngelException {
try {
MatrixClient matrixClient = taskContext.getMatrix(MatrixRowGetTest.SPARSE_LONGKEY_FLOAT_MAT);
MatrixMeta matrixMeta = PSAgentContext.get().getMatrixMetaManager().getMatrixMeta(MatrixRowGetTest.SPARSE_LONGKEY_FLOAT_MAT);
long[] indices = genLongIndexs(matrixMeta.getColNum(), MatrixRowGetTest.longnnz);
long[] getIndices = genGetLongIndexs(indices, MatrixRowGetTest.nGet);
LongFloatVector deltatest = VFactory.sparseLongKeyFloatVector(matrixMeta.getColNum(), MatrixRowGetTest.longnnz);
for (int i = 0; i < MatrixRowGetTest.longnnz; i++) {
deltatest.set(indices[i], (float) (indices[i] * 1.3));
}
deltatest.setRowId(0);
LOG.info(" delta use " + deltatest.getType() + " type storage");
int updateTime = 0;
long startTs = System.currentTimeMillis();
try {
matrixClient.increment(0, deltatest, true);
long startoneTs = System.currentTimeMillis();
LOG.info("increment time= " + (startoneTs - startTs));
while (true) {
LongFloatVector temp = (LongFloatVector) matrixClient.get(0, getIndices);
updateTime++;
if (updateTime % 10 == 0) {
LOG.info("get times = " + updateTime + ", avg update time=" + (System.currentTimeMillis() - startoneTs) / updateTime);
}
}
} catch (Throwable ie) {
LOG.info("mistake happened in MatrixRowGet try");
}
} catch (InvalidParameterException e) {
LOG.error("get matrix failed ", e);
throw new AngelException(e);
}
}
use of com.tencent.angel.ml.math2.vector.LongFloatVector in project angel by Tencent.
the class MatrixRowGetUDFTask method run.
@Override
public void run(TaskContext taskContext) throws AngelException {
try {
MatrixClient matrixClient = taskContext.getMatrix(MatrixRowGetTest.SPARSE_LONGKEY_FLOAT_MAT);
MatrixMeta matrixMeta = PSAgentContext.get().getMatrixMetaManager().getMatrixMeta(MatrixRowGetTest.SPARSE_LONGKEY_FLOAT_MAT);
long[] indices = genLongIndexs(matrixMeta.getColNum(), MatrixRowGetTest.longnnz);
long[] getIndices = genGetLongIndexs(indices, MatrixRowGetTest.nGet);
LongFloatVector deltatest = VFactory.sparseLongKeyFloatVector(matrixMeta.getColNum(), MatrixRowGetTest.longnnz);
for (int i = 0; i < MatrixRowGetTest.longnnz; i++) {
deltatest.set(indices[i], (float) (indices[i] * 1.3));
}
deltatest.setRowId(0);
LOG.info(" delta use " + deltatest.getType() + " type storage");
int updateTime = 0;
long startTs = System.currentTimeMillis();
try {
matrixClient.increment(0, deltatest, true);
long startoneTs = System.currentTimeMillis();
LOG.info("increment time= " + (startoneTs - startTs));
while (true) {
LongIndexGet funca = new LongIndexGet(new LongIndexGetParam(matrixClient.getMatrixId(), 0, getIndices));
LongFloatVector tempOne = (LongFloatVector) ((GetRowResult) PSAgentContext.get().getUserRequestAdapter().get(funca).get()).getRow();
updateTime++;
if (updateTime % 10 == 0) {
LOG.info("get times = " + updateTime + ", avg update time=" + (System.currentTimeMillis() - startoneTs) / updateTime);
}
}
} catch (Throwable ie) {
LOG.info("mistake happened in MatrixRowGet try");
}
} catch (InvalidParameterException e) {
LOG.error("get matrix failed ", e);
throw new AngelException(e);
}
}
Aggregations