Repository: systemml
Updated Branches:
  refs/heads/master 532da1bc5 -> 4ad8f7742


[SYSTEMML-1776] Collapse subsequent assignvar and rmvar instructions

This patch adds a minor cleanup step to the instruction generation,
which collapses subsequent assignvar and rmvar instructions, in order to
simplify debugging and reduce unnecessary interpretation overhead:

Example: assignvar s1 s2, rmvar s1 -> mvvar s1 s2


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

Branch: refs/heads/master
Commit: 4ad8f7742e0829998414180d49a4aa5cabc1e669
Parents: 532da1b
Author: Matthias Boehm <[email protected]>
Authored: Mon Jul 17 22:10:53 2017 -0700
Committer: Matthias Boehm <[email protected]>
Committed: Mon Jul 17 22:10:53 2017 -0700

----------------------------------------------------------------------
 .../java/org/apache/sysml/lops/compile/Dag.java | 51 ++++++++++++++++++++
 .../instructions/cp/VariableCPInstruction.java  | 26 ++++++----
 2 files changed, 68 insertions(+), 9 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/systemml/blob/4ad8f774/src/main/java/org/apache/sysml/lops/compile/Dag.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/sysml/lops/compile/Dag.java 
b/src/main/java/org/apache/sysml/lops/compile/Dag.java
index f108d2f..ddee17d 100644
--- a/src/main/java/org/apache/sysml/lops/compile/Dag.java
+++ b/src/main/java/org/apache/sysml/lops/compile/Dag.java
@@ -24,6 +24,7 @@ import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.HashMap;
 import java.util.HashSet;
+import java.util.Iterator;
 
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
@@ -3816,9 +3817,59 @@ public class Dag<N extends Lop>
                return false;
        }
        
+       /**
+        * Performs various cleanups on the list of instructions in order to 
reduce the
+        * number of instructions to simply debugging and reduce interpretation 
overhead. 
+        * 
+        * @param insts list of instructions
+        * @return new list of potentially modified instructions
+        * @throws DMLRuntimeException in case of instruction parsing errors
+        */
        private static ArrayList<Instruction> 
cleanupInstructions(ArrayList<Instruction> insts) 
                throws DMLRuntimeException 
        {
+               //step 1: create mvvar instructions: assignvar s1 s2, rmvar s1 
-> mvvar s1 s2
+               ArrayList<Instruction> tmp1 = 
collapseAssignvarAndRmvarInstructions(insts);
+               
+               //step 2: create packed rmvar instructions: rmvar m1, rmvar m2 
-> rmvar m1 m2
+               ArrayList<Instruction> tmp2 = 
createPackedRmvarInstructions(tmp1);
+               
+               return tmp2;
+       }
+       
+       private static ArrayList<Instruction> 
collapseAssignvarAndRmvarInstructions(ArrayList<Instruction> insts) 
+               throws DMLRuntimeException 
+       {
+               ArrayList<Instruction> ret = new ArrayList<Instruction>();
+               Iterator<Instruction> iter = insts.iterator();
+               while( iter.hasNext() ) {
+                       Instruction inst = iter.next();
+                       if( iter.hasNext() && inst instanceof 
VariableCPInstruction
+                               && 
((VariableCPInstruction)inst).isAssignVariable() ) {
+                               VariableCPInstruction inst1 = 
(VariableCPInstruction) inst;
+                               Instruction inst2 = iter.next();
+                               if( inst2 instanceof VariableCPInstruction
+                                       && 
((VariableCPInstruction)inst2).isRemoveVariableNoFile()
+                                       && inst1.getInput1().getName().equals(
+                                               
((VariableCPInstruction)inst2).getInput1().getName()) ) {
+                                       
ret.add(VariableCPInstruction.prepareMoveInstruction(
+                                               inst1.getInput1().getName(), 
inst1.getInput2().getName()));
+                               }
+                               else {
+                                       ret.add(inst1);
+                                       ret.add(inst2);
+                               }
+                       }
+                       else {
+                               ret.add(inst);
+                       }
+               }
+               return ret;
+       }
+       
+       private static ArrayList<Instruction> 
createPackedRmvarInstructions(ArrayList<Instruction> insts) 
+               throws DMLRuntimeException 
+       {
                ArrayList<Instruction> ret = new ArrayList<Instruction>();
                ArrayList<String> currRmVar = new ArrayList<String>();
                for( Instruction inst : insts ) {

http://git-wip-us.apache.org/repos/asf/systemml/blob/4ad8f774/src/main/java/org/apache/sysml/runtime/instructions/cp/VariableCPInstruction.java
----------------------------------------------------------------------
diff --git 
a/src/main/java/org/apache/sysml/runtime/instructions/cp/VariableCPInstruction.java
 
b/src/main/java/org/apache/sysml/runtime/instructions/cp/VariableCPInstruction.java
index 816d026..3b9ef66 100644
--- 
a/src/main/java/org/apache/sysml/runtime/instructions/cp/VariableCPInstruction.java
+++ 
b/src/main/java/org/apache/sysml/runtime/instructions/cp/VariableCPInstruction.java
@@ -228,6 +228,10 @@ public class VariableCPInstruction extends CPInstruction
                        || opcode == 
VariableOperationCode.RemoveVariableAndFile);
        }
        
+       public boolean isAssignVariable() {
+               return (opcode == VariableOperationCode.AssignVariable);
+       }
+       
        public FileFormatProperties getFormatProperties() {
                return _formatProperties;
        }
@@ -707,15 +711,19 @@ public class VariableCPInstruction extends CPInstruction
                        // get source variable 
                        Data srcData = ec.getVariable(getInput1().getName());   
        
                                
-                       if ( srcData == null ) 
-                               throw new DMLRuntimeException("Unexpected 
error: could not find a data object for variable name:" + getInput1().getName() 
+ ", while processing instruction " +this.toString());
-                               
-                       // remove existing variable bound to target name
-                       Data tgt = ec.removeVariable(getInput2().getName());
-                               
-                       //cleanup matrix data on fs/hdfs (if necessary)
-                       if ( tgt != null && tgt instanceof MatrixObject ) {
-                               ec.cleanupMatrixObject((MatrixObject) tgt);
+                       if ( srcData == null ) {
+                               throw new DMLRuntimeException("Unexpected 
error: could not find a data object "
+                                       + "for variable name:" + 
getInput1().getName() + ", while processing instruction ");
+                       }
+                       
+                       if( getInput2().getDataType().isMatrix() ) {
+                               // remove existing variable bound to target name
+                               Data tgt = 
ec.removeVariable(getInput2().getName());
+                                       
+                               //cleanup matrix data on fs/hdfs (if necessary)
+                               if ( tgt != null && tgt instanceof MatrixObject 
) {
+                                       ec.cleanupMatrixObject((MatrixObject) 
tgt);
+                               }
                        }
                        
                        // do the actual move

Reply via email to