[SYSTEMML-1931] Generalized logical NOT to matrices, incl cleanups

This patch generalizes the existing NOT operator from scalars to
matrices by extending the unary runtime of all backends. Furthermore,
this also includes a number of cleanups removing unnecessary redundancy
and minor fixes of the previously deduplicated binary instructions.


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

Branch: refs/heads/master
Commit: 9067654e79fac334f1f0f6c760c20165e9318bb2
Parents: ebdf770
Author: Matthias Boehm <[email protected]>
Authored: Thu Jan 4 19:27:19 2018 -0800
Committer: Matthias Boehm <[email protected]>
Committed: Thu Jan 4 19:27:19 2018 -0800

----------------------------------------------------------------------
 .../java/org/apache/sysml/hops/LiteralOp.java   | 39 ++++----
 .../hops/cost/CostEstimatorStaticRuntime.java   | 14 +--
 .../hops/rewrite/RewriteConstantFolding.java    |  2 +-
 .../instructions/CPInstructionParser.java       | 80 ++++++++--------
 .../runtime/instructions/InstructionUtils.java  |  7 ++
 .../instructions/MRInstructionParser.java       | 97 ++++++++++----------
 .../instructions/SPInstructionParser.java       | 51 +++++-----
 .../cp/BinaryScalarScalarCPInstruction.java     | 58 ++++++++----
 .../cp/BooleanBinaryCPInstruction.java          | 67 --------------
 .../cp/BooleanUnaryCPInstruction.java           | 68 --------------
 .../cp/BuiltinUnaryCPInstruction.java           | 76 ---------------
 .../runtime/instructions/cp/CPInstruction.java  |  6 +-
 .../cp/CompressionCPInstruction.java            |  2 +-
 .../cp/MatrixBuiltinCPInstruction.java          | 60 ------------
 .../cp/ScalarBuiltinCPInstruction.java          | 80 ----------------
 .../instructions/cp/UnaryCPInstruction.java     | 60 ++++++++----
 .../cp/UnaryMatrixCPInstruction.java            | 60 ++++++++++++
 .../cp/UnaryScalarCPInstruction.java            | 75 +++++++++++++++
 .../instructions/mr/BinaryInstruction.java      |  2 +-
 .../instructions/mr/BinaryMInstruction.java     |  2 +-
 .../runtime/instructions/mr/MRInstruction.java  |  2 +-
 .../instructions/mr/PlusMultInstruction.java    |  2 +-
 .../instructions/mr/ScalarInstruction.java      | 21 ++---
 .../instructions/mr/UnaryInstruction.java       | 27 ++----
 .../spark/BuiltinUnarySPInstruction.java        | 47 ----------
 .../spark/MatrixBuiltinSPInstruction.java       | 76 ---------------
 .../instructions/spark/SPInstruction.java       |  2 +-
 .../spark/UnaryMatrixSPInstruction.java         | 87 ++++++++++++++++++
 .../sysml/runtime/matrix/data/MatrixBlock.java  |  3 +-
 .../matrix/data/OperationsOnMatrixValues.java   | 15 ---
 .../runtime/matrix/operators/UnaryOperator.java |  4 +-
 .../binary/matrix/ElementwiseLogicalTest.java   | 63 +++++++------
 32 files changed, 503 insertions(+), 752 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/systemml/blob/9067654e/src/main/java/org/apache/sysml/hops/LiteralOp.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/sysml/hops/LiteralOp.java 
b/src/main/java/org/apache/sysml/hops/LiteralOp.java
index 6199777..d385d6b 100644
--- a/src/main/java/org/apache/sysml/hops/LiteralOp.java
+++ b/src/main/java/org/apache/sysml/hops/LiteralOp.java
@@ -199,16 +199,15 @@ public class LiteralOp extends Hop
                //do nothing; it is a scalar
        }
        
-       public long getLongValue() 
-       {
+       public long getLongValue() {
                switch( getValueType() ) {
-                       case INT:               
+                       case INT:
                                return value_long;
-                       case DOUBLE:    
+                       case DOUBLE:
                                return UtilFunctions.toLong(value_double);
                        case STRING:
-                               return Long.parseLong(value_string);    
-                       case BOOLEAN: 
+                               return Long.parseLong(value_string);
+                       case BOOLEAN:
                                return value_boolean ? 1 : 0;
                        default:
                                return -1;
@@ -217,9 +216,9 @@ public class LiteralOp extends Hop
        
        public double getDoubleValue() throws HopsException {
                switch( getValueType() ) {
-                       case INT:               
+                       case INT:
                                return value_long;
-                       case DOUBLE:    
+                       case DOUBLE:
                                return value_double;
                        case STRING:
                                return Double.parseDouble(value_string);
@@ -231,26 +230,32 @@ public class LiteralOp extends Hop
        }
        
        public boolean getBooleanValue() throws HopsException {
-               if ( getValueType() == ValueType.BOOLEAN ) {
-                       return value_boolean;
+               switch( getValueType() ) {
+                       case INT:
+                               return (value_long != 0);
+                       case DOUBLE:
+                               return (value_double != 0);
+                       case STRING:
+                               return Boolean.parseBoolean(value_string);
+                       case BOOLEAN:
+                               return value_boolean;
+                       default:
+                               throw new HopsException("Can not coerce an 
object of type " + getValueType() + " into Boolean.");
                }
-               else
-                       throw new HopsException("Can not coerce an object of 
type " + getValueType() + " into Boolean.");
        }
        
-       public String getStringValue() 
-       {
+       public String getStringValue() {
                switch( getValueType() ) {
                        case BOOLEAN:
                                return String.valueOf(value_boolean);
-                       case INT:               
+                       case INT:
                                return String.valueOf(value_long);
-                       case DOUBLE:    
+                       case DOUBLE:
                                return String.valueOf(value_double);
                        case STRING:
                                return value_string;
                        case OBJECT:
-                       case UNKNOWN:   
+                       case UNKNOWN:
                                //do nothing (return null)
                }
                

http://git-wip-us.apache.org/repos/asf/systemml/blob/9067654e/src/main/java/org/apache/sysml/hops/cost/CostEstimatorStaticRuntime.java
----------------------------------------------------------------------
diff --git 
a/src/main/java/org/apache/sysml/hops/cost/CostEstimatorStaticRuntime.java 
b/src/main/java/org/apache/sysml/hops/cost/CostEstimatorStaticRuntime.java
index c2627ef..364ff09 100644
--- a/src/main/java/org/apache/sysml/hops/cost/CostEstimatorStaticRuntime.java
+++ b/src/main/java/org/apache/sysml/hops/cost/CostEstimatorStaticRuntime.java
@@ -893,12 +893,6 @@ public class CostEstimatorStaticRuntime extends 
CostEstimator
                                        }
                                        return 0;
                                
-                               case BooleanBinary: //opcodes: &&, ||
-                                       return 1; //always scalar-scalar
-                               
-                               case BooleanUnary: //opcodes: !
-                                       return 1; //always scalar-scalar
-
                                case Builtin: //opcodes: log 
                                        //note: covers scalar-scalar, 
scalar-matrix, matrix-matrix
                                        //note: can be unary or binary
@@ -907,7 +901,7 @@ public class CostEstimatorStaticRuntime extends 
CostEstimator
                                        else //unary
                                                return d3m * d3n;
                                        
-                               case BuiltinUnary: //opcodes: exp, abs, sin, 
cos, tan, sign, sqrt, plogp, print, round, sprop, sigmoid
+                               case Unary: //opcodes: exp, abs, sin, cos, tan, 
sign, sqrt, plogp, print, round, sprop, sigmoid
                                        //TODO add cost functions for commons 
math builtins: inverse, cholesky
                                        if( optype.equals("print") ) //scalar 
only
                                                return 1;
@@ -1123,10 +1117,8 @@ public class CostEstimatorStaticRuntime extends 
CostEstimator
                                               + 2 * d2n * d1n * d1m * 
(leftSparse?d1s:1.0) //ba(+*)
                                               + d2n * d1n; //r(t)
                                        
-                               case ArithmeticBinary: //opcodes: s-r, so, max, 
min, 
-                                                          //         >, >=, <, 
<=, ==, != 
-                                       //TODO Should be relational 
-                               
+                               case Binary: //opcodes: s-r, so, max, min, 
+                                       //         >, >=, <, <=, ==, != 
                                        //note: all relational ops are not 
sparsesafe
                                        return d3m * d3n; //covers all 
combinations of scalar and matrix  
        

http://git-wip-us.apache.org/repos/asf/systemml/blob/9067654e/src/main/java/org/apache/sysml/hops/rewrite/RewriteConstantFolding.java
----------------------------------------------------------------------
diff --git 
a/src/main/java/org/apache/sysml/hops/rewrite/RewriteConstantFolding.java 
b/src/main/java/org/apache/sysml/hops/rewrite/RewriteConstantFolding.java
index 2d94259..9fefeb9 100644
--- a/src/main/java/org/apache/sysml/hops/rewrite/RewriteConstantFolding.java
+++ b/src/main/java/org/apache/sysml/hops/rewrite/RewriteConstantFolding.java
@@ -267,7 +267,7 @@ public class RewriteConstantFolding extends HopRewriteRule
        
        private static boolean isApplicableFalseConjunctivePredicate( Hop hop ) 
throws HopsException {
                ArrayList<Hop> in = hop.getInput();
-               return (   HopRewriteUtils.isBinary(hop, OpOp2.AND)
+               return (   HopRewriteUtils.isBinary(hop, OpOp2.AND) 
                                && ( (in.get(0) instanceof LiteralOp && 
!((LiteralOp)in.get(0)).getBooleanValue())
                                   ||(in.get(1) instanceof LiteralOp && 
!((LiteralOp)in.get(1)).getBooleanValue())) );
        }

http://git-wip-us.apache.org/repos/asf/systemml/blob/9067654e/src/main/java/org/apache/sysml/runtime/instructions/CPInstructionParser.java
----------------------------------------------------------------------
diff --git 
a/src/main/java/org/apache/sysml/runtime/instructions/CPInstructionParser.java 
b/src/main/java/org/apache/sysml/runtime/instructions/CPInstructionParser.java
index b739ef0..772b487 100644
--- 
a/src/main/java/org/apache/sysml/runtime/instructions/CPInstructionParser.java
+++ 
b/src/main/java/org/apache/sysml/runtime/instructions/CPInstructionParser.java
@@ -34,10 +34,7 @@ import 
org.apache.sysml.runtime.instructions.cp.AggregateTernaryCPInstruction;
 import org.apache.sysml.runtime.instructions.cp.AggregateUnaryCPInstruction;
 import org.apache.sysml.runtime.instructions.cp.AppendCPInstruction;
 import org.apache.sysml.runtime.instructions.cp.BinaryCPInstruction;
-import org.apache.sysml.runtime.instructions.cp.BooleanBinaryCPInstruction;
-import org.apache.sysml.runtime.instructions.cp.BooleanUnaryCPInstruction;
 import org.apache.sysml.runtime.instructions.cp.BuiltinNaryCPInstruction;
-import org.apache.sysml.runtime.instructions.cp.BuiltinUnaryCPInstruction;
 import org.apache.sysml.runtime.instructions.cp.CPInstruction;
 import org.apache.sysml.runtime.instructions.cp.CPInstruction.CPType;
 import org.apache.sysml.runtime.instructions.cp.CentralMomentCPInstruction;
@@ -64,6 +61,7 @@ import 
org.apache.sysml.runtime.instructions.cp.SpoofCPInstruction;
 import org.apache.sysml.runtime.instructions.cp.StringInitCPInstruction;
 import org.apache.sysml.runtime.instructions.cp.TernaryCPInstruction;
 import org.apache.sysml.runtime.instructions.cp.UaggOuterChainCPInstruction;
+import org.apache.sysml.runtime.instructions.cp.UnaryCPInstruction;
 import org.apache.sysml.runtime.instructions.cp.VariableCPInstruction;
 import 
org.apache.sysml.runtime.instructions.cpfile.MatrixIndexingCPFileInstruction;
 import 
org.apache.sysml.runtime.instructions.cpfile.ParameterizedBuiltinCPFileInstruction;
@@ -128,10 +126,10 @@ public class CPInstructionParser extends InstructionParser
                String2CPInstructionType.put( "-*"   , CPType.Binary);
                
                // Boolean Instruction Opcodes 
-               String2CPInstructionType.put( "&&"   , CPType.BooleanBinary);
-               String2CPInstructionType.put( "||"   , CPType.BooleanBinary);
-               String2CPInstructionType.put( "xor"  , CPType.BooleanBinary);
-               String2CPInstructionType.put( "!"    , CPType.BooleanUnary);
+               String2CPInstructionType.put( "&&"   , CPType.Binary);
+               String2CPInstructionType.put( "||"   , CPType.Binary);
+               String2CPInstructionType.put( "xor"  , CPType.Binary);
+               String2CPInstructionType.put( "!"    , CPType.Unary);
 
                // Relational Instruction Opcodes 
                String2CPInstructionType.put( "=="   , CPType.Binary);
@@ -149,34 +147,34 @@ public class CPInstructionParser extends InstructionParser
                String2CPInstructionType.put( "min"  , CPType.Binary);
                String2CPInstructionType.put( "solve"  , CPType.Binary);
                
-               String2CPInstructionType.put( "exp"   , CPType.BuiltinUnary);
-               String2CPInstructionType.put( "abs"   , CPType.BuiltinUnary);
-               String2CPInstructionType.put( "sin"   , CPType.BuiltinUnary);
-               String2CPInstructionType.put( "cos"   , CPType.BuiltinUnary);
-               String2CPInstructionType.put( "tan"   , CPType.BuiltinUnary);
-               String2CPInstructionType.put( "sinh"   , CPType.BuiltinUnary);
-               String2CPInstructionType.put( "cosh"   , CPType.BuiltinUnary);
-               String2CPInstructionType.put( "tanh"   , CPType.BuiltinUnary);
-               String2CPInstructionType.put( "asin"  , CPType.BuiltinUnary);
-               String2CPInstructionType.put( "acos"  , CPType.BuiltinUnary);
-               String2CPInstructionType.put( "atan"  , CPType.BuiltinUnary);
-               String2CPInstructionType.put( "sign"  , CPType.BuiltinUnary);
-               String2CPInstructionType.put( "sqrt"  , CPType.BuiltinUnary);
-               String2CPInstructionType.put( "plogp" , CPType.BuiltinUnary);
-               String2CPInstructionType.put( "print" , CPType.BuiltinUnary);
-               String2CPInstructionType.put( "round" , CPType.BuiltinUnary);
-               String2CPInstructionType.put( "ceil"  , CPType.BuiltinUnary);
-               String2CPInstructionType.put( "floor" , CPType.BuiltinUnary);
-               String2CPInstructionType.put( "ucumk+", CPType.BuiltinUnary);
-               String2CPInstructionType.put( "ucum*" , CPType.BuiltinUnary);
-               String2CPInstructionType.put( "ucummin", CPType.BuiltinUnary);
-               String2CPInstructionType.put( "ucummax", CPType.BuiltinUnary);
-               String2CPInstructionType.put( "stop"  , CPType.BuiltinUnary);
-               String2CPInstructionType.put( "inverse", CPType.BuiltinUnary);
-               String2CPInstructionType.put( "cholesky",CPType.BuiltinUnary);
-               String2CPInstructionType.put( "sprop", CPType.BuiltinUnary);
-               String2CPInstructionType.put( "sigmoid", CPType.BuiltinUnary);
-               String2CPInstructionType.put( "sel+", CPType.BuiltinUnary);
+               String2CPInstructionType.put( "exp"   , CPType.Unary);
+               String2CPInstructionType.put( "abs"   , CPType.Unary);
+               String2CPInstructionType.put( "sin"   , CPType.Unary);
+               String2CPInstructionType.put( "cos"   , CPType.Unary);
+               String2CPInstructionType.put( "tan"   , CPType.Unary);
+               String2CPInstructionType.put( "sinh"   , CPType.Unary);
+               String2CPInstructionType.put( "cosh"   , CPType.Unary);
+               String2CPInstructionType.put( "tanh"   , CPType.Unary);
+               String2CPInstructionType.put( "asin"  , CPType.Unary);
+               String2CPInstructionType.put( "acos"  , CPType.Unary);
+               String2CPInstructionType.put( "atan"  , CPType.Unary);
+               String2CPInstructionType.put( "sign"  , CPType.Unary);
+               String2CPInstructionType.put( "sqrt"  , CPType.Unary);
+               String2CPInstructionType.put( "plogp" , CPType.Unary);
+               String2CPInstructionType.put( "print" , CPType.Unary);
+               String2CPInstructionType.put( "round" , CPType.Unary);
+               String2CPInstructionType.put( "ceil"  , CPType.Unary);
+               String2CPInstructionType.put( "floor" , CPType.Unary);
+               String2CPInstructionType.put( "ucumk+", CPType.Unary);
+               String2CPInstructionType.put( "ucum*" , CPType.Unary);
+               String2CPInstructionType.put( "ucummin", CPType.Unary);
+               String2CPInstructionType.put( "ucummax", CPType.Unary);
+               String2CPInstructionType.put( "stop"  , CPType.Unary);
+               String2CPInstructionType.put( "inverse", CPType.Unary);
+               String2CPInstructionType.put( "cholesky",CPType.Unary);
+               String2CPInstructionType.put( "sprop", CPType.Unary);
+               String2CPInstructionType.put( "sigmoid", CPType.Unary);
+               String2CPInstructionType.put( "sel+", CPType.Unary);
                
                String2CPInstructionType.put( "printf" , CPType.BuiltinNary);
                String2CPInstructionType.put( "cbind" , CPType.BuiltinNary);
@@ -330,15 +328,9 @@ public class CPInstructionParser extends InstructionParser
                        
                        case Quaternary:
                                return 
QuaternaryCPInstruction.parseInstruction(str);
-                       
-                       case BooleanBinary:
-                               return 
BooleanBinaryCPInstruction.parseInstruction(str);
-                               
-                       case BooleanUnary:
-                               return 
BooleanUnaryCPInstruction.parseInstruction(str);
                                
-                       case BuiltinUnary:
-                               return 
BuiltinUnaryCPInstruction.parseInstruction(str);
+                       case Unary:
+                               return UnaryCPInstruction.parseInstruction(str);
                        
                        case BuiltinNary:
                                return 
BuiltinNaryCPInstruction.parseInstruction(str);
@@ -401,7 +393,7 @@ public class CPInstructionParser extends InstructionParser
                                if ( parts[0].equals("log") || 
parts[0].equals("log_nz") ) {
                                        if ( parts.length == 3 ) {
                                                // B=log(A), y=log(x)
-                                               return 
BuiltinUnaryCPInstruction.parseInstruction(str);
+                                               return 
UnaryCPInstruction.parseInstruction(str);
                                        } else if ( parts.length == 4 ) {
                                                // B=log(A,10), y=log(x,10)
                                                return 
BinaryCPInstruction.parseInstruction(str);

http://git-wip-us.apache.org/repos/asf/systemml/blob/9067654e/src/main/java/org/apache/sysml/runtime/instructions/InstructionUtils.java
----------------------------------------------------------------------
diff --git 
a/src/main/java/org/apache/sysml/runtime/instructions/InstructionUtils.java 
b/src/main/java/org/apache/sysml/runtime/instructions/InstructionUtils.java
index 14f4d49..0749d16 100644
--- a/src/main/java/org/apache/sysml/runtime/instructions/InstructionUtils.java
+++ b/src/main/java/org/apache/sysml/runtime/instructions/InstructionUtils.java
@@ -62,6 +62,7 @@ import org.apache.sysml.runtime.functionobjects.MinusNz;
 import org.apache.sysml.runtime.functionobjects.Modulus;
 import org.apache.sysml.runtime.functionobjects.Multiply;
 import org.apache.sysml.runtime.functionobjects.Multiply2;
+import org.apache.sysml.runtime.functionobjects.Not;
 import org.apache.sysml.runtime.functionobjects.NotEquals;
 import org.apache.sysml.runtime.functionobjects.Or;
 import org.apache.sysml.runtime.functionobjects.Plus;
@@ -482,6 +483,12 @@ public class InstructionUtils
                
                return aggun;
        }
+       
+       public static UnaryOperator parseUnaryOperator(String opcode) {
+               return opcode.equals("!") ?
+                       new UnaryOperator(Not.getNotFnObject()) :
+                       new UnaryOperator(Builtin.getBuiltinFnObject(opcode));
+       }
 
        public static Operator parseBinaryOrBuiltinOperator(String opcode, 
CPOperand in1, CPOperand in2) 
                throws DMLRuntimeException 

http://git-wip-us.apache.org/repos/asf/systemml/blob/9067654e/src/main/java/org/apache/sysml/runtime/instructions/MRInstructionParser.java
----------------------------------------------------------------------
diff --git 
a/src/main/java/org/apache/sysml/runtime/instructions/MRInstructionParser.java 
b/src/main/java/org/apache/sysml/runtime/instructions/MRInstructionParser.java
index ee1e923..f42e10e 100644
--- 
a/src/main/java/org/apache/sysml/runtime/instructions/MRInstructionParser.java
+++ 
b/src/main/java/org/apache/sysml/runtime/instructions/MRInstructionParser.java
@@ -154,6 +154,7 @@ public class MRInstructionParser extends InstructionParser
                String2MRInstructionType.put( "sprop", MRType.Unary);
                String2MRInstructionType.put( "sigmoid", MRType.Unary);
                String2MRInstructionType.put( "sel+", MRType.Unary);
+               String2MRInstructionType.put( "!", MRType.Unary);
                
                // Specific UNARY Instruction Opcodes
                String2MRInstructionType.put( "tsmm" , MRType.MMTSJ);
@@ -162,53 +163,53 @@ public class MRInstructionParser extends InstructionParser
                String2MRInstructionType.put( "binuaggchain", 
MRType.BinUaggChain);
                
                // BINARY and SCALAR Instruction Opcodes 
-               String2MRInstructionType.put( "+"    , MRType.ArithmeticBinary);
-               String2MRInstructionType.put( "-"    , MRType.ArithmeticBinary);
-               String2MRInstructionType.put( "s-r"  , MRType.ArithmeticBinary);
-               String2MRInstructionType.put( "*"    , MRType.ArithmeticBinary);
-               String2MRInstructionType.put( "/"    , MRType.ArithmeticBinary);
-               String2MRInstructionType.put( "%%"   , MRType.ArithmeticBinary);
-               String2MRInstructionType.put( "%/%"  , MRType.ArithmeticBinary);
-               String2MRInstructionType.put( "1-*"  , 
MRType.ArithmeticBinary); //special * case
-               String2MRInstructionType.put( "so"   , MRType.ArithmeticBinary);
-               String2MRInstructionType.put( "^"    , MRType.ArithmeticBinary);
-               String2MRInstructionType.put( "max"  , MRType.ArithmeticBinary);
-               String2MRInstructionType.put( "min"  , MRType.ArithmeticBinary);
-               String2MRInstructionType.put( ">"    , MRType.ArithmeticBinary);
-               String2MRInstructionType.put( ">="   , MRType.ArithmeticBinary);
-               String2MRInstructionType.put( "<"    , MRType.ArithmeticBinary);
-               String2MRInstructionType.put( "<="   , MRType.ArithmeticBinary);
-               String2MRInstructionType.put( "=="   , MRType.ArithmeticBinary);
-               String2MRInstructionType.put( "!="   , MRType.ArithmeticBinary);
-               String2MRInstructionType.put( "^"    , MRType.ArithmeticBinary);
-               String2MRInstructionType.put( "^2"   , 
MRType.ArithmeticBinary); //special ^ case
-               String2MRInstructionType.put( "*2"   , 
MRType.ArithmeticBinary); //special * case
-               String2MRInstructionType.put( "-nz"  , 
MRType.ArithmeticBinary); //special - case
-               String2MRInstructionType.put( "&&"   , MRType.ArithmeticBinary);
-               String2MRInstructionType.put( "||"   , MRType.ArithmeticBinary);
-               String2MRInstructionType.put( "xor"  , MRType.ArithmeticBinary);
-               String2MRInstructionType.put( "+*"   , 
MRType.ArithmeticBinary2); 
-               String2MRInstructionType.put( "-*"   , 
MRType.ArithmeticBinary2); 
+               String2MRInstructionType.put( "+"    , MRType.Binary);
+               String2MRInstructionType.put( "-"    , MRType.Binary);
+               String2MRInstructionType.put( "s-r"  , MRType.Binary);
+               String2MRInstructionType.put( "*"    , MRType.Binary);
+               String2MRInstructionType.put( "/"    , MRType.Binary);
+               String2MRInstructionType.put( "%%"   , MRType.Binary);
+               String2MRInstructionType.put( "%/%"  , MRType.Binary);
+               String2MRInstructionType.put( "1-*"  , MRType.Binary); 
//special * case
+               String2MRInstructionType.put( "so"   , MRType.Binary);
+               String2MRInstructionType.put( "^"    , MRType.Binary);
+               String2MRInstructionType.put( "max"  , MRType.Binary);
+               String2MRInstructionType.put( "min"  , MRType.Binary);
+               String2MRInstructionType.put( ">"    , MRType.Binary);
+               String2MRInstructionType.put( ">="   , MRType.Binary);
+               String2MRInstructionType.put( "<"    , MRType.Binary);
+               String2MRInstructionType.put( "<="   , MRType.Binary);
+               String2MRInstructionType.put( "=="   , MRType.Binary);
+               String2MRInstructionType.put( "!="   , MRType.Binary);
+               String2MRInstructionType.put( "^"    , MRType.Binary);
+               String2MRInstructionType.put( "^2"   , MRType.Binary); 
//special ^ case
+               String2MRInstructionType.put( "*2"   , MRType.Binary); 
//special * case
+               String2MRInstructionType.put( "-nz"  , MRType.Binary); 
//special - case
+               String2MRInstructionType.put( "&&"   , MRType.Binary);
+               String2MRInstructionType.put( "||"   , MRType.Binary);
+               String2MRInstructionType.put( "xor"  , MRType.Binary);
+               String2MRInstructionType.put( "+*"   , MRType.Binary2); 
+               String2MRInstructionType.put( "-*"   , MRType.Binary2); 
                
-               String2MRInstructionType.put( "map+"    , 
MRType.ArithmeticBinary);
-               String2MRInstructionType.put( "map-"    , 
MRType.ArithmeticBinary);
-               String2MRInstructionType.put( "map*"    , 
MRType.ArithmeticBinary);
-               String2MRInstructionType.put( "map/"    , 
MRType.ArithmeticBinary);
-               String2MRInstructionType.put( "map%%"   , 
MRType.ArithmeticBinary);
-               String2MRInstructionType.put( "map%/%"  , 
MRType.ArithmeticBinary);
-               String2MRInstructionType.put( "map1-*"  , 
MRType.ArithmeticBinary);
-               String2MRInstructionType.put( "map^"    , 
MRType.ArithmeticBinary);
-               String2MRInstructionType.put( "mapmax"  , 
MRType.ArithmeticBinary);
-               String2MRInstructionType.put( "mapmin"  , 
MRType.ArithmeticBinary);
-               String2MRInstructionType.put( "map>"    , 
MRType.ArithmeticBinary);
-               String2MRInstructionType.put( "map>="   , 
MRType.ArithmeticBinary);
-               String2MRInstructionType.put( "map<"    , 
MRType.ArithmeticBinary);
-               String2MRInstructionType.put( "map<="   , 
MRType.ArithmeticBinary);
-               String2MRInstructionType.put( "map=="   , 
MRType.ArithmeticBinary);
-               String2MRInstructionType.put( "map!="   , 
MRType.ArithmeticBinary);
-               String2MRInstructionType.put( "map&&"   , 
MRType.ArithmeticBinary);
-               String2MRInstructionType.put( "map||"   , 
MRType.ArithmeticBinary);
-               String2MRInstructionType.put( "mapxor"  , 
MRType.ArithmeticBinary);
+               String2MRInstructionType.put( "map+"    , MRType.Binary);
+               String2MRInstructionType.put( "map-"    , MRType.Binary);
+               String2MRInstructionType.put( "map*"    , MRType.Binary);
+               String2MRInstructionType.put( "map/"    , MRType.Binary);
+               String2MRInstructionType.put( "map%%"   , MRType.Binary);
+               String2MRInstructionType.put( "map%/%"  , MRType.Binary);
+               String2MRInstructionType.put( "map1-*"  , MRType.Binary);
+               String2MRInstructionType.put( "map^"    , MRType.Binary);
+               String2MRInstructionType.put( "mapmax"  , MRType.Binary);
+               String2MRInstructionType.put( "mapmin"  , MRType.Binary);
+               String2MRInstructionType.put( "map>"    , MRType.Binary);
+               String2MRInstructionType.put( "map>="   , MRType.Binary);
+               String2MRInstructionType.put( "map<"    , MRType.Binary);
+               String2MRInstructionType.put( "map<="   , MRType.Binary);
+               String2MRInstructionType.put( "map=="   , MRType.Binary);
+               String2MRInstructionType.put( "map!="   , MRType.Binary);
+               String2MRInstructionType.put( "map&&"   , MRType.Binary);
+               String2MRInstructionType.put( "map||"   , MRType.Binary);
+               String2MRInstructionType.put( "mapxor"  , MRType.Binary);
                
                String2MRInstructionType.put( "uaggouterchain", 
MRType.UaggOuterChain);
                
@@ -326,7 +327,7 @@ public class MRInstructionParser extends InstructionParser
                        case Aggregate:
                                return 
AggregateInstruction.parseInstruction(str);
                                
-                       case ArithmeticBinary: {
+                       case Binary: {
                                String opcode = InstructionUtils.getOpCode(str);
                                String[] parts = 
InstructionUtils.getInstructionPartsWithValueType(str);
                                // extract datatypes of first and second input 
operands
@@ -343,7 +344,7 @@ public class MRInstructionParser extends InstructionParser
                                }
                        }
                        
-                       case ArithmeticBinary2:
+                       case Binary2:
                                return 
PlusMultInstruction.parseInstruction(str);
                        
                        case AggregateBinary:

http://git-wip-us.apache.org/repos/asf/systemml/blob/9067654e/src/main/java/org/apache/sysml/runtime/instructions/SPInstructionParser.java
----------------------------------------------------------------------
diff --git 
a/src/main/java/org/apache/sysml/runtime/instructions/SPInstructionParser.java 
b/src/main/java/org/apache/sysml/runtime/instructions/SPInstructionParser.java
index c60756f..1f1cb4d 100644
--- 
a/src/main/java/org/apache/sysml/runtime/instructions/SPInstructionParser.java
+++ 
b/src/main/java/org/apache/sysml/runtime/instructions/SPInstructionParser.java
@@ -46,7 +46,6 @@ import 
org.apache.sysml.runtime.instructions.spark.AppendRSPInstruction;
 import org.apache.sysml.runtime.instructions.spark.BinUaggChainSPInstruction;
 import org.apache.sysml.runtime.instructions.spark.BinarySPInstruction;
 import org.apache.sysml.runtime.instructions.spark.BuiltinNarySPInstruction;
-import org.apache.sysml.runtime.instructions.spark.BuiltinUnarySPInstruction;
 import org.apache.sysml.runtime.instructions.spark.CSVReblockSPInstruction;
 import org.apache.sysml.runtime.instructions.spark.CastSPInstruction;
 import org.apache.sysml.runtime.instructions.spark.CentralMomentSPInstruction;
@@ -80,6 +79,7 @@ import 
org.apache.sysml.runtime.instructions.spark.Tsmm2SPInstruction;
 import org.apache.sysml.runtime.instructions.spark.TsmmSPInstruction;
 import org.apache.sysml.runtime.instructions.spark.QuantileSortSPInstruction;
 import org.apache.sysml.runtime.instructions.spark.UaggOuterChainSPInstruction;
+import org.apache.sysml.runtime.instructions.spark.UnaryMatrixSPInstruction;
 import org.apache.sysml.runtime.instructions.spark.WriteSPInstruction;
 import org.apache.sysml.runtime.instructions.spark.ZipmmSPInstruction;
 
@@ -193,11 +193,10 @@ public class SPInstructionParser extends InstructionParser
                String2SPInstructionType.put( "&&"   , SPType.Binary);
                String2SPInstructionType.put( "||"   , SPType.Binary);
                String2SPInstructionType.put( "xor"  , SPType.Binary);
-               String2SPInstructionType.put( "!"    , SPType.BuiltinUnary);
+               String2SPInstructionType.put( "!"    , SPType.Unary);
                String2SPInstructionType.put( "map&&"   , SPType.Binary);
                String2SPInstructionType.put( "map||"   , SPType.Binary);
                String2SPInstructionType.put( "mapxor"  , SPType.Binary);
-               String2SPInstructionType.put( "map!"    , SPType.BuiltinUnary);
                
                // REBLOCK Instruction Opcodes 
                String2SPInstructionType.put( "rblk"   , SPType.Reblock);
@@ -217,26 +216,26 @@ public class SPInstructionParser extends InstructionParser
                String2SPInstructionType.put( "mapmax"  , SPType.Builtin);
                String2SPInstructionType.put( "mapmin"  , SPType.Builtin);
                
-               String2SPInstructionType.put( "exp"   , SPType.BuiltinUnary);
-               String2SPInstructionType.put( "abs"   , SPType.BuiltinUnary);
-               String2SPInstructionType.put( "sin"   , SPType.BuiltinUnary);
-               String2SPInstructionType.put( "cos"   , SPType.BuiltinUnary);
-               String2SPInstructionType.put( "tan"   , SPType.BuiltinUnary);
-               String2SPInstructionType.put( "asin"  , SPType.BuiltinUnary);
-               String2SPInstructionType.put( "acos"  , SPType.BuiltinUnary);
-               String2SPInstructionType.put( "atan"  , SPType.BuiltinUnary);
-               String2SPInstructionType.put( "sinh"   , SPType.BuiltinUnary);
-               String2SPInstructionType.put( "cosh"   , SPType.BuiltinUnary);
-               String2SPInstructionType.put( "tanh"   , SPType.BuiltinUnary);
-               String2SPInstructionType.put( "sign"  , SPType.BuiltinUnary);
-               String2SPInstructionType.put( "sqrt"  , SPType.BuiltinUnary);
-               String2SPInstructionType.put( "plogp" , SPType.BuiltinUnary);
-               String2SPInstructionType.put( "round" , SPType.BuiltinUnary);
-               String2SPInstructionType.put( "ceil"  , SPType.BuiltinUnary);
-               String2SPInstructionType.put( "floor" , SPType.BuiltinUnary);
-               String2SPInstructionType.put( "sprop", SPType.BuiltinUnary);
-               String2SPInstructionType.put( "sigmoid", SPType.BuiltinUnary);
-               String2SPInstructionType.put( "sel+", SPType.BuiltinUnary);
+               String2SPInstructionType.put( "exp"   , SPType.Unary);
+               String2SPInstructionType.put( "abs"   , SPType.Unary);
+               String2SPInstructionType.put( "sin"   , SPType.Unary);
+               String2SPInstructionType.put( "cos"   , SPType.Unary);
+               String2SPInstructionType.put( "tan"   , SPType.Unary);
+               String2SPInstructionType.put( "asin"  , SPType.Unary);
+               String2SPInstructionType.put( "acos"  , SPType.Unary);
+               String2SPInstructionType.put( "atan"  , SPType.Unary);
+               String2SPInstructionType.put( "sinh"   , SPType.Unary);
+               String2SPInstructionType.put( "cosh"   , SPType.Unary);
+               String2SPInstructionType.put( "tanh"   , SPType.Unary);
+               String2SPInstructionType.put( "sign"  , SPType.Unary);
+               String2SPInstructionType.put( "sqrt"  , SPType.Unary);
+               String2SPInstructionType.put( "plogp" , SPType.Unary);
+               String2SPInstructionType.put( "round" , SPType.Unary);
+               String2SPInstructionType.put( "ceil"  , SPType.Unary);
+               String2SPInstructionType.put( "floor" , SPType.Unary);
+               String2SPInstructionType.put( "sprop", SPType.Unary);
+               String2SPInstructionType.put( "sigmoid", SPType.Unary);
+               String2SPInstructionType.put( "sel+", SPType.Unary);
                
                // Parameterized Builtin Functions
                String2SPInstructionType.put( "groupedagg"       , 
SPType.ParameterizedBuiltin);
@@ -392,7 +391,7 @@ public class SPInstructionParser extends InstructionParser
                                if ( parts[0].equals("log") || 
parts[0].equals("log_nz") ) {
                                        if ( parts.length == 3 ) {
                                                // B=log(A), y=log(x)
-                                               return 
BuiltinUnarySPInstruction.parseInstruction(str);
+                                               return 
UnaryMatrixSPInstruction.parseInstruction(str);
                                        } else if ( parts.length == 4 ) {
                                                // B=log(A,10), y=log(x,10)
                                                return 
BinarySPInstruction.parseInstruction(str);
@@ -402,8 +401,8 @@ public class SPInstructionParser extends InstructionParser
                                        throw new DMLRuntimeException("Invalid 
Builtin Instruction: " + str );
                                }
                                
-                       case BuiltinUnary:
-                               return 
BuiltinUnarySPInstruction.parseInstruction(str);
+                       case Unary:
+                               return 
UnaryMatrixSPInstruction.parseInstruction(str);
                        
                        case BuiltinNary:
                                return 
BuiltinNarySPInstruction.parseInstruction(str);

http://git-wip-us.apache.org/repos/asf/systemml/blob/9067654e/src/main/java/org/apache/sysml/runtime/instructions/cp/BinaryScalarScalarCPInstruction.java
----------------------------------------------------------------------
diff --git 
a/src/main/java/org/apache/sysml/runtime/instructions/cp/BinaryScalarScalarCPInstruction.java
 
b/src/main/java/org/apache/sysml/runtime/instructions/cp/BinaryScalarScalarCPInstruction.java
index a577938..76f53fb 100644
--- 
a/src/main/java/org/apache/sysml/runtime/instructions/cp/BinaryScalarScalarCPInstruction.java
+++ 
b/src/main/java/org/apache/sysml/runtime/instructions/cp/BinaryScalarScalarCPInstruction.java
@@ -22,6 +22,7 @@ package org.apache.sysml.runtime.instructions.cp;
 import org.apache.sysml.parser.Expression.ValueType;
 import org.apache.sysml.runtime.DMLRuntimeException;
 import org.apache.sysml.runtime.controlprogram.context.ExecutionContext;
+import org.apache.sysml.runtime.functionobjects.ValueComparisonFunction;
 import org.apache.sysml.runtime.matrix.operators.BinaryOperator;
 import org.apache.sysml.runtime.matrix.operators.Operator;
 
@@ -36,30 +37,47 @@ public class BinaryScalarScalarCPInstruction extends 
BinaryCPInstruction {
                ScalarObject so1 = ec.getScalarInput(input1.getName(), 
input1.getValueType(), input1.isLiteral());
                ScalarObject so2 = ec.getScalarInput(input2.getName(), 
input2.getValueType(), input2.isLiteral() );
                
+               String opcode = getOpcode();
                BinaryOperator dop = (BinaryOperator) _optr;
                ScalarObject sores = null;
-
-               //compute output value, incl implicit type promotion if 
necessary
-               if( so1 instanceof StringObject || so2 instanceof StringObject 
) {
-                       if( !getOpcode().equals("+") ) //not string 
concatenation
-                               throw new DMLRuntimeException("Arithmetic 
'"+getOpcode()+"' not supported over string inputs.");
-                       sores = new StringObject( dop.fn.execute(
-                               so1.getLanguageSpecificStringValue(), 
so2.getLanguageSpecificStringValue()) );
-               }
-               else if( so1 instanceof DoubleObject || so2 instanceof 
DoubleObject || output.getValueType()==ValueType.DOUBLE ) {
-                       sores = new DoubleObject( 
dop.fn.execute(so1.getDoubleValue(), so2.getDoubleValue()) );
-               }
-               else if( so1 instanceof IntObject || so2 instanceof IntObject ) 
{
-                       double tmp = dop.fn.execute(so1.getLongValue(), 
so2.getLongValue());
-                       if( tmp > Long.MAX_VALUE ) //cast to long if no 
overflow, otherwise controlled exception
-                               throw new DMLRuntimeException("Integer 
operation created numerical result overflow ("+tmp+" > "+Long.MAX_VALUE+").");
-                       sores = new IntObject((long) tmp);
+               
+               //compare output value, incl implicit type promotion if 
necessary
+               if( dop.fn instanceof ValueComparisonFunction ) {
+                       ValueComparisonFunction vcomp = 
(ValueComparisonFunction) dop.fn;
+                       if( so1 instanceof StringObject || so2 instanceof 
StringObject )
+                               sores = new  
BooleanObject(vcomp.compare(so1.getStringValue(), so2.getStringValue()));
+                       else if( so1 instanceof DoubleObject || so2 instanceof 
DoubleObject )
+                               sores = new  
BooleanObject(vcomp.compare(so1.getDoubleValue(), so2.getDoubleValue()));
+                       else if( so1 instanceof IntObject || so2 instanceof 
IntObject )
+                               sores = new  
BooleanObject(vcomp.compare(so1.getLongValue(), so2.getLongValue()));
+                       else //all boolean
+                               sores = new  
BooleanObject(vcomp.compare(so1.getBooleanValue(), so2.getBooleanValue()));
                }
-               else { //all boolean
-                       //NOTE: boolean-boolean arithmetic treated as double 
for consistency with R
-                       sores = new DoubleObject( 
dop.fn.execute(so1.getDoubleValue(), so2.getDoubleValue()) );
+               //compute output value, incl implicit type promotion if 
necessary
+               else {
+                       if( so1 instanceof StringObject || so2 instanceof 
StringObject ) {
+                               if( !opcode.equals("+") ) //not string 
concatenation
+                                       throw new 
DMLRuntimeException("Arithmetic '"+opcode+"' not supported over string 
inputs.");
+                               sores = new StringObject( dop.fn.execute(
+                                       so1.getLanguageSpecificStringValue(), 
so2.getLanguageSpecificStringValue()) );
+                       }
+                       else if( so1 instanceof DoubleObject || so2 instanceof 
DoubleObject || output.getValueType()==ValueType.DOUBLE ) {
+                               sores = new DoubleObject( 
dop.fn.execute(so1.getDoubleValue(), so2.getDoubleValue()) );
+                       }
+                       else if( so1 instanceof IntObject || so2 instanceof 
IntObject ) {
+                               double tmp = dop.fn.execute(so1.getLongValue(), 
so2.getLongValue());
+                               if( tmp > Long.MAX_VALUE ) //cast to long if no 
overflow, otherwise controlled exception
+                                       throw new DMLRuntimeException("Integer 
operation created numerical result overflow ("+tmp+" > "+Long.MAX_VALUE+").");
+                               sores = new IntObject((long) tmp);
+                       }
+                       else { //all boolean
+                               //NOTE: boolean-boolean arithmetic treated as 
double for consistency with R
+                               if( opcode.equals("&&") || opcode.equals("||") 
|| opcode.equals("xor") )
+                                       sores = new BooleanObject( 
dop.fn.execute(so1.getBooleanValue(), so2.getBooleanValue()) );
+                               else
+                                       sores = new DoubleObject( 
dop.fn.execute(so1.getDoubleValue(), so2.getDoubleValue()) );
+                       }
                }
-               
                ec.setScalarOutput(output.getName(), sores);
        }
 }

http://git-wip-us.apache.org/repos/asf/systemml/blob/9067654e/src/main/java/org/apache/sysml/runtime/instructions/cp/BooleanBinaryCPInstruction.java
----------------------------------------------------------------------
diff --git 
a/src/main/java/org/apache/sysml/runtime/instructions/cp/BooleanBinaryCPInstruction.java
 
b/src/main/java/org/apache/sysml/runtime/instructions/cp/BooleanBinaryCPInstruction.java
deleted file mode 100644
index dad7d97..0000000
--- 
a/src/main/java/org/apache/sysml/runtime/instructions/cp/BooleanBinaryCPInstruction.java
+++ /dev/null
@@ -1,67 +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.sysml.runtime.instructions.cp;
-
-import org.apache.sysml.parser.Expression.DataType;
-import org.apache.sysml.parser.Expression.ValueType;
-import org.apache.sysml.runtime.DMLRuntimeException;
-import org.apache.sysml.runtime.controlprogram.context.ExecutionContext;
-import org.apache.sysml.runtime.instructions.InstructionUtils;
-import org.apache.sysml.runtime.matrix.operators.BinaryOperator;
-import org.apache.sysml.runtime.matrix.operators.Operator;
-
-public class BooleanBinaryCPInstruction extends BinaryCPInstruction {
-
-       protected BooleanBinaryCPInstruction(Operator op, CPOperand in1, 
CPOperand in2, CPOperand out, String opcode,
-                       String istr) {
-               super(CPType.BooleanBinary, op, in1, in2, out, opcode, istr);
-       }
-
-       public static BinaryCPInstruction parseInstruction (String str)
-               throws DMLRuntimeException 
-       {
-               CPOperand in1 = new CPOperand("", ValueType.UNKNOWN, 
DataType.UNKNOWN);
-               CPOperand in2 = new CPOperand("", ValueType.UNKNOWN, 
DataType.UNKNOWN);
-               CPOperand out = new CPOperand("", ValueType.UNKNOWN, 
DataType.UNKNOWN);
-               String opcode = parseBinaryInstruction(str, in1, in2, out);
-               BinaryOperator bop = 
InstructionUtils.parseBinaryOperator(opcode);
-               
-               if ( in1.getDataType() == DataType.SCALAR && in2.getDataType() 
== DataType.SCALAR )
-                       return new BooleanBinaryCPInstruction(bop, in1, in2, 
out, opcode, str);
-               else if ( in1.getDataType() == DataType.MATRIX && 
in2.getDataType() == DataType.MATRIX )
-                       return new BinaryMatrixMatrixCPInstruction(bop, in1, 
in2, out, opcode, str);
-               else
-                       return new BinaryMatrixScalarCPInstruction(bop, in1, 
in2, out, opcode, str);
-       }
-       
-       @Override
-       public void processInstruction(ExecutionContext ec) 
-               throws DMLRuntimeException 
-       {
-               ScalarObject so1 = ec.getScalarInput( input1.getName(), 
input1.getValueType(), input1.isLiteral() );
-               ScalarObject so2 = ec.getScalarInput( input2.getName(), 
input2.getValueType(), input2.isLiteral() );
-               
-               BinaryOperator dop = (BinaryOperator) _optr;
-               boolean rval = dop.fn.execute(so1.getBooleanValue(), 
so2.getBooleanValue());
-               ScalarObject sores = (ScalarObject) new BooleanObject(rval);
-               
-               ec.setScalarOutput(output.getName(), sores);
-       }
-}

http://git-wip-us.apache.org/repos/asf/systemml/blob/9067654e/src/main/java/org/apache/sysml/runtime/instructions/cp/BooleanUnaryCPInstruction.java
----------------------------------------------------------------------
diff --git 
a/src/main/java/org/apache/sysml/runtime/instructions/cp/BooleanUnaryCPInstruction.java
 
b/src/main/java/org/apache/sysml/runtime/instructions/cp/BooleanUnaryCPInstruction.java
deleted file mode 100644
index e8d31c1..0000000
--- 
a/src/main/java/org/apache/sysml/runtime/instructions/cp/BooleanUnaryCPInstruction.java
+++ /dev/null
@@ -1,68 +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.sysml.runtime.instructions.cp;
-
-import org.apache.sysml.parser.Expression.DataType;
-import org.apache.sysml.parser.Expression.ValueType;
-import org.apache.sysml.runtime.DMLRuntimeException;
-import org.apache.sysml.runtime.controlprogram.context.ExecutionContext;
-import org.apache.sysml.runtime.matrix.operators.Operator;
-import org.apache.sysml.runtime.matrix.operators.SimpleOperator;
-
-public class BooleanUnaryCPInstruction extends UnaryCPInstruction {
-
-       private BooleanUnaryCPInstruction(Operator op, CPOperand in, CPOperand 
out, String opcode, String instr) {
-               super(CPType.BooleanUnary, op, in, out, opcode, instr);
-       }
-
-       public static BooleanUnaryCPInstruction parseInstruction (String str) 
-               throws DMLRuntimeException 
-       {
-               CPOperand in = new CPOperand("", ValueType.UNKNOWN, 
DataType.UNKNOWN);
-               CPOperand out = new CPOperand("", ValueType.UNKNOWN, 
DataType.UNKNOWN);
-               String opcode = parseUnaryInstruction(str, in, out);
-               
-               // Boolean operations must be performed on BOOLEAN
-               ValueType vt1 = in.getValueType();
-               ValueType vt2 = out.getValueType();
-               if ( vt1 != ValueType.BOOLEAN || vt2 != ValueType.BOOLEAN )
-                       throw new DMLRuntimeException("Unexpected ValueType in 
ArithmeticInstruction.");
-               
-               // Determine appropriate Function Object based on opcode        
-               return new 
BooleanUnaryCPInstruction(getSimpleUnaryOperator(opcode), in, out, opcode, str);
-       }
-       
-       @Override
-       public void processInstruction(ExecutionContext ec) 
-               throws DMLRuntimeException 
-       {
-               // 1) Obtain data objects associated with inputs 
-               ScalarObject so = ec.getScalarInput(input1.getName(), 
input1.getValueType(), input1.isLiteral());
-               
-               // 2) Compute the result value & make an appropriate data 
object 
-               SimpleOperator dop = (SimpleOperator) _optr;
-               boolean rval = dop.fn.execute(so.getBooleanValue());
-               
-               ScalarObject sores = (ScalarObject) new BooleanObject(rval);
-               
-               // 3) Put the result value into ProgramBlock
-               ec.setScalarOutput(output.getName(), sores);
-       }
-}

http://git-wip-us.apache.org/repos/asf/systemml/blob/9067654e/src/main/java/org/apache/sysml/runtime/instructions/cp/BuiltinUnaryCPInstruction.java
----------------------------------------------------------------------
diff --git 
a/src/main/java/org/apache/sysml/runtime/instructions/cp/BuiltinUnaryCPInstruction.java
 
b/src/main/java/org/apache/sysml/runtime/instructions/cp/BuiltinUnaryCPInstruction.java
deleted file mode 100644
index 9f72d62..0000000
--- 
a/src/main/java/org/apache/sysml/runtime/instructions/cp/BuiltinUnaryCPInstruction.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.sysml.runtime.instructions.cp;
-
-import java.util.Arrays;
-
-import org.apache.sysml.parser.Expression.DataType;
-import org.apache.sysml.parser.Expression.ValueType;
-import org.apache.sysml.runtime.DMLRuntimeException;
-import org.apache.sysml.runtime.functionobjects.Builtin;
-import org.apache.sysml.runtime.functionobjects.ValueFunction;
-import org.apache.sysml.runtime.instructions.InstructionUtils;
-import org.apache.sysml.runtime.matrix.data.LibCommonsMath;
-import org.apache.sysml.runtime.matrix.operators.Operator;
-import org.apache.sysml.runtime.matrix.operators.SimpleOperator;
-import org.apache.sysml.runtime.matrix.operators.UnaryOperator;
-
-public abstract class BuiltinUnaryCPInstruction extends UnaryCPInstruction {
-
-       protected BuiltinUnaryCPInstruction(Operator op, CPOperand in, 
CPOperand out, String opcode, String istr) {
-               super(CPType.BuiltinUnary, op, in, out, opcode, istr);
-       }
-
-       public static BuiltinUnaryCPInstruction parseInstruction ( String str ) 
-               throws DMLRuntimeException
-       {
-               CPOperand in = new CPOperand("", ValueType.UNKNOWN, 
DataType.UNKNOWN);
-               CPOperand out = new CPOperand("", ValueType.UNKNOWN, 
DataType.UNKNOWN);
-               
-               String[] parts = 
InstructionUtils.getInstructionPartsWithValueType(str);
-               String opcode = null;
-               ValueFunction func = null;
-               
-               //print or stop or cumulative aggregates
-               if( parts.length==4 ) {
-                       opcode = parts[0];
-                       in.split(parts[1]);
-                       out.split(parts[2]);
-                       func = Builtin.getBuiltinFnObject(opcode);
-                       
-                       if( Arrays.asList(new 
String[]{"ucumk+","ucum*","ucummin","ucummax"}).contains(opcode) )
-                               return new MatrixBuiltinCPInstruction(new 
UnaryOperator(func,Integer.parseInt(parts[3])), in, out, opcode, str); 
-                       else
-                               return new ScalarBuiltinCPInstruction(new 
SimpleOperator(func), in, out, opcode, str);
-               }
-               else { //2+1, general case
-                       opcode = parseUnaryInstruction(str, in, out);
-                       func = Builtin.getBuiltinFnObject(opcode);
-                       
-                       if(in.getDataType() == DataType.SCALAR)
-                               return new ScalarBuiltinCPInstruction(new 
SimpleOperator(func), in, out, opcode, str);
-                       else if(in.getDataType() == DataType.MATRIX)
-                               return new 
MatrixBuiltinCPInstruction(LibCommonsMath.isSupportedUnaryOperation(opcode) ?
-                                       null : new UnaryOperator(func), in, 
out, opcode, str);
-               }
-               
-               return null;
-       }
-}

http://git-wip-us.apache.org/repos/asf/systemml/blob/9067654e/src/main/java/org/apache/sysml/runtime/instructions/cp/CPInstruction.java
----------------------------------------------------------------------
diff --git 
a/src/main/java/org/apache/sysml/runtime/instructions/cp/CPInstruction.java 
b/src/main/java/org/apache/sysml/runtime/instructions/cp/CPInstruction.java
index cdf5805..bebf2ca 100644
--- a/src/main/java/org/apache/sysml/runtime/instructions/cp/CPInstruction.java
+++ b/src/main/java/org/apache/sysml/runtime/instructions/cp/CPInstruction.java
@@ -31,9 +31,9 @@ public abstract class CPInstruction extends Instruction
 {
        public enum CPType {
                AggregateUnary, AggregateBinary, AggregateTernary,
-               Binary, Ternary, Quaternary, BooleanBinary, BooleanUnary, 
BuiltinUnary, BuiltinNary, 
-               MultiReturnParameterizedBuiltin, ParameterizedBuiltin, 
MultiReturnBuiltin, 
-               Builtin, Reorg, Variable, External, Append, Rand, QSort, QPick, 
+               Unary, Binary, Ternary, Quaternary, BuiltinNary,
+               MultiReturnParameterizedBuiltin, ParameterizedBuiltin, 
MultiReturnBuiltin,
+               Builtin, Reorg, Variable, External, Append, Rand, QSort, QPick,
                MatrixIndexing, MMTSJ, PMMJ, MMChain, MatrixReshape, Partition, 
Compression, SpoofFused,
                StringInit, CentralMoment, Covariance, UaggOuterChain, 
Convolution }
        

http://git-wip-us.apache.org/repos/asf/systemml/blob/9067654e/src/main/java/org/apache/sysml/runtime/instructions/cp/CompressionCPInstruction.java
----------------------------------------------------------------------
diff --git 
a/src/main/java/org/apache/sysml/runtime/instructions/cp/CompressionCPInstruction.java
 
b/src/main/java/org/apache/sysml/runtime/instructions/cp/CompressionCPInstruction.java
index 630cdc2..b676a64 100644
--- 
a/src/main/java/org/apache/sysml/runtime/instructions/cp/CompressionCPInstruction.java
+++ 
b/src/main/java/org/apache/sysml/runtime/instructions/cp/CompressionCPInstruction.java
@@ -28,7 +28,7 @@ import org.apache.sysml.runtime.instructions.InstructionUtils;
 import org.apache.sysml.runtime.matrix.data.MatrixBlock;
 import org.apache.sysml.runtime.matrix.operators.Operator;
 
-public class CompressionCPInstruction extends UnaryCPInstruction {
+public class CompressionCPInstruction extends ComputationCPInstruction {
 
        private CompressionCPInstruction(Operator op, CPOperand in, CPOperand 
out, String opcode, String istr) {
                super(CPType.Compression, op, in, null, null, out, opcode, 
istr);

http://git-wip-us.apache.org/repos/asf/systemml/blob/9067654e/src/main/java/org/apache/sysml/runtime/instructions/cp/MatrixBuiltinCPInstruction.java
----------------------------------------------------------------------
diff --git 
a/src/main/java/org/apache/sysml/runtime/instructions/cp/MatrixBuiltinCPInstruction.java
 
b/src/main/java/org/apache/sysml/runtime/instructions/cp/MatrixBuiltinCPInstruction.java
deleted file mode 100644
index b120dab..0000000
--- 
a/src/main/java/org/apache/sysml/runtime/instructions/cp/MatrixBuiltinCPInstruction.java
+++ /dev/null
@@ -1,60 +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.sysml.runtime.instructions.cp;
-
-import org.apache.sysml.runtime.DMLRuntimeException;
-import org.apache.sysml.runtime.controlprogram.context.ExecutionContext;
-import org.apache.sysml.runtime.matrix.data.LibCommonsMath;
-import org.apache.sysml.runtime.matrix.data.MatrixBlock;
-import org.apache.sysml.runtime.matrix.operators.Operator;
-import org.apache.sysml.runtime.matrix.operators.UnaryOperator;
-
-public class MatrixBuiltinCPInstruction extends BuiltinUnaryCPInstruction {
-       protected MatrixBuiltinCPInstruction(Operator op, CPOperand in, 
CPOperand out, String opcode, String instr) {
-               super(op, in, out, opcode, instr);
-       }
-
-       @Override 
-       public void processInstruction(ExecutionContext ec) 
-               throws DMLRuntimeException 
-       {       
-               String output_name = output.getName();
-               
-               String opcode = getOpcode();
-               if(LibCommonsMath.isSupportedUnaryOperation(opcode)) {
-                       MatrixBlock retBlock = 
LibCommonsMath.unaryOperations(ec.getMatrixObject(input1.getName()),getOpcode());
-                       ec.setMatrixOutput(output_name, retBlock, 
getExtendedOpcode());
-               }
-               else {
-                       UnaryOperator u_op = (UnaryOperator) _optr;
-                       MatrixBlock inBlock = 
ec.getMatrixInput(input1.getName(), getExtendedOpcode());
-                       MatrixBlock retBlock = (MatrixBlock) 
(inBlock.unaryOperations(u_op, new MatrixBlock()));
-               
-                       ec.releaseMatrixInput(input1.getName(), 
getExtendedOpcode());
-                       
-                       // Ensure right dense/sparse output representation 
(guarded by released input memory)
-                       if( checkGuardedRepresentationChange(inBlock, retBlock) 
) {
-                               retBlock.examSparsity();
-                       }
-                       
-                       ec.setMatrixOutput(output_name, retBlock, 
getExtendedOpcode());
-               }
-       }
-}

http://git-wip-us.apache.org/repos/asf/systemml/blob/9067654e/src/main/java/org/apache/sysml/runtime/instructions/cp/ScalarBuiltinCPInstruction.java
----------------------------------------------------------------------
diff --git 
a/src/main/java/org/apache/sysml/runtime/instructions/cp/ScalarBuiltinCPInstruction.java
 
b/src/main/java/org/apache/sysml/runtime/instructions/cp/ScalarBuiltinCPInstruction.java
deleted file mode 100644
index 77fb73e..0000000
--- 
a/src/main/java/org/apache/sysml/runtime/instructions/cp/ScalarBuiltinCPInstruction.java
+++ /dev/null
@@ -1,80 +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.sysml.runtime.instructions.cp;
-
-import org.apache.sysml.api.DMLScript;
-import org.apache.sysml.parser.Expression.ValueType;
-import org.apache.sysml.runtime.DMLRuntimeException;
-import org.apache.sysml.runtime.DMLScriptException;
-import org.apache.sysml.runtime.controlprogram.context.ExecutionContext;
-import org.apache.sysml.runtime.matrix.operators.Operator;
-import org.apache.sysml.runtime.matrix.operators.SimpleOperator;
-
-public class ScalarBuiltinCPInstruction extends BuiltinUnaryCPInstruction {
-
-       protected ScalarBuiltinCPInstruction(Operator op, CPOperand in, 
CPOperand out, String opcode, String instr) {
-               super(op, in, out, opcode, instr);
-       }
-
-       @Override 
-       public void processInstruction(ExecutionContext ec) 
-               throws DMLRuntimeException 
-       {       
-               String opcode = getOpcode();
-               SimpleOperator dop = (SimpleOperator) _optr;
-               ScalarObject sores = null;
-               ScalarObject so = null;
-               
-               //get the scalar input 
-               so = ec.getScalarInput( input1.getName(), 
input1.getValueType(), input1.isLiteral() );
-                       
-               //core execution
-               if ( opcode.equalsIgnoreCase("print") ) {
-                       String outString = so.getLanguageSpecificStringValue();
-                       
-                       // print to stdout only when suppress flag in DMLScript 
is not set.
-                       // The flag will be set, for example, when SystemML is 
invoked in fenced mode from Jaql.
-                       if (!DMLScript.suppressPrint2Stdout())
-                               System.out.println(outString);
-                       
-                       // String that is printed on stdout will be inserted 
into symbol table (dummy, not necessary!) 
-                       sores = new StringObject(outString);
-               }
-               else if ( opcode.equalsIgnoreCase("stop") ) {
-                       throw new DMLScriptException(so.getStringValue());
-               }
-               else {
-                       //Inputs for all builtins other than PRINT are treated 
as DOUBLE.
-                       if ( so instanceof IntObject  && output.getValueType() 
== ValueType.INT )
-                       {
-                               long rval = (long) 
dop.fn.execute(so.getLongValue());
-                               sores = (ScalarObject) new IntObject(rval);
-                       }
-                       else 
-                       {
-                               double rval = 
dop.fn.execute(so.getDoubleValue());
-                               sores = (ScalarObject) new DoubleObject(rval);
-                       }
-               }
-               
-               ec.setScalarOutput(output.getName(), sores);
-       }
-
-}

http://git-wip-us.apache.org/repos/asf/systemml/blob/9067654e/src/main/java/org/apache/sysml/runtime/instructions/cp/UnaryCPInstruction.java
----------------------------------------------------------------------
diff --git 
a/src/main/java/org/apache/sysml/runtime/instructions/cp/UnaryCPInstruction.java
 
b/src/main/java/org/apache/sysml/runtime/instructions/cp/UnaryCPInstruction.java
index a89f2bc..fa9200f 100644
--- 
a/src/main/java/org/apache/sysml/runtime/instructions/cp/UnaryCPInstruction.java
+++ 
b/src/main/java/org/apache/sysml/runtime/instructions/cp/UnaryCPInstruction.java
@@ -19,11 +19,16 @@
 
 package org.apache.sysml.runtime.instructions.cp;
 
+import java.util.Arrays;
+
+import org.apache.sysml.parser.Expression.DataType;
+import org.apache.sysml.parser.Expression.ValueType;
 import org.apache.sysml.runtime.DMLRuntimeException;
-import org.apache.sysml.runtime.functionobjects.Not;
+import org.apache.sysml.runtime.functionobjects.Builtin;
+import org.apache.sysml.runtime.functionobjects.ValueFunction;
 import org.apache.sysml.runtime.instructions.InstructionUtils;
+import org.apache.sysml.runtime.matrix.data.LibCommonsMath;
 import org.apache.sysml.runtime.matrix.operators.Operator;
-import org.apache.sysml.runtime.matrix.operators.SimpleOperator;
 import org.apache.sysml.runtime.matrix.operators.UnaryOperator;
 
 public abstract class UnaryCPInstruction extends ComputationCPInstruction {
@@ -41,6 +46,41 @@ public abstract class UnaryCPInstruction extends 
ComputationCPInstruction {
                        String instr) {
                super(type, op, in1, in2, in3, out, opcode, instr);
        }
+       
+       public static UnaryCPInstruction parseInstruction ( String str ) 
+               throws DMLRuntimeException
+       {
+               CPOperand in = new CPOperand("", ValueType.UNKNOWN, 
DataType.UNKNOWN);
+               CPOperand out = new CPOperand("", ValueType.UNKNOWN, 
DataType.UNKNOWN);
+               
+               String[] parts = 
InstructionUtils.getInstructionPartsWithValueType(str);
+               String opcode = null;
+               ValueFunction func = null;
+               
+               //print or stop or cumulative aggregates
+               if( parts.length==4 ) {
+                       opcode = parts[0];
+                       in.split(parts[1]);
+                       out.split(parts[2]);
+                       func = Builtin.getBuiltinFnObject(opcode);
+                       
+                       if( Arrays.asList(new 
String[]{"ucumk+","ucum*","ucummin","ucummax"}).contains(opcode) )
+                               return new UnaryMatrixCPInstruction(new 
UnaryOperator(func,Integer.parseInt(parts[3])), in, out, opcode, str); 
+                       else
+                               return new UnaryScalarCPInstruction(null, in, 
out, opcode, str);
+               }
+               else { //2+1, general case
+                       opcode = parseUnaryInstruction(str, in, out);
+                       
+                       if(in.getDataType() == DataType.SCALAR)
+                               return new 
UnaryScalarCPInstruction(InstructionUtils.parseUnaryOperator(opcode), in, out, 
opcode, str);
+                       else if(in.getDataType() == DataType.MATRIX)
+                               return new 
UnaryMatrixCPInstruction(LibCommonsMath.isSupportedUnaryOperation(opcode) ?
+                                       null : 
InstructionUtils.parseUnaryOperator(opcode), in, out, opcode, str);
+               }
+               
+               return null;
+       }
 
        static String parseUnaryInstruction(String instr, CPOperand in,
                        CPOperand out) throws DMLRuntimeException {
@@ -88,20 +128,4 @@ public abstract class UnaryCPInstruction extends 
ComputationCPInstruction {
                }
                return opcode;
        }
-
-       static SimpleOperator getSimpleUnaryOperator(String opcode)
-                       throws DMLRuntimeException {
-               if (opcode.equalsIgnoreCase("!"))
-                       return new SimpleOperator(Not.getNotFnObject());
-
-               throw new DMLRuntimeException("Unknown unary operator " + 
opcode);
-       }
-
-       static UnaryOperator getUnaryOperator(String opcode)
-               throws DMLRuntimeException {
-               if (opcode.equalsIgnoreCase("!"))
-                       return new UnaryOperator(Not.getNotFnObject());
-
-               throw new DMLRuntimeException("Unknown unary operator " + 
opcode);
-       }
 }

http://git-wip-us.apache.org/repos/asf/systemml/blob/9067654e/src/main/java/org/apache/sysml/runtime/instructions/cp/UnaryMatrixCPInstruction.java
----------------------------------------------------------------------
diff --git 
a/src/main/java/org/apache/sysml/runtime/instructions/cp/UnaryMatrixCPInstruction.java
 
b/src/main/java/org/apache/sysml/runtime/instructions/cp/UnaryMatrixCPInstruction.java
new file mode 100644
index 0000000..e57d0e6
--- /dev/null
+++ 
b/src/main/java/org/apache/sysml/runtime/instructions/cp/UnaryMatrixCPInstruction.java
@@ -0,0 +1,60 @@
+/*
+ * 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.sysml.runtime.instructions.cp;
+
+import org.apache.sysml.runtime.DMLRuntimeException;
+import org.apache.sysml.runtime.controlprogram.context.ExecutionContext;
+import org.apache.sysml.runtime.matrix.data.LibCommonsMath;
+import org.apache.sysml.runtime.matrix.data.MatrixBlock;
+import org.apache.sysml.runtime.matrix.operators.Operator;
+import org.apache.sysml.runtime.matrix.operators.UnaryOperator;
+
+public class UnaryMatrixCPInstruction extends UnaryCPInstruction {
+       protected UnaryMatrixCPInstruction(Operator op, CPOperand in, CPOperand 
out, String opcode, String instr) {
+               super(CPType.Unary, op, in, out, opcode, instr);
+       }
+
+       @Override 
+       public void processInstruction(ExecutionContext ec) 
+               throws DMLRuntimeException 
+       {       
+               String output_name = output.getName();
+               
+               String opcode = getOpcode();
+               if(LibCommonsMath.isSupportedUnaryOperation(opcode)) {
+                       MatrixBlock retBlock = 
LibCommonsMath.unaryOperations(ec.getMatrixObject(input1.getName()),getOpcode());
+                       ec.setMatrixOutput(output_name, retBlock, 
getExtendedOpcode());
+               }
+               else {
+                       UnaryOperator u_op = (UnaryOperator) _optr;
+                       MatrixBlock inBlock = 
ec.getMatrixInput(input1.getName(), getExtendedOpcode());
+                       MatrixBlock retBlock = (MatrixBlock) 
(inBlock.unaryOperations(u_op, new MatrixBlock()));
+               
+                       ec.releaseMatrixInput(input1.getName(), 
getExtendedOpcode());
+                       
+                       // Ensure right dense/sparse output representation 
(guarded by released input memory)
+                       if( checkGuardedRepresentationChange(inBlock, retBlock) 
) {
+                               retBlock.examSparsity();
+                       }
+                       
+                       ec.setMatrixOutput(output_name, retBlock, 
getExtendedOpcode());
+               }
+       }
+}

http://git-wip-us.apache.org/repos/asf/systemml/blob/9067654e/src/main/java/org/apache/sysml/runtime/instructions/cp/UnaryScalarCPInstruction.java
----------------------------------------------------------------------
diff --git 
a/src/main/java/org/apache/sysml/runtime/instructions/cp/UnaryScalarCPInstruction.java
 
b/src/main/java/org/apache/sysml/runtime/instructions/cp/UnaryScalarCPInstruction.java
new file mode 100644
index 0000000..b582fdc
--- /dev/null
+++ 
b/src/main/java/org/apache/sysml/runtime/instructions/cp/UnaryScalarCPInstruction.java
@@ -0,0 +1,75 @@
+/*
+ * 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.sysml.runtime.instructions.cp;
+
+import org.apache.sysml.api.DMLScript;
+import org.apache.sysml.parser.Expression.ValueType;
+import org.apache.sysml.runtime.DMLRuntimeException;
+import org.apache.sysml.runtime.DMLScriptException;
+import org.apache.sysml.runtime.controlprogram.context.ExecutionContext;
+import org.apache.sysml.runtime.matrix.operators.Operator;
+import org.apache.sysml.runtime.matrix.operators.UnaryOperator;
+
+public class UnaryScalarCPInstruction extends UnaryMatrixCPInstruction {
+
+       protected UnaryScalarCPInstruction(Operator op, CPOperand in, CPOperand 
out, String opcode, String instr) {
+               super(op, in, out, opcode, instr);
+       }
+
+       @Override 
+       public void processInstruction(ExecutionContext ec) 
+               throws DMLRuntimeException 
+       {       
+               String opcode = getOpcode();
+               ScalarObject sores = null;
+               ScalarObject so = null;
+               
+               //get the scalar input 
+               so = ec.getScalarInput( input1.getName(), 
input1.getValueType(), input1.isLiteral() );
+               
+               //core execution
+               if ( opcode.equalsIgnoreCase("print") ) {
+                       String outString = so.getLanguageSpecificStringValue();
+                       
+                       // print to stdout only when suppress flag in DMLScript 
is not set.
+                       // The flag will be set, for example, when SystemML is 
invoked in fenced mode from Jaql.
+                       if (!DMLScript.suppressPrint2Stdout())
+                               System.out.println(outString);
+                       
+                       // String that is printed on stdout will be inserted 
into symbol table (dummy, not necessary!) 
+                       sores = new StringObject(outString);
+               }
+               else if ( opcode.equalsIgnoreCase("stop") ) {
+                       throw new DMLScriptException(so.getStringValue());
+               }
+               else {
+                       UnaryOperator dop = (UnaryOperator) _optr;
+                       if ( so instanceof IntObject && output.getValueType() 
== ValueType.INT )
+                               sores = new 
IntObject((long)dop.fn.execute(so.getLongValue()));
+                       else if( so instanceof BooleanObject && 
output.getValueType() == ValueType.BOOLEAN )
+                               sores = new 
BooleanObject(dop.fn.execute(so.getBooleanValue()));
+                       else
+                               sores = new 
DoubleObject(dop.fn.execute(so.getDoubleValue()));
+               }
+               
+               ec.setScalarOutput(output.getName(), sores);
+       }
+
+}

http://git-wip-us.apache.org/repos/asf/systemml/blob/9067654e/src/main/java/org/apache/sysml/runtime/instructions/mr/BinaryInstruction.java
----------------------------------------------------------------------
diff --git 
a/src/main/java/org/apache/sysml/runtime/instructions/mr/BinaryInstruction.java 
b/src/main/java/org/apache/sysml/runtime/instructions/mr/BinaryInstruction.java
index e67e425..cde7f78 100644
--- 
a/src/main/java/org/apache/sysml/runtime/instructions/mr/BinaryInstruction.java
+++ 
b/src/main/java/org/apache/sysml/runtime/instructions/mr/BinaryInstruction.java
@@ -51,7 +51,7 @@ public class BinaryInstruction extends 
BinaryMRInstructionBase {
                
                BinaryOperator bop = 
InstructionUtils.parseBinaryOperator(opcode);
                if( bop != null )
-                       return new BinaryInstruction(MRType.ArithmeticBinary, 
bop, in1, in2, out, str);
+                       return new BinaryInstruction(MRType.Binary, bop, in1, 
in2, out, str);
                else
                        return null;
        }

http://git-wip-us.apache.org/repos/asf/systemml/blob/9067654e/src/main/java/org/apache/sysml/runtime/instructions/mr/BinaryMInstruction.java
----------------------------------------------------------------------
diff --git 
a/src/main/java/org/apache/sysml/runtime/instructions/mr/BinaryMInstruction.java
 
b/src/main/java/org/apache/sysml/runtime/instructions/mr/BinaryMInstruction.java
index 07615e8..c8ebdfd 100644
--- 
a/src/main/java/org/apache/sysml/runtime/instructions/mr/BinaryMInstruction.java
+++ 
b/src/main/java/org/apache/sysml/runtime/instructions/mr/BinaryMInstruction.java
@@ -38,7 +38,7 @@ public class BinaryMInstruction extends 
BinaryMRInstructionBase implements IDist
        private VectorType _vectorType = null;
 
        private BinaryMInstruction(Operator op, byte in1, byte in2, CacheType 
ctype, VectorType vtype, byte out, String istr) {
-               super(MRType.ArithmeticBinary, op, in1, in2, out);
+               super(MRType.Binary, op, in1, in2, out);
                instString = istr;
                _vectorType = vtype;
        }

http://git-wip-us.apache.org/repos/asf/systemml/blob/9067654e/src/main/java/org/apache/sysml/runtime/instructions/mr/MRInstruction.java
----------------------------------------------------------------------
diff --git 
a/src/main/java/org/apache/sysml/runtime/instructions/mr/MRInstruction.java 
b/src/main/java/org/apache/sysml/runtime/instructions/mr/MRInstruction.java
index 51f4d8b..b2e5283 100644
--- a/src/main/java/org/apache/sysml/runtime/instructions/mr/MRInstruction.java
+++ b/src/main/java/org/apache/sysml/runtime/instructions/mr/MRInstruction.java
@@ -30,7 +30,7 @@ import org.apache.sysml.runtime.matrix.operators.Operator;
 public abstract class MRInstruction extends Instruction {
 
        public enum MRType {
-               Append, Aggregate, ArithmeticBinary, ArithmeticBinary2, 
AggregateBinary, AggregateUnary, Rand,
+               Append, Aggregate, Binary, Binary2, AggregateBinary, 
AggregateUnary, Rand,
                Seq, CSVReblock, CSVWrite, Reblock, Reorg, Replicate, Unary, 
CombineBinary, CombineUnary, CombineTernary,
                PickByCount, Partition, Ternary, Quaternary, CM_N_COV, 
MapGroupedAggregate, GroupedAggregate, RightIndex,
                ZeroOut, MMTSJ, PMMJ, MatrixReshape, ParameterizedBuiltin, 
Sort, MapMultChain, CumsumAggregate, CumsumSplit,

http://git-wip-us.apache.org/repos/asf/systemml/blob/9067654e/src/main/java/org/apache/sysml/runtime/instructions/mr/PlusMultInstruction.java
----------------------------------------------------------------------
diff --git 
a/src/main/java/org/apache/sysml/runtime/instructions/mr/PlusMultInstruction.java
 
b/src/main/java/org/apache/sysml/runtime/instructions/mr/PlusMultInstruction.java
index 40a851a..ff9a426 100644
--- 
a/src/main/java/org/apache/sysml/runtime/instructions/mr/PlusMultInstruction.java
+++ 
b/src/main/java/org/apache/sysml/runtime/instructions/mr/PlusMultInstruction.java
@@ -30,7 +30,7 @@ import org.apache.sysml.runtime.matrix.operators.Operator;
 
 public class PlusMultInstruction extends BinaryInstruction {
        private PlusMultInstruction(Operator op, byte in1, byte in2, byte out, 
String istr) {
-               super(MRType.ArithmeticBinary2, op, in1, in2, out, istr);
+               super(MRType.Binary2, op, in1, in2, out, istr);
        }
 
        public static PlusMultInstruction parseInstruction ( String str ) 

http://git-wip-us.apache.org/repos/asf/systemml/blob/9067654e/src/main/java/org/apache/sysml/runtime/instructions/mr/ScalarInstruction.java
----------------------------------------------------------------------
diff --git 
a/src/main/java/org/apache/sysml/runtime/instructions/mr/ScalarInstruction.java 
b/src/main/java/org/apache/sysml/runtime/instructions/mr/ScalarInstruction.java
index 534ad50..762a193 100644
--- 
a/src/main/java/org/apache/sysml/runtime/instructions/mr/ScalarInstruction.java
+++ 
b/src/main/java/org/apache/sysml/runtime/instructions/mr/ScalarInstruction.java
@@ -26,7 +26,6 @@ import org.apache.sysml.parser.Expression.DataType;
 import org.apache.sysml.runtime.DMLRuntimeException;
 import org.apache.sysml.runtime.instructions.InstructionUtils;
 import org.apache.sysml.runtime.matrix.data.MatrixValue;
-import org.apache.sysml.runtime.matrix.data.OperationsOnMatrixValues;
 import org.apache.sysml.runtime.matrix.mapred.CachedValueMap;
 import org.apache.sysml.runtime.matrix.mapred.IndexedMatrixValue;
 import org.apache.sysml.runtime.matrix.operators.ScalarOperator;
@@ -34,7 +33,7 @@ import 
org.apache.sysml.runtime.matrix.operators.ScalarOperator;
 public class ScalarInstruction extends UnaryMRInstructionBase {
 
        private ScalarInstruction(ScalarOperator op, byte in, byte out, String 
istr) {
-               super(MRType.ArithmeticBinary, op, in, out);
+               super(MRType.Binary, op, in, out);
                instString = istr;
        }
 
@@ -61,21 +60,16 @@ public class ScalarInstruction extends 
UnaryMRInstructionBase {
        {
                ArrayList<IndexedMatrixValue> blkList = cachedValues.get(input);
                if( blkList != null )
-                       for( IndexedMatrixValue in : blkList )
-                       {
-                               if(in==null)
-                                       continue;
+                       for( IndexedMatrixValue in : blkList ) {
+                               if(in==null) continue;
                        
                                //allocate space for the output value
-                               IndexedMatrixValue out;
-                               if(input==output)
-                                       out=tempValue;
-                               else
-                                       out=cachedValues.holdPlace(output, 
valueClass);
+                               IndexedMatrixValue out = (input==output) ? 
tempValue :
+                                       cachedValues.holdPlace(output, 
valueClass);
                                
                                //process instruction
                                out.getIndexes().setIndexes(in.getIndexes());
-                               
OperationsOnMatrixValues.performScalarIgnoreIndexes(in.getValue(), 
out.getValue(), ((ScalarOperator)this.optr));
+                               
in.getValue().scalarOperations((ScalarOperator)this.optr, out.getValue());
                                
                                //put the output value in the cache
                                if(out==tempValue)
@@ -83,8 +77,7 @@ public class ScalarInstruction extends UnaryMRInstructionBase 
{
                        }
        }
 
-       private static boolean isFirstArgumentScalar(String inst)
-       {
+       private static boolean isFirstArgumentScalar(String inst) {
                //get first argument
                String[] parts = 
InstructionUtils.getInstructionPartsWithValueType(inst);
                String arg1 = parts[1];

http://git-wip-us.apache.org/repos/asf/systemml/blob/9067654e/src/main/java/org/apache/sysml/runtime/instructions/mr/UnaryInstruction.java
----------------------------------------------------------------------
diff --git 
a/src/main/java/org/apache/sysml/runtime/instructions/mr/UnaryInstruction.java 
b/src/main/java/org/apache/sysml/runtime/instructions/mr/UnaryInstruction.java
index 7974d6a..49f91fa 100644
--- 
a/src/main/java/org/apache/sysml/runtime/instructions/mr/UnaryInstruction.java
+++ 
b/src/main/java/org/apache/sysml/runtime/instructions/mr/UnaryInstruction.java
@@ -22,10 +22,8 @@ package org.apache.sysml.runtime.instructions.mr;
 import java.util.ArrayList;
 
 import org.apache.sysml.runtime.DMLRuntimeException;
-import org.apache.sysml.runtime.functionobjects.Builtin;
 import org.apache.sysml.runtime.instructions.InstructionUtils;
 import org.apache.sysml.runtime.matrix.data.MatrixValue;
-import org.apache.sysml.runtime.matrix.data.OperationsOnMatrixValues;
 import org.apache.sysml.runtime.matrix.mapred.CachedValueMap;
 import org.apache.sysml.runtime.matrix.mapred.IndexedMatrixValue;
 import org.apache.sysml.runtime.matrix.operators.Operator;
@@ -45,14 +43,8 @@ public class UnaryInstruction extends UnaryMRInstructionBase 
{
                byte in, out;
                in = Byte.parseByte(parts[1]);
                out = Byte.parseByte(parts[2]);
-               
-               // Currently, UnaryInstructions are used primarily for 
executing Builtins like SIN(A), LOG(A,2)
-               if ( InstructionUtils.isBuiltinFunction(opcode) ) {
-                       UnaryOperator unary = new 
UnaryOperator(Builtin.getBuiltinFnObject(opcode));
-                       return new UnaryInstruction(MRType.Unary, unary, in, 
out, str);
-               }
-               else 
-                       return new UnaryInstruction(MRType.Unary, null, in, 
out, str);
+               return new UnaryInstruction(MRType.Unary,
+                       InstructionUtils.parseUnaryOperator(opcode), in, out, 
str);
        }
 
        @Override
@@ -64,21 +56,16 @@ public class UnaryInstruction extends 
UnaryMRInstructionBase {
                ArrayList<IndexedMatrixValue> blkList = cachedValues.get(input);
                
                if( blkList != null )
-                       for(IndexedMatrixValue in : blkList )
-                       {
-                               if(in==null)
-                                       continue;
+                       for(IndexedMatrixValue in : blkList ) {
+                               if(in==null) continue;
                                
                                //allocate space for the output value
-                               IndexedMatrixValue out;
-                               if(input==output)
-                                       out=tempValue;
-                               else
-                                       out=cachedValues.holdPlace(output, 
valueClass);
+                               IndexedMatrixValue out= (input==output) ? 
tempValue :
+                                       cachedValues.holdPlace(output, 
valueClass);
                                
                                //process instruction
                                out.getIndexes().setIndexes(in.getIndexes());
-                               
OperationsOnMatrixValues.performUnaryIgnoreIndexes(in.getValue(), 
out.getValue(), (UnaryOperator)optr);
+                               
in.getValue().unaryOperations((UnaryOperator)optr, out.getValue());
                                
                                //put the output value in the cache
                                if(out==tempValue)

http://git-wip-us.apache.org/repos/asf/systemml/blob/9067654e/src/main/java/org/apache/sysml/runtime/instructions/spark/BuiltinUnarySPInstruction.java
----------------------------------------------------------------------
diff --git 
a/src/main/java/org/apache/sysml/runtime/instructions/spark/BuiltinUnarySPInstruction.java
 
b/src/main/java/org/apache/sysml/runtime/instructions/spark/BuiltinUnarySPInstruction.java
deleted file mode 100644
index d158445..0000000
--- 
a/src/main/java/org/apache/sysml/runtime/instructions/spark/BuiltinUnarySPInstruction.java
+++ /dev/null
@@ -1,47 +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.sysml.runtime.instructions.spark;
-
-import org.apache.sysml.parser.Expression.DataType;
-import org.apache.sysml.parser.Expression.ValueType;
-import org.apache.sysml.runtime.DMLRuntimeException;
-import org.apache.sysml.runtime.functionobjects.Builtin;
-import org.apache.sysml.runtime.functionobjects.ValueFunction;
-import org.apache.sysml.runtime.instructions.cp.CPOperand;
-import org.apache.sysml.runtime.matrix.operators.Operator;
-import org.apache.sysml.runtime.matrix.operators.UnaryOperator;
-
-public abstract class BuiltinUnarySPInstruction extends UnarySPInstruction {
-
-       protected BuiltinUnarySPInstruction(Operator op, CPOperand in, 
CPOperand out, String opcode, String istr) {
-               super(SPType.BuiltinUnary, op, in, out, opcode, istr);
-       }
-
-       public static BuiltinUnarySPInstruction parseInstruction ( String str ) 
-                       throws DMLRuntimeException 
-       {
-               CPOperand in = new CPOperand("", ValueType.UNKNOWN, 
DataType.UNKNOWN);
-               CPOperand out = new CPOperand("", ValueType.UNKNOWN, 
DataType.UNKNOWN);
-               
-               String opcode = parseUnaryInstruction(str, in, out);
-               ValueFunction func = Builtin.getBuiltinFnObject(opcode);
-               return new MatrixBuiltinSPInstruction(new UnaryOperator(func), 
in, out, opcode, str);
-       }
-}

http://git-wip-us.apache.org/repos/asf/systemml/blob/9067654e/src/main/java/org/apache/sysml/runtime/instructions/spark/MatrixBuiltinSPInstruction.java
----------------------------------------------------------------------
diff --git 
a/src/main/java/org/apache/sysml/runtime/instructions/spark/MatrixBuiltinSPInstruction.java
 
b/src/main/java/org/apache/sysml/runtime/instructions/spark/MatrixBuiltinSPInstruction.java
deleted file mode 100644
index f6e6264..0000000
--- 
a/src/main/java/org/apache/sysml/runtime/instructions/spark/MatrixBuiltinSPInstruction.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.sysml.runtime.instructions.spark;
-
-import org.apache.spark.api.java.JavaPairRDD;
-import org.apache.spark.api.java.function.Function;
-import org.apache.sysml.runtime.DMLRuntimeException;
-import org.apache.sysml.runtime.controlprogram.context.ExecutionContext;
-import org.apache.sysml.runtime.controlprogram.context.SparkExecutionContext;
-import org.apache.sysml.runtime.instructions.cp.CPOperand;
-import org.apache.sysml.runtime.matrix.data.MatrixBlock;
-import org.apache.sysml.runtime.matrix.data.MatrixIndexes;
-import org.apache.sysml.runtime.matrix.operators.Operator;
-import org.apache.sysml.runtime.matrix.operators.UnaryOperator;
-
-public class MatrixBuiltinSPInstruction extends BuiltinUnarySPInstruction {
-
-       protected MatrixBuiltinSPInstruction(Operator op, CPOperand in, 
CPOperand out, String opcode, String instr) {
-               super(op, in, out, opcode, instr);
-       }
-
-       @Override 
-       public void processInstruction(ExecutionContext ec) 
-               throws DMLRuntimeException 
-       {       
-               SparkExecutionContext sec = (SparkExecutionContext)ec;
-               
-               //get input
-               JavaPairRDD<MatrixIndexes,MatrixBlock> in = 
sec.getBinaryBlockRDDHandleForVariable( input1.getName() );
-               
-               //execute unary builtin operation
-               UnaryOperator uop = (UnaryOperator) _optr;
-               JavaPairRDD<MatrixIndexes,MatrixBlock> out = in.mapValues(new 
RDDMatrixBuiltinUnaryOp(uop));
-               
-               //set output RDD
-               updateUnaryOutputMatrixCharacteristics(sec);
-               sec.setRDDHandleForVariable(output.getName(), out);     
-               sec.addLineageRDD(output.getName(), input1.getName());
-       }
-
-       private static class RDDMatrixBuiltinUnaryOp implements 
Function<MatrixBlock,MatrixBlock> 
-       {
-               private static final long serialVersionUID = 
-3128192099832877491L;
-               
-               private UnaryOperator _op = null;
-               
-               public RDDMatrixBuiltinUnaryOp(UnaryOperator u_op) {
-                       _op = u_op;
-               }
-
-               @Override
-               public MatrixBlock call(MatrixBlock arg0) 
-                       throws Exception 
-               {
-                       return (MatrixBlock) arg0.unaryOperations(_op, new 
MatrixBlock());
-               }               
-       }
-}
-

http://git-wip-us.apache.org/repos/asf/systemml/blob/9067654e/src/main/java/org/apache/sysml/runtime/instructions/spark/SPInstruction.java
----------------------------------------------------------------------
diff --git 
a/src/main/java/org/apache/sysml/runtime/instructions/spark/SPInstruction.java 
b/src/main/java/org/apache/sysml/runtime/instructions/spark/SPInstruction.java
index 8ff4640..d1c12ef 100644
--- 
a/src/main/java/org/apache/sysml/runtime/instructions/spark/SPInstruction.java
+++ 
b/src/main/java/org/apache/sysml/runtime/instructions/spark/SPInstruction.java
@@ -33,7 +33,7 @@ public abstract class SPInstruction extends Instruction {
                MAPMM, MAPMMCHAIN, CPMM, RMM, TSMM, TSMM2, PMM, ZIPMM, PMAPMM, 
//matrix multiplication instructions  
                MatrixIndexing, Reorg, Binary,
                AggregateUnary, AggregateTernary, Reblock, CSVReblock, 
-               Builtin, BuiltinUnary, BuiltinNary, MultiReturnBuiltin, 
Checkpoint, Compression, Cast,
+               Builtin, Unary, BuiltinNary, MultiReturnBuiltin, Checkpoint, 
Compression, Cast,
                CentralMoment, Covariance, QSort, QPick, 
                ParameterizedBuiltin, MAppend, RAppend, GAppend, 
GAlignedAppend, Rand, 
                MatrixReshape, Ternary, Quaternary, CumsumAggregate, 
CumsumOffset, BinUaggChain, UaggOuterChain, 

http://git-wip-us.apache.org/repos/asf/systemml/blob/9067654e/src/main/java/org/apache/sysml/runtime/instructions/spark/UnaryMatrixSPInstruction.java
----------------------------------------------------------------------
diff --git 
a/src/main/java/org/apache/sysml/runtime/instructions/spark/UnaryMatrixSPInstruction.java
 
b/src/main/java/org/apache/sysml/runtime/instructions/spark/UnaryMatrixSPInstruction.java
new file mode 100644
index 0000000..fb1e626
--- /dev/null
+++ 
b/src/main/java/org/apache/sysml/runtime/instructions/spark/UnaryMatrixSPInstruction.java
@@ -0,0 +1,87 @@
+/*
+ * 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.sysml.runtime.instructions.spark;
+
+import org.apache.spark.api.java.JavaPairRDD;
+import org.apache.spark.api.java.function.Function;
+import org.apache.sysml.parser.Expression.DataType;
+import org.apache.sysml.parser.Expression.ValueType;
+import org.apache.sysml.runtime.DMLRuntimeException;
+import org.apache.sysml.runtime.controlprogram.context.ExecutionContext;
+import org.apache.sysml.runtime.controlprogram.context.SparkExecutionContext;
+import org.apache.sysml.runtime.instructions.InstructionUtils;
+import org.apache.sysml.runtime.instructions.cp.CPOperand;
+import org.apache.sysml.runtime.matrix.data.MatrixBlock;
+import org.apache.sysml.runtime.matrix.data.MatrixIndexes;
+import org.apache.sysml.runtime.matrix.operators.Operator;
+import org.apache.sysml.runtime.matrix.operators.UnaryOperator;
+
+public class UnaryMatrixSPInstruction extends UnarySPInstruction {
+
+       protected UnaryMatrixSPInstruction(Operator op, CPOperand in, CPOperand 
out, String opcode, String instr) {
+               super(SPType.Unary, op, in, out, opcode, instr);
+       }
+       
+       public static UnarySPInstruction parseInstruction ( String str ) throws 
DMLRuntimeException {
+               CPOperand in = new CPOperand("", ValueType.UNKNOWN, 
DataType.UNKNOWN);
+               CPOperand out = new CPOperand("", ValueType.UNKNOWN, 
DataType.UNKNOWN);
+               String opcode = parseUnaryInstruction(str, in, out);
+               return new UnaryMatrixSPInstruction(
+                       InstructionUtils.parseUnaryOperator(opcode), in, out, 
opcode, str);
+       }
+
+       @Override 
+       public void processInstruction(ExecutionContext ec) 
+               throws DMLRuntimeException 
+       {       
+               SparkExecutionContext sec = (SparkExecutionContext)ec;
+               
+               //get input
+               JavaPairRDD<MatrixIndexes,MatrixBlock> in = 
sec.getBinaryBlockRDDHandleForVariable( input1.getName() );
+               
+               //execute unary builtin operation
+               UnaryOperator uop = (UnaryOperator) _optr;
+               JavaPairRDD<MatrixIndexes,MatrixBlock> out = in.mapValues(new 
RDDMatrixBuiltinUnaryOp(uop));
+               
+               //set output RDD
+               updateUnaryOutputMatrixCharacteristics(sec);
+               sec.setRDDHandleForVariable(output.getName(), out);     
+               sec.addLineageRDD(output.getName(), input1.getName());
+       }
+
+       private static class RDDMatrixBuiltinUnaryOp implements 
Function<MatrixBlock,MatrixBlock> 
+       {
+               private static final long serialVersionUID = 
-3128192099832877491L;
+               
+               private UnaryOperator _op = null;
+               
+               public RDDMatrixBuiltinUnaryOp(UnaryOperator u_op) {
+                       _op = u_op;
+               }
+
+               @Override
+               public MatrixBlock call(MatrixBlock arg0) 
+                       throws Exception 
+               {
+                       return (MatrixBlock) arg0.unaryOperations(_op, new 
MatrixBlock());
+               }               
+       }
+}
+

http://git-wip-us.apache.org/repos/asf/systemml/blob/9067654e/src/main/java/org/apache/sysml/runtime/matrix/data/MatrixBlock.java
----------------------------------------------------------------------
diff --git 
a/src/main/java/org/apache/sysml/runtime/matrix/data/MatrixBlock.java 
b/src/main/java/org/apache/sysml/runtime/matrix/data/MatrixBlock.java
index ff70cfd..e8d3d76 100644
--- a/src/main/java/org/apache/sysml/runtime/matrix/data/MatrixBlock.java
+++ b/src/main/java/org/apache/sysml/runtime/matrix/data/MatrixBlock.java
@@ -2670,6 +2670,7 @@ public class MatrixBlock extends MatrixValue implements 
CacheBlock, Externalizab
                }
                else if( sparse ) //DENSE <- SPARSE
                {
+                       ret.allocateDenseBlock(false);
                        SparseBlock a = sparseBlock;
                        DenseBlock c = ret.denseBlock;
                        long nnz = (ret.nonZeros > 0) ?
@@ -2693,7 +2694,7 @@ public class MatrixBlock extends MatrixValue implements 
CacheBlock, Externalizab
                else //DENSE <- DENSE
                {
                        //allocate dense output block
-                       ret.allocateDenseBlock();
+                       ret.allocateDenseBlock(false);
                        DenseBlock da = getDenseBlock();
                        DenseBlock dc = ret.getDenseBlock();
                        

Reply via email to