diff --git a/src/main/java/org/apache/sysds/hops/estim/EstimatorBasicAvg.java b/src/main/java/org/apache/sysds/hops/estim/EstimatorBasicAvg.java index 4b30e793c6d..76915fccb83 100644 --- a/src/main/java/org/apache/sysds/hops/estim/EstimatorBasicAvg.java +++ b/src/main/java/org/apache/sysds/hops/estim/EstimatorBasicAvg.java @@ -57,6 +57,26 @@ public double estim(MatrixBlock m, OpCode op) { return estimIntern(m.getDataCharacteristics(), null, op).getSparsity(); } + protected void obtainSparsityInformation(MMNode node) { + MatrixBlock mb = node.getData(); + if(mb != null && mb.getDataCharacteristics().getSparsity() == -1) { + if(mb.getNonZeros() != 0) { + mb.getDataCharacteristics().setNonZeros(mb.getNonZeros()); + } + else { + mb.recomputeNonZeros(); + mb.getDataCharacteristics().setNonZeros(mb.getNonZeros()); + } + } + } + + @Override + public MMNode getInputMMNode(MatrixBlock mb) { + MMNode node = new MMNode(mb); + obtainSparsityInformation(node); + return node; + } + private DataCharacteristics estimIntern(DataCharacteristics dc1, DataCharacteristics dc2, OpCode op) { switch (op) { case MM: diff --git a/src/main/java/org/apache/sysds/hops/estim/EstimatorBasicWorst.java b/src/main/java/org/apache/sysds/hops/estim/EstimatorBasicWorst.java index af7ed987189..791afde63ea 100644 --- a/src/main/java/org/apache/sysds/hops/estim/EstimatorBasicWorst.java +++ b/src/main/java/org/apache/sysds/hops/estim/EstimatorBasicWorst.java @@ -65,6 +65,26 @@ public double estim(MatrixBlock m, OpCode op) { return estimIntern(m.getDataCharacteristics(), null, op).getSparsity(); } + protected void obtainSparsityInformation(MMNode node) { + MatrixBlock mb = node.getData(); + if(mb != null && mb.getDataCharacteristics().getSparsity() == -1) { + if(mb.getNonZeros() != 0) { + mb.getDataCharacteristics().setNonZeros(mb.getNonZeros()); + } + else { + mb.recomputeNonZeros(); + mb.getDataCharacteristics().setNonZeros(mb.getNonZeros()); + } + } + } + + @Override + public MMNode getInputMMNode(MatrixBlock mb) { + MMNode node = new MMNode(mb); + obtainSparsityInformation(node); + return node; + } + private DataCharacteristics estimIntern(DataCharacteristics mc1, DataCharacteristics mc2, OpCode op) { switch (op) { case MM: diff --git a/src/main/java/org/apache/sysds/hops/estim/EstimatorBitsetMM.java b/src/main/java/org/apache/sysds/hops/estim/EstimatorBitsetMM.java index ec257553e7c..8e86cdec272 100644 --- a/src/main/java/org/apache/sysds/hops/estim/EstimatorBitsetMM.java +++ b/src/main/java/org/apache/sysds/hops/estim/EstimatorBitsetMM.java @@ -93,7 +93,14 @@ public double estim(MatrixBlock m, OpCode op) { return OptimizerUtils.getSparsity(outMap.getNumRows(), outMap.getNumColumns(), outMap.getNonZeros()); } - + + @Override + public MMNode getInputMMNode(MatrixBlock mb) { + MMNode node = new MMNode(mb); + getCachedSynopsis(node); + return node; + } + private BitsetMatrix getCachedSynopsis(MMNode node) { if( node == null ) return null; @@ -104,7 +111,7 @@ else if( !node.isLeaf() ) estim(node); //recursively obtain synopsis return (BitsetMatrix) node.getSynopsis(); } - + private static BitsetMatrix estimInternal(BitsetMatrix m1Map, BitsetMatrix m2Map, OpCode op) { switch(op) { case MM: return m1Map.matMult(m2Map); diff --git a/src/main/java/org/apache/sysds/hops/estim/EstimatorDensityMap.java b/src/main/java/org/apache/sysds/hops/estim/EstimatorDensityMap.java index 002ab88c0eb..0669ce6c939 100644 --- a/src/main/java/org/apache/sysds/hops/estim/EstimatorDensityMap.java +++ b/src/main/java/org/apache/sysds/hops/estim/EstimatorDensityMap.java @@ -88,7 +88,14 @@ public double estim(MatrixBlock m1, MatrixBlock m2, OpCode op) { public double estim(MatrixBlock m, OpCode op) { return estim(m, null, op); } - + + @Override + public MMNode getInputMMNode(MatrixBlock mb) { + MMNode node = new MMNode(mb); + getCachedSynopsis(node); + return node; + } + private DensityMap getCachedSynopsis(MMNode node) { if( node == null ) return null; diff --git a/src/main/java/org/apache/sysds/hops/estim/EstimatorLayeredGraph.java b/src/main/java/org/apache/sysds/hops/estim/EstimatorLayeredGraph.java index b5dd8954dd1..4e171efa6f7 100644 --- a/src/main/java/org/apache/sysds/hops/estim/EstimatorLayeredGraph.java +++ b/src/main/java/org/apache/sysds/hops/estim/EstimatorLayeredGraph.java @@ -115,6 +115,11 @@ public double estim(MatrixBlock m1, MatrixBlock m2) { m1.getNumRows(), m2.getNumColumns(), graph.estimateNnz()); } + @Override + public MMNode getInputMMNode(MatrixBlock mb) { + throw new UnsupportedOperationException("Not implemented yet."); + } + private static LayeredGraph estimInternal(LayeredGraph lg1, LayeredGraph lg2, OpCode op) { switch(op) { case MM: return lg1.matMult(lg2); diff --git a/src/main/java/org/apache/sysds/hops/estim/EstimatorMatrixHistogram.java b/src/main/java/org/apache/sysds/hops/estim/EstimatorMatrixHistogram.java index f545d0d06ed..83f568c21b3 100644 --- a/src/main/java/org/apache/sysds/hops/estim/EstimatorMatrixHistogram.java +++ b/src/main/java/org/apache/sysds/hops/estim/EstimatorMatrixHistogram.java @@ -105,7 +105,14 @@ public double estim(MatrixBlock m1, OpCode op) { MatrixHistogram h1 = new MatrixHistogram(m1, _useExtended); return estimIntern(h1, null, op, null); } - + + @Override + public MMNode getInputMMNode(MatrixBlock mb) { + MMNode node = new MMNode(mb); + getCachedSynopsis(node); + return node; + } + private MatrixHistogram getCachedSynopsis(MMNode node) { if( node == null ) return null; @@ -116,7 +123,7 @@ else if( !node.isLeaf() ) estim(node, false); //recursively obtain synopsis return (MatrixHistogram) node.getSynopsis(); } - + public double estimIntern(MatrixHistogram h1, MatrixHistogram h2, OpCode op, long[] misc) { double msize = (double)h1.getRows()*h1.getCols(); switch (op) { @@ -314,6 +321,7 @@ else if( !in.isEmpty() ) { } } } + in.setNonZeros(IntStream.of(cNnz).sum()); } public MatrixHistogram(int[] r, int[] r1e, int[] c, int[] c1e, int rmax, int cmax) { diff --git a/src/main/java/org/apache/sysds/hops/estim/EstimatorRowWise.java b/src/main/java/org/apache/sysds/hops/estim/EstimatorRowWise.java index 0cee03c8dfc..9ab39408322 100644 --- a/src/main/java/org/apache/sysds/hops/estim/EstimatorRowWise.java +++ b/src/main/java/org/apache/sysds/hops/estim/EstimatorRowWise.java @@ -72,6 +72,13 @@ public double estim(MatrixBlock m1, OpCode op) { return DoubleStream.of(rsOut).average().orElse(0); } + @Override + public MMNode getInputMMNode(MatrixBlock mb) { + MMNode node = new MMNode(mb); + throw new UnsupportedOperationException("TODO: Implement getCachedSynopsis similarly to EstimatorMatrixHistogram."); + // return node; + } + private double[] estimInternChain(MMNode node) { return estimInternChain(node, null, null); } diff --git a/src/main/java/org/apache/sysds/hops/estim/EstimatorSample.java b/src/main/java/org/apache/sysds/hops/estim/EstimatorSample.java index ec269cd97f3..c86c80cd801 100644 --- a/src/main/java/org/apache/sysds/hops/estim/EstimatorSample.java +++ b/src/main/java/org/apache/sysds/hops/estim/EstimatorSample.java @@ -197,7 +197,12 @@ private static int[] computeColumnNnz(MatrixBlock in, int[] ix) { ret[i] = nnz[ix[i]]; return ret; } - + + @Override + public MMNode getInputMMNode(MatrixBlock mb) { + throw new UnsupportedOperationException("Not implemented yet."); + } + private static int[] computeRowNnz(MatrixBlock in, int[] ix) { //copy nnz into reduced vector int[] ret = new int[ix.length]; diff --git a/src/main/java/org/apache/sysds/hops/estim/EstimatorSampleRa.java b/src/main/java/org/apache/sysds/hops/estim/EstimatorSampleRa.java index 86b0c35e912..194d492d467 100644 --- a/src/main/java/org/apache/sysds/hops/estim/EstimatorSampleRa.java +++ b/src/main/java/org/apache/sysds/hops/estim/EstimatorSampleRa.java @@ -127,7 +127,12 @@ public double estim(MatrixBlock m1, MatrixBlock m2) { return OptimizerUtils.getSparsity( m1.getNumRows(), m2.getNumColumns(), nnz); } - + + @Override + public MMNode getInputMMNode(MatrixBlock mb) { + throw new UnsupportedOperationException("Not implemented yet."); + } + private void initHashArrays(int m, int n, int l) { if( h1 == null ) { h1 = new double[m]; diff --git a/src/main/java/org/apache/sysds/hops/estim/SparsityEstimator.java b/src/main/java/org/apache/sysds/hops/estim/SparsityEstimator.java index edc6f13cafa..f527b127422 100644 --- a/src/main/java/org/apache/sysds/hops/estim/SparsityEstimator.java +++ b/src/main/java/org/apache/sysds/hops/estim/SparsityEstimator.java @@ -85,7 +85,9 @@ public static enum OpCode { * @return sparsity */ public abstract double estim(MatrixBlock m, OpCode op); - + + public abstract MMNode getInputMMNode(MatrixBlock mb); + protected boolean isExactMetadataOp(OpCode op) { return ArrayUtils.contains(EXACT_META_DATA_OPS, op); } diff --git a/src/main/java/org/apache/sysds/hops/recompile/Recompiler.java b/src/main/java/org/apache/sysds/hops/recompile/Recompiler.java index 0aef8a1583d..53809afab4f 100644 --- a/src/main/java/org/apache/sysds/hops/recompile/Recompiler.java +++ b/src/main/java/org/apache/sysds/hops/recompile/Recompiler.java @@ -46,6 +46,7 @@ import org.apache.sysds.common.Types.ValueType; import org.apache.sysds.conf.CompilerConfig.ConfigType; import org.apache.sysds.conf.ConfigurationManager; +import org.apache.sysds.conf.DMLConfig; import org.apache.sysds.hops.DataGenOp; import org.apache.sysds.hops.DataOp; import org.apache.sysds.hops.FunctionOp; @@ -61,6 +62,7 @@ import org.apache.sysds.hops.codegen.SpoofCompiler; import org.apache.sysds.hops.rewrite.HopRewriteUtils; import org.apache.sysds.hops.rewrite.ProgramRewriter; +import org.apache.sysds.hops.rewrite.SparsityOptimizer; import org.apache.sysds.lops.Lop; import org.apache.sysds.lops.compile.Dag; import org.apache.sysds.lops.rewrite.LopRewriter; @@ -337,7 +339,7 @@ else if( !codegen ) { for( Hop hopRoot : hops ) rClearLops( hopRoot ); } - + // get max parallelism constraint, see below Hop.resetVisitStatus(hops); int maxK = rGetMaxParallelism(hops); @@ -396,7 +398,14 @@ else if( !codegen ) { hops = SpoofCompiler.optimize(hops, (status==null || !status.isInitialCodegen())); } - + + if(ConfigurationManager.getDMLConfig().getBooleanValue(DMLConfig.SPARSITY_RECOMPILE)) { + Hop.resetVisitStatus(hops); + boolean executed = SparsityOptimizer.optimizeAndExecute(hops, ec); + if(executed) + return null; + } + // set max parallelism constraint to ensure compilation // incl rewrites does not lose these hop-lop constraints Hop.resetVisitStatus(hops); diff --git a/src/main/java/org/apache/sysds/hops/rewrite/ProgramRewriter.java b/src/main/java/org/apache/sysds/hops/rewrite/ProgramRewriter.java index 849f2d91e80..178a3ecf238 100644 --- a/src/main/java/org/apache/sysds/hops/rewrite/ProgramRewriter.java +++ b/src/main/java/org/apache/sysds/hops/rewrite/ProgramRewriter.java @@ -149,9 +149,6 @@ public ProgramRewriter(boolean staticRewrites, boolean dynamicRewrites) if(ConfigurationManager.getDMLConfig().getBooleanValue(DMLConfig.SPARSITY_REWRITES)) { _dagRuleSet.add( new RewriteMatrixMultChainOptimizationSparse() ); } - if( ConfigurationManager.getDMLConfig().getBooleanValue(DMLConfig.SPARSITY_RECOMPILE) ) { - _dagRuleSet.add(new SparsityOptimizer()); - } if( OptimizerUtils.ALLOW_ALGEBRAIC_SIMPLIFICATION ) { _dagRuleSet.add( new RewriteAlgebraicSimplificationDynamic() ); //dependencies: cse _dagRuleSet.add( new RewriteAlgebraicSimplificationStatic() ); //dependencies: cse diff --git a/src/main/java/org/apache/sysds/hops/rewrite/SparsityOptimizer.java b/src/main/java/org/apache/sysds/hops/rewrite/SparsityOptimizer.java index 36b84b9cf9f..76eda7473e0 100644 --- a/src/main/java/org/apache/sysds/hops/rewrite/SparsityOptimizer.java +++ b/src/main/java/org/apache/sysds/hops/rewrite/SparsityOptimizer.java @@ -20,36 +20,62 @@ package org.apache.sysds.hops.rewrite; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; +import org.apache.commons.lang3.mutable.MutableInt; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.sysds.common.Types.OpOpData; +import org.apache.sysds.conf.ConfigurationManager; +import org.apache.sysds.conf.DMLConfig; import org.apache.sysds.hops.AggBinaryOp; import org.apache.sysds.hops.DataOp; import org.apache.sysds.hops.Hop; +import org.apache.sysds.hops.HopsException; +import org.apache.sysds.hops.OptimizerUtils; +import org.apache.sysds.hops.estim.MMNode; +import org.apache.sysds.hops.estim.SparsityEstimator; +import org.apache.sysds.hops.estim.SparsityEstimator.OpCode; +import org.apache.sysds.hops.estim.EstimationUtils.EstimatorType; +import org.apache.sysds.runtime.controlprogram.caching.MatrixObject; +import org.apache.sysds.runtime.controlprogram.context.ExecutionContext; import org.apache.sysds.utils.Explain; -public class SparsityOptimizer extends HopRewriteRule { +public class SparsityOptimizer { protected static final Log LOG = LogFactory.getLog(SparsityOptimizer.class.getName()); - @Override - public ArrayList rewriteHopDAGs(ArrayList roots, ProgramRewriteStatus state) { - ArrayList ret = new ArrayList(); - for(Hop rootHop : roots) - ret.add(rewriteHopDAG(rootHop, state)); - return ret; + private SparsityEstimator _estim; + + public SparsityOptimizer() { + _estim = EstimatorType.valueOf(ConfigurationManager.getDMLConfig() + .getTextValue(DMLConfig.SPARSITY_ESTIMATOR)).getEstimator(); } - @Override - public Hop rewriteHopDAG(Hop root, ProgramRewriteStatus state) { - if(root.isVisited()) - return root; + public static boolean optimizeAndExecute(ArrayList roots, ExecutionContext ec) { + SparsityOptimizer spOpt = new SparsityOptimizer(); + roots = spOpt.optimizeHops(roots, ec); + // boolean wasExecuted = execute(); + return false; + } - if(root instanceof DataOp && ((DataOp)root).isWrite() && root.getInput().size() > 0 && - HopRewriteUtils.isMatrixMultiply(root.getInput(0)) && isMMChain(root.getInput())) - return optimizeMMChain(root); - return root; + public ArrayList optimizeHops(ArrayList roots, ExecutionContext ec) { + ArrayList ret = new ArrayList(); + for(Hop rootHop : roots) { + if(rootHop.isVisited()) { + ret.add(rootHop); + continue; + } + if(rootHop instanceof DataOp && ((DataOp)rootHop).isWrite() && rootHop.getInput().size() > 0 && + HopRewriteUtils.isMatrixMultiply(rootHop.getInput(0)) && isMMChain(rootHop.getInput())) { + ret.add(optimizeMMChain(rootHop, ec)); + } + else { + ret.add(rootHop); + } + rootHop.setVisited(); + } + return ret; } private static boolean isMMChain(List hopList) { @@ -68,13 +94,13 @@ else if(hop instanceof DataOp) return false; } - public Hop optimizeMMChain(Hop rootHop) { + public Hop optimizeMMChain(Hop rootHop, ExecutionContext ec) { List mmChain = new ArrayList<>(); List mmOperators = new ArrayList<>(); Hop mmHop = rootHop.getInput(0); prepareMMChain(mmHop, mmChain, mmOperators); - Hop newMMHop = sculptMMChain(mmHop, mmChain, mmOperators); + Hop newMMHop = sculptMMChain(mmHop, mmChain, mmOperators, ec); if(newMMHop != null) { HopRewriteUtils.replaceChildReference(rootHop, mmHop, newMMHop); mmHop = newMMHop; @@ -105,7 +131,7 @@ public void prepareMMChain(Hop root, List mmChain, List mmOperators) { Hop hop = mmChain.get(counter); /* - * Check if mmChain[i] is expandable: + * Check if mmChain[i] is expandable: * 1) It must be MATMULT * 2) It must not have been visited already * (one MATMULT should get expanded only in one chain) @@ -131,14 +157,235 @@ public void prepareMMChain(Hop root, List mmChain, List mmOperators) { } } + /** + * Obtains all dimension information of the chain and constructs the dimArray. + * If all dimensions are known it returns true; othrewise the mmchain rewrite + * should be ended without modifications. + * + * @param hop high-level operator + * @param chain list of high-level operators + * @param dimsArray dimension array + * @return true if all dimensions known + */ + protected static boolean getDimsArray(Hop hop, List chain, double[] dimsArray) { + boolean dimsKnown = true; + + // Build the array containing dimensions from all matrices in the chain + // check the dimensions in the matrix chain to insure all dimensions are known + for(int i=0; i< chain.size(); i++) + if(chain.get(i).getDim1() <= 0 || chain.get(i).getDim2() <= 0) + dimsKnown = false; + + if(dimsKnown) { //populate dims array if all dims known + for(int i = 0; i < chain.size(); i++) { + if(i == 0) { + dimsArray[i] = chain.get(i).getDim1(); + if(dimsArray[i] <= 0) { + throw new HopsException(hop.printErrorLocation() + + "Hops::optimizeMMChain() : Invalid Matrix Dimension: "+ dimsArray[i]); + } + } + else if(chain.get(i - 1).getDim2() != chain.get(i).getDim1()) { + throw new HopsException(hop.printErrorLocation() + + "Hops::optimizeMMChain() : Matrix Dimension Mismatch: " + + chain.get(i - 1).getDim2()+" != "+chain.get(i).getDim1()); + } + + dimsArray[i + 1] = chain.get(i).getDim2(); + if(dimsArray[i + 1] <= 0) { + throw new HopsException(hop.printErrorLocation() + + "Hops::optimizeMMChain() : Invalid Matrix Dimension: " + dimsArray[i + 1]); + } + } + } + return dimsKnown; + } + + protected static void clearLinksWithinChain(Hop hop, List operators) { + for(int i=0; i < operators.size(); i++) { + Hop op = operators.get(i); + if(op.getInput().size() != 2 || (i != 0 && op.getParent().size() > 1)) { + throw new HopsException(hop.printErrorLocation() + + "Unexpected error while applying optimization on matrix-mult chain.\n"); + } + Hop input1 = op.getInput().get(0); + Hop input2 = op.getInput().get(1); + + op.getInput().clear(); + input1.getParent().remove(op); + input2.getParent().remove(op); + } + } + + /** + * mmChainRelinkHops(): This method gets invoked after finding the optimal + * order (split[][]) from dynamic programming. It relinks the Hops that are + * part of the mmChain. + * @param mmChain : basic operands in the entire matrix multiplication chain. + * @param mmOperators : Hops that store the intermediate results in the chain. + * For example: A = B %*% (C %*% D) there will be three Hops in + * mmChain (B,C,D), and two Hops in mmOperators (one for each * %*%). + * @param h high level operator + * @param i array index i + * @param j array index j + * @param opIndex operator index + * @param split optimal order + * @param level log level + */ + protected final void mmChainRelinkHops(Hop h, int i, int j, List mmChain, + List mmOperators, MutableInt opIndex, int[][] split, int level) { + //NOTE: the opIndex is a MutableInt in order to get the correct positions + //in ragged chains like ((((a, b), c), (D, E), f), e) that might be given + //like that by the original scripts variable assignments + + //single matrix - end of recursion + if(i == j) { + logTraceHop(h, level); + return; + } + + if(LOG.isTraceEnabled()) { + String offset = Explain.getIdentation(level); + LOG.trace(offset + "("); + } + + // Set Input1 for current Hop h + if(i == split[i][j]) { + h.getInput().add(mmChain.get(i)); + mmChain.get(i).getParent().add(h); + } + else { + int ix = opIndex.getValue(); + opIndex.increment(); + h.getInput().add(mmOperators.get(ix)); + mmOperators.get(ix).getParent().add(h); + } + + // Set Input2 for current Hop h + if(split[i][j] + 1 == j) { + h.getInput().add(mmChain.get(j)); + mmChain.get(j).getParent().add(h); + } + else { + int ix = opIndex.getValue(); + opIndex.increment(); + h.getInput().add(mmOperators.get(ix)); + mmOperators.get(ix).getParent().add(h); + } + + // Find children for both the inputs + mmChainRelinkHops(h.getInput().get(0), i, split[i][j], mmChain, mmOperators, opIndex, split, level+1); + mmChainRelinkHops(h.getInput().get(1), split[i][j] + 1, j, mmChain, mmOperators, opIndex, split, level+1); + + // Propagate properties of input hops to current hop h + h.refreshSizeInformation(); + + if(LOG.isTraceEnabled()){ + String offset = Explain.getIdentation(level); + LOG.trace(offset + ")"); + } + } + // Optimize the matrix multiplication chain by rewriting it // This method can modify the mm chain completely, even re-creating it from scratch is allowed. // NOTE: This method should return the new mm hop when re-creating it from scratch. - private Hop sculptMMChain(Hop mmHop, List mmChain, List mmOperators) { - (new RewriteMatrixMultChainOptimizationSparse()).optimizeMMChain(mmHop, mmChain, mmOperators, null); + private Hop sculptMMChain(Hop mmHop, List mmChain, List mmOperators, ExecutionContext ec) { + // Step 2: construct dims array and input matrices + double[] dimsArray = new double[mmChain.size() + 1]; + boolean dimsKnown = getDimsArray(mmHop, mmChain, dimsArray ); + MMNode[] sketchArray = new MMNode[mmChain.size() + 1]; + boolean inputMetaAvail = getInputMatrixCharacteristics(mmChain, sketchArray, ec); + if(dimsKnown && inputMetaAvail) { + // Step 3: clear the links among Hops within the identified chain + clearLinksWithinChain(mmHop, mmOperators); + + // Step 4: Find the optimal ordering via dynamic programming. + int size = mmChain.size(); + int[][] split = mmChainDPSparse(dimsArray, sketchArray, mmChain.size()); + + // Step 5: Relink the hops using the optimal ordering (split[][]) found from DP. + LOG.trace("Optimal Sparse MM Chain:"); + mmChainRelinkHops(mmOperators.get(0), 0, size - 1, mmChain, mmOperators, new MutableInt(1), split, 1); + } return null; } + private boolean getInputMatrixCharacteristics(List chain, MMNode[] sketchArray, ExecutionContext ec) { + boolean inputMetaAvail = true; + + for(int counter = 0; counter < chain.size(); counter++) { + Hop currentHop = chain.get(counter); + inputMetaAvail &= currentHop.isMatrix(); + inputMetaAvail &= !currentHop.isFederated(); + // inputMetaAvail &= (currentHop.getNnz() != -1); + inputMetaAvail &= ec.containsVariable(currentHop.getName()); + if(inputMetaAvail) { + MatrixObject mo = ec.getMatrixObject(currentHop.getName()); + sketchArray[counter] =_estim.getInputMMNode(mo.acquireReadAndRelease()); + } + else + break; + } + + return inputMetaAvail; + } + + /** + * mmChainDP(): Core method to perform dynamic programming on a given array + * of matrix dimensions. + * + * Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, Clifford Stein + * Introduction to Algorithms, Third Edition, MIT Press, page 395. + */ + private static int[][] mmChainDPSparse(double[] dimArray, MMNode[] sketchArray, int size) { + double[][] dpMatrix = new double[size][size]; //min cost table + MMNode[][] dpMatrixS = new MMNode[size][size]; //min sketch table + int[][] split = new int[size][size]; //min cost index table + + //init minimum costs for chains of length 1 + for( int i = 0; i < size; i++ ) { + Arrays.fill(dpMatrix[i], 0); + Arrays.fill(split[i], -1); + dpMatrixS[i][i] = sketchArray[i]; + } + + //compute cost-optimal chains for increasing chain sizes + SparsityEstimator estim = EstimatorType.valueOf(ConfigurationManager.getDMLConfig() + .getTextValue(DMLConfig.SPARSITY_ESTIMATOR)).getEstimator(); + for(int l = 2; l <= size; l++) { // chain length + for(int i = 0; i < size - l + 1; i++) { + int j = i + l - 1; + // find cost of (i,j) + dpMatrix[i][j] = Double.MAX_VALUE; + for( int k = i; k <= j - 1; k++ ) + { + //construct estimation nodes (w/ lazy propagation and memoization) + MMNode tmp = new MMNode(dpMatrixS[i][k], dpMatrixS[k+1][j], OpCode.MM); + estim.estim(tmp); + + // recursive cost computation + double cost = dpMatrix[i][k] + dpMatrix[k + 1][j] + + OptimizerUtils.getSparsity(tmp.getLeft().getDataCharacteristics()) * + OptimizerUtils.getSparsity(tmp.getRight().getDataCharacteristics()) * + tmp.getLeft().getRows() * tmp.getLeft().getCols() * tmp.getRight().getCols(); + + //prune suboptimal + if( cost < dpMatrix[i][j] ) { + dpMatrix[i][j] = cost; + dpMatrixS[i][j] = tmp; + split[i][j] = k; + } + } + + if(LOG.isTraceEnabled()) + LOG.trace("mmchainoptsparse [i=" + (i + 1) + ",j=" + (j + 1) + "]: costs = " + dpMatrix[i][j] + + ", split = " + (split[i][j] + 1)); + } + } + + return split; + } + protected void logTraceHop(Hop hop, int level) { if(LOG.isTraceEnabled()) { String offset = Explain.getIdentation(level);