Search in sources :

Example 11 with SparseDistributedMatrixStorage

use of org.apache.ignite.ml.math.impls.storage.matrix.SparseDistributedMatrixStorage 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

SparseDistributedMatrixStorage (org.apache.ignite.ml.math.impls.storage.matrix.SparseDistributedMatrixStorage)11 Vector (org.apache.ignite.ml.math.Vector)9 UUID (java.util.UUID)8 SparseDistributedMatrix (org.apache.ignite.ml.math.impls.matrix.SparseDistributedMatrix)7 Map (java.util.Map)6 DenseLocalOnHeapVector (org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector)6 List (java.util.List)5 Random (java.util.Random)5 Collectors (java.util.stream.Collectors)5 SparseMatrixKey (org.apache.ignite.ml.math.distributed.keys.impl.SparseMatrixKey)5 DenseLocalOnHeapMatrix (org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix)4 Int2DoubleOpenHashMap (it.unimi.dsi.fastutil.ints.Int2DoubleOpenHashMap)3 IOException (java.io.IOException)3 InputStream (java.io.InputStream)3 ArrayList (java.util.ArrayList)3 Arrays (java.util.Arrays)3 Collections (java.util.Collections)3 HashMap (java.util.HashMap)3 Iterator (java.util.Iterator)3 LinkedList (java.util.LinkedList)3