Search in sources :

Example 31 with IntDoubleVector

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

the class MixedDotExecutor method apply.

private static double apply(CompIntDoubleVector v1, IntLongVector v2) {
    double dotValue = 0.0;
    if (v2.isDense()) {
        int base = 0;
        long[] v2Values = v2.getStorage().getValues();
        for (IntDoubleVector part : v1.getPartitions()) {
            if (part.isDense()) {
                double[] partValues = part.getStorage().getValues();
                for (int i = 0; i < partValues.length; i++) {
                    int idx = base + i;
                    dotValue += partValues[i] * v2Values[idx];
                }
            } else if (part.isSparse()) {
                ObjectIterator<Int2DoubleMap.Entry> iter = part.getStorage().entryIterator();
                while (iter.hasNext()) {
                    Int2DoubleMap.Entry entry = iter.next();
                    int idx = base + entry.getIntKey();
                    dotValue += entry.getDoubleValue() * v2Values[idx];
                }
            } else {
                // isSorted
                int[] partIndices = part.getStorage().getIndices();
                double[] partValues = part.getStorage().getValues();
                for (int i = 0; i < partIndices.length; i++) {
                    int idx = base + partIndices[i];
                    dotValue += partValues[i] * v2Values[idx];
                }
            }
            base += part.getDim();
        }
    } else if (v2.isSparse()) {
        ObjectIterator<Int2LongMap.Entry> iter = v2.getStorage().entryIterator();
        while (iter.hasNext()) {
            Int2LongMap.Entry entry = iter.next();
            int idx = entry.getIntKey();
            dotValue += v1.get(idx) * entry.getLongValue();
        }
    } else if (v2.isSorted() && v1.size() > v2.size()) {
        // v2 is sorted
        int[] v2Indices = v2.getStorage().getIndices();
        long[] v2Values = v2.getStorage().getValues();
        for (int i = 0; i < v2Indices.length; i++) {
            int idx = v2Indices[i];
            dotValue += v1.get(idx) * v2Values[i];
        }
    } else {
        int base = 0;
        for (IntDoubleVector part : v1.getPartitions()) {
            if (part.isDense()) {
                double[] partValues = part.getStorage().getValues();
                for (int i = 0; i < partValues.length; i++) {
                    int idx = base + i;
                    dotValue += partValues[i] * v2.get(idx);
                }
            } else if (part.isSparse()) {
                ObjectIterator<Int2DoubleMap.Entry> iter = part.getStorage().entryIterator();
                while (iter.hasNext()) {
                    Int2DoubleMap.Entry entry = iter.next();
                    int idx = base + entry.getIntKey();
                    dotValue += entry.getDoubleValue() * v2.get(idx);
                }
            } else {
                // isSorted
                int[] partIndices = part.getStorage().getIndices();
                double[] partValues = part.getStorage().getValues();
                for (int i = 0; i < partIndices.length; i++) {
                    int idx = base + partIndices[i];
                    dotValue += partValues[i] * v2.get(idx);
                }
            }
            base += part.getDim();
        }
    }
    return dotValue;
}
Also used : Int2DoubleMap(it.unimi.dsi.fastutil.ints.Int2DoubleMap) Int2LongMap(it.unimi.dsi.fastutil.ints.Int2LongMap) CompIntDoubleVector(com.tencent.angel.ml.math2.vector.CompIntDoubleVector) IntDoubleVector(com.tencent.angel.ml.math2.vector.IntDoubleVector) ObjectIterator(it.unimi.dsi.fastutil.objects.ObjectIterator)

Example 32 with IntDoubleVector

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

the class MixedDotExecutor method apply.

private static double apply(CompIntDoubleVector v1, IntDoubleVector v2) {
    double dotValue = 0.0;
    if (v2.isDense()) {
        int base = 0;
        double[] v2Values = v2.getStorage().getValues();
        for (IntDoubleVector part : v1.getPartitions()) {
            if (part.isDense()) {
                double[] partValues = part.getStorage().getValues();
                for (int i = 0; i < partValues.length; i++) {
                    int idx = base + i;
                    dotValue += partValues[i] * v2Values[idx];
                }
            } else if (part.isSparse()) {
                ObjectIterator<Int2DoubleMap.Entry> iter = part.getStorage().entryIterator();
                while (iter.hasNext()) {
                    Int2DoubleMap.Entry entry = iter.next();
                    int idx = base + entry.getIntKey();
                    dotValue += entry.getDoubleValue() * v2Values[idx];
                }
            } else {
                // isSorted
                int[] partIndices = part.getStorage().getIndices();
                double[] partValues = part.getStorage().getValues();
                for (int i = 0; i < partIndices.length; i++) {
                    int idx = base + partIndices[i];
                    dotValue += partValues[i] * v2Values[idx];
                }
            }
            base += part.getDim();
        }
    } else if (v2.isSparse()) {
        ObjectIterator<Int2DoubleMap.Entry> iter = v2.getStorage().entryIterator();
        while (iter.hasNext()) {
            Int2DoubleMap.Entry entry = iter.next();
            int idx = entry.getIntKey();
            dotValue += v1.get(idx) * entry.getDoubleValue();
        }
    } else if (v2.isSorted() && v1.size() > v2.size()) {
        // v2 is sorted
        int[] v2Indices = v2.getStorage().getIndices();
        double[] v2Values = v2.getStorage().getValues();
        for (int i = 0; i < v2Indices.length; i++) {
            int idx = v2Indices[i];
            dotValue += v1.get(idx) * v2Values[i];
        }
    } else {
        int base = 0;
        for (IntDoubleVector part : v1.getPartitions()) {
            if (part.isDense()) {
                double[] partValues = part.getStorage().getValues();
                for (int i = 0; i < partValues.length; i++) {
                    int idx = base + i;
                    dotValue += partValues[i] * v2.get(idx);
                }
            } else if (part.isSparse()) {
                ObjectIterator<Int2DoubleMap.Entry> iter = part.getStorage().entryIterator();
                while (iter.hasNext()) {
                    Int2DoubleMap.Entry entry = iter.next();
                    int idx = base + entry.getIntKey();
                    dotValue += entry.getDoubleValue() * v2.get(idx);
                }
            } else {
                // isSorted
                int[] partIndices = part.getStorage().getIndices();
                double[] partValues = part.getStorage().getValues();
                for (int i = 0; i < partIndices.length; i++) {
                    int idx = base + partIndices[i];
                    dotValue += partValues[i] * v2.get(idx);
                }
            }
            base += part.getDim();
        }
    }
    return dotValue;
}
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)

Example 33 with IntDoubleVector

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

the class ByteBufSerdeUtils method main.

public static void main(String[] args) {
    boolean b = true;
    ByteBuf buf = ByteBufUtils.newByteBuf(serializedBooleanLen(b), true);
    serializeBoolean(buf, b);
    assert serializedBooleanLen(b) == buf.writerIndex();
    boolean db = deserializeBoolean(buf);
    assert b == db;
    buf.release();
    byte by = 1;
    buf = ByteBufUtils.newByteBuf(serializedByteLen(by), true);
    serializeByte(buf, by);
    assert serializedByteLen(by) == buf.writerIndex();
    byte dby = deserializeByte(buf);
    assert by == dby;
    buf.release();
    char c = 'a';
    buf = ByteBufUtils.newByteBuf(serializedCharLen(c), true);
    serializeChar(buf, c);
    assert serializedCharLen(c) == buf.writerIndex();
    char dc = deserializeChar(buf);
    assert c == dc;
    buf.release();
    int i = 124;
    buf = ByteBufUtils.newByteBuf(serializedIntLen(i), true);
    serializeInt(buf, i);
    assert serializedIntLen(i) == buf.writerIndex();
    int di = deserializeInt(buf);
    assert i == di;
    buf.release();
    long l = 124L;
    buf = ByteBufUtils.newByteBuf(serializedLongLen(l), true);
    serializeLong(buf, l);
    assert serializedLongLen(l) == buf.writerIndex();
    long dl = deserializeLong(buf);
    assert l == dl;
    buf.release();
    float f = 12.34f;
    buf = ByteBufUtils.newByteBuf(serializedFloatLen(f), true);
    serializeFloat(buf, f);
    assert serializedFloatLen(f) == buf.writerIndex();
    float df = deserializeFloat(buf);
    assert f == df;
    double d = 12.34;
    buf = ByteBufUtils.newByteBuf(serializedDoubleLen(d), true);
    serializeDouble(buf, d);
    assert serializedDoubleLen(d) == buf.writerIndex();
    double dd = deserializeDouble(buf);
    assert d == dd;
    buf.release();
    String s = "123";
    buf = ByteBufUtils.newByteBuf(serializedUTF8Len(s), true);
    serializeUTF8(buf, s);
    assert serializedUTF8Len(s) == buf.writerIndex();
    String ds = deserializeUTF8(buf);
    assert s.equalsIgnoreCase(ds);
    buf.release();
    int[] ia = new int[3];
    for (i = 0; i < ia.length; i++) {
        ia[i] = i;
    }
    buf = ByteBufUtils.newByteBuf(serializedIntsLen(ia, 0, ia.length), true);
    serializeInts(buf, ia);
    assert serializedIntsLen(ia, 0, ia.length) == buf.writerIndex();
    int[] dia = deserializeInts(buf);
    assert ia.length == dia.length;
    for (i = 0; i < ia.length; i++) {
        assert ia[i] == dia[i];
    }
    buf.release();
    float[] fa = new float[3];
    for (i = 0; i < ia.length; i++) {
        fa[i] = i;
    }
    buf = ByteBufUtils.newByteBuf(serializedFloatsLen(fa, 0, fa.length), true);
    serializeFloats(buf, fa);
    assert serializedFloatsLen(fa, 0, fa.length) == buf.writerIndex();
    float[] dfa = deserializeFloats(buf);
    assert fa.length == dfa.length;
    for (i = 0; i < fa.length; i++) {
        assert fa[i] == dfa[i];
    }
    buf.release();
    double[] da = new double[3];
    for (i = 0; i < ia.length; i++) {
        da[i] = i;
    }
    buf = ByteBufUtils.newByteBuf(serializedDoublesLen(da, 0, da.length), true);
    serializeDoubles(buf, da, 0, da.length);
    assert serializedDoublesLen(da, 0, fa.length) == buf.writerIndex();
    double[] dda = deserializeDoubles(buf);
    assert da.length == dda.length;
    for (i = 0; i < da.length; i++) {
        assert da[i] == dda[i];
    }
    buf.release();
    long[] la = new long[3];
    for (i = 0; i < la.length; i++) {
        la[i] = i;
    }
    buf = ByteBufUtils.newByteBuf(serializedLongsLen(la, 0, la.length), true);
    serializeLongs(buf, la, 0, da.length);
    assert serializedLongsLen(la, 0, la.length) == buf.writerIndex();
    long[] dla = deserializeLongs(buf);
    assert la.length == dla.length;
    for (i = 0; i < la.length; i++) {
        assert la[i] == dla[i];
    }
    buf.release();
    IntDoubleVector didVector = VFactory.denseDoubleVector(da);
    buf = ByteBufUtils.newByteBuf(serializedVectorLen(didVector));
    serializeVector(buf, didVector);
    assert serializedVectorLen(didVector) == buf.writerIndex();
    IntDoubleVector ddidVector = (IntDoubleVector) deserializeVector(buf);
    for (i = 0; i < da.length; i++) {
        assert ddidVector.get(i) == da[i];
    }
}
Also used : ByteBuf(io.netty.buffer.ByteBuf) IntDoubleVector(com.tencent.angel.ml.math2.vector.IntDoubleVector)

Example 34 with IntDoubleVector

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

the class ByteBufSerdeUtils method serializedIntDoubleVectorLen.

public static int serializedIntDoubleVectorLen(IntDoubleVector vector) {
    int len = 0;
    IntDoubleVectorStorage storage = vector.getStorage();
    if (storage.isDense()) {
        len += serializedIntLen(DENSE_STORAGE_TYPE);
        len += serializedDoublesLen(storage.getValues());
    } else if (storage.isSparse()) {
        len += serializedIntLen(SPARSE_STORAGE_TYPE);
        len += serializedIntLen(storage.size());
        len += storage.size() * (INT_LENGTH + DOUBLE_LENGTH);
    } else if (storage.isSorted()) {
        len += serializedIntLen(SORTED_STORAGE_TYPE);
        len += serializedIntsLen(vector.getStorage().getIndices());
        len += serializedDoublesLen(vector.getStorage().getValues());
    } else {
        throw new UnsupportedOperationException("Unsupport storage type " + vector.getStorage().getClass());
    }
    return len;
}
Also used : IntDoubleVectorStorage(com.tencent.angel.ml.math2.storage.IntDoubleVectorStorage)

Example 35 with IntDoubleVector

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

the class ByteBufSerdeUtils method deserializeIntDoubleVector.

private static IntDoubleVector deserializeIntDoubleVector(ByteBuf in, long dim) {
    int storageType = deserializeInt(in);
    switch(storageType) {
        case DENSE_STORAGE_TYPE:
            return VFactory.denseDoubleVector(deserializeDoubles(in));
        case SPARSE_STORAGE_TYPE:
            int len = deserializeInt(in);
            Int2DoubleOpenHashMap idToValueMap = new Int2DoubleOpenHashMap(len);
            for (int i = 0; i < len; i++) {
                idToValueMap.put(deserializeInt(in), deserializeDouble(in));
            }
            return new IntDoubleVector((int) dim, new IntDoubleSparseVectorStorage((int) dim, idToValueMap));
        case SORTED_STORAGE_TYPE:
            return VFactory.sortedDoubleVector((int) dim, deserializeInts(in), deserializeDoubles(in));
        default:
            throw new UnsupportedOperationException("Unsupport storage type " + storageType);
    }
}
Also used : IntDoubleSparseVectorStorage(com.tencent.angel.ml.math2.storage.IntDoubleSparseVectorStorage) Int2DoubleOpenHashMap(it.unimi.dsi.fastutil.ints.Int2DoubleOpenHashMap) IntDoubleVector(com.tencent.angel.ml.math2.vector.IntDoubleVector)

Aggregations

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