Search in sources :

Example 71 with ObjectIterator

use of it.unimi.dsi.fastutil.objects.ObjectIterator in project angel by Tencent.

the class MixedBinaryOutZAExecutor method apply.

private static Vector apply(CompLongIntVector v1, LongIntVector v2, Binary op) {
    LongIntVector[] parts = v1.getPartitions();
    Storage[] resParts = StorageSwitch.applyComp(v1, v2, op);
    if (v2.isSparse()) {
        ObjectIterator<Long2IntMap.Entry> iter = v2.getStorage().entryIterator();
        if (v1.size() > v2.size()) {
            long subDim = (v1.getDim() + v1.getNumPartitions() - 1) / v1.getNumPartitions();
            while (iter.hasNext()) {
                Long2IntMap.Entry entry = iter.next();
                long idx = entry.getLongKey();
                int pidx = (int) (idx / subDim);
                long subidx = idx % subDim;
                if (parts[pidx].hasKey(subidx)) {
                    ((LongIntVectorStorage) resParts[pidx]).set(subidx, op.apply(parts[pidx].get(subidx), entry.getIntValue()));
                }
            }
        } else {
            long base = 0;
            for (int i = 0; i < parts.length; i++) {
                LongIntVector part = parts[i];
                LongIntVectorStorage resPart = (LongIntVectorStorage) resParts[i];
                if (part.isDense()) {
                    int[] partValues = part.getStorage().getValues();
                    int[] 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<Long2IntMap.Entry> piter = part.getStorage().entryIterator();
                    while (piter.hasNext()) {
                        Long2IntMap.Entry entry = piter.next();
                        long idx = entry.getLongKey();
                        if (v2.hasKey(idx + base)) {
                            resPart.set(idx, op.apply(entry.getIntValue(), v2.get(idx + base)));
                        }
                    }
                } else {
                    // sorted
                    if (op.isKeepStorage()) {
                        long[] partIndices = part.getStorage().getIndices();
                        int[] partValues = part.getStorage().getValues();
                        long[] resPartIndices = resPart.getIndices();
                        int[] resPartValues = resPart.getValues();
                        for (int j = 0; j < partIndices.length; j++) {
                            long idx = partIndices[j];
                            if (v2.hasKey(idx + base)) {
                                resPartIndices[j] = idx;
                                resPartValues[j] = op.apply(partValues[j], v2.get(idx + base));
                            }
                        }
                    } else {
                        long[] partIndices = part.getStorage().getIndices();
                        int[] partValues = part.getStorage().getValues();
                        for (int j = 0; j < partIndices.length; j++) {
                            long 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()) {
            long subDim = (v1.getDim() + v1.getNumPartitions() - 1) / v1.getNumPartitions();
            long[] v2Indices = v2.getStorage().getIndices();
            int[] v2Values = v2.getStorage().getValues();
            for (int i = 0; i < v2Indices.length; i++) {
                long idx = v2Indices[i];
                int pidx = (int) (idx / subDim);
                long subidx = idx % subDim;
                if (parts[pidx].hasKey(subidx)) {
                    ((LongIntVectorStorage) resParts[pidx]).set(subidx, op.apply(parts[pidx].get(subidx), v2Values[i]));
                }
            }
        } else {
            long base = 0;
            for (int i = 0; i < parts.length; i++) {
                LongIntVector part = parts[i];
                LongIntVectorStorage resPart = (LongIntVectorStorage) resParts[i];
                if (part.isDense()) {
                    int[] partValues = part.getStorage().getValues();
                    int[] 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<Long2IntMap.Entry> piter = part.getStorage().entryIterator();
                    while (piter.hasNext()) {
                        Long2IntMap.Entry entry = piter.next();
                        long idx = entry.getLongKey();
                        if (v2.hasKey(idx + base)) {
                            resPart.set(idx, op.apply(entry.getIntValue(), v2.get(idx + base)));
                        }
                    }
                } else {
                    // sorted
                    if (op.isKeepStorage()) {
                        long[] partIndices = part.getStorage().getIndices();
                        int[] partValues = part.getStorage().getValues();
                        long[] resPartIndices = resPart.getIndices();
                        int[] resPartValues = resPart.getValues();
                        for (int j = 0; j < partIndices.length; j++) {
                            long idx = partIndices[j];
                            if (v2.hasKey(idx + base)) {
                                resPartIndices[j] = idx;
                                resPartValues[j] = op.apply(partValues[j], v2.get(idx + base));
                            }
                        }
                    } else {
                        long[] partIndices = part.getStorage().getIndices();
                        int[] partValues = part.getStorage().getValues();
                        for (int j = 0; j < partIndices.length; j++) {
                            long idx = partIndices[j];
                            if (v2.hasKey(idx + base)) {
                                resPart.set(idx, op.apply(partValues[j], v2.get(idx + base)));
                            }
                        }
                    }
                }
                base += part.getDim();
            }
        }
    }
    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++;
    }
    return new CompLongIntVector(v1.getMatrixId(), v1.getRowId(), v1.getClock(), v1.getDim(), res, v1.getSubDim());
}
Also used : CompLongIntVector(com.tencent.angel.ml.math2.vector.CompLongIntVector) LongIntVector(com.tencent.angel.ml.math2.vector.LongIntVector) LongIntVectorStorage(com.tencent.angel.ml.math2.storage.LongIntVectorStorage) Long2IntMap(it.unimi.dsi.fastutil.longs.Long2IntMap) 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) CompLongIntVector(com.tencent.angel.ml.math2.vector.CompLongIntVector)

Example 72 with ObjectIterator

use of it.unimi.dsi.fastutil.objects.ObjectIterator in project angel by Tencent.

the class MixedBinaryOutZAExecutor method apply.

private static Vector apply(CompIntDoubleVector v1, IntFloatVector v2, Binary op) {
    IntDoubleVector[] parts = v1.getPartitions();
    Storage[] resParts = StorageSwitch.applyComp(v1, v2, op);
    if (v2.isDense()) {
        int base = 0;
        float[] 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 < resPartValues.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<Int2FloatMap.Entry> iter = v2.getStorage().entryIterator();
        if (v1.size() > v2.size()) {
            int subDim = (v1.getDim() + v1.getNumPartitions() - 1) / v1.getNumPartitions();
            while (iter.hasNext()) {
                Int2FloatMap.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.getFloatValue()));
                }
            }
        } 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();
            float[] 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++;
    }
    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) 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) Int2FloatMap(it.unimi.dsi.fastutil.ints.Int2FloatMap) CompIntDoubleVector(com.tencent.angel.ml.math2.vector.CompIntDoubleVector)

Example 73 with ObjectIterator

use of it.unimi.dsi.fastutil.objects.ObjectIterator in project angel by Tencent.

the class MixedBinaryOutZAExecutor 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 base = 0;
        int[] 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 < resPartValues.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<Int2IntMap.Entry> iter = v2.getStorage().entryIterator();
        if (v1.size() > v2.size()) {
            int subDim = (v1.getDim() + v1.getNumPartitions() - 1) / v1.getNumPartitions();
            while (iter.hasNext()) {
                Int2IntMap.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.getIntValue()));
                }
            }
        } 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();
            int[] 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++;
    }
    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) 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) CompIntDoubleVector(com.tencent.angel.ml.math2.vector.CompIntDoubleVector) Int2IntMap(it.unimi.dsi.fastutil.ints.Int2IntMap)

Example 74 with ObjectIterator

use of it.unimi.dsi.fastutil.objects.ObjectIterator in project angel by Tencent.

the class SimpleBinaryInZAExecutor method apply.

private static Vector apply(IntLongVector v1, IntIntVector v2, Binary op) {
    IntLongVectorStorage newStorage = (IntLongVectorStorage) StorageSwitch.apply(v1, v2, op);
    if (v1.isDense() && v2.isDense()) {
        long[] v1Values = newStorage.getValues();
        int[] v2Values = v2.getStorage().getValues();
        for (int idx = 0; idx < v1Values.length; idx++) {
            v1Values[idx] = op.apply(v1Values[idx], v2Values[idx]);
        }
        return v1;
    } else if (v1.isDense() && v2.isSparse()) {
        long[] v1Values = newStorage.getValues();
        if (v2.size() < Constant.sparseDenseStorageThreshold * v2.getDim() || v1.getDim() < Constant.denseStorageThreshold) {
            // slower but memory efficient, for small vector only
            IntIntVectorStorage v2storage = v2.getStorage();
            for (int i = 0; i < v1Values.length; i++) {
                if (v2storage.hasKey(i)) {
                    v1Values[i] = op.apply(v1Values[i], v2.get(i));
                }
            }
        } else {
            // faster but not memory efficient
            long[] newValues = newStorage.getValues();
            ObjectIterator<Int2IntMap.Entry> iter = v2.getStorage().entryIterator();
            while (iter.hasNext()) {
                Int2IntMap.Entry entry = iter.next();
                int idx = entry.getIntKey();
                newValues[idx] = op.apply(v1Values[idx], entry.getIntValue());
            }
        }
        return v1;
    } else if (v1.isDense() && v2.isSorted()) {
        if ((v2.isSparse() && v2.getSize() >= Constant.sparseDenseStorageThreshold * v2.dim()) || (v2.isSorted() && v2.getSize() >= Constant.sortedDenseStorageThreshold * v2.dim())) {
            // dense preferred, KeepStorage is guaranteed
            long[] newValues = newStorage.getValues();
            long[] v1Values = v1.getStorage().getValues();
            int[] vIndices = v2.getStorage().getIndices();
            int[] v2Values = v2.getStorage().getValues();
            int size = v2.size();
            for (int i = 0; i < size; i++) {
                int idx = vIndices[i];
                newValues[idx] = op.apply(v1Values[idx], v2Values[i]);
            }
        } else {
            if (op.isKeepStorage()) {
                long[] newValues = newStorage.getValues();
                long[] v1Values = v1.getStorage().getValues();
                int[] vIndices = v2.getStorage().getIndices();
                int[] v2Values = v2.getStorage().getValues();
                int size = v2.size();
                for (int i = 0; i < size; i++) {
                    int idx = vIndices[i];
                    newValues[idx] = op.apply(v1Values[idx], v2Values[i]);
                }
            } else {
                long[] v1Values = v1.getStorage().getValues();
                int[] vIndices = v2.getStorage().getIndices();
                int[] v2Values = v2.getStorage().getValues();
                int size = v2.size();
                for (int i = 0; i < size; i++) {
                    int idx = vIndices[i];
                    newStorage.set(idx, op.apply(v1Values[idx], v2Values[i]));
                }
            }
        }
    } else if (v1.isSparse() && v2.isDense()) {
        if (op.isKeepStorage() || v1.getSize() <= Constant.sparseDenseStorageThreshold * v1.dim()) {
            // sparse preferred, keep storage guaranteed
            ObjectIterator<Int2LongMap.Entry> iter = v1.getStorage().entryIterator();
            int[] v2Values = v2.getStorage().getValues();
            while (iter.hasNext()) {
                Int2LongMap.Entry entry = iter.next();
                int idx = entry.getIntKey();
                newStorage.set(idx, op.apply(entry.getLongValue(), v2Values[idx]));
            }
        } else {
            // dense preferred
            long[] newValues = newStorage.getValues();
            ObjectIterator<Int2LongMap.Entry> iter = v1.getStorage().entryIterator();
            int[] v2Values = v2.getStorage().getValues();
            while (iter.hasNext()) {
                Int2LongMap.Entry entry = iter.next();
                int idx = entry.getIntKey();
                newValues[idx] = op.apply(entry.getLongValue(), v2Values[idx]);
            }
        }
    } else if (v1.isSparse() && v2.isSparse()) {
        if (v1.getSize() >= v2.getSize() && v2.getSize() <= Constant.sparseDenseStorageThreshold * v2.dim()) {
            // sparse preferred, keep storage guaranteed
            ObjectIterator<Int2IntMap.Entry> iter = v2.getStorage().entryIterator();
            IntLongVectorStorage v1storage = v1.getStorage();
            while (iter.hasNext()) {
                Int2IntMap.Entry entry = iter.next();
                int idx = entry.getIntKey();
                if (v1storage.hasKey(idx)) {
                    newStorage.set(idx, op.apply(v1.get(idx), entry.getIntValue()));
                }
            }
        } else if (v1.getSize() <= v2.getSize() && v1.getSize() <= Constant.sparseDenseStorageThreshold * v1.dim()) {
            // sparse preferred, keep storage guaranteed
            ObjectIterator<Int2LongMap.Entry> iter = v1.getStorage().entryIterator();
            IntIntVectorStorage v2storage = v2.getStorage();
            while (iter.hasNext()) {
                Int2LongMap.Entry entry = iter.next();
                int idx = entry.getIntKey();
                if (v2storage.hasKey(idx)) {
                    newStorage.set(idx, op.apply(entry.getLongValue(), v2.get(idx)));
                }
            }
        } else if (v1.getSize() > v2.getSize() && v2.getSize() > Constant.sparseDenseStorageThreshold * v2.dim()) {
            // preferred dense
            if (op.isKeepStorage()) {
                ObjectIterator<Int2IntMap.Entry> iter = v2.getStorage().entryIterator();
                IntLongVectorStorage v1storage = v1.getStorage();
                while (iter.hasNext()) {
                    Int2IntMap.Entry entry = iter.next();
                    int idx = entry.getIntKey();
                    if (v1storage.hasKey(idx)) {
                        newStorage.set(idx, op.apply(v1.get(idx), entry.getIntValue()));
                    }
                }
            } else {
                ObjectIterator<Int2IntMap.Entry> iter = v2.getStorage().entryIterator();
                IntLongVectorStorage v1storage = v1.getStorage();
                while (iter.hasNext()) {
                    Int2IntMap.Entry entry = iter.next();
                    int idx = entry.getIntKey();
                    if (v1storage.hasKey(idx)) {
                        newStorage.set(idx, op.apply(v1.get(idx), entry.getIntValue()));
                    }
                }
            }
        } else {
            // preferred dense
            if (op.isKeepStorage()) {
                ObjectIterator<Int2LongMap.Entry> iter = v1.getStorage().entryIterator();
                IntIntVectorStorage v2storage = v2.getStorage();
                while (iter.hasNext()) {
                    Int2LongMap.Entry entry = iter.next();
                    int idx = entry.getIntKey();
                    if (v2storage.hasKey(idx)) {
                        newStorage.set(idx, op.apply(entry.getLongValue(), v2.get(idx)));
                    }
                }
            } else {
                ObjectIterator<Int2LongMap.Entry> iter = v1.getStorage().entryIterator();
                IntIntVectorStorage v2storage = v2.getStorage();
                while (iter.hasNext()) {
                    Int2LongMap.Entry entry = iter.next();
                    int idx = entry.getIntKey();
                    if (v2storage.hasKey(idx)) {
                        newStorage.set(idx, op.apply(entry.getLongValue(), v2.get(idx)));
                    }
                }
            }
        }
    } else if (v1.isSparse() && v2.isSorted()) {
        if (v1.getSize() >= v2.getSize() && v2.getSize() <= Constant.sparseDenseStorageThreshold * v2.dim()) {
            // sparse preferred, keep storage guaranteed
            int[] v2Indices = v2.getStorage().getIndices();
            int[] v2Values = v2.getStorage().getValues();
            IntLongVectorStorage storage = v1.getStorage();
            int size = v2.size();
            for (int i = 0; i < size; i++) {
                int idx = v2Indices[i];
                if (storage.hasKey(idx)) {
                    newStorage.set(idx, op.apply(storage.get(idx), v2Values[i]));
                }
            }
        } else if (v1.getSize() <= v2.getSize() && v1.getSize() <= Constant.sparseDenseStorageThreshold * v1.dim()) {
            // sparse preferred, keep storage guaranteed
            ObjectIterator<Int2LongMap.Entry> iter = v1.getStorage().entryIterator();
            IntIntVectorStorage v2storage = v2.getStorage();
            while (iter.hasNext()) {
                Int2LongMap.Entry entry = iter.next();
                int idx = entry.getIntKey();
                if (v2storage.hasKey(idx)) {
                    newStorage.set(idx, op.apply(entry.getLongValue(), v2.get(idx)));
                }
            }
        } else if (v1.getSize() > v2.getSize() && v2.getSize() > Constant.sparseDenseStorageThreshold * v2.dim()) {
            // preferred dense
            int[] v2Indices = v2.getStorage().getIndices();
            int[] v2Values = v2.getStorage().getValues();
            IntLongVectorStorage storage = v1.getStorage();
            int size = v2.size();
            for (int i = 0; i < size; i++) {
                int idx = v2Indices[i];
                if (storage.hasKey(idx)) {
                    newStorage.set(idx, op.apply(storage.get(idx), v2Values[i]));
                }
            }
        } else {
            // preferred dense
            ObjectIterator<Int2LongMap.Entry> iter = v1.getStorage().entryIterator();
            IntIntVectorStorage v2storage = v2.getStorage();
            while (iter.hasNext()) {
                Int2LongMap.Entry entry = iter.next();
                int idx = entry.getIntKey();
                if (v2storage.hasKey(idx)) {
                    newStorage.set(idx, op.apply(entry.getLongValue(), v2.get(idx)));
                }
            }
        }
    } else if (v1.isSorted() && v2.isSparse()) {
        if (v1.getSize() >= v2.getSize() && v2.getSize() <= Constant.sortedDenseStorageThreshold * v2.dim()) {
            if (op.isKeepStorage()) {
                // sorted preferred v2.size
                ObjectIterator<Int2IntMap.Entry> iter = v2.getStorage().entryIterator();
                IntLongVectorStorage v1storage = v1.getStorage();
                while (iter.hasNext()) {
                    Int2IntMap.Entry entry = iter.next();
                    int idx = entry.getIntKey();
                    if (v1storage.hasKey(idx)) {
                        newStorage.set(idx, op.apply(v1storage.get(idx), entry.getIntValue()));
                    }
                }
            } else {
                // sparse preferred
                ObjectIterator<Int2IntMap.Entry> iter = v2.getStorage().entryIterator();
                IntLongVectorStorage v1storage = v1.getStorage();
                while (iter.hasNext()) {
                    Int2IntMap.Entry entry = iter.next();
                    int idx = entry.getIntKey();
                    if (v1storage.hasKey(idx)) {
                        newStorage.set(idx, op.apply(v1storage.get(idx), entry.getIntValue()));
                    }
                }
            }
        } else if (v1.getSize() <= v2.getSize() && v1.getSize() <= Constant.sortedDenseStorageThreshold * v1.dim()) {
            if (op.isKeepStorage()) {
                // sorted preferred v1.size
                int[] resIndices = newStorage.getIndices();
                long[] resValues = newStorage.getValues();
                int[] v1Indices = v1.getStorage().getIndices();
                long[] v1Values = v1.getStorage().getValues();
                IntIntVectorStorage storage = v2.getStorage();
                int size = v1.size();
                for (int i = 0; i < size; i++) {
                    int idx = v1Indices[i];
                    if (storage.hasKey(idx)) {
                        resIndices[i] = idx;
                        resValues[i] = op.apply(v1Values[i], storage.get(idx));
                    }
                }
            } else {
                int[] v1Indices = v1.getStorage().getIndices();
                long[] v1Values = v1.getStorage().getValues();
                IntIntVectorStorage storage = v2.getStorage();
                int size = v1.size();
                for (int i = 0; i < size; i++) {
                    int idx = v1Indices[i];
                    if (storage.hasKey(idx)) {
                        newStorage.set(idx, op.apply(v1Values[i], storage.get(idx)));
                    }
                }
            }
        } else if (v1.getSize() > v2.getSize() && v2.getSize() > Constant.sortedDenseStorageThreshold * v2.dim()) {
            ObjectIterator<Int2IntMap.Entry> iter = v2.getStorage().entryIterator();
            IntLongVectorStorage v1storage = v1.getStorage();
            while (iter.hasNext()) {
                Int2IntMap.Entry entry = iter.next();
                int idx = entry.getIntKey();
                if (v1storage.hasKey(idx)) {
                    newStorage.set(idx, op.apply(v1storage.get(idx), entry.getIntValue()));
                }
            }
        } else {
            // dense preferred
            if (op.isKeepStorage()) {
                // sorted preferred v1.size
                int[] resIndices = newStorage.getIndices();
                long[] resValues = newStorage.getValues();
                int[] v1Indices = v1.getStorage().getIndices();
                long[] v1Values = v1.getStorage().getValues();
                IntIntVectorStorage storage = v2.getStorage();
                int size = v1.size();
                for (int i = 0; i < size; i++) {
                    int idx = v1Indices[i];
                    if (storage.hasKey(idx)) {
                        resIndices[i] = idx;
                        resValues[i] = op.apply(v1Values[i], storage.get(idx));
                    }
                }
            } else {
                // dense preferred
                int[] v1Indices = v1.getStorage().getIndices();
                long[] v1Values = v1.getStorage().getValues();
                IntIntVectorStorage storage = v2.getStorage();
                int size = v1.size();
                for (int i = 0; i < size; i++) {
                    int idx = v1Indices[i];
                    if (storage.hasKey(idx)) {
                        newStorage.set(idx, op.apply(v1Values[i], storage.get(idx)));
                    }
                }
            }
        }
    } else if (v1.isSorted() && v2.isSorted()) {
        int v1Pointor = 0;
        int v2Pointor = 0;
        int size1 = v1.size();
        int size2 = v2.size();
        if (v1.getSize() >= v2.getSize() && v2.getSize() <= Constant.sortedDenseStorageThreshold * v2.dim()) {
            if (op.isKeepStorage()) {
                // sorted v2.size
                int[] v1Indices = v1.getStorage().getIndices();
                long[] v1Values = v1.getStorage().getValues();
                int[] v2Indices = v2.getStorage().getIndices();
                int[] v2Values = v2.getStorage().getValues();
                int[] resIndices = ArrayCopy.copy(v2Indices);
                long[] resValues = newStorage.getValues();
                while (v1Pointor < size1 && v2Pointor < size2) {
                    if (v1Indices[v1Pointor] == v2Indices[v2Pointor]) {
                        resValues[v2Pointor] = op.apply(v1Values[v1Pointor], v2Values[v2Pointor]);
                        v1Pointor++;
                        v2Pointor++;
                    } else if (v1Indices[v1Pointor] < v2Indices[v2Pointor]) {
                        v1Pointor++;
                    } else {
                        // v1Indices[v1Pointor] > v2Indices[v2Pointor]
                        v2Pointor++;
                    }
                }
                newStorage = new IntLongSortedVectorStorage(v1.getDim(), (int) v2.size(), resIndices, resValues);
            } else {
                // sparse preferred
                int[] v1Indices = v1.getStorage().getIndices();
                long[] v1Values = v1.getStorage().getValues();
                int[] v2Indices = v2.getStorage().getIndices();
                int[] v2Values = v2.getStorage().getValues();
                while (v1Pointor < size1 && v2Pointor < size2) {
                    if (v1Indices[v1Pointor] == v2Indices[v2Pointor]) {
                        newStorage.set(v1Indices[v1Pointor], op.apply(v1Values[v1Pointor], v2Values[v2Pointor]));
                        v1Pointor++;
                        v2Pointor++;
                    } else if (v1Indices[v1Pointor] < v2Indices[v2Pointor]) {
                        v1Pointor++;
                    } else {
                        // v1Indices[v1Pointor] > v2Indices[v2Pointor]
                        v2Pointor++;
                    }
                }
            }
        } else if (v1.getSize() <= v2.getSize() && v1.getSize() <= Constant.sortedDenseStorageThreshold * v1.dim()) {
            if (op.isKeepStorage()) {
                int[] v1Indices = v1.getStorage().getIndices();
                long[] v1Values = v1.getStorage().getValues();
                int[] v2Indices = v2.getStorage().getIndices();
                int[] v2Values = v2.getStorage().getValues();
                int[] resIndices = ArrayCopy.copy(v1Indices);
                long[] resValues = newStorage.getValues();
                while (v1Pointor < size1 && v2Pointor < size2) {
                    if (v1Indices[v1Pointor] == v2Indices[v2Pointor]) {
                        resValues[v1Pointor] = op.apply(v1Values[v1Pointor], v2Values[v2Pointor]);
                        v1Pointor++;
                        v2Pointor++;
                    } else if (v1Indices[v1Pointor] < v2Indices[v2Pointor]) {
                        v1Pointor++;
                    } else {
                        // v1Indices[v1Pointor] > v2Indices[v2Pointor]
                        v2Pointor++;
                    }
                }
                newStorage = new IntLongSortedVectorStorage(v1.getDim(), (int) v1.size(), resIndices, resValues);
            } else {
                // sparse preferred
                int[] v1Indices = v1.getStorage().getIndices();
                long[] v1Values = v1.getStorage().getValues();
                int[] v2Indices = v2.getStorage().getIndices();
                int[] v2Values = v2.getStorage().getValues();
                while (v1Pointor < size1 && v2Pointor < size2) {
                    if (v1Indices[v1Pointor] == v2Indices[v2Pointor]) {
                        newStorage.set(v1Indices[v1Pointor], op.apply(v1Values[v1Pointor], v2Values[v2Pointor]));
                        v1Pointor++;
                        v2Pointor++;
                    } else if (v1Indices[v1Pointor] < v2Indices[v2Pointor]) {
                        v1Pointor++;
                    } else {
                        // v1Indices[v1Pointor] > v2Indices[v2Pointor]
                        v2Pointor++;
                    }
                }
            }
        } else if (v1.getSize() > v2.getSize() && v2.getSize() > Constant.sortedDenseStorageThreshold * v2.dim()) {
            if (op.isKeepStorage()) {
                // sorted v2.size
                int[] v1Indices = v1.getStorage().getIndices();
                long[] v1Values = v1.getStorage().getValues();
                int[] v2Indices = v2.getStorage().getIndices();
                int[] v2Values = v2.getStorage().getValues();
                int[] resIndices = ArrayCopy.copy(v2Indices);
                long[] resValues = newStorage.getValues();
                while (v1Pointor < size1 && v2Pointor < size2) {
                    if (v1Indices[v1Pointor] == v2Indices[v2Pointor]) {
                        resValues[v2Pointor] = op.apply(v1Values[v1Pointor], v2Values[v2Pointor]);
                        v1Pointor++;
                        v2Pointor++;
                    } else if (v1Indices[v1Pointor] < v2Indices[v2Pointor]) {
                        v1Pointor++;
                    } else {
                        // v1Indices[v1Pointor] > v2Indices[v2Pointor]
                        v2Pointor++;
                    }
                }
                newStorage = new IntLongSortedVectorStorage(v1.getDim(), (int) v2.size(), resIndices, resValues);
            } else {
                // dense preferred
                int[] v1Indices = v1.getStorage().getIndices();
                long[] v1Values = v1.getStorage().getValues();
                int[] v2Indices = v2.getStorage().getIndices();
                int[] v2Values = v2.getStorage().getValues();
                while (v1Pointor < size1 && v2Pointor < size2) {
                    if (v1Indices[v1Pointor] == v2Indices[v2Pointor]) {
                        newStorage.set(v1Indices[v1Pointor], op.apply(v1Values[v1Pointor], v2Values[v2Pointor]));
                        v1Pointor++;
                        v2Pointor++;
                    } else if (v1Indices[v1Pointor] < v2Indices[v2Pointor]) {
                        v1Pointor++;
                    } else {
                        // v1Indices[v1Pointor] > v2Indices[v2Pointor]
                        v2Pointor++;
                    }
                }
            }
        } else {
            if (op.isKeepStorage()) {
                int[] v1Indices = v1.getStorage().getIndices();
                long[] v1Values = v1.getStorage().getValues();
                int[] v2Indices = v2.getStorage().getIndices();
                int[] v2Values = v2.getStorage().getValues();
                int[] resIndices = ArrayCopy.copy(v1Indices);
                long[] resValues = newStorage.getValues();
                while (v1Pointor < size1 && v2Pointor < size2) {
                    if (v1Indices[v1Pointor] == v2Indices[v2Pointor]) {
                        resValues[v1Pointor] = op.apply(v1Values[v1Pointor], v2Values[v2Pointor]);
                        v1Pointor++;
                        v2Pointor++;
                    } else if (v1Indices[v1Pointor] < v2Indices[v2Pointor]) {
                        v1Pointor++;
                    } else {
                        // v1Indices[v1Pointor] > v2Indices[v2Pointor]
                        v2Pointor++;
                    }
                }
                newStorage = new IntLongSortedVectorStorage(v1.getDim(), (int) v1.size(), resIndices, resValues);
            } else {
                // dense preferred
                int[] v1Indices = v1.getStorage().getIndices();
                long[] v1Values = v1.getStorage().getValues();
                int[] v2Indices = v2.getStorage().getIndices();
                int[] v2Values = v2.getStorage().getValues();
                long[] resValues = newStorage.getValues();
                while (v1Pointor < size1 && v2Pointor < size2) {
                    if (v1Indices[v1Pointor] == v2Indices[v2Pointor]) {
                        newStorage.set(v1Indices[v1Pointor], op.apply(v1Values[v1Pointor], v2Values[v2Pointor]));
                        v1Pointor++;
                        v2Pointor++;
                    } else if (v1Indices[v1Pointor] < v2Indices[v2Pointor]) {
                        v1Pointor++;
                    } else {
                        // v1Indices[v1Pointor] > v2Indices[v2Pointor]
                        v2Pointor++;
                    }
                }
            }
        }
    } else {
        throw new AngelException("The operation is not support!");
    }
    v1.setStorage(newStorage);
    return v1;
}
Also used : AngelException(com.tencent.angel.exception.AngelException) ObjectIterator(it.unimi.dsi.fastutil.objects.ObjectIterator) Int2LongMap(it.unimi.dsi.fastutil.ints.Int2LongMap) Int2IntMap(it.unimi.dsi.fastutil.ints.Int2IntMap)

Example 75 with ObjectIterator

use of it.unimi.dsi.fastutil.objects.ObjectIterator in project angel by Tencent.

the class SimpleBinaryInZAExecutor method apply.

private static Vector apply(LongLongVector v1, LongIntVector v2, Binary op) {
    LongLongVectorStorage newStorage = (LongLongVectorStorage) StorageSwitch.apply(v1, v2, op);
    if (v1.isSparse() && v2.isSparse()) {
        if (v1.getSize() >= v2.getSize() && v2.getSize() <= Constant.sparseDenseStorageThreshold * v2.dim()) {
            // sparse preferred, keep storage guaranteed
            ObjectIterator<Long2IntMap.Entry> iter = v2.getStorage().entryIterator();
            LongLongVectorStorage v1storage = v1.getStorage();
            while (iter.hasNext()) {
                Long2IntMap.Entry entry = iter.next();
                long idx = entry.getLongKey();
                if (v1storage.hasKey(idx)) {
                    newStorage.set(idx, op.apply(v1.get(idx), entry.getIntValue()));
                }
            }
        } else if (v1.getSize() <= v2.getSize() && v1.getSize() <= Constant.sparseDenseStorageThreshold * v1.dim()) {
            // sparse preferred, keep storage guaranteed
            ObjectIterator<Long2LongMap.Entry> iter = v1.getStorage().entryIterator();
            LongIntVectorStorage v2storage = v2.getStorage();
            while (iter.hasNext()) {
                Long2LongMap.Entry entry = iter.next();
                long idx = entry.getLongKey();
                if (v2storage.hasKey(idx)) {
                    newStorage.set(idx, op.apply(entry.getLongValue(), v2.get(idx)));
                }
            }
        } else if (v1.getSize() > v2.getSize() && v2.getSize() > Constant.sparseDenseStorageThreshold * v2.dim()) {
            // preferred dense
            if (op.isKeepStorage()) {
                ObjectIterator<Long2IntMap.Entry> iter = v2.getStorage().entryIterator();
                LongLongVectorStorage v1storage = v1.getStorage();
                while (iter.hasNext()) {
                    Long2IntMap.Entry entry = iter.next();
                    long idx = entry.getLongKey();
                    if (v1storage.hasKey(idx)) {
                        newStorage.set(idx, op.apply(v1.get(idx), entry.getIntValue()));
                    }
                }
            } else {
                ObjectIterator<Long2IntMap.Entry> iter = v2.getStorage().entryIterator();
                LongLongVectorStorage v1storage = v1.getStorage();
                while (iter.hasNext()) {
                    Long2IntMap.Entry entry = iter.next();
                    long idx = entry.getLongKey();
                    if (v1storage.hasKey(idx)) {
                        newStorage.set(idx, op.apply(v1.get(idx), entry.getIntValue()));
                    }
                }
            }
        } else {
            // preferred dense
            if (op.isKeepStorage()) {
                ObjectIterator<Long2LongMap.Entry> iter = v1.getStorage().entryIterator();
                LongIntVectorStorage v2storage = v2.getStorage();
                while (iter.hasNext()) {
                    Long2LongMap.Entry entry = iter.next();
                    long idx = entry.getLongKey();
                    if (v2storage.hasKey(idx)) {
                        newStorage.set(idx, op.apply(entry.getLongValue(), v2.get(idx)));
                    }
                }
            } else {
                ObjectIterator<Long2LongMap.Entry> iter = v1.getStorage().entryIterator();
                LongIntVectorStorage v2storage = v2.getStorage();
                while (iter.hasNext()) {
                    Long2LongMap.Entry entry = iter.next();
                    long idx = entry.getLongKey();
                    if (v2storage.hasKey(idx)) {
                        newStorage.set(idx, op.apply(entry.getLongValue(), v2.get(idx)));
                    }
                }
            }
        }
    } else if (v1.isSparse() && v2.isSorted()) {
        if (v1.getSize() >= v2.getSize() && v2.getSize() <= Constant.sparseDenseStorageThreshold * v2.dim()) {
            // sparse preferred, keep storage guaranteed
            long[] v2Indices = v2.getStorage().getIndices();
            int[] v2Values = v2.getStorage().getValues();
            LongLongVectorStorage storage = v1.getStorage();
            long size = v2.size();
            for (int i = 0; i < size; i++) {
                long idx = v2Indices[i];
                if (storage.hasKey(idx)) {
                    newStorage.set(idx, op.apply(storage.get(idx), v2Values[i]));
                }
            }
        } else if (v1.getSize() <= v2.getSize() && v1.getSize() <= Constant.sparseDenseStorageThreshold * v1.dim()) {
            // sparse preferred, keep storage guaranteed
            ObjectIterator<Long2LongMap.Entry> iter = v1.getStorage().entryIterator();
            LongIntVectorStorage v2storage = v2.getStorage();
            while (iter.hasNext()) {
                Long2LongMap.Entry entry = iter.next();
                long idx = entry.getLongKey();
                if (v2storage.hasKey(idx)) {
                    newStorage.set(idx, op.apply(entry.getLongValue(), v2.get(idx)));
                }
            }
        } else if (v1.getSize() > v2.getSize() && v2.getSize() > Constant.sparseDenseStorageThreshold * v2.dim()) {
            // preferred dense
            long[] v2Indices = v2.getStorage().getIndices();
            int[] v2Values = v2.getStorage().getValues();
            LongLongVectorStorage storage = v1.getStorage();
            long size = v2.size();
            for (int i = 0; i < size; i++) {
                long idx = v2Indices[i];
                if (storage.hasKey(idx)) {
                    newStorage.set(idx, op.apply(storage.get(idx), v2Values[i]));
                }
            }
        } else {
            // preferred dense
            ObjectIterator<Long2LongMap.Entry> iter = v1.getStorage().entryIterator();
            LongIntVectorStorage v2storage = v2.getStorage();
            while (iter.hasNext()) {
                Long2LongMap.Entry entry = iter.next();
                long idx = entry.getLongKey();
                if (v2storage.hasKey(idx)) {
                    newStorage.set(idx, op.apply(entry.getLongValue(), v2.get(idx)));
                }
            }
        }
    } else if (v1.isSorted() && v2.isSparse()) {
        if (v1.getSize() >= v2.getSize() && v2.getSize() <= Constant.sortedDenseStorageThreshold * v2.dim()) {
            if (op.isKeepStorage()) {
                // sorted preferred v2.size
                ObjectIterator<Long2IntMap.Entry> iter = v2.getStorage().entryIterator();
                LongLongVectorStorage v1storage = v1.getStorage();
                while (iter.hasNext()) {
                    Long2IntMap.Entry entry = iter.next();
                    long idx = entry.getLongKey();
                    if (v1storage.hasKey(idx)) {
                        newStorage.set(idx, op.apply(v1storage.get(idx), entry.getIntValue()));
                    }
                }
            } else {
                // sparse preferred
                ObjectIterator<Long2IntMap.Entry> iter = v2.getStorage().entryIterator();
                LongLongVectorStorage v1storage = v1.getStorage();
                while (iter.hasNext()) {
                    Long2IntMap.Entry entry = iter.next();
                    long idx = entry.getLongKey();
                    if (v1storage.hasKey(idx)) {
                        newStorage.set(idx, op.apply(v1storage.get(idx), entry.getIntValue()));
                    }
                }
            }
        } else if (v1.getSize() <= v2.getSize() && v1.getSize() <= Constant.sortedDenseStorageThreshold * v1.dim()) {
            if (op.isKeepStorage()) {
                // sorted preferred v1.size
                long[] resIndices = newStorage.getIndices();
                long[] resValues = newStorage.getValues();
                long[] v1Indices = v1.getStorage().getIndices();
                long[] v1Values = v1.getStorage().getValues();
                LongIntVectorStorage storage = v2.getStorage();
                long size = v1.size();
                for (int i = 0; i < size; i++) {
                    long idx = v1Indices[i];
                    if (storage.hasKey(idx)) {
                        resIndices[i] = idx;
                        resValues[i] = op.apply(v1Values[i], storage.get(idx));
                    }
                }
            } else {
                long[] v1Indices = v1.getStorage().getIndices();
                long[] v1Values = v1.getStorage().getValues();
                LongIntVectorStorage storage = v2.getStorage();
                long size = v1.size();
                for (int i = 0; i < size; i++) {
                    long idx = v1Indices[i];
                    if (storage.hasKey(idx)) {
                        newStorage.set(idx, op.apply(v1Values[i], storage.get(idx)));
                    }
                }
            }
        } else if (v1.getSize() > v2.getSize() && v2.getSize() > Constant.sortedDenseStorageThreshold * v2.dim()) {
            ObjectIterator<Long2IntMap.Entry> iter = v2.getStorage().entryIterator();
            LongLongVectorStorage v1storage = v1.getStorage();
            while (iter.hasNext()) {
                Long2IntMap.Entry entry = iter.next();
                long idx = entry.getLongKey();
                if (v1storage.hasKey(idx)) {
                    newStorage.set(idx, op.apply(v1storage.get(idx), entry.getIntValue()));
                }
            }
        } else {
            // dense preferred
            if (op.isKeepStorage()) {
                // sorted preferred v1.size
                long[] resIndices = newStorage.getIndices();
                long[] resValues = newStorage.getValues();
                long[] v1Indices = v1.getStorage().getIndices();
                long[] v1Values = v1.getStorage().getValues();
                LongIntVectorStorage storage = v2.getStorage();
                long size = v1.size();
                for (int i = 0; i < size; i++) {
                    long idx = v1Indices[i];
                    if (storage.hasKey(idx)) {
                        resIndices[i] = idx;
                        resValues[i] = op.apply(v1Values[i], storage.get(idx));
                    }
                }
            } else {
                // dense preferred
                long[] v1Indices = v1.getStorage().getIndices();
                long[] v1Values = v1.getStorage().getValues();
                LongIntVectorStorage storage = v2.getStorage();
                long size = v1.size();
                for (int i = 0; i < size; i++) {
                    long idx = v1Indices[i];
                    if (storage.hasKey(idx)) {
                        newStorage.set(idx, op.apply(v1Values[i], storage.get(idx)));
                    }
                }
            }
        }
    } else if (v1.isSorted() && v2.isSorted()) {
        int v1Pointor = 0;
        int v2Pointor = 0;
        long size1 = v1.size();
        long size2 = v2.size();
        if (v1.getSize() >= v2.getSize() && v2.getSize() <= Constant.sortedDenseStorageThreshold * v2.dim()) {
            if (op.isKeepStorage()) {
                // sorted v2.size
                long[] v1Indices = v1.getStorage().getIndices();
                long[] v1Values = v1.getStorage().getValues();
                long[] v2Indices = v2.getStorage().getIndices();
                int[] v2Values = v2.getStorage().getValues();
                long[] resIndices = ArrayCopy.copy(v2Indices);
                long[] resValues = newStorage.getValues();
                while (v1Pointor < size1 && v2Pointor < size2) {
                    if (v1Indices[v1Pointor] == v2Indices[v2Pointor]) {
                        resValues[v2Pointor] = op.apply(v1Values[v1Pointor], v2Values[v2Pointor]);
                        v1Pointor++;
                        v2Pointor++;
                    } else if (v1Indices[v1Pointor] < v2Indices[v2Pointor]) {
                        v1Pointor++;
                    } else {
                        // v1Indices[v1Pointor] > v2Indices[v2Pointor]
                        v2Pointor++;
                    }
                }
                newStorage = new LongLongSortedVectorStorage(v1.getDim(), (int) v2.size(), resIndices, resValues);
            } else {
                // sparse preferred
                long[] v1Indices = v1.getStorage().getIndices();
                long[] v1Values = v1.getStorage().getValues();
                long[] v2Indices = v2.getStorage().getIndices();
                int[] v2Values = v2.getStorage().getValues();
                while (v1Pointor < size1 && v2Pointor < size2) {
                    if (v1Indices[v1Pointor] == v2Indices[v2Pointor]) {
                        newStorage.set(v1Indices[v1Pointor], op.apply(v1Values[v1Pointor], v2Values[v2Pointor]));
                        v1Pointor++;
                        v2Pointor++;
                    } else if (v1Indices[v1Pointor] < v2Indices[v2Pointor]) {
                        v1Pointor++;
                    } else {
                        // v1Indices[v1Pointor] > v2Indices[v2Pointor]
                        v2Pointor++;
                    }
                }
            }
        } else if (v1.getSize() <= v2.getSize() && v1.getSize() <= Constant.sortedDenseStorageThreshold * v1.dim()) {
            if (op.isKeepStorage()) {
                long[] v1Indices = v1.getStorage().getIndices();
                long[] v1Values = v1.getStorage().getValues();
                long[] v2Indices = v2.getStorage().getIndices();
                int[] v2Values = v2.getStorage().getValues();
                long[] resIndices = ArrayCopy.copy(v1Indices);
                long[] resValues = newStorage.getValues();
                while (v1Pointor < size1 && v2Pointor < size2) {
                    if (v1Indices[v1Pointor] == v2Indices[v2Pointor]) {
                        resValues[v1Pointor] = op.apply(v1Values[v1Pointor], v2Values[v2Pointor]);
                        v1Pointor++;
                        v2Pointor++;
                    } else if (v1Indices[v1Pointor] < v2Indices[v2Pointor]) {
                        v1Pointor++;
                    } else {
                        // v1Indices[v1Pointor] > v2Indices[v2Pointor]
                        v2Pointor++;
                    }
                }
                newStorage = new LongLongSortedVectorStorage(v1.getDim(), (int) v1.size(), resIndices, resValues);
            } else {
                // sparse preferred
                long[] v1Indices = v1.getStorage().getIndices();
                long[] v1Values = v1.getStorage().getValues();
                long[] v2Indices = v2.getStorage().getIndices();
                int[] v2Values = v2.getStorage().getValues();
                while (v1Pointor < size1 && v2Pointor < size2) {
                    if (v1Indices[v1Pointor] == v2Indices[v2Pointor]) {
                        newStorage.set(v1Indices[v1Pointor], op.apply(v1Values[v1Pointor], v2Values[v2Pointor]));
                        v1Pointor++;
                        v2Pointor++;
                    } else if (v1Indices[v1Pointor] < v2Indices[v2Pointor]) {
                        v1Pointor++;
                    } else {
                        // v1Indices[v1Pointor] > v2Indices[v2Pointor]
                        v2Pointor++;
                    }
                }
            }
        } else if (v1.getSize() > v2.getSize() && v2.getSize() > Constant.sortedDenseStorageThreshold * v2.dim()) {
            if (op.isKeepStorage()) {
                // sorted v2.size
                long[] v1Indices = v1.getStorage().getIndices();
                long[] v1Values = v1.getStorage().getValues();
                long[] v2Indices = v2.getStorage().getIndices();
                int[] v2Values = v2.getStorage().getValues();
                long[] resIndices = ArrayCopy.copy(v2Indices);
                long[] resValues = newStorage.getValues();
                while (v1Pointor < size1 && v2Pointor < size2) {
                    if (v1Indices[v1Pointor] == v2Indices[v2Pointor]) {
                        resValues[v2Pointor] = op.apply(v1Values[v1Pointor], v2Values[v2Pointor]);
                        v1Pointor++;
                        v2Pointor++;
                    } else if (v1Indices[v1Pointor] < v2Indices[v2Pointor]) {
                        v1Pointor++;
                    } else {
                        // v1Indices[v1Pointor] > v2Indices[v2Pointor]
                        v2Pointor++;
                    }
                }
                newStorage = new LongLongSortedVectorStorage(v1.getDim(), (int) v2.size(), resIndices, resValues);
            } else {
                // dense preferred
                long[] v1Indices = v1.getStorage().getIndices();
                long[] v1Values = v1.getStorage().getValues();
                long[] v2Indices = v2.getStorage().getIndices();
                int[] v2Values = v2.getStorage().getValues();
                while (v1Pointor < size1 && v2Pointor < size2) {
                    if (v1Indices[v1Pointor] == v2Indices[v2Pointor]) {
                        newStorage.set(v1Indices[v1Pointor], op.apply(v1Values[v1Pointor], v2Values[v2Pointor]));
                        v1Pointor++;
                        v2Pointor++;
                    } else if (v1Indices[v1Pointor] < v2Indices[v2Pointor]) {
                        v1Pointor++;
                    } else {
                        // v1Indices[v1Pointor] > v2Indices[v2Pointor]
                        v2Pointor++;
                    }
                }
            }
        } else {
            if (op.isKeepStorage()) {
                long[] v1Indices = v1.getStorage().getIndices();
                long[] v1Values = v1.getStorage().getValues();
                long[] v2Indices = v2.getStorage().getIndices();
                int[] v2Values = v2.getStorage().getValues();
                long[] resIndices = ArrayCopy.copy(v1Indices);
                long[] resValues = newStorage.getValues();
                while (v1Pointor < size1 && v2Pointor < size2) {
                    if (v1Indices[v1Pointor] == v2Indices[v2Pointor]) {
                        resValues[v1Pointor] = op.apply(v1Values[v1Pointor], v2Values[v2Pointor]);
                        v1Pointor++;
                        v2Pointor++;
                    } else if (v1Indices[v1Pointor] < v2Indices[v2Pointor]) {
                        v1Pointor++;
                    } else {
                        // v1Indices[v1Pointor] > v2Indices[v2Pointor]
                        v2Pointor++;
                    }
                }
                newStorage = new LongLongSortedVectorStorage(v1.getDim(), (int) v1.size(), resIndices, resValues);
            } else {
                // dense preferred
                long[] v1Indices = v1.getStorage().getIndices();
                long[] v1Values = v1.getStorage().getValues();
                long[] v2Indices = v2.getStorage().getIndices();
                int[] v2Values = v2.getStorage().getValues();
                long[] resValues = newStorage.getValues();
                while (v1Pointor < size1 && v2Pointor < size2) {
                    if (v1Indices[v1Pointor] == v2Indices[v2Pointor]) {
                        newStorage.set(v1Indices[v1Pointor], op.apply(v1Values[v1Pointor], v2Values[v2Pointor]));
                        v1Pointor++;
                        v2Pointor++;
                    } else if (v1Indices[v1Pointor] < v2Indices[v2Pointor]) {
                        v1Pointor++;
                    } else {
                        // v1Indices[v1Pointor] > v2Indices[v2Pointor]
                        v2Pointor++;
                    }
                }
            }
        }
    } else {
        throw new AngelException("The operation is not support!");
    }
    v1.setStorage(newStorage);
    return v1;
}
Also used : AngelException(com.tencent.angel.exception.AngelException) Long2IntMap(it.unimi.dsi.fastutil.longs.Long2IntMap) ObjectIterator(it.unimi.dsi.fastutil.objects.ObjectIterator) Long2LongMap(it.unimi.dsi.fastutil.longs.Long2LongMap)

Aggregations

ObjectIterator (it.unimi.dsi.fastutil.objects.ObjectIterator)254 IntDoubleVectorStorage (com.tencent.angel.ml.math2.storage.IntDoubleVectorStorage)115 IntFloatVectorStorage (com.tencent.angel.ml.math2.storage.IntFloatVectorStorage)114 IntLongVectorStorage (com.tencent.angel.ml.math2.storage.IntLongVectorStorage)111 IntIntVectorStorage (com.tencent.angel.ml.math2.storage.IntIntVectorStorage)110 AngelException (com.tencent.angel.exception.AngelException)100 LongDoubleVectorStorage (com.tencent.angel.ml.math2.storage.LongDoubleVectorStorage)100 LongFloatVectorStorage (com.tencent.angel.ml.math2.storage.LongFloatVectorStorage)99 LongLongVectorStorage (com.tencent.angel.ml.math2.storage.LongLongVectorStorage)98 LongIntVectorStorage (com.tencent.angel.ml.math2.storage.LongIntVectorStorage)97 Storage (com.tencent.angel.ml.math2.storage.Storage)96 Int2FloatMap (it.unimi.dsi.fastutil.ints.Int2FloatMap)62 Int2LongMap (it.unimi.dsi.fastutil.ints.Int2LongMap)57 Int2DoubleMap (it.unimi.dsi.fastutil.ints.Int2DoubleMap)56 Int2IntMap (it.unimi.dsi.fastutil.ints.Int2IntMap)55 IntDoubleVector (com.tencent.angel.ml.math2.vector.IntDoubleVector)47 IntDoubleSortedVectorStorage (com.tencent.angel.ml.math2.storage.IntDoubleSortedVectorStorage)44 LongDoubleSortedVectorStorage (com.tencent.angel.ml.math2.storage.LongDoubleSortedVectorStorage)44 IntFloatSortedVectorStorage (com.tencent.angel.ml.math2.storage.IntFloatSortedVectorStorage)43 LongFloatSortedVectorStorage (com.tencent.angel.ml.math2.storage.LongFloatSortedVectorStorage)43