Search in sources :

Example 6 with IntFloatDenseVectorStorage

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

the class DotMatrixExecutor method apply.

private static Vector apply(BlasFloatMatrix mat, boolean trans, IntDummyVector v) {
    int m = mat.getNumRows(), n = mat.getNumCols();
    float[] resArr;
    if (trans) {
        assert m == v.getDim();
        resArr = new float[n];
    } else {
        assert n == v.getDim();
        resArr = new float[m];
    }
    int r = mat.getNumRows(), c = mat.getNumCols();
    float[] data = mat.getData();
    if (trans) {
        for (int j = 0; j < c; j++) {
            int[] idxs = v.getIndices();
            for (int i : idxs) {
                resArr[j] += data[i * c + j];
            }
        }
    } else {
        for (int i = 0; i < r; i++) {
            int[] idxs = v.getIndices();
            for (int j : idxs) {
                resArr[i] += data[i * c + j];
            }
        }
    }
    IntFloatDenseVectorStorage storage = new IntFloatDenseVectorStorage(resArr);
    return new IntFloatVector(v.getMatrixId(), v.getClock(), 0, resArr.length, storage);
}
Also used : IntFloatDenseVectorStorage(com.tencent.angel.ml.math2.storage.IntFloatDenseVectorStorage) IntFloatVector(com.tencent.angel.ml.math2.vector.IntFloatVector)

Example 7 with IntFloatDenseVectorStorage

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

the class DotMatrixExecutor method apply.

private static Vector apply(BlasFloatMatrix mat, boolean trans, IntFloatVector v) {
    int m = mat.getNumRows(), n = mat.getNumCols();
    float[] resArr;
    if (trans) {
        assert m == v.getDim();
        resArr = new float[n];
    } else {
        assert n == v.getDim();
        resArr = new float[m];
    }
    int r = mat.getNumRows(), c = mat.getNumCols();
    float[] data = mat.getData();
    if (v.isDense()) {
        float[] tempArray = v.getStorage().getValues();
        if (trans) {
            blas.sgemv("N", c, r, 1.0f, data, c, tempArray, 1, 0.0f, resArr, 1);
        } else {
            blas.sgemv("T", c, r, 1.0f, data, c, tempArray, 1, 0.0f, resArr, 1);
        }
    } else if (v.isSparse()) {
        if (trans) {
            for (int j = 0; j < c; j++) {
                ObjectIterator<Int2FloatMap.Entry> iter = v.getStorage().entryIterator();
                while (iter.hasNext()) {
                    Int2FloatMap.Entry entry = iter.next();
                    int i = entry.getIntKey();
                    resArr[j] += data[i * c + j] * entry.getFloatValue();
                }
            }
        } else {
            for (int i = 0; i < r; i++) {
                ObjectIterator<Int2FloatMap.Entry> iter = v.getStorage().entryIterator();
                while (iter.hasNext()) {
                    Int2FloatMap.Entry entry = iter.next();
                    int j = entry.getIntKey();
                    resArr[i] += data[i * c + j] * entry.getFloatValue();
                }
            }
        }
    } else {
        // sorted
        if (trans) {
            for (int j = 0; j < r; j++) {
                int[] idxs = v.getStorage().getIndices();
                float[] vals = v.getStorage().getValues();
                for (int k = 0; k < idxs.length; k++) {
                    resArr[j] += data[idxs[k] * c + j] * vals[k];
                }
            }
        } else {
            for (int i = 0; i < r; i++) {
                int[] idxs = v.getStorage().getIndices();
                float[] vals = v.getStorage().getValues();
                for (int k = 0; k < idxs.length; k++) {
                    resArr[i] += data[i * c + idxs[k]] * vals[k];
                }
            }
        }
    }
    IntFloatDenseVectorStorage storage = new IntFloatDenseVectorStorage(resArr);
    return new IntFloatVector(v.getMatrixId(), v.getClock(), 0, resArr.length, storage);
}
Also used : IntFloatDenseVectorStorage(com.tencent.angel.ml.math2.storage.IntFloatDenseVectorStorage) Int2FloatMap(it.unimi.dsi.fastutil.ints.Int2FloatMap) IntFloatVector(com.tencent.angel.ml.math2.vector.IntFloatVector) ObjectIterator(it.unimi.dsi.fastutil.objects.ObjectIterator)

Example 8 with IntFloatDenseVectorStorage

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

the class DotMatrixExecutor method apply.

private static Vector apply(BlasFloatMatrix mat, boolean trans, IntIntVector v) {
    int m = mat.getNumRows(), n = mat.getNumCols();
    float[] resArr;
    if (trans) {
        assert m == v.getDim();
        resArr = new float[n];
    } else {
        assert n == v.getDim();
        resArr = new float[m];
    }
    int r = mat.getNumRows(), c = mat.getNumCols();
    float[] data = mat.getData();
    if (v.isDense()) {
        float[] tempArray = ArrayCopy.copy(v.getStorage().getValues(), new float[v.getDim()]);
        if (trans) {
            blas.sgemv("N", c, r, 1.0f, data, c, tempArray, 1, 0.0f, resArr, 1);
        } else {
            blas.sgemv("T", c, r, 1.0f, data, c, tempArray, 1, 0.0f, resArr, 1);
        }
    } else if (v.isSparse()) {
        if (trans) {
            for (int j = 0; j < c; j++) {
                ObjectIterator<Int2IntMap.Entry> iter = v.getStorage().entryIterator();
                while (iter.hasNext()) {
                    Int2IntMap.Entry entry = iter.next();
                    int i = entry.getIntKey();
                    resArr[j] += data[i * c + j] * entry.getIntValue();
                }
            }
        } else {
            for (int i = 0; i < r; i++) {
                ObjectIterator<Int2IntMap.Entry> iter = v.getStorage().entryIterator();
                while (iter.hasNext()) {
                    Int2IntMap.Entry entry = iter.next();
                    int j = entry.getIntKey();
                    resArr[i] += data[i * c + j] * entry.getIntValue();
                }
            }
        }
    } else {
        // sorted
        if (trans) {
            for (int j = 0; j < r; j++) {
                int[] idxs = v.getStorage().getIndices();
                int[] vals = v.getStorage().getValues();
                for (int k = 0; k < idxs.length; k++) {
                    resArr[j] += data[idxs[k] * c + j] * vals[k];
                }
            }
        } else {
            for (int i = 0; i < r; i++) {
                int[] idxs = v.getStorage().getIndices();
                int[] vals = v.getStorage().getValues();
                for (int k = 0; k < idxs.length; k++) {
                    resArr[i] += data[i * c + idxs[k]] * vals[k];
                }
            }
        }
    }
    IntFloatDenseVectorStorage storage = new IntFloatDenseVectorStorage(resArr);
    return new IntFloatVector(v.getMatrixId(), v.getClock(), 0, resArr.length, storage);
}
Also used : IntFloatDenseVectorStorage(com.tencent.angel.ml.math2.storage.IntFloatDenseVectorStorage) Int2IntMap(it.unimi.dsi.fastutil.ints.Int2IntMap) IntFloatVector(com.tencent.angel.ml.math2.vector.IntFloatVector) ObjectIterator(it.unimi.dsi.fastutil.objects.ObjectIterator)

Example 9 with IntFloatDenseVectorStorage

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

the class UpdatePSFTest method testDenseFloatUDF.

public void testDenseFloatUDF() throws Exception {
    Worker worker = LocalClusterContext.get().getWorker(workerAttempt0Id).getWorker();
    MatrixClient client1 = worker.getPSAgent().getMatrixClient(DENSE_FLOAT_MAT, 0);
    int matrixW1Id = client1.getMatrixId();
    int[] index = genIndexs(feaNum, nnz);
    IntFloatVector deltaVec = new IntFloatVector(feaNum, new IntFloatDenseVectorStorage(feaNum));
    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();
    IntFloatVector row = (IntFloatVector) client1.getRow(0);
    for (int id : index) {
        Assert.assertTrue(row.get(id) == deltaVec.get(id));
    }
    Assert.assertTrue(feaNum == row.size());
}
Also used : IncrementRowsParam(com.tencent.angel.ml.matrix.psf.update.update.IncrementRowsParam) IncrementRows(com.tencent.angel.ml.matrix.psf.update.update.IncrementRows) IntFloatDenseVectorStorage(com.tencent.angel.ml.math2.storage.IntFloatDenseVectorStorage) 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) IntFloatVector(com.tencent.angel.ml.math2.vector.IntFloatVector)

Example 10 with IntFloatDenseVectorStorage

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

the class HashRouterUtils method splitIntFloatVector.

public static void splitIntFloatVector(KeyHash hasher, MatrixMeta matrixMeta, IntFloatVector vector, KeyValuePart[] dataParts) {
    int dataPartNum = dataParts.length;
    int dataPartNumMinus1 = dataPartNum - 1;
    if (isPow2(dataPartNum)) {
        IntFloatVectorStorage storage = vector.getStorage();
        if (storage.isSparse()) {
            // Use iterator
            IntFloatSparseVectorStorage sparseStorage = (IntFloatSparseVectorStorage) storage;
            ObjectIterator<Int2FloatMap.Entry> iter = sparseStorage.entryIterator();
            while (iter.hasNext()) {
                Int2FloatMap.Entry keyValue = iter.next();
                int partId = computeHashCode(hasher, keyValue.getIntKey()) & dataPartNumMinus1;
                ((HashIntKeysFloatValuesPart) dataParts[partId]).add(keyValue.getIntKey(), keyValue.getFloatValue());
            }
        } else if (storage.isDense()) {
            // Get values
            IntFloatDenseVectorStorage denseStorage = (IntFloatDenseVectorStorage) storage;
            float[] values = denseStorage.getValues();
            for (int i = 0; i < values.length; i++) {
                int partId = computeHashCode(hasher, i) & dataPartNumMinus1;
                ((HashIntKeysFloatValuesPart) dataParts[partId]).add(i, values[i]);
            }
        } else {
            // Key and value array pair
            IntFloatSortedVectorStorage sortStorage = (IntFloatSortedVectorStorage) storage;
            int[] keys = sortStorage.getIndices();
            float[] values = sortStorage.getValues();
            for (int i = 0; i < keys.length; i++) {
                int partId = computeHashCode(hasher, keys[i]) & dataPartNumMinus1;
                ((HashIntKeysFloatValuesPart) dataParts[partId]).add(keys[i], values[i]);
            }
        }
    } else {
        IntFloatVectorStorage storage = vector.getStorage();
        if (storage.isSparse()) {
            // Use iterator
            IntFloatSparseVectorStorage sparseStorage = (IntFloatSparseVectorStorage) storage;
            ObjectIterator<Int2FloatMap.Entry> iter = sparseStorage.entryIterator();
            while (iter.hasNext()) {
                Int2FloatMap.Entry keyValue = iter.next();
                int partId = computeHashCode(hasher, keyValue.getIntKey()) % dataPartNum;
                ((HashIntKeysFloatValuesPart) dataParts[partId]).add(keyValue.getIntKey(), keyValue.getFloatValue());
            }
        } else if (storage.isDense()) {
            // Get values
            IntFloatDenseVectorStorage denseStorage = (IntFloatDenseVectorStorage) storage;
            float[] values = denseStorage.getValues();
            for (int i = 0; i < values.length; i++) {
                int partId = computeHashCode(hasher, i) % dataPartNum;
                ((HashIntKeysFloatValuesPart) dataParts[partId]).add(i, values[i]);
            }
        } else {
            // Key and value array pair
            IntFloatSortedVectorStorage sortStorage = (IntFloatSortedVectorStorage) storage;
            int[] keys = sortStorage.getIndices();
            float[] values = sortStorage.getValues();
            for (int i = 0; i < keys.length; i++) {
                int partId = computeHashCode(hasher, keys[i]) % dataPartNum;
                ((HashIntKeysFloatValuesPart) dataParts[partId]).add(keys[i], values[i]);
            }
        }
    }
}
Also used : IntFloatSortedVectorStorage(com.tencent.angel.ml.math2.storage.IntFloatSortedVectorStorage) IntFloatVectorStorage(com.tencent.angel.ml.math2.storage.IntFloatVectorStorage) IntFloatDenseVectorStorage(com.tencent.angel.ml.math2.storage.IntFloatDenseVectorStorage) IntFloatSparseVectorStorage(com.tencent.angel.ml.math2.storage.IntFloatSparseVectorStorage) Int2FloatMap(it.unimi.dsi.fastutil.ints.Int2FloatMap)

Aggregations

IntFloatDenseVectorStorage (com.tencent.angel.ml.math2.storage.IntFloatDenseVectorStorage)19 IntFloatVector (com.tencent.angel.ml.math2.vector.IntFloatVector)16 IntFloatSortedVectorStorage (com.tencent.angel.ml.math2.storage.IntFloatSortedVectorStorage)3 IntFloatSparseVectorStorage (com.tencent.angel.ml.math2.storage.IntFloatSparseVectorStorage)3 IntFloatVectorStorage (com.tencent.angel.ml.math2.storage.IntFloatVectorStorage)3 Int2FloatMap (it.unimi.dsi.fastutil.ints.Int2FloatMap)3 ObjectIterator (it.unimi.dsi.fastutil.objects.ObjectIterator)3 CompIntFloatVector (com.tencent.angel.ml.math2.vector.CompIntFloatVector)2 IntDoubleVector (com.tencent.angel.ml.math2.vector.IntDoubleVector)1 IntIntVector (com.tencent.angel.ml.math2.vector.IntIntVector)1 IntLongVector (com.tencent.angel.ml.math2.vector.IntLongVector)1 LongDoubleVector (com.tencent.angel.ml.math2.vector.LongDoubleVector)1 LongFloatVector (com.tencent.angel.ml.math2.vector.LongFloatVector)1 LongIntVector (com.tencent.angel.ml.math2.vector.LongIntVector)1 LongLongVector (com.tencent.angel.ml.math2.vector.LongLongVector)1 Vector (com.tencent.angel.ml.math2.vector.Vector)1 IncrementRows (com.tencent.angel.ml.matrix.psf.update.update.IncrementRows)1 IncrementRowsParam (com.tencent.angel.ml.matrix.psf.update.update.IncrementRowsParam)1 MatrixClient (com.tencent.angel.psagent.matrix.MatrixClient)1 Worker (com.tencent.angel.worker.Worker)1