use of com.tencent.angel.ml.math2.vector.ComponentVector in project angel by Tencent.
the class VectorUtils method emptyLike.
private static ComponentVector emptyLike(ComponentVector v) {
ComponentVector result;
if (v instanceof CompIntDoubleVector) {
IntDoubleVector[] parts = new IntDoubleVector[v.getNumPartitions()];
IntDoubleVector[] refParts = ((CompIntDoubleVector) v).getPartitions();
for (int i = 0; i < refParts.length; i++) {
if (null != refParts[i]) {
parts[i] = (IntDoubleVector) emptyLike((SimpleVector) refParts[i]);
}
}
result = new CompIntDoubleVector(((CompIntDoubleVector) v).getMatrixId(), ((CompIntDoubleVector) v).getRowId(), ((CompIntDoubleVector) v).getClock(), ((CompIntDoubleVector) v).getDim(), parts, ((CompIntDoubleVector) v).getSubDim());
} else if (v instanceof CompIntFloatVector) {
IntFloatVector[] parts = new IntFloatVector[v.getNumPartitions()];
IntFloatVector[] refParts = ((CompIntFloatVector) v).getPartitions();
for (int i = 0; i < refParts.length; i++) {
if (null != refParts[i]) {
parts[i] = (IntFloatVector) emptyLike((SimpleVector) refParts[i]);
}
}
result = new CompIntFloatVector(((CompIntFloatVector) v).getMatrixId(), ((CompIntFloatVector) v).getRowId(), ((CompIntFloatVector) v).getClock(), ((CompIntFloatVector) v).getDim(), parts, ((CompIntFloatVector) v).getSubDim());
} else if (v instanceof CompIntLongVector) {
IntLongVector[] parts = new IntLongVector[v.getNumPartitions()];
IntLongVector[] refParts = ((CompIntLongVector) v).getPartitions();
for (int i = 0; i < refParts.length; i++) {
if (null != refParts[i]) {
parts[i] = (IntLongVector) emptyLike((SimpleVector) refParts[i]);
}
}
result = new CompIntLongVector(((CompIntLongVector) v).getMatrixId(), ((CompIntLongVector) v).getRowId(), ((CompIntLongVector) v).getClock(), ((CompIntLongVector) v).getDim(), parts, ((CompIntLongVector) v).getSubDim());
} else if (v instanceof CompIntIntVector) {
IntIntVector[] parts = new IntIntVector[v.getNumPartitions()];
IntIntVector[] refParts = ((CompIntIntVector) v).getPartitions();
for (int i = 0; i < refParts.length; i++) {
if (null != refParts[i]) {
parts[i] = (IntIntVector) emptyLike((SimpleVector) refParts[i]);
}
}
result = new CompIntIntVector(((CompIntIntVector) v).getMatrixId(), ((CompIntIntVector) v).getRowId(), ((CompIntIntVector) v).getClock(), ((CompIntIntVector) v).getDim(), parts, ((CompIntIntVector) v).getSubDim());
} else if (v instanceof CompLongDoubleVector) {
LongDoubleVector[] parts = new LongDoubleVector[v.getNumPartitions()];
LongDoubleVector[] refParts = ((CompLongDoubleVector) v).getPartitions();
for (int i = 0; i < refParts.length; i++) {
if (null != refParts[i]) {
parts[i] = (LongDoubleVector) emptyLike((SimpleVector) refParts[i]);
}
}
result = new CompLongDoubleVector(((CompLongDoubleVector) v).getMatrixId(), ((CompLongDoubleVector) v).getRowId(), ((CompLongDoubleVector) v).getClock(), ((CompLongDoubleVector) v).getDim(), parts, ((CompLongDoubleVector) v).getSubDim());
} else if (v instanceof CompLongFloatVector) {
LongFloatVector[] parts = new LongFloatVector[v.getNumPartitions()];
LongFloatVector[] refParts = ((CompLongFloatVector) v).getPartitions();
for (int i = 0; i < refParts.length; i++) {
if (null != refParts[i]) {
parts[i] = (LongFloatVector) emptyLike((SimpleVector) refParts[i]);
}
}
result = new CompLongFloatVector(((CompLongFloatVector) v).getMatrixId(), ((CompLongFloatVector) v).getRowId(), ((CompLongFloatVector) v).getClock(), ((CompLongFloatVector) v).getDim(), parts, ((CompLongFloatVector) v).getSubDim());
} else if (v instanceof CompLongLongVector) {
LongLongVector[] parts = new LongLongVector[v.getNumPartitions()];
LongLongVector[] refParts = ((CompLongLongVector) v).getPartitions();
for (int i = 0; i < refParts.length; i++) {
if (null != refParts[i]) {
parts[i] = (LongLongVector) emptyLike((SimpleVector) refParts[i]);
}
}
result = new CompLongLongVector(((CompLongLongVector) v).getMatrixId(), ((CompLongLongVector) v).getRowId(), ((CompLongLongVector) v).getClock(), ((CompLongLongVector) v).getDim(), parts, ((CompLongLongVector) v).getSubDim());
} else if (v instanceof CompLongIntVector) {
LongIntVector[] parts = new LongIntVector[v.getNumPartitions()];
LongIntVector[] refParts = ((CompLongIntVector) v).getPartitions();
for (int i = 0; i < refParts.length; i++) {
if (null != refParts[i]) {
parts[i] = (LongIntVector) emptyLike((SimpleVector) refParts[i]);
}
}
result = new CompLongIntVector(((CompLongIntVector) v).getMatrixId(), ((CompLongIntVector) v).getRowId(), ((CompLongIntVector) v).getClock(), ((CompLongIntVector) v).getDim(), parts, ((CompLongIntVector) v).getSubDim());
} else {
throw new AngelException("The operation is not support!");
}
return result;
}
use of com.tencent.angel.ml.math2.vector.ComponentVector in project angel by Tencent.
the class CompUnaryExecutor method apply.
public static Vector apply(ComponentVector v1, Unary op) {
ComponentVector result;
if (op.isInplace()) {
result = v1;
} else {
result = (ComponentVector) VectorUtils.emptyLike((Vector) v1);
}
CompUnaExe task = new CompUnaExe(v1, op, result, 0, v1.getNumPartitions() - 1);
pool.execute(task);
task.join();
return (Vector) result;
}
use of com.tencent.angel.ml.math2.vector.ComponentVector 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.ComponentVector in project angel by Tencent.
the class CompReduceExecutor method apply.
public static double apply(ComponentVector vector, ReduceOP op) {
CompRedExe task = new CompRedExe(vector, op, 0, vector.getNumPartitions() - 1);
Future<UnionEle> futRes = pool.submit(task);
try {
UnionEle tmpRes = futRes.get();
switch(op) {
case Avg:
return tmpRes.getDouble1() / tmpRes.getLong1();
case Std:
double avg1 = tmpRes.getDouble1() / tmpRes.getLong1();
double avg2 = tmpRes.getDouble2() / tmpRes.getLong1();
return Math.sqrt(avg2 - avg1 * avg1);
case Norm:
return Math.sqrt(tmpRes.getDouble2());
default:
return tmpRes.getDouble1() + tmpRes.getLong1();
}
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
return Double.NaN;
}
use of com.tencent.angel.ml.math2.vector.ComponentVector in project angel by Tencent.
the class CompBinaryExecutor method apply.
public static Vector apply(ComponentVector v1, ComponentVector v2, Binary op) {
ComponentVector result;
if (op.isInplace()) {
result = v1;
} else {
result = (ComponentVector) VectorUtils.emptyLike((Vector) v1);
}
CompBinExe task = new CompBinExe(v1, v2, op, result, 0, v1.getNumPartitions() - 1);
pool.execute(task);
task.join();
return (Vector) result;
}
Aggregations