Search in sources :

Example 1 with MatrixBlockEntry

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

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

the class BlockMatrixStorage method getEmptyBlockEntry.

/**
 * Builds empty BlockEntry with sizes based on blockId and BlockMatrixStorage fields' values.
 *
 * @param blockId blockId
 * @return Empty BlockEntry
 */
private MatrixBlockEntry getEmptyBlockEntry(IgnitePair<Long> blockId) {
    MatrixBlockEntry entry;
    int rowMod = rows % maxBlockEdge;
    int colMod = cols % maxBlockEdge;
    int rowSize;
    if (rowMod == 0)
        rowSize = maxBlockEdge;
    else
        rowSize = blockId.get1() != (blocksInRow - 1) ? maxBlockEdge : rowMod;
    int colSize;
    if (colMod == 0)
        colSize = maxBlockEdge;
    else
        colSize = blockId.get2() != (blocksInCol - 1) ? maxBlockEdge : colMod;
    entry = new MatrixBlockEntry(rowSize, colSize);
    return entry;
}
Also used : MatrixBlockEntry(org.apache.ignite.ml.math.impls.matrix.MatrixBlockEntry)

Example 3 with MatrixBlockEntry

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

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

MatrixBlockEntry (org.apache.ignite.ml.math.impls.matrix.MatrixBlockEntry)4 MatrixBlockKey (org.apache.ignite.ml.math.distributed.keys.impl.MatrixBlockKey)3