use of org.apache.ignite.ml.math.distributed.keys.RowColMatrixKey in project ignite by apache.
the class SparseDistributedMatrix method times.
/**
* {@inheritDoc}
*/
@Override
public Vector times(Vector vec) {
if (vec == null)
throw new IllegalArgumentException("The vector should be not null.");
if (columnSize() != vec.size())
throw new CardinalityException(columnSize(), vec.size());
SparseDistributedMatrix matrixA = this;
SparseDistributedVector vectorB = (SparseDistributedVector) vec;
String cacheName = storage().cacheName();
int rows = this.rowSize();
SparseDistributedVector vectorC = (SparseDistributedVector) likeVector(rows);
CacheUtils.bcast(cacheName, () -> {
Ignite ignite = Ignition.localIgnite();
Affinity<RowColMatrixKey> affinity = ignite.affinity(cacheName);
ClusterNode locNode = ignite.cluster().localNode();
SparseDistributedVectorStorage storageC = vectorC.storage();
Map<ClusterNode, Collection<RowColMatrixKey>> keysCToNodes = affinity.mapKeysToNodes(storageC.getAllKeys());
Collection<RowColMatrixKey> locKeys = keysCToNodes.get(locNode);
if (locKeys == null)
return;
// compute Cij locally on each node
// TODO: IGNITE:5114, exec in parallel
locKeys.forEach(key -> {
int idx = key.index();
Vector Aik = matrixA.getRow(idx);
vectorC.set(idx, Aik.times(vectorB).sum());
});
});
return vectorC;
}
use of org.apache.ignite.ml.math.distributed.keys.RowColMatrixKey in project ignite by apache.
the class SparseDistributedVectorStorage method set.
/**
* Sets vector element by index
*
* @param i Vector element index.
* @param v Value to set at given index.
*/
@Override
public void set(int i, double v) {
// Remote set on the primary node (where given row or column is stored locally).
ignite().compute(getClusterGroupForGivenKey(CACHE_NAME, getCacheKey(i))).run(() -> {
IgniteCache<RowColMatrixKey, Double> cache = Ignition.localIgnite().getOrCreateCache(CACHE_NAME);
RowColMatrixKey cacheKey = getCacheKey(i);
if (v != 0.0)
cache.put(cacheKey, v);
else if (// remove zero elements
cache.containsKey(cacheKey))
cache.remove(cacheKey);
});
}
use of org.apache.ignite.ml.math.distributed.keys.RowColMatrixKey in project ignite by apache.
the class SparseDistributedMatrixMapReducer method mapReduce.
/**
*/
public <R, T> R mapReduce(IgniteBiFunction<Matrix, T, R> mapper, IgniteFunction<Collection<R>, R> reducer, T args) {
Ignite ignite = Ignition.localIgnite();
SparseDistributedMatrixStorage storage = (SparseDistributedMatrixStorage) distributedMatrix.getStorage();
int colSize = distributedMatrix.columnSize();
Collection<R> results = ignite.compute(ignite.cluster().forDataNodes(storage.cacheName())).broadcast(arguments -> {
Ignite locIgnite = Ignition.localIgnite();
Affinity<RowColMatrixKey> affinity = locIgnite.affinity(storage.cacheName());
ClusterNode locNode = locIgnite.cluster().localNode();
Map<ClusterNode, Collection<RowColMatrixKey>> keys = affinity.mapKeysToNodes(storage.getAllKeys());
Collection<RowColMatrixKey> locKeys = keys.get(locNode);
if (locKeys != null) {
int idx = 0;
Matrix locMatrix = new DenseLocalOnHeapMatrix(locKeys.size(), colSize);
for (RowColMatrixKey key : locKeys) {
Map<Integer, Double> row = storage.cache().get(key);
for (Map.Entry<Integer, Double> cell : row.entrySet()) locMatrix.set(idx, cell.getKey(), cell.getValue());
idx++;
}
return mapper.apply(locMatrix, arguments);
}
return null;
}, args);
return reducer.apply(results);
}
use of org.apache.ignite.ml.math.distributed.keys.RowColMatrixKey in project ignite by apache.
the class SparseDistributedMatrixStorage 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) {
// Remote set on the primary node (where given row or column is stored locally).
ignite().compute(getClusterGroupForGivenKey(CACHE_NAME, a)).run(() -> {
IgniteCache<RowColMatrixKey, Map<Integer, Double>> cache = Ignition.localIgnite().getOrCreateCache(CACHE_NAME);
// Local get.
Map<Integer, Double> map = cache.localPeek(getCacheKey(a), CachePeekMode.PRIMARY);
if (map == null) {
// Remote entry get.
map = cache.get(getCacheKey(a));
if (map == null)
map = acsMode == SEQUENTIAL_ACCESS_MODE ? new Int2DoubleRBTreeMap() : new Int2DoubleOpenHashMap();
}
if (v != 0.0)
map.put(b, v);
else if (map.containsKey(b))
map.remove(b);
// Local put.
cache.put(getCacheKey(a), map);
});
}
use of org.apache.ignite.ml.math.distributed.keys.RowColMatrixKey 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