Repository: ignite Updated Branches: refs/heads/master 6f94659b5 -> cc6975dab
IGNITE-6805: Fixed bugs in BlockMatrixStorage. This closes #2965 Project: http://git-wip-us.apache.org/repos/asf/ignite/repo Commit: http://git-wip-us.apache.org/repos/asf/ignite/commit/cc6975da Tree: http://git-wip-us.apache.org/repos/asf/ignite/tree/cc6975da Diff: http://git-wip-us.apache.org/repos/asf/ignite/diff/cc6975da Branch: refs/heads/master Commit: cc6975dabf3769d8852005185e4eff3e2d5d0983 Parents: 6f94659 Author: Yury Babak <[email protected]> Authored: Tue Nov 7 12:02:43 2017 +0300 Committer: Igor Sapego <[email protected]> Committed: Tue Nov 7 12:04:31 2017 +0300 ---------------------------------------------------------------------- .../storage/matrix/BlockMatrixStorage.java | 86 +++++++++++--------- .../SparseDistributedBlockMatrixTest.java | 37 +++++++-- 2 files changed, 78 insertions(+), 45 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/ignite/blob/cc6975da/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/storage/matrix/BlockMatrixStorage.java ---------------------------------------------------------------------- diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/storage/matrix/BlockMatrixStorage.java b/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/storage/matrix/BlockMatrixStorage.java index 2fb4b30..0d5cf0a 100644 --- a/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/storage/matrix/BlockMatrixStorage.java +++ b/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/storage/matrix/BlockMatrixStorage.java @@ -225,7 +225,9 @@ public class BlockMatrixStorage extends CacheUtils implements MatrixStorage, Sto /** {@inheritDoc} */ @Override public Set<BlockMatrixKey> getAllKeys() { - IgnitePair<Long> maxBlockId = getBlockId(rows, cols); + int maxRowIdx = rows - 1; + int maxColIdx = cols - 1; + IgnitePair<Long> maxBlockId = getBlockId(maxRowIdx, maxColIdx); Set<BlockMatrixKey> keyset = new HashSet<>(); @@ -251,7 +253,8 @@ public class BlockMatrixStorage extends CacheUtils implements MatrixStorage, Sto List<BlockEntry> res = new LinkedList<>(); for (int i = 0; i < blocksInCol; i++) - res.add(getEntryById(new IgnitePair<>((long) i, blockId.get2()))); + res.add(getEntryById(new IgnitePair<>(blockId.get1(), (long) i))); + return res; } @@ -266,16 +269,11 @@ public class BlockMatrixStorage extends CacheUtils implements MatrixStorage, Sto List<BlockEntry> res = new LinkedList<>(); for (int i = 0; i < blocksInRow; i++) - res.add(getEntryById(new IgnitePair<>(blockId.get1(), (long) i))); + res.add(getEntryById(new IgnitePair<>((long) i, blockId.get2()))); return res; } - /** */ - public IgnitePair<Long> getBlockId(int x, int y) { - return new IgnitePair<>((long)x / maxBlockEdge, (long)y / maxBlockEdge); - } - /** {@inheritDoc} */ @Override public int hashCode() { int res = blocksInCol; @@ -306,22 +304,48 @@ public class BlockMatrixStorage extends CacheUtils implements MatrixStorage, Sto } /** - * + * Returns cached or new BlockEntry by given blockId + * @param blockId blockId + * @return BlockEntry */ private BlockEntry getEntryById(IgnitePair<Long> blockId) { BlockMatrixKey key = getCacheKey(blockId.get1(), blockId.get2()); - BlockEntry entry = cache.localPeek(key); + BlockEntry entry = cache.localPeek(key, CachePeekMode.PRIMARY); entry = entry != null ? entry : cache.get(key); - if (entry == null) { + if (entry == null) + entry = getEmptyBlockEntry(blockId); - int colSize = blockId.get1() == (blocksInCol - 1) ? rows % maxBlockEdge : maxBlockEdge; - int rowSize = blockId.get2() == (blocksInRow -1 ) ? cols % maxBlockEdge : maxBlockEdge; + return entry; + } + + /** + * Builds empty BlockEntry with sizes based on blockId and BlockMatrixStorage fields' values + * @param blockId blockId + * @return Empty BlockEntry + */ + private BlockEntry getEmptyBlockEntry(IgnitePair<Long> blockId) { + BlockEntry entry; + int rowMod = rows % maxBlockEdge; + int colMod = cols % maxBlockEdge; + + int rowSize; + + if(rowMod == 0) + rowSize = maxBlockEdge; + else + rowSize = blockId.get1() != (blocksInRow - 1) ? maxBlockEdge : rowMod; - entry = new BlockEntry(rowSize, colSize); - } + int colSize; + + if(colMod == 0) + colSize = maxBlockEdge; + else + colSize = blockId.get2() != (blocksInCol - 1) ? maxBlockEdge : colMod; + + entry = new BlockEntry(rowSize, colSize); return entry; } @@ -350,13 +374,7 @@ public class BlockMatrixStorage extends CacheUtils implements MatrixStorage, Sto BlockMatrixKey key = getCacheKey(blockId.get1(), blockId.get2()); // Local get. - BlockEntry block = cache.localPeek(key, CachePeekMode.PRIMARY); - - if (block == null) - block = cache.get(key); //Remote entry get. - - if (block == null) - block = initBlockFor(a, b); + BlockEntry block = getEntryById(blockId); block.set(a % block.rowSize(), b % block.columnSize(), v); @@ -365,22 +383,14 @@ public class BlockMatrixStorage extends CacheUtils implements MatrixStorage, Sto }); } - /** */ - private BlockEntry initBlockFor(int x, int y) { - int blockRows = 0; - int blockCols = 0; - - if(rows >= maxBlockEdge) - blockRows = rows - x >= maxBlockEdge ? maxBlockEdge : rows - x; - else - blockRows = rows; - - if(cols >= maxBlockEdge) - blockCols = cols - y >= maxBlockEdge ? maxBlockEdge : cols - y; - else - blockCols = cols; - - return new BlockEntry(blockRows, blockCols); + /** + * Calculates blockId for given cell's coordinates + * @param x x1 attribute in (x1,x2) coordinates + * @param y x2 attribute in (x1, x2) coordinates + * @return blockId as an IgnitePair + */ + private IgnitePair<Long> getBlockId(int x, int y) { + return new IgnitePair<>((long)x / maxBlockEdge, (long)y / maxBlockEdge); } /** http://git-wip-us.apache.org/repos/asf/ignite/blob/cc6975da/modules/ml/src/test/java/org/apache/ignite/ml/math/impls/matrix/SparseDistributedBlockMatrixTest.java ---------------------------------------------------------------------- diff --git a/modules/ml/src/test/java/org/apache/ignite/ml/math/impls/matrix/SparseDistributedBlockMatrixTest.java b/modules/ml/src/test/java/org/apache/ignite/ml/math/impls/matrix/SparseDistributedBlockMatrixTest.java index 35329ae..2943bc0 100644 --- a/modules/ml/src/test/java/org/apache/ignite/ml/math/impls/matrix/SparseDistributedBlockMatrixTest.java +++ b/modules/ml/src/test/java/org/apache/ignite/ml/math/impls/matrix/SparseDistributedBlockMatrixTest.java @@ -216,12 +216,23 @@ public class SparseDistributedBlockMatrixTest extends GridCommonAbstractTest { fail("UnsupportedOperationException expected."); } - /** */ + /** Test cache behaviour for matrix with different blocks */ public void testCacheBehaviour(){ IgniteUtils.setCurrentIgniteName(ignite.configuration().getIgniteInstanceName()); + cacheBehaviorLogic(rows); + } + + /** Test cache behaviour for matrix with homogeneous blocks */ + public void testCacheBehaviourWithHomogeneousBlocks(){ + IgniteUtils.setCurrentIgniteName(ignite.configuration().getIgniteInstanceName()); + int size = BlockEntry.MAX_BLOCK_SIZE * 3; + cacheBehaviorLogic(size); + } - SparseBlockDistributedMatrix cacheMatrix1 = new SparseBlockDistributedMatrix(rows, cols); - SparseBlockDistributedMatrix cacheMatrix2 = new SparseBlockDistributedMatrix(rows, cols); + /** */ + private void cacheBehaviorLogic(int size) { + SparseBlockDistributedMatrix cacheMatrix1 = new SparseBlockDistributedMatrix(size, size); + SparseBlockDistributedMatrix cacheMatrix2 = new SparseBlockDistributedMatrix(size, size); initMtx(cacheMatrix1); initMtx(cacheMatrix2); @@ -277,9 +288,23 @@ public class SparseDistributedBlockMatrixTest extends GridCommonAbstractTest { * Simple test for two square matrices. */ public void testSquareMatrixTimes(){ - IgniteUtils.setCurrentIgniteName(ignite.configuration().getIgniteInstanceName()); + int size = rows; + + squareMatrixTimesLogic(size); + } + + /** + * Simple test for two square matrices with size which is proportional to MAX_BLOCK_SIZE constant + */ + public void testSquareMatrixTimesWithHomogeneousBlocks(){ + int size = BlockEntry.MAX_BLOCK_SIZE * 3; - int size = 100; + squareMatrixTimesLogic(size); + } + + /** Build two square matrices, multiply them and check main diagonal elements */ + private void squareMatrixTimesLogic(int size) { + IgniteUtils.setCurrentIgniteName(ignite.configuration().getIgniteInstanceName()); Matrix cacheMatrix1 = new SparseBlockDistributedMatrix(size, size); Matrix cacheMatrix2 = new SparseBlockDistributedMatrix(size, size); @@ -291,8 +316,6 @@ public class SparseDistributedBlockMatrixTest extends GridCommonAbstractTest { Matrix res = cacheMatrix1.times(cacheMatrix2); - BlockMatrixStorage storage = (BlockMatrixStorage)res.getStorage(); - for(int i = 0; i < size; i++) for(int j = 0; j < size; j++) if (i == j)
