use of org.apache.sysml.hops.HopsException in project incubator-systemml by apache.
the class ScriptExecutor method rewritePersistentReadsAndWrites.
/**
* Replace persistent reads and writes with transient reads and writes in
* the symbol table.
*/
protected void rewritePersistentReadsAndWrites() {
LocalVariableMap symbolTable = script.getSymbolTable();
if (symbolTable != null) {
String[] inputs = (script.getInputVariables() == null) ? new String[0] : script.getInputVariables().toArray(new String[0]);
String[] outputs = (script.getOutputVariables() == null) ? new String[0] : script.getOutputVariables().toArray(new String[0]);
RewriteRemovePersistentReadWrite rewrite = new RewriteRemovePersistentReadWrite(inputs, outputs, script.getSymbolTable());
ProgramRewriter programRewriter = new ProgramRewriter(rewrite);
try {
programRewriter.rewriteProgramHopDAGs(dmlProgram);
} catch (LanguageException e) {
throw new MLContextException("Exception occurred while rewriting persistent reads and writes", e);
} catch (HopsException e) {
throw new MLContextException("Exception occurred while rewriting persistent reads and writes", e);
}
}
}
use of org.apache.sysml.hops.HopsException in project incubator-systemml by apache.
the class RewriteMatrixMultChainOptimization method clearLinksWithinChain.
private void clearLinksWithinChain(Hop hop, ArrayList<Hop> operators) throws HopsException {
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);
}
}
use of org.apache.sysml.hops.HopsException in project incubator-systemml by apache.
the class RewriteMatrixMultChainOptimization method optimizeMMChain.
/**
* optimizeMMChain(): It optimizes the matrix multiplication chain in which
* the last Hop is "this". Step-1) Identify the chain (mmChain). (Step-2) clear all
* links among the Hops that are involved in mmChain. (Step-3) Find the
* optimal ordering (dynamic programming) (Step-4) Relink the hops in
* mmChain.
*
* @param hop high-level operator
* @throws HopsException if HopsException occurs
*/
private void optimizeMMChain(Hop hop) throws HopsException {
if (LOG.isTraceEnabled()) {
LOG.trace("MM Chain Optimization for HOP: (" + hop.getClass().getSimpleName() + ", " + hop.getHopID() + ", " + hop.getName() + ")");
}
ArrayList<Hop> mmChain = new ArrayList<Hop>();
ArrayList<Hop> mmOperators = new ArrayList<Hop>();
ArrayList<Hop> tempList;
// Step 1: Identify the chain (mmChain) & clear all links among the Hops
// that are involved in mmChain.
// Initialize mmChain with my inputs
mmOperators.add(hop);
for (Hop hi : hop.getInput()) mmChain.add(hi);
// expand each Hop in mmChain to find the entire matrix multiplication
// chain
int i = 0;
while (i < mmChain.size()) {
boolean expandable = false;
Hop h = mmChain.get(i);
if (HopRewriteUtils.isMatrixMultiply(h) && !((AggBinaryOp) hop).hasLeftPMInput() && !h.isVisited()) {
// not be expanded.
if (h.getParent().size() > 1 || inputCount(h.getParent().get(0), h) > 1) {
expandable = false;
break;
} else {
expandable = true;
}
}
h.setVisited();
if (!expandable) {
i = i + 1;
} else {
tempList = mmChain.get(i).getInput();
if (tempList.size() != 2) {
throw new HopsException(hop.printErrorLocation() + "Hops::rule_OptimizeMMChain(): AggBinary must have exactly two inputs.");
}
// add current operator to mmOperators, and its input nodes to mmChain
mmOperators.add(mmChain.get(i));
mmChain.set(i, tempList.get(0));
mmChain.add(i + 1, tempList.get(1));
}
}
// print the MMChain
if (LOG.isTraceEnabled()) {
LOG.trace("Identified MM Chain: ");
for (Hop h : mmChain) {
logTraceHop(h, 1);
}
}
if (mmChain.size() == 2) {
// If the chain size is 2, then there is nothing to optimize.
return;
} else {
// Step 2: construct dims array
double[] dimsArray = new double[mmChain.size() + 1];
boolean dimsKnown = getDimsArray(hop, mmChain, dimsArray);
if (dimsKnown) {
// Step 3: clear the links among Hops within the identified chain
clearLinksWithinChain(hop, mmOperators);
// Step 4: Find the optimal ordering via dynamic programming.
// Invoke Dynamic Programming
int size = mmChain.size();
int[][] split = mmChainDP(dimsArray, mmChain.size());
// Step 5: Relink the hops using the optimal ordering (split[][]) found from DP.
LOG.trace("Optimal MM Chain: ");
mmChainRelinkHops(mmOperators.get(0), 0, size - 1, mmChain, mmOperators, 1, split, 1);
}
}
}
use of org.apache.sysml.hops.HopsException in project incubator-systemml by apache.
the class RewriteSplitDagUnknownCSVRead method rewriteStatementBlock.
@Override
public ArrayList<StatementBlock> rewriteStatementBlock(StatementBlock sb, ProgramRewriteStatus state) throws HopsException {
ArrayList<StatementBlock> ret = new ArrayList<StatementBlock>();
//collect all unknown csv reads hops
ArrayList<Hop> cand = new ArrayList<Hop>();
collectCSVReadHopsUnknownSize(sb.get_hops(), cand);
//split hop dag on demand
if (!cand.isEmpty()) {
try {
//duplicate sb incl live variable sets
StatementBlock sb1 = new StatementBlock();
sb1.setDMLProg(sb.getDMLProg());
sb1.setAllPositions(sb.getFilename(), sb.getBeginLine(), sb.getBeginColumn(), sb.getEndLine(), sb.getEndColumn());
sb1.setLiveIn(new VariableSet());
sb1.setLiveOut(new VariableSet());
//move csv reads incl reblock to new statement block
//(and replace original persistent read with transient read)
ArrayList<Hop> sb1hops = new ArrayList<Hop>();
for (Hop c : cand) {
Hop reblock = c;
long rlen = reblock.getDim1();
long clen = reblock.getDim2();
long nnz = reblock.getNnz();
UpdateType update = c.getUpdateType();
long brlen = reblock.getRowsInBlock();
long bclen = reblock.getColsInBlock();
//create new transient read
DataOp tread = new DataOp(reblock.getName(), reblock.getDataType(), reblock.getValueType(), DataOpTypes.TRANSIENTREAD, null, rlen, clen, nnz, update, brlen, bclen);
HopRewriteUtils.copyLineNumbers(reblock, tread);
//replace reblock with transient read
ArrayList<Hop> parents = new ArrayList<Hop>(reblock.getParent());
for (int i = 0; i < parents.size(); i++) {
Hop parent = parents.get(i);
HopRewriteUtils.replaceChildReference(parent, reblock, tread);
}
//add reblock sub dag to first statement block
DataOp twrite = new DataOp(reblock.getName(), reblock.getDataType(), reblock.getValueType(), reblock, DataOpTypes.TRANSIENTWRITE, null);
twrite.setOutputParams(rlen, clen, nnz, update, brlen, bclen);
HopRewriteUtils.copyLineNumbers(reblock, twrite);
sb1hops.add(twrite);
//update live in and out of new statement block (for piggybacking)
DataIdentifier diVar = sb.variablesRead().getVariable(reblock.getName());
if (diVar != null) {
//var read should always exist because persistent read
sb1.liveOut().addVariable(reblock.getName(), new DataIdentifier(diVar));
sb.liveIn().addVariable(reblock.getName(), new DataIdentifier(diVar));
}
}
sb1.set_hops(sb1hops);
sb1.updateRecompilationFlag();
//statement block with csv reblocks
ret.add(sb1);
//statement block with remaining hops
ret.add(sb);
} catch (Exception ex) {
throw new HopsException("Failed to split hops dag for csv read with unknown size.", ex);
}
LOG.debug("Applied splitDagUnknownCSVRead.");
} else //keep original hop dag
{
ret.add(sb);
}
return ret;
}
use of org.apache.sysml.hops.HopsException in project incubator-systemml by apache.
the class FunctionCallGraph method constructFunctionCallGraph.
private void constructFunctionCallGraph(DMLProgram prog) {
if (!prog.hasFunctionStatementBlocks())
//early abort if prog without functions
return;
try {
Stack<String> fstack = new Stack<String>();
HashSet<String> lfset = new HashSet<String>();
_fGraph.put(MAIN_FUNCTION_KEY, new HashSet<String>());
for (StatementBlock sblk : prog.getStatementBlocks()) rConstructFunctionCallGraph(MAIN_FUNCTION_KEY, sblk, fstack, lfset);
} catch (HopsException ex) {
throw new RuntimeException(ex);
}
}
Aggregations