Search in sources :

Example 1 with ComponentVector

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;
}
Also used : CompIntLongVector(com.tencent.angel.ml.math2.vector.CompIntLongVector) IntLongVector(com.tencent.angel.ml.math2.vector.IntLongVector) AngelException(com.tencent.angel.exception.AngelException) CompLongLongVector(com.tencent.angel.ml.math2.vector.CompLongLongVector) CompLongIntVector(com.tencent.angel.ml.math2.vector.CompLongIntVector) LongIntVector(com.tencent.angel.ml.math2.vector.LongIntVector) ComponentVector(com.tencent.angel.ml.math2.vector.ComponentVector) LongLongVector(com.tencent.angel.ml.math2.vector.LongLongVector) CompLongLongVector(com.tencent.angel.ml.math2.vector.CompLongLongVector) CompIntLongVector(com.tencent.angel.ml.math2.vector.CompIntLongVector) IntIntVector(com.tencent.angel.ml.math2.vector.IntIntVector) CompIntIntVector(com.tencent.angel.ml.math2.vector.CompIntIntVector) CompLongFloatVector(com.tencent.angel.ml.math2.vector.CompLongFloatVector) LongFloatVector(com.tencent.angel.ml.math2.vector.LongFloatVector) CompIntFloatVector(com.tencent.angel.ml.math2.vector.CompIntFloatVector) IntFloatVector(com.tencent.angel.ml.math2.vector.IntFloatVector) CompIntIntVector(com.tencent.angel.ml.math2.vector.CompIntIntVector) CompIntDoubleVector(com.tencent.angel.ml.math2.vector.CompIntDoubleVector) IntDoubleVector(com.tencent.angel.ml.math2.vector.IntDoubleVector) CompIntFloatVector(com.tencent.angel.ml.math2.vector.CompIntFloatVector) CompLongDoubleVector(com.tencent.angel.ml.math2.vector.CompLongDoubleVector) CompLongFloatVector(com.tencent.angel.ml.math2.vector.CompLongFloatVector) CompLongDoubleVector(com.tencent.angel.ml.math2.vector.CompLongDoubleVector) LongDoubleVector(com.tencent.angel.ml.math2.vector.LongDoubleVector) CompLongIntVector(com.tencent.angel.ml.math2.vector.CompLongIntVector) SimpleVector(com.tencent.angel.ml.math2.vector.SimpleVector) CompIntDoubleVector(com.tencent.angel.ml.math2.vector.CompIntDoubleVector)

Example 2 with ComponentVector

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;
}
Also used : ComponentVector(com.tencent.angel.ml.math2.vector.ComponentVector) CompLongLongVector(com.tencent.angel.ml.math2.vector.CompLongLongVector) CompIntDoubleVector(com.tencent.angel.ml.math2.vector.CompIntDoubleVector) CompLongIntVector(com.tencent.angel.ml.math2.vector.CompLongIntVector) IntLongVector(com.tencent.angel.ml.math2.vector.IntLongVector) CompIntFloatVector(com.tencent.angel.ml.math2.vector.CompIntFloatVector) IntFloatVector(com.tencent.angel.ml.math2.vector.IntFloatVector) LongDoubleVector(com.tencent.angel.ml.math2.vector.LongDoubleVector) ComponentVector(com.tencent.angel.ml.math2.vector.ComponentVector) LongIntVector(com.tencent.angel.ml.math2.vector.LongIntVector) Vector(com.tencent.angel.ml.math2.vector.Vector) CompIntIntVector(com.tencent.angel.ml.math2.vector.CompIntIntVector) LongFloatVector(com.tencent.angel.ml.math2.vector.LongFloatVector) LongLongVector(com.tencent.angel.ml.math2.vector.LongLongVector) CompIntLongVector(com.tencent.angel.ml.math2.vector.CompIntLongVector) CompLongDoubleVector(com.tencent.angel.ml.math2.vector.CompLongDoubleVector) IntDoubleVector(com.tencent.angel.ml.math2.vector.IntDoubleVector) CompLongFloatVector(com.tencent.angel.ml.math2.vector.CompLongFloatVector) IntIntVector(com.tencent.angel.ml.math2.vector.IntIntVector)

Example 3 with ComponentVector

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;
}
Also used : LongKeyVectorStorage(com.tencent.angel.ml.math2.storage.LongKeyVectorStorage) Storage(com.tencent.angel.ml.math2.storage.Storage) LongKeyVectorStorage(com.tencent.angel.ml.math2.storage.LongKeyVectorStorage) ComponentVector(com.tencent.angel.ml.math2.vector.ComponentVector) Vector(com.tencent.angel.ml.math2.vector.Vector)

Example 4 with ComponentVector

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;
}
Also used : UnionEle(com.tencent.angel.ml.math2.utils.UnionEle) ExecutionException(java.util.concurrent.ExecutionException)

Example 5 with ComponentVector

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;
}
Also used : ComponentVector(com.tencent.angel.ml.math2.vector.ComponentVector) CompLongLongVector(com.tencent.angel.ml.math2.vector.CompLongLongVector) CompIntDoubleVector(com.tencent.angel.ml.math2.vector.CompIntDoubleVector) CompLongIntVector(com.tencent.angel.ml.math2.vector.CompLongIntVector) IntLongVector(com.tencent.angel.ml.math2.vector.IntLongVector) CompIntFloatVector(com.tencent.angel.ml.math2.vector.CompIntFloatVector) IntFloatVector(com.tencent.angel.ml.math2.vector.IntFloatVector) LongDoubleVector(com.tencent.angel.ml.math2.vector.LongDoubleVector) ComponentVector(com.tencent.angel.ml.math2.vector.ComponentVector) LongIntVector(com.tencent.angel.ml.math2.vector.LongIntVector) Vector(com.tencent.angel.ml.math2.vector.Vector) CompIntIntVector(com.tencent.angel.ml.math2.vector.CompIntIntVector) LongFloatVector(com.tencent.angel.ml.math2.vector.LongFloatVector) LongLongVector(com.tencent.angel.ml.math2.vector.LongLongVector) CompIntLongVector(com.tencent.angel.ml.math2.vector.CompIntLongVector) CompLongDoubleVector(com.tencent.angel.ml.math2.vector.CompLongDoubleVector) IntDoubleVector(com.tencent.angel.ml.math2.vector.IntDoubleVector) CompLongFloatVector(com.tencent.angel.ml.math2.vector.CompLongFloatVector) IntIntVector(com.tencent.angel.ml.math2.vector.IntIntVector)

Aggregations

ComponentVector (com.tencent.angel.ml.math2.vector.ComponentVector)4 CompIntDoubleVector (com.tencent.angel.ml.math2.vector.CompIntDoubleVector)3 CompIntFloatVector (com.tencent.angel.ml.math2.vector.CompIntFloatVector)3 CompIntIntVector (com.tencent.angel.ml.math2.vector.CompIntIntVector)3 CompIntLongVector (com.tencent.angel.ml.math2.vector.CompIntLongVector)3 CompLongDoubleVector (com.tencent.angel.ml.math2.vector.CompLongDoubleVector)3 CompLongFloatVector (com.tencent.angel.ml.math2.vector.CompLongFloatVector)3 CompLongIntVector (com.tencent.angel.ml.math2.vector.CompLongIntVector)3 CompLongLongVector (com.tencent.angel.ml.math2.vector.CompLongLongVector)3 IntDoubleVector (com.tencent.angel.ml.math2.vector.IntDoubleVector)3 IntFloatVector (com.tencent.angel.ml.math2.vector.IntFloatVector)3 IntIntVector (com.tencent.angel.ml.math2.vector.IntIntVector)3 IntLongVector (com.tencent.angel.ml.math2.vector.IntLongVector)3 LongDoubleVector (com.tencent.angel.ml.math2.vector.LongDoubleVector)3 LongFloatVector (com.tencent.angel.ml.math2.vector.LongFloatVector)3 LongIntVector (com.tencent.angel.ml.math2.vector.LongIntVector)3 LongLongVector (com.tencent.angel.ml.math2.vector.LongLongVector)3 Vector (com.tencent.angel.ml.math2.vector.Vector)3 AngelException (com.tencent.angel.exception.AngelException)1 LongKeyVectorStorage (com.tencent.angel.ml.math2.storage.LongKeyVectorStorage)1