Fix error handling ultra-sparse data generation (int overflow)

Project: http://git-wip-us.apache.org/repos/asf/incubator-systemml/repo
Commit: 
http://git-wip-us.apache.org/repos/asf/incubator-systemml/commit/10d1afc9
Tree: http://git-wip-us.apache.org/repos/asf/incubator-systemml/tree/10d1afc9
Diff: http://git-wip-us.apache.org/repos/asf/incubator-systemml/diff/10d1afc9

Branch: refs/heads/master
Commit: 10d1afc9dac5fe2c4615019e52113de8f9400754
Parents: 9cb7d55
Author: Matthias Boehm <[email protected]>
Authored: Fri Jan 22 20:54:05 2016 -0800
Committer: Matthias Boehm <[email protected]>
Committed: Sat Jan 23 16:08:07 2016 -0800

----------------------------------------------------------------------
 .../runtime/matrix/data/LibMatrixDatagen.java   | 27 +++++++++++---------
 1 file changed, 15 insertions(+), 12 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-systemml/blob/10d1afc9/src/main/java/org/apache/sysml/runtime/matrix/data/LibMatrixDatagen.java
----------------------------------------------------------------------
diff --git 
a/src/main/java/org/apache/sysml/runtime/matrix/data/LibMatrixDatagen.java 
b/src/main/java/org/apache/sysml/runtime/matrix/data/LibMatrixDatagen.java
index dcaaf1a..c190e50 100644
--- a/src/main/java/org/apache/sysml/runtime/matrix/data/LibMatrixDatagen.java
+++ b/src/main/java/org/apache/sysml/runtime/matrix/data/LibMatrixDatagen.java
@@ -127,19 +127,22 @@ public class LibMatrixDatagen
         * @return
         * @throws DMLRuntimeException
         */
-       public static long[] computeNNZperBlock(long nrow, long ncol, int 
brlen, int bclen, double sparsity) throws DMLRuntimeException {
-               int numBlocks = (int) (Math.ceil((double)nrow/brlen) * 
Math.ceil((double)ncol/bclen));
-               
-               // CURRENT: 
-               //              Total #of NNZ is set to the expected value 
(nrow*ncol*sparsity).
-               // TODO: 
-               //              Instead of using the expected value, one should 
actually 
-               //              treat NNZ as a random variable and accordingly 
generate a random value.
-               long nnz = (long) Math.ceil (nrow * (ncol*sparsity));
-
-               if ( numBlocks > Integer.MAX_VALUE ) {
-                       throw new DMLRuntimeException("A random matrix of size 
[" + nrow + "," + ncol + "] can not be created. Number of blocks (" +  
numBlocks + ") exceeds the maximum integer size. Try to increase the block 
size.");
+       public static long[] computeNNZperBlock(long nrow, long ncol, int 
brlen, int bclen, double sparsity) 
+               throws DMLRuntimeException 
+       {
+               long lnumBlocks = (long) (Math.ceil((double)nrow/brlen) * 
Math.ceil((double)ncol/bclen));
+               
+               //sanity check max number of blocks (before cast to avoid 
overflow)
+               if ( lnumBlocks > Integer.MAX_VALUE ) {
+                       throw new DMLRuntimeException("A random matrix of size 
[" + nrow + "," + ncol + "] can not be created. "
+                                       + "Number of blocks ("+lnumBlocks+") 
exceeds the maximum integer size. Try to increase the block size.");
                }
+
+               // NOTE: Total #of NNZ is set to the expected value 
(nrow*ncol*sparsity).
+               // TODO: Instead of using the expected value, NNZ should be 
random variable 
+               
+               int numBlocks = (int) lnumBlocks;
+               long nnz = (long) Math.ceil (nrow * (ncol*sparsity));
                
                // Compute block-level NNZ
                long[] ret  = new long[numBlocks];

Reply via email to