Search in sources :

Example 81 with IntDoubleVector

use of com.tencent.angel.ml.math2.vector.IntDoubleVector in project angel by Tencent.

the class MixedBinaryInZAExecutor method apply.

private static Vector apply(CompIntDoubleVector v1, IntDoubleVector v2, Binary op) {
    IntDoubleVector[] parts = v1.getPartitions();
    Storage[] resParts = StorageSwitch.applyComp(v1, v2, op);
    if (v2.isDense()) {
        int base = 0;
        double[] v2Values = v2.getStorage().getValues();
        for (int i = 0; i < parts.length; i++) {
            IntDoubleVector part = parts[i];
            IntDoubleVectorStorage resPart = (IntDoubleVectorStorage) resParts[i];
            if (part.isDense()) {
                double[] resPartValues = resPart.getValues();
                double[] partValues = part.getStorage().getValues();
                for (int j = 0; j < partValues.length; j++) {
                    resPartValues[j] = op.apply(partValues[j], v2Values[base + j]);
                }
            } else if (part.isSparse()) {
                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 {
                // sorted
                if (op.isKeepStorage()) {
                    int[] resPartIndices = resPart.getIndices();
                    double[] resPartValues = resPart.getValues();
                    int[] partIndices = part.getStorage().getIndices();
                    double[] partValues = part.getStorage().getValues();
                    for (int j = 0; j < partIndices.length; j++) {
                        int idx = partIndices[j];
                        resPartIndices[j] = idx;
                        resPartValues[j] = op.apply(partValues[j], v2Values[idx + base]);
                    }
                } else {
                    int[] partIndices = part.getStorage().getIndices();
                    double[] partValues = part.getStorage().getValues();
                    for (int j = 0; j < partIndices.length; j++) {
                        int idx = partIndices[j];
                        resPart.set(idx, op.apply(partValues[j], v2Values[idx + base]));
                    }
                }
            }
            base += part.getDim();
        }
    } else if (v2.isSparse()) {
        ObjectIterator<Int2DoubleMap.Entry> iter = v2.getStorage().entryIterator();
        if (v1.size() > v2.size()) {
            int subDim = (v1.getDim() + v1.getNumPartitions() - 1) / v1.getNumPartitions();
            while (iter.hasNext()) {
                Int2DoubleMap.Entry entry = iter.next();
                int idx = entry.getIntKey();
                int pidx = (int) (idx / subDim);
                int subidx = idx % subDim;
                if (parts[pidx].hasKey(subidx)) {
                    ((IntDoubleVectorStorage) resParts[pidx]).set(subidx, op.apply(parts[pidx].get(subidx), entry.getDoubleValue()));
                }
            }
        } else {
            int base = 0;
            for (int i = 0; i < parts.length; i++) {
                IntDoubleVector part = parts[i];
                IntDoubleVectorStorage resPart = (IntDoubleVectorStorage) resParts[i];
                if (part.isDense()) {
                    double[] partValues = part.getStorage().getValues();
                    double[] resPartValues = resPart.getValues();
                    for (int j = 0; j < partValues.length; j++) {
                        if (v2.hasKey(j + base)) {
                            resPartValues[j] = op.apply(partValues[j], v2.get(j + base));
                        }
                    }
                } else if (part.isSparse()) {
                    ObjectIterator<Int2DoubleMap.Entry> piter = part.getStorage().entryIterator();
                    while (piter.hasNext()) {
                        Int2DoubleMap.Entry entry = piter.next();
                        int idx = entry.getIntKey();
                        if (v2.hasKey(idx + base)) {
                            resPart.set(idx, op.apply(entry.getDoubleValue(), v2.get(idx + base)));
                        }
                    }
                } else {
                    // sorted
                    if (op.isKeepStorage()) {
                        int[] partIndices = part.getStorage().getIndices();
                        double[] partValues = part.getStorage().getValues();
                        int[] resPartIndices = resPart.getIndices();
                        double[] resPartValues = resPart.getValues();
                        for (int j = 0; j < partIndices.length; j++) {
                            int idx = partIndices[j];
                            if (v2.hasKey(idx + base)) {
                                resPartIndices[j] = idx;
                                resPartValues[j] = op.apply(partValues[j], v2.get(idx + base));
                            }
                        }
                    } else {
                        int[] partIndices = part.getStorage().getIndices();
                        double[] partValues = part.getStorage().getValues();
                        for (int j = 0; j < partIndices.length; j++) {
                            int idx = partIndices[j];
                            if (v2.hasKey(idx + base)) {
                                resPart.set(idx, op.apply(partValues[j], v2.get(idx + base)));
                            }
                        }
                    }
                }
                base += part.getDim();
            }
        }
    } else {
        // sorted
        if (v1.size() > v2.size()) {
            int subDim = (v1.getDim() + v1.getNumPartitions() - 1) / v1.getNumPartitions();
            int[] v2Indices = v2.getStorage().getIndices();
            double[] v2Values = v2.getStorage().getValues();
            for (int i = 0; i < v2Indices.length; i++) {
                int idx = v2Indices[i];
                int pidx = (int) (idx / subDim);
                int subidx = idx % subDim;
                if (parts[pidx].hasKey(subidx)) {
                    ((IntDoubleVectorStorage) resParts[pidx]).set(subidx, op.apply(parts[pidx].get(subidx), v2Values[i]));
                }
            }
        } else {
            int base = 0;
            for (int i = 0; i < parts.length; i++) {
                IntDoubleVector part = parts[i];
                IntDoubleVectorStorage resPart = (IntDoubleVectorStorage) resParts[i];
                if (part.isDense()) {
                    double[] partValues = part.getStorage().getValues();
                    double[] resPartValues = resPart.getValues();
                    for (int j = 0; j < partValues.length; j++) {
                        if (v2.hasKey(j + base)) {
                            resPartValues[j] = op.apply(partValues[j], v2.get(j + base));
                        }
                    }
                } else if (part.isSparse()) {
                    ObjectIterator<Int2DoubleMap.Entry> piter = part.getStorage().entryIterator();
                    while (piter.hasNext()) {
                        Int2DoubleMap.Entry entry = piter.next();
                        int idx = entry.getIntKey();
                        if (v2.hasKey(idx + base)) {
                            resPart.set(idx, op.apply(entry.getDoubleValue(), v2.get(idx + base)));
                        }
                    }
                } else {
                    // sorted
                    if (op.isKeepStorage()) {
                        int[] partIndices = part.getStorage().getIndices();
                        double[] partValues = part.getStorage().getValues();
                        int[] resPartIndices = resPart.getIndices();
                        double[] resPartValues = resPart.getValues();
                        for (int j = 0; j < partIndices.length; j++) {
                            int idx = partIndices[j];
                            if (v2.hasKey(idx + base)) {
                                resPartIndices[j] = idx;
                                resPartValues[j] = op.apply(partValues[j], v2.get(idx + base));
                            }
                        }
                    } else {
                        int[] partIndices = part.getStorage().getIndices();
                        double[] partValues = part.getStorage().getValues();
                        for (int j = 0; j < partIndices.length; j++) {
                            int idx = partIndices[j];
                            if (v2.hasKey(idx + base)) {
                                resPart.set(idx, op.apply(partValues[j], v2.get(idx + base)));
                            }
                        }
                    }
                }
                base += part.getDim();
            }
        }
    }
    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;
}
Also used : Int2DoubleMap(it.unimi.dsi.fastutil.ints.Int2DoubleMap) CompIntDoubleVector(com.tencent.angel.ml.math2.vector.CompIntDoubleVector) IntDoubleVector(com.tencent.angel.ml.math2.vector.IntDoubleVector) ObjectIterator(it.unimi.dsi.fastutil.objects.ObjectIterator) IntIntVectorStorage(com.tencent.angel.ml.math2.storage.IntIntVectorStorage) Storage(com.tencent.angel.ml.math2.storage.Storage) LongIntVectorStorage(com.tencent.angel.ml.math2.storage.LongIntVectorStorage) IntFloatVectorStorage(com.tencent.angel.ml.math2.storage.IntFloatVectorStorage) LongDoubleVectorStorage(com.tencent.angel.ml.math2.storage.LongDoubleVectorStorage) IntDoubleVectorStorage(com.tencent.angel.ml.math2.storage.IntDoubleVectorStorage) LongLongVectorStorage(com.tencent.angel.ml.math2.storage.LongLongVectorStorage) LongFloatVectorStorage(com.tencent.angel.ml.math2.storage.LongFloatVectorStorage) IntLongVectorStorage(com.tencent.angel.ml.math2.storage.IntLongVectorStorage) IntDoubleVectorStorage(com.tencent.angel.ml.math2.storage.IntDoubleVectorStorage)

Example 82 with IntDoubleVector

use of com.tencent.angel.ml.math2.vector.IntDoubleVector in project angel by Tencent.

the class MixedBinaryOutNonZAExecutor method apply.

private static Vector apply(CompIntDoubleVector v1, IntLongVector v2, Binary op) {
    IntDoubleVector[] parts = v1.getPartitions();
    Storage[] resParts = StorageSwitch.applyComp(v1, v2, op);
    if (v2.isDense()) {
        long[] v2Values = v2.getStorage().getValues();
        int base = 0, k = 0;
        for (IntDoubleVector part : parts) {
            IntDoubleVectorStorage resPart = (IntDoubleVectorStorage) resParts[k];
            double[] newValues = resPart.getValues();
            if (part.isDense()) {
                double[] partValue = part.getStorage().getValues();
                for (int i = 0; i < partValue.length; i++) {
                    int idx = i + base;
                    newValues[i] = op.apply(partValue[i], v2Values[idx]);
                }
            } else if (part.isSparse()) {
                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 < newValues.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
                if (op.isKeepStorage()) {
                    int dim = part.getDim();
                    int[] resIndices = resPart.getIndices();
                    double[] resValues = resPart.getValues();
                    int[] partIndices = part.getStorage().getIndices();
                    double[] partValues = part.getStorage().getValues();
                    for (int i = 0; i < dim; i++) {
                        resIndices[i] = i;
                        resValues[i] = op.apply(0, v2Values[i + base]);
                    }
                    int size = part.size();
                    for (int i = 0; i < size; i++) {
                        int idx = partIndices[i];
                        resValues[idx] = op.apply(partValues[i], v2Values[idx + 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++) {
                            newValues[i] = op.apply(0, v2Values[i + base]);
                        }
                        int size = part.size();
                        for (int i = 0; i < size; i++) {
                            int idx = partIndices[i];
                            newValues[idx] = op.apply(partValues[i], v2Values[idx + base]);
                        }
                    } else {
                        IntDoubleVectorStorage partStorage = part.getStorage();
                        for (int i = 0; i < newValues.length; i++) {
                            if (partStorage.hasKey(i)) {
                                newValues[i] = op.apply(partStorage.get(i), v2Values[i + base]);
                            } else {
                                newValues[i] = op.apply(0, v2Values[i + base]);
                            }
                        }
                    }
                }
            }
            base += part.getDim();
            k++;
        }
    } else {
        if (v2.isSparse()) {
            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();
            ObjectIterator<Int2LongMap.Entry> iter = v2.getStorage().entryIterator();
            while (iter.hasNext()) {
                Int2LongMap.Entry entry = iter.next();
                int gidx = entry.getIntKey();
                int pidx = (int) (gidx / subDim);
                int subidx = gidx % subDim;
                ((IntDoubleVectorStorage) resParts[pidx]).set(subidx, op.apply(parts[pidx].get(subidx), entry.getLongValue()));
            }
        } else {
            // sorted
            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();
            int[] v2Indices = v2.getStorage().getIndices();
            long[] v2Values = v2.getStorage().getValues();
            for (int i = 0; i < v2Indices.length; i++) {
                int gidx = v2Indices[i];
                int pidx = (int) (gidx / subDim);
                int subidx = gidx % subDim;
                ((IntDoubleVectorStorage) resParts[pidx]).set(subidx, op.apply(parts[pidx].get(subidx), v2Values[i]));
            }
        }
    }
    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++;
    }
    return new CompIntDoubleVector(v1.getMatrixId(), v1.getRowId(), v1.getClock(), v1.getDim(), res, v1.getSubDim());
}
Also used : Int2DoubleMap(it.unimi.dsi.fastutil.ints.Int2DoubleMap) CompIntDoubleVector(com.tencent.angel.ml.math2.vector.CompIntDoubleVector) IntDoubleVector(com.tencent.angel.ml.math2.vector.IntDoubleVector) ObjectIterator(it.unimi.dsi.fastutil.objects.ObjectIterator) IntDoubleSparseVectorStorage(com.tencent.angel.ml.math2.storage.IntDoubleSparseVectorStorage) IntIntVectorStorage(com.tencent.angel.ml.math2.storage.IntIntVectorStorage) Storage(com.tencent.angel.ml.math2.storage.Storage) IntDoubleSparseVectorStorage(com.tencent.angel.ml.math2.storage.IntDoubleSparseVectorStorage) LongIntVectorStorage(com.tencent.angel.ml.math2.storage.LongIntVectorStorage) LongLongSparseVectorStorage(com.tencent.angel.ml.math2.storage.LongLongSparseVectorStorage) IntDoubleSortedVectorStorage(com.tencent.angel.ml.math2.storage.IntDoubleSortedVectorStorage) LongDoubleSparseVectorStorage(com.tencent.angel.ml.math2.storage.LongDoubleSparseVectorStorage) LongDoubleSortedVectorStorage(com.tencent.angel.ml.math2.storage.LongDoubleSortedVectorStorage) LongLongVectorStorage(com.tencent.angel.ml.math2.storage.LongLongVectorStorage) LongFloatVectorStorage(com.tencent.angel.ml.math2.storage.LongFloatVectorStorage) IntLongVectorStorage(com.tencent.angel.ml.math2.storage.IntLongVectorStorage) IntIntSortedVectorStorage(com.tencent.angel.ml.math2.storage.IntIntSortedVectorStorage) LongIntSortedVectorStorage(com.tencent.angel.ml.math2.storage.LongIntSortedVectorStorage) IntLongSortedVectorStorage(com.tencent.angel.ml.math2.storage.IntLongSortedVectorStorage) IntLongSparseVectorStorage(com.tencent.angel.ml.math2.storage.IntLongSparseVectorStorage) LongIntSparseVectorStorage(com.tencent.angel.ml.math2.storage.LongIntSparseVectorStorage) IntFloatVectorStorage(com.tencent.angel.ml.math2.storage.IntFloatVectorStorage) IntFloatSortedVectorStorage(com.tencent.angel.ml.math2.storage.IntFloatSortedVectorStorage) LongLongSortedVectorStorage(com.tencent.angel.ml.math2.storage.LongLongSortedVectorStorage) LongDoubleVectorStorage(com.tencent.angel.ml.math2.storage.LongDoubleVectorStorage) IntDoubleVectorStorage(com.tencent.angel.ml.math2.storage.IntDoubleVectorStorage) IntIntSparseVectorStorage(com.tencent.angel.ml.math2.storage.IntIntSparseVectorStorage) IntFloatSparseVectorStorage(com.tencent.angel.ml.math2.storage.IntFloatSparseVectorStorage) LongFloatSparseVectorStorage(com.tencent.angel.ml.math2.storage.LongFloatSparseVectorStorage) LongFloatSortedVectorStorage(com.tencent.angel.ml.math2.storage.LongFloatSortedVectorStorage) IntDoubleVectorStorage(com.tencent.angel.ml.math2.storage.IntDoubleVectorStorage) Int2LongMap(it.unimi.dsi.fastutil.ints.Int2LongMap) IntDoubleSortedVectorStorage(com.tencent.angel.ml.math2.storage.IntDoubleSortedVectorStorage) CompIntDoubleVector(com.tencent.angel.ml.math2.vector.CompIntDoubleVector)

Example 83 with IntDoubleVector

use of com.tencent.angel.ml.math2.vector.IntDoubleVector in project angel by Tencent.

the class MixedBinaryOutNonZAExecutor method apply.

private static Vector apply(CompIntDoubleVector v1, IntDoubleVector v2, Binary op) {
    IntDoubleVector[] parts = v1.getPartitions();
    Storage[] resParts = StorageSwitch.applyComp(v1, v2, op);
    if (v2.isDense()) {
        double[] v2Values = v2.getStorage().getValues();
        int base = 0, k = 0;
        for (IntDoubleVector part : parts) {
            IntDoubleVectorStorage resPart = (IntDoubleVectorStorage) resParts[k];
            double[] newValues = resPart.getValues();
            if (part.isDense()) {
                double[] partValue = part.getStorage().getValues();
                for (int i = 0; i < partValue.length; i++) {
                    int idx = i + base;
                    newValues[i] = op.apply(partValue[i], v2Values[idx]);
                }
            } else if (part.isSparse()) {
                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 < newValues.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
                if (op.isKeepStorage()) {
                    int dim = part.getDim();
                    int[] resIndices = resPart.getIndices();
                    double[] resValues = resPart.getValues();
                    int[] partIndices = part.getStorage().getIndices();
                    double[] partValues = part.getStorage().getValues();
                    for (int i = 0; i < dim; i++) {
                        resIndices[i] = i;
                        resValues[i] = op.apply(0, v2Values[i + base]);
                    }
                    int size = part.size();
                    for (int i = 0; i < size; i++) {
                        int idx = partIndices[i];
                        resValues[idx] = op.apply(partValues[i], v2Values[idx + 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++) {
                            newValues[i] = op.apply(0, v2Values[i + base]);
                        }
                        int size = part.size();
                        for (int i = 0; i < size; i++) {
                            int idx = partIndices[i];
                            newValues[idx] = op.apply(partValues[i], v2Values[idx + base]);
                        }
                    } else {
                        IntDoubleVectorStorage partStorage = part.getStorage();
                        for (int i = 0; i < newValues.length; i++) {
                            if (partStorage.hasKey(i)) {
                                newValues[i] = op.apply(partStorage.get(i), v2Values[i + base]);
                            } else {
                                newValues[i] = op.apply(0, v2Values[i + base]);
                            }
                        }
                    }
                }
            }
            base += part.getDim();
            k++;
        }
    } else {
        if (v2.isSparse()) {
            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();
            ObjectIterator<Int2DoubleMap.Entry> iter = v2.getStorage().entryIterator();
            while (iter.hasNext()) {
                Int2DoubleMap.Entry entry = iter.next();
                int gidx = entry.getIntKey();
                int pidx = (int) (gidx / subDim);
                int subidx = gidx % subDim;
                ((IntDoubleVectorStorage) resParts[pidx]).set(subidx, op.apply(parts[pidx].get(subidx), entry.getDoubleValue()));
            }
        } else {
            // sorted
            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();
            int[] v2Indices = v2.getStorage().getIndices();
            double[] v2Values = v2.getStorage().getValues();
            for (int i = 0; i < v2Indices.length; i++) {
                int gidx = v2Indices[i];
                int pidx = (int) (gidx / subDim);
                int subidx = gidx % subDim;
                ((IntDoubleVectorStorage) resParts[pidx]).set(subidx, op.apply(parts[pidx].get(subidx), v2Values[i]));
            }
        }
    }
    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++;
    }
    return new CompIntDoubleVector(v1.getMatrixId(), v1.getRowId(), v1.getClock(), v1.getDim(), res, v1.getSubDim());
}
Also used : Int2DoubleMap(it.unimi.dsi.fastutil.ints.Int2DoubleMap) CompIntDoubleVector(com.tencent.angel.ml.math2.vector.CompIntDoubleVector) IntDoubleVector(com.tencent.angel.ml.math2.vector.IntDoubleVector) ObjectIterator(it.unimi.dsi.fastutil.objects.ObjectIterator) IntDoubleSparseVectorStorage(com.tencent.angel.ml.math2.storage.IntDoubleSparseVectorStorage) IntIntVectorStorage(com.tencent.angel.ml.math2.storage.IntIntVectorStorage) Storage(com.tencent.angel.ml.math2.storage.Storage) IntDoubleSparseVectorStorage(com.tencent.angel.ml.math2.storage.IntDoubleSparseVectorStorage) LongIntVectorStorage(com.tencent.angel.ml.math2.storage.LongIntVectorStorage) LongLongSparseVectorStorage(com.tencent.angel.ml.math2.storage.LongLongSparseVectorStorage) IntDoubleSortedVectorStorage(com.tencent.angel.ml.math2.storage.IntDoubleSortedVectorStorage) LongDoubleSparseVectorStorage(com.tencent.angel.ml.math2.storage.LongDoubleSparseVectorStorage) LongDoubleSortedVectorStorage(com.tencent.angel.ml.math2.storage.LongDoubleSortedVectorStorage) LongLongVectorStorage(com.tencent.angel.ml.math2.storage.LongLongVectorStorage) LongFloatVectorStorage(com.tencent.angel.ml.math2.storage.LongFloatVectorStorage) IntLongVectorStorage(com.tencent.angel.ml.math2.storage.IntLongVectorStorage) IntIntSortedVectorStorage(com.tencent.angel.ml.math2.storage.IntIntSortedVectorStorage) LongIntSortedVectorStorage(com.tencent.angel.ml.math2.storage.LongIntSortedVectorStorage) IntLongSortedVectorStorage(com.tencent.angel.ml.math2.storage.IntLongSortedVectorStorage) IntLongSparseVectorStorage(com.tencent.angel.ml.math2.storage.IntLongSparseVectorStorage) LongIntSparseVectorStorage(com.tencent.angel.ml.math2.storage.LongIntSparseVectorStorage) IntFloatVectorStorage(com.tencent.angel.ml.math2.storage.IntFloatVectorStorage) IntFloatSortedVectorStorage(com.tencent.angel.ml.math2.storage.IntFloatSortedVectorStorage) LongLongSortedVectorStorage(com.tencent.angel.ml.math2.storage.LongLongSortedVectorStorage) LongDoubleVectorStorage(com.tencent.angel.ml.math2.storage.LongDoubleVectorStorage) IntDoubleVectorStorage(com.tencent.angel.ml.math2.storage.IntDoubleVectorStorage) IntIntSparseVectorStorage(com.tencent.angel.ml.math2.storage.IntIntSparseVectorStorage) IntFloatSparseVectorStorage(com.tencent.angel.ml.math2.storage.IntFloatSparseVectorStorage) LongFloatSparseVectorStorage(com.tencent.angel.ml.math2.storage.LongFloatSparseVectorStorage) LongFloatSortedVectorStorage(com.tencent.angel.ml.math2.storage.LongFloatSortedVectorStorage) IntDoubleVectorStorage(com.tencent.angel.ml.math2.storage.IntDoubleVectorStorage) IntDoubleSortedVectorStorage(com.tencent.angel.ml.math2.storage.IntDoubleSortedVectorStorage) CompIntDoubleVector(com.tencent.angel.ml.math2.vector.CompIntDoubleVector)

Example 84 with IntDoubleVector

use of com.tencent.angel.ml.math2.vector.IntDoubleVector in project angel by Tencent.

the class BinaryMatrixExecutor method apply.

private static Matrix apply(BlasDoubleMatrix mat, IntDoubleVector v, boolean onCol, Binary op) {
    double[] data = mat.getData();
    int m = mat.getNumRows(), n = mat.getNumCols();
    int size = v.size();
    byte[] flag = null;
    if (!v.isDense()) {
        flag = new byte[v.getDim()];
    }
    if (onCol && op.isInplace()) {
        if (v.isDense()) {
            double[] values = v.getStorage().getValues();
            for (int i = 0; i < m; i++) {
                double value = values[i];
                for (int j = 0; j < n; j++) {
                    data[i * n + j] = op.apply(data[i * n + j], value);
                }
            }
        } else if (v.isSparse()) {
            ObjectIterator<Int2DoubleMap.Entry> iter = v.getStorage().entryIterator();
            while (iter.hasNext()) {
                Int2DoubleMap.Entry entry = iter.next();
                int i = entry.getIntKey();
                flag[i] = 1;
                double value = entry.getDoubleValue();
                for (int j = 0; j < n; j++) {
                    data[i * n + j] = op.apply(data[i * n + j], value);
                }
            }
        } else {
            // sorted
            int[] idxs = v.getStorage().getIndices();
            double[] values = v.getStorage().getValues();
            for (int k = 0; k < size; k++) {
                int i = idxs[k];
                flag[i] = 1;
                double value = values[k];
                for (int j = 0; j < n; j++) {
                    data[i * n + j] = op.apply(data[i * n + j], value);
                }
            }
        }
        if (!v.isDense()) {
            switch(op.getOpType()) {
                case INTERSECTION:
                    for (int i = 0; i < m; i++) {
                        if (flag[i] == 0) {
                            for (int j = 0; j < n; j++) {
                                data[i * n + j] = 0;
                            }
                        }
                    }
                case UNION:
                    break;
                case ALL:
                    for (int i = 0; i < m; i++) {
                        if (flag[i] == 0) {
                            for (int j = 0; j < n; j++) {
                                data[i * n + j] = op.apply(data[i * n + j], 0);
                            }
                        }
                    }
            }
        }
        return mat;
    } else if (onCol && !op.isInplace()) {
        double[] newData;
        if (op.getOpType() == INTERSECTION) {
            newData = new double[m * n];
        } else {
            newData = ArrayCopy.copy(data);
        }
        if (v.isDense()) {
            double[] values = v.getStorage().getValues();
            for (int i = 0; i < m; i++) {
                double value = values[i];
                for (int j = 0; j < n; j++) {
                    newData[i * n + j] = op.apply(data[i * n + j], value);
                }
            }
        } else if (v.isSparse()) {
            ObjectIterator<Int2DoubleMap.Entry> iter = v.getStorage().entryIterator();
            while (iter.hasNext()) {
                Int2DoubleMap.Entry entry = iter.next();
                int i = entry.getIntKey();
                flag[i] = 1;
                double value = entry.getDoubleValue();
                for (int j = 0; j < n; j++) {
                    newData[i * n + j] = op.apply(data[i * n + j], value);
                }
            }
        } else {
            // sorted
            int[] idxs = v.getStorage().getIndices();
            double[] values = v.getStorage().getValues();
            for (int k = 0; k < size; k++) {
                int i = idxs[k];
                flag[i] = 1;
                double value = values[k];
                for (int j = 0; j < n; j++) {
                    newData[i * n + j] = op.apply(data[i * n + j], value);
                }
            }
        }
        if (!v.isDense()) {
            switch(op.getOpType()) {
                case INTERSECTION:
                    break;
                case UNION:
                    break;
                case ALL:
                    for (int i = 0; i < m; i++) {
                        if (flag[i] == 0) {
                            for (int j = 0; j < n; j++) {
                                newData[i * n + j] = op.apply(data[i * n + j], 0);
                            }
                        }
                    }
            }
        }
        return new BlasDoubleMatrix(mat.getMatrixId(), mat.getClock(), m, n, newData);
    } else if (!onCol && op.isInplace()) {
        if (v.isDense()) {
            double[] values = v.getStorage().getValues();
            for (int i = 0; i < m; i++) {
                for (int j = 0; j < n; j++) {
                    data[i * n + j] = op.apply(data[i * n + j], values[j]);
                }
            }
        } else if (v.isSparse()) {
            ObjectIterator<Int2DoubleMap.Entry> iter = v.getStorage().entryIterator();
            while (iter.hasNext()) {
                Int2DoubleMap.Entry entry = iter.next();
                int j = entry.getIntKey();
                double value = entry.getDoubleValue();
                flag[j] = 1;
                for (int i = 0; i < m; i++) {
                    data[i * n + j] = op.apply(data[i * n + j], value);
                }
            }
        } else {
            // sorted
            int[] idxs = v.getStorage().getIndices();
            double[] values = v.getStorage().getValues();
            for (int k = 0; k < size; k++) {
                int j = idxs[k];
                double value = values[k];
                flag[j] = 1;
                for (int i = 0; i < m; i++) {
                    data[i * n + j] = op.apply(data[i * n + j], value);
                }
            }
        }
        if (!v.isDense()) {
            switch(op.getOpType()) {
                case INTERSECTION:
                    for (int j = 0; j < n; j++) {
                        if (flag[j] == 0) {
                            for (int i = 0; i < m; i++) {
                                data[i * n + j] = 0;
                            }
                        }
                    }
                case UNION:
                    break;
                case ALL:
                    for (int j = 0; j < n; j++) {
                        if (flag[j] == 0) {
                            for (int i = 0; i < m; i++) {
                                data[i * n + j] = op.apply(data[i * n + j], 0);
                            }
                        }
                    }
            }
        }
        return mat;
    } else {
        double[] newData;
        if (op.getOpType() == INTERSECTION) {
            newData = new double[m * n];
        } else {
            newData = ArrayCopy.copy(data);
        }
        if (v.isDense()) {
            double[] values = v.getStorage().getValues();
            for (int j = 0; j < n; j++) {
                double value = values[j];
                for (int i = 0; i < m; i++) {
                    newData[i * n + j] = op.apply(data[i * n + j], value);
                }
            }
        } else if (v.isSparse()) {
            ObjectIterator<Int2DoubleMap.Entry> iter = v.getStorage().entryIterator();
            while (iter.hasNext()) {
                Int2DoubleMap.Entry entry = iter.next();
                int j = entry.getIntKey();
                flag[j] = 1;
                double value = entry.getDoubleValue();
                for (int i = 0; i < m; i++) {
                    newData[i * n + j] = op.apply(data[i * n + j], value);
                }
            }
        } else {
            // sorted
            int[] idxs = v.getStorage().getIndices();
            double[] values = v.getStorage().getValues();
            for (int k = 0; k < size; k++) {
                int j = idxs[k];
                flag[j] = 1;
                double value = values[k];
                for (int i = 0; i < m; i++) {
                    newData[i * n + j] = op.apply(data[i * n + j], value);
                }
            }
        }
        if (!v.isDense()) {
            switch(op.getOpType()) {
                case INTERSECTION:
                    break;
                case UNION:
                    break;
                case ALL:
                    for (int j = 0; j < n; j++) {
                        if (flag[j] == 0) {
                            for (int i = 0; i < m; i++) {
                                newData[i * n + j] = op.apply(data[i * n + j], 0);
                            }
                        }
                    }
            }
        }
        return new BlasDoubleMatrix(mat.getMatrixId(), mat.getClock(), m, n, newData);
    }
}
Also used : Int2DoubleMap(it.unimi.dsi.fastutil.ints.Int2DoubleMap) BlasDoubleMatrix(com.tencent.angel.ml.math2.matrix.BlasDoubleMatrix) ObjectIterator(it.unimi.dsi.fastutil.objects.ObjectIterator)

Example 85 with IntDoubleVector

use of com.tencent.angel.ml.math2.vector.IntDoubleVector in project angel by Tencent.

the class CompReduceExecutor method apply.

private static UnionEle apply(CompIntDoubleVector v, ReduceOP op, int start, int end) {
    UnionEle res = new UnionEle();
    IntDoubleVector[] parts = v.getPartitions();
    switch(op) {
        case Sum:
            for (int i = start; i <= end; i++) {
                res.setDouble1(res.getDouble1() + parts[i].sum());
            }
            break;
        case Avg:
            for (int i = start; i <= end; i++) {
                res.setDouble1(res.getDouble1() + parts[i].sum());
                res.setLong1(res.getLong1() + parts[i].getDim());
            }
            break;
        case Std:
            for (int i = start; i <= end; i++) {
                res.setDouble1(res.getDouble1() + parts[i].sum());
                double norm = parts[i].norm();
                res.setDouble2(res.getDouble2() + norm * norm);
                res.setLong1(res.getLong1() + parts[i].getDim());
            }
            break;
        case Norm:
            for (int i = start; i <= end; i++) {
                double norm = parts[i].norm();
                res.setDouble2(res.getDouble2() + norm * norm);
            }
            break;
        case Min:
            res.setDouble1(Double.MAX_VALUE);
            for (int i = start; i <= end; i++) {
                res.setDouble1(Math.min(res.getDouble1(), parts[i].min()));
            }
            break;
        case Max:
            res.setDouble1(Double.MIN_VALUE);
            for (int i = start; i <= end; i++) {
                res.setDouble1(Math.max(res.getDouble1(), parts[i].max()));
            }
            break;
        case Size:
            for (int i = start; i <= end; i++) {
                res.setLong1(res.getLong1() + parts[i].size());
            }
            break;
        case Numzeros:
            for (int i = start; i <= end; i++) {
                res.setLong1(res.getLong1() + parts[i].numZeros());
            }
            break;
    }
    return res;
}
Also used : UnionEle(com.tencent.angel.ml.math2.utils.UnionEle) CompIntDoubleVector(com.tencent.angel.ml.math2.vector.CompIntDoubleVector) IntDoubleVector(com.tencent.angel.ml.math2.vector.IntDoubleVector)

Aggregations

IntDoubleVector (com.tencent.angel.ml.math2.vector.IntDoubleVector)95 ObjectIterator (it.unimi.dsi.fastutil.objects.ObjectIterator)55 IntDoubleVectorStorage (com.tencent.angel.ml.math2.storage.IntDoubleVectorStorage)51 Int2DoubleMap (it.unimi.dsi.fastutil.ints.Int2DoubleMap)51 CompIntDoubleVector (com.tencent.angel.ml.math2.vector.CompIntDoubleVector)40 IntDoubleSparseVectorStorage (com.tencent.angel.ml.math2.storage.IntDoubleSparseVectorStorage)32 IntFloatVectorStorage (com.tencent.angel.ml.math2.storage.IntFloatVectorStorage)32 IntIntVectorStorage (com.tencent.angel.ml.math2.storage.IntIntVectorStorage)32 IntLongVectorStorage (com.tencent.angel.ml.math2.storage.IntLongVectorStorage)32 LongDoubleVectorStorage (com.tencent.angel.ml.math2.storage.LongDoubleVectorStorage)30 LongFloatVectorStorage (com.tencent.angel.ml.math2.storage.LongFloatVectorStorage)30 LongIntVectorStorage (com.tencent.angel.ml.math2.storage.LongIntVectorStorage)30 LongLongVectorStorage (com.tencent.angel.ml.math2.storage.LongLongVectorStorage)30 Storage (com.tencent.angel.ml.math2.storage.Storage)30 IntDoubleSortedVectorStorage (com.tencent.angel.ml.math2.storage.IntDoubleSortedVectorStorage)26 IntDoubleDenseVectorStorage (com.tencent.angel.ml.math2.storage.IntDoubleDenseVectorStorage)23 IntFloatSortedVectorStorage (com.tencent.angel.ml.math2.storage.IntFloatSortedVectorStorage)20 IntFloatSparseVectorStorage (com.tencent.angel.ml.math2.storage.IntFloatSparseVectorStorage)20 IntIntSortedVectorStorage (com.tencent.angel.ml.math2.storage.IntIntSortedVectorStorage)20 IntIntSparseVectorStorage (com.tencent.angel.ml.math2.storage.IntIntSparseVectorStorage)20