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());
});
}
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;
}
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;
}
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);
});
}
Aggregations