use of com.tencent.angel.ml.math2.storage.LongFloatSparseVectorStorage 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.storage.LongFloatSparseVectorStorage 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.storage.LongFloatSparseVectorStorage in project angel by Tencent.
the class CooLongFloatMatrix method getRow.
@Override
public Vector getRow(int idx) {
LongArrayList cols = new LongArrayList();
FloatArrayList data = new FloatArrayList();
for (int i = 0; i < rowIndices.length; i++) {
if (rowIndices[i] == idx) {
cols.add(colIndices[i]);
data.add(values[i]);
}
}
LongFloatSparseVectorStorage storage = new LongFloatSparseVectorStorage(shape[1], cols.toLongArray(), data.toFloatArray());
return new LongFloatVector(getMatrixId(), idx, getClock(), shape[1], storage);
}
use of com.tencent.angel.ml.math2.storage.LongFloatSparseVectorStorage in project angel by Tencent.
the class CooLongFloatMatrix method getCol.
@Override
public Vector getCol(int idx) {
LongArrayList cols = new LongArrayList();
FloatArrayList data = new FloatArrayList();
for (int i = 0; i < colIndices.length; i++) {
if (colIndices[i] == idx) {
cols.add(rowIndices[i]);
data.add(values[i]);
}
}
LongFloatSparseVectorStorage storage = new LongFloatSparseVectorStorage(shape[0], cols.toLongArray(), data.toFloatArray());
return new LongFloatVector(getMatrixId(), 0, getClock(), shape[0], storage);
}
use of com.tencent.angel.ml.math2.storage.LongFloatSparseVectorStorage in project angel by Tencent.
the class RBLongFloatMatrix method initEmpty.
@Override
public void initEmpty(int idx) {
if (null == rows[idx]) {
LongFloatSparseVectorStorage storage = new LongFloatSparseVectorStorage((long) getDim());
rows[idx] = new LongFloatVector(matrixId, idx, clock, (long) getDim(), storage);
}
}
Aggregations