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