Search in sources :

Example 31 with IntLongVectorStorage

use of com.tencent.angel.ml.math2.storage.IntLongVectorStorage in project angel by Tencent.

the class IntLongVector method argmin.

public int argmin() {
    IntLongVectorStorage idstorage = (IntLongVectorStorage) storage;
    if (idstorage.size() == 0)
        return -1;
    long minval = Long.MAX_VALUE;
    int minidx = -1;
    if (idstorage.isDense()) {
        long[] val = idstorage.getValues();
        int length = val.length;
        for (int idx = 0; idx < length; idx++) {
            if (val[idx] < minval) {
                minval = val[idx];
                minidx = idx;
            }
        }
    } else if (idstorage.isSparse()) {
        ObjectIterator<Int2LongMap.Entry> iter = idstorage.entryIterator();
        while (iter.hasNext()) {
            Int2LongMap.Entry entry = iter.next();
            int idx = entry.getIntKey();
            long val = entry.getLongValue();
            if (val < minval) {
                minval = val;
                minidx = idx;
            }
        }
    } else {
        int[] indices = idstorage.getIndices();
        long[] val = idstorage.getValues();
        int size = idstorage.size();
        for (int i = 0; i < size; i++) {
            int idx = indices[i];
            if (val[i] < minval) {
                minval = val[i];
                minidx = idx;
            }
        }
    }
    return minidx;
}
Also used : IntLongVectorStorage(com.tencent.angel.ml.math2.storage.IntLongVectorStorage) Int2LongMap(it.unimi.dsi.fastutil.ints.Int2LongMap) ObjectIterator(it.unimi.dsi.fastutil.objects.ObjectIterator)

Example 32 with IntLongVectorStorage

use of com.tencent.angel.ml.math2.storage.IntLongVectorStorage in project angel by Tencent.

the class IntLongVector method numZeros.

public int numZeros() {
    IntLongVectorStorage dstorage = (IntLongVectorStorage) storage;
    if (dstorage.size() == 0)
        return (int) dim;
    int numZero = 0;
    if (dstorage.isSparse()) {
        LongIterator iter = dstorage.valueIterator();
        while (iter.hasNext()) {
            if (iter.nextLong() != 0) {
                numZero += 1;
            }
        }
    } else {
        for (long val : dstorage.getValues()) {
            if (val != 0) {
                numZero += 1;
            }
        }
    }
    return (int) getDim() - numZero;
}
Also used : IntLongVectorStorage(com.tencent.angel.ml.math2.storage.IntLongVectorStorage) LongIterator(it.unimi.dsi.fastutil.longs.LongIterator)

Example 33 with IntLongVectorStorage

use of com.tencent.angel.ml.math2.storage.IntLongVectorStorage in project angel by Tencent.

the class CompIntLongRowUpdateSplit method serialize.

@Override
public void serialize(ByteBuf buf) {
    super.serialize(buf);
    IntLongVectorStorage storage = split.getStorage();
    if (storage instanceof IntLongSparseVectorStorage) {
        buf.writeInt(storage.size());
        ObjectIterator<Int2LongMap.Entry> iter = storage.entryIterator();
        Int2LongMap.Entry entry;
        while (iter.hasNext()) {
            entry = iter.next();
            buf.writeInt(entry.getIntKey());
            buf.writeLong(entry.getLongValue());
        }
    } else if (storage instanceof IntLongSortedVectorStorage) {
        buf.writeInt(storage.size());
        int[] indices = storage.getIndices();
        long[] values = storage.getValues();
        for (int i = 0; i < indices.length; i++) {
            buf.writeInt(indices[i]);
            buf.writeLong(values[i]);
        }
    } else if (storage instanceof IntLongDenseVectorStorage) {
        long[] values = storage.getValues();
        int writeSize = values.length < maxItemNum ? values.length : maxItemNum;
        buf.writeInt(writeSize);
        for (int i = 0; i < writeSize; i++) {
            buf.writeLong(values[i]);
        }
    } else {
        throw new UnsupportedOperationException("unsupport split for storage " + storage.getClass().getName());
    }
}
Also used : IntLongSparseVectorStorage(com.tencent.angel.ml.math2.storage.IntLongSparseVectorStorage) IntLongVectorStorage(com.tencent.angel.ml.math2.storage.IntLongVectorStorage) IntLongSortedVectorStorage(com.tencent.angel.ml.math2.storage.IntLongSortedVectorStorage) Int2LongMap(it.unimi.dsi.fastutil.ints.Int2LongMap) IntLongDenseVectorStorage(com.tencent.angel.ml.math2.storage.IntLongDenseVectorStorage)

Example 34 with IntLongVectorStorage

use of com.tencent.angel.ml.math2.storage.IntLongVectorStorage in project angel by Tencent.

the class MixedBinaryOutZAExecutor method apply.

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

Example 35 with IntLongVectorStorage

use of com.tencent.angel.ml.math2.storage.IntLongVectorStorage in project angel by Tencent.

the class MixedBinaryOutZAExecutor method apply.

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

Aggregations

IntLongVectorStorage (com.tencent.angel.ml.math2.storage.IntLongVectorStorage)41 Int2LongMap (it.unimi.dsi.fastutil.ints.Int2LongMap)31 ObjectIterator (it.unimi.dsi.fastutil.objects.ObjectIterator)29 IntLongVector (com.tencent.angel.ml.math2.vector.IntLongVector)25 IntDoubleVectorStorage (com.tencent.angel.ml.math2.storage.IntDoubleVectorStorage)20 IntFloatVectorStorage (com.tencent.angel.ml.math2.storage.IntFloatVectorStorage)20 IntIntVectorStorage (com.tencent.angel.ml.math2.storage.IntIntVectorStorage)20 LongDoubleVectorStorage (com.tencent.angel.ml.math2.storage.LongDoubleVectorStorage)18 LongFloatVectorStorage (com.tencent.angel.ml.math2.storage.LongFloatVectorStorage)18 LongIntVectorStorage (com.tencent.angel.ml.math2.storage.LongIntVectorStorage)18 LongLongVectorStorage (com.tencent.angel.ml.math2.storage.LongLongVectorStorage)18 Storage (com.tencent.angel.ml.math2.storage.Storage)18 CompIntLongVector (com.tencent.angel.ml.math2.vector.CompIntLongVector)18 IntLongSortedVectorStorage (com.tencent.angel.ml.math2.storage.IntLongSortedVectorStorage)17 IntLongSparseVectorStorage (com.tencent.angel.ml.math2.storage.IntLongSparseVectorStorage)15 AngelException (com.tencent.angel.exception.AngelException)12 IntDoubleSortedVectorStorage (com.tencent.angel.ml.math2.storage.IntDoubleSortedVectorStorage)12 IntDoubleSparseVectorStorage (com.tencent.angel.ml.math2.storage.IntDoubleSparseVectorStorage)12 IntFloatSortedVectorStorage (com.tencent.angel.ml.math2.storage.IntFloatSortedVectorStorage)12 IntFloatSparseVectorStorage (com.tencent.angel.ml.math2.storage.IntFloatSparseVectorStorage)12