use of org.apache.ignite.ml.math.impls.storage.matrix.BlockVectorStorage in project ignite by apache.
the class SparseBlockDistributedMatrix method times.
/**
* {@inheritDoc}
*/
@SuppressWarnings({ "unchecked" })
@Override
public Vector times(final Vector vec) {
if (vec == null)
throw new IllegalArgumentException("The vector should be not null.");
if (columnSize() != vec.size())
throw new CardinalityException(columnSize(), vec.size());
SparseBlockDistributedMatrix matrixA = this;
SparseBlockDistributedVector vectorB = (SparseBlockDistributedVector) vec;
String cacheName = this.storage().cacheName();
SparseBlockDistributedVector vectorC = new SparseBlockDistributedVector(matrixA.rowSize());
CacheUtils.bcast(cacheName, () -> {
Ignite ignite = Ignition.localIgnite();
Affinity<VectorBlockKey> affinity = ignite.affinity(cacheName);
IgniteCache<VectorBlockKey, VectorBlockEntry> cache = ignite.getOrCreateCache(cacheName);
ClusterNode locNode = ignite.cluster().localNode();
BlockVectorStorage storageC = vectorC.storage();
Map<ClusterNode, Collection<VectorBlockKey>> keysCToNodes = affinity.mapKeysToNodes(storageC.getAllKeys());
Collection<VectorBlockKey> locKeys = keysCToNodes.get(locNode);
if (locKeys == null)
return;
// compute Cij locally on each node
// TODO: IGNITE:5114, exec in parallel
locKeys.forEach(key -> {
long newBlockId = key.blockId();
IgnitePair<Long> newBlockIdForMtx = new IgnitePair<>(newBlockId, 0L);
VectorBlockEntry blockC = null;
List<MatrixBlockEntry> aRow = matrixA.storage().getRowForBlock(newBlockIdForMtx);
List<VectorBlockEntry> bCol = vectorB.storage().getColForBlock(newBlockId);
for (int i = 0; i < aRow.size(); i++) {
MatrixBlockEntry blockA = aRow.get(i);
VectorBlockEntry blockB = bCol.get(i);
VectorBlockEntry tmpBlock = new VectorBlockEntry(blockA.times(blockB));
blockC = blockC == null ? tmpBlock : new VectorBlockEntry(blockC.plus(tmpBlock));
}
cache.put(storageC.getCacheKey(newBlockId), blockC);
});
});
return vectorC;
}
Aggregations