Search in sources :

Example 16 with ReorgOp

use of org.apache.sysml.hops.ReorgOp in project incubator-systemml by apache.

the class Recompiler method rUpdateStatistics.

public static void rUpdateStatistics(Hop hop, LocalVariableMap vars) {
    if (hop.isVisited())
        return;
    // recursively process children
    if (hop.getInput() != null)
        for (Hop c : hop.getInput()) rUpdateStatistics(c, vars);
    boolean updatedSizeExpr = false;
    // (with awareness not to override persistent reads to an existing name)
    if (hop instanceof DataOp && ((DataOp) hop).getDataOpType() != DataOpTypes.PERSISTENTREAD) {
        DataOp d = (DataOp) hop;
        String varName = d.getName();
        if (vars.keySet().contains(varName)) {
            Data dat = vars.get(varName);
            if (dat instanceof MatrixObject) {
                MatrixObject mo = (MatrixObject) dat;
                d.setDim1(mo.getNumRows());
                d.setDim2(mo.getNumColumns());
                d.setNnz(mo.getNnz());
            } else if (dat instanceof FrameObject) {
                FrameObject fo = (FrameObject) dat;
                d.setDim1(fo.getNumRows());
                d.setDim2(fo.getNumColumns());
            }
        }
    } else // special case for persistent reads with unknown size (read-after-write)
    if (hop instanceof DataOp && ((DataOp) hop).getDataOpType() == DataOpTypes.PERSISTENTREAD && !hop.dimsKnown() && ((DataOp) hop).getInputFormatType() != FileFormatTypes.CSV && !ConfigurationManager.getCompilerConfigFlag(ConfigType.IGNORE_READ_WRITE_METADATA)) {
        // update hop with read meta data
        DataOp dop = (DataOp) hop;
        tryReadMetaDataFileMatrixCharacteristics(dop);
    } else // update size expression for rand/seq according to symbol table entries
    if (hop instanceof DataGenOp) {
        DataGenOp d = (DataGenOp) hop;
        HashMap<String, Integer> params = d.getParamIndexMap();
        if (d.getOp() == DataGenMethod.RAND || d.getOp() == DataGenMethod.SINIT || d.getOp() == DataGenMethod.SAMPLE) {
            boolean initUnknown = !d.dimsKnown();
            int ix1 = params.get(DataExpression.RAND_ROWS);
            int ix2 = params.get(DataExpression.RAND_COLS);
            // update rows/cols by evaluating simple expression of literals, nrow, ncol, scalars, binaryops
            HashMap<Long, Long> memo = new HashMap<>();
            d.refreshRowsParameterInformation(d.getInput().get(ix1), vars, memo);
            d.refreshColsParameterInformation(d.getInput().get(ix2), vars, memo);
            updatedSizeExpr = initUnknown & d.dimsKnown();
        } else if (d.getOp() == DataGenMethod.SEQ) {
            boolean initUnknown = !d.dimsKnown();
            int ix1 = params.get(Statement.SEQ_FROM);
            int ix2 = params.get(Statement.SEQ_TO);
            int ix3 = params.get(Statement.SEQ_INCR);
            HashMap<Long, Double> memo = new HashMap<>();
            double from = d.computeBoundsInformation(d.getInput().get(ix1), vars, memo);
            double to = d.computeBoundsInformation(d.getInput().get(ix2), vars, memo);
            double incr = d.computeBoundsInformation(d.getInput().get(ix3), vars, memo);
            // special case increment
            if (from != Double.MAX_VALUE && to != Double.MAX_VALUE) {
                incr *= ((from > to && incr > 0) || (from < to && incr < 0)) ? -1.0 : 1.0;
            }
            if (from != Double.MAX_VALUE && to != Double.MAX_VALUE && incr != Double.MAX_VALUE) {
                d.setDim1(UtilFunctions.getSeqLength(from, to, incr));
                d.setDim2(1);
                d.setIncrementValue(incr);
            }
            updatedSizeExpr = initUnknown & d.dimsKnown();
        } else {
            throw new DMLRuntimeException("Unexpected data generation method: " + d.getOp());
        }
    } else // update size expression for reshape according to symbol table entries
    if (hop instanceof ReorgOp && ((ReorgOp) (hop)).getOp() == Hop.ReOrgOp.RESHAPE) {
        ReorgOp d = (ReorgOp) hop;
        boolean initUnknown = !d.dimsKnown();
        HashMap<Long, Long> memo = new HashMap<>();
        d.refreshRowsParameterInformation(d.getInput().get(1), vars, memo);
        d.refreshColsParameterInformation(d.getInput().get(2), vars, memo);
        updatedSizeExpr = initUnknown & d.dimsKnown();
    } else // update size expression for indexing according to symbol table entries
    if (hop instanceof IndexingOp) {
        IndexingOp iop = (IndexingOp) hop;
        // inpRowL
        Hop input2 = iop.getInput().get(1);
        // inpRowU
        Hop input3 = iop.getInput().get(2);
        // inpColL
        Hop input4 = iop.getInput().get(3);
        // inpColU
        Hop input5 = iop.getInput().get(4);
        boolean initUnknown = !iop.dimsKnown();
        HashMap<Long, Double> memo = new HashMap<>();
        double rl = iop.computeBoundsInformation(input2, vars, memo);
        double ru = iop.computeBoundsInformation(input3, vars, memo);
        double cl = iop.computeBoundsInformation(input4, vars, memo);
        double cu = iop.computeBoundsInformation(input5, vars, memo);
        if (rl != Double.MAX_VALUE && ru != Double.MAX_VALUE)
            iop.setDim1((long) (ru - rl + 1));
        if (cl != Double.MAX_VALUE && cu != Double.MAX_VALUE)
            iop.setDim2((long) (cu - cl + 1));
        updatedSizeExpr = initUnknown & iop.dimsKnown();
    }
    // without overwriting inferred size expressions
    if (!updatedSizeExpr) {
        hop.refreshSizeInformation();
    }
    hop.setVisited();
}
Also used : MatrixObject(org.apache.sysml.runtime.controlprogram.caching.MatrixObject) HashMap(java.util.HashMap) Hop(org.apache.sysml.hops.Hop) CacheableData(org.apache.sysml.runtime.controlprogram.caching.CacheableData) Data(org.apache.sysml.runtime.instructions.cp.Data) FrameObject(org.apache.sysml.runtime.controlprogram.caching.FrameObject) DMLRuntimeException(org.apache.sysml.runtime.DMLRuntimeException) IndexingOp(org.apache.sysml.hops.IndexingOp) DataGenOp(org.apache.sysml.hops.DataGenOp) ReorgOp(org.apache.sysml.hops.ReorgOp) DataOp(org.apache.sysml.hops.DataOp)

Example 17 with ReorgOp

use of org.apache.sysml.hops.ReorgOp in project incubator-systemml by apache.

the class HopRewriteUtils method createReorg.

public static ReorgOp createReorg(ArrayList<Hop> inputs, ReOrgOp rop) {
    Hop main = inputs.get(0);
    ReorgOp reorg = new ReorgOp(main.getName(), main.getDataType(), main.getValueType(), rop, inputs);
    reorg.setOutputBlocksizes(main.getRowsInBlock(), main.getColsInBlock());
    copyLineNumbers(main, reorg);
    reorg.refreshSizeInformation();
    return reorg;
}
Also used : Hop(org.apache.sysml.hops.Hop) ReorgOp(org.apache.sysml.hops.ReorgOp)

Example 18 with ReorgOp

use of org.apache.sysml.hops.ReorgOp in project incubator-systemml by apache.

the class RewriteAlgebraicSimplificationDynamic method removeUnnecessaryReorgOperation.

private static Hop removeUnnecessaryReorgOperation(Hop parent, Hop hi, int pos) {
    if (hi instanceof ReorgOp) {
        ReorgOp rop = (ReorgOp) hi;
        Hop input = hi.getInput().get(0);
        boolean apply = false;
        // equal dims of reshape input and output -> no need for reshape because
        // byrow always refers to both input/output and hence gives the same result
        apply |= (rop.getOp() == ReOrgOp.RESHAPE && HopRewriteUtils.isEqualSize(hi, input));
        // 1x1 dimensions of transpose/reshape -> no need for reorg
        apply |= ((rop.getOp() == ReOrgOp.TRANSPOSE || rop.getOp() == ReOrgOp.RESHAPE) && rop.getDim1() == 1 && rop.getDim2() == 1);
        if (apply) {
            HopRewriteUtils.replaceChildReference(parent, hi, input, pos);
            hi = input;
            LOG.debug("Applied removeUnnecessaryReorg.");
        }
    }
    return hi;
}
Also used : ReorgOp(org.apache.sysml.hops.ReorgOp) Hop(org.apache.sysml.hops.Hop)

Example 19 with ReorgOp

use of org.apache.sysml.hops.ReorgOp in project incubator-systemml by apache.

the class RewriteAlgebraicSimplificationDynamic method simplifySumDiagToTrace.

private static Hop simplifySumDiagToTrace(Hop hi) {
    if (hi instanceof AggUnaryOp) {
        AggUnaryOp au = (AggUnaryOp) hi;
        if (// sum
        au.getOp() == AggOp.SUM && au.getDirection() == Direction.RowCol) {
            Hop hi2 = au.getInput().get(0);
            if (// diagM2V
            hi2 instanceof ReorgOp && ((ReorgOp) hi2).getOp() == ReOrgOp.DIAG && hi2.getDim2() == 1) {
                Hop hi3 = hi2.getInput().get(0);
                // remove diag operator
                HopRewriteUtils.replaceChildReference(au, hi2, hi3, 0);
                HopRewriteUtils.cleanupUnreferenced(hi2);
                // change sum to trace
                au.setOp(AggOp.TRACE);
                LOG.debug("Applied simplifySumDiagToTrace");
            }
        }
    }
    return hi;
}
Also used : AggUnaryOp(org.apache.sysml.hops.AggUnaryOp) Hop(org.apache.sysml.hops.Hop) ReorgOp(org.apache.sysml.hops.ReorgOp)

Example 20 with ReorgOp

use of org.apache.sysml.hops.ReorgOp in project incubator-systemml by apache.

the class RewriteAlgebraicSimplificationDynamic method simplifyRowSumsMVMult.

private static Hop simplifyRowSumsMVMult(Hop parent, Hop hi, int pos) {
    // removed by other rewrite if unnecessary, i.e., if Y==t(Z)
    if (hi instanceof AggUnaryOp) {
        AggUnaryOp uhi = (AggUnaryOp) hi;
        Hop input = uhi.getInput().get(0);
        if (// rowsums
        uhi.getOp() == AggOp.SUM && uhi.getDirection() == Direction.Row && // b(*)
        HopRewriteUtils.isBinary(input, OpOp2.MULT)) {
            Hop left = input.getInput().get(0);
            Hop right = input.getInput().get(1);
            if (left.getDim1() > 1 && left.getDim2() > 1 && right.getDim1() == 1 && // MV (row vector)
            right.getDim2() > 1) {
                // create new operators
                ReorgOp trans = HopRewriteUtils.createTranspose(right);
                AggBinaryOp mmult = HopRewriteUtils.createMatrixMultiply(left, trans);
                // relink new child
                HopRewriteUtils.replaceChildReference(parent, hi, mmult, pos);
                HopRewriteUtils.cleanupUnreferenced(hi, input);
                hi = mmult;
                LOG.debug("Applied simplifyRowSumsMVMult");
            }
        }
    }
    return hi;
}
Also used : AggUnaryOp(org.apache.sysml.hops.AggUnaryOp) AggBinaryOp(org.apache.sysml.hops.AggBinaryOp) Hop(org.apache.sysml.hops.Hop) ReorgOp(org.apache.sysml.hops.ReorgOp)

Aggregations

ReorgOp (org.apache.sysml.hops.ReorgOp)28 Hop (org.apache.sysml.hops.Hop)26 AggBinaryOp (org.apache.sysml.hops.AggBinaryOp)11 AggUnaryOp (org.apache.sysml.hops.AggUnaryOp)11 LiteralOp (org.apache.sysml.hops.LiteralOp)9 BinaryOp (org.apache.sysml.hops.BinaryOp)6 ArrayList (java.util.ArrayList)5 DataGenOp (org.apache.sysml.hops.DataGenOp)5 UnaryOp (org.apache.sysml.hops.UnaryOp)5 HashMap (java.util.HashMap)4 ParameterizedBuiltinOp (org.apache.sysml.hops.ParameterizedBuiltinOp)4 TernaryOp (org.apache.sysml.hops.TernaryOp)4 DataOp (org.apache.sysml.hops.DataOp)3 IndexingOp (org.apache.sysml.hops.IndexingOp)3 MultiThreadedHop (org.apache.sysml.hops.Hop.MultiThreadedHop)2 DMLRuntimeException (org.apache.sysml.runtime.DMLRuntimeException)2 ConvolutionOp (org.apache.sysml.hops.ConvolutionOp)1 FunctionOp (org.apache.sysml.hops.FunctionOp)1 DataGenMethod (org.apache.sysml.hops.Hop.DataGenMethod)1 OpOp2 (org.apache.sysml.hops.Hop.OpOp2)1