use of com.tencent.angel.ml.math2.vector.Vector in project angel by Tencent.
the class MixedBinaryInAllExecutor method apply.
private static Vector apply(CompIntDoubleVector v1, IntIntVector v2, Binary op) {
IntDoubleVector[] parts = v1.getPartitions();
Storage[] resParts = StorageSwitch.applyComp(v1, v2, op);
if (v2.isDense()) {
int[] v2Values = v2.getStorage().getValues();
int base = 0, k = 0;
for (IntDoubleVector part : parts) {
IntDoubleVectorStorage resPart = (IntDoubleVectorStorage) resParts[k];
if (part.isDense()) {
double[] partValue = part.getStorage().getValues();
double[] resPartValues = resPart.getValues();
for (int i = 0; i < partValue.length; i++) {
int idx = i + base;
resPartValues[i] = op.apply(partValue[i], v2Values[idx]);
}
} else if (part.isSparse()) {
double[] resPartValues = resPart.getValues();
if (part.size() < Constant.denseLoopThreshold * part.getDim()) {
for (int i = 0; i < part.getDim(); i++) {
resPart.set(i, op.apply(0, v2Values[i + base]));
}
ObjectIterator<Int2DoubleMap.Entry> iter = part.getStorage().entryIterator();
while (iter.hasNext()) {
Int2DoubleMap.Entry entry = iter.next();
int idx = entry.getIntKey();
resPart.set(idx, op.apply(entry.getDoubleValue(), v2Values[idx + base]));
}
} else {
for (int i = 0; i < resPartValues.length; i++) {
if (part.getStorage().hasKey(i)) {
resPart.set(i, op.apply(part.get(i), v2Values[i + base]));
} else {
resPart.set(i, op.apply(0, v2Values[i + base]));
}
}
}
} else {
// sorted
int[] resPartIndices = resPart.getIndices();
double[] resPartValues = resPart.getValues();
if (op.isKeepStorage()) {
if (part.size() < Constant.denseLoopThreshold * part.getDim()) {
int[] partIndices = part.getStorage().getIndices();
double[] partValues = part.getStorage().getValues();
for (int i = 0; i < part.getDim(); i++) {
resPartIndices[i] = i;
resPartValues[i] = op.apply(0, v2Values[i + base]);
}
int size = part.size();
for (int i = 0; i < size; i++) {
int idx = partIndices[i];
resPartValues[idx] = op.apply(partValues[i], v2Values[idx + base]);
}
} else {
IntDoubleVectorStorage partStorage = part.getStorage();
for (int i = 0; i < resPartValues.length; i++) {
if (partStorage.hasKey(i)) {
resPartIndices[i] = i;
resPartValues[i] = op.apply(partStorage.get(i), v2Values[i + base]);
} else {
resPartIndices[i] = i;
resPartValues[i] = op.apply(0, v2Values[i + base]);
}
}
}
} else {
if (part.size() < Constant.denseLoopThreshold * part.getDim()) {
int[] partIndices = part.getStorage().getIndices();
double[] partValues = part.getStorage().getValues();
for (int i = 0; i < part.getDim(); i++) {
resPartValues[i] = op.apply(0, v2Values[i + base]);
}
int size = part.size();
for (int i = 0; i < size; i++) {
int idx = partIndices[i];
resPartValues[idx] = op.apply(partValues[i], v2Values[idx + base]);
}
} else {
IntDoubleVectorStorage partStorage = part.getStorage();
for (int i = 0; i < resPartValues.length; i++) {
if (partStorage.hasKey(i)) {
resPartValues[i] = op.apply(partStorage.get(i), v2Values[i + base]);
} else {
resPartValues[i] = op.apply(0, v2Values[i + base]);
}
}
}
}
}
base += part.getDim();
k++;
}
} else {
if (!op.isKeepStorage()) {
for (int i = 0; i < parts.length; i++) {
if (parts[i].getStorage() instanceof IntDoubleSortedVectorStorage) {
resParts[i] = new IntDoubleSparseVectorStorage(parts[i].getDim(), parts[i].getStorage().getIndices(), parts[i].getStorage().getValues());
}
}
}
int subDim = (v1.getDim() + v1.getNumPartitions() - 1) / v1.getNumPartitions();
for (int i = 0; i < v1.getDim(); i++) {
int pidx = (int) (i / subDim);
int subidx = i % subDim;
if (v2.getStorage().hasKey(i)) {
((IntDoubleVectorStorage) resParts[pidx]).set(subidx, op.apply(parts[pidx].get(subidx), v2.get(i)));
} else {
((IntDoubleVectorStorage) resParts[pidx]).set(subidx, op.apply(parts[pidx].get(subidx), 0));
}
}
}
IntDoubleVector[] res = new IntDoubleVector[parts.length];
int i = 0;
for (IntDoubleVector part : parts) {
res[i] = new IntDoubleVector(part.getMatrixId(), part.getRowId(), part.getClock(), part.getDim(), (IntDoubleVectorStorage) resParts[i]);
i++;
}
v1.setPartitions(res);
return v1;
}
use of com.tencent.angel.ml.math2.vector.Vector in project angel by Tencent.
the class MixedBinaryInAllExecutor method apply.
private static Vector apply(CompIntFloatVector v1, IntDummyVector v2, Binary op) {
IntFloatVector[] 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 IntFloatSortedVectorStorage) {
resParts[i] = new IntFloatSparseVectorStorage(parts[i].getDim(), parts[i].getStorage().getIndices(), parts[i].getStorage().getValues());
}
}
}
int subDim = (v1.getDim() + v1.getNumPartitions() - 1) / v1.getNumPartitions();
for (int i = 0; i < v1.getDim(); i++) {
int pidx = (int) (i / subDim);
int subidx = i % subDim;
((IntFloatVectorStorage) resParts[pidx]).set(subidx, op.apply(parts[pidx].get(subidx), v2.get(i)));
}
IntFloatVector[] res = new IntFloatVector[parts.length];
int i = 0;
for (IntFloatVector part : parts) {
res[i] = new IntFloatVector(part.getMatrixId(), part.getRowId(), part.getClock(), part.getDim(), (IntFloatVectorStorage) resParts[i]);
i++;
}
v1.setPartitions(res);
return v1;
}
use of com.tencent.angel.ml.math2.vector.Vector in project angel by Tencent.
the class StorageSwitch method applyComp.
public static Storage[] applyComp(ComponentVector v1, Vector v2, Binary op) {
Vector[] parts = v1.getPartitions();
Storage[] resParts = new Storage[parts.length];
int k = 0;
if (op.getOpType() == OpType.UNION) {
if (v2.isDense()) {
for (Vector part : parts) {
if (part.isDense()) {
if (op.isInplace()) {
resParts[k] = part.getStorage();
} else {
resParts[k] = part.copy().getStorage();
}
} else if (part.isSparse()) {
if (op.isKeepStorage()) {
resParts[k] = emptyStorage(part.getStorage(), StorageMethod.emptySparse, part.dim());
} else {
resParts[k] = emptyStorage(part.getStorage(), StorageMethod.emptyDense);
}
} else {
if (op.isKeepStorage()) {
resParts[k] = emptyStorage(part.getStorage(), StorageMethod.emptySorted, part.dim());
} else {
resParts[k] = emptyStorage(part.getStorage(), StorageMethod.emptyDense);
}
}
k++;
}
} else {
for (Vector part : parts) {
if (op.isInplace()) {
resParts[k] = part.getStorage();
} else {
resParts[k] = part.copy().getStorage();
}
k++;
}
}
} else if (op.getOpType() == OpType.INTERSECTION) {
if (v2.isDense()) {
for (Vector part : parts) {
if (part.isDense()) {
resParts[k] = emptyStorage(part.getStorage(), StorageMethod.emptyDense);
} else if (part.isSparse()) {
resParts[k] = emptyStorage(part.getStorage(), StorageMethod.emptySparse);
} else {
if (op.isKeepStorage()) {
resParts[k] = emptyStorage(part.getStorage(), StorageMethod.emptySorted);
} else {
resParts[k] = emptyStorage(part.getStorage(), StorageMethod.emptySparse);
}
}
k++;
}
} else {
if (((Vector) v1).getSize() > v2.getSize()) {
for (Vector part : parts) {
if (op.isKeepStorage()) {
if (part.isDense()) {
resParts[k] = emptyStorage(part.getStorage(), StorageMethod.emptyDense);
} else if (part.isSparse()) {
resParts[k] = emptyStorage(part.getStorage(), StorageMethod.emptySparse);
} else {
resParts[k] = emptyStorage(part.getStorage(), StorageMethod.emptySorted);
}
} else {
resParts[k] = emptyStorage(part.getStorage(), StorageMethod.emptySparse);
}
k++;
}
} else {
for (Vector part : parts) {
if (part.isDense()) {
resParts[k] = emptyStorage(part.getStorage(), StorageMethod.emptyDense);
} else if (part.isSparse()) {
resParts[k] = emptyStorage(part.getStorage(), StorageMethod.emptySparse);
} else {
if (op.isKeepStorage()) {
resParts[k] = emptyStorage(part.getStorage(), StorageMethod.emptySorted);
} else {
resParts[k] = emptyStorage(part.getStorage(), StorageMethod.emptySparse);
}
}
k++;
}
}
}
} else {
// OpType.ALL
for (Vector part : parts) {
if (part.isDense()) {
if (op.isInplace()) {
resParts[k] = part.getStorage();
} else {
resParts[k] = emptyStorage(part.getStorage(), StorageMethod.emptyDense);
}
} else {
if (op.isKeepStorage()) {
if (part.isSparse()) {
resParts[k] = emptyStorage(part.getStorage(), StorageMethod.emptySparse, part.dim());
} else {
// sorted
resParts[k] = emptyStorage(part.getStorage(), StorageMethod.emptySorted, part.dim());
}
} else {
if (part.getStorage() instanceof LongKeyVectorStorage) {
resParts[k] = emptyStorage(part.getStorage(), StorageMethod.emptySparse, part.dim());
} else {
resParts[k] = emptyStorage(part.getStorage(), StorageMethod.emptyDense);
}
}
}
k++;
}
}
return resParts;
}
use of com.tencent.angel.ml.math2.vector.Vector in project angel by Tencent.
the class SnapshotFormat method save.
private void save(ServerLongLongRow row, PSMatrixSaveContext saveContext, MatrixPartitionMeta meta, DataOutputStream out) throws IOException {
long startCol = meta.getStartCol();
if (ServerRowUtils.getVector(row) instanceof IntLongVector) {
IntLongVector vector = (IntLongVector) ServerRowUtils.getVector(row);
if (vector.isDense()) {
long[] data = vector.getStorage().getValues();
for (int i = 0; i < data.length; i++) {
out.writeLong(data[i]);
}
} else if (vector.isSorted()) {
int[] indices = vector.getStorage().getIndices();
long[] values = vector.getStorage().getValues();
for (int i = 0; i < indices.length; i++) {
out.writeLong(indices[i] + startCol);
out.writeLong(values[i]);
}
} else {
ObjectIterator<Int2LongMap.Entry> iter = vector.getStorage().entryIterator();
Int2LongMap.Entry entry;
while (iter.hasNext()) {
entry = iter.next();
out.writeLong(entry.getIntKey() + startCol);
out.writeLong(entry.getLongValue());
}
}
} else {
LongLongVector vector = (LongLongVector) ServerRowUtils.getVector(row);
if (vector.isSorted()) {
long[] indices = vector.getStorage().getIndices();
long[] values = vector.getStorage().getValues();
for (int i = 0; i < indices.length; i++) {
out.writeLong(indices[i] + startCol);
out.writeLong(values[i]);
}
} else {
ObjectIterator<Long2LongMap.Entry> iter = vector.getStorage().entryIterator();
Long2LongMap.Entry entry;
while (iter.hasNext()) {
entry = iter.next();
out.writeLong(entry.getLongKey() + startCol);
out.writeLong(entry.getLongValue());
}
}
}
}
use of com.tencent.angel.ml.math2.vector.Vector in project angel by Tencent.
the class SnapshotFormat method save.
private void save(ServerIntFloatRow row, PSMatrixSaveContext saveContext, MatrixPartitionMeta meta, DataOutputStream out) throws IOException {
int startCol = (int) meta.getStartCol();
IntFloatVector vector = ServerRowUtils.getVector(row);
if (vector.isDense()) {
float[] data = vector.getStorage().getValues();
for (int i = 0; i < data.length; i++) {
out.writeFloat(data[i]);
}
} else if (vector.isSorted()) {
int[] indices = vector.getStorage().getIndices();
float[] values = vector.getStorage().getValues();
for (int i = 0; i < indices.length; i++) {
out.writeInt(indices[i] + startCol);
out.writeFloat(values[i]);
}
} else {
ObjectIterator<Int2FloatMap.Entry> iter = vector.getStorage().entryIterator();
Int2FloatMap.Entry entry;
while (iter.hasNext()) {
entry = iter.next();
out.writeInt(entry.getIntKey() + startCol);
out.writeFloat(entry.getFloatValue());
}
}
}
Aggregations