use of org.apache.ignite.ml.math.impls.storage.matrix.SparseDistributedMatrixStorage in project ignite by apache.
the class SparseDistributedMatrix method times.
/**
* {@inheritDoc}
*/
@Override
public Matrix times(Matrix mtx) {
if (mtx == null)
throw new IllegalArgumentException("The matrix should be not null.");
if (columnSize() != mtx.rowSize())
throw new CardinalityException(columnSize(), mtx.rowSize());
SparseDistributedMatrix matrixA = this;
SparseDistributedMatrix matrixB = (SparseDistributedMatrix) mtx;
String cacheName = storage().cacheName();
SparseDistributedMatrix matrixC = new SparseDistributedMatrix(matrixA.rowSize(), matrixB.columnSize(), getStorage().storageMode(), getStorage().isRandomAccess() ? RANDOM_ACCESS_MODE : SEQUENTIAL_ACCESS_MODE);
CacheUtils.bcast(cacheName, () -> {
Ignite ignite = Ignition.localIgnite();
Affinity<RowColMatrixKey> affinity = ignite.affinity(cacheName);
ClusterNode locNode = ignite.cluster().localNode();
SparseDistributedMatrixStorage storageC = matrixC.storage();
Map<ClusterNode, Collection<RowColMatrixKey>> keysCToNodes = affinity.mapKeysToNodes(storageC.getAllKeys());
Collection<RowColMatrixKey> locKeys = keysCToNodes.get(locNode);
boolean isRowMode = storageC.storageMode() == ROW_STORAGE_MODE;
if (locKeys == null)
return;
// compute Cij locally on each node
// TODO: IGNITE:5114, exec in parallel
locKeys.forEach(key -> {
int idx = key.index();
if (isRowMode) {
Vector Aik = matrixA.getRow(idx);
for (int i = 0; i < matrixB.columnSize(); i++) {
Vector Bkj = matrixB.getCol(i);
matrixC.set(idx, i, Aik.times(Bkj).sum());
}
} else {
Vector Bkj = matrixB.getCol(idx);
for (int i = 0; i < matrixA.rowSize(); i++) {
Vector Aik = matrixA.getRow(i);
matrixC.set(idx, i, Aik.times(Bkj).sum());
}
}
});
});
return matrixC;
}
Aggregations