Search in sources :

Example 1 with IntLongSparseVectorStorage

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

the class MixedBinaryOutNonZAExecutor method apply.

private static Vector apply(CompIntLongVector v1, IntDummyVector v2, Binary op) {
    IntLongVector[] parts = v1.getPartitions();
    Storage[] resParts = StorageSwitch.applyComp(v1, v2, op);
    if (!op.isKeepStorage()) {
        for (int i = 0; i < parts.length; i++) {
            if (parts[i].getStorage() instanceof IntLongSortedVectorStorage) {
                resParts[i] = new IntLongSparseVectorStorage(parts[i].getDim(), parts[i].getStorage().getIndices(), parts[i].getStorage().getValues());
            }
        }
    }
    int subDim = (v1.getDim() + v1.getNumPartitions() - 1) / v1.getNumPartitions();
    int[] v2Indices = v2.getIndices();
    for (int i = 0; i < v2Indices.length; i++) {
        int gidx = v2Indices[i];
        int pidx = (int) (gidx / subDim);
        int subidx = gidx % subDim;
        ((IntLongVectorStorage) resParts[pidx]).set(subidx, op.apply(parts[pidx].get(subidx), 1));
    }
    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) IntLongSparseVectorStorage(com.tencent.angel.ml.math2.storage.IntLongSparseVectorStorage) IntIntVectorStorage(com.tencent.angel.ml.math2.storage.IntIntVectorStorage) Storage(com.tencent.angel.ml.math2.storage.Storage) IntDoubleSparseVectorStorage(com.tencent.angel.ml.math2.storage.IntDoubleSparseVectorStorage) LongIntVectorStorage(com.tencent.angel.ml.math2.storage.LongIntVectorStorage) LongLongSparseVectorStorage(com.tencent.angel.ml.math2.storage.LongLongSparseVectorStorage) IntDoubleSortedVectorStorage(com.tencent.angel.ml.math2.storage.IntDoubleSortedVectorStorage) LongDoubleSparseVectorStorage(com.tencent.angel.ml.math2.storage.LongDoubleSparseVectorStorage) LongDoubleSortedVectorStorage(com.tencent.angel.ml.math2.storage.LongDoubleSortedVectorStorage) LongLongVectorStorage(com.tencent.angel.ml.math2.storage.LongLongVectorStorage) LongFloatVectorStorage(com.tencent.angel.ml.math2.storage.LongFloatVectorStorage) IntLongVectorStorage(com.tencent.angel.ml.math2.storage.IntLongVectorStorage) IntIntSortedVectorStorage(com.tencent.angel.ml.math2.storage.IntIntSortedVectorStorage) LongIntSortedVectorStorage(com.tencent.angel.ml.math2.storage.LongIntSortedVectorStorage) IntLongSortedVectorStorage(com.tencent.angel.ml.math2.storage.IntLongSortedVectorStorage) IntLongSparseVectorStorage(com.tencent.angel.ml.math2.storage.IntLongSparseVectorStorage) LongIntSparseVectorStorage(com.tencent.angel.ml.math2.storage.LongIntSparseVectorStorage) IntFloatVectorStorage(com.tencent.angel.ml.math2.storage.IntFloatVectorStorage) IntFloatSortedVectorStorage(com.tencent.angel.ml.math2.storage.IntFloatSortedVectorStorage) LongLongSortedVectorStorage(com.tencent.angel.ml.math2.storage.LongLongSortedVectorStorage) LongDoubleVectorStorage(com.tencent.angel.ml.math2.storage.LongDoubleVectorStorage) IntDoubleVectorStorage(com.tencent.angel.ml.math2.storage.IntDoubleVectorStorage) IntIntSparseVectorStorage(com.tencent.angel.ml.math2.storage.IntIntSparseVectorStorage) IntFloatSparseVectorStorage(com.tencent.angel.ml.math2.storage.IntFloatSparseVectorStorage) LongFloatSparseVectorStorage(com.tencent.angel.ml.math2.storage.LongFloatSparseVectorStorage) LongFloatSortedVectorStorage(com.tencent.angel.ml.math2.storage.LongFloatSortedVectorStorage) IntLongSortedVectorStorage(com.tencent.angel.ml.math2.storage.IntLongSortedVectorStorage) IntLongVectorStorage(com.tencent.angel.ml.math2.storage.IntLongVectorStorage) CompIntLongVector(com.tencent.angel.ml.math2.vector.CompIntLongVector)

Example 2 with IntLongSparseVectorStorage

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

the class RBCompIntLongMatrix method initEmpty.

@Override
public void initEmpty(int idx) {
    int numComp = (int) ((getDim() + subDim - 1) / subDim);
    if (null == rows[idx]) {
        IntLongVector[] tmpParts = new IntLongVector[numComp];
        for (int i = 0; i < numComp; i++) {
            IntLongSparseVectorStorage storage = new IntLongSparseVectorStorage(subDim);
            tmpParts[i] = new IntLongVector(matrixId, idx, clock, (int) getDim(), storage);
        }
        CompIntLongVector tmpVect = new CompIntLongVector(matrixId, idx, clock, (int) getDim(), tmpParts, subDim);
        rows[idx] = tmpVect;
    }
}
Also used : CompIntLongVector(com.tencent.angel.ml.math2.vector.CompIntLongVector) IntLongVector(com.tencent.angel.ml.math2.vector.IntLongVector) IntLongSparseVectorStorage(com.tencent.angel.ml.math2.storage.IntLongSparseVectorStorage) CompIntLongVector(com.tencent.angel.ml.math2.vector.CompIntLongVector)

Example 3 with IntLongSparseVectorStorage

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

the class MixedBinaryOutAllExecutor 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()) {
        long[] v2Values = v2.getStorage().getValues();
        int base = 0, k = 0;
        for (IntLongVector part : parts) {
            IntLongVectorStorage resPart = (IntLongVectorStorage) resParts[k];
            if (part.isDense()) {
                long[] partValue = part.getStorage().getValues();
                long[] resPartValues = resPart.getValues();
                for (int i = 0; i < partValue.length; i++) {
                    int idx = i;
                    resPartValues[i] = op.apply(partValue[i], v2Values[idx + base]);
                }
            } else if (part.isSparse()) {
                if (part.size() < Constant.denseLoopThreshold * part.getDim()) {
                    for (int i = 0; i < part.getDim(); i++) {
                        resPart.set(i, op.apply(0, v2Values[i + base]));
                    }
                    ObjectIterator<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 {
                    for (int i = 0; i < resPart.size(); i++) {
                        if (part.getStorage().hasKey(i)) {
                            resPart.set(i, op.apply(part.get(i), v2Values[i + base]));
                        } else {
                            resPart.set(i, op.apply(0, v2Values[i + base]));
                        }
                    }
                }
            } else {
                // sorted
                if (op.isKeepStorage()) {
                    int[] resPartIndices = resPart.getIndices();
                    long[] resPartValues = resPart.getValues();
                    if (part.size() < Constant.denseLoopThreshold * part.getDim()) {
                        int[] partIndices = part.getStorage().getIndices();
                        long[] partValues = part.getStorage().getValues();
                        for (int i = 0; i < part.getDim(); i++) {
                            resPartIndices[i] = i;
                            resPartValues[i] = op.apply(0, v2Values[i + base]);
                        }
                        int size = part.size();
                        for (int i = 0; i < size; i++) {
                            int idx = partIndices[i];
                            resPartValues[idx] = op.apply(partValues[i], v2Values[idx + base]);
                        }
                    } else {
                        IntLongVectorStorage partStorage = part.getStorage();
                        for (int i = 0; i < resPartValues.length; i++) {
                            if (partStorage.hasKey(i)) {
                                resPartIndices[i] = i;
                                resPartValues[i] = op.apply(partStorage.get(i), v2Values[i + base]);
                            } else {
                                resPartIndices[i] = i;
                                resPartValues[i] = op.apply(0, v2Values[i + base]);
                            }
                        }
                    }
                } else {
                    long[] resPartValues = resPart.getValues();
                    if (part.size() < Constant.denseLoopThreshold * part.getDim()) {
                        int[] partIndices = part.getStorage().getIndices();
                        long[] partValues = part.getStorage().getValues();
                        for (int i = 0; i < part.getDim(); i++) {
                            resPartValues[i] = op.apply(0, v2Values[i + base]);
                        }
                        int size = part.size();
                        for (int i = 0; i < size; i++) {
                            int idx = partIndices[i];
                            resPartValues[idx] = op.apply(partValues[i], v2Values[idx + base]);
                        }
                    } else {
                        IntLongVectorStorage partStorage = part.getStorage();
                        for (int i = 0; i < resPartValues.length; i++) {
                            if (partStorage.hasKey(i)) {
                                resPartValues[i] = op.apply(partStorage.get(i), v2Values[i + base]);
                            } else {
                                resPartValues[i] = op.apply(0, v2Values[i + base]);
                            }
                        }
                    }
                }
            }
            base += part.getDim();
            k++;
        }
    } else {
        if (!op.isKeepStorage()) {
            for (int i = 0; i < parts.length; i++) {
                if (parts[i].getStorage() instanceof IntLongSortedVectorStorage) {
                    resParts[i] = new IntLongSparseVectorStorage(parts[i].getDim(), parts[i].getStorage().getIndices(), parts[i].getStorage().getValues());
                }
            }
        }
        int subDim = (v1.getDim() + v1.getNumPartitions() - 1) / v1.getNumPartitions();
        for (int i = 0; i < v1.getDim(); i++) {
            int pidx = (int) (i / subDim);
            int subidx = i % subDim;
            if (v2.getStorage().hasKey(i)) {
                ((IntLongVectorStorage) resParts[pidx]).set(subidx, op.apply(parts[pidx].get(subidx), v2.get(i)));
            } else {
                ((IntLongVectorStorage) resParts[pidx]).set(subidx, op.apply(parts[pidx].get(subidx), 0));
            }
        }
    }
    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) IntLongSortedVectorStorage(com.tencent.angel.ml.math2.storage.IntLongSortedVectorStorage) CompIntLongVector(com.tencent.angel.ml.math2.vector.CompIntLongVector) ObjectIterator(it.unimi.dsi.fastutil.objects.ObjectIterator) IntLongSparseVectorStorage(com.tencent.angel.ml.math2.storage.IntLongSparseVectorStorage) IntIntVectorStorage(com.tencent.angel.ml.math2.storage.IntIntVectorStorage) Storage(com.tencent.angel.ml.math2.storage.Storage) IntDoubleSparseVectorStorage(com.tencent.angel.ml.math2.storage.IntDoubleSparseVectorStorage) LongIntVectorStorage(com.tencent.angel.ml.math2.storage.LongIntVectorStorage) LongLongSparseVectorStorage(com.tencent.angel.ml.math2.storage.LongLongSparseVectorStorage) IntDoubleSortedVectorStorage(com.tencent.angel.ml.math2.storage.IntDoubleSortedVectorStorage) LongDoubleSparseVectorStorage(com.tencent.angel.ml.math2.storage.LongDoubleSparseVectorStorage) LongDoubleSortedVectorStorage(com.tencent.angel.ml.math2.storage.LongDoubleSortedVectorStorage) LongLongVectorStorage(com.tencent.angel.ml.math2.storage.LongLongVectorStorage) LongFloatVectorStorage(com.tencent.angel.ml.math2.storage.LongFloatVectorStorage) IntLongVectorStorage(com.tencent.angel.ml.math2.storage.IntLongVectorStorage) IntIntSortedVectorStorage(com.tencent.angel.ml.math2.storage.IntIntSortedVectorStorage) LongIntSortedVectorStorage(com.tencent.angel.ml.math2.storage.LongIntSortedVectorStorage) IntLongSortedVectorStorage(com.tencent.angel.ml.math2.storage.IntLongSortedVectorStorage) IntLongSparseVectorStorage(com.tencent.angel.ml.math2.storage.IntLongSparseVectorStorage) LongIntSparseVectorStorage(com.tencent.angel.ml.math2.storage.LongIntSparseVectorStorage) IntFloatVectorStorage(com.tencent.angel.ml.math2.storage.IntFloatVectorStorage) IntFloatSortedVectorStorage(com.tencent.angel.ml.math2.storage.IntFloatSortedVectorStorage) LongLongSortedVectorStorage(com.tencent.angel.ml.math2.storage.LongLongSortedVectorStorage) LongDoubleVectorStorage(com.tencent.angel.ml.math2.storage.LongDoubleVectorStorage) IntDoubleVectorStorage(com.tencent.angel.ml.math2.storage.IntDoubleVectorStorage) IntIntSparseVectorStorage(com.tencent.angel.ml.math2.storage.IntIntSparseVectorStorage) IntFloatSparseVectorStorage(com.tencent.angel.ml.math2.storage.IntFloatSparseVectorStorage) LongFloatSparseVectorStorage(com.tencent.angel.ml.math2.storage.LongFloatSparseVectorStorage) LongFloatSortedVectorStorage(com.tencent.angel.ml.math2.storage.LongFloatSortedVectorStorage) IntLongVectorStorage(com.tencent.angel.ml.math2.storage.IntLongVectorStorage) Int2LongMap(it.unimi.dsi.fastutil.ints.Int2LongMap)

Example 4 with IntLongSparseVectorStorage

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

the class UpdatePSFTest method testSparseLongUDF.

public void testSparseLongUDF() throws Exception {
    Worker worker = LocalClusterContext.get().getWorker(workerAttempt0Id).getWorker();
    MatrixClient client1 = worker.getPSAgent().getMatrixClient(SPARSE_LONG_MAT, 0);
    int matrixW1Id = client1.getMatrixId();
    int[] index = genIndexs(feaNum, nnz);
    IntLongVector deltaVec = new IntLongVector(feaNum, new IntLongSparseVectorStorage(feaNum, nnz));
    for (int i = 0; i < index.length; i++) {
        deltaVec.set(index[i], index[i]);
    }
    // for (int i = 0; i < feaNum; i++) {
    // deltaVec.set(i, i);
    // }
    deltaVec.setRowId(0);
    Vector[] updates = new Vector[1];
    updates[0] = deltaVec;
    client1.asyncUpdate(new IncrementRows(new IncrementRowsParam(matrixW1Id, updates))).get();
    IntLongVector row = (IntLongVector) client1.getRow(0);
    for (int id : index) {
        // System.out.println("id=" + id + ", value=" + row.get(id));
        Assert.assertTrue(row.get(id) == deltaVec.get(id));
    }
    Assert.assertTrue(index.length == row.size());
}
Also used : IntLongVector(com.tencent.angel.ml.math2.vector.IntLongVector) IntLongSparseVectorStorage(com.tencent.angel.ml.math2.storage.IntLongSparseVectorStorage) IncrementRowsParam(com.tencent.angel.ml.matrix.psf.update.update.IncrementRowsParam) IncrementRows(com.tencent.angel.ml.matrix.psf.update.update.IncrementRows) Worker(com.tencent.angel.worker.Worker) MatrixClient(com.tencent.angel.psagent.matrix.MatrixClient) IntLongVector(com.tencent.angel.ml.math2.vector.IntLongVector) LongIntVector(com.tencent.angel.ml.math2.vector.LongIntVector) Vector(com.tencent.angel.ml.math2.vector.Vector) LongFloatVector(com.tencent.angel.ml.math2.vector.LongFloatVector) IntDoubleVector(com.tencent.angel.ml.math2.vector.IntDoubleVector) IntFloatVector(com.tencent.angel.ml.math2.vector.IntFloatVector) LongDoubleVector(com.tencent.angel.ml.math2.vector.LongDoubleVector) LongLongVector(com.tencent.angel.ml.math2.vector.LongLongVector) IntIntVector(com.tencent.angel.ml.math2.vector.IntIntVector)

Example 5 with IntLongSparseVectorStorage

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

the class HashRouterUtils method splitIntLongVector.

public static void splitIntLongVector(KeyHash hasher, MatrixMeta matrixMeta, IntLongVector vector, KeyValuePart[] dataParts) {
    int dataPartNum = dataParts.length;
    int dataPartNumMinus1 = dataPartNum - 1;
    if (isPow2(dataPartNum)) {
        IntLongVectorStorage storage = vector.getStorage();
        if (storage.isSparse()) {
            // Use iterator
            IntLongSparseVectorStorage sparseStorage = (IntLongSparseVectorStorage) storage;
            ObjectIterator<Int2LongMap.Entry> iter = sparseStorage.entryIterator();
            while (iter.hasNext()) {
                Int2LongMap.Entry keyValue = iter.next();
                int partId = computeHashCode(hasher, keyValue.getIntKey()) & dataPartNumMinus1;
                ((HashIntKeysLongValuesPart) dataParts[partId]).add(keyValue.getIntKey(), keyValue.getLongValue());
            }
        } else if (storage.isDense()) {
            // Get values
            IntLongDenseVectorStorage denseStorage = (IntLongDenseVectorStorage) storage;
            long[] values = denseStorage.getValues();
            for (int i = 0; i < values.length; i++) {
                int partId = computeHashCode(hasher, i) & dataPartNumMinus1;
                ((HashIntKeysLongValuesPart) dataParts[partId]).add(i, values[i]);
            }
        } else {
            // Key and value array pair
            IntLongSortedVectorStorage sortStorage = (IntLongSortedVectorStorage) storage;
            int[] keys = sortStorage.getIndices();
            long[] values = sortStorage.getValues();
            for (int i = 0; i < keys.length; i++) {
                int partId = computeHashCode(hasher, keys[i]) & dataPartNumMinus1;
                ((HashIntKeysLongValuesPart) dataParts[partId]).add(keys[i], values[i]);
            }
        }
    } else {
        IntLongVectorStorage storage = vector.getStorage();
        if (storage.isSparse()) {
            // Use iterator
            IntLongSparseVectorStorage sparseStorage = (IntLongSparseVectorStorage) storage;
            ObjectIterator<Int2LongMap.Entry> iter = sparseStorage.entryIterator();
            while (iter.hasNext()) {
                Int2LongMap.Entry keyValue = iter.next();
                int partId = computeHashCode(hasher, keyValue.getIntKey()) % dataPartNum;
                ((HashIntKeysLongValuesPart) dataParts[partId]).add(keyValue.getIntKey(), keyValue.getLongValue());
            }
        } else if (storage.isDense()) {
            // Get values
            IntLongDenseVectorStorage denseStorage = (IntLongDenseVectorStorage) storage;
            long[] values = denseStorage.getValues();
            for (int i = 0; i < values.length; i++) {
                int partId = computeHashCode(hasher, i) % dataPartNum;
                ((HashIntKeysLongValuesPart) dataParts[partId]).add(i, values[i]);
            }
        } else {
            // Key and value array pair
            IntLongSortedVectorStorage sortStorage = (IntLongSortedVectorStorage) storage;
            int[] keys = sortStorage.getIndices();
            long[] values = sortStorage.getValues();
            for (int i = 0; i < keys.length; i++) {
                int partId = computeHashCode(hasher, keys[i]) % dataPartNum;
                ((HashIntKeysLongValuesPart) dataParts[partId]).add(keys[i], values[i]);
            }
        }
    }
}
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)

Aggregations

IntLongSparseVectorStorage (com.tencent.angel.ml.math2.storage.IntLongSparseVectorStorage)18 IntLongSortedVectorStorage (com.tencent.angel.ml.math2.storage.IntLongSortedVectorStorage)15 IntLongVectorStorage (com.tencent.angel.ml.math2.storage.IntLongVectorStorage)15 IntLongVector (com.tencent.angel.ml.math2.vector.IntLongVector)15 CompIntLongVector (com.tencent.angel.ml.math2.vector.CompIntLongVector)13 IntDoubleSortedVectorStorage (com.tencent.angel.ml.math2.storage.IntDoubleSortedVectorStorage)12 IntDoubleSparseVectorStorage (com.tencent.angel.ml.math2.storage.IntDoubleSparseVectorStorage)12 IntDoubleVectorStorage (com.tencent.angel.ml.math2.storage.IntDoubleVectorStorage)12 IntFloatSortedVectorStorage (com.tencent.angel.ml.math2.storage.IntFloatSortedVectorStorage)12 IntFloatSparseVectorStorage (com.tencent.angel.ml.math2.storage.IntFloatSparseVectorStorage)12 IntFloatVectorStorage (com.tencent.angel.ml.math2.storage.IntFloatVectorStorage)12 IntIntSortedVectorStorage (com.tencent.angel.ml.math2.storage.IntIntSortedVectorStorage)12 IntIntSparseVectorStorage (com.tencent.angel.ml.math2.storage.IntIntSparseVectorStorage)12 IntIntVectorStorage (com.tencent.angel.ml.math2.storage.IntIntVectorStorage)12 LongDoubleSortedVectorStorage (com.tencent.angel.ml.math2.storage.LongDoubleSortedVectorStorage)12 LongDoubleSparseVectorStorage (com.tencent.angel.ml.math2.storage.LongDoubleSparseVectorStorage)12 LongDoubleVectorStorage (com.tencent.angel.ml.math2.storage.LongDoubleVectorStorage)12 LongFloatSortedVectorStorage (com.tencent.angel.ml.math2.storage.LongFloatSortedVectorStorage)12 LongFloatSparseVectorStorage (com.tencent.angel.ml.math2.storage.LongFloatSparseVectorStorage)12 LongFloatVectorStorage (com.tencent.angel.ml.math2.storage.LongFloatVectorStorage)12