Search in sources :

Example 1 with RowColMatrixKey

use of org.apache.ignite.ml.math.distributed.keys.RowColMatrixKey in project ignite by apache.

the class SparseDistributedMatrix method times.

/**
 * {@inheritDoc}
 */
@Override
public Vector times(Vector vec) {
    if (vec == null)
        throw new IllegalArgumentException("The vector should be not null.");
    if (columnSize() != vec.size())
        throw new CardinalityException(columnSize(), vec.size());
    SparseDistributedMatrix matrixA = this;
    SparseDistributedVector vectorB = (SparseDistributedVector) vec;
    String cacheName = storage().cacheName();
    int rows = this.rowSize();
    SparseDistributedVector vectorC = (SparseDistributedVector) likeVector(rows);
    CacheUtils.bcast(cacheName, () -> {
        Ignite ignite = Ignition.localIgnite();
        Affinity<RowColMatrixKey> affinity = ignite.affinity(cacheName);
        ClusterNode locNode = ignite.cluster().localNode();
        SparseDistributedVectorStorage storageC = vectorC.storage();
        Map<ClusterNode, Collection<RowColMatrixKey>> keysCToNodes = affinity.mapKeysToNodes(storageC.getAllKeys());
        Collection<RowColMatrixKey> locKeys = keysCToNodes.get(locNode);
        if (locKeys == null)
            return;
        // compute Cij locally on each node
        // TODO: IGNITE:5114, exec in parallel
        locKeys.forEach(key -> {
            int idx = key.index();
            Vector Aik = matrixA.getRow(idx);
            vectorC.set(idx, Aik.times(vectorB).sum());
        });
    });
    return vectorC;
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) SparseDistributedVectorStorage(org.apache.ignite.ml.math.impls.storage.vector.SparseDistributedVectorStorage) SparseDistributedVector(org.apache.ignite.ml.math.impls.vector.SparseDistributedVector) RowColMatrixKey(org.apache.ignite.ml.math.distributed.keys.RowColMatrixKey) Collection(java.util.Collection) Ignite(org.apache.ignite.Ignite) CardinalityException(org.apache.ignite.ml.math.exceptions.CardinalityException) SparseDistributedVector(org.apache.ignite.ml.math.impls.vector.SparseDistributedVector) Vector(org.apache.ignite.ml.math.Vector)

Example 2 with RowColMatrixKey

use of org.apache.ignite.ml.math.distributed.keys.RowColMatrixKey in project ignite by apache.

the class SparseDistributedVectorStorage method set.

/**
 * Sets vector element by index
 *
 * @param i Vector element index.
 * @param v Value to set at given index.
 */
@Override
public void set(int i, double v) {
    // Remote set on the primary node (where given row or column is stored locally).
    ignite().compute(getClusterGroupForGivenKey(CACHE_NAME, getCacheKey(i))).run(() -> {
        IgniteCache<RowColMatrixKey, Double> cache = Ignition.localIgnite().getOrCreateCache(CACHE_NAME);
        RowColMatrixKey cacheKey = getCacheKey(i);
        if (v != 0.0)
            cache.put(cacheKey, v);
        else if (// remove zero elements
        cache.containsKey(cacheKey))
            cache.remove(cacheKey);
    });
}
Also used : RowColMatrixKey(org.apache.ignite.ml.math.distributed.keys.RowColMatrixKey)

Example 3 with RowColMatrixKey

use of org.apache.ignite.ml.math.distributed.keys.RowColMatrixKey in project ignite by apache.

the class SparseDistributedMatrixMapReducer method mapReduce.

/**
 */
public <R, T> R mapReduce(IgniteBiFunction<Matrix, T, R> mapper, IgniteFunction<Collection<R>, R> reducer, T args) {
    Ignite ignite = Ignition.localIgnite();
    SparseDistributedMatrixStorage storage = (SparseDistributedMatrixStorage) distributedMatrix.getStorage();
    int colSize = distributedMatrix.columnSize();
    Collection<R> results = ignite.compute(ignite.cluster().forDataNodes(storage.cacheName())).broadcast(arguments -> {
        Ignite locIgnite = Ignition.localIgnite();
        Affinity<RowColMatrixKey> affinity = locIgnite.affinity(storage.cacheName());
        ClusterNode locNode = locIgnite.cluster().localNode();
        Map<ClusterNode, Collection<RowColMatrixKey>> keys = affinity.mapKeysToNodes(storage.getAllKeys());
        Collection<RowColMatrixKey> locKeys = keys.get(locNode);
        if (locKeys != null) {
            int idx = 0;
            Matrix locMatrix = new DenseLocalOnHeapMatrix(locKeys.size(), colSize);
            for (RowColMatrixKey key : locKeys) {
                Map<Integer, Double> row = storage.cache().get(key);
                for (Map.Entry<Integer, Double> cell : row.entrySet()) locMatrix.set(idx, cell.getKey(), cell.getValue());
                idx++;
            }
            return mapper.apply(locMatrix, arguments);
        }
        return null;
    }, args);
    return reducer.apply(results);
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) SparseDistributedMatrixStorage(org.apache.ignite.ml.math.impls.storage.matrix.SparseDistributedMatrixStorage) DenseLocalOnHeapMatrix(org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix) SparseDistributedMatrix(org.apache.ignite.ml.math.impls.matrix.SparseDistributedMatrix) Matrix(org.apache.ignite.ml.math.Matrix) DenseLocalOnHeapMatrix(org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix) RowColMatrixKey(org.apache.ignite.ml.math.distributed.keys.RowColMatrixKey) Collection(java.util.Collection) Ignite(org.apache.ignite.Ignite) Map(java.util.Map)

Example 4 with RowColMatrixKey

use of org.apache.ignite.ml.math.distributed.keys.RowColMatrixKey in project ignite by apache.

the class SparseDistributedMatrixStorage method matrixSet.

/**
 * Distributed matrix set.
 *
 * @param a Row or column index.
 * @param b Row or column index.
 * @param v New value to set.
 */
private void matrixSet(int a, int b, double v) {
    // Remote set on the primary node (where given row or column is stored locally).
    ignite().compute(getClusterGroupForGivenKey(CACHE_NAME, a)).run(() -> {
        IgniteCache<RowColMatrixKey, Map<Integer, Double>> cache = Ignition.localIgnite().getOrCreateCache(CACHE_NAME);
        // Local get.
        Map<Integer, Double> map = cache.localPeek(getCacheKey(a), CachePeekMode.PRIMARY);
        if (map == null) {
            // Remote entry get.
            map = cache.get(getCacheKey(a));
            if (map == null)
                map = acsMode == SEQUENTIAL_ACCESS_MODE ? new Int2DoubleRBTreeMap() : new Int2DoubleOpenHashMap();
        }
        if (v != 0.0)
            map.put(b, v);
        else if (map.containsKey(b))
            map.remove(b);
        // Local put.
        cache.put(getCacheKey(a), map);
    });
}
Also used : Int2DoubleOpenHashMap(it.unimi.dsi.fastutil.ints.Int2DoubleOpenHashMap) RowColMatrixKey(org.apache.ignite.ml.math.distributed.keys.RowColMatrixKey) Int2DoubleRBTreeMap(it.unimi.dsi.fastutil.ints.Int2DoubleRBTreeMap) Int2DoubleRBTreeMap(it.unimi.dsi.fastutil.ints.Int2DoubleRBTreeMap) Int2DoubleOpenHashMap(it.unimi.dsi.fastutil.ints.Int2DoubleOpenHashMap) Map(java.util.Map)

Example 5 with RowColMatrixKey

use of org.apache.ignite.ml.math.distributed.keys.RowColMatrixKey in project ignite by apache.

the class SparseDistributedMatrix method times.

/**
 * {@inheritDoc}
 */
@Override
public Matrix times(Matrix mtx) {
    if (mtx == null)
        throw new IllegalArgumentException("The matrix should be not null.");
    if (columnSize() != mtx.rowSize())
        throw new CardinalityException(columnSize(), mtx.rowSize());
    SparseDistributedMatrix matrixA = this;
    SparseDistributedMatrix matrixB = (SparseDistributedMatrix) mtx;
    String cacheName = storage().cacheName();
    SparseDistributedMatrix matrixC = new SparseDistributedMatrix(matrixA.rowSize(), matrixB.columnSize(), getStorage().storageMode(), getStorage().isRandomAccess() ? RANDOM_ACCESS_MODE : SEQUENTIAL_ACCESS_MODE);
    CacheUtils.bcast(cacheName, () -> {
        Ignite ignite = Ignition.localIgnite();
        Affinity<RowColMatrixKey> affinity = ignite.affinity(cacheName);
        ClusterNode locNode = ignite.cluster().localNode();
        SparseDistributedMatrixStorage storageC = matrixC.storage();
        Map<ClusterNode, Collection<RowColMatrixKey>> keysCToNodes = affinity.mapKeysToNodes(storageC.getAllKeys());
        Collection<RowColMatrixKey> locKeys = keysCToNodes.get(locNode);
        boolean isRowMode = storageC.storageMode() == ROW_STORAGE_MODE;
        if (locKeys == null)
            return;
        // compute Cij locally on each node
        // TODO: IGNITE:5114, exec in parallel
        locKeys.forEach(key -> {
            int idx = key.index();
            if (isRowMode) {
                Vector Aik = matrixA.getRow(idx);
                for (int i = 0; i < matrixB.columnSize(); i++) {
                    Vector Bkj = matrixB.getCol(i);
                    matrixC.set(idx, i, Aik.times(Bkj).sum());
                }
            } else {
                Vector Bkj = matrixB.getCol(idx);
                for (int i = 0; i < matrixA.rowSize(); i++) {
                    Vector Aik = matrixA.getRow(i);
                    matrixC.set(idx, i, Aik.times(Bkj).sum());
                }
            }
        });
    });
    return matrixC;
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) SparseDistributedMatrixStorage(org.apache.ignite.ml.math.impls.storage.matrix.SparseDistributedMatrixStorage) RowColMatrixKey(org.apache.ignite.ml.math.distributed.keys.RowColMatrixKey) Collection(java.util.Collection) Ignite(org.apache.ignite.Ignite) CardinalityException(org.apache.ignite.ml.math.exceptions.CardinalityException) SparseDistributedVector(org.apache.ignite.ml.math.impls.vector.SparseDistributedVector) Vector(org.apache.ignite.ml.math.Vector)

Aggregations

RowColMatrixKey (org.apache.ignite.ml.math.distributed.keys.RowColMatrixKey)5 Collection (java.util.Collection)3 Ignite (org.apache.ignite.Ignite)3 ClusterNode (org.apache.ignite.cluster.ClusterNode)3 Map (java.util.Map)2 Vector (org.apache.ignite.ml.math.Vector)2 CardinalityException (org.apache.ignite.ml.math.exceptions.CardinalityException)2 SparseDistributedMatrixStorage (org.apache.ignite.ml.math.impls.storage.matrix.SparseDistributedMatrixStorage)2 SparseDistributedVector (org.apache.ignite.ml.math.impls.vector.SparseDistributedVector)2 Int2DoubleOpenHashMap (it.unimi.dsi.fastutil.ints.Int2DoubleOpenHashMap)1 Int2DoubleRBTreeMap (it.unimi.dsi.fastutil.ints.Int2DoubleRBTreeMap)1 Matrix (org.apache.ignite.ml.math.Matrix)1 DenseLocalOnHeapMatrix (org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix)1 SparseDistributedMatrix (org.apache.ignite.ml.math.impls.matrix.SparseDistributedMatrix)1 SparseDistributedVectorStorage (org.apache.ignite.ml.math.impls.storage.vector.SparseDistributedVectorStorage)1