This is an automated email from the ASF dual-hosted git repository.

mboehm7 pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/systemds.git


The following commit(s) were added to refs/heads/main by this push:
     new b716d02bf3 [SYSTEMDS-3798] Fix rewrite loop vectorization and code 
coverage
b716d02bf3 is described below

commit b716d02bf362e1c4aa4bc8fffc0b4d3f07343d60
Author: Matthias Boehm <[email protected]>
AuthorDate: Fri Nov 29 15:41:38 2024 +0100

    [SYSTEMDS-3798] Fix rewrite loop vectorization and code coverage
    
    The existing single loop vectorization rewrite only checked for
    correct results not if the rewrite was indeed applied. This issue has
    been found while improving the code coverage of rewrites. When the
    handling of predicates (with special transient write) was changed in
    the past, these rewrites were not properly updated. This patch fixes
    all rewrites and also add tests for all cases.
    
    Furthermore, this patch also removes two other unused hop rewrites.
---
 .../apache/sysds/hops/rewrite/ProgramRewriter.java |   1 -
 .../hops/rewrite/RewriteForLoopVectorization.java  | 231 ++++++++++-----------
 .../RewriteRemoveDanglingParentReferences.java     | 110 ----------
 .../RewriteTransientWriteParentHandling.java       |  76 -------
 .../rewrite/RewriteLoopVectorization.java          |  68 ++++--
 ...ionSum.dml => RewriteLoopVectorizationBinary.R} |  18 +-
 ...nSum.dml => RewriteLoopVectorizationBinary.dml} |  11 +-
 ...m.dml => RewriteLoopVectorizationIndexedCopy.R} |  17 +-
 ...dml => RewriteLoopVectorizationIndexedCopy.dml} |  10 +-
 .../rewrite/RewriteLoopVectorizationSum.dml        |   4 +-
 ...tionSum.dml => RewriteLoopVectorizationUnary.R} |  17 +-
 ...onSum.dml => RewriteLoopVectorizationUnary.dml} |  10 +-
 12 files changed, 210 insertions(+), 363 deletions(-)

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 03633d06a8..8433a5d90b 100644
--- a/src/main/java/org/apache/sysds/hops/rewrite/ProgramRewriter.java
+++ b/src/main/java/org/apache/sysds/hops/rewrite/ProgramRewriter.java
@@ -75,7 +75,6 @@ public class ProgramRewriter{
                if( staticRewrites )
                {
                        //add static HOP DAG rewrite rules
-                       _dagRuleSet.add(     new 
RewriteTransientWriteParentHandling()       );
                        _dagRuleSet.add(     new RewriteRemoveReadAfterWrite()  
             ); //dependency: before blocksize
                        _dagRuleSet.add(     new RewriteBlockSizeAndReblock()   
             );
                        if( OptimizerUtils.ALLOW_ALGEBRAIC_SIMPLIFICATION )
diff --git 
a/src/main/java/org/apache/sysds/hops/rewrite/RewriteForLoopVectorization.java 
b/src/main/java/org/apache/sysds/hops/rewrite/RewriteForLoopVectorization.java
index 92898bb3b4..1d2223dcf9 100644
--- 
a/src/main/java/org/apache/sysds/hops/rewrite/RewriteForLoopVectorization.java
+++ 
b/src/main/java/org/apache/sysds/hops/rewrite/RewriteForLoopVectorization.java
@@ -40,6 +40,7 @@ import org.apache.sysds.common.Types.DataType;
 import org.apache.sysds.common.Types.Direction;
 import org.apache.sysds.common.Types.OpOp1;
 import org.apache.sysds.common.Types.OpOp2;
+import org.apache.sysds.common.Types.OpOpData;
 
 /**
  * Rule: Simplify program structure by pulling if or else statement body out
@@ -64,9 +65,9 @@ public class RewriteForLoopVectorization extends 
StatementBlockRewriteRule
                {
                        ForStatementBlock fsb = (ForStatementBlock) sb;
                        ForStatement fs = (ForStatement) fsb.getStatement(0);
-                       Hop from = fsb.getFromHops();
-                       Hop to = fsb.getToHops();
-                       Hop incr = fsb.getIncrementHops();
+                       Hop from = unwrap(fsb.getFromHops());
+                       Hop to = unwrap(fsb.getToHops());
+                       Hop incr = unwrap(fsb.getIncrementHops());
                        String iterVar = 
fsb.getIterPredicate().getIterVar().getName();
                        
                        if( fs.getBody()!=null && fs.getBody().size()==1 ) 
//single child block
@@ -76,11 +77,16 @@ public class RewriteForLoopVectorization extends 
StatementBlockRewriteRule
                                          || csb instanceof IfStatementBlock 
                                          || csb instanceof ForStatementBlock ) 
)
                                {
+                                       if( !(incr==null || incr instanceof 
LiteralOp 
+                                                       && 
((LiteralOp)incr).getDoubleValue()==1.0) ) {
+                                               return Arrays.asList(sb);
+                                       }
+                                       
                                        //AUTO VECTORIZATION PATTERNS
                                        //Note: unnecessary row or column 
indexing then later removed via hop rewrites
                                        
                                        //e.g., for(i in a:b){s = s + 
as.scalar(X[i,2])} -> s = sum(X[a:b,2])
-                                       sb = vectorizeScalarAggregate(sb, csb, 
from, to, incr, iterVar);  
+                                       sb = vectorizeScalarAggregate(sb, csb, 
from, to, incr, iterVar);
                                        
                                        //e.g., for(i in a:b){X[i,2] = Y[i,1] + 
Z[i,3]} -> X[a:b,2] = Y[a:b,1] + Z[a:b,3];
                                        sb = vectorizeElementwiseBinary(sb, 
csb, from, to, incr, iterVar);
@@ -90,9 +96,9 @@ public class RewriteForLoopVectorization extends 
StatementBlockRewriteRule
                                
                                        //e.g., for(i in a:b){X[7,i] = Y[1,i]} 
-> X[7,a:b] = Y[1,a:b];
                                        sb = vectorizeIndexedCopy(sb, csb, 
from, to, incr, iterVar);
-                               }       
-                       }       
-               }       
+                               }
+                       }
+               }
                
                //if no rewrite applied sb is the original for loop otherwise a 
last level statement block
                //that includes the equivalent vectorized operations.
@@ -108,40 +114,34 @@ public class RewriteForLoopVectorization extends 
StatementBlockRewriteRule
        {
                StatementBlock ret = sb;
                
-               //check missing and supported increment values
-               if( !(increment!=null && increment instanceof LiteralOp 
-                               && 
((LiteralOp)increment).getDoubleValue()==1.0) ) {
-                       return ret;
-               }
-                       
                //check for applicability
                boolean leftScalar = false;
                boolean rightScalar = false;
                boolean rowIx = false; //row or col
                
-               if( csb.getHops()!=null && csb.getHops().size()==1 ){
+               if( csb.getHops()!=null && csb.getHops().size()==1 ) {
                        Hop root = csb.getHops().get(0);
                        
-                       if( root.getDataType()==DataType.SCALAR && 
root.getInput().get(0) instanceof BinaryOp ) {
-                               BinaryOp bop = (BinaryOp) 
root.getInput().get(0);
-                               Hop left = bop.getInput().get(0);
-                               Hop right = bop.getInput().get(1);
+                       if( root.getDataType()==DataType.SCALAR && 
root.getInput(0) instanceof BinaryOp ) {
+                               BinaryOp bop = (BinaryOp) root.getInput(0);
+                               Hop left = bop.getInput(0);
+                               Hop right = bop.getInput(1);
                                
                                //check for left scalar plus
                                if( HopRewriteUtils.isValidOp(bop.getOp(), 
MAP_SCALAR_AGGREGATE_SOURCE_OPS) 
                                        && left instanceof DataOp && 
left.getDataType() == DataType.SCALAR
-                                       && 
root.getName().equals(left.getName()) 
+                                       && root.getName().equals(left.getName())
                                        && right instanceof UnaryOp && 
((UnaryOp) right).getOp() == OpOp1.CAST_AS_SCALAR
-                                       && right.getInput().get(0) instanceof 
IndexingOp )
+                                       && right.getInput(0) instanceof 
IndexingOp )
                                {
-                                       IndexingOp ix = 
(IndexingOp)right.getInput().get(0);
-                                       if( ix.isRowLowerEqualsUpper() && 
ix.getInput().get(1) instanceof DataOp
-                                               && 
ix.getInput().get(1).getName().equals(itervar) ){
+                                       IndexingOp ix = 
(IndexingOp)right.getInput(0);
+                                       if( ix.isRowLowerEqualsUpper() && 
ix.getInput(1) instanceof DataOp
+                                               && 
ix.getInput(1).getName().equals(itervar) ){
                                                leftScalar = true;
                                                rowIx = true;
                                        }
-                                       else if( ix.isColLowerEqualsUpper() && 
ix.getInput().get(3) instanceof DataOp
-                                               && 
ix.getInput().get(3).getName().equals(itervar) ){
+                                       else if( ix.isColLowerEqualsUpper() && 
ix.getInput(3) instanceof DataOp
+                                               && 
ix.getInput(3).getName().equals(itervar) ){
                                                leftScalar = true;
                                                rowIx = false;
                                        }
@@ -151,16 +151,16 @@ public class RewriteForLoopVectorization extends 
StatementBlockRewriteRule
                                        && right instanceof DataOp && 
right.getDataType() == DataType.SCALAR
                                        && 
root.getName().equals(right.getName()) 
                                        && left instanceof UnaryOp && 
((UnaryOp) left).getOp() == OpOp1.CAST_AS_SCALAR
-                                       && left.getInput().get(0) instanceof 
IndexingOp )
+                                       && left.getInput(0) instanceof 
IndexingOp )
                                {
-                                       IndexingOp ix = 
(IndexingOp)left.getInput().get(0);
-                                       if( ix.isRowLowerEqualsUpper() && 
ix.getInput().get(1) instanceof DataOp
-                                               && 
ix.getInput().get(1).getName().equals(itervar) ){
+                                       IndexingOp ix = 
(IndexingOp)left.getInput(0);
+                                       if( ix.isRowLowerEqualsUpper() && 
ix.getInput(1) instanceof DataOp
+                                               && 
ix.getInput(1).getName().equals(itervar) ){
                                                rightScalar = true;
                                                rowIx = true;
                                        }
-                                       else if( ix.isColLowerEqualsUpper() && 
ix.getInput().get(3) instanceof DataOp
-                                               && 
ix.getInput().get(3).getName().equals(itervar) ){
+                                       else if( ix.isColLowerEqualsUpper() && 
ix.getInput(3) instanceof DataOp
+                                               && 
ix.getInput(3).getName().equals(itervar) ){
                                                rightScalar = true;
                                                rowIx = false;
                                        }
@@ -169,12 +169,11 @@ public class RewriteForLoopVectorization extends 
StatementBlockRewriteRule
                }
                
                //apply rewrite if possible
-               if( leftScalar || rightScalar ) 
-               {
+               if( leftScalar || rightScalar ) {
                        Hop root = csb.getHops().get(0);
-                       BinaryOp bop = (BinaryOp) root.getInput().get(0);
+                       BinaryOp bop = (BinaryOp) root.getInput(0);
                        Hop cast = bop.getInput().get( leftScalar?1:0 );
-                       Hop ix = cast.getInput().get(0);
+                       Hop ix = cast.getInput(0);
                        int aggOpPos = 
HopRewriteUtils.getValidOpPos(bop.getOp(), MAP_SCALAR_AGGREGATE_SOURCE_OPS);
                        AggOp aggOp = MAP_SCALAR_AGGREGATE_TARGET_OPS[aggOpPos];
                        
@@ -195,8 +194,9 @@ public class RewriteForLoopVectorization extends 
StatementBlockRewriteRule
                        if( rowIx )
                                ((IndexingOp)ix).setRowLowerEqualsUpper(false);
                        else
-                               ((IndexingOp)ix).setColLowerEqualsUpper(false); 
+                               ((IndexingOp)ix).setColLowerEqualsUpper(false);
                        ix.refreshSizeInformation();
+                       Hop.resetVisitStatus(csb.getHops(), true);
                        
                        ret = csb;
                        LOG.debug("Applied vectorizeScalarSumForLoop.");
@@ -208,77 +208,70 @@ public class RewriteForLoopVectorization extends 
StatementBlockRewriteRule
        private static StatementBlock vectorizeElementwiseBinary( 
StatementBlock sb, StatementBlock csb, Hop from, Hop to, Hop increment, String 
itervar ) 
        {
                StatementBlock ret = sb;
-               
-               //check supported increment values
-               if( !(increment instanceof LiteralOp && 
((LiteralOp)increment).getDoubleValue()==1.0) ){
-                       return ret;
-               }
-                       
+
                //check for applicability
                boolean apply = false;
                boolean rowIx = false; //row or col
-               if( csb.getHops()!=null && csb.getHops().size()==1 )
-               {
+               if( csb.getHops()!=null && csb.getHops().size()==1 ) {
                        Hop root = csb.getHops().get(0);
-                       
-                       if( root.getDataType()==DataType.MATRIX && 
root.getInput().get(0) instanceof LeftIndexingOp )
-                       {
-                               LeftIndexingOp lix = (LeftIndexingOp) 
root.getInput().get(0);
-                               Hop lixlhs = lix.getInput().get(0);
-                               Hop lixrhs = lix.getInput().get(1);
-                               
+
+                       if( root.getDataType()==DataType.MATRIX && 
root.getInput(0) instanceof LeftIndexingOp ) {
+                               LeftIndexingOp lix = (LeftIndexingOp) 
root.getInput(0);
+                               Hop lixlhs = lix.getInput(0);
+                               Hop lixrhs = lix.getInput(1);
+
                                if( lixlhs instanceof DataOp && lixrhs 
instanceof BinaryOp
-                                       && lixrhs.getInput().get(0) instanceof 
IndexingOp       
-                                       && lixrhs.getInput().get(1) instanceof 
IndexingOp
-                                       && 
lixrhs.getInput().get(0).getInput().get(0) instanceof DataOp
-                                       && 
lixrhs.getInput().get(1).getInput().get(0) instanceof DataOp)
-                               {                       
-                                       IndexingOp rix0 = (IndexingOp) 
lixrhs.getInput().get(0);
-                                       IndexingOp rix1 = (IndexingOp) 
lixrhs.getInput().get(1);
-                                       
+                                       && lixrhs.getInput(0) instanceof 
IndexingOp
+                                       && lixrhs.getInput(1) instanceof 
IndexingOp
+                                       && lixrhs.getInput(0).getInput(0) 
instanceof DataOp
+                                       && lixrhs.getInput(1).getInput(0) 
instanceof DataOp)
+                               {
+                                       IndexingOp rix0 = (IndexingOp) 
lixrhs.getInput(0);
+                                       IndexingOp rix1 = (IndexingOp) 
lixrhs.getInput(1);
+
                                        //check for rowwise
                                        if(    lix.isRowLowerEqualsUpper() && 
rix0.isRowLowerEqualsUpper() && rix1.isRowLowerEqualsUpper() 
-                                               && 
lix.getInput().get(2).getName().equals(itervar)
-                                               && 
rix0.getInput().get(1).getName().equals(itervar)
-                                               && 
rix1.getInput().get(1).getName().equals(itervar))
+                                               && 
lix.getInput(2).getName().equals(itervar)
+                                               && 
rix0.getInput(1).getName().equals(itervar)
+                                               && 
rix1.getInput(1).getName().equals(itervar))
                                        {
                                                apply = true;
                                                rowIx = true;
                                        }
                                        //check for colwise
                                        if(    lix.isColLowerEqualsUpper() && 
rix0.isColLowerEqualsUpper() && rix1.isColLowerEqualsUpper() 
-                                               && 
lix.getInput().get(4).getName().equals(itervar)
-                                               && 
rix0.getInput().get(3).getName().equals(itervar)
-                                               && 
rix1.getInput().get(3).getName().equals(itervar))
+                                               && 
lix.getInput(4).getName().equals(itervar)
+                                               && 
rix0.getInput(3).getName().equals(itervar)
+                                               && 
rix1.getInput(3).getName().equals(itervar))
                                        {
                                                apply = true;
                                                rowIx = false;
                                        }
                                }
                        }
-               }       
+               }
                
                //apply rewrite if possible
-               if( apply ) 
-               {
+               if( apply ) {
                        Hop root = csb.getHops().get(0);
-                       LeftIndexingOp lix = (LeftIndexingOp) 
root.getInput().get(0);
-                       BinaryOp bop = (BinaryOp) lix.getInput().get(1);
-                       IndexingOp rix0 = (IndexingOp) bop.getInput().get(0);
-                       IndexingOp rix1 = (IndexingOp) bop.getInput().get(1);
+                       LeftIndexingOp lix = (LeftIndexingOp) root.getInput(0);
+                       BinaryOp bop = (BinaryOp) lix.getInput(1);
+                       IndexingOp rix0 = (IndexingOp) bop.getInput(0);
+                       IndexingOp rix1 = (IndexingOp) bop.getInput(1);
                        int index1 = rowIx ? 2 : 4;
                        int index2 = rowIx ? 3 : 5;
                        //modify left indexing bounds
                        HopRewriteUtils.replaceChildReference(lix, 
lix.getInput().get(index1),from, index1);
                        HopRewriteUtils.replaceChildReference(lix, 
lix.getInput().get(index2),to, index2);
                        //modify both right indexing
-                       HopRewriteUtils.replaceChildReference(rix0, 
rix0.getInput().get(index1-1), from, index1-1);
-                       HopRewriteUtils.replaceChildReference(rix0, 
rix0.getInput().get(index2-1), to, index2-1);
-                       HopRewriteUtils.replaceChildReference(rix1, 
rix1.getInput().get(index1-1), from, index1-1);
-                       HopRewriteUtils.replaceChildReference(rix1, 
rix1.getInput().get(index2-1), to, index2-1);
+                       HopRewriteUtils.replaceChildReference(rix0, 
rix0.getInput(index1-1), from, index1-1);
+                       HopRewriteUtils.replaceChildReference(rix0, 
rix0.getInput(index2-1), to, index2-1);
+                       HopRewriteUtils.replaceChildReference(rix1, 
rix1.getInput(index1-1), from, index1-1);
+                       HopRewriteUtils.replaceChildReference(rix1, 
rix1.getInput(index2-1), to, index2-1);
                        updateLeftAndRightIndexingSizes(rowIx, lix, rix0, rix1);
                        bop.refreshSizeInformation();
                        lix.refreshSizeInformation(); //after bop update
+                       Hop.resetVisitStatus(csb.getHops(), true);
                        
                        ret = csb;
                        //ret.liveIn().removeVariable(itervar);
@@ -292,53 +285,47 @@ public class RewriteForLoopVectorization extends 
StatementBlockRewriteRule
        {
                StatementBlock ret = sb;
                
-               //check supported increment values
-               if( !(increment instanceof LiteralOp && 
((LiteralOp)increment).getDoubleValue()==1.0) ){
-                       return ret;
-               }
-                       
                //check for applicability
                boolean apply = false;
                boolean rowIx = false; //row or col
-               if( csb.getHops()!=null && csb.getHops().size()==1 )
-               {
+               if( csb.getHops()!=null && csb.getHops().size()==1 ) {
                        Hop root = csb.getHops().get(0);
                        
-                       if( root.getDataType()==DataType.MATRIX && 
root.getInput().get(0) instanceof LeftIndexingOp )
-                       {
-                               LeftIndexingOp lix = (LeftIndexingOp) 
root.getInput().get(0);
-                               Hop lixlhs = lix.getInput().get(0);
-                               Hop lixrhs = lix.getInput().get(1);
+                       if( root.getDataType()==DataType.MATRIX && 
root.getInput(0) instanceof LeftIndexingOp ) {
+                               LeftIndexingOp lix = (LeftIndexingOp) 
root.getInput(0);
+                               Hop lixlhs = lix.getInput(0);
+                               Hop lixrhs = lix.getInput(1);
                                
                                if( lixlhs instanceof DataOp && lixrhs 
instanceof UnaryOp 
-                                       && lixrhs.getInput().get(0) instanceof 
IndexingOp
-                                       && 
lixrhs.getInput().get(0).getInput().get(0) instanceof DataOp )
+                                       && lixrhs.getInput(0) instanceof 
IndexingOp
+                                       && lixrhs.getInput(0).getInput(0) 
instanceof DataOp )
                                {
                                        boolean[] tmp = 
checkLeftAndRightIndexing(lix, 
-                                                       (IndexingOp) 
lixrhs.getInput().get(0), itervar);
+                                                       (IndexingOp) 
lixrhs.getInput(0), itervar);
                                        apply = tmp[0];
                                        rowIx = tmp[1];
                                }
                        }
-               }       
+               }
                
                //apply rewrite if possible
                if( apply ) {
                        Hop root = csb.getHops().get(0);
-                       LeftIndexingOp lix = (LeftIndexingOp) 
root.getInput().get(0);
-                       UnaryOp uop = (UnaryOp) lix.getInput().get(1);
-                       IndexingOp rix = (IndexingOp) uop.getInput().get(0);
+                       LeftIndexingOp lix = (LeftIndexingOp) root.getInput(0);
+                       UnaryOp uop = (UnaryOp) lix.getInput(1);
+                       IndexingOp rix = (IndexingOp) uop.getInput(0);
                        int index1 = rowIx ? 2 : 4;
                        int index2 = rowIx ? 3 : 5;
                        //modify left indexing bounds
-                       HopRewriteUtils.replaceChildReference(lix, 
lix.getInput().get(index1), from, index1);
-                       HopRewriteUtils.replaceChildReference(lix, 
lix.getInput().get(index2), to, index2);
+                       HopRewriteUtils.replaceChildReference(lix, 
lix.getInput(index1), from, index1);
+                       HopRewriteUtils.replaceChildReference(lix, 
lix.getInput(index2), to, index2);
                        //modify right indexing
-                       HopRewriteUtils.replaceChildReference(rix, 
rix.getInput().get(index1-1), from, index1-1);
-                       HopRewriteUtils.replaceChildReference(rix, 
rix.getInput().get(index2-1), to, index2-1);
+                       HopRewriteUtils.replaceChildReference(rix, 
rix.getInput(index1-1), from, index1-1);
+                       HopRewriteUtils.replaceChildReference(rix, 
rix.getInput(index2-1), to, index2-1);
                        updateLeftAndRightIndexingSizes(rowIx, lix, rix);
                        uop.refreshSizeInformation();
                        lix.refreshSizeInformation(); //after uop update
+                       Hop.resetVisitStatus(csb.getHops(), true);
                        
                        ret = csb;
                        LOG.debug("Applied vectorizeElementwiseUnaryForLoop.");
@@ -351,11 +338,6 @@ public class RewriteForLoopVectorization extends 
StatementBlockRewriteRule
        {
                StatementBlock ret = sb;
                
-               //check supported increment values
-               if( !(increment instanceof LiteralOp && 
((LiteralOp)increment).getDoubleValue()==1.0) ) {
-                       return ret;
-               }
-                       
                //check for applicability
                boolean apply = false;
                boolean rowIx = false; //row or col
@@ -363,36 +345,37 @@ public class RewriteForLoopVectorization extends 
StatementBlockRewriteRule
                {
                        Hop root = csb.getHops().get(0);
                        
-                       if( root.getDataType()==DataType.MATRIX && 
root.getInput().get(0) instanceof LeftIndexingOp )
+                       if( root.getDataType()==DataType.MATRIX && 
root.getInput(0) instanceof LeftIndexingOp )
                        {
-                               LeftIndexingOp lix = (LeftIndexingOp) 
root.getInput().get(0);
-                               Hop lixlhs = lix.getInput().get(0);
-                               Hop lixrhs = lix.getInput().get(1);
+                               LeftIndexingOp lix = (LeftIndexingOp) 
root.getInput(0);
+                               Hop lixlhs = lix.getInput(0);
+                               Hop lixrhs = lix.getInput(1);
                                
                                if( lixlhs instanceof DataOp && lixrhs 
instanceof IndexingOp
-                                       && lixrhs.getInput().get(0) instanceof 
DataOp )
+                                       && lixrhs.getInput(0) instanceof DataOp 
)
                                {
                                        boolean[] tmp = 
checkLeftAndRightIndexing(lix, (IndexingOp)lixrhs, itervar);
                                        apply = tmp[0];
                                        rowIx = tmp[1];
                                }
                        }
-               }       
+               }
                
                //apply rewrite if possible
                if( apply ) {
                        Hop root = csb.getHops().get(0);
-                       LeftIndexingOp lix = (LeftIndexingOp) 
root.getInput().get(0);
-                       IndexingOp rix = (IndexingOp) lix.getInput().get(1);
+                       LeftIndexingOp lix = (LeftIndexingOp) root.getInput(0);
+                       IndexingOp rix = (IndexingOp) lix.getInput(1);
                        int index1 = rowIx ? 2 : 4;
                        int index2 = rowIx ? 3 : 5;
                        //modify left indexing bounds
-                       HopRewriteUtils.replaceChildReference(lix, 
lix.getInput().get(index1), from, index1);
-                       HopRewriteUtils.replaceChildReference(lix, 
lix.getInput().get(index2), to, index2);
+                       HopRewriteUtils.replaceChildReference(lix, 
lix.getInput(index1), from, index1);
+                       HopRewriteUtils.replaceChildReference(lix, 
lix.getInput(index2), to, index2);
                        //modify right indexing
-                       HopRewriteUtils.replaceChildReference(rix, 
rix.getInput().get(index1-1), from, index1-1);
-                       HopRewriteUtils.replaceChildReference(rix, 
rix.getInput().get(index2-1), to, index2-1);
+                       HopRewriteUtils.replaceChildReference(rix, 
rix.getInput(index1-1), from, index1-1);
+                       HopRewriteUtils.replaceChildReference(rix, 
rix.getInput(index2-1), to, index2-1);
                        updateLeftAndRightIndexingSizes(rowIx, lix, rix);
+                       Hop.resetVisitStatus(csb.getHops(), true);
                        
                        ret = csb;
                        LOG.debug("Applied vectorizeIndexedCopy.");
@@ -405,16 +388,16 @@ public class RewriteForLoopVectorization extends 
StatementBlockRewriteRule
                boolean[] ret = new boolean[2]; //apply, rowIx
                
                //check for rowwise
-               if(    lix.isRowLowerEqualsUpper() && 
rix.isRowLowerEqualsUpper() 
-                       && lix.getInput().get(2).getName().equals(itervar)
-                       && rix.getInput().get(1).getName().equals(itervar) ) {
+               if(    lix.isRowLowerEqualsUpper() && 
rix.isRowLowerEqualsUpper()
+                       && lix.getInput(2).getName().equals(itervar)
+                       && rix.getInput(1).getName().equals(itervar) ) {
                        ret[0] = true;
                        ret[1] = true;
                }
                //check for colwise
-               if(    lix.isColLowerEqualsUpper() && 
rix.isColLowerEqualsUpper() 
-                       && lix.getInput().get(4).getName().equals(itervar)
-                       && rix.getInput().get(3).getName().equals(itervar) ) {
+               if(    lix.isColLowerEqualsUpper() && 
rix.isColLowerEqualsUpper()
+                       && lix.getInput(4).getName().equals(itervar)
+                       && rix.getInput(3).getName().equals(itervar) ) {
                        ret[0] = true;
                        ret[1] = false;
                }
@@ -438,4 +421,8 @@ public class RewriteForLoopVectorization extends 
StatementBlockRewriteRule
                        rixi.refreshSizeInformation();
                lix.refreshSizeInformation();
        }
+       
+       private Hop unwrap(Hop hop) {
+               return HopRewriteUtils.isData(hop, OpOpData.TRANSIENTWRITE) ? 
hop.getInput(0) : hop;
+       }
 }
diff --git 
a/src/main/java/org/apache/sysds/hops/rewrite/RewriteRemoveDanglingParentReferences.java
 
b/src/main/java/org/apache/sysds/hops/rewrite/RewriteRemoveDanglingParentReferences.java
deleted file mode 100644
index 573afe856c..0000000000
--- 
a/src/main/java/org/apache/sysds/hops/rewrite/RewriteRemoveDanglingParentReferences.java
+++ /dev/null
@@ -1,110 +0,0 @@
-/*
- * 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.rewrite;
-
-import java.util.ArrayList;
-import java.util.List;
-
-import org.apache.sysds.common.Types.OpOp1;
-import org.apache.sysds.common.Types.OpOpData;
-import org.apache.sysds.common.Types.OpOpN;
-import org.apache.sysds.hops.DataOp;
-import org.apache.sysds.hops.FunctionOp;
-import org.apache.sysds.hops.Hop;
-import org.apache.sysds.hops.NaryOp;
-import org.apache.sysds.hops.UnaryOp;
-
-/**
- * This rewrite is a general-purpose cleanup pass that removes any
- * dangling parent references in one pass through the hop DAG. These
- * dangling references could have been introduced by rewrites that 
- * remove operators but miss a proper cleanup. 
- * 
- */
-public class RewriteRemoveDanglingParentReferences extends HopRewriteRule
-{
-       @Override
-       public ArrayList<Hop> rewriteHopDAGs(ArrayList<Hop> roots, 
ProgramRewriteStatus state) {
-               if( roots == null )
-                       return null;
-               
-               int numRm = 0;
-               for( Hop h : roots ) 
-                       numRm += removeDanglingParentReferences( h, false );
-               
-               if( LOG.isDebugEnabled() && numRm > 0 )
-                       LOG.debug("Remove Dangling Parents - removed "+numRm+" 
operators.");
-               
-               return roots;
-       }
-
-       @Override
-       public Hop rewriteHopDAG(Hop root, ProgramRewriteStatus state) {
-               if( root == null )
-                       return root;
-               
-               //note: the predicate can have an arbitrary root node
-               //and hence, we pin this node to prevent its removal
-               int numRm = removeDanglingParentReferences( root, true );
-               
-               if( LOG.isDebugEnabled() && numRm > 0 )
-                       LOG.debug("Remove Dangling Parents - removed "+numRm+" 
operators.");
-               
-               return root;
-       }
-       
-       private int removeDanglingParentReferences( Hop hop, boolean pin ) {
-               //check mark processed
-               if( hop.isVisited() )
-                       return 0;
-               
-               //mark node itself as processed (because it's reachable over 
parents)
-               hop.setVisited();
-               
-               //process parents recursively (w/ potential modification)
-               int count = 0;
-               for( int i=0; i<hop.getParent().size(); i++ ) {
-                       Hop p = hop.getParent().get(i);
-                       count += removeDanglingParentReferences(p, false);
-                       i -= hop.getParent().contains(p) ? 0 : 1; //skip back
-               }
-               
-               //process node itself and children recursively
-               List<Hop> inputs = hop.getInput();
-               if( !pin && hop.getParent().isEmpty() && !isValidRootNode(hop) 
) {
-                       HopRewriteUtils.cleanupUnreferenced(hop);
-                       count++;
-               }
-               for( int i=0; i<inputs.size(); i++ )
-                       count += removeDanglingParentReferences(inputs.get(i), 
false);
-               
-               return count;
-       }
-       
-       private static boolean isValidRootNode(Hop hop) {
-               return (hop instanceof DataOp && ((DataOp)hop).isWrite())
-                       || (hop instanceof UnaryOp && 
((UnaryOp)hop).getOp()==OpOp1.STOP)
-                       || (hop instanceof UnaryOp && 
((UnaryOp)hop).getOp()==OpOp1.PRINT)
-                       || (hop instanceof UnaryOp && 
((UnaryOp)hop).getOp()==OpOp1.ASSERT)
-                       || (hop instanceof NaryOp && 
((NaryOp)hop).getOp()==OpOpN.PRINTF)
-                       || (hop instanceof FunctionOp)
-                       || (hop instanceof DataOp && 
((DataOp)hop).getOp()==OpOpData.FUNCTIONOUTPUT);
-       }
-}
diff --git 
a/src/main/java/org/apache/sysds/hops/rewrite/RewriteTransientWriteParentHandling.java
 
b/src/main/java/org/apache/sysds/hops/rewrite/RewriteTransientWriteParentHandling.java
deleted file mode 100644
index a32274cb6f..0000000000
--- 
a/src/main/java/org/apache/sysds/hops/rewrite/RewriteTransientWriteParentHandling.java
+++ /dev/null
@@ -1,76 +0,0 @@
-/*
- * 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.rewrite;
-
-import java.util.ArrayList;
-
-import org.apache.sysds.common.Types.OpOpData;
-import org.apache.sysds.hops.DataOp;
-import org.apache.sysds.hops.Hop;
-
-/**
- * Rule: Eliminate for Transient Write DataHops to have no parents
- * Solution: Move parent edges of Transient Write Hop to parent of
- * its child 
- * Reason: Transient Write not being a root messes up
- * analysis for Lop's to Instruction translation (according to Amol)
- */
-public class RewriteTransientWriteParentHandling extends HopRewriteRule
-{
-       @Override
-       public ArrayList<Hop> rewriteHopDAGs(ArrayList<Hop> roots, 
ProgramRewriteStatus state) {
-               for (Hop h : roots) 
-                       rule_RehangTransientWriteParents(h, roots);
-               return roots;
-       }
-
-       @Override
-       public Hop rewriteHopDAG(Hop root, ProgramRewriteStatus state) {
-               // do nothing (does not apply to predicate hop DAGs)
-               return root;
-       }
-
-       private void rule_RehangTransientWriteParents(Hop hop, ArrayList<Hop> 
sbHops) 
-       {
-               if (hop instanceof DataOp && ((DataOp) hop).getOp() == 
OpOpData.TRANSIENTWRITE
-                               && !hop.getParent().isEmpty()) {
-
-                       // update parents inputs with data op input
-                       for (Hop p : hop.getParent()) {
-                               p.getInput().set(p.getInput().indexOf(hop), 
hop.getInput().get(0));
-                       }
-
-                       // update dataop input parent to add new parents except 
for
-                       // dataop itself
-                       
hop.getInput().get(0).getParent().addAll(hop.getParent());
-
-                       // remove dataop parents
-                       hop.getParent().clear();
-
-                       // add dataop as root for this Hops DAG
-                       sbHops.add(hop);
-
-                       // do the same thing for my inputs (children)
-                       for (Hop hi : hop.getInput()) {
-                               rule_RehangTransientWriteParents(hi,sbHops);
-                       }
-               }
-       }
-}
diff --git 
a/src/test/java/org/apache/sysds/test/functions/rewrite/RewriteLoopVectorization.java
 
b/src/test/java/org/apache/sysds/test/functions/rewrite/RewriteLoopVectorization.java
index fe8a47bf37..d9358fef30 100644
--- 
a/src/test/java/org/apache/sysds/test/functions/rewrite/RewriteLoopVectorization.java
+++ 
b/src/test/java/org/apache/sysds/test/functions/rewrite/RewriteLoopVectorization.java
@@ -21,23 +21,23 @@ package org.apache.sysds.test.functions.rewrite;
 
 import java.util.HashMap;
 
+import org.junit.Assert;
 import org.junit.Test;
 import org.apache.sysds.hops.OptimizerUtils;
 import org.apache.sysds.runtime.matrix.data.MatrixValue.CellIndex;
 import org.apache.sysds.test.AutomatedTestBase;
 import org.apache.sysds.test.TestConfiguration;
 import org.apache.sysds.test.TestUtils;
+import org.apache.sysds.utils.Statistics;
 
-/**
- * Regression test for loop vectorization rewrite
- * for(i in 1:n) s = s + as.scalar(A[i,1]) -> s = s + sum(A[1:n,1])
- * 
- */
 public class RewriteLoopVectorization extends AutomatedTestBase 
 {
        private static final String TEST_NAME1 = "RewriteLoopVectorizationSum"; 
//amendable
        private static final String TEST_NAME2 = 
"RewriteLoopVectorizationSum2"; //not amendable
-
+       private static final String TEST_NAME3 = 
"RewriteLoopVectorizationBinary"; //amendable
+       private static final String TEST_NAME4 = 
"RewriteLoopVectorizationUnary"; //amendable
+       private static final String TEST_NAME5 = 
"RewriteLoopVectorizationIndexedCopy"; //amendable
+       
        private static final String TEST_DIR = "functions/rewrite/";
        private static final String TEST_CLASS_DIR = TEST_DIR + 
RewriteLoopVectorization.class.getSimpleName() + "/";
        
@@ -46,6 +46,9 @@ public class RewriteLoopVectorization extends 
AutomatedTestBase
                TestUtils.clearAssertionInformation();
                addTestConfiguration( TEST_NAME1, new 
TestConfiguration(TEST_CLASS_DIR, TEST_NAME1, new String[] { "R" }) );
                addTestConfiguration( TEST_NAME2, new 
TestConfiguration(TEST_CLASS_DIR, TEST_NAME2, new String[] { "R" }) );
+               addTestConfiguration( TEST_NAME3, new 
TestConfiguration(TEST_CLASS_DIR, TEST_NAME3, new String[] { "R" }) );
+               addTestConfiguration( TEST_NAME4, new 
TestConfiguration(TEST_CLASS_DIR, TEST_NAME4, new String[] { "R" }) );
+               addTestConfiguration( TEST_NAME5, new 
TestConfiguration(TEST_CLASS_DIR, TEST_NAME5, new String[] { "R" }) );
        }
        
        @Test
@@ -68,14 +71,39 @@ public class RewriteLoopVectorization extends 
AutomatedTestBase
                testRewriteLoopVectorizationSum( TEST_NAME2, true );
        }
        
-       /**
-        * 
-        * @param testname
-        * @param rewrites
-        */
+       @Test
+       public void testLoopVectorizationBinaryNoRewrite() {
+               testRewriteLoopVectorizationSum( TEST_NAME3, false );
+       }
+       
+       @Test
+       public void testLoopVectorizationBinaryRewrite() {
+               testRewriteLoopVectorizationSum( TEST_NAME3, true );
+       }
+       
+       @Test
+       public void testLoopVectorizationUnaryNoRewrite() {
+               testRewriteLoopVectorizationSum( TEST_NAME4, false );
+       }
+       
+       @Test
+       public void testLoopVectorizationUnaryRewrite() {
+               testRewriteLoopVectorizationSum( TEST_NAME4, true );
+       }
+       
+       @Test
+       public void testLoopVectorizationIndexedCopyNoRewrite() {
+               testRewriteLoopVectorizationSum( TEST_NAME5, false );
+       }
+       
+       @Test
+       public void testLoopVectorizationIndexedCopyRewrite() {
+               testRewriteLoopVectorizationSum( TEST_NAME5, true );
+       }
+       
        private void testRewriteLoopVectorizationSum( String testname, boolean 
rewrites )
        {       
-               boolean oldFlag = OptimizerUtils.ALLOW_ALGEBRAIC_SIMPLIFICATION;
+               boolean oldFlag = OptimizerUtils.ALLOW_AUTO_VECTORIZATION;
                
                try
                {
@@ -84,12 +112,12 @@ public class RewriteLoopVectorization extends 
AutomatedTestBase
                        
                        String HOME = SCRIPT_DIR + TEST_DIR;
                        fullDMLScriptName = HOME + testname + ".dml";
-                       programArgs = new String[]{ "-stats","-args", 
output("Scalar") };
+                       programArgs = new String[]{ "-stats", "-args", 
output("Scalar") };
                        
                        fullRScriptName = HOME + testname + ".R";
-                       rCmd = getRCmd(inputDir(), expectedDir());              
        
+                       rCmd = getRCmd(inputDir(), expectedDir());
 
-                       OptimizerUtils.ALLOW_ALGEBRAIC_SIMPLIFICATION = 
rewrites;
+                       OptimizerUtils.ALLOW_AUTO_VECTORIZATION = rewrites;
 
                        runTest(true, false, null, -1); 
                        runRScript(true); 
@@ -98,9 +126,11 @@ public class RewriteLoopVectorization extends 
AutomatedTestBase
                        HashMap<CellIndex, Double> dmlfile = 
readDMLScalarFromOutputDir("Scalar");
                        HashMap<CellIndex, Double> rfile  = 
readRScalarFromExpectedDir("Scalar");
                        TestUtils.compareScalars(dmlfile.toString(), 
rfile.toString());
+                       if( !testname.equals(TEST_NAME2) && rewrites )
+                               
Assert.assertTrue(Statistics.getCPHeavyHitterCount("rightIndex") <= 2);
                }
                finally {
-                       OptimizerUtils.ALLOW_ALGEBRAIC_SIMPLIFICATION = oldFlag;
-               }       
-       }       
-}
\ No newline at end of file
+                       OptimizerUtils.ALLOW_AUTO_VECTORIZATION = oldFlag;
+               }
+       }
+}
diff --git a/src/test/scripts/functions/rewrite/RewriteLoopVectorizationSum.dml 
b/src/test/scripts/functions/rewrite/RewriteLoopVectorizationBinary.R
similarity index 85%
copy from src/test/scripts/functions/rewrite/RewriteLoopVectorizationSum.dml
copy to src/test/scripts/functions/rewrite/RewriteLoopVectorizationBinary.R
index f199af339b..07f36462fe 100644
--- a/src/test/scripts/functions/rewrite/RewriteLoopVectorizationSum.dml
+++ b/src/test/scripts/functions/rewrite/RewriteLoopVectorizationBinary.R
@@ -1,5 +1,3 @@
-#-------------------------------------------------------------
-#
 # 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
@@ -7,9 +5,9 @@
 # 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
@@ -19,12 +17,18 @@
 #
 #-------------------------------------------------------------
 
+args<-commandArgs(TRUE)
+
 A = matrix(7.0, 10, 10)
+B = matrix(3.0, 10, 10)
 n = nrow(A)
 s = 0.0
 
-for( i in 1:n ) {
-  s = s + as.scalar(A[i,1])
+X = A
+for(i in 2:(n-1)) {
+  X[i,2] = A[i,1] + B[i,3]
 }
+s = sum(X)
+
+write(s, paste(args[2], "Scalar",sep=""))
 
-write(s, $1)
diff --git a/src/test/scripts/functions/rewrite/RewriteLoopVectorizationSum.dml 
b/src/test/scripts/functions/rewrite/RewriteLoopVectorizationBinary.dml
similarity index 91%
copy from src/test/scripts/functions/rewrite/RewriteLoopVectorizationSum.dml
copy to src/test/scripts/functions/rewrite/RewriteLoopVectorizationBinary.dml
index f199af339b..4bf6090c20 100644
--- a/src/test/scripts/functions/rewrite/RewriteLoopVectorizationSum.dml
+++ b/src/test/scripts/functions/rewrite/RewriteLoopVectorizationBinary.dml
@@ -7,9 +7,9 @@
 # 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
@@ -20,11 +20,14 @@
 #-------------------------------------------------------------
 
 A = matrix(7.0, 10, 10)
+B = matrix(3.0, 10, 10)
 n = nrow(A)
 s = 0.0
 
-for( i in 1:n ) {
-  s = s + as.scalar(A[i,1])
+X = A
+for(i in 2:n-1) {
+  X[i,2] = A[i,1] + B[i,3]
 }
+s = sum(X)
 
 write(s, $1)
diff --git a/src/test/scripts/functions/rewrite/RewriteLoopVectorizationSum.dml 
b/src/test/scripts/functions/rewrite/RewriteLoopVectorizationIndexedCopy.R
similarity index 86%
copy from src/test/scripts/functions/rewrite/RewriteLoopVectorizationSum.dml
copy to src/test/scripts/functions/rewrite/RewriteLoopVectorizationIndexedCopy.R
index f199af339b..6d896595d8 100644
--- a/src/test/scripts/functions/rewrite/RewriteLoopVectorizationSum.dml
+++ b/src/test/scripts/functions/rewrite/RewriteLoopVectorizationIndexedCopy.R
@@ -1,5 +1,3 @@
-#-------------------------------------------------------------
-#
 # 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
@@ -7,9 +5,9 @@
 # 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
@@ -19,12 +17,17 @@
 #
 #-------------------------------------------------------------
 
+args<-commandArgs(TRUE)
+
 A = matrix(7.0, 10, 10)
+B = matrix(3.0, 10, 10)
 n = nrow(A)
 s = 0.0
 
-for( i in 1:n ) {
-  s = s + as.scalar(A[i,1])
+for(i in 1:n) {
+  A[i,2] = B[i,1]
 }
+s = sum(A)
+
+write(s, paste(args[2], "Scalar",sep=""))
 
-write(s, $1)
diff --git a/src/test/scripts/functions/rewrite/RewriteLoopVectorizationSum.dml 
b/src/test/scripts/functions/rewrite/RewriteLoopVectorizationIndexedCopy.dml
similarity index 93%
copy from src/test/scripts/functions/rewrite/RewriteLoopVectorizationSum.dml
copy to 
src/test/scripts/functions/rewrite/RewriteLoopVectorizationIndexedCopy.dml
index f199af339b..df7320364e 100644
--- a/src/test/scripts/functions/rewrite/RewriteLoopVectorizationSum.dml
+++ b/src/test/scripts/functions/rewrite/RewriteLoopVectorizationIndexedCopy.dml
@@ -7,9 +7,9 @@
 # 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
@@ -20,11 +20,13 @@
 #-------------------------------------------------------------
 
 A = matrix(7.0, 10, 10)
+B = matrix(3.0, 10, 10)
 n = nrow(A)
 s = 0.0
 
-for( i in 1:n ) {
-  s = s + as.scalar(A[i,1])
+for(i in 1:n) {
+  A[i,2] = B[i,1]
 }
+s = sum(A)
 
 write(s, $1)
diff --git a/src/test/scripts/functions/rewrite/RewriteLoopVectorizationSum.dml 
b/src/test/scripts/functions/rewrite/RewriteLoopVectorizationSum.dml
index f199af339b..844e697141 100644
--- a/src/test/scripts/functions/rewrite/RewriteLoopVectorizationSum.dml
+++ b/src/test/scripts/functions/rewrite/RewriteLoopVectorizationSum.dml
@@ -7,9 +7,9 @@
 # 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
diff --git a/src/test/scripts/functions/rewrite/RewriteLoopVectorizationSum.dml 
b/src/test/scripts/functions/rewrite/RewriteLoopVectorizationUnary.R
similarity index 86%
copy from src/test/scripts/functions/rewrite/RewriteLoopVectorizationSum.dml
copy to src/test/scripts/functions/rewrite/RewriteLoopVectorizationUnary.R
index f199af339b..7f23854e91 100644
--- a/src/test/scripts/functions/rewrite/RewriteLoopVectorizationSum.dml
+++ b/src/test/scripts/functions/rewrite/RewriteLoopVectorizationUnary.R
@@ -1,5 +1,3 @@
-#-------------------------------------------------------------
-#
 # 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
@@ -7,9 +5,9 @@
 # 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
@@ -19,12 +17,17 @@
 #
 #-------------------------------------------------------------
 
+args<-commandArgs(TRUE)
+
 A = matrix(7.0, 10, 10)
+B = matrix(3.0, 10, 10)
 n = nrow(A)
 s = 0.0
 
-for( i in 1:n ) {
-  s = s + as.scalar(A[i,1])
+for(i in 1:n) {
+  A[i,2] = abs(B[i,1])
 }
+s = sum(A)
+
+write(s, paste(args[2], "Scalar",sep=""))
 
-write(s, $1)
diff --git a/src/test/scripts/functions/rewrite/RewriteLoopVectorizationSum.dml 
b/src/test/scripts/functions/rewrite/RewriteLoopVectorizationUnary.dml
similarity index 92%
copy from src/test/scripts/functions/rewrite/RewriteLoopVectorizationSum.dml
copy to src/test/scripts/functions/rewrite/RewriteLoopVectorizationUnary.dml
index f199af339b..22de37efeb 100644
--- a/src/test/scripts/functions/rewrite/RewriteLoopVectorizationSum.dml
+++ b/src/test/scripts/functions/rewrite/RewriteLoopVectorizationUnary.dml
@@ -7,9 +7,9 @@
 # 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
@@ -20,11 +20,13 @@
 #-------------------------------------------------------------
 
 A = matrix(7.0, 10, 10)
+B = matrix(3.0, 10, 10)
 n = nrow(A)
 s = 0.0
 
-for( i in 1:n ) {
-  s = s + as.scalar(A[i,1])
+for(i in 1:n) {
+  A[i,2] = abs(B[i,1])
 }
+s = sum(A)
 
 write(s, $1)


Reply via email to