Search in sources :

Example 1 with SparseDistributedVectorStorage

use of org.apache.ignite.ml.math.impls.storage.vector.SparseDistributedVectorStorage 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;
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) SparseDistributedVectorStorage(org.apache.ignite.ml.math.impls.storage.vector.SparseDistributedVectorStorage) SparseDistributedVector(org.apache.ignite.ml.math.impls.vector.SparseDistributedVector) RowColMatrixKey(org.apache.ignite.ml.math.distributed.keys.RowColMatrixKey) Collection(java.util.Collection) Ignite(org.apache.ignite.Ignite) CardinalityException(org.apache.ignite.ml.math.exceptions.CardinalityException) SparseDistributedVector(org.apache.ignite.ml.math.impls.vector.SparseDistributedVector) Vector(org.apache.ignite.ml.math.Vector)

Aggregations

Collection (java.util.Collection)1 Ignite (org.apache.ignite.Ignite)1 ClusterNode (org.apache.ignite.cluster.ClusterNode)1 Vector (org.apache.ignite.ml.math.Vector)1 RowColMatrixKey (org.apache.ignite.ml.math.distributed.keys.RowColMatrixKey)1 CardinalityException (org.apache.ignite.ml.math.exceptions.CardinalityException)1 SparseDistributedVectorStorage (org.apache.ignite.ml.math.impls.storage.vector.SparseDistributedVectorStorage)1 SparseDistributedVector (org.apache.ignite.ml.math.impls.vector.SparseDistributedVector)1