use of com.tencent.angel.ml.math2.vector.LongIntVector in project angel by Tencent.
the class MixedBinaryOutAllExecutor method apply.
private static Vector apply(CompLongDoubleVector v1, LongIntVector v2, Binary op) {
LongDoubleVector[] parts = v1.getPartitions();
Storage[] resParts = StorageSwitch.applyComp(v1, v2, op);
if (!op.isKeepStorage()) {
for (int i = 0; i < parts.length; i++) {
if (parts[i].getStorage() instanceof LongDoubleSortedVectorStorage) {
resParts[i] = new LongDoubleSparseVectorStorage(parts[i].getDim(), parts[i].getStorage().getIndices(), parts[i].getStorage().getValues());
}
}
}
long subDim = (v1.getDim() + v1.getNumPartitions() - 1) / v1.getNumPartitions();
for (int i = 0; i < v1.getDim(); i++) {
int pidx = (int) (i / subDim);
long subidx = i % subDim;
if (v2.getStorage().hasKey(i)) {
((LongDoubleVectorStorage) resParts[pidx]).set(subidx, op.apply(parts[pidx].get(subidx), v2.get(i)));
} else {
((LongDoubleVectorStorage) resParts[pidx]).set(subidx, op.apply(parts[pidx].get(subidx), 0));
}
}
LongDoubleVector[] res = new LongDoubleVector[parts.length];
int i = 0;
for (LongDoubleVector part : parts) {
res[i] = new LongDoubleVector(part.getMatrixId(), part.getRowId(), part.getClock(), part.getDim(), (LongDoubleVectorStorage) resParts[i]);
i++;
}
return new CompLongDoubleVector(v1.getMatrixId(), v1.getRowId(), v1.getClock(), v1.getDim(), res, v1.getSubDim());
}
use of com.tencent.angel.ml.math2.vector.LongIntVector in project angel by Tencent.
the class SimpleBinaryInAllExecutor method apply.
public static Vector apply(LongIntVector v1, LongDummyVector v2, Binary op) {
if (v1.isSparse()) {
if (op.isKeepStorage()) {
throw new AngelException("operation is not support!");
} else {
// multi-rehash
LongIntVectorStorage newStorage = v1.getStorage().emptySparse((int) (v1.getDim()));
LongIntVectorStorage v1Storage = v1.getStorage();
for (int i = 0; i < v1.getDim(); i++) {
if (v1Storage.hasKey(i)) {
newStorage.set(i, op.apply(v1.get(i), v2.get(i)));
} else {
newStorage.set(i, op.apply(0, v2.get(i)));
}
}
v1.setStorage(newStorage);
}
} else {
// sorted
if (op.isKeepStorage()) {
throw new AngelException("operation is not support!");
} else {
LongIntVectorStorage newStorage = new LongIntSparseVectorStorage(v1.getDim());
LongIntVectorStorage v1Storage = v1.getStorage();
for (int i = 0; i < v1.getDim(); i++) {
if (v1Storage.hasKey(i)) {
newStorage.set(i, op.apply(v1.get(i), v2.get(i)));
} else {
newStorage.set(i, op.apply(0, v2.get(i)));
}
}
v1.setStorage(newStorage);
}
}
return v1;
}
use of com.tencent.angel.ml.math2.vector.LongIntVector in project angel by Tencent.
the class SnapshotFormat method save.
private void save(ServerLongIntRow row, PSMatrixSaveContext saveContext, MatrixPartitionMeta meta, DataOutputStream out) throws IOException {
long startCol = meta.getStartCol();
if (ServerRowUtils.getVector(row) instanceof IntIntVector) {
IntIntVector vector = (IntIntVector) ServerRowUtils.getVector(row);
if (vector.isDense()) {
int[] data = vector.getStorage().getValues();
for (int i = 0; i < data.length; i++) {
out.writeInt(data[i]);
}
} else if (vector.isSorted()) {
int[] indices = vector.getStorage().getIndices();
int[] values = vector.getStorage().getValues();
for (int i = 0; i < indices.length; i++) {
out.writeLong(indices[i] + startCol);
out.writeInt(values[i]);
}
} else {
ObjectIterator<Int2IntMap.Entry> iter = vector.getStorage().entryIterator();
Int2IntMap.Entry entry;
while (iter.hasNext()) {
entry = iter.next();
out.writeLong(entry.getIntKey() + startCol);
out.writeInt(entry.getIntValue());
}
}
} else {
LongIntVector vector = (LongIntVector) ServerRowUtils.getVector(row);
if (vector.isSorted()) {
long[] indices = vector.getStorage().getIndices();
int[] values = vector.getStorage().getValues();
for (int i = 0; i < indices.length; i++) {
out.writeLong(indices[i] + startCol);
out.writeInt(values[i]);
}
} else {
ObjectIterator<Long2IntMap.Entry> iter = vector.getStorage().entryIterator();
Long2IntMap.Entry entry;
while (iter.hasNext()) {
entry = iter.next();
out.writeLong(entry.getLongKey() + startCol);
out.writeInt(entry.getIntValue());
}
}
}
}
use of com.tencent.angel.ml.math2.vector.LongIntVector in project angel by Tencent.
the class MixedBinaryInAllExecutor method apply.
private static Vector apply(CompLongFloatVector v1, LongIntVector v2, Binary op) {
LongFloatVector[] parts = v1.getPartitions();
Storage[] resParts = StorageSwitch.applyComp(v1, v2, op);
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();
for (int i = 0; i < v1.getDim(); i++) {
int pidx = (int) (i / subDim);
long subidx = i % subDim;
if (v2.getStorage().hasKey(i)) {
((LongFloatVectorStorage) resParts[pidx]).set(subidx, op.apply(parts[pidx].get(subidx), v2.get(i)));
} else {
((LongFloatVectorStorage) resParts[pidx]).set(subidx, op.apply(parts[pidx].get(subidx), 0));
}
}
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++;
}
v1.setPartitions(res);
return v1;
}
use of com.tencent.angel.ml.math2.vector.LongIntVector in project angel by Tencent.
the class MixedBinaryInAllExecutor method apply.
private static Vector apply(CompLongIntVector v1, LongIntVector v2, Binary op) {
LongIntVector[] parts = v1.getPartitions();
Storage[] resParts = StorageSwitch.applyComp(v1, v2, op);
if (!op.isKeepStorage()) {
for (int i = 0; i < parts.length; i++) {
if (parts[i].getStorage() instanceof LongIntSortedVectorStorage) {
resParts[i] = new LongIntSparseVectorStorage(parts[i].getDim(), parts[i].getStorage().getIndices(), parts[i].getStorage().getValues());
}
}
}
long subDim = (v1.getDim() + v1.getNumPartitions() - 1) / v1.getNumPartitions();
for (int i = 0; i < v1.getDim(); i++) {
int pidx = (int) (i / subDim);
long subidx = i % subDim;
if (v2.getStorage().hasKey(i)) {
((LongIntVectorStorage) resParts[pidx]).set(subidx, op.apply(parts[pidx].get(subidx), v2.get(i)));
} else {
((LongIntVectorStorage) resParts[pidx]).set(subidx, op.apply(parts[pidx].get(subidx), 0));
}
}
LongIntVector[] res = new LongIntVector[parts.length];
int i = 0;
for (LongIntVector part : parts) {
res[i] = new LongIntVector(part.getMatrixId(), part.getRowId(), part.getClock(), part.getDim(), (LongIntVectorStorage) resParts[i]);
i++;
}
v1.setPartitions(res);
return v1;
}
Aggregations