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 76915fccb83..6a350ce011b 100644 --- a/src/main/java/org/apache/sysds/hops/estim/EstimatorBasicAvg.java +++ b/src/main/java/org/apache/sysds/hops/estim/EstimatorBasicAvg.java @@ -22,8 +22,6 @@ import org.apache.commons.lang3.NotImplementedException; import org.apache.sysds.hops.OptimizerUtils; import org.apache.sysds.runtime.matrix.data.MatrixBlock; -import org.apache.sysds.runtime.meta.DataCharacteristics; -import org.apache.sysds.runtime.meta.MatrixCharacteristics; /** * Basic average case estimator for matrix sparsity: @@ -32,14 +30,18 @@ public class EstimatorBasicAvg extends SparsityEstimator { @Override - public DataCharacteristics estim(MMNode root) { - DataCharacteristics dc1 = !root.getLeft().isLeaf() ? - estim(root.getLeft()) : root.getLeft().getDataCharacteristics(); - DataCharacteristics dc2 = root.getRight()==null ? null : + public ScalarSparsity estim(MMNode root) { + SparsityInformation spInf1 = !root.getLeft().isLeaf() ? + estim(root.getLeft()) : obtainSparsityInformation(root.getLeft()); + SparsityInformation spInf2 = root.getRight()==null ? null : !root.getRight().isLeaf() ? estim(root.getRight()) : - root.getRight().getDataCharacteristics(); - return root.setDataCharacteristics( - estimIntern(dc1, dc2, root.getOp())); + obtainSparsityInformation(root.getRight()); + if(spInf1 == null || (spInf2 == null && root.getOp().isBinaryOp())) { + return null; + } + ScalarSparsity spEst = estimIntern(spInf1, spInf2, root.getOp()); + root.setSparsityInformation(spEst); + return spEst; } @Override @@ -49,25 +51,27 @@ public double estim(MatrixBlock m1, MatrixBlock m2) { @Override public double estim(MatrixBlock m1, MatrixBlock m2, OpCode op) { - return estimIntern(m1.getDataCharacteristics(), m2.getDataCharacteristics(), op).getSparsity(); + return estimIntern(new ScalarSparsity(m1), new ScalarSparsity(m2), op).getAvgSparsity(); } @Override public double estim(MatrixBlock m, OpCode op) { - return estimIntern(m.getDataCharacteristics(), null, op).getSparsity(); + return estimIntern(new ScalarSparsity(m), null, op).getAvgSparsity(); } - protected void obtainSparsityInformation(MMNode node) { + protected SparsityInformation obtainSparsityInformation(MMNode node) { MatrixBlock mb = node.getData(); - if(mb != null && mb.getDataCharacteristics().getSparsity() == -1) { - if(mb.getNonZeros() != 0) { + if(mb != null) { + if(mb.getDataCharacteristics().getSparsity() == -1) { + node.setSparsityInformation(new ScalarSparsity(mb)); mb.getDataCharacteristics().setNonZeros(mb.getNonZeros()); } else { - mb.recomputeNonZeros(); - mb.getDataCharacteristics().setNonZeros(mb.getNonZeros()); + node.setSparsityInformation(new ScalarSparsity(mb.getNumRows(), mb.getNumColumns(), + mb.getDataCharacteristics().getSparsity())); } } + return node.getSparsityInformation(); } @Override @@ -77,20 +81,20 @@ public MMNode getInputMMNode(MatrixBlock mb) { return node; } - private DataCharacteristics estimIntern(DataCharacteristics dc1, DataCharacteristics dc2, OpCode op) { + private ScalarSparsity estimIntern(SparsityInformation spInf1, SparsityInformation spInf2, OpCode op) { switch (op) { case MM: - return new MatrixCharacteristics(dc1.getRows(), dc2.getCols(), - OptimizerUtils.getMatMultNnz(dc1.getSparsity(), dc2.getSparsity(), - dc1.getRows(), dc1.getCols(), dc2.getCols(), false)); + return new ScalarSparsity((int)spInf1.getNumRows(), (int)spInf2.getNumColumns(), + OptimizerUtils.getMatMultNnz(spInf1.getAvgSparsity(), spInf2.getAvgSparsity(), + spInf1.getNumRows(), spInf1.getNumColumns(), spInf2.getNumColumns(), false)); case MULT: - return new MatrixCharacteristics(dc1.getRows(), dc1.getCols(), - OptimizerUtils.getNnz(dc1.getRows(), dc1.getCols(), - dc1.getSparsity() * dc2.getSparsity())); + return new ScalarSparsity((int)spInf1.getNumRows(), (int)spInf1.getNumColumns(), + OptimizerUtils.getNnz(spInf1.getNumRows(), spInf1.getNumColumns(), + spInf1.getAvgSparsity() * spInf2.getAvgSparsity())); case PLUS: - return new MatrixCharacteristics(dc1.getRows(), dc1.getCols(), - OptimizerUtils.getNnz(dc1.getRows(), dc1.getCols(), - dc1.getSparsity() + dc2.getSparsity() - dc1.getSparsity() * dc2.getSparsity())); + return new ScalarSparsity((int)spInf1.getNumRows(), (int)spInf1.getNumColumns(), + OptimizerUtils.getNnz(spInf1.getNumRows(), spInf1.getNumColumns(), + spInf1.getAvgSparsity() + spInf2.getAvgSparsity() - spInf1.getAvgSparsity() * spInf2.getAvgSparsity())); case EQZERO: case DIAG: case CBIND: @@ -98,9 +102,40 @@ private DataCharacteristics estimIntern(DataCharacteristics dc1, DataCharacteris case NEQZERO: case TRANS: case RESHAPE: - return estimExactMetaData(dc1, dc2, op); + return estimExactMetaData(spInf1, spInf2, op); default: throw new NotImplementedException(); } } + + public static class ScalarSparsity extends SparsityInformation { + private final double _sparsityValue; + + public ScalarSparsity(int nRows, int nCols, double sparsity) { + super(nRows, nCols); + _sparsityValue = sparsity; + } + + public ScalarSparsity(int nRows, int nCols, long nnz) { + this(nRows, nCols, (double)nnz / (nRows * nCols)); + } + + public ScalarSparsity(MatrixBlock mb) { + super(mb); + if(mb.getSparsity() <= 0) + mb.recomputeNonZeros(); + _sparsityValue = mb.getSparsity(); + } + + @Override + public long getNonZeros() { + return Double.valueOf(getAvgSparsity() * getNumRows() * getNumColumns()) + .longValue(); + } + + @Override + public double getAvgSparsity() { + return _sparsityValue; + } + } } 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 791afde63ea..eba8f7ccdb2 100644 --- a/src/main/java/org/apache/sysds/hops/estim/EstimatorBasicWorst.java +++ b/src/main/java/org/apache/sysds/hops/estim/EstimatorBasicWorst.java @@ -21,9 +21,8 @@ import org.apache.commons.lang3.NotImplementedException; import org.apache.sysds.hops.OptimizerUtils; +import org.apache.sysds.hops.estim.EstimatorBasicAvg.ScalarSparsity; import org.apache.sysds.runtime.matrix.data.MatrixBlock; -import org.apache.sysds.runtime.meta.DataCharacteristics; -import org.apache.sysds.runtime.meta.MatrixCharacteristics; /** * Basic average case estimator for matrix sparsity: @@ -33,21 +32,23 @@ * estimate is equivalent to the average case estimate and * the exact output sparsity. */ -public class EstimatorBasicWorst extends SparsityEstimator -{ +public class EstimatorBasicWorst extends SparsityEstimator { @Override - public DataCharacteristics estim(MMNode root) { + public ScalarSparsity estim(MMNode root) { if (!root.getLeft().isLeaf()) - estim(root.getLeft()); // obtain synopsis + estim(root.getLeft()); // obtain sparsity information if (root.getRight()!=null && !root.getRight().isLeaf()) - estim(root.getRight()); // obtain synopsis - DataCharacteristics mc1 = !root.getLeft().isLeaf() ? - estim(root.getLeft()) : root.getLeft().getDataCharacteristics(); - DataCharacteristics mc2 = root.getRight()==null ? null : + estim(root.getRight()); // obtain sparsity information + SparsityInformation spInf1 = !root.getLeft().isLeaf() ? + estim(root.getLeft()) : obtainSparsityInformation(root.getLeft()); + SparsityInformation spInf2 = root.getRight()==null ? null : !root.getRight().isLeaf() ? estim(root.getRight()) : - root.getRight().getDataCharacteristics(); - return root.setDataCharacteristics( - estimIntern(mc1, mc2, root.getOp())); + obtainSparsityInformation(root.getRight()); + if(spInf1 == null || (spInf2 == null && root.getOp().isBinaryOp())) + return null; + ScalarSparsity spEst = estimIntern(spInf1, spInf2, root.getOp()); + root.setSparsityInformation(spEst); + return spEst; } @Override @@ -57,25 +58,27 @@ public double estim(MatrixBlock m1, MatrixBlock m2) { @Override public double estim(MatrixBlock m1, MatrixBlock m2, OpCode op) { - return estimIntern(m1.getDataCharacteristics(), m2.getDataCharacteristics(), op).getSparsity(); + return estimIntern(new ScalarSparsity(m1), new ScalarSparsity(m2), op).getAvgSparsity(); } @Override public double estim(MatrixBlock m, OpCode op) { - return estimIntern(m.getDataCharacteristics(), null, op).getSparsity(); + return estimIntern(new ScalarSparsity(m), null, op).getAvgSparsity(); } - protected void obtainSparsityInformation(MMNode node) { + protected SparsityInformation obtainSparsityInformation(MMNode node) { MatrixBlock mb = node.getData(); - if(mb != null && mb.getDataCharacteristics().getSparsity() == -1) { - if(mb.getNonZeros() != 0) { + if(mb != null) { + if(mb.getDataCharacteristics().getSparsity() == -1) { + node.setSparsityInformation(new ScalarSparsity(mb)); mb.getDataCharacteristics().setNonZeros(mb.getNonZeros()); } else { - mb.recomputeNonZeros(); - mb.getDataCharacteristics().setNonZeros(mb.getNonZeros()); + node.setSparsityInformation(new ScalarSparsity(mb.getNumRows(), mb.getNumColumns(), + mb.getDataCharacteristics().getSparsity())); } } + return node.getSparsityInformation(); } @Override @@ -85,20 +88,20 @@ public MMNode getInputMMNode(MatrixBlock mb) { return node; } - private DataCharacteristics estimIntern(DataCharacteristics mc1, DataCharacteristics mc2, OpCode op) { + private ScalarSparsity estimIntern(SparsityInformation spInf1, SparsityInformation spInf2, OpCode op) { switch (op) { case MM: - return new MatrixCharacteristics(mc1.getRows(), mc2.getCols(), - OptimizerUtils.getMatMultNnz(mc1.getSparsity(), mc2.getSparsity(), - mc1.getRows(), mc1.getCols(), mc2.getCols(), true)); + return new ScalarSparsity(spInf1.getNumRows(), spInf2.getNumColumns(), + OptimizerUtils.getMatMultNnz(spInf1.getAvgSparsity(), spInf2.getAvgSparsity(), + spInf1.getNumRows(), spInf1.getNumColumns(), spInf2.getNumColumns(), true)); case MULT: - return new MatrixCharacteristics(mc1.getRows(), mc1.getCols(), - OptimizerUtils.getNnz(mc1.getRows(), mc1.getCols(), - Math.min(mc1.getSparsity(), mc2.getSparsity()))); + return new ScalarSparsity(spInf1.getNumRows(), spInf1.getNumColumns(), + OptimizerUtils.getNnz(spInf1.getNumRows(), spInf1.getNumColumns(), + Math.min(spInf1.getAvgSparsity(), spInf2.getAvgSparsity()))); case PLUS: - return new MatrixCharacteristics(mc1.getRows(), mc1.getCols(), - OptimizerUtils.getNnz(mc1.getRows(), mc1.getCols(), - Math.min(mc1.getSparsity() + mc2.getSparsity(), 1))); + return new ScalarSparsity(spInf1.getNumRows(), spInf1.getNumColumns(), + OptimizerUtils.getNnz(spInf1.getNumRows(), spInf1.getNumColumns(), + Math.min(spInf1.getAvgSparsity() + spInf2.getAvgSparsity(), 1))); case EQZERO: case DIAG: case CBIND: @@ -106,7 +109,7 @@ private DataCharacteristics estimIntern(DataCharacteristics mc1, DataCharacteris case NEQZERO: case TRANS: case RESHAPE: - return estimExactMetaData(mc1, mc2, op); + return estimExactMetaData(spInf1, spInf2, op); default: throw new NotImplementedException(); } 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 8e86cdec272..2c95096ec15 100644 --- a/src/main/java/org/apache/sysds/hops/estim/EstimatorBitsetMM.java +++ b/src/main/java/org/apache/sysds/hops/estim/EstimatorBitsetMM.java @@ -22,11 +22,10 @@ import org.apache.commons.lang3.NotImplementedException; import org.apache.sysds.hops.HopsException; import org.apache.sysds.hops.OptimizerUtils; +import org.apache.sysds.hops.estim.EstimatorBasicAvg.ScalarSparsity; import org.apache.sysds.runtime.data.DenseBlock; import org.apache.sysds.runtime.data.SparseBlock; import org.apache.sysds.runtime.matrix.data.MatrixBlock; -import org.apache.sysds.runtime.meta.DataCharacteristics; -import org.apache.sysds.runtime.meta.MatrixCharacteristics; import org.apache.sysds.utils.stats.InfrastructureAnalyzer; import java.util.BitSet; @@ -56,14 +55,15 @@ public EstimatorBitsetMM(int type) { } @Override - public DataCharacteristics estim(MMNode root) { - BitsetMatrix m1Map = getCachedSynopsis(root.getLeft()); - BitsetMatrix m2Map = getCachedSynopsis(root.getRight()); - + public BitsetMatrix estim(MMNode root) { + BitsetMatrix m1Map = getCachedSparsityInformation(root.getLeft()); + BitsetMatrix m2Map = getCachedSparsityInformation(root.getRight()); + + if(m1Map == null || (m2Map == null && root.getOp().isBinaryOp())) + return null; BitsetMatrix outMap = estimInternal(m1Map, m2Map, root.getOp()); - root.setSynopsis(outMap); // memorize boolean matrix - return root.setDataCharacteristics(new MatrixCharacteristics( - outMap.getNumRows(), outMap.getNumColumns(), outMap.getNonZeros())); + root.setSparsityInformation(outMap); // memorize boolean matrix + return outMap; } @Override @@ -73,9 +73,9 @@ public double estim(MatrixBlock m1, MatrixBlock m2) { @Override public double estim(MatrixBlock m1, MatrixBlock m2, OpCode op) { - if( isExactMetadataOp(op) ) - return estimExactMetaData(m1.getDataCharacteristics(), - m2.getDataCharacteristics(), op).getSparsity(); + if(isExactMetadataOp(op)) + return estimExactMetaData(new ScalarSparsity(m1), + new ScalarSparsity(m2), op).getAvgSparsity(); BitsetMatrix m1Map = createBitset(m1); BitsetMatrix m2Map = (m1 == m2) ? //self product m1Map : createBitset(m2); @@ -87,7 +87,7 @@ public double estim(MatrixBlock m1, MatrixBlock m2, OpCode op) { @Override public double estim(MatrixBlock m, OpCode op) { if( isExactMetadataOp(op) ) - return estimExactMetaData(m.getDataCharacteristics(), null, op).getSparsity(); + return estimExactMetaData(new ScalarSparsity(m), null, op).getAvgSparsity(); BitsetMatrix m1Map = createBitset(m); BitsetMatrix outMap = estimInternal(m1Map, null, op); return OptimizerUtils.getSparsity(outMap.getNumRows(), @@ -97,19 +97,19 @@ public double estim(MatrixBlock m, OpCode op) { @Override public MMNode getInputMMNode(MatrixBlock mb) { MMNode node = new MMNode(mb); - getCachedSynopsis(node); + getCachedSparsityInformation(node); return node; } - private BitsetMatrix getCachedSynopsis(MMNode node) { + private BitsetMatrix getCachedSparsityInformation(MMNode node) { if( node == null ) return null; //ensure synopsis is properly cached and reused - if( node.isLeaf() && node.getSynopsis() == null ) - node.setSynopsis(createBitset(node.getData())); + if( node.isLeaf() && node.getSparsityInformation() == null ) + node.setSparsityInformation(createBitset(node.getData())); else if( !node.isLeaf() ) estim(node); //recursively obtain synopsis - return (BitsetMatrix) node.getSynopsis(); + return (BitsetMatrix) node.getSparsityInformation(); } private static BitsetMatrix estimInternal(BitsetMatrix m1Map, BitsetMatrix m2Map, OpCode op) { @@ -131,23 +131,16 @@ private static BitsetMatrix estimInternal(BitsetMatrix m1Map, BitsetMatrix m2Map } } - public abstract static class BitsetMatrix { - protected final int _rlen; - protected final int _clen; + public abstract static class BitsetMatrix extends SparsityInformation { protected long _nonZeros; public BitsetMatrix(int rlen, int clen) { - _rlen = rlen; - _clen = clen; + super(rlen, clen); _nonZeros = 0; } - - public int getNumRows() { - return _rlen; - } - public int getNumColumns() { - return _clen; + public BitsetMatrix(MatrixBlock mb) { + super(mb); } public long getNonZeros() { @@ -171,7 +164,10 @@ protected void init(MatrixBlock in) { //single-threaded bitset construction buildIntern(in, 0, in.getNumRows()); } - _nonZeros = in.getNonZeros(); + if((_nonZeros = in.getNonZeros()) <= 0) { + in.recomputeNonZeros(); + _nonZeros = in.getNonZeros(); + } } public BitsetMatrix matMult(BitsetMatrix m2) { 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 0669ce6c939..f5215883ee9 100644 --- a/src/main/java/org/apache/sysds/hops/estim/EstimatorDensityMap.java +++ b/src/main/java/org/apache/sysds/hops/estim/EstimatorDensityMap.java @@ -21,12 +21,11 @@ import org.apache.commons.lang3.NotImplementedException; import org.apache.sysds.hops.OptimizerUtils; +import org.apache.sysds.hops.estim.EstimatorBasicAvg.ScalarSparsity; import org.apache.sysds.runtime.data.DenseBlock; import org.apache.sysds.runtime.data.SparseBlock; import org.apache.sysds.runtime.matrix.data.LibMatrixReorg; import org.apache.sysds.runtime.matrix.data.MatrixBlock; -import org.apache.sysds.runtime.meta.DataCharacteristics; -import org.apache.sysds.runtime.meta.MatrixCharacteristics; import org.apache.sysds.runtime.util.UtilFunctions; /** @@ -55,15 +54,16 @@ public EstimatorDensityMap(int blocksize) { } @Override - public DataCharacteristics estim(MMNode root) { - DensityMap m1Map = getCachedSynopsis(root.getLeft()); - DensityMap m2Map = getCachedSynopsis(root.getRight()); + public DensityMap estim(MMNode root) { + DensityMap m1Map = getCachedSparsityInformation(root.getLeft()); + DensityMap m2Map = getCachedSparsityInformation(root.getRight()); + if(m1Map == null || (m2Map == null && root.getOp().isBinaryOp())) + return null; //estimate output density map and sparsity DensityMap outMap = estimIntern(m1Map, m2Map, root.getOp()); - root.setSynopsis(outMap); //memoize density map - return root.setDataCharacteristics(new MatrixCharacteristics( - outMap.getNumRowsOrig(), outMap.getNumColumnsOrig(), outMap.getNonZeros())); + root.setSparsityInformation(outMap); //memoize density map + return outMap; } @Override @@ -73,9 +73,9 @@ public double estim(MatrixBlock m1, MatrixBlock m2) { @Override public double estim(MatrixBlock m1, MatrixBlock m2, OpCode op) { - if( isExactMetadataOp(op) ) - return estimExactMetaData(m1.getDataCharacteristics(), m2 != null ? - m2.getDataCharacteristics() : null, op).getSparsity(); + if(isExactMetadataOp(op)) + return estimExactMetaData(new ScalarSparsity(m1), m2 != null ? + new ScalarSparsity(m2) : null, op).getAvgSparsity(); DensityMap m1Map = new DensityMap(m1, _b); DensityMap m2Map = (m1 == m2 || m2 == null) ? //self product m1Map : new DensityMap(m2, _b); @@ -92,19 +92,19 @@ public double estim(MatrixBlock m, OpCode op) { @Override public MMNode getInputMMNode(MatrixBlock mb) { MMNode node = new MMNode(mb); - getCachedSynopsis(node); + getCachedSparsityInformation(node); return node; } - private DensityMap getCachedSynopsis(MMNode node) { + private DensityMap getCachedSparsityInformation(MMNode node) { if( node == null ) return null; //ensure synopsis is properly cached and reused - if( node.isLeaf() && node.getSynopsis() == null ) - node.setSynopsis(new DensityMap(node.getData(), _b)); + if( node.isLeaf() && node.getSparsityInformation() == null ) + node.setSparsityInformation(new DensityMap(node.getData(), _b)); else if( !node.isLeaf() ) estim(node); //recursively obtain synopsis - return (DensityMap) node.getSynopsis(); + return (DensityMap) node.getSparsityInformation(); } /** @@ -227,16 +227,13 @@ private DensityMap estimInternEqZero(DensityMap m1Map) { m1Map.getNumColumnsOrig(), _b, m1Map._scaled); } - public static class DensityMap { + public static class DensityMap extends SparsityInformation { private final MatrixBlock _map; - private final int _rlen; - private final int _clen; private final int _b; private boolean _scaled; //false->nnz, true->sp public DensityMap(MatrixBlock in, int b) { - _rlen = in.getNumRows(); - _clen = in.getNumColumns(); + super(in); _b = b; _map = init(in); _scaled = false; @@ -245,8 +242,7 @@ public DensityMap(MatrixBlock in, int b) { } public DensityMap(MatrixBlock map, int rlenOrig, int clenOrig, int b, boolean scaled) { - _rlen = rlenOrig; - _clen = clenOrig; + super(rlenOrig, clenOrig); _b = b; _map = map; _scaled = scaled; @@ -273,12 +269,21 @@ public int getNumRowsOrig() { public int getNumColumnsOrig() { return _clen; } - + + @Override public long getNonZeros() { - if( _scaled ) toNnz(); + if(_scaled) + toNnz(); return Math.round(_map.sum()); } - + + @Override + public double getAvgSparsity() { + if(!_scaled) + toSparsity(); + return _map.mean(); + } + public int getRowBlockize(int r) { return UtilFunctions.computeBlockSize(_rlen, r+1, _b); } 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 4e171efa6f7..1ba90927bab 100644 --- a/src/main/java/org/apache/sysds/hops/estim/EstimatorLayeredGraph.java +++ b/src/main/java/org/apache/sysds/hops/estim/EstimatorLayeredGraph.java @@ -26,8 +26,6 @@ import org.apache.sysds.runtime.data.DenseBlock; import org.apache.sysds.runtime.data.SparseBlock; import org.apache.sysds.runtime.matrix.data.MatrixBlock; -import org.apache.sysds.runtime.meta.DataCharacteristics; -import org.apache.sysds.runtime.meta.MatrixCharacteristics; import java.util.ArrayList; import java.util.Arrays; @@ -63,14 +61,12 @@ public EstimatorLayeredGraph(int rounds, int seed) { } @Override - public DataCharacteristics estim(MMNode root) { + public LayeredGraph estim(MMNode root) { //List leafs = getMatrices(root, new ArrayList<>()); //List ops = getOps(root, new ArrayList<>()); //List LGs = new ArrayList<>(); LayeredGraph ret = traverse(root); - long nnz = ret.estimateNnz(); - return root.setDataCharacteristics(new MatrixCharacteristics( - ret._nodes.get(0).length, ret._nodes.get(ret._nodes.size() - 1).length, nnz)); + return ret; } public LayeredGraph traverse(MMNode node) { @@ -84,6 +80,8 @@ public LayeredGraph traverse(MMNode node) { right = (node.getRight().getData() == null) ? retR : new LayeredGraph(node.getRight().getData(), _rounds, _seeds.nextInt()); + if(left == null || (right == null && node.getOp().isBinaryOp())) + return null; ret = estimInternal(left, right, node.getOp()); return ret; @@ -110,14 +108,15 @@ public double estim(MatrixBlock m, OpCode op) { @Override public double estim(MatrixBlock m1, MatrixBlock m2) { - LayeredGraph graph = new LayeredGraph(Arrays.asList(m1,m2), _rounds, _seeds.nextInt()); + LayeredGraph graph = new LayeredGraph(Arrays.asList(m1,m2), m1.getNumRows(), m2.getNumColumns(), + _rounds, _seeds.nextInt()); return OptimizerUtils.getSparsity( m1.getNumRows(), m2.getNumColumns(), graph.estimateNnz()); } @Override public MMNode getInputMMNode(MatrixBlock mb) { - throw new UnsupportedOperationException("Not implemented yet."); + throw new NotImplementedException("Not implemented yet."); } private static LayeredGraph estimInternal(LayeredGraph lg1, LayeredGraph lg2, OpCode op) { @@ -162,24 +161,29 @@ private List getOps(MMNode node, List ops) { return ops; } - public static class LayeredGraph { + public static class LayeredGraph extends SparsityInformation { private final List _nodes; //nodes partitioned by graph level private final int _rounds; //length of propagated r-vectors private final Random _seeds; - + public LayeredGraph(int r, int seed) { + this(List.of(), -1, -1, r, seed); + } + + public LayeredGraph(List chain, int nRowsFirst, int nColsLast, int r, int seed) { + super(nRowsFirst, nColsLast); _nodes = new ArrayList<>(); _rounds = r; _seeds = new Random(seed); - } - - public LayeredGraph(List chain, int r, int seed) { - this(r, seed); chain.forEach(i -> buildNext(i)); } public LayeredGraph(MatrixBlock m, int r, int seed) { - this(r, seed); + super(m); + + _nodes = new ArrayList<>(); + _rounds = r; + _seeds = new Random(seed); buildNext(m); } @@ -228,7 +232,14 @@ public void buildNext(MatrixBlock mb) { } } } - + + @Override + public long getNonZeros() { + if(_data != null) + return _data.getNonZeros(); + return estimateNnz(); + } + public long estimateNnz() { //step 1: assign random vectors ~exp(lambda=1) to all leaf nodes //(lambda is not the mean, if lambda is 2 mean is 1/2) @@ -252,7 +263,7 @@ private static double calcNNZ(double[] inpvec, int rounds) { } public LayeredGraph rbind(LayeredGraph lg) { - LayeredGraph ret = new LayeredGraph(List.of(), _rounds, _seeds.nextInt()); + LayeredGraph ret = new LayeredGraph(_rounds, _seeds.nextInt()); Node[] rows = new Node[_nodes.get(0).length + lg._nodes.get(0).length]; Node[] columns = _nodes.get(1).clone(); @@ -276,7 +287,7 @@ public LayeredGraph rbind(LayeredGraph lg) { } public LayeredGraph cbind(LayeredGraph lg) { - LayeredGraph ret = new LayeredGraph(List.of(), _rounds, _seeds.nextInt()); + LayeredGraph ret = new LayeredGraph(_rounds, _seeds.nextInt()); int colLength = _nodes.get(1).length + lg._nodes.get(1).length; Node[] rows = _nodes.get(0).clone(); @@ -304,11 +315,12 @@ public LayeredGraph matMult(LayeredGraph lg) { List m = Stream.concat( this.toMatrixBlockList().stream(), lg.toMatrixBlockList().stream()) .collect(Collectors.toList()); - return new LayeredGraph(m, _rounds, _seeds.nextInt()); + return new LayeredGraph(m, m.get(0).getNumRows(), m.get(m.size()-1).getNumColumns(), + _rounds, _seeds.nextInt()); } public LayeredGraph or(LayeredGraph lg) { - LayeredGraph ret = new LayeredGraph(List.of(), _rounds, _seeds.nextInt()); + LayeredGraph ret = new LayeredGraph(_rounds, _seeds.nextInt()); Node[] rows = new Node[_nodes.get(0).length]; for (int i = 0; i < _nodes.get(0).length; i++) rows[i] = new Node(); @@ -337,7 +349,7 @@ public LayeredGraph or(LayeredGraph lg) { } public LayeredGraph and(LayeredGraph lg) { - LayeredGraph ret = new LayeredGraph(List.of(), _rounds, _seeds.nextInt()); + LayeredGraph ret = new LayeredGraph(_rounds, _seeds.nextInt()); Node[] rows = new Node[_nodes.get(0).length]; for (int i = 0; i < _nodes.get(0).length; i++) rows[i] = new Node(); @@ -366,7 +378,7 @@ public LayeredGraph and(LayeredGraph lg) { } public LayeredGraph transpose() { - LayeredGraph ret = new LayeredGraph(List.of(), _rounds, _seeds.nextInt()); + LayeredGraph ret = new LayeredGraph(_rounds, _seeds.nextInt()); Node[] rows = new Node[_nodes.get(_nodes.size() - 1).length]; for (int i = 0; i < rows.length; i++) rows[i] = new Node(); @@ -395,7 +407,7 @@ public LayeredGraph transpose() { } public LayeredGraph diag() { - LayeredGraph ret = new LayeredGraph(List.of(), _rounds, _seeds.nextInt()); + LayeredGraph ret = new LayeredGraph(_rounds, _seeds.nextInt()); Node[] rowsOld = _nodes.get(0); Node[] columnsOld = _nodes.get(1); 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 83f568c21b3..f68ebcc8f91 100644 --- a/src/main/java/org/apache/sysds/hops/estim/EstimatorMatrixHistogram.java +++ b/src/main/java/org/apache/sysds/hops/estim/EstimatorMatrixHistogram.java @@ -22,12 +22,11 @@ import org.apache.commons.lang3.ArrayUtils; import org.apache.commons.lang3.NotImplementedException; import org.apache.sysds.hops.OptimizerUtils; +import org.apache.sysds.hops.estim.EstimatorBasicAvg.ScalarSparsity; import org.apache.sysds.runtime.data.DenseBlock; import org.apache.sysds.runtime.data.SparseBlock; import org.apache.sysds.runtime.matrix.data.LibMatrixAgg; import org.apache.sysds.runtime.matrix.data.MatrixBlock; -import org.apache.sysds.runtime.meta.DataCharacteristics; -import org.apache.sysds.runtime.meta.MatrixCharacteristics; import java.util.Arrays; import java.util.Random; @@ -56,30 +55,31 @@ public EstimatorMatrixHistogram(boolean useExtended) { } @Override - public DataCharacteristics estim(MMNode root) { + public SparsityInformation estim(MMNode root) { return estim(root, true); } - public DataCharacteristics estim(MMNode root, boolean topLevel) { + public SparsityInformation estim(MMNode root, boolean topLevel) { //NOTE: not estimateInputs due to handling of topLevel - MatrixHistogram h1 = getCachedSynopsis(root.getLeft()); - MatrixHistogram h2 = getCachedSynopsis(root.getRight()); + MatrixHistogram h1 = getCachedSparsityInformation(root.getLeft()); + MatrixHistogram h2 = getCachedSparsityInformation(root.getRight()); + if(h1 == null || (h2 == null && root.getOp().isBinaryOp())) + return null; //estimate output sparsity based on input histograms double ret = estimIntern(h1, h2, root.getOp(), root.getMisc()); - if( topLevel ) { //fast-path final result - return MatrixHistogram.deriveOutputCharacteristics( + if(topLevel) { // fast-path final result + return MatrixHistogram.deriveAverageSparsity( h1, h2, ret, root.getOp(), root.getMisc()); } //sketch propagation for intermediates other than final result - if( h2 != null && root.getRight() != null ) + if(h2 != null && root.getRight() != null) h2.setData(root.getRight().isLeaf() ? root.getRight().getData() : null); MatrixHistogram outMap = MatrixHistogram .deriveOutputHistogram(h1, h2, ret, root.getOp(), root.getMisc()); - root.setSynopsis(outMap); - return root.setDataCharacteristics(new MatrixCharacteristics( - outMap.getRows(), outMap.getCols(), outMap.getNonZeros())); + root.setSparsityInformation(outMap); + return outMap; } @Override @@ -90,8 +90,8 @@ public double estim(MatrixBlock m1, MatrixBlock m2) { @Override public double estim(MatrixBlock m1, MatrixBlock m2, OpCode op) { if( isExactMetadataOp(op) ) - return estimExactMetaData(m1.getDataCharacteristics(), - m2.getDataCharacteristics(), op).getSparsity(); + return estimExactMetaData(new ScalarSparsity(m1), + new ScalarSparsity(m2), op).getAvgSparsity(); MatrixHistogram h1 = new MatrixHistogram(m1, _useExtended); MatrixHistogram h2 = (m1 == m2) ? //self product h1 : new MatrixHistogram(m2, _useExtended); @@ -101,7 +101,7 @@ public double estim(MatrixBlock m1, MatrixBlock m2, OpCode op) { @Override public double estim(MatrixBlock m1, OpCode op) { if( isExactMetadataOp(op) ) - return estimExactMetaData(m1.getDataCharacteristics(), null, op).getSparsity(); + return estimExactMetaData(new ScalarSparsity(m1), null, op).getAvgSparsity(); MatrixHistogram h1 = new MatrixHistogram(m1, _useExtended); return estimIntern(h1, null, op, null); } @@ -109,62 +109,64 @@ public double estim(MatrixBlock m1, OpCode op) { @Override public MMNode getInputMMNode(MatrixBlock mb) { MMNode node = new MMNode(mb); - getCachedSynopsis(node); + getCachedSparsityInformation(node); return node; } - private MatrixHistogram getCachedSynopsis(MMNode node) { + private MatrixHistogram getCachedSparsityInformation(MMNode node) { if( node == null ) return null; - //ensure synopsis is properly cached and reused - if( node.isLeaf() && node.getSynopsis() == null ) - node.setSynopsis(new MatrixHistogram(node.getData(), _useExtended)); + //ensure sparsity information is properly cached and reused + if(node.isLeaf() && node.getSparsityInformation() == null && node.getData() != null) + node.setSparsityInformation(new MatrixHistogram(node.getData(), _useExtended)); else if( !node.isLeaf() ) estim(node, false); //recursively obtain synopsis - return (MatrixHistogram) node.getSynopsis(); + return (MatrixHistogram) node.getSparsityInformation(); } public double estimIntern(MatrixHistogram h1, MatrixHistogram h2, OpCode op, long[] misc) { - double msize = (double)h1.getRows()*h1.getCols(); + if(h1 == null || (op.isBinaryOp() && h2 == null)) + return -1; + double msize = (double)h1.getNumRows() * h1.getNumColumns(); switch (op) { case MM: return estimInternMM(h1, h2); case MULT: { - final double scale = IntStream.range(0, h1.getCols()) + final double scale = IntStream.range(0, h1.getNumColumns()) .mapToDouble(j -> (double)h1.cNnz[j] * h2.cNnz[j]).sum() / h1.getNonZeros() / h2.getNonZeros(); - return IntStream.range(0, h1.getRows()) + return IntStream.range(0, h1.getNumRows()) .mapToDouble(i -> (double)h1.rNnz[i] * h2.rNnz[i] * scale) //collisions .sum() / msize; } case PLUS: { - final double scale = IntStream.range(0, h1.getCols()) + final double scale = IntStream.range(0, h1.getNumColumns()) .mapToDouble(j -> (double)h1.cNnz[j] * h2.cNnz[j]).sum() / h1.getNonZeros() / h2.getNonZeros(); - return IntStream.range(0, h1.getRows()) + return IntStream.range(0, h1.getNumRows()) .mapToDouble(i -> (double)h1.rNnz[i] + h2.rNnz[i] //all minus collisions - (double)h1.rNnz[i] * h2.rNnz[i] * scale) .sum() / msize; } case EQZERO: - return OptimizerUtils.getSparsity(h1.getRows(), h1.getCols(), - (long)h1.getRows() * h1.getCols() - h1.getNonZeros()); + return OptimizerUtils.getSparsity(h1.getNumRows(), h1.getNumColumns(), + (long)h1.getNumRows() * h1.getNumColumns() - h1.getNonZeros()); case DIAG: - return (h1.getCols()==1) ? - OptimizerUtils.getSparsity(h1.getRows(), h1.getRows(), h1.getNonZeros()) : - OptimizerUtils.getSparsity(h1.getRows(), 1, Math.min(h1.getRows(), h1.getNonZeros())); + return (h1.getNumColumns()==1) ? + OptimizerUtils.getSparsity(h1.getNumRows(), h1.getNumRows(), h1.getNonZeros()) : + OptimizerUtils.getSparsity(h1.getNumRows(), 1, Math.min(h1.getNumRows(), h1.getNonZeros())); //binary operations that preserve sparsity exactly case CBIND: - return OptimizerUtils.getSparsity(h1.getRows(), - h1.getCols()+h2.getCols(), h1.getNonZeros() + h2.getNonZeros()); + return OptimizerUtils.getSparsity(h1.getNumRows(), + h1.getNumColumns()+h2.getNumColumns(), h1.getNonZeros() + h2.getNonZeros()); case RBIND: - return OptimizerUtils.getSparsity(h1.getRows()+h2.getRows(), - h1.getCols(), h1.getNonZeros() + h2.getNonZeros()); + return OptimizerUtils.getSparsity(h1.getNumRows()+h2.getNumRows(), + h1.getNumColumns(), h1.getNonZeros() + h2.getNonZeros()); //unary operation that preserve sparsity exactly case NEQZERO: case TRANS: case RESHAPE: - return OptimizerUtils.getSparsity(h1.getRows(), h1.getCols(), h1.getNonZeros()); + return OptimizerUtils.getSparsity(h1.getNumRows(), h1.getNumColumns(), h1.getNonZeros()); default: throw new NotImplementedException(); } @@ -175,19 +177,19 @@ private double estimInternMM(MatrixHistogram h1, MatrixHistogram h2) { //special case, with exact sparsity estimate, where the dot product //dot(h1.cNnz,h2rNnz) gives the exact number of non-zeros in the output if( h1.rMaxNnz <= 1 || h2.cMaxNnz <= 1 ) { - for( int j=0; j i!=0).count(); cNonEmpty = (int)Arrays.stream(cNnz).filter(i -> i!=0).count(); } - - public int getRows() { - return rNnz.length; - } - - public int getCols() { - return cNnz.length; - } - + public int[] getRowCounts() { return rNnz; } @@ -356,16 +353,14 @@ public int[] getColCounts() { } public long getNonZeros() { - return getRows() < getCols() ? - IntStream.range(0, getRows()).mapToLong(i-> rNnz[i]).sum() : - IntStream.range(0, getCols()).mapToLong(i-> cNnz[i]).sum(); - } - - public void setData(MatrixBlock mb) { - _data = mb; + return getNumRows() < getNumColumns() ? + IntStream.range(0, getNumRows()).mapToLong(i-> rNnz[i]).sum() : + IntStream.range(0, getNumColumns()).mapToLong(i-> cNnz[i]).sum(); } public static MatrixHistogram deriveOutputHistogram(MatrixHistogram h1, MatrixHistogram h2, double spOut, OpCode op, long[] misc) { + if(h1 == null || (op.isBinaryOp() && h2 == null)) + return null; switch(op) { case MM: return deriveMMHistogram(h1, h2, spOut); case MULT: return deriveMultHistogram(h1, h2); @@ -382,31 +377,47 @@ public static MatrixHistogram deriveOutputHistogram(MatrixHistogram h1, MatrixHi } } - public static DataCharacteristics deriveOutputCharacteristics(MatrixHistogram h1, MatrixHistogram h2, double spOut, OpCode op, long[] misc) { + public static ScalarSparsity deriveAverageSparsity(MatrixHistogram h1, MatrixHistogram h2, double spOut, OpCode op, long[] misc) { switch(op) { case MM: - return new MatrixCharacteristics(h1.getRows(), h2.getCols(), - OptimizerUtils.getNnz(h1.getRows(), h2.getCols(), spOut)); + if(h1 == null || h2 == null) + return null; + return new ScalarSparsity(h1.getNumRows(), h2.getNumColumns(), + OptimizerUtils.getNnz(h1.getNumRows(), h2.getNumColumns(), spOut)); case MULT: case PLUS: case NEQZERO: case EQZERO: - return new MatrixCharacteristics(h1.getRows(), h1.getCols(), - OptimizerUtils.getNnz(h1.getRows(), h1.getCols(), spOut)); + if(h1 != null) + return new ScalarSparsity(h1.getNumRows(), h1.getNumColumns(), + OptimizerUtils.getNnz(h1.getNumRows(), h1.getNumColumns(), spOut)); + else if(h2 != null) + return new ScalarSparsity(h2.getNumRows(), h2.getNumColumns(), + OptimizerUtils.getNnz(h2.getNumRows(), h2.getNumColumns(), spOut)); + else + return null; case RBIND: - return new MatrixCharacteristics(h1.getRows()+h2.getRows(), h1.getCols(), - OptimizerUtils.getNnz(h1.getRows()+h2.getRows(), h1.getCols(), spOut)); + if(h1 == null || h2 == null) + return null; + return new ScalarSparsity(h1.getNumRows()+h2.getNumRows(), h1.getNumColumns(), + OptimizerUtils.getNnz(h1.getNumRows()+h2.getNumRows(), h1.getNumColumns(), spOut)); case CBIND: - return new MatrixCharacteristics(h1.getRows(), h1.getCols()+h2.getCols(), - OptimizerUtils.getNnz(h1.getRows(), h1.getCols()+h2.getCols(), spOut)); + if(h1 == null || h2 == null) + return null; + return new ScalarSparsity(h1.getNumRows(), h1.getNumColumns()+h2.getNumColumns(), + OptimizerUtils.getNnz(h1.getNumRows(), h1.getNumColumns()+h2.getNumColumns(), spOut)); case DIAG: - int ncol = h1.getCols()==1 ? h1.getRows() : 1; - return new MatrixCharacteristics(h1.getRows(), ncol, - OptimizerUtils.getNnz(h1.getRows(), ncol, spOut)); + if(h1 == null) + return null; + int ncol = h1.getNumColumns()==1 ? h1.getNumRows() : 1; + return new ScalarSparsity(h1.getNumRows(), ncol, + OptimizerUtils.getNnz(h1.getNumRows(), ncol, spOut)); case TRANS: - return new MatrixCharacteristics(h1.getCols(), h1.getRows(), h1.getNonZeros()); + if(h1 == null) + return null; + return new ScalarSparsity(h1.getNumColumns(), h1.getNumRows(), h1.getNonZeros()); case RESHAPE: - return new MatrixCharacteristics((int)misc[0], (int)misc[1], + return new ScalarSparsity((int)misc[0], (int)misc[1], OptimizerUtils.getNnz((int)misc[0], (int)misc[1], spOut)); default: throw new NotImplementedException(); @@ -422,22 +433,22 @@ private static MatrixHistogram deriveMMHistogram(MatrixHistogram h1, MatrixHisto //get input/output nnz for scaling long nnz1 = h1.getNonZeros(); long nnz2 = h2.getNonZeros(); - double nnzOut = spOut * h1.getRows() * h2.getCols(); + double nnzOut = spOut * h1.getNumRows() * h2.getNumColumns(); //propagate h1.r and h2.c to output via simple scaling //(this implies 0s propagate and distribution is preserved) int rMaxNnz = 0, cMaxNnz = 0; - int[] rNnz = new int[h1.getRows()]; + int[] rNnz = new int[h1.getNumRows()]; Random rn = new Random(); - for( int i=0; i (double)h1.cNnz[j] * h2.cNnz[j]) .sum() / h1.getNonZeros() / h2.getNonZeros(); - final double scalec = IntStream.range(0, h1.getRows()) + final double scalec = IntStream.range(0, h1.getNumRows()) .mapToDouble(j -> (double)h1.rNnz[j] * h2.rNnz[j]) .sum() / h1.getNonZeros() / h2.getNonZeros(); int rMaxNnz = 0, cMaxNnz = 0; Random rn = new Random(); - int[] rNnz = new int[h1.getRows()]; - for(int i=0; i (double)h1.cNnz[j] * h2.cNnz[j]) .sum() / h1.getNonZeros() / h2.getNonZeros(); - final double scalec = IntStream.range(0, h1.getRows()) + final double scalec = IntStream.range(0, h1.getNumRows()) .mapToDouble(j -> (double)h1.rNnz[j] * h2.rNnz[j]) .sum() / h1.getNonZeros() / h2.getNonZeros(); int rMaxNnz = 0, cMaxNnz = 0; Random rn = new Random(); - int[] rNnz = new int[h1.getRows()]; - for(int i=0; iN rows + if( h1.getNumColumns() % cols == 0 ) { //1->N rows //scale and replicate row counts - int scale = h1.getCols()/cols; + int scale = h1.getNumColumns()/cols; for(int i=0, pos=0; i1 rows - int scale = h1.getRows()/rows; + else if ( h1.getNumRows() % rows == 0 ) { //N->1 rows + int scale = h1.getNumRows()/rows; //scale and replicate column counts for(int i=0, pos=0; i (double)a.size(idx) / mb.getNumColumns()).toArray(); + } + else { + DenseBlock a = mb.getDenseBlock(); + rSpar = IntStream.range(0, mb.getNumRows()) + .mapToDouble(idx -> (double)a.countNonZeros(idx) / mb.getNumColumns()).toArray(); + } + } + else { + throw new DMLRuntimeException("Matrix Block should not be empty at this point."); + } + } + + @Override + public long getNonZeros() { + return Double.valueOf(getAvgSparsity() * getNumRows() * getNumColumns()) + .longValue(); + } + + @Override + public double getAvgSparsity() { + return DoubleStream.of(rSpar).average().orElse(0); + } + + public double get(int rIdx) { + return rSpar[rIdx]; + } + + public void set(int rIdx, double val) { + rSpar[rIdx] = val; + } + + public double[] getVector() { + return rSpar; + } + + public void setVector(double[] vec) { + rSpar = vec; + } + } }; 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 c86c80cd801..a9545692f1e 100644 --- a/src/main/java/org/apache/sysds/hops/estim/EstimatorSample.java +++ b/src/main/java/org/apache/sysds/hops/estim/EstimatorSample.java @@ -21,12 +21,12 @@ import org.apache.commons.lang3.NotImplementedException; import org.apache.sysds.hops.OptimizerUtils; +import org.apache.sysds.hops.estim.EstimatorBasicAvg.ScalarSparsity; import org.apache.sysds.runtime.DMLRuntimeException; import org.apache.sysds.runtime.data.DenseBlock; import org.apache.sysds.runtime.data.SparseBlock; import org.apache.sysds.runtime.matrix.data.LibMatrixAgg; import org.apache.sysds.runtime.matrix.data.MatrixBlock; -import org.apache.sysds.runtime.meta.DataCharacteristics; import org.apache.sysds.runtime.util.UtilFunctions; /** @@ -63,7 +63,7 @@ public EstimatorSample(double sampleFrac, boolean extended) { } @Override - public DataCharacteristics estim(MMNode root) { + public ScalarSparsity estim(MMNode root) { LOG.warn("Recursive estimates not supported by EstimatorSample, falling back to EstimatorBasicAvg."); return new EstimatorBasicAvg().estim(root); } @@ -157,9 +157,9 @@ public double estim(MatrixBlock m1, MatrixBlock m2, OpCode op) { case TRANS: case DIAG: case RESHAPE: - DataCharacteristics mc1 = m1.getDataCharacteristics(); - DataCharacteristics mc2 = m2.getDataCharacteristics(); - return OptimizerUtils.getSparsity(estimExactMetaData(mc1, mc2, op)); + ScalarSparsity ssp1 = new ScalarSparsity(m1); + ScalarSparsity ssp2 = new ScalarSparsity(m2); + return estimExactMetaData(ssp1, ssp2, op).getAvgSparsity(); default: throw new NotImplementedException(); } @@ -200,7 +200,7 @@ private static int[] computeColumnNnz(MatrixBlock in, int[] ix) { @Override public MMNode getInputMMNode(MatrixBlock mb) { - throw new UnsupportedOperationException("Not implemented yet."); + throw new NotImplementedException("Not implemented yet."); } private static int[] computeRowNnz(MatrixBlock in, int[] ix) { 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 194d492d467..a6f7b1b0c7a 100644 --- a/src/main/java/org/apache/sysds/hops/estim/EstimatorSampleRa.java +++ b/src/main/java/org/apache/sysds/hops/estim/EstimatorSampleRa.java @@ -22,11 +22,11 @@ import org.apache.commons.lang3.NotImplementedException; import org.apache.commons.math3.random.Well1024a; import org.apache.sysds.hops.OptimizerUtils; +import org.apache.sysds.hops.estim.EstimatorBasicAvg.ScalarSparsity; import org.apache.sysds.runtime.DMLRuntimeException; import org.apache.sysds.runtime.data.SparseBlock; import org.apache.sysds.runtime.matrix.data.LibMatrixDatagen; import org.apache.sysds.runtime.matrix.data.MatrixBlock; -import org.apache.sysds.runtime.meta.DataCharacteristics; import java.util.ArrayList; import java.util.Arrays; @@ -90,7 +90,7 @@ public EstimatorSampleRa(int runs, double sampleFrac, double eps, double delta, } @Override - public DataCharacteristics estim(MMNode root) { + public ScalarSparsity estim(MMNode root) { LOG.warn("Recursive estimates not supported by EstimatorSampleRa," + " falling back to EstimatorBasicAvg."); return new EstimatorBasicAvg().estim(root); @@ -130,7 +130,7 @@ public double estim(MatrixBlock m1, MatrixBlock m2) { @Override public MMNode getInputMMNode(MatrixBlock mb) { - throw new UnsupportedOperationException("Not implemented yet."); + throw new NotImplementedException("Not implemented yet."); } private void initHashArrays(int m, int n, int l) { diff --git a/src/main/java/org/apache/sysds/hops/estim/MMNode.java b/src/main/java/org/apache/sysds/hops/estim/MMNode.java index 7483dba0223..acc0c014e35 100644 --- a/src/main/java/org/apache/sysds/hops/estim/MMNode.java +++ b/src/main/java/org/apache/sysds/hops/estim/MMNode.java @@ -35,7 +35,7 @@ public class MMNode private final MMNode _m2; private final MatrixBlock _data; private final DataCharacteristics _mc; - private Object _synops = null; + private SparsityInformation _sparsityInfo = null; private final OpCode _op; private final long[] _misc; @@ -83,7 +83,7 @@ public void reset() { _m1.reset(); if( _m2 != null ) _m2.reset(); - _synops = null; + _sparsityInfo = null; } public int getRows() { @@ -128,12 +128,12 @@ public MatrixBlock getData() { return _data; } - public void setSynopsis(Object obj) { - _synops = obj; + public void setSparsityInformation(SparsityInformation spInf) { + _sparsityInfo = spInf; } - public Object getSynopsis() { - return _synops; + public SparsityInformation getSparsityInformation() { + return _sparsityInfo; } public OpCode getOp() { 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 f527b127422..86f5cdef3cb 100644 --- a/src/main/java/org/apache/sysds/hops/estim/SparsityEstimator.java +++ b/src/main/java/org/apache/sysds/hops/estim/SparsityEstimator.java @@ -23,9 +23,8 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.sysds.hops.HopsException; +import org.apache.sysds.hops.estim.EstimatorBasicAvg.ScalarSparsity; import org.apache.sysds.runtime.matrix.data.MatrixBlock; -import org.apache.sysds.runtime.meta.DataCharacteristics; -import org.apache.sysds.runtime.meta.MatrixCharacteristics; public abstract class SparsityEstimator { @@ -40,12 +39,20 @@ public abstract class SparsityEstimator OpCode.EQZERO, OpCode.NEQZERO, OpCode.CBIND, OpCode.RBIND, OpCode.TRANS, OpCode.DIAG, OpCode.RESHAPE }; - + + private static OpCode[] BINARY_OPS = new OpCode[] { + OpCode.MM, OpCode.MULT, OpCode.PLUS, OpCode.CBIND, OpCode.RBIND, + }; + public static enum OpCode { MM, MULT, PLUS, EQZERO, NEQZERO, CBIND, RBIND, TRANS, DIAG, RESHAPE; + + public boolean isBinaryOp() { + return ArrayUtils.contains(BINARY_OPS, this); + } } /** @@ -53,9 +60,9 @@ public static enum OpCode { * for the given operator graph of a single root node. * * @param root DAG root node - * @return output data characteristics + * @return output sparsity */ - public abstract DataCharacteristics estim(MMNode root); + public abstract SparsityInformation estim(MMNode root); /** @@ -97,28 +104,28 @@ protected boolean isExactMetadataOp(OpCode op, int clen) { && (op != OpCode.DIAG || clen == 1); } - protected DataCharacteristics estimExactMetaData(DataCharacteristics dc1, DataCharacteristics dc2, OpCode op) { + protected ScalarSparsity estimExactMetaData(SparsityInformation ssp1, SparsityInformation ssp2, OpCode op) { switch( op ) { case EQZERO: - return new MatrixCharacteristics(dc1.getRows(), dc1.getCols(), - dc1.getRows() * dc1.getCols() - dc1.getNonZeros()); + return new ScalarSparsity(ssp1.getNumRows(), ssp1.getNumColumns(), + ssp1.getNumRows() * ssp1.getNumColumns() - ssp1.getNonZeros()); case DIAG: - return (dc1.getCols() == 1) ? - new MatrixCharacteristics(dc1.getRows(), dc1.getRows(), dc1.getNonZeros()) : - new MatrixCharacteristics(dc1.getRows(), 1, Math.min(dc1.getRows(), dc1.getNonZeros())); + return (ssp1.getNumColumns() == 1) ? + new ScalarSparsity(ssp1.getNumRows(), ssp1.getNumRows(), ssp1.getNonZeros()) : + new ScalarSparsity(ssp1.getNumRows(), 1, Math.min(ssp1.getNumRows(), ssp1.getNonZeros())); // binary operations that preserve sparsity exactly case CBIND: - return new MatrixCharacteristics(dc1.getRows(), - dc1.getCols() + dc2.getCols(), dc1.getNonZeros() + dc2.getNonZeros()); + return new ScalarSparsity(ssp1.getNumRows(), + ssp1.getNumColumns() + ssp2.getNumColumns(), ssp1.getNonZeros() + ssp2.getNonZeros()); case RBIND: - return new MatrixCharacteristics(dc1.getRows() + dc2.getRows(), - dc1.getCols(), dc1.getNonZeros() + dc2.getNonZeros()); + return new ScalarSparsity(ssp1.getNumRows() + ssp2.getNumRows(), + ssp1.getNumColumns(), ssp1.getNonZeros() + ssp2.getNonZeros()); case TRANS: - return new MatrixCharacteristics(dc1.getCols(), dc1.getRows(), dc1.getNonZeros()); + return new ScalarSparsity(ssp1.getNumColumns(), ssp1.getNumRows(), ssp1.getNonZeros()); // unary operation that preserve sparsity exactly case NEQZERO: case RESHAPE: - return dc1; + return new ScalarSparsity(ssp1.getNumRows(), ssp1.getNumColumns(), ssp1.getNonZeros()); default: throw new HopsException("Opcode is not an exact meta data operation: "+op.name()); } diff --git a/src/main/java/org/apache/sysds/hops/estim/SparsityInformation.java b/src/main/java/org/apache/sysds/hops/estim/SparsityInformation.java new file mode 100644 index 00000000000..a3e2a8c1a5b --- /dev/null +++ b/src/main/java/org/apache/sysds/hops/estim/SparsityInformation.java @@ -0,0 +1,57 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.sysds.hops.estim; + +import org.apache.sysds.runtime.matrix.data.MatrixBlock; + +public abstract class SparsityInformation { + protected MatrixBlock _data = null; + protected final int _rlen; + protected final int _clen; + + public SparsityInformation(int rlen, int clen) { + _rlen = rlen; + _clen = clen; + } + + public SparsityInformation(MatrixBlock mb) { + _data = mb; + _rlen = mb.getNumRows(); + _clen = mb.getNumColumns(); + } + + public void setData(MatrixBlock mb) { + _data = mb; + } + + public int getNumRows() { + return _rlen; + } + + public int getNumColumns() { + return _clen; + } + + public abstract long getNonZeros(); + + public double getAvgSparsity() { + return (double)getNonZeros() / (getNumRows()*getNumColumns()); + } +} diff --git a/src/test/java/org/apache/sysds/test/component/estim/OpBindChainTest.java b/src/test/java/org/apache/sysds/test/component/estim/OpBindChainTest.java index 9626f9eb74f..9fb984a16d7 100644 --- a/src/test/java/org/apache/sysds/test/component/estim/OpBindChainTest.java +++ b/src/test/java/org/apache/sysds/test/component/estim/OpBindChainTest.java @@ -171,7 +171,7 @@ private void runSparsityEstimateTest(SparsityEstimator estim, OpCode op) { } MatrixBlock m5 = m3.aggregateBinaryOperations(m3, m4, new MatrixBlock(), InstructionUtils.getMatMultOperator(1)); - double est = estim.estim(new MMNode(new MMNode(new MMNode(m1), new MMNode(m2), op), new MMNode(m4), OpCode.MM)).getSparsity(); + double est = estim.estim(new MMNode(new MMNode(new MMNode(m1), new MMNode(m2), op), new MMNode(m4), OpCode.MM)).getAvgSparsity(); //compare estimated and real sparsity TestUtils.compareScalars(est, m5.getSparsity(), (estim instanceof EstimatorBasicWorst) ? 5e-1 : diff --git a/src/test/java/org/apache/sysds/test/component/estim/OpElemWChainTest.java b/src/test/java/org/apache/sysds/test/component/estim/OpElemWChainTest.java index da18067867b..ebc98bbf16f 100644 --- a/src/test/java/org/apache/sysds/test/component/estim/OpElemWChainTest.java +++ b/src/test/java/org/apache/sysds/test/component/estim/OpElemWChainTest.java @@ -168,7 +168,7 @@ private void runSparsityEstimateTest(SparsityEstimator estim, OpCode op) { m1.binaryOperations(bOp, m2, m4); MatrixBlock m5 = m4.aggregateBinaryOperations(m4, m3, new MatrixBlock(), InstructionUtils.getMatMultOperator(1)); - double est = estim.estim(new MMNode(new MMNode(new MMNode(m1), new MMNode(m2), op), new MMNode(m3), OpCode.MM)).getSparsity(); + double est = estim.estim(new MMNode(new MMNode(new MMNode(m1), new MMNode(m2), op), new MMNode(m3), OpCode.MM)).getAvgSparsity(); //compare estimated and real sparsity TestUtils.compareScalars(est, m5.getSparsity(), (estim instanceof EstimatorBasicWorst) ? 9e-1 : diff --git a/src/test/java/org/apache/sysds/test/component/estim/SquaredProductChainTest.java b/src/test/java/org/apache/sysds/test/component/estim/SquaredProductChainTest.java index a2b04b34df1..6d69d53247a 100644 --- a/src/test/java/org/apache/sysds/test/component/estim/SquaredProductChainTest.java +++ b/src/test/java/org/apache/sysds/test/component/estim/SquaredProductChainTest.java @@ -134,7 +134,7 @@ private void runSparsityEstimateTest(SparsityEstimator estim) { //compare estimated and real sparsity double est = estim.estim(new MMNode(new MMNode(new MMNode(m1), new MMNode(m2), - OpCode.MM), new MMNode(m3), OpCode.MM)).getSparsity(); + OpCode.MM), new MMNode(m3), OpCode.MM)).getAvgSparsity(); TestUtils.compareScalars(est, m5.getSparsity(), (estim instanceof EstimatorBitsetMM) ? 0 : //exact (estim instanceof EstimatorBasicWorst) ? 1.0 : 1e-4); diff --git a/src/test/java/org/apache/sysds/test/functions/rewrite/SparsityOptimizerTest.java b/src/test/java/org/apache/sysds/test/functions/rewrite/SparsityOptimizerTest.java index 48c7046da3e..c661d0c9c2b 100644 --- a/src/test/java/org/apache/sysds/test/functions/rewrite/SparsityOptimizerTest.java +++ b/src/test/java/org/apache/sysds/test/functions/rewrite/SparsityOptimizerTest.java @@ -139,7 +139,7 @@ private void testSparsityOptimization(String testName) { try { DMLConfig dmlConfig = new DMLConfig(getCurConfigFile().getPath()); dmlConfig.setTextValue(DMLConfig.SPARSITY_RECOMPILE, "true"); - dmlConfig.setTextValue(DMLConfig.SPARSITY_ESTIMATOR, "BASIC_AVG"); + dmlConfig.setTextValue(DMLConfig.SPARSITY_ESTIMATOR, "MNC"); overwriteCurrentConfig(dmlConfig); } catch(FileNotFoundException fnfe) {