Search in sources :

Example 1 with MatrixBlockKey

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

the class BlockMatrixStorage method matrixGet.

/**
 * Distributed matrix get.
 *
 * @param a Row or column index.
 * @param b Row or column index.
 * @return Matrix value at (a, b) index.
 */
private double matrixGet(int a, int b) {
    // Remote get from the primary node (where given row or column is stored locally).
    return ignite().compute(getClusterGroupForGivenKey(CACHE_NAME, getBlockId(a, b))).call(() -> {
        IgniteCache<MatrixBlockKey, MatrixBlockEntry> cache = Ignition.localIgnite().getOrCreateCache(CACHE_NAME);
        MatrixBlockKey key = getCacheKey(getBlockId(a, b));
        // Local get.
        MatrixBlockEntry block = cache.localPeek(key, CachePeekMode.PRIMARY);
        if (block == null)
            block = cache.get(key);
        return block == null ? 0.0 : block.get(a % block.rowSize(), b % block.columnSize());
    });
}
Also used : MatrixBlockKey(org.apache.ignite.ml.math.distributed.keys.impl.MatrixBlockKey) MatrixBlockEntry(org.apache.ignite.ml.math.impls.matrix.MatrixBlockEntry)

Example 2 with MatrixBlockKey

use of org.apache.ignite.ml.math.distributed.keys.impl.MatrixBlockKey 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)

Example 3 with MatrixBlockKey

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

the class BlockMatrixStorage method getEntryById.

/**
 * Returns cached or new BlockEntry by given blockId.
 *
 * @param blockId blockId
 * @return BlockEntry
 */
private MatrixBlockEntry getEntryById(IgnitePair<Long> blockId) {
    MatrixBlockKey key = getCacheKey(blockId.get1(), blockId.get2());
    MatrixBlockEntry entry = cache.localPeek(key, CachePeekMode.PRIMARY);
    entry = entry != null ? entry : cache.get(key);
    if (entry == null)
        entry = getEmptyBlockEntry(blockId);
    return entry;
}
Also used : MatrixBlockKey(org.apache.ignite.ml.math.distributed.keys.impl.MatrixBlockKey) MatrixBlockEntry(org.apache.ignite.ml.math.impls.matrix.MatrixBlockEntry)

Example 4 with MatrixBlockKey

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

the class BlockMatrixStorage 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) {
    IgnitePair<Long> blockId = getBlockId(a, b);
    // Remote set on the primary node (where given row or column is stored locally).
    ignite().compute(getClusterGroupForGivenKey(CACHE_NAME, blockId)).run(() -> {
        IgniteCache<MatrixBlockKey, MatrixBlockEntry> cache = Ignition.localIgnite().getOrCreateCache(CACHE_NAME);
        MatrixBlockKey key = getCacheKey(blockId.get1(), blockId.get2());
        // Local get.
        MatrixBlockEntry block = getEntryById(blockId);
        block.set(a % block.rowSize(), b % block.columnSize(), v);
        // Local put.
        cache.put(key, block);
    });
}
Also used : MatrixBlockKey(org.apache.ignite.ml.math.distributed.keys.impl.MatrixBlockKey) MatrixBlockEntry(org.apache.ignite.ml.math.impls.matrix.MatrixBlockEntry)

Aggregations

MatrixBlockKey (org.apache.ignite.ml.math.distributed.keys.impl.MatrixBlockKey)4 MatrixBlockEntry (org.apache.ignite.ml.math.impls.matrix.MatrixBlockEntry)3 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 CardinalityException (org.apache.ignite.ml.math.exceptions.CardinalityException)1 BlockMatrixStorage (org.apache.ignite.ml.math.impls.storage.matrix.BlockMatrixStorage)1