Search in sources :

Example 26 with IntFloatVectorStorage

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

the class SimpleBinaryOutNonZAExecutor method apply.

public static Vector apply(IntFloatVector v1, IntIntVector v2, Binary op) {
    IntFloatVectorStorage newStorage = (IntFloatVectorStorage) StorageSwitch.apply(v1, v2, op);
    if (v1.isDense() && v2.isDense()) {
        float[] resValues = newStorage.getValues();
        float[] v1Values = v1.getStorage().getValues();
        int[] v2Values = v2.getStorage().getValues();
        for (int idx = 0; idx < resValues.length; idx++) {
            resValues[idx] = op.apply(v1Values[idx], v2Values[idx]);
        }
    } else if (v1.isDense() && v2.isSparse()) {
        float[] resValues = newStorage.getValues();
        float[] v1Values = v1.getStorage().getValues();
        ObjectIterator<Int2IntMap.Entry> iter = v2.getStorage().entryIterator();
        while (iter.hasNext()) {
            Int2IntMap.Entry entry = iter.next();
            int idx = entry.getIntKey();
            resValues[idx] = op.apply(v1Values[idx], entry.getIntValue());
        }
    } else if (v1.isDense() && v2.isSorted()) {
        float[] resValues = newStorage.getValues();
        float[] v1Values = v1.getStorage().getValues();
        int[] v2Indices = v2.getStorage().getIndices();
        int[] v2Values = v2.getStorage().getValues();
        int size = v2.size();
        for (int i = 0; i < size; i++) {
            int idx = v2Indices[i];
            resValues[idx] = op.apply(v1Values[idx], v2Values[i]);
        }
    } else if (v1.isSparse() && v2.isDense()) {
        if (op.isKeepStorage()) {
            int dim = v1.getDim();
            int[] v2Values = v2.getStorage().getValues();
            if (v1.size() < Constant.denseLoopThreshold * v1.getDim()) {
                for (int i = 0; i < dim; i++) {
                    newStorage.set(i, op.apply(0, v2Values[i]));
                }
                ObjectIterator<Int2FloatMap.Entry> iter = v1.getStorage().entryIterator();
                while (iter.hasNext()) {
                    Int2FloatMap.Entry entry = iter.next();
                    int idx = entry.getIntKey();
                    newStorage.set(idx, op.apply(entry.getFloatValue(), v2Values[idx]));
                }
            } else {
                for (int i = 0; i < dim; i++) {
                    if (v1.getStorage().hasKey(i)) {
                        newStorage.set(i, op.apply(v1.get(i), v2Values[i]));
                    } else {
                        newStorage.set(i, op.apply(0, v2Values[i]));
                    }
                }
            }
        } else {
            float[] resValues = newStorage.getValues();
            int[] v2Values = v2.getStorage().getValues();
            if (v1.size() < Constant.denseLoopThreshold * v1.getDim()) {
                for (int i = 0; i < resValues.length; i++) {
                    resValues[i] = op.apply(0, v2Values[i]);
                }
                ObjectIterator<Int2FloatMap.Entry> iter = v1.getStorage().entryIterator();
                while (iter.hasNext()) {
                    Int2FloatMap.Entry entry = iter.next();
                    int idx = entry.getIntKey();
                    resValues[idx] = op.apply(entry.getFloatValue(), v2Values[idx]);
                }
            } else {
                for (int i = 0; i < resValues.length; i++) {
                    if (v1.getStorage().hasKey(i)) {
                        resValues[i] = op.apply(v1.get(i), v2Values[i]);
                    } else {
                        resValues[i] = op.apply(0, v2Values[i]);
                    }
                }
            }
        }
    } else if (v1.isSorted() && v2.isDense()) {
        if (op.isKeepStorage()) {
            int dim = v1.getDim();
            int[] resIndices = newStorage.getIndices();
            float[] resValues = newStorage.getValues();
            int[] v2Values = v2.getStorage().getValues();
            int[] v1Indices = v1.getStorage().getIndices();
            float[] v1Values = v1.getStorage().getValues();
            for (int i = 0; i < dim; i++) {
                resIndices[i] = i;
                resValues[i] = op.apply(0, v2Values[i]);
            }
            int size = v1.size();
            for (int i = 0; i < size; i++) {
                int idx = v1Indices[i];
                resValues[idx] = op.apply(v1Values[i], v2Values[idx]);
            }
        } else {
            float[] resValues = newStorage.getValues();
            int[] v2Values = v2.getStorage().getValues();
            if (v1.size() < Constant.denseLoopThreshold * v1.getDim()) {
                int[] v1Indices = v1.getStorage().getIndices();
                float[] v1Values = v1.getStorage().getValues();
                for (int i = 0; i < resValues.length; i++) {
                    resValues[i] = op.apply(0, v2Values[i]);
                }
                int size = v1.size();
                for (int i = 0; i < size; i++) {
                    int idx = v1Indices[i];
                    resValues[idx] = op.apply(v1Values[i], v2Values[idx]);
                }
            } else {
                IntFloatVectorStorage v1Storage = v1.getStorage();
                for (int i = 0; i < resValues.length; i++) {
                    if (v1Storage.hasKey(i)) {
                        resValues[i] = op.apply(v1.get(i), v2Values[i]);
                    } else {
                        resValues[i] = op.apply(0, v2Values[i]);
                    }
                }
            }
        }
    } else if (v1.isSparse() && v2.isSparse()) {
        int v1Size = v1.size();
        int v2Size = v2.size();
        if (v1Size >= v2Size * Constant.sparseThreshold && (v1Size + v2Size) * Constant.intersectionCoeff <= Constant.sparseDenseStorageThreshold * v1.dim()) {
            // we gauss the indices of v2 maybe is a subset of v1, or overlap is very large
            ObjectIterator<Int2IntMap.Entry> iter = v2.getStorage().entryIterator();
            while (iter.hasNext()) {
                Int2IntMap.Entry entry = iter.next();
                int idx = entry.getIntKey();
                newStorage.set(idx, op.apply(v1.get(idx), entry.getIntValue()));
            }
        } else if ((v1Size + v2Size) * Constant.intersectionCoeff >= Constant.sparseDenseStorageThreshold * v1.dim()) {
            // we gauss dense storage is more efficient
            ObjectIterator<Int2FloatMap.Entry> iter1 = v1.getStorage().entryIterator();
            while (iter1.hasNext()) {
                Int2FloatMap.Entry entry = iter1.next();
                int idx = entry.getIntKey();
                newStorage.set(idx, entry.getFloatValue());
            }
            ObjectIterator<Int2IntMap.Entry> iter2 = v2.getStorage().entryIterator();
            while (iter2.hasNext()) {
                Int2IntMap.Entry entry = iter2.next();
                int idx = entry.getIntKey();
                newStorage.set(idx, op.apply(v1.get(idx), entry.getIntValue()));
            }
        } else {
            // to avoid multi-rehash
            int capacity = 1 << (32 - Integer.numberOfLeadingZeros((int) (v1.size() / 0.75)));
            if (v1.size() + v2.size() <= 1.5 * capacity) {
                // no rehashor one onle rehash is required, nothing to optimization
                ObjectIterator<Int2IntMap.Entry> iter = v2.getStorage().entryIterator();
                while (iter.hasNext()) {
                    Int2IntMap.Entry entry = iter.next();
                    int idx = entry.getIntKey();
                    newStorage.set(idx, op.apply(v1.get(idx), entry.getIntValue()));
                }
            } else {
                // multi-rehash
                ObjectIterator<Int2FloatMap.Entry> iter1 = v1.getStorage().entryIterator();
                while (iter1.hasNext()) {
                    Int2FloatMap.Entry entry = iter1.next();
                    int idx = entry.getIntKey();
                    newStorage.set(idx, entry.getFloatValue());
                }
                ObjectIterator<Int2IntMap.Entry> iter2 = v2.getStorage().entryIterator();
                while (iter2.hasNext()) {
                    Int2IntMap.Entry entry = iter2.next();
                    int idx = entry.getIntKey();
                    newStorage.set(idx, op.apply(v1.get(idx), entry.getIntValue()));
                }
            }
        }
    } else if (v1.isSparse() && v2.isSorted()) {
        int v1Size = v1.size();
        int v2Size = v2.size();
        if (v1Size >= v2Size * Constant.sparseThreshold && (v1Size + v2Size) * Constant.intersectionCoeff <= Constant.sparseDenseStorageThreshold * v1.dim()) {
            // we gauss the indices of v2 maybe is a subset of v1, or overlap is very large
            int[] v2Indices = v2.getStorage().getIndices();
            int[] v2Values = v2.getStorage().getValues();
            for (int i = 0; i < v2.size(); i++) {
                int idx = v2Indices[i];
                newStorage.set(idx, op.apply(v1.get(idx), v2Values[i]));
            }
        } else if ((v1Size + v2Size) * Constant.intersectionCoeff >= Constant.sparseDenseStorageThreshold * v1.dim()) {
            ObjectIterator<Int2FloatMap.Entry> iter1 = v1.getStorage().entryIterator();
            while (iter1.hasNext()) {
                Int2FloatMap.Entry entry = iter1.next();
                int idx = entry.getIntKey();
                newStorage.set(idx, entry.getFloatValue());
            }
            int[] v2Indices = v2.getStorage().getIndices();
            int[] v2Values = v2.getStorage().getValues();
            int size = v2.size();
            for (int i = 0; i < size; i++) {
                int idx = v2Indices[i];
                newStorage.set(idx, op.apply(v1.get(idx), v2Values[i]));
            }
        } else {
            // to avoid multi-rehash
            int capacity = 1 << (32 - Integer.numberOfLeadingZeros((int) (v1.size() / 0.75)));
            if (v1.size() + v2.size() <= 1.5 * capacity) {
                int[] v2Indices = v2.getStorage().getIndices();
                int[] v2Values = v2.getStorage().getValues();
                for (int i = 0; i < v2.size(); i++) {
                    int idx = v2Indices[i];
                    newStorage.set(idx, op.apply(v1.get(idx), v2Values[i]));
                }
            } else {
                ObjectIterator<Int2FloatMap.Entry> iter1 = v1.getStorage().entryIterator();
                while (iter1.hasNext()) {
                    Int2FloatMap.Entry entry = iter1.next();
                    int idx = entry.getIntKey();
                    newStorage.set(idx, entry.getFloatValue());
                }
                int[] v2Indices = v2.getStorage().getIndices();
                int[] v2Values = v2.getStorage().getValues();
                int size = v2.size();
                for (int i = 0; i < size; i++) {
                    int idx = v2Indices[i];
                    newStorage.set(idx, op.apply(v1.get(idx), v2Values[i]));
                }
            }
        }
    } else if (v1.isSorted() && v2.isSparse()) {
        int v1Size = v1.size();
        int v2Size = v2.size();
        if ((v1Size + v2Size) * Constant.intersectionCoeff >= Constant.sortedDenseStorageThreshold * v1.dim()) {
            if (op.isKeepStorage()) {
                int[] v1Indices = v1.getStorage().getIndices();
                int[] idxiter = v2.getStorage().indexIterator().toIntArray();
                int[] indices = new int[(int) (v1Size + v2Size)];
                System.arraycopy(v1Indices, 0, indices, 0, (int) v1.size());
                System.arraycopy(idxiter, 0, indices, (int) v1.size(), (int) v2.size());
                IntAVLTreeSet avl = new IntAVLTreeSet(indices);
                IntBidirectionalIterator iter = avl.iterator();
                float[] values = new float[indices.length];
                int i = 0;
                while (iter.hasNext()) {
                    int idx = iter.nextInt();
                    indices[i] = idx;
                    values[i] = op.apply(v1.get(idx), v2.get(idx));
                    i++;
                }
                while (i < indices.length) {
                    indices[i] = 0;
                    i++;
                }
                newStorage = new IntFloatSortedVectorStorage(v1.getDim(), (int) avl.size(), indices, values);
            } else {
                int[] v1Indices = v1.getStorage().getIndices();
                float[] v1Values = v1.getStorage().getValues();
                int size = v1.size();
                for (int i = 0; i < size; i++) {
                    int idx = v1Indices[i];
                    newStorage.set(idx, v1Values[i]);
                }
                ObjectIterator<Int2IntMap.Entry> iter = v2.getStorage().entryIterator();
                while (iter.hasNext()) {
                    Int2IntMap.Entry entry = iter.next();
                    int idx = entry.getIntKey();
                    newStorage.set(idx, op.apply(newStorage.get(idx), entry.getIntValue()));
                }
            }
        } else {
            if (op.isKeepStorage()) {
                int[] v1Indices = v1.getStorage().getIndices();
                int[] idxiter = v2.getStorage().indexIterator().toIntArray();
                int[] indices = new int[(int) (v1Size + v2Size)];
                System.arraycopy(v1Indices, 0, indices, 0, (int) v1.size());
                System.arraycopy(idxiter, 0, indices, (int) v1.size(), (int) v2.size());
                IntAVLTreeSet avl = new IntAVLTreeSet(indices);
                IntBidirectionalIterator iter = avl.iterator();
                float[] values = new float[indices.length];
                int i = 0;
                while (iter.hasNext()) {
                    int idx = iter.nextInt();
                    indices[i] = idx;
                    values[i] = op.apply(v1.get(idx), v2.get(idx));
                    i++;
                }
                while (i < indices.length) {
                    indices[i] = 0;
                    i++;
                }
                newStorage = new IntFloatSortedVectorStorage(v1.getDim(), (int) avl.size(), indices, values);
            } else {
                int[] v1Indices = v1.getStorage().getIndices();
                float[] v1Values = v1.getStorage().getValues();
                int size = v1.size();
                for (int i = 0; i < size; i++) {
                    int idx = v1Indices[i];
                    newStorage.set(idx, v1Values[i]);
                }
                ObjectIterator<Int2IntMap.Entry> iter = v2.getStorage().entryIterator();
                while (iter.hasNext()) {
                    Int2IntMap.Entry entry = iter.next();
                    int idx = entry.getIntKey();
                    newStorage.set(idx, op.apply(newStorage.get(idx), entry.getIntValue()));
                }
            }
        }
    } else if (v1.isSorted() && v2.isSorted()) {
        int v1Pointor = 0;
        int v2Pointor = 0;
        int size1 = v1.size();
        int size2 = v2.size();
        int[] v1Indices = v1.getStorage().getIndices();
        float[] v1Values = v1.getStorage().getValues();
        int[] v2Indices = v2.getStorage().getIndices();
        int[] v2Values = v2.getStorage().getValues();
        if ((size1 + size2) * Constant.intersectionCoeff >= Constant.sortedDenseStorageThreshold * v1.dim()) {
            if (op.isKeepStorage()) {
                // sorted
                int[] resIndices = newStorage.getIndices();
                float[] resValues = newStorage.getValues();
                int global = 0;
                while (v1Pointor < size1 && v2Pointor < size2) {
                    if (v1Indices[v1Pointor] == v2Indices[v2Pointor]) {
                        resIndices[global] = v1Indices[v1Pointor];
                        resValues[global] = op.apply(v1Values[v1Pointor], v2Values[v2Pointor]);
                        global++;
                        v1Pointor++;
                        v2Pointor++;
                    } else if (v1Indices[v1Pointor] < v2Indices[v2Pointor]) {
                        resIndices[global] = v1Indices[v1Pointor];
                        resValues[global] = v1Values[v1Pointor];
                        global++;
                        v1Pointor++;
                    } else {
                        // v1Indices[v1Pointor] > v2Indices[v2Pointor]
                        resIndices[global] = v2Indices[v2Pointor];
                        resValues[global] = op.apply(0, v2Values[v2Pointor]);
                        global++;
                        v2Pointor++;
                    }
                }
            } else {
                // dense
                while (v1Pointor < size1 || v2Pointor < size2) {
                    if ((v1Pointor < size1 && v2Pointor < size2) && v1Indices[v1Pointor] == v2Indices[v2Pointor]) {
                        newStorage.set(v1Indices[v1Pointor], op.apply(v1Values[v1Pointor], v2Values[v2Pointor]));
                        v1Pointor++;
                        v2Pointor++;
                    } else if ((v1Pointor < size1 && v2Pointor < size2) && v1Indices[v1Pointor] < v2Indices[v2Pointor] || (v1Pointor < size1 && v2Pointor >= size2)) {
                        newStorage.set(v1Indices[v1Pointor], v1Values[v1Pointor]);
                        v1Pointor++;
                    } else if (((v1Pointor < size1 && v2Pointor < size2) && v1Indices[v1Pointor] >= v2Indices[v2Pointor]) || (v1Pointor >= size1 && v2Pointor < size2)) {
                        newStorage.set(v2Indices[v2Pointor], op.apply(0, v2Values[v2Pointor]));
                        v2Pointor++;
                    }
                }
            }
        } else {
            if (op.isKeepStorage()) {
                int[] resIndices = newStorage.getIndices();
                float[] resValues = newStorage.getValues();
                int globalPointor = 0;
                while (v1Pointor < size1 && v2Pointor < size2) {
                    if (v1Indices[v1Pointor] == v2Indices[v2Pointor]) {
                        resIndices[globalPointor] = v1Indices[v1Pointor];
                        resValues[globalPointor] = op.apply(v1Values[v1Pointor], v2Values[v2Pointor]);
                        v1Pointor++;
                        v2Pointor++;
                        globalPointor++;
                    } else if (v1Indices[v1Pointor] < v2Indices[v2Pointor]) {
                        resIndices[globalPointor] = v1Indices[v1Pointor];
                        resValues[globalPointor] = v1Values[v1Pointor];
                        v1Pointor++;
                        globalPointor++;
                    } else {
                        // v1Indices[v1Pointor] > v2Indices[v2Pointor]
                        resIndices[globalPointor] = v2Indices[v2Pointor];
                        resValues[globalPointor] = op.apply(0, v2Values[v2Pointor]);
                        v2Pointor++;
                        globalPointor++;
                    }
                }
            } else {
                while (v1Pointor < size1 || v2Pointor < size2) {
                    if ((v1Pointor < size1 && v2Pointor < size2) && v1Indices[v1Pointor] == v2Indices[v2Pointor]) {
                        newStorage.set(v1Indices[v1Pointor], op.apply(v1Values[v1Pointor], v2Values[v2Pointor]));
                        v1Pointor++;
                        v2Pointor++;
                    } else if ((v1Pointor < size1 && v2Pointor < size2) && v1Indices[v1Pointor] < v2Indices[v2Pointor] || (v1Pointor < size1 && v2Pointor >= size2)) {
                        newStorage.set(v1Indices[v1Pointor], v1Values[v1Pointor]);
                        v1Pointor++;
                    } else if (((v1Pointor < size1 && v2Pointor < size2) && v1Indices[v1Pointor] >= v2Indices[v2Pointor]) || (v1Pointor >= size1 && v2Pointor < size2)) {
                        newStorage.set(v2Indices[v2Pointor], op.apply(0, v2Values[v2Pointor]));
                        v2Pointor++;
                    }
                }
            }
        }
    } else {
        throw new AngelException("The operation is not support!");
    }
    return new IntFloatVector(v1.getMatrixId(), v1.getRowId(), v1.getClock(), v1.getDim(), newStorage);
}
Also used : AngelException(com.tencent.angel.exception.AngelException) IntBidirectionalIterator(it.unimi.dsi.fastutil.ints.IntBidirectionalIterator) IntFloatVector(com.tencent.angel.ml.math2.vector.IntFloatVector) ObjectIterator(it.unimi.dsi.fastutil.objects.ObjectIterator) IntFloatSortedVectorStorage(com.tencent.angel.ml.math2.storage.IntFloatSortedVectorStorage) IntFloatVectorStorage(com.tencent.angel.ml.math2.storage.IntFloatVectorStorage) Int2FloatMap(it.unimi.dsi.fastutil.ints.Int2FloatMap) IntAVLTreeSet(it.unimi.dsi.fastutil.ints.IntAVLTreeSet) Int2IntMap(it.unimi.dsi.fastutil.ints.Int2IntMap)

Example 27 with IntFloatVectorStorage

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

the class SimpleBinaryOutNonZAExecutor method apply.

public static Vector apply(IntFloatVector v1, IntFloatVector v2, Binary op) {
    IntFloatVectorStorage newStorage = (IntFloatVectorStorage) StorageSwitch.apply(v1, v2, op);
    if (v1.isDense() && v2.isDense()) {
        float[] resValues = newStorage.getValues();
        float[] v1Values = v1.getStorage().getValues();
        float[] v2Values = v2.getStorage().getValues();
        for (int idx = 0; idx < resValues.length; idx++) {
            resValues[idx] = op.apply(v1Values[idx], v2Values[idx]);
        }
    } else if (v1.isDense() && v2.isSparse()) {
        float[] resValues = newStorage.getValues();
        float[] v1Values = v1.getStorage().getValues();
        ObjectIterator<Int2FloatMap.Entry> iter = v2.getStorage().entryIterator();
        while (iter.hasNext()) {
            Int2FloatMap.Entry entry = iter.next();
            int idx = entry.getIntKey();
            resValues[idx] = op.apply(v1Values[idx], entry.getFloatValue());
        }
    } else if (v1.isDense() && v2.isSorted()) {
        float[] resValues = newStorage.getValues();
        float[] v1Values = v1.getStorage().getValues();
        int[] v2Indices = v2.getStorage().getIndices();
        float[] v2Values = v2.getStorage().getValues();
        int size = v2.size();
        for (int i = 0; i < size; i++) {
            int idx = v2Indices[i];
            resValues[idx] = op.apply(v1Values[idx], v2Values[i]);
        }
    } else if (v1.isSparse() && v2.isDense()) {
        if (op.isKeepStorage()) {
            int dim = v1.getDim();
            float[] v2Values = v2.getStorage().getValues();
            if (v1.size() < Constant.denseLoopThreshold * v1.getDim()) {
                for (int i = 0; i < dim; i++) {
                    newStorage.set(i, op.apply(0, v2Values[i]));
                }
                ObjectIterator<Int2FloatMap.Entry> iter = v1.getStorage().entryIterator();
                while (iter.hasNext()) {
                    Int2FloatMap.Entry entry = iter.next();
                    int idx = entry.getIntKey();
                    newStorage.set(idx, op.apply(entry.getFloatValue(), v2Values[idx]));
                }
            } else {
                for (int i = 0; i < dim; i++) {
                    if (v1.getStorage().hasKey(i)) {
                        newStorage.set(i, op.apply(v1.get(i), v2Values[i]));
                    } else {
                        newStorage.set(i, op.apply(0, v2Values[i]));
                    }
                }
            }
        } else {
            float[] resValues = newStorage.getValues();
            float[] v2Values = v2.getStorage().getValues();
            if (v1.size() < Constant.denseLoopThreshold * v1.getDim()) {
                for (int i = 0; i < resValues.length; i++) {
                    resValues[i] = op.apply(0, v2Values[i]);
                }
                ObjectIterator<Int2FloatMap.Entry> iter = v1.getStorage().entryIterator();
                while (iter.hasNext()) {
                    Int2FloatMap.Entry entry = iter.next();
                    int idx = entry.getIntKey();
                    resValues[idx] = op.apply(entry.getFloatValue(), v2Values[idx]);
                }
            } else {
                for (int i = 0; i < resValues.length; i++) {
                    if (v1.getStorage().hasKey(i)) {
                        resValues[i] = op.apply(v1.get(i), v2Values[i]);
                    } else {
                        resValues[i] = op.apply(0, v2Values[i]);
                    }
                }
            }
        }
    } else if (v1.isSorted() && v2.isDense()) {
        if (op.isKeepStorage()) {
            int dim = v1.getDim();
            int[] resIndices = newStorage.getIndices();
            float[] resValues = newStorage.getValues();
            float[] v2Values = v2.getStorage().getValues();
            int[] v1Indices = v1.getStorage().getIndices();
            float[] v1Values = v1.getStorage().getValues();
            for (int i = 0; i < dim; i++) {
                resIndices[i] = i;
                resValues[i] = op.apply(0, v2Values[i]);
            }
            int size = v1.size();
            for (int i = 0; i < size; i++) {
                int idx = v1Indices[i];
                resValues[idx] = op.apply(v1Values[i], v2Values[idx]);
            }
        } else {
            float[] resValues = newStorage.getValues();
            float[] v2Values = v2.getStorage().getValues();
            if (v1.size() < Constant.denseLoopThreshold * v1.getDim()) {
                int[] v1Indices = v1.getStorage().getIndices();
                float[] v1Values = v1.getStorage().getValues();
                for (int i = 0; i < resValues.length; i++) {
                    resValues[i] = op.apply(0, v2Values[i]);
                }
                int size = v1.size();
                for (int i = 0; i < size; i++) {
                    int idx = v1Indices[i];
                    resValues[idx] = op.apply(v1Values[i], v2Values[idx]);
                }
            } else {
                IntFloatVectorStorage v1Storage = v1.getStorage();
                for (int i = 0; i < resValues.length; i++) {
                    if (v1Storage.hasKey(i)) {
                        resValues[i] = op.apply(v1.get(i), v2Values[i]);
                    } else {
                        resValues[i] = op.apply(0, v2Values[i]);
                    }
                }
            }
        }
    } else if (v1.isSparse() && v2.isSparse()) {
        int v1Size = v1.size();
        int v2Size = v2.size();
        if (v1Size >= v2Size * Constant.sparseThreshold && (v1Size + v2Size) * Constant.intersectionCoeff <= Constant.sparseDenseStorageThreshold * v1.dim()) {
            // we gauss the indices of v2 maybe is a subset of v1, or overlap is very large
            ObjectIterator<Int2FloatMap.Entry> iter = v2.getStorage().entryIterator();
            while (iter.hasNext()) {
                Int2FloatMap.Entry entry = iter.next();
                int idx = entry.getIntKey();
                newStorage.set(idx, op.apply(v1.get(idx), entry.getFloatValue()));
            }
        } else if ((v1Size + v2Size) * Constant.intersectionCoeff >= Constant.sparseDenseStorageThreshold * v1.dim()) {
            // we gauss dense storage is more efficient
            ObjectIterator<Int2FloatMap.Entry> iter1 = v1.getStorage().entryIterator();
            while (iter1.hasNext()) {
                Int2FloatMap.Entry entry = iter1.next();
                int idx = entry.getIntKey();
                newStorage.set(idx, entry.getFloatValue());
            }
            ObjectIterator<Int2FloatMap.Entry> iter2 = v2.getStorage().entryIterator();
            while (iter2.hasNext()) {
                Int2FloatMap.Entry entry = iter2.next();
                int idx = entry.getIntKey();
                newStorage.set(idx, op.apply(v1.get(idx), entry.getFloatValue()));
            }
        } else {
            // to avoid multi-rehash
            int capacity = 1 << (32 - Integer.numberOfLeadingZeros((int) (v1.size() / 0.75)));
            if (v1.size() + v2.size() <= 1.5 * capacity) {
                // no rehashor one onle rehash is required, nothing to optimization
                ObjectIterator<Int2FloatMap.Entry> iter = v2.getStorage().entryIterator();
                while (iter.hasNext()) {
                    Int2FloatMap.Entry entry = iter.next();
                    int idx = entry.getIntKey();
                    newStorage.set(idx, op.apply(v1.get(idx), entry.getFloatValue()));
                }
            } else {
                // multi-rehash
                ObjectIterator<Int2FloatMap.Entry> iter1 = v1.getStorage().entryIterator();
                while (iter1.hasNext()) {
                    Int2FloatMap.Entry entry = iter1.next();
                    int idx = entry.getIntKey();
                    newStorage.set(idx, entry.getFloatValue());
                }
                ObjectIterator<Int2FloatMap.Entry> iter2 = v2.getStorage().entryIterator();
                while (iter2.hasNext()) {
                    Int2FloatMap.Entry entry = iter2.next();
                    int idx = entry.getIntKey();
                    newStorage.set(idx, op.apply(v1.get(idx), entry.getFloatValue()));
                }
            }
        }
    } else if (v1.isSparse() && v2.isSorted()) {
        int v1Size = v1.size();
        int v2Size = v2.size();
        if (v1Size >= v2Size * Constant.sparseThreshold && (v1Size + v2Size) * Constant.intersectionCoeff <= Constant.sparseDenseStorageThreshold * v1.dim()) {
            // we gauss the indices of v2 maybe is a subset of v1, or overlap is very large
            int[] v2Indices = v2.getStorage().getIndices();
            float[] v2Values = v2.getStorage().getValues();
            for (int i = 0; i < v2.size(); i++) {
                int idx = v2Indices[i];
                newStorage.set(idx, op.apply(v1.get(idx), v2Values[i]));
            }
        } else if ((v1Size + v2Size) * Constant.intersectionCoeff >= Constant.sparseDenseStorageThreshold * v1.dim()) {
            ObjectIterator<Int2FloatMap.Entry> iter1 = v1.getStorage().entryIterator();
            while (iter1.hasNext()) {
                Int2FloatMap.Entry entry = iter1.next();
                int idx = entry.getIntKey();
                newStorage.set(idx, entry.getFloatValue());
            }
            int[] v2Indices = v2.getStorage().getIndices();
            float[] v2Values = v2.getStorage().getValues();
            int size = v2.size();
            for (int i = 0; i < size; i++) {
                int idx = v2Indices[i];
                newStorage.set(idx, op.apply(v1.get(idx), v2Values[i]));
            }
        } else {
            // to avoid multi-rehash
            int capacity = 1 << (32 - Integer.numberOfLeadingZeros((int) (v1.size() / 0.75)));
            if (v1.size() + v2.size() <= 1.5 * capacity) {
                int[] v2Indices = v2.getStorage().getIndices();
                float[] v2Values = v2.getStorage().getValues();
                for (int i = 0; i < v2.size(); i++) {
                    int idx = v2Indices[i];
                    newStorage.set(idx, op.apply(v1.get(idx), v2Values[i]));
                }
            } else {
                ObjectIterator<Int2FloatMap.Entry> iter1 = v1.getStorage().entryIterator();
                while (iter1.hasNext()) {
                    Int2FloatMap.Entry entry = iter1.next();
                    int idx = entry.getIntKey();
                    newStorage.set(idx, entry.getFloatValue());
                }
                int[] v2Indices = v2.getStorage().getIndices();
                float[] v2Values = v2.getStorage().getValues();
                int size = v2.size();
                for (int i = 0; i < size; i++) {
                    int idx = v2Indices[i];
                    newStorage.set(idx, op.apply(v1.get(idx), v2Values[i]));
                }
            }
        }
    } else if (v1.isSorted() && v2.isSparse()) {
        int v1Size = v1.size();
        int v2Size = v2.size();
        if ((v1Size + v2Size) * Constant.intersectionCoeff >= Constant.sortedDenseStorageThreshold * v1.dim()) {
            if (op.isKeepStorage()) {
                int[] v1Indices = v1.getStorage().getIndices();
                int[] idxiter = v2.getStorage().indexIterator().toIntArray();
                int[] indices = new int[(int) (v1Size + v2Size)];
                System.arraycopy(v1Indices, 0, indices, 0, (int) v1.size());
                System.arraycopy(idxiter, 0, indices, (int) v1.size(), (int) v2.size());
                IntAVLTreeSet avl = new IntAVLTreeSet(indices);
                IntBidirectionalIterator iter = avl.iterator();
                float[] values = new float[indices.length];
                int i = 0;
                while (iter.hasNext()) {
                    int idx = iter.nextInt();
                    indices[i] = idx;
                    values[i] = op.apply(v1.get(idx), v2.get(idx));
                    i++;
                }
                while (i < indices.length) {
                    indices[i] = 0;
                    i++;
                }
                newStorage = new IntFloatSortedVectorStorage(v1.getDim(), (int) avl.size(), indices, values);
            } else {
                int[] v1Indices = v1.getStorage().getIndices();
                float[] v1Values = v1.getStorage().getValues();
                int size = v1.size();
                for (int i = 0; i < size; i++) {
                    int idx = v1Indices[i];
                    newStorage.set(idx, v1Values[i]);
                }
                ObjectIterator<Int2FloatMap.Entry> iter = v2.getStorage().entryIterator();
                while (iter.hasNext()) {
                    Int2FloatMap.Entry entry = iter.next();
                    int idx = entry.getIntKey();
                    newStorage.set(idx, op.apply(newStorage.get(idx), entry.getFloatValue()));
                }
            }
        } else {
            if (op.isKeepStorage()) {
                int[] v1Indices = v1.getStorage().getIndices();
                int[] idxiter = v2.getStorage().indexIterator().toIntArray();
                int[] indices = new int[(int) (v1Size + v2Size)];
                System.arraycopy(v1Indices, 0, indices, 0, (int) v1.size());
                System.arraycopy(idxiter, 0, indices, (int) v1.size(), (int) v2.size());
                IntAVLTreeSet avl = new IntAVLTreeSet(indices);
                IntBidirectionalIterator iter = avl.iterator();
                float[] values = new float[indices.length];
                int i = 0;
                while (iter.hasNext()) {
                    int idx = iter.nextInt();
                    indices[i] = idx;
                    values[i] = op.apply(v1.get(idx), v2.get(idx));
                    i++;
                }
                while (i < indices.length) {
                    indices[i] = 0;
                    i++;
                }
                newStorage = new IntFloatSortedVectorStorage(v1.getDim(), (int) avl.size(), indices, values);
            } else {
                int[] v1Indices = v1.getStorage().getIndices();
                float[] v1Values = v1.getStorage().getValues();
                int size = v1.size();
                for (int i = 0; i < size; i++) {
                    int idx = v1Indices[i];
                    newStorage.set(idx, v1Values[i]);
                }
                ObjectIterator<Int2FloatMap.Entry> iter = v2.getStorage().entryIterator();
                while (iter.hasNext()) {
                    Int2FloatMap.Entry entry = iter.next();
                    int idx = entry.getIntKey();
                    newStorage.set(idx, op.apply(newStorage.get(idx), entry.getFloatValue()));
                }
            }
        }
    } else if (v1.isSorted() && v2.isSorted()) {
        int v1Pointor = 0;
        int v2Pointor = 0;
        int size1 = v1.size();
        int size2 = v2.size();
        int[] v1Indices = v1.getStorage().getIndices();
        float[] v1Values = v1.getStorage().getValues();
        int[] v2Indices = v2.getStorage().getIndices();
        float[] v2Values = v2.getStorage().getValues();
        if ((size1 + size2) * Constant.intersectionCoeff >= Constant.sortedDenseStorageThreshold * v1.dim()) {
            if (op.isKeepStorage()) {
                // sorted
                int[] resIndices = newStorage.getIndices();
                float[] resValues = newStorage.getValues();
                int global = 0;
                while (v1Pointor < size1 && v2Pointor < size2) {
                    if (v1Indices[v1Pointor] == v2Indices[v2Pointor]) {
                        resIndices[global] = v1Indices[v1Pointor];
                        resValues[global] = op.apply(v1Values[v1Pointor], v2Values[v2Pointor]);
                        global++;
                        v1Pointor++;
                        v2Pointor++;
                    } else if (v1Indices[v1Pointor] < v2Indices[v2Pointor]) {
                        resIndices[global] = v1Indices[v1Pointor];
                        resValues[global] = v1Values[v1Pointor];
                        global++;
                        v1Pointor++;
                    } else {
                        // v1Indices[v1Pointor] > v2Indices[v2Pointor]
                        resIndices[global] = v2Indices[v2Pointor];
                        resValues[global] = op.apply(0, v2Values[v2Pointor]);
                        global++;
                        v2Pointor++;
                    }
                }
            } else {
                // dense
                while (v1Pointor < size1 || v2Pointor < size2) {
                    if ((v1Pointor < size1 && v2Pointor < size2) && v1Indices[v1Pointor] == v2Indices[v2Pointor]) {
                        newStorage.set(v1Indices[v1Pointor], op.apply(v1Values[v1Pointor], v2Values[v2Pointor]));
                        v1Pointor++;
                        v2Pointor++;
                    } else if ((v1Pointor < size1 && v2Pointor < size2) && v1Indices[v1Pointor] < v2Indices[v2Pointor] || (v1Pointor < size1 && v2Pointor >= size2)) {
                        newStorage.set(v1Indices[v1Pointor], v1Values[v1Pointor]);
                        v1Pointor++;
                    } else if (((v1Pointor < size1 && v2Pointor < size2) && v1Indices[v1Pointor] >= v2Indices[v2Pointor]) || (v1Pointor >= size1 && v2Pointor < size2)) {
                        newStorage.set(v2Indices[v2Pointor], op.apply(0, v2Values[v2Pointor]));
                        v2Pointor++;
                    }
                }
            }
        } else {
            if (op.isKeepStorage()) {
                int[] resIndices = newStorage.getIndices();
                float[] resValues = newStorage.getValues();
                int globalPointor = 0;
                while (v1Pointor < size1 && v2Pointor < size2) {
                    if (v1Indices[v1Pointor] == v2Indices[v2Pointor]) {
                        resIndices[globalPointor] = v1Indices[v1Pointor];
                        resValues[globalPointor] = op.apply(v1Values[v1Pointor], v2Values[v2Pointor]);
                        v1Pointor++;
                        v2Pointor++;
                        globalPointor++;
                    } else if (v1Indices[v1Pointor] < v2Indices[v2Pointor]) {
                        resIndices[globalPointor] = v1Indices[v1Pointor];
                        resValues[globalPointor] = v1Values[v1Pointor];
                        v1Pointor++;
                        globalPointor++;
                    } else {
                        // v1Indices[v1Pointor] > v2Indices[v2Pointor]
                        resIndices[globalPointor] = v2Indices[v2Pointor];
                        resValues[globalPointor] = op.apply(0, v2Values[v2Pointor]);
                        v2Pointor++;
                        globalPointor++;
                    }
                }
            } else {
                while (v1Pointor < size1 || v2Pointor < size2) {
                    if ((v1Pointor < size1 && v2Pointor < size2) && v1Indices[v1Pointor] == v2Indices[v2Pointor]) {
                        newStorage.set(v1Indices[v1Pointor], op.apply(v1Values[v1Pointor], v2Values[v2Pointor]));
                        v1Pointor++;
                        v2Pointor++;
                    } else if ((v1Pointor < size1 && v2Pointor < size2) && v1Indices[v1Pointor] < v2Indices[v2Pointor] || (v1Pointor < size1 && v2Pointor >= size2)) {
                        newStorage.set(v1Indices[v1Pointor], v1Values[v1Pointor]);
                        v1Pointor++;
                    } else if (((v1Pointor < size1 && v2Pointor < size2) && v1Indices[v1Pointor] >= v2Indices[v2Pointor]) || (v1Pointor >= size1 && v2Pointor < size2)) {
                        newStorage.set(v2Indices[v2Pointor], op.apply(0, v2Values[v2Pointor]));
                        v2Pointor++;
                    }
                }
            }
        }
    } else {
        throw new AngelException("The operation is not support!");
    }
    return new IntFloatVector(v1.getMatrixId(), v1.getRowId(), v1.getClock(), v1.getDim(), newStorage);
}
Also used : AngelException(com.tencent.angel.exception.AngelException) IntBidirectionalIterator(it.unimi.dsi.fastutil.ints.IntBidirectionalIterator) IntFloatVector(com.tencent.angel.ml.math2.vector.IntFloatVector) ObjectIterator(it.unimi.dsi.fastutil.objects.ObjectIterator) IntFloatSortedVectorStorage(com.tencent.angel.ml.math2.storage.IntFloatSortedVectorStorage) IntFloatVectorStorage(com.tencent.angel.ml.math2.storage.IntFloatVectorStorage) Int2FloatMap(it.unimi.dsi.fastutil.ints.Int2FloatMap) IntAVLTreeSet(it.unimi.dsi.fastutil.ints.IntAVLTreeSet)

Example 28 with IntFloatVectorStorage

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

the class SimpleBinaryOutNonZAExecutor method apply.

public static Vector apply(IntFloatVector v1, IntLongVector v2, Binary op) {
    IntFloatVectorStorage newStorage = (IntFloatVectorStorage) StorageSwitch.apply(v1, v2, op);
    if (v1.isDense() && v2.isDense()) {
        float[] resValues = newStorage.getValues();
        float[] v1Values = v1.getStorage().getValues();
        long[] v2Values = v2.getStorage().getValues();
        for (int idx = 0; idx < resValues.length; idx++) {
            resValues[idx] = op.apply(v1Values[idx], v2Values[idx]);
        }
    } else if (v1.isDense() && v2.isSparse()) {
        float[] resValues = newStorage.getValues();
        float[] v1Values = v1.getStorage().getValues();
        ObjectIterator<Int2LongMap.Entry> iter = v2.getStorage().entryIterator();
        while (iter.hasNext()) {
            Int2LongMap.Entry entry = iter.next();
            int idx = entry.getIntKey();
            resValues[idx] = op.apply(v1Values[idx], entry.getLongValue());
        }
    } else if (v1.isDense() && v2.isSorted()) {
        float[] resValues = newStorage.getValues();
        float[] v1Values = v1.getStorage().getValues();
        int[] v2Indices = v2.getStorage().getIndices();
        long[] v2Values = v2.getStorage().getValues();
        int size = v2.size();
        for (int i = 0; i < size; i++) {
            int idx = v2Indices[i];
            resValues[idx] = op.apply(v1Values[idx], v2Values[i]);
        }
    } else if (v1.isSparse() && v2.isDense()) {
        if (op.isKeepStorage()) {
            int dim = v1.getDim();
            long[] v2Values = v2.getStorage().getValues();
            if (v1.size() < Constant.denseLoopThreshold * v1.getDim()) {
                for (int i = 0; i < dim; i++) {
                    newStorage.set(i, op.apply(0, v2Values[i]));
                }
                ObjectIterator<Int2FloatMap.Entry> iter = v1.getStorage().entryIterator();
                while (iter.hasNext()) {
                    Int2FloatMap.Entry entry = iter.next();
                    int idx = entry.getIntKey();
                    newStorage.set(idx, op.apply(entry.getFloatValue(), v2Values[idx]));
                }
            } else {
                for (int i = 0; i < dim; i++) {
                    if (v1.getStorage().hasKey(i)) {
                        newStorage.set(i, op.apply(v1.get(i), v2Values[i]));
                    } else {
                        newStorage.set(i, op.apply(0, v2Values[i]));
                    }
                }
            }
        } else {
            float[] resValues = newStorage.getValues();
            long[] v2Values = v2.getStorage().getValues();
            if (v1.size() < Constant.denseLoopThreshold * v1.getDim()) {
                for (int i = 0; i < resValues.length; i++) {
                    resValues[i] = op.apply(0, v2Values[i]);
                }
                ObjectIterator<Int2FloatMap.Entry> iter = v1.getStorage().entryIterator();
                while (iter.hasNext()) {
                    Int2FloatMap.Entry entry = iter.next();
                    int idx = entry.getIntKey();
                    resValues[idx] = op.apply(entry.getFloatValue(), v2Values[idx]);
                }
            } else {
                for (int i = 0; i < resValues.length; i++) {
                    if (v1.getStorage().hasKey(i)) {
                        resValues[i] = op.apply(v1.get(i), v2Values[i]);
                    } else {
                        resValues[i] = op.apply(0, v2Values[i]);
                    }
                }
            }
        }
    } else if (v1.isSorted() && v2.isDense()) {
        if (op.isKeepStorage()) {
            int dim = v1.getDim();
            int[] resIndices = newStorage.getIndices();
            float[] resValues = newStorage.getValues();
            long[] v2Values = v2.getStorage().getValues();
            int[] v1Indices = v1.getStorage().getIndices();
            float[] v1Values = v1.getStorage().getValues();
            for (int i = 0; i < dim; i++) {
                resIndices[i] = i;
                resValues[i] = op.apply(0, v2Values[i]);
            }
            int size = v1.size();
            for (int i = 0; i < size; i++) {
                int idx = v1Indices[i];
                resValues[idx] = op.apply(v1Values[i], v2Values[idx]);
            }
        } else {
            float[] resValues = newStorage.getValues();
            long[] v2Values = v2.getStorage().getValues();
            if (v1.size() < Constant.denseLoopThreshold * v1.getDim()) {
                int[] v1Indices = v1.getStorage().getIndices();
                float[] v1Values = v1.getStorage().getValues();
                for (int i = 0; i < resValues.length; i++) {
                    resValues[i] = op.apply(0, v2Values[i]);
                }
                int size = v1.size();
                for (int i = 0; i < size; i++) {
                    int idx = v1Indices[i];
                    resValues[idx] = op.apply(v1Values[i], v2Values[idx]);
                }
            } else {
                IntFloatVectorStorage v1Storage = v1.getStorage();
                for (int i = 0; i < resValues.length; i++) {
                    if (v1Storage.hasKey(i)) {
                        resValues[i] = op.apply(v1.get(i), v2Values[i]);
                    } else {
                        resValues[i] = op.apply(0, v2Values[i]);
                    }
                }
            }
        }
    } else if (v1.isSparse() && v2.isSparse()) {
        int v1Size = v1.size();
        int v2Size = v2.size();
        if (v1Size >= v2Size * Constant.sparseThreshold && (v1Size + v2Size) * Constant.intersectionCoeff <= Constant.sparseDenseStorageThreshold * v1.dim()) {
            // we gauss the indices of v2 maybe is a subset of v1, or overlap is very large
            ObjectIterator<Int2LongMap.Entry> iter = v2.getStorage().entryIterator();
            while (iter.hasNext()) {
                Int2LongMap.Entry entry = iter.next();
                int idx = entry.getIntKey();
                newStorage.set(idx, op.apply(v1.get(idx), entry.getLongValue()));
            }
        } else if ((v1Size + v2Size) * Constant.intersectionCoeff >= Constant.sparseDenseStorageThreshold * v1.dim()) {
            // we gauss dense storage is more efficient
            ObjectIterator<Int2FloatMap.Entry> iter1 = v1.getStorage().entryIterator();
            while (iter1.hasNext()) {
                Int2FloatMap.Entry entry = iter1.next();
                int idx = entry.getIntKey();
                newStorage.set(idx, entry.getFloatValue());
            }
            ObjectIterator<Int2LongMap.Entry> iter2 = v2.getStorage().entryIterator();
            while (iter2.hasNext()) {
                Int2LongMap.Entry entry = iter2.next();
                int idx = entry.getIntKey();
                newStorage.set(idx, op.apply(v1.get(idx), entry.getLongValue()));
            }
        } else {
            // to avoid multi-rehash
            int capacity = 1 << (32 - Integer.numberOfLeadingZeros((int) (v1.size() / 0.75)));
            if (v1.size() + v2.size() <= 1.5 * capacity) {
                // no rehashor one onle rehash is required, nothing to optimization
                ObjectIterator<Int2LongMap.Entry> iter = v2.getStorage().entryIterator();
                while (iter.hasNext()) {
                    Int2LongMap.Entry entry = iter.next();
                    int idx = entry.getIntKey();
                    newStorage.set(idx, op.apply(v1.get(idx), entry.getLongValue()));
                }
            } else {
                // multi-rehash
                ObjectIterator<Int2FloatMap.Entry> iter1 = v1.getStorage().entryIterator();
                while (iter1.hasNext()) {
                    Int2FloatMap.Entry entry = iter1.next();
                    int idx = entry.getIntKey();
                    newStorage.set(idx, entry.getFloatValue());
                }
                ObjectIterator<Int2LongMap.Entry> iter2 = v2.getStorage().entryIterator();
                while (iter2.hasNext()) {
                    Int2LongMap.Entry entry = iter2.next();
                    int idx = entry.getIntKey();
                    newStorage.set(idx, op.apply(v1.get(idx), entry.getLongValue()));
                }
            }
        }
    } else if (v1.isSparse() && v2.isSorted()) {
        int v1Size = v1.size();
        int v2Size = v2.size();
        if (v1Size >= v2Size * Constant.sparseThreshold && (v1Size + v2Size) * Constant.intersectionCoeff <= Constant.sparseDenseStorageThreshold * v1.dim()) {
            // we gauss the indices of v2 maybe is a subset of v1, or overlap is very large
            int[] v2Indices = v2.getStorage().getIndices();
            long[] v2Values = v2.getStorage().getValues();
            for (int i = 0; i < v2.size(); i++) {
                int idx = v2Indices[i];
                newStorage.set(idx, op.apply(v1.get(idx), v2Values[i]));
            }
        } else if ((v1Size + v2Size) * Constant.intersectionCoeff >= Constant.sparseDenseStorageThreshold * v1.dim()) {
            ObjectIterator<Int2FloatMap.Entry> iter1 = v1.getStorage().entryIterator();
            while (iter1.hasNext()) {
                Int2FloatMap.Entry entry = iter1.next();
                int idx = entry.getIntKey();
                newStorage.set(idx, entry.getFloatValue());
            }
            int[] v2Indices = v2.getStorage().getIndices();
            long[] v2Values = v2.getStorage().getValues();
            int size = v2.size();
            for (int i = 0; i < size; i++) {
                int idx = v2Indices[i];
                newStorage.set(idx, op.apply(v1.get(idx), v2Values[i]));
            }
        } else {
            // to avoid multi-rehash
            int capacity = 1 << (32 - Integer.numberOfLeadingZeros((int) (v1.size() / 0.75)));
            if (v1.size() + v2.size() <= 1.5 * capacity) {
                int[] v2Indices = v2.getStorage().getIndices();
                long[] v2Values = v2.getStorage().getValues();
                for (int i = 0; i < v2.size(); i++) {
                    int idx = v2Indices[i];
                    newStorage.set(idx, op.apply(v1.get(idx), v2Values[i]));
                }
            } else {
                ObjectIterator<Int2FloatMap.Entry> iter1 = v1.getStorage().entryIterator();
                while (iter1.hasNext()) {
                    Int2FloatMap.Entry entry = iter1.next();
                    int idx = entry.getIntKey();
                    newStorage.set(idx, entry.getFloatValue());
                }
                int[] v2Indices = v2.getStorage().getIndices();
                long[] v2Values = v2.getStorage().getValues();
                int size = v2.size();
                for (int i = 0; i < size; i++) {
                    int idx = v2Indices[i];
                    newStorage.set(idx, op.apply(v1.get(idx), v2Values[i]));
                }
            }
        }
    } else if (v1.isSorted() && v2.isSparse()) {
        int v1Size = v1.size();
        int v2Size = v2.size();
        if ((v1Size + v2Size) * Constant.intersectionCoeff >= Constant.sortedDenseStorageThreshold * v1.dim()) {
            if (op.isKeepStorage()) {
                int[] v1Indices = v1.getStorage().getIndices();
                int[] idxiter = v2.getStorage().indexIterator().toIntArray();
                int[] indices = new int[(int) (v1Size + v2Size)];
                System.arraycopy(v1Indices, 0, indices, 0, (int) v1.size());
                System.arraycopy(idxiter, 0, indices, (int) v1.size(), (int) v2.size());
                IntAVLTreeSet avl = new IntAVLTreeSet(indices);
                IntBidirectionalIterator iter = avl.iterator();
                float[] values = new float[indices.length];
                int i = 0;
                while (iter.hasNext()) {
                    int idx = iter.nextInt();
                    indices[i] = idx;
                    values[i] = op.apply(v1.get(idx), v2.get(idx));
                    i++;
                }
                while (i < indices.length) {
                    indices[i] = 0;
                    i++;
                }
                newStorage = new IntFloatSortedVectorStorage(v1.getDim(), (int) avl.size(), indices, values);
            } else {
                int[] v1Indices = v1.getStorage().getIndices();
                float[] v1Values = v1.getStorage().getValues();
                int size = v1.size();
                for (int i = 0; i < size; i++) {
                    int idx = v1Indices[i];
                    newStorage.set(idx, v1Values[i]);
                }
                ObjectIterator<Int2LongMap.Entry> iter = v2.getStorage().entryIterator();
                while (iter.hasNext()) {
                    Int2LongMap.Entry entry = iter.next();
                    int idx = entry.getIntKey();
                    newStorage.set(idx, op.apply(newStorage.get(idx), entry.getLongValue()));
                }
            }
        } else {
            if (op.isKeepStorage()) {
                int[] v1Indices = v1.getStorage().getIndices();
                int[] idxiter = v2.getStorage().indexIterator().toIntArray();
                int[] indices = new int[(int) (v1Size + v2Size)];
                System.arraycopy(v1Indices, 0, indices, 0, (int) v1.size());
                System.arraycopy(idxiter, 0, indices, (int) v1.size(), (int) v2.size());
                IntAVLTreeSet avl = new IntAVLTreeSet(indices);
                IntBidirectionalIterator iter = avl.iterator();
                float[] values = new float[indices.length];
                int i = 0;
                while (iter.hasNext()) {
                    int idx = iter.nextInt();
                    indices[i] = idx;
                    values[i] = op.apply(v1.get(idx), v2.get(idx));
                    i++;
                }
                while (i < indices.length) {
                    indices[i] = 0;
                    i++;
                }
                newStorage = new IntFloatSortedVectorStorage(v1.getDim(), (int) avl.size(), indices, values);
            } else {
                int[] v1Indices = v1.getStorage().getIndices();
                float[] v1Values = v1.getStorage().getValues();
                int size = v1.size();
                for (int i = 0; i < size; i++) {
                    int idx = v1Indices[i];
                    newStorage.set(idx, v1Values[i]);
                }
                ObjectIterator<Int2LongMap.Entry> iter = v2.getStorage().entryIterator();
                while (iter.hasNext()) {
                    Int2LongMap.Entry entry = iter.next();
                    int idx = entry.getIntKey();
                    newStorage.set(idx, op.apply(newStorage.get(idx), entry.getLongValue()));
                }
            }
        }
    } else if (v1.isSorted() && v2.isSorted()) {
        int v1Pointor = 0;
        int v2Pointor = 0;
        int size1 = v1.size();
        int size2 = v2.size();
        int[] v1Indices = v1.getStorage().getIndices();
        float[] v1Values = v1.getStorage().getValues();
        int[] v2Indices = v2.getStorage().getIndices();
        long[] v2Values = v2.getStorage().getValues();
        if ((size1 + size2) * Constant.intersectionCoeff >= Constant.sortedDenseStorageThreshold * v1.dim()) {
            if (op.isKeepStorage()) {
                // sorted
                int[] resIndices = newStorage.getIndices();
                float[] resValues = newStorage.getValues();
                int global = 0;
                while (v1Pointor < size1 && v2Pointor < size2) {
                    if (v1Indices[v1Pointor] == v2Indices[v2Pointor]) {
                        resIndices[global] = v1Indices[v1Pointor];
                        resValues[global] = op.apply(v1Values[v1Pointor], v2Values[v2Pointor]);
                        global++;
                        v1Pointor++;
                        v2Pointor++;
                    } else if (v1Indices[v1Pointor] < v2Indices[v2Pointor]) {
                        resIndices[global] = v1Indices[v1Pointor];
                        resValues[global] = v1Values[v1Pointor];
                        global++;
                        v1Pointor++;
                    } else {
                        // v1Indices[v1Pointor] > v2Indices[v2Pointor]
                        resIndices[global] = v2Indices[v2Pointor];
                        resValues[global] = op.apply(0, v2Values[v2Pointor]);
                        global++;
                        v2Pointor++;
                    }
                }
            } else {
                // dense
                while (v1Pointor < size1 || v2Pointor < size2) {
                    if ((v1Pointor < size1 && v2Pointor < size2) && v1Indices[v1Pointor] == v2Indices[v2Pointor]) {
                        newStorage.set(v1Indices[v1Pointor], op.apply(v1Values[v1Pointor], v2Values[v2Pointor]));
                        v1Pointor++;
                        v2Pointor++;
                    } else if ((v1Pointor < size1 && v2Pointor < size2) && v1Indices[v1Pointor] < v2Indices[v2Pointor] || (v1Pointor < size1 && v2Pointor >= size2)) {
                        newStorage.set(v1Indices[v1Pointor], v1Values[v1Pointor]);
                        v1Pointor++;
                    } else if (((v1Pointor < size1 && v2Pointor < size2) && v1Indices[v1Pointor] >= v2Indices[v2Pointor]) || (v1Pointor >= size1 && v2Pointor < size2)) {
                        newStorage.set(v2Indices[v2Pointor], op.apply(0, v2Values[v2Pointor]));
                        v2Pointor++;
                    }
                }
            }
        } else {
            if (op.isKeepStorage()) {
                int[] resIndices = newStorage.getIndices();
                float[] resValues = newStorage.getValues();
                int globalPointor = 0;
                while (v1Pointor < size1 && v2Pointor < size2) {
                    if (v1Indices[v1Pointor] == v2Indices[v2Pointor]) {
                        resIndices[globalPointor] = v1Indices[v1Pointor];
                        resValues[globalPointor] = op.apply(v1Values[v1Pointor], v2Values[v2Pointor]);
                        v1Pointor++;
                        v2Pointor++;
                        globalPointor++;
                    } else if (v1Indices[v1Pointor] < v2Indices[v2Pointor]) {
                        resIndices[globalPointor] = v1Indices[v1Pointor];
                        resValues[globalPointor] = v1Values[v1Pointor];
                        v1Pointor++;
                        globalPointor++;
                    } else {
                        // v1Indices[v1Pointor] > v2Indices[v2Pointor]
                        resIndices[globalPointor] = v2Indices[v2Pointor];
                        resValues[globalPointor] = op.apply(0, v2Values[v2Pointor]);
                        v2Pointor++;
                        globalPointor++;
                    }
                }
            } else {
                while (v1Pointor < size1 || v2Pointor < size2) {
                    if ((v1Pointor < size1 && v2Pointor < size2) && v1Indices[v1Pointor] == v2Indices[v2Pointor]) {
                        newStorage.set(v1Indices[v1Pointor], op.apply(v1Values[v1Pointor], v2Values[v2Pointor]));
                        v1Pointor++;
                        v2Pointor++;
                    } else if ((v1Pointor < size1 && v2Pointor < size2) && v1Indices[v1Pointor] < v2Indices[v2Pointor] || (v1Pointor < size1 && v2Pointor >= size2)) {
                        newStorage.set(v1Indices[v1Pointor], v1Values[v1Pointor]);
                        v1Pointor++;
                    } else if (((v1Pointor < size1 && v2Pointor < size2) && v1Indices[v1Pointor] >= v2Indices[v2Pointor]) || (v1Pointor >= size1 && v2Pointor < size2)) {
                        newStorage.set(v2Indices[v2Pointor], op.apply(0, v2Values[v2Pointor]));
                        v2Pointor++;
                    }
                }
            }
        }
    } else {
        throw new AngelException("The operation is not support!");
    }
    return new IntFloatVector(v1.getMatrixId(), v1.getRowId(), v1.getClock(), v1.getDim(), newStorage);
}
Also used : AngelException(com.tencent.angel.exception.AngelException) IntBidirectionalIterator(it.unimi.dsi.fastutil.ints.IntBidirectionalIterator) IntFloatVector(com.tencent.angel.ml.math2.vector.IntFloatVector) ObjectIterator(it.unimi.dsi.fastutil.objects.ObjectIterator) IntFloatSortedVectorStorage(com.tencent.angel.ml.math2.storage.IntFloatSortedVectorStorage) IntFloatVectorStorage(com.tencent.angel.ml.math2.storage.IntFloatVectorStorage) Int2FloatMap(it.unimi.dsi.fastutil.ints.Int2FloatMap) Int2LongMap(it.unimi.dsi.fastutil.ints.Int2LongMap) IntAVLTreeSet(it.unimi.dsi.fastutil.ints.IntAVLTreeSet)

Example 29 with IntFloatVectorStorage

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

the class SimpleBinaryOutAllExecutor method apply.

public static Vector apply(IntFloatVector v1, IntDummyVector v2, Binary op) {
    IntFloatVector res;
    if (v1.isDense()) {
        res = v1.copy();
        float[] resValues = res.getStorage().getValues();
        for (int i = 0; i < v1.getDim(); i++) {
            resValues[i] = op.apply(resValues[i], v2.get(i));
        }
    } else if (v1.isSparse()) {
        if (op.isKeepStorage()) {
            throw new AngelException("operation is not support!");
        } else {
            IntFloatVectorStorage newStorage = v1.getStorage().emptyDense();
            float[] resValues = newStorage.getValues();
            ObjectIterator<Int2FloatMap.Entry> iter1 = v1.getStorage().entryIterator();
            while (iter1.hasNext()) {
                Int2FloatMap.Entry entry = iter1.next();
                int idx = entry.getIntKey();
                resValues[idx] = entry.getFloatValue();
            }
            for (int i = 0; i < v1.getDim(); i++) {
                resValues[i] = op.apply(resValues[i], v2.get(i));
            }
            res = new IntFloatVector(v1.getMatrixId(), v1.getRowId(), v1.getClock(), v1.getDim(), newStorage);
        }
    } else {
        // sorted
        if (op.isKeepStorage()) {
            throw new AngelException("operation is not support!");
        } else {
            IntFloatVectorStorage newStorage = v1.getStorage().emptyDense();
            float[] resValues = newStorage.getValues();
            int[] v1Indices = v1.getStorage().getIndices();
            float[] v1Values = v1.getStorage().getValues();
            int size = v1.size();
            for (int i = 0; i < size; i++) {
                int idx = v1Indices[i];
                resValues[idx] = v1Values[i];
            }
            for (int i = 0; i < v1.getDim(); i++) {
                resValues[i] = op.apply(resValues[i], v2.get(i));
            }
            res = new IntFloatVector(v1.getMatrixId(), v1.getRowId(), v1.getClock(), v1.getDim(), newStorage);
        }
    }
    return res;
}
Also used : AngelException(com.tencent.angel.exception.AngelException) IntFloatVectorStorage(com.tencent.angel.ml.math2.storage.IntFloatVectorStorage) Int2FloatMap(it.unimi.dsi.fastutil.ints.Int2FloatMap) IntFloatVector(com.tencent.angel.ml.math2.vector.IntFloatVector) ObjectIterator(it.unimi.dsi.fastutil.objects.ObjectIterator)

Example 30 with IntFloatVectorStorage

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

the class SimpleUnaryExecutor method apply.

private static Vector apply(IntFloatVector v1, Unary op) {
    IntFloatVector res;
    if (op.isOrigin() || v1.isDense()) {
        if (!op.isInplace()) {
            res = v1.copy();
        } else {
            res = v1;
        }
        if (v1.isDense()) {
            float[] values = res.getStorage().getValues();
            for (int i = 0; i < values.length; i++) {
                values[i] = op.apply(values[i]);
            }
        } else if (v1.isSparse()) {
            ObjectIterator<Int2FloatMap.Entry> iter = res.getStorage().entryIterator();
            while (iter.hasNext()) {
                Int2FloatMap.Entry entry = iter.next();
                entry.setValue(op.apply(entry.getFloatValue()));
            }
        } else if (v1.isSorted()) {
            float[] values = res.getStorage().getValues();
            for (int i = 0; i < v1.size(); i++) {
                values[i] = op.apply(values[i]);
            }
        } else {
            throw new AngelException("The operation is not support!");
        }
    } else {
        IntFloatVectorStorage newstorage = v1.getStorage().emptyDense();
        IntFloatVectorStorage storage = v1.getStorage();
        float[] values = newstorage.getValues();
        float tmp = op.apply((float) 0);
        int dim = v1.getDim();
        for (int i = 0; i < dim; i++) {
            values[i] = tmp;
        }
        if (v1.isSparse()) {
            ObjectIterator<Int2FloatMap.Entry> iter = storage.entryIterator();
            while (iter.hasNext()) {
                Int2FloatMap.Entry entry = iter.next();
                values[entry.getIntKey()] = op.apply(entry.getFloatValue());
            }
        } else {
            // sort
            int[] idxs = storage.getIndices();
            float[] v1Values = storage.getValues();
            for (int k = 0; k < idxs.length; k++) {
                values[idxs[k]] = op.apply(v1Values[k]);
            }
        }
        if (op.isInplace()) {
            v1.setStorage(newstorage);
            res = v1;
        } else {
            res = new IntFloatVector(v1.getMatrixId(), v1.getRowId(), v1.getClock(), v1.getDim(), newstorage);
        }
    }
    return res;
}
Also used : AngelException(com.tencent.angel.exception.AngelException) IntFloatVectorStorage(com.tencent.angel.ml.math2.storage.IntFloatVectorStorage) Int2FloatMap(it.unimi.dsi.fastutil.ints.Int2FloatMap) IntFloatVector(com.tencent.angel.ml.math2.vector.IntFloatVector) ObjectIterator(it.unimi.dsi.fastutil.objects.ObjectIterator)

Aggregations

IntFloatVectorStorage (com.tencent.angel.ml.math2.storage.IntFloatVectorStorage)52 Int2FloatMap (it.unimi.dsi.fastutil.ints.Int2FloatMap)40 ObjectIterator (it.unimi.dsi.fastutil.objects.ObjectIterator)38 IntFloatVector (com.tencent.angel.ml.math2.vector.IntFloatVector)33 IntDoubleVectorStorage (com.tencent.angel.ml.math2.storage.IntDoubleVectorStorage)26 IntIntVectorStorage (com.tencent.angel.ml.math2.storage.IntIntVectorStorage)26 IntLongVectorStorage (com.tencent.angel.ml.math2.storage.IntLongVectorStorage)26 LongDoubleVectorStorage (com.tencent.angel.ml.math2.storage.LongDoubleVectorStorage)24 LongFloatVectorStorage (com.tencent.angel.ml.math2.storage.LongFloatVectorStorage)24 LongIntVectorStorage (com.tencent.angel.ml.math2.storage.LongIntVectorStorage)24 LongLongVectorStorage (com.tencent.angel.ml.math2.storage.LongLongVectorStorage)24 Storage (com.tencent.angel.ml.math2.storage.Storage)24 CompIntFloatVector (com.tencent.angel.ml.math2.vector.CompIntFloatVector)24 IntFloatSortedVectorStorage (com.tencent.angel.ml.math2.storage.IntFloatSortedVectorStorage)22 IntFloatSparseVectorStorage (com.tencent.angel.ml.math2.storage.IntFloatSparseVectorStorage)19 IntDoubleSortedVectorStorage (com.tencent.angel.ml.math2.storage.IntDoubleSortedVectorStorage)16 IntDoubleSparseVectorStorage (com.tencent.angel.ml.math2.storage.IntDoubleSparseVectorStorage)16 IntIntSortedVectorStorage (com.tencent.angel.ml.math2.storage.IntIntSortedVectorStorage)16 IntIntSparseVectorStorage (com.tencent.angel.ml.math2.storage.IntIntSparseVectorStorage)16 IntLongSortedVectorStorage (com.tencent.angel.ml.math2.storage.IntLongSortedVectorStorage)16