Search in sources :

Example 1 with VectorBlockEntry

use of org.apache.ignite.ml.math.impls.vector.VectorBlockEntry in project ignite by apache.

the class BlockVectorStorage method getEmptyBlockEntry.

/**
 * Get empty block entry by the given block id.
 */
@NotNull
private VectorBlockEntry getEmptyBlockEntry(long blockId) {
    VectorBlockEntry entry;
    int colMod = size % maxBlockEdge;
    int colSize;
    if (colMod == 0)
        colSize = maxBlockEdge;
    else
        colSize = blockId != (blocks - 1) ? maxBlockEdge : colMod;
    entry = new VectorBlockEntry(colSize);
    return entry;
}
Also used : VectorBlockEntry(org.apache.ignite.ml.math.impls.vector.VectorBlockEntry) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with VectorBlockEntry

use of org.apache.ignite.ml.math.impls.vector.VectorBlockEntry in project ignite by apache.

the class BlockVectorStorage method matrixGet.

/**
 * Distributed vector get.
 *
 * @param idx index.
 * @return Vector value at (idx) index.
 */
private double matrixGet(int idx) {
    // Remote get from the primary node (where given row or column is stored locally).
    return ignite().compute(getClusterGroupForGivenKey(CACHE_NAME, getBlockId(idx))).call(() -> {
        IgniteCache<VectorBlockKey, VectorBlockEntry> cache = Ignition.localIgnite().getOrCreateCache(CACHE_NAME);
        VectorBlockKey key = getCacheKey(getBlockId(idx));
        // Local get.
        VectorBlockEntry block = cache.localPeek(key, CachePeekMode.PRIMARY);
        if (block == null)
            block = cache.get(key);
        return block == null ? 0.0 : block.get(idx % block.size());
    });
}
Also used : VectorBlockKey(org.apache.ignite.ml.math.distributed.keys.impl.VectorBlockKey) VectorBlockEntry(org.apache.ignite.ml.math.impls.vector.VectorBlockEntry)

Example 3 with VectorBlockEntry

use of org.apache.ignite.ml.math.impls.vector.VectorBlockEntry in project ignite by apache.

the class CacheUtils method sparseMapForVector.

/**
 * @param vectorUuid Matrix UUID.
 * @param mapper Mapping {@link IgniteFunction}.
 */
@SuppressWarnings("unchecked")
public static <K, V> void sparseMapForVector(UUID vectorUuid, IgniteDoubleFunction<V> mapper, String cacheName) {
    A.notNull(vectorUuid, "vectorUuid");
    A.notNull(cacheName, "cacheName");
    A.notNull(mapper, "mapper");
    foreach(cacheName, (CacheEntry<K, V> ce) -> {
        K k = ce.entry().getKey();
        V v = ce.entry().getValue();
        if (v instanceof VectorBlockEntry) {
            VectorBlockEntry entry = (VectorBlockEntry) v;
            for (int i = 0; i < entry.size(); i++) entry.set(i, (Double) mapper.apply(entry.get(i)));
            ce.cache().put(k, (V) entry);
        } else {
            V mappingRes = mapper.apply((Double) v);
            ce.cache().put(k, mappingRes);
        }
    }, sparseKeyFilter(vectorUuid));
}
Also used : VectorBlockEntry(org.apache.ignite.ml.math.impls.vector.VectorBlockEntry)

Example 4 with VectorBlockEntry

use of org.apache.ignite.ml.math.impls.vector.VectorBlockEntry 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;
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) VectorBlockKey(org.apache.ignite.ml.math.distributed.keys.impl.VectorBlockKey) VectorBlockEntry(org.apache.ignite.ml.math.impls.vector.VectorBlockEntry) BlockVectorStorage(org.apache.ignite.ml.math.impls.storage.matrix.BlockVectorStorage) IgnitePair(org.apache.ignite.internal.util.lang.IgnitePair) Collection(java.util.Collection) SparseBlockDistributedVector(org.apache.ignite.ml.math.impls.vector.SparseBlockDistributedVector) Ignite(org.apache.ignite.Ignite) CardinalityException(org.apache.ignite.ml.math.exceptions.CardinalityException)

Example 5 with VectorBlockEntry

use of org.apache.ignite.ml.math.impls.vector.VectorBlockEntry in project ignite by apache.

the class BlockVectorStorage method matrixSet.

/**
 * Distributed matrix set.
 *
 * @param idx Row or column index.
 * @param v New value to set.
 */
private void matrixSet(int idx, double v) {
    long blockId = getBlockId(idx);
    // Remote set on the primary node (where given row or column is stored locally).
    ignite().compute(getClusterGroupForGivenKey(CACHE_NAME, blockId)).run(() -> {
        IgniteCache<VectorBlockKey, VectorBlockEntry> cache = Ignition.localIgnite().getOrCreateCache(CACHE_NAME);
        VectorBlockKey key = getCacheKey(blockId);
        // Local get.
        VectorBlockEntry block = getEntryById(blockId);
        block.set(idx % block.size(), v);
        // Local put.
        cache.put(key, block);
    });
}
Also used : VectorBlockKey(org.apache.ignite.ml.math.distributed.keys.impl.VectorBlockKey) VectorBlockEntry(org.apache.ignite.ml.math.impls.vector.VectorBlockEntry)

Aggregations

VectorBlockEntry (org.apache.ignite.ml.math.impls.vector.VectorBlockEntry)6 VectorBlockKey (org.apache.ignite.ml.math.distributed.keys.impl.VectorBlockKey)4 Collection (java.util.Collection)1 Ignite (org.apache.ignite.Ignite)1 ClusterNode (org.apache.ignite.cluster.ClusterNode)1 IgnitePair (org.apache.ignite.internal.util.lang.IgnitePair)1 CardinalityException (org.apache.ignite.ml.math.exceptions.CardinalityException)1 BlockVectorStorage (org.apache.ignite.ml.math.impls.storage.matrix.BlockVectorStorage)1 SparseBlockDistributedVector (org.apache.ignite.ml.math.impls.vector.SparseBlockDistributedVector)1 NotNull (org.jetbrains.annotations.NotNull)1