Search in sources :

Example 1 with BlockMatrixStorage

use of org.apache.ignite.ml.math.impls.storage.matrix.BlockMatrixStorage in project ignite by apache.

the class SparseBlockDistributedMatrix method times.

/**
 * {@inheritDoc}
 */
@SuppressWarnings({ "unchecked" })
@Override
public Matrix times(final Matrix mtx) {
    if (mtx == null)
        throw new IllegalArgumentException("The matrix should be not null.");
    if (columnSize() != mtx.rowSize())
        throw new CardinalityException(columnSize(), mtx.rowSize());
    SparseBlockDistributedMatrix matrixA = this;
    SparseBlockDistributedMatrix matrixB = (SparseBlockDistributedMatrix) mtx;
    String cacheName = this.storage().cacheName();
    SparseBlockDistributedMatrix matrixC = new SparseBlockDistributedMatrix(matrixA.rowSize(), matrixB.columnSize());
    CacheUtils.bcast(cacheName, () -> {
        Ignite ignite = Ignition.localIgnite();
        Affinity<MatrixBlockKey> affinity = ignite.affinity(cacheName);
        IgniteCache<MatrixBlockKey, MatrixBlockEntry> cache = ignite.getOrCreateCache(cacheName);
        ClusterNode locNode = ignite.cluster().localNode();
        BlockMatrixStorage storageC = matrixC.storage();
        Map<ClusterNode, Collection<MatrixBlockKey>> keysCToNodes = affinity.mapKeysToNodes(storageC.getAllKeys());
        Collection<MatrixBlockKey> locKeys = keysCToNodes.get(locNode);
        if (locKeys == null)
            return;
        // compute Cij locally on each node
        // TODO: IGNITE:5114, exec in parallel
        locKeys.forEach(key -> {
            long newBlockIdRow = key.blockRowId();
            long newBlockIdCol = key.blockColId();
            IgnitePair<Long> newBlockId = new IgnitePair<>(newBlockIdRow, newBlockIdCol);
            MatrixBlockEntry blockC = null;
            List<MatrixBlockEntry> aRow = matrixA.storage().getRowForBlock(newBlockId);
            List<MatrixBlockEntry> bCol = matrixB.storage().getColForBlock(newBlockId);
            for (int i = 0; i < aRow.size(); i++) {
                MatrixBlockEntry blockA = aRow.get(i);
                MatrixBlockEntry blockB = bCol.get(i);
                MatrixBlockEntry tmpBlock = new MatrixBlockEntry(blockA.times(blockB));
                blockC = blockC == null ? tmpBlock : new MatrixBlockEntry(blockC.plus(tmpBlock));
            }
            cache.put(storageC.getCacheKey(newBlockIdRow, newBlockIdCol), blockC);
        });
    });
    return matrixC;
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) BlockMatrixStorage(org.apache.ignite.ml.math.impls.storage.matrix.BlockMatrixStorage) MatrixBlockKey(org.apache.ignite.ml.math.distributed.keys.impl.MatrixBlockKey) IgnitePair(org.apache.ignite.internal.util.lang.IgnitePair) Collection(java.util.Collection) Ignite(org.apache.ignite.Ignite) CardinalityException(org.apache.ignite.ml.math.exceptions.CardinalityException)

Aggregations

Collection (java.util.Collection)1 Ignite (org.apache.ignite.Ignite)1 ClusterNode (org.apache.ignite.cluster.ClusterNode)1 IgnitePair (org.apache.ignite.internal.util.lang.IgnitePair)1 MatrixBlockKey (org.apache.ignite.ml.math.distributed.keys.impl.MatrixBlockKey)1 CardinalityException (org.apache.ignite.ml.math.exceptions.CardinalityException)1 BlockMatrixStorage (org.apache.ignite.ml.math.impls.storage.matrix.BlockMatrixStorage)1