You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@systemml.apache.org by mb...@apache.org on 2018/01/18 19:20:29 UTC

[1/5] systemml git commit: [MINOR] Refactoring agg-binary, ternary, and quaternary instructions

Repository: systemml
Updated Branches:
  refs/heads/master 5b0fb0ccf -> ce9e42fef


http://git-wip-us.apache.org/repos/asf/systemml/blob/57438103/src/main/java/org/apache/sysml/runtime/matrix/data/MatrixCell.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/sysml/runtime/matrix/data/MatrixCell.java b/src/main/java/org/apache/sysml/runtime/matrix/data/MatrixCell.java
index b1190ad..a9b5b37 100644
--- a/src/main/java/org/apache/sysml/runtime/matrix/data/MatrixCell.java
+++ b/src/main/java/org/apache/sysml/runtime/matrix/data/MatrixCell.java
@@ -32,12 +32,10 @@ import org.apache.sysml.runtime.DMLRuntimeException;
 import org.apache.sysml.runtime.functionobjects.CTable;
 import org.apache.sysml.runtime.functionobjects.ReduceDiag;
 import org.apache.sysml.runtime.matrix.mapred.IndexedMatrixValue;
-import org.apache.sysml.runtime.matrix.operators.AggregateBinaryOperator;
 import org.apache.sysml.runtime.matrix.operators.AggregateOperator;
 import org.apache.sysml.runtime.matrix.operators.AggregateUnaryOperator;
 import org.apache.sysml.runtime.matrix.operators.BinaryOperator;
 import org.apache.sysml.runtime.matrix.operators.Operator;
-import org.apache.sysml.runtime.matrix.operators.QuaternaryOperator;
 import org.apache.sysml.runtime.matrix.operators.ReorgOperator;
 import org.apache.sysml.runtime.matrix.operators.ScalarOperator;
 import org.apache.sysml.runtime.matrix.operators.UnaryOperator;
@@ -170,20 +168,6 @@ public class MatrixCell extends MatrixValue implements WritableComparable, Seria
 	}
 
 	@Override
-	public MatrixValue aggregateBinaryOperations(MatrixValue value1,
-			MatrixValue value2, MatrixValue result, AggregateBinaryOperator op) 
-	throws DMLRuntimeException {
-		
-		MatrixCell c1=checkType(value1);
-		MatrixCell c2=checkType(value2);
-		MatrixCell c3=checkType(result);
-		if(c3==null)
-			c3=new MatrixCell();
-		c3.setValue(op.binaryFn.execute(c1.getValue(), c2.getValue()));
-		return c3;
-	}
-
-	@Override
 	public MatrixValue aggregateUnaryOperations(AggregateUnaryOperator op,
 			MatrixValue result, int brlen, int bclen,
 			MatrixIndexes indexesIn) throws DMLRuntimeException {
@@ -314,7 +298,7 @@ public class MatrixCell extends MatrixValue implements WritableComparable, Seria
 	}
 
 	@Override
-	public void ternaryOperations(Operator op, MatrixValue that,
+	public void ctableOperations(Operator op, MatrixValue that,
 			MatrixValue that2, CTableMap resultMap, MatrixBlock resultBlock)
 			throws DMLRuntimeException 
 	{
@@ -328,32 +312,32 @@ public class MatrixCell extends MatrixValue implements WritableComparable, Seria
 	}
 
 	@Override
-	public void ternaryOperations(Operator op, MatrixValue that, double scalarThat2, boolean ignoreZeros, 
+	public void ctableOperations(Operator op, MatrixValue that, double scalarThat2, boolean ignoreZeros, 
 			CTableMap ctableResult, MatrixBlock ctableResultBlock)
 			throws DMLRuntimeException 
 	{
 		MatrixCell c2=checkType(that);
 		CTable ctable = CTable.getCTableFnObject();
 		if ( ctableResult != null)
-			ctable.execute(this.value, c2.value, scalarThat2, ignoreZeros, ctableResult);		
+			ctable.execute(this.value, c2.value, scalarThat2, ignoreZeros, ctableResult);
 		else
-			ctable.execute(this.value, c2.value, scalarThat2, ignoreZeros, ctableResultBlock);		
+			ctable.execute(this.value, c2.value, scalarThat2, ignoreZeros, ctableResultBlock);
 	}
 
 	@Override
-	public void ternaryOperations(Operator op, double scalarThat,
+	public void ctableOperations(Operator op, double scalarThat,
 			double scalarThat2, CTableMap resultMap, MatrixBlock resultBlock)
 			throws DMLRuntimeException 
 	{
 		CTable ctable = CTable.getCTableFnObject();
 		if ( resultMap != null)
-			ctable.execute(this.value, scalarThat, scalarThat2, false, resultMap);		
+			ctable.execute(this.value, scalarThat, scalarThat2, false, resultMap);
 		else
-			ctable.execute(this.value, scalarThat, scalarThat2, false, resultBlock);		
+			ctable.execute(this.value, scalarThat, scalarThat2, false, resultBlock);
 	}
 	
 	@Override
-	public void ternaryOperations(Operator op, MatrixIndexes ix1, double scalarThat, boolean left, int brlen,
+	public void ctableOperations(Operator op, MatrixIndexes ix1, double scalarThat, boolean left, int brlen,
 			CTableMap resultMap, MatrixBlock resultBlock)
 			throws DMLRuntimeException 
 	{
@@ -361,20 +345,20 @@ public class MatrixCell extends MatrixValue implements WritableComparable, Seria
 		CTable ctable = CTable.getCTableFnObject();
 		if ( resultMap != null ) {
 			if( left )
-				ctable.execute(ix1.getRowIndex(), this.value, scalarThat, false, resultMap);		
+				ctable.execute(ix1.getRowIndex(), this.value, scalarThat, false, resultMap);
 			else
-				ctable.execute(this.value, ix1.getRowIndex(), scalarThat, false, resultMap);			
+				ctable.execute(this.value, ix1.getRowIndex(), scalarThat, false, resultMap);
 		} 
 		else {
 			if( left )
-				ctable.execute(ix1.getRowIndex(), this.value, scalarThat, false, resultBlock);		
+				ctable.execute(ix1.getRowIndex(), this.value, scalarThat, false, resultBlock);
 			else
-				ctable.execute(this.value, ix1.getRowIndex(), scalarThat, false, resultBlock);			
+				ctable.execute(this.value, ix1.getRowIndex(), scalarThat, false, resultBlock);
 		}
 	}
 
 	@Override
-	public void ternaryOperations(Operator op, double scalarThat,
+	public void ctableOperations(Operator op, double scalarThat,
 			MatrixValue that2, CTableMap resultMap, MatrixBlock resultBlock)
 			throws DMLRuntimeException 
 	{
@@ -386,13 +370,6 @@ public class MatrixCell extends MatrixValue implements WritableComparable, Seria
 			ctable.execute(this.value, scalarThat, c3.value, false, resultBlock);
 
 	}
-	
-	@Override
-	public MatrixValue quaternaryOperations(QuaternaryOperator qop, MatrixValue um, MatrixValue vm, MatrixValue wm, MatrixValue out)
-		throws DMLRuntimeException
-	{
-		throw new DMLRuntimeException("operation not supported fro MatrixCell");
-	}
 
 	@Override
 	public void sliceOperations(ArrayList<IndexedMatrixValue> outlist,
@@ -407,7 +384,7 @@ public class MatrixCell extends MatrixValue implements WritableComparable, Seria
 	public MatrixValue replaceOperations(MatrixValue result, double pattern, double replacement)
 			throws DMLRuntimeException 
 	{
-		MatrixCell out = checkType(result);		
+		MatrixCell out = checkType(result);
 		if( value == pattern || (Double.isNaN(pattern) && Double.isNaN(value)) )
 			out.value = replacement;
 		else
@@ -424,14 +401,6 @@ public class MatrixCell extends MatrixValue implements WritableComparable, Seria
 	}
 
 	@Override
-	public MatrixValue aggregateBinaryOperations(MatrixIndexes m1Index,
-			MatrixValue m1Value, MatrixIndexes m2Index, MatrixValue m2Value,
-			MatrixValue result, AggregateBinaryOperator op)
-			throws DMLRuntimeException {
-		throw new DMLRuntimeException("MatrixCell.aggregateBinaryOperations should never be called");
-	}
-
-	@Override
 	public void appendOperations(MatrixValue valueIn2, ArrayList<IndexedMatrixValue> outlist,
 			int blockRowFactor, int blockColFactor, boolean cbind, boolean m2IsLast, int nextNCol) 
 	throws DMLRuntimeException  {

http://git-wip-us.apache.org/repos/asf/systemml/blob/57438103/src/main/java/org/apache/sysml/runtime/matrix/data/MatrixValue.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/sysml/runtime/matrix/data/MatrixValue.java b/src/main/java/org/apache/sysml/runtime/matrix/data/MatrixValue.java
index c6361bc..4f225be 100644
--- a/src/main/java/org/apache/sysml/runtime/matrix/data/MatrixValue.java
+++ b/src/main/java/org/apache/sysml/runtime/matrix/data/MatrixValue.java
@@ -26,12 +26,10 @@ import org.apache.hadoop.io.WritableComparable;
 
 import org.apache.sysml.runtime.DMLRuntimeException;
 import org.apache.sysml.runtime.matrix.mapred.IndexedMatrixValue;
-import org.apache.sysml.runtime.matrix.operators.AggregateBinaryOperator;
 import org.apache.sysml.runtime.matrix.operators.AggregateOperator;
 import org.apache.sysml.runtime.matrix.operators.AggregateUnaryOperator;
 import org.apache.sysml.runtime.matrix.operators.BinaryOperator;
 import org.apache.sysml.runtime.matrix.operators.Operator;
-import org.apache.sysml.runtime.matrix.operators.QuaternaryOperator;
 import org.apache.sysml.runtime.matrix.operators.ReorgOperator;
 import org.apache.sysml.runtime.matrix.operators.ScalarOperator;
 import org.apache.sysml.runtime.matrix.operators.UnaryOperator;
@@ -116,24 +114,21 @@ public abstract class MatrixValue implements WritableComparable
 			int startRow, int startColumn, int length)
 	throws DMLRuntimeException;
 	
-	public abstract void ternaryOperations(Operator op, MatrixValue that, MatrixValue that2, CTableMap resultMap, MatrixBlock resultBlock)
+	public abstract void ctableOperations(Operator op, MatrixValue that, MatrixValue that2, CTableMap resultMap, MatrixBlock resultBlock)
 		throws DMLRuntimeException;
 	
-	public abstract void ternaryOperations(Operator op, MatrixValue that, double scalar_that2, boolean ignoreZeros, CTableMap resultMap, MatrixBlock resultBlock)
+	public abstract void ctableOperations(Operator op, MatrixValue that, double scalar_that2, boolean ignoreZeros, CTableMap resultMap, MatrixBlock resultBlock)
 		throws DMLRuntimeException;
 	
-	public abstract void ternaryOperations(Operator op, double scalar_that, double scalar_that2, CTableMap resultMap, MatrixBlock resultBlock)
+	public abstract void ctableOperations(Operator op, double scalar_that, double scalar_that2, CTableMap resultMap, MatrixBlock resultBlock)
 		throws DMLRuntimeException;
 	
-	public abstract void ternaryOperations(Operator op, MatrixIndexes ix1, double scalar_that, boolean left, int brlen, CTableMap resultMap, MatrixBlock resultBlock)
+	public abstract void ctableOperations(Operator op, MatrixIndexes ix1, double scalar_that, boolean left, int brlen, CTableMap resultMap, MatrixBlock resultBlock)
 		throws DMLRuntimeException;
 
-	public abstract void ternaryOperations(Operator op, double scalarThat, MatrixValue that2, CTableMap ctableResult, MatrixBlock ctableResultBlock)
+	public abstract void ctableOperations(Operator op, double scalarThat, MatrixValue that2, CTableMap ctableResult, MatrixBlock ctableResultBlock)
 		throws DMLRuntimeException;
-
-	public abstract MatrixValue quaternaryOperations(QuaternaryOperator qop, MatrixValue um, MatrixValue vm, MatrixValue wm, MatrixValue out)
-		throws DMLRuntimeException;
-		
+	
 	public abstract MatrixValue aggregateUnaryOperations(AggregateUnaryOperator op, MatrixValue result, 
 			int brlen, int bclen, MatrixIndexes indexesIn) throws DMLRuntimeException;
 	
@@ -141,14 +136,6 @@ public abstract class MatrixValue implements WritableComparable
 			int blockingFactorRow, int blockingFactorCol, MatrixIndexes indexesIn, boolean inCP) 
 	throws DMLRuntimeException;
 	
-	public abstract MatrixValue aggregateBinaryOperations(MatrixValue m1Value, MatrixValue m2Value, 
-			MatrixValue result, AggregateBinaryOperator op) 
-	throws DMLRuntimeException;
-	
-	public abstract MatrixValue aggregateBinaryOperations(MatrixIndexes m1Index, MatrixValue m1Value, MatrixIndexes m2Index, MatrixValue m2Value, 
-			MatrixValue result, AggregateBinaryOperator op) 
-	throws DMLRuntimeException;
-	
 	public abstract MatrixValue unaryOperations(UnaryOperator op, MatrixValue result) 
 	throws DMLRuntimeException;
 

http://git-wip-us.apache.org/repos/asf/systemml/blob/57438103/src/main/java/org/apache/sysml/runtime/matrix/data/OperationsOnMatrixValues.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/sysml/runtime/matrix/data/OperationsOnMatrixValues.java b/src/main/java/org/apache/sysml/runtime/matrix/data/OperationsOnMatrixValues.java
index 90a8d15..c11688e 100644
--- a/src/main/java/org/apache/sysml/runtime/matrix/data/OperationsOnMatrixValues.java
+++ b/src/main/java/org/apache/sysml/runtime/matrix/data/OperationsOnMatrixValues.java
@@ -71,44 +71,44 @@ public class OperationsOnMatrixValues
 	}
 	
 	// ------------- Ternary Operations -------------
-	public static void performTernary(MatrixIndexes indexesIn1, MatrixValue valueIn1, MatrixIndexes indexesIn2, MatrixValue valueIn2, 
+	public static void performCtable(MatrixIndexes indexesIn1, MatrixValue valueIn1, MatrixIndexes indexesIn2, MatrixValue valueIn2, 
 			MatrixIndexes indexesIn3, MatrixValue valueIn3, CTableMap resultMap, MatrixBlock resultBlock, Operator op ) 
 		throws DMLRuntimeException
 	{
 		//operation on the cells inside the value
-		valueIn1.ternaryOperations(op, valueIn2, valueIn3, resultMap, resultBlock);
+		valueIn1.ctableOperations(op, valueIn2, valueIn3, resultMap, resultBlock);
 	}
 	
-	public static void performTernary(MatrixIndexes indexesIn1, MatrixValue valueIn1, MatrixIndexes indexesIn2, MatrixValue valueIn2, 
+	public static void performCtable(MatrixIndexes indexesIn1, MatrixValue valueIn1, MatrixIndexes indexesIn2, MatrixValue valueIn2, 
 			double scalarIn3, CTableMap resultMap, MatrixBlock resultBlock, Operator op) 
 		throws DMLRuntimeException
 	{
 		//operation on the cells inside the value
-		valueIn1.ternaryOperations(op, valueIn2, scalarIn3, false, resultMap, resultBlock);
+		valueIn1.ctableOperations(op, valueIn2, scalarIn3, false, resultMap, resultBlock);
 	}
 	
-	public static void performTernary(MatrixIndexes indexesIn1, MatrixValue valueIn1, double scalarIn2, 
+	public static void performCtable(MatrixIndexes indexesIn1, MatrixValue valueIn1, double scalarIn2, 
 			double scalarIn3, CTableMap resultMap, MatrixBlock resultBlock, Operator op ) 
 		throws DMLRuntimeException
 	{
 		//operation on the cells inside the value
-		valueIn1.ternaryOperations(op, scalarIn2, scalarIn3, resultMap, resultBlock);
+		valueIn1.ctableOperations(op, scalarIn2, scalarIn3, resultMap, resultBlock);
 	}
 	
-	public static void performTernary(MatrixIndexes indexesIn1, MatrixValue valueIn1, double scalarIn2, boolean left,
+	public static void performCtable(MatrixIndexes indexesIn1, MatrixValue valueIn1, double scalarIn2, boolean left,
 			int brlen, CTableMap resultMap, MatrixBlock resultBlock, Operator op ) 
 		throws DMLRuntimeException
 	{
 		//operation on the cells inside the value
-		valueIn1.ternaryOperations(op, indexesIn1, scalarIn2, left, brlen, resultMap, resultBlock);
+		valueIn1.ctableOperations(op, indexesIn1, scalarIn2, left, brlen, resultMap, resultBlock);
 	}
 	
-	public static void performTernary(MatrixIndexes indexesIn1, MatrixValue valueIn1, double scalarIn2, 
+	public static void performCtable(MatrixIndexes indexesIn1, MatrixValue valueIn1, double scalarIn2, 
 			MatrixIndexes indexesIn3, MatrixValue valueIn3, CTableMap resultMap, MatrixBlock resultBlock, Operator op ) 
 		throws DMLRuntimeException
 	{
 		//operation on the cells inside the value
-		valueIn1.ternaryOperations(op, scalarIn2, valueIn3, resultMap, resultBlock);
+		valueIn1.ctableOperations(op, scalarIn2, valueIn3, resultMap, resultBlock);
 	}
 	// -----------------------------------------------------
 	
@@ -240,13 +240,12 @@ public class OperationsOnMatrixValues
 		valueIn.aggregateUnaryOperations(op, valueOut, brlen, bclen, indexesIn);
 	}
 	
-	public static void performAggregateBinary(MatrixIndexes indexes1, MatrixValue value1, MatrixIndexes indexes2, MatrixValue value2, 
-			MatrixIndexes indexesOut, MatrixValue valueOut, AggregateBinaryOperator op)
+	public static void performAggregateBinary(MatrixIndexes indexes1, MatrixBlock value1, MatrixIndexes indexes2, MatrixBlock value2, 
+			MatrixIndexes indexesOut, MatrixBlock valueOut, AggregateBinaryOperator op)
 	throws DMLRuntimeException
 	{
 		//compute output index
 		indexesOut.setIndexes(indexes1.getRowIndex(), indexes2.getColumnIndex());
-		
 		//perform on the value
 		if( value2 instanceof CompressedMatrixBlock )
 			value2.aggregateBinaryOperations(value1, value2, valueOut, op);
@@ -254,11 +253,9 @@ public class OperationsOnMatrixValues
 			value1.aggregateBinaryOperations(indexes1, value1, indexes2, value2, valueOut, op);
 	}
 
-	public static MatrixValue performAggregateBinaryIgnoreIndexes(
-			MatrixValue value1, MatrixValue value2,
-			MatrixValue valueOut, AggregateBinaryOperator op) 
+	public static MatrixValue performAggregateBinaryIgnoreIndexes(MatrixBlock value1, MatrixBlock value2,
+			MatrixBlock valueOut, AggregateBinaryOperator op) 
 	throws DMLRuntimeException {
-			
 		//perform on the value
 		if( value2 instanceof CompressedMatrixBlock )
 			value2.aggregateBinaryOperations(value1, value2, valueOut, op);

http://git-wip-us.apache.org/repos/asf/systemml/blob/57438103/src/main/java/org/apache/sysml/runtime/matrix/mapred/GMRReducer.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/sysml/runtime/matrix/mapred/GMRReducer.java b/src/main/java/org/apache/sysml/runtime/matrix/mapred/GMRReducer.java
index 3859636..a8ed5d9 100644
--- a/src/main/java/org/apache/sysml/runtime/matrix/mapred/GMRReducer.java
+++ b/src/main/java/org/apache/sysml/runtime/matrix/mapred/GMRReducer.java
@@ -31,7 +31,7 @@ import org.apache.hadoop.mapred.Reporter;
 
 import org.apache.sysml.runtime.instructions.mr.AppendRInstruction;
 import org.apache.sysml.runtime.instructions.mr.MRInstruction;
-import org.apache.sysml.runtime.instructions.mr.TernaryInstruction;
+import org.apache.sysml.runtime.instructions.mr.CtableInstruction;
 import org.apache.sysml.runtime.matrix.MatrixCharacteristics;
 import org.apache.sysml.runtime.matrix.data.MatrixCell;
 import org.apache.sysml.runtime.matrix.data.MatrixIndexes;
@@ -78,10 +78,10 @@ implements Reducer<MatrixIndexes, TaggedMatrixValue, MatrixIndexes, MatrixValue>
 		{		
 			for(MRInstruction ins: mixed_instructions)
 			{
-				if(ins instanceof TernaryInstruction)
+				if(ins instanceof CtableInstruction)
 				{  
-					MatrixCharacteristics dim = dimensions.get(((TernaryInstruction) ins).input1);
-					((TernaryInstruction) ins).processInstruction(valueClass, cachedValues, zeroInput, _buff.getMapBuffer(), _buff.getBlockBuffer(), dim.getRowsPerBlock(), dim.getColsPerBlock());
+					MatrixCharacteristics dim = dimensions.get(((CtableInstruction) ins).input1);
+					((CtableInstruction) ins).processInstruction(valueClass, cachedValues, zeroInput, _buff.getMapBuffer(), _buff.getBlockBuffer(), dim.getRowsPerBlock(), dim.getColsPerBlock());
 					if( _buff.getBufferSize() > GMRCtableBuffer.MAX_BUFFER_SIZE )
 						_buff.flushBuffer(cachedReporter); //prevent oom for large/many ctables
 				}

http://git-wip-us.apache.org/repos/asf/systemml/blob/57438103/src/main/java/org/apache/sysml/runtime/matrix/mapred/MMCJMRReducerWithAggregator.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/sysml/runtime/matrix/mapred/MMCJMRReducerWithAggregator.java b/src/main/java/org/apache/sysml/runtime/matrix/mapred/MMCJMRReducerWithAggregator.java
index 85306a6..a9e1714 100644
--- a/src/main/java/org/apache/sysml/runtime/matrix/mapred/MMCJMRReducerWithAggregator.java
+++ b/src/main/java/org/apache/sysml/runtime/matrix/mapred/MMCJMRReducerWithAggregator.java
@@ -123,15 +123,15 @@ public class MMCJMRReducerWithAggregator extends MMCJMRCombinerReducerBase
 					{
 						//perform matrix multiplication
 						indexesbuffer.setIndexes(tmp.getKey().getRowIndex(), inIndex);
-						OperationsOnMatrixValues.performAggregateBinaryIgnoreIndexes(tmp.getValue(), inValue, valueBuffer, 
-								                               (AggregateBinaryOperator)aggBinInstruction.getOperator());
+						OperationsOnMatrixValues.performAggregateBinaryIgnoreIndexes((MatrixBlock)tmp.getValue(),
+							(MatrixBlock)inValue, (MatrixBlock)valueBuffer, (AggregateBinaryOperator)aggBinInstruction.getOperator());
 					}
 					else //right cached
 					{
 						//perform matrix multiplication
 						indexesbuffer.setIndexes(inIndex, tmp.getKey().getColumnIndex());
-						OperationsOnMatrixValues.performAggregateBinaryIgnoreIndexes(inValue, tmp.getValue(), valueBuffer, 
-								                               (AggregateBinaryOperator)aggBinInstruction.getOperator());		
+						OperationsOnMatrixValues.performAggregateBinaryIgnoreIndexes((MatrixBlock)inValue,
+							(MatrixBlock)tmp.getValue(), (MatrixBlock)valueBuffer, (AggregateBinaryOperator)aggBinInstruction.getOperator());
 					}
 					
 					//aggregate block to output buffer or direct output

http://git-wip-us.apache.org/repos/asf/systemml/blob/57438103/src/main/java/org/apache/sysml/runtime/matrix/mapred/MMRJMRReducer.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/sysml/runtime/matrix/mapred/MMRJMRReducer.java b/src/main/java/org/apache/sysml/runtime/matrix/mapred/MMRJMRReducer.java
index 4bcb9c6..1572719 100644
--- a/src/main/java/org/apache/sysml/runtime/matrix/mapred/MMRJMRReducer.java
+++ b/src/main/java/org/apache/sysml/runtime/matrix/mapred/MMRJMRReducer.java
@@ -30,6 +30,7 @@ import org.apache.hadoop.mapred.Reporter;
 
 import org.apache.sysml.runtime.DMLRuntimeException;
 import org.apache.sysml.runtime.instructions.mr.AggregateBinaryInstruction;
+import org.apache.sysml.runtime.matrix.data.MatrixBlock;
 import org.apache.sysml.runtime.matrix.data.MatrixIndexes;
 import org.apache.sysml.runtime.matrix.data.MatrixValue;
 import org.apache.sysml.runtime.matrix.data.OperationsOnMatrixValues;
@@ -42,9 +43,8 @@ import org.apache.sysml.runtime.matrix.operators.AggregateBinaryOperator;
 public class MMRJMRReducer extends ReduceBase
 implements Reducer<TripleIndexes, TaggedMatrixValue, MatrixIndexes, MatrixValue>
 {
-	
 	private Reporter cachedReporter=null;
-	private MatrixValue resultblock=null;
+	private MatrixBlock resultblock=null;
 	private MatrixIndexes aggIndexes=new MatrixIndexes();
 	private TripleIndexes prevIndexes=new TripleIndexes(-1, -1, -1);
 	//aggregate binary instruction for the mmrj
@@ -106,14 +106,11 @@ implements Reducer<TripleIndexes, TaggedMatrixValue, MatrixIndexes, MatrixValue>
 	{
 		IndexedMatrixValue left = cachedValues.getFirst(aggBinInstruction.input1);
 		IndexedMatrixValue right= cachedValues.getFirst(aggBinInstruction.input2);
-	//	System.out.println("left: \n"+left.getValue());
-	//	System.out.println("right: \n"+right.getValue());
 		if(left!=null && right!=null)
 		{
 			try {
-				resultblock=left.getValue().aggregateBinaryOperations(left.getValue(), right.getValue(), 
+				resultblock=((MatrixBlock)left.getValue()).aggregateBinaryOperations((MatrixBlock)left.getValue(), (MatrixBlock)right.getValue(), 
 						resultblock, (AggregateBinaryOperator) aggBinInstruction.getOperator());
-		//		System.out.println("resultblock: \n"+resultblock);
 				IndexedMatrixValue out=cachedValues.getFirst(aggBinInstruction.output);
 				if(out==null)
 				{

http://git-wip-us.apache.org/repos/asf/systemml/blob/57438103/src/main/java/org/apache/sysml/runtime/matrix/mapred/ReduceBase.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/sysml/runtime/matrix/mapred/ReduceBase.java b/src/main/java/org/apache/sysml/runtime/matrix/mapred/ReduceBase.java
index 80df55d..15441ec 100644
--- a/src/main/java/org/apache/sysml/runtime/matrix/mapred/ReduceBase.java
+++ b/src/main/java/org/apache/sysml/runtime/matrix/mapred/ReduceBase.java
@@ -33,7 +33,7 @@ import org.apache.sysml.runtime.DMLRuntimeException;
 import org.apache.sysml.runtime.functionobjects.Plus;
 import org.apache.sysml.runtime.instructions.mr.AggregateInstruction;
 import org.apache.sysml.runtime.instructions.mr.MRInstruction;
-import org.apache.sysml.runtime.instructions.mr.TernaryInstruction;
+import org.apache.sysml.runtime.instructions.mr.CtableInstruction;
 import org.apache.sysml.runtime.matrix.data.MatrixIndexes;
 import org.apache.sysml.runtime.matrix.data.MatrixValue;
 import org.apache.sysml.runtime.matrix.data.OperationsOnMatrixValues;
@@ -367,7 +367,7 @@ public class ReduceBase extends MRBaseForCommonInstructions
 	{
 		if( mixed_instructions != null )
 			for(MRInstruction inst : mixed_instructions)
-				if( inst instanceof TernaryInstruction )
+				if( inst instanceof CtableInstruction )
 					return true;
 		return false;
 	}
@@ -375,7 +375,7 @@ public class ReduceBase extends MRBaseForCommonInstructions
 	protected boolean dimsKnownForTernaryInstructions() {
 		if( mixed_instructions != null )
 			for(MRInstruction inst : mixed_instructions)
-				if( inst instanceof TernaryInstruction && !((TernaryInstruction)inst).knownOutputDims() )
+				if( inst instanceof CtableInstruction && !((CtableInstruction)inst).knownOutputDims() )
 					return false;
 		return true;
 	}
@@ -384,9 +384,9 @@ public class ReduceBase extends MRBaseForCommonInstructions
 	{
 		if( mixed_instructions != null )
 			for(MRInstruction inst : mixed_instructions)
-				if( inst instanceof TernaryInstruction )
+				if( inst instanceof CtableInstruction )
 				{
-					TernaryInstruction tinst = (TernaryInstruction) inst;
+					CtableInstruction tinst = (CtableInstruction) inst;
 					if( tinst.input1!=-1 )
 						dimensions.put(tinst.input1, MRJobConfiguration.getMatrixCharacteristicsForInput(job, tinst.input1));					
 					//extend as required, currently only ctableexpand needs blocksizes

http://git-wip-us.apache.org/repos/asf/systemml/blob/57438103/src/test/java/org/apache/sysml/test/integration/functions/compress/BasicMatrixVectorMultTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/sysml/test/integration/functions/compress/BasicMatrixVectorMultTest.java b/src/test/java/org/apache/sysml/test/integration/functions/compress/BasicMatrixVectorMultTest.java
index 537bef6..67e464a 100644
--- a/src/test/java/org/apache/sysml/test/integration/functions/compress/BasicMatrixVectorMultTest.java
+++ b/src/test/java/org/apache/sysml/test/integration/functions/compress/BasicMatrixVectorMultTest.java
@@ -171,10 +171,10 @@ public class BasicMatrixVectorMultTest extends AutomatedTestBase
 			//matrix-vector uncompressed
 			AggregateOperator aop = new AggregateOperator(0, Plus.getPlusFnObject());
 			AggregateBinaryOperator abop = new AggregateBinaryOperator(Multiply.getMultiplyFnObject(), aop);
-			MatrixBlock ret1 = (MatrixBlock)mb.aggregateBinaryOperations(mb, vector, new MatrixBlock(), abop);
+			MatrixBlock ret1 = mb.aggregateBinaryOperations(mb, vector, new MatrixBlock(), abop);
 			
 			//matrix-vector compressed
-			MatrixBlock ret2 = (MatrixBlock)cmb.aggregateBinaryOperations(cmb, vector, new MatrixBlock(), abop);
+			MatrixBlock ret2 = cmb.aggregateBinaryOperations(cmb, vector, new MatrixBlock(), abop);
 			
 			//compare result with input
 			double[][] d1 = DataConverter.convertToDoubleMatrix(ret1);

http://git-wip-us.apache.org/repos/asf/systemml/blob/57438103/src/test/java/org/apache/sysml/test/integration/functions/compress/BasicVectorMatrixMultTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/sysml/test/integration/functions/compress/BasicVectorMatrixMultTest.java b/src/test/java/org/apache/sysml/test/integration/functions/compress/BasicVectorMatrixMultTest.java
index 29832f1..c8592af 100644
--- a/src/test/java/org/apache/sysml/test/integration/functions/compress/BasicVectorMatrixMultTest.java
+++ b/src/test/java/org/apache/sysml/test/integration/functions/compress/BasicVectorMatrixMultTest.java
@@ -171,10 +171,10 @@ public class BasicVectorMatrixMultTest extends AutomatedTestBase
 			//matrix-vector uncompressed
 			AggregateOperator aop = new AggregateOperator(0, Plus.getPlusFnObject());
 			AggregateBinaryOperator abop = new AggregateBinaryOperator(Multiply.getMultiplyFnObject(), aop);
-			MatrixBlock ret1 = (MatrixBlock)vector.aggregateBinaryOperations(vector, mb, new MatrixBlock(), abop);
+			MatrixBlock ret1 = vector.aggregateBinaryOperations(vector, mb, new MatrixBlock(), abop);
 			
 			//matrix-vector compressed
-			MatrixBlock ret2 = (MatrixBlock)cmb.aggregateBinaryOperations(vector, cmb, new MatrixBlock(), abop);
+			MatrixBlock ret2 = cmb.aggregateBinaryOperations(vector, cmb, new MatrixBlock(), abop);
 			
 			//compare result with input
 			double[][] d1 = DataConverter.convertToDoubleMatrix(ret1);

http://git-wip-us.apache.org/repos/asf/systemml/blob/57438103/src/test/java/org/apache/sysml/test/integration/functions/compress/LargeMatrixVectorMultTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/sysml/test/integration/functions/compress/LargeMatrixVectorMultTest.java b/src/test/java/org/apache/sysml/test/integration/functions/compress/LargeMatrixVectorMultTest.java
index 990845b..3401c77 100644
--- a/src/test/java/org/apache/sysml/test/integration/functions/compress/LargeMatrixVectorMultTest.java
+++ b/src/test/java/org/apache/sysml/test/integration/functions/compress/LargeMatrixVectorMultTest.java
@@ -171,10 +171,10 @@ public class LargeMatrixVectorMultTest extends AutomatedTestBase
 			//matrix-vector uncompressed
 			AggregateOperator aop = new AggregateOperator(0, Plus.getPlusFnObject());
 			AggregateBinaryOperator abop = new AggregateBinaryOperator(Multiply.getMultiplyFnObject(), aop);
-			MatrixBlock ret1 = (MatrixBlock)mb.aggregateBinaryOperations(mb, vector, new MatrixBlock(), abop);
+			MatrixBlock ret1 = mb.aggregateBinaryOperations(mb, vector, new MatrixBlock(), abop);
 			
 			//matrix-vector compressed
-			MatrixBlock ret2 = (MatrixBlock)cmb.aggregateBinaryOperations(cmb, vector, new MatrixBlock(), abop);
+			MatrixBlock ret2 = cmb.aggregateBinaryOperations(cmb, vector, new MatrixBlock(), abop);
 			
 			//compare result with input
 			double[][] d1 = DataConverter.convertToDoubleMatrix(ret1);

http://git-wip-us.apache.org/repos/asf/systemml/blob/57438103/src/test/java/org/apache/sysml/test/integration/functions/compress/LargeParMatrixVectorMultTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/sysml/test/integration/functions/compress/LargeParMatrixVectorMultTest.java b/src/test/java/org/apache/sysml/test/integration/functions/compress/LargeParMatrixVectorMultTest.java
index c5f9560..19d971a 100644
--- a/src/test/java/org/apache/sysml/test/integration/functions/compress/LargeParMatrixVectorMultTest.java
+++ b/src/test/java/org/apache/sysml/test/integration/functions/compress/LargeParMatrixVectorMultTest.java
@@ -173,10 +173,10 @@ public class LargeParMatrixVectorMultTest extends AutomatedTestBase
 			AggregateOperator aop = new AggregateOperator(0, Plus.getPlusFnObject());
 			AggregateBinaryOperator abop = new AggregateBinaryOperator(Multiply.getMultiplyFnObject(), aop,
 					InfrastructureAnalyzer.getLocalParallelism());
-			MatrixBlock ret1 = (MatrixBlock)mb.aggregateBinaryOperations(mb, vector, new MatrixBlock(), abop);
+			MatrixBlock ret1 = mb.aggregateBinaryOperations(mb, vector, new MatrixBlock(), abop);
 			
 			//matrix-vector compressed
-			MatrixBlock ret2 = (MatrixBlock)cmb.aggregateBinaryOperations(cmb, vector, new MatrixBlock(), abop);
+			MatrixBlock ret2 = cmb.aggregateBinaryOperations(cmb, vector, new MatrixBlock(), abop);
 			
 			//compare result with input
 			double[][] d1 = DataConverter.convertToDoubleMatrix(ret1);

http://git-wip-us.apache.org/repos/asf/systemml/blob/57438103/src/test/java/org/apache/sysml/test/integration/functions/compress/LargeVectorMatrixMultTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/sysml/test/integration/functions/compress/LargeVectorMatrixMultTest.java b/src/test/java/org/apache/sysml/test/integration/functions/compress/LargeVectorMatrixMultTest.java
index 982c0c1..d7b9094 100644
--- a/src/test/java/org/apache/sysml/test/integration/functions/compress/LargeVectorMatrixMultTest.java
+++ b/src/test/java/org/apache/sysml/test/integration/functions/compress/LargeVectorMatrixMultTest.java
@@ -171,10 +171,10 @@ public class LargeVectorMatrixMultTest extends AutomatedTestBase
 			//matrix-vector uncompressed
 			AggregateOperator aop = new AggregateOperator(0, Plus.getPlusFnObject());
 			AggregateBinaryOperator abop = new AggregateBinaryOperator(Multiply.getMultiplyFnObject(), aop);
-			MatrixBlock ret1 = (MatrixBlock)vector.aggregateBinaryOperations(vector, mb, new MatrixBlock(), abop);
+			MatrixBlock ret1 = vector.aggregateBinaryOperations(vector, mb, new MatrixBlock(), abop);
 			
 			//matrix-vector compressed
-			MatrixBlock ret2 = (MatrixBlock)cmb.aggregateBinaryOperations(vector, cmb, new MatrixBlock(), abop);
+			MatrixBlock ret2 = cmb.aggregateBinaryOperations(vector, cmb, new MatrixBlock(), abop);
 			
 			//compare result with input
 			double[][] d1 = DataConverter.convertToDoubleMatrix(ret1);

http://git-wip-us.apache.org/repos/asf/systemml/blob/57438103/src/test/java/org/apache/sysml/test/integration/functions/compress/ParMatrixVectorMultTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/sysml/test/integration/functions/compress/ParMatrixVectorMultTest.java b/src/test/java/org/apache/sysml/test/integration/functions/compress/ParMatrixVectorMultTest.java
index c34216c..3fdeb19 100644
--- a/src/test/java/org/apache/sysml/test/integration/functions/compress/ParMatrixVectorMultTest.java
+++ b/src/test/java/org/apache/sysml/test/integration/functions/compress/ParMatrixVectorMultTest.java
@@ -173,10 +173,10 @@ public class ParMatrixVectorMultTest extends AutomatedTestBase
 			AggregateOperator aop = new AggregateOperator(0, Plus.getPlusFnObject());
 			AggregateBinaryOperator abop = new AggregateBinaryOperator(Multiply.getMultiplyFnObject(), aop,
 					InfrastructureAnalyzer.getLocalParallelism());
-			MatrixBlock ret1 = (MatrixBlock)mb.aggregateBinaryOperations(mb, vector, new MatrixBlock(), abop);
+			MatrixBlock ret1 = mb.aggregateBinaryOperations(mb, vector, new MatrixBlock(), abop);
 			
 			//matrix-vector compressed
-			MatrixBlock ret2 = (MatrixBlock)cmb.aggregateBinaryOperations(cmb, vector, new MatrixBlock(), abop);
+			MatrixBlock ret2 = cmb.aggregateBinaryOperations(cmb, vector, new MatrixBlock(), abop);
 			
 			//compare result with input
 			double[][] d1 = DataConverter.convertToDoubleMatrix(ret1);

http://git-wip-us.apache.org/repos/asf/systemml/blob/57438103/src/test/java/org/apache/sysml/test/integration/functions/compress/ParVectorMatrixMultTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/sysml/test/integration/functions/compress/ParVectorMatrixMultTest.java b/src/test/java/org/apache/sysml/test/integration/functions/compress/ParVectorMatrixMultTest.java
index 1fee256..6bbcde9 100644
--- a/src/test/java/org/apache/sysml/test/integration/functions/compress/ParVectorMatrixMultTest.java
+++ b/src/test/java/org/apache/sysml/test/integration/functions/compress/ParVectorMatrixMultTest.java
@@ -172,10 +172,10 @@ public class ParVectorMatrixMultTest extends AutomatedTestBase
 			AggregateOperator aop = new AggregateOperator(0, Plus.getPlusFnObject());
 			AggregateBinaryOperator abop = new AggregateBinaryOperator(Multiply.getMultiplyFnObject(), aop,
 					InfrastructureAnalyzer.getLocalParallelism());
-			MatrixBlock ret1 = (MatrixBlock)vector.aggregateBinaryOperations(vector, mb, new MatrixBlock(), abop);
+			MatrixBlock ret1 = vector.aggregateBinaryOperations(vector, mb, new MatrixBlock(), abop);
 			
 			//matrix-vector compressed
-			MatrixBlock ret2 = (MatrixBlock)cmb.aggregateBinaryOperations(vector, cmb, new MatrixBlock(), abop);
+			MatrixBlock ret2 = cmb.aggregateBinaryOperations(vector, cmb, new MatrixBlock(), abop);
 			
 			//compare result with input
 			double[][] d1 = DataConverter.convertToDoubleMatrix(ret1);


[2/5] systemml git commit: [MINOR] Refactoring agg-binary, ternary, and quaternary instructions

Posted by mb...@apache.org.
http://git-wip-us.apache.org/repos/asf/systemml/blob/57438103/src/main/java/org/apache/sysml/runtime/instructions/mr/CtableInstruction.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/sysml/runtime/instructions/mr/CtableInstruction.java b/src/main/java/org/apache/sysml/runtime/instructions/mr/CtableInstruction.java
new file mode 100644
index 0000000..213f401
--- /dev/null
+++ b/src/main/java/org/apache/sysml/runtime/instructions/mr/CtableInstruction.java
@@ -0,0 +1,264 @@
+/*
+ * 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.mr;
+
+import java.util.HashMap;
+
+import org.apache.sysml.lops.Ctable;
+import org.apache.sysml.lops.Ctable.OperationTypes;
+import org.apache.sysml.runtime.DMLRuntimeException;
+import org.apache.sysml.runtime.instructions.InstructionUtils;
+import org.apache.sysml.runtime.matrix.data.CTableMap;
+import org.apache.sysml.runtime.matrix.data.MatrixBlock;
+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;
+
+public class CtableInstruction extends MRInstruction {
+	private OperationTypes _op;
+
+	public byte input1;
+	public byte input2;
+	public byte input3;
+	public double scalar_input2;
+	public double scalar_input3;
+	private long _outputDim1, _outputDim2;
+
+	private CtableInstruction(MRType type, OperationTypes op, byte in1, double scalar_in2, double scalar_in3, byte out,
+			long outputDim1, long outputDim2, String istr) {
+		super(type, null, out);
+		_op = op;
+		input1 = in1;
+		scalar_input2 = scalar_in2;
+		scalar_input3 = scalar_in3;
+		_outputDim1 = outputDim1;
+		_outputDim2 = outputDim2;
+		instString = istr;
+	}
+
+	private CtableInstruction(MRType type, OperationTypes op, byte in1, byte in2, double scalar_in3, byte out, long outputDim1,
+			long outputDim2, String istr) {
+		super(type, null, out);
+		_op = op;
+		input1 = in1;
+		input2 = in2;
+		scalar_input3 = scalar_in3;
+		_outputDim1 = outputDim1;
+		_outputDim2 = outputDim2;
+		instString = istr;
+	}
+
+	private CtableInstruction(MRType type, OperationTypes op, byte in1, double scalar_in2, byte in3, byte out, long outputDim1,
+			long outputDim2, String istr) {
+		super(type, null, out);
+		_op = op;
+		input1 = in1;
+		scalar_input2 = scalar_in2;
+		input3 = in3;
+		_outputDim1 = outputDim1;
+		_outputDim2 = outputDim2;
+		instString = istr;
+	}
+
+	protected CtableInstruction(MRType type, OperationTypes op, byte in1, byte in2, byte in3, byte out, long outputDim1,
+			long outputDim2, String istr) {
+		super(type, null, out);
+		_op = op;
+		input1 = in1;
+		input2 = in2;
+		input3 = in3;
+		_outputDim1 = outputDim1;
+		_outputDim2 = outputDim2;
+		instString = istr;
+	}
+
+	public long getOutputDim1() {
+		return _outputDim1;
+	}
+	
+	public long getOutputDim2() {
+		return _outputDim2;
+	}
+	
+	public boolean knownOutputDims() {
+		return (_outputDim1 >0 && _outputDim2>0);
+	}
+
+	public static CtableInstruction parseInstruction ( String str ) 
+		throws DMLRuntimeException 
+	{		
+		// example instruction string 
+		// - ctabletransform:::0:DOUBLE:::1:DOUBLE:::2:DOUBLE:::3:DOUBLE 
+		// - ctabletransformscalarweight:::0:DOUBLE:::1:DOUBLE:::1.0:DOUBLE:::3:DOUBLE 
+		// - ctabletransformhistogram:::0:DOUBLE:::1.0:DOUBLE:::1.0:DOUBLE:::3:DOUBLE 
+		// - ctabletransformweightedhistogram:::0:DOUBLE:::1:INT:::1:DOUBLE:::2:DOUBLE 
+		
+		//check number of fields
+		InstructionUtils.checkNumFields ( str, 6 );
+		
+		//common setup
+		byte in1, in2, in3, out;
+		String[] parts = InstructionUtils.getInstructionParts ( str );
+		String opcode = parts[0];
+		in1 = Byte.parseByte(parts[1]);
+		long outputDim1 = (long) Double.parseDouble(parts[4]);
+		long outputDim2 = (long) Double.parseDouble(parts[5]);
+		out = Byte.parseByte(parts[6]);
+		
+		OperationTypes op = Ctable.getOperationType(opcode);
+		
+		switch( op )
+		{
+			case CTABLE_TRANSFORM: {
+				in2 = Byte.parseByte(parts[2]);
+				in3 = Byte.parseByte(parts[3]);
+				return new CtableInstruction(MRType.Ctable, op, in1, in2, in3, out, outputDim1, outputDim2, str);
+			}
+			case CTABLE_TRANSFORM_SCALAR_WEIGHT: {
+				in2 = Byte.parseByte(parts[2]);
+				double scalar_in3 = Double.parseDouble(parts[3]);
+				return new CtableInstruction(MRType.Ctable, op, in1, in2, scalar_in3, out, outputDim1, outputDim2, str);
+			}
+			case CTABLE_EXPAND_SCALAR_WEIGHT: {
+				double scalar_in2 = Double.parseDouble(parts[2]);
+				double type = Double.parseDouble(parts[3]); //used as type (1 left, 0 right)
+				return new CtableInstruction(MRType.Ctable, op, in1, scalar_in2, type, out, outputDim1, outputDim2, str);
+			}
+			case CTABLE_TRANSFORM_HISTOGRAM: {
+				double scalar_in2 = Double.parseDouble(parts[2]);
+				double scalar_in3 = Double.parseDouble(parts[3]);
+				return new CtableInstruction(MRType.Ctable, op, in1, scalar_in2, scalar_in3, out, outputDim1, outputDim2, str);
+			}
+			case CTABLE_TRANSFORM_WEIGHTED_HISTOGRAM: {
+				double scalar_in2 = Double.parseDouble(parts[2]);
+				in3 = Byte.parseByte(parts[3]);
+				return new CtableInstruction(MRType.Ctable, op, in1, scalar_in2, in3, out, outputDim1, outputDim2, str);	
+			}
+			default:
+				throw new DMLRuntimeException("Unrecognized opcode in Ternary Instruction: " + op);	
+		}
+	}
+	
+	public void processInstruction(Class<? extends MatrixValue> valueClass, CachedValueMap cachedValues, 
+			            IndexedMatrixValue zeroInput, HashMap<Byte, CTableMap> resultMaps, HashMap<Byte, MatrixBlock> resultBlocks, 
+			            int blockRowFactor, int blockColFactor)
+		throws DMLRuntimeException 
+	{	
+		
+		IndexedMatrixValue in1, in2, in3 = null;
+		in1 = cachedValues.getFirst(input1);
+		
+		CTableMap ctableResult = null;
+		MatrixBlock ctableResultBlock = null;
+		
+		if ( knownOutputDims() ) {
+			if ( resultBlocks != null ) {
+				ctableResultBlock = resultBlocks.get(output);
+				if ( ctableResultBlock == null ) {
+					// From MR, output of ctable is set to be sparse since it is built from a single input block.
+					ctableResultBlock = new MatrixBlock((int)_outputDim1, (int)_outputDim2, true);
+					resultBlocks.put(output, ctableResultBlock);
+				}
+			}
+			else {
+				throw new DMLRuntimeException("Unexpected error in processing table instruction.");
+			}
+		}
+		else {
+			//prepare aggregation maps
+			ctableResult=resultMaps.get(output);
+			if(ctableResult==null)
+			{
+				ctableResult = new CTableMap();
+				resultMaps.put(output, ctableResult);
+			}
+		}
+		
+		//get inputs and process instruction
+		switch( _op )
+		{
+			case CTABLE_TRANSFORM: {
+				in2 = cachedValues.getFirst(input2);
+				in3 = cachedValues.getFirst(input3);
+				if(in1==null || in2==null || in3 == null )
+					return;	
+				OperationsOnMatrixValues.performCtable(in1.getIndexes(), in1.getValue(), in2.getIndexes(), in2.getValue(), 
+					in3.getIndexes(), in3.getValue(), ctableResult, ctableResultBlock, optr);
+				break;
+			}
+			case CTABLE_TRANSFORM_SCALAR_WEIGHT: {
+				// 3rd input is a scalar
+				in2 = cachedValues.getFirst(input2);
+				if(in1==null || in2==null )
+					return;
+				OperationsOnMatrixValues.performCtable(in1.getIndexes(), in1.getValue(), in2.getIndexes(), in2.getValue(), 
+					scalar_input3, ctableResult, ctableResultBlock, optr);
+				break;
+			}
+			case CTABLE_EXPAND_SCALAR_WEIGHT: {
+				// 2nd and 3rd input is a scalar
+				if(in1==null )
+					return;
+				OperationsOnMatrixValues.performCtable(in1.getIndexes(), in1.getValue(), scalar_input2, (scalar_input3==1), 
+					blockRowFactor, ctableResult, ctableResultBlock, optr);
+				break;
+			}
+			case CTABLE_TRANSFORM_HISTOGRAM: {
+				// 2nd and 3rd inputs are scalars
+				if(in1==null )
+					return;	
+				OperationsOnMatrixValues.performCtable(in1.getIndexes(), in1.getValue(), scalar_input2, scalar_input3, ctableResult, ctableResultBlock, optr);
+				break;
+			}
+			case CTABLE_TRANSFORM_WEIGHTED_HISTOGRAM: {
+				// 2nd and 3rd inputs are scalars
+				in3 = cachedValues.getFirst(input3);
+				if(in1==null || in3==null)
+					return;
+				OperationsOnMatrixValues.performCtable(in1.getIndexes(), in1.getValue(), scalar_input2, 
+					in3.getIndexes(), in3.getValue(), ctableResult, ctableResultBlock, optr);
+				break;
+			}
+			default:
+				throw new DMLRuntimeException("Unrecognized opcode in Tertiary Instruction: " + instString);		
+		}
+	}
+	
+	@Override
+	public void processInstruction(Class<? extends MatrixValue> valueClass,
+			CachedValueMap cachedValues, IndexedMatrixValue tempValue, IndexedMatrixValue zeroInput, 
+			int blockRowFactor, int blockColFactor)
+		throws DMLRuntimeException 
+	{
+		throw new DMLRuntimeException("This function should not be called!");
+	}
+
+	@Override
+	public byte[] getAllIndexes() throws DMLRuntimeException {
+		return new byte[]{input1, input2, input3, output};
+	}
+
+	@Override
+	public byte[] getInputIndexes() throws DMLRuntimeException {
+		return new byte[]{input1, input2, input3};
+	}
+
+}

http://git-wip-us.apache.org/repos/asf/systemml/blob/57438103/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 b2e5283..355ccb4 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
@@ -32,7 +32,7 @@ public abstract class MRInstruction extends Instruction {
 	public enum MRType {
 		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,
+		PickByCount, Partition, Ctable, Quaternary, CM_N_COV, MapGroupedAggregate, GroupedAggregate, RightIndex,
 		ZeroOut, MMTSJ, PMMJ, MatrixReshape, ParameterizedBuiltin, Sort, MapMultChain, CumsumAggregate, CumsumSplit,
 		CumsumOffset, BinUaggChain, UaggOuterChain, RemoveEmpty
 	}

http://git-wip-us.apache.org/repos/asf/systemml/blob/57438103/src/main/java/org/apache/sysml/runtime/instructions/mr/QuaternaryInstruction.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/sysml/runtime/instructions/mr/QuaternaryInstruction.java b/src/main/java/org/apache/sysml/runtime/instructions/mr/QuaternaryInstruction.java
index 8efddb2..98504b0 100644
--- a/src/main/java/org/apache/sysml/runtime/instructions/mr/QuaternaryInstruction.java
+++ b/src/main/java/org/apache/sysml/runtime/instructions/mr/QuaternaryInstruction.java
@@ -293,7 +293,7 @@ public class QuaternaryInstruction extends MRInstruction implements IDistributed
 				if( imv==null )
 					continue;
 				MatrixIndexes inIx = imv.getIndexes();
-				MatrixValue inVal = imv.getValue();
+				MatrixBlock inVal = (MatrixBlock) imv.getValue();
 				
 				//allocate space for the output value
 				IndexedMatrixValue iout = null;
@@ -305,8 +305,8 @@ public class QuaternaryInstruction extends MRInstruction implements IDistributed
 				MatrixIndexes outIx = iout.getIndexes();
 				MatrixValue outVal = iout.getValue();
 				
-				//Step 2: get remaining inputs: Wij, Ui, Vj		
-				MatrixValue Xij = inVal;
+				//Step 2: get remaining inputs: Wij, Ui, Vj
+				MatrixBlock Xij = inVal;
 				
 				//get Wij if existing (null of WeightsType.NONE or WSigmoid any type)
 				IndexedMatrixValue iWij = (_input4 != -1) ? cachedValues.getFirst(_input4) : null; 
@@ -331,7 +331,7 @@ public class QuaternaryInstruction extends MRInstruction implements IDistributed
 				}
 				
 				//Step 3: process instruction
-				Xij.quaternaryOperations(qop, Ui, Vj, Wij, outVal);
+				Xij.quaternaryOperations(qop, (MatrixBlock)Ui, (MatrixBlock)Vj, (MatrixBlock)Wij, (MatrixBlock)outVal);
 				
 				//set output indexes
 				

http://git-wip-us.apache.org/repos/asf/systemml/blob/57438103/src/main/java/org/apache/sysml/runtime/instructions/mr/TernaryInstruction.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/sysml/runtime/instructions/mr/TernaryInstruction.java b/src/main/java/org/apache/sysml/runtime/instructions/mr/TernaryInstruction.java
deleted file mode 100644
index 14772f2..0000000
--- a/src/main/java/org/apache/sysml/runtime/instructions/mr/TernaryInstruction.java
+++ /dev/null
@@ -1,264 +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.mr;
-
-import java.util.HashMap;
-
-import org.apache.sysml.lops.Ternary;
-import org.apache.sysml.lops.Ternary.OperationTypes;
-import org.apache.sysml.runtime.DMLRuntimeException;
-import org.apache.sysml.runtime.instructions.InstructionUtils;
-import org.apache.sysml.runtime.matrix.data.CTableMap;
-import org.apache.sysml.runtime.matrix.data.MatrixBlock;
-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;
-
-public class TernaryInstruction extends MRInstruction {
-	private OperationTypes _op;
-
-	public byte input1;
-	public byte input2;
-	public byte input3;
-	public double scalar_input2;
-	public double scalar_input3;
-	private long _outputDim1, _outputDim2;
-
-	private TernaryInstruction(MRType type, OperationTypes op, byte in1, double scalar_in2, double scalar_in3, byte out,
-			long outputDim1, long outputDim2, String istr) {
-		super(type, null, out);
-		_op = op;
-		input1 = in1;
-		scalar_input2 = scalar_in2;
-		scalar_input3 = scalar_in3;
-		_outputDim1 = outputDim1;
-		_outputDim2 = outputDim2;
-		instString = istr;
-	}
-
-	private TernaryInstruction(MRType type, OperationTypes op, byte in1, byte in2, double scalar_in3, byte out, long outputDim1,
-			long outputDim2, String istr) {
-		super(type, null, out);
-		_op = op;
-		input1 = in1;
-		input2 = in2;
-		scalar_input3 = scalar_in3;
-		_outputDim1 = outputDim1;
-		_outputDim2 = outputDim2;
-		instString = istr;
-	}
-
-	private TernaryInstruction(MRType type, OperationTypes op, byte in1, double scalar_in2, byte in3, byte out, long outputDim1,
-			long outputDim2, String istr) {
-		super(type, null, out);
-		_op = op;
-		input1 = in1;
-		scalar_input2 = scalar_in2;
-		input3 = in3;
-		_outputDim1 = outputDim1;
-		_outputDim2 = outputDim2;
-		instString = istr;
-	}
-
-	protected TernaryInstruction(MRType type, OperationTypes op, byte in1, byte in2, byte in3, byte out, long outputDim1,
-			long outputDim2, String istr) {
-		super(type, null, out);
-		_op = op;
-		input1 = in1;
-		input2 = in2;
-		input3 = in3;
-		_outputDim1 = outputDim1;
-		_outputDim2 = outputDim2;
-		instString = istr;
-	}
-
-	public long getOutputDim1() {
-		return _outputDim1;
-	}
-	
-	public long getOutputDim2() {
-		return _outputDim2;
-	}
-	
-	public boolean knownOutputDims() {
-		return (_outputDim1 >0 && _outputDim2>0);
-	}
-
-	public static TernaryInstruction parseInstruction ( String str ) 
-		throws DMLRuntimeException 
-	{		
-		// example instruction string 
-		// - ctabletransform:::0:DOUBLE:::1:DOUBLE:::2:DOUBLE:::3:DOUBLE 
-		// - ctabletransformscalarweight:::0:DOUBLE:::1:DOUBLE:::1.0:DOUBLE:::3:DOUBLE 
-		// - ctabletransformhistogram:::0:DOUBLE:::1.0:DOUBLE:::1.0:DOUBLE:::3:DOUBLE 
-		// - ctabletransformweightedhistogram:::0:DOUBLE:::1:INT:::1:DOUBLE:::2:DOUBLE 
-		
-		//check number of fields
-		InstructionUtils.checkNumFields ( str, 6 );
-		
-		//common setup
-		byte in1, in2, in3, out;
-		String[] parts = InstructionUtils.getInstructionParts ( str );		
-		String opcode = parts[0];
-		in1 = Byte.parseByte(parts[1]);
-		long outputDim1 = (long) Double.parseDouble(parts[4]);
-		long outputDim2 = (long) Double.parseDouble(parts[5]);
-		out = Byte.parseByte(parts[6]);
-		
-		OperationTypes op = Ternary.getOperationType(opcode);
-		
-		switch( op )
-		{
-			case CTABLE_TRANSFORM: {
-				in2 = Byte.parseByte(parts[2]);
-				in3 = Byte.parseByte(parts[3]);
-				return new TernaryInstruction(MRType.Ternary, op, in1, in2, in3, out, outputDim1, outputDim2, str);
-			}
-			case CTABLE_TRANSFORM_SCALAR_WEIGHT: {
-				in2 = Byte.parseByte(parts[2]);
-				double scalar_in3 = Double.parseDouble(parts[3]);
-				return new TernaryInstruction(MRType.Ternary, op, in1, in2, scalar_in3, out, outputDim1, outputDim2, str);
-			}
-			case CTABLE_EXPAND_SCALAR_WEIGHT: {
-				double scalar_in2 = Double.parseDouble(parts[2]);
-				double type = Double.parseDouble(parts[3]); //used as type (1 left, 0 right)
-				return new TernaryInstruction(MRType.Ternary, op, in1, scalar_in2, type, out, outputDim1, outputDim2, str);
-			}
-			case CTABLE_TRANSFORM_HISTOGRAM: {
-				double scalar_in2 = Double.parseDouble(parts[2]);
-				double scalar_in3 = Double.parseDouble(parts[3]);
-				return new TernaryInstruction(MRType.Ternary, op, in1, scalar_in2, scalar_in3, out, outputDim1, outputDim2, str);
-			}
-			case CTABLE_TRANSFORM_WEIGHTED_HISTOGRAM: {
-				double scalar_in2 = Double.parseDouble(parts[2]);
-				in3 = Byte.parseByte(parts[3]);
-				return new TernaryInstruction(MRType.Ternary, op, in1, scalar_in2, in3, out, outputDim1, outputDim2, str);	
-			}
-			default:
-				throw new DMLRuntimeException("Unrecognized opcode in Ternary Instruction: " + op);	
-		}
-	}
-	
-	public void processInstruction(Class<? extends MatrixValue> valueClass, CachedValueMap cachedValues, 
-			            IndexedMatrixValue zeroInput, HashMap<Byte, CTableMap> resultMaps, HashMap<Byte, MatrixBlock> resultBlocks, 
-			            int blockRowFactor, int blockColFactor)
-		throws DMLRuntimeException 
-	{	
-		
-		IndexedMatrixValue in1, in2, in3 = null;
-		in1 = cachedValues.getFirst(input1);
-		
-		CTableMap ctableResult = null;
-		MatrixBlock ctableResultBlock = null;
-		
-		if ( knownOutputDims() ) {
-			if ( resultBlocks != null ) {
-				ctableResultBlock = resultBlocks.get(output);
-				if ( ctableResultBlock == null ) {
-					// From MR, output of ctable is set to be sparse since it is built from a single input block.
-					ctableResultBlock = new MatrixBlock((int)_outputDim1, (int)_outputDim2, true);
-					resultBlocks.put(output, ctableResultBlock);
-				}
-			}
-			else {
-				throw new DMLRuntimeException("Unexpected error in processing table instruction.");
-			}
-		}
-		else {
-			//prepare aggregation maps
-			ctableResult=resultMaps.get(output);
-			if(ctableResult==null)
-			{
-				ctableResult = new CTableMap();
-				resultMaps.put(output, ctableResult);
-			}
-		}
-		
-		//get inputs and process instruction
-		switch( _op )
-		{
-			case CTABLE_TRANSFORM: {
-				in2 = cachedValues.getFirst(input2);
-				in3 = cachedValues.getFirst(input3);
-				if(in1==null || in2==null || in3 == null )
-					return;	
-				OperationsOnMatrixValues.performTernary(in1.getIndexes(), in1.getValue(), in2.getIndexes(), in2.getValue(), 
-						                                 in3.getIndexes(), in3.getValue(), ctableResult, ctableResultBlock, optr);
-				break;
-			}
-			case CTABLE_TRANSFORM_SCALAR_WEIGHT: {
-				// 3rd input is a scalar
-				in2 = cachedValues.getFirst(input2);
-				if(in1==null || in2==null )
-					return;
-				OperationsOnMatrixValues.performTernary(in1.getIndexes(), in1.getValue(), in2.getIndexes(), in2.getValue(), 
-						                                 scalar_input3, ctableResult, ctableResultBlock, optr);
-				break;
-			}
-			case CTABLE_EXPAND_SCALAR_WEIGHT: {
-				// 2nd and 3rd input is a scalar
-				if(in1==null )
-					return;
-				OperationsOnMatrixValues.performTernary(in1.getIndexes(), in1.getValue(), scalar_input2, (scalar_input3==1), 
-						                                 blockRowFactor, ctableResult, ctableResultBlock, optr);
-				break;
-			}
-			case CTABLE_TRANSFORM_HISTOGRAM: {
-				// 2nd and 3rd inputs are scalars
-				if(in1==null )
-					return;	
-				OperationsOnMatrixValues.performTernary(in1.getIndexes(), in1.getValue(), scalar_input2, scalar_input3, ctableResult, ctableResultBlock, optr);
-				break;
-			}
-			case CTABLE_TRANSFORM_WEIGHTED_HISTOGRAM: {
-				// 2nd and 3rd inputs are scalars
-				in3 = cachedValues.getFirst(input3);
-				if(in1==null || in3==null)
-					return;
-				OperationsOnMatrixValues.performTernary(in1.getIndexes(), in1.getValue(), scalar_input2, 
-						                                 in3.getIndexes(), in3.getValue(), ctableResult, ctableResultBlock, optr);		
-				break;
-			}
-			default:
-				throw new DMLRuntimeException("Unrecognized opcode in Tertiary Instruction: " + instString);		
-		}
-	}
-	
-	@Override
-	public void processInstruction(Class<? extends MatrixValue> valueClass,
-			CachedValueMap cachedValues, IndexedMatrixValue tempValue, IndexedMatrixValue zeroInput, 
-			int blockRowFactor, int blockColFactor)
-		throws DMLRuntimeException 
-	{
-		throw new DMLRuntimeException("This function should not be called!");
-	}
-
-	@Override
-	public byte[] getAllIndexes() throws DMLRuntimeException {
-		return new byte[]{input1, input2, input3, output};
-	}
-
-	@Override
-	public byte[] getInputIndexes() throws DMLRuntimeException {
-		return new byte[]{input1, input2, input3};
-	}
-
-}

http://git-wip-us.apache.org/repos/asf/systemml/blob/57438103/src/main/java/org/apache/sysml/runtime/instructions/spark/CtableSPInstruction.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/sysml/runtime/instructions/spark/CtableSPInstruction.java b/src/main/java/org/apache/sysml/runtime/instructions/spark/CtableSPInstruction.java
new file mode 100644
index 0000000..001e7b6
--- /dev/null
+++ b/src/main/java/org/apache/sysml/runtime/instructions/spark/CtableSPInstruction.java
@@ -0,0 +1,510 @@
+/*
+ * 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 java.util.ArrayList;
+import java.util.Iterator;
+
+import org.apache.spark.api.java.JavaPairRDD;
+import org.apache.spark.api.java.function.Function;
+import org.apache.spark.api.java.function.PairFlatMapFunction;
+import org.apache.spark.api.java.function.PairFunction;
+
+import scala.Tuple2;
+
+import org.apache.sysml.lops.Ctable;
+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.functionobjects.CTable;
+import org.apache.sysml.runtime.instructions.Instruction;
+import org.apache.sysml.runtime.instructions.InstructionUtils;
+import org.apache.sysml.runtime.instructions.cp.CPOperand;
+import org.apache.sysml.runtime.instructions.spark.utils.RDDAggregateUtils;
+import org.apache.sysml.runtime.instructions.spark.utils.SparkUtils;
+import org.apache.sysml.runtime.matrix.MatrixCharacteristics;
+import org.apache.sysml.runtime.matrix.data.CTableMap;
+import org.apache.sysml.runtime.matrix.data.MatrixBlock;
+import org.apache.sysml.runtime.matrix.data.MatrixCell;
+import org.apache.sysml.runtime.matrix.data.MatrixIndexes;
+import org.apache.sysml.runtime.matrix.data.OperationsOnMatrixValues;
+import org.apache.sysml.runtime.matrix.data.Pair;
+import org.apache.sysml.runtime.matrix.mapred.IndexedMatrixValue;
+import org.apache.sysml.runtime.matrix.operators.Operator;
+import org.apache.sysml.runtime.matrix.operators.SimpleOperator;
+import org.apache.sysml.runtime.util.LongLongDoubleHashMap.ADoubleEntry;
+import org.apache.sysml.runtime.util.UtilFunctions;
+
+public class CtableSPInstruction extends ComputationSPInstruction {
+	private String _outDim1;
+	private String _outDim2;
+	private boolean _dim1Literal;
+	private boolean _dim2Literal;
+	private boolean _isExpand;
+	private boolean _ignoreZeros;
+
+	private CtableSPInstruction(Operator op, CPOperand in1, CPOperand in2, CPOperand in3, CPOperand out,
+			String outputDim1, boolean dim1Literal, String outputDim2, boolean dim2Literal, boolean isExpand,
+			boolean ignoreZeros, String opcode, String istr) {
+		super(SPType.Ctable, op, in1, in2, in3, out, opcode, istr);
+		_outDim1 = outputDim1;
+		_dim1Literal = dim1Literal;
+		_outDim2 = outputDim2;
+		_dim2Literal = dim2Literal;
+		_isExpand = isExpand;
+		_ignoreZeros = ignoreZeros;
+	}
+
+	public static CtableSPInstruction parseInstruction(String inst) 
+		throws DMLRuntimeException
+	{	
+		String[] parts = InstructionUtils.getInstructionPartsWithValueType(inst);
+		InstructionUtils.checkNumFields ( parts, 7 );
+		
+		String opcode = parts[0];
+		
+		//handle opcode
+		if ( !(opcode.equalsIgnoreCase("ctable") || opcode.equalsIgnoreCase("ctableexpand")) ) {
+			throw new DMLRuntimeException("Unexpected opcode in TertiarySPInstruction: " + inst);
+		}
+		boolean isExpand = opcode.equalsIgnoreCase("ctableexpand");
+		
+		//handle operands
+		CPOperand in1 = new CPOperand(parts[1]);
+		CPOperand in2 = new CPOperand(parts[2]);
+		CPOperand in3 = new CPOperand(parts[3]);
+		
+		//handle known dimension information
+		String[] dim1Fields = parts[4].split(Instruction.LITERAL_PREFIX);
+		String[] dim2Fields = parts[5].split(Instruction.LITERAL_PREFIX);
+
+		CPOperand out = new CPOperand(parts[6]);
+		boolean ignoreZeros = Boolean.parseBoolean(parts[7]);
+		
+		// ctable does not require any operator, so we simply pass-in a dummy operator with null functionobject
+		return new CtableSPInstruction(new SimpleOperator(null), in1, in2, in3, out, dim1Fields[0], Boolean.parseBoolean(dim1Fields[1]), dim2Fields[0], Boolean.parseBoolean(dim2Fields[1]), isExpand, ignoreZeros, opcode, inst);
+	}
+
+
+	@Override
+	public void processInstruction(ExecutionContext ec) 
+		throws DMLRuntimeException 
+	{	
+		SparkExecutionContext sec = (SparkExecutionContext)ec;
+	
+		//get input rdd handle
+		JavaPairRDD<MatrixIndexes,MatrixBlock> in1 = sec.getBinaryBlockRDDHandleForVariable( input1.getName() );
+		JavaPairRDD<MatrixIndexes,MatrixBlock> in2 = null;
+		JavaPairRDD<MatrixIndexes,MatrixBlock> in3 = null;
+		double scalar_input2 = -1, scalar_input3 = -1;
+		
+		Ctable.OperationTypes ctableOp = Ctable.findCtableOperationByInputDataTypes(
+				input1.getDataType(), input2.getDataType(), input3.getDataType());
+		ctableOp = _isExpand ? Ctable.OperationTypes.CTABLE_EXPAND_SCALAR_WEIGHT : ctableOp;
+		
+		MatrixCharacteristics mc1 = sec.getMatrixCharacteristics(input1.getName());
+		MatrixCharacteristics mcOut = sec.getMatrixCharacteristics(output.getName());
+		
+		// First get the block sizes and then set them as -1 to allow for binary cell reblock
+		int brlen = mc1.getRowsPerBlock();
+		int bclen = mc1.getColsPerBlock();
+		
+		JavaPairRDD<MatrixIndexes, ArrayList<MatrixBlock>> inputMBs = null;
+		JavaPairRDD<MatrixIndexes, CTableMap> ctables = null;
+		JavaPairRDD<MatrixIndexes, Double> bincellsNoFilter = null;
+		boolean setLineage2 = false;
+		boolean setLineage3 = false;
+		switch(ctableOp) {
+			case CTABLE_TRANSFORM: //(VECTOR)
+				// F=ctable(A,B,W) 
+				in2 = sec.getBinaryBlockRDDHandleForVariable( input2.getName() );
+				in3 = sec.getBinaryBlockRDDHandleForVariable( input3.getName() );
+				setLineage2 = true;
+				setLineage3 = true;
+				
+				inputMBs = in1.cogroup(in2).cogroup(in3)
+							.mapToPair(new MapThreeMBIterableIntoAL());
+				
+				ctables = inputMBs.mapToPair(new PerformCTableMapSideOperation(ctableOp, scalar_input2, 
+							scalar_input3, this.instString, (SimpleOperator)_optr, _ignoreZeros));
+				break;
+			
+				
+			case CTABLE_EXPAND_SCALAR_WEIGHT: //(VECTOR)
+				// F = ctable(seq,A) or F = ctable(seq,B,1)
+				scalar_input3 = sec.getScalarInput(input3.getName(), input3.getValueType(), input3.isLiteral()).getDoubleValue();
+				if(scalar_input3 == 1) {
+					in2 = sec.getBinaryBlockRDDHandleForVariable( input2.getName() );
+					setLineage2 = true;
+					bincellsNoFilter = in2.flatMapToPair(new ExpandScalarCtableOperation(brlen));
+					break;
+				}
+			case CTABLE_TRANSFORM_SCALAR_WEIGHT: //(VECTOR/MATRIX)
+				// F = ctable(A,B) or F = ctable(A,B,1)
+				in2 = sec.getBinaryBlockRDDHandleForVariable( input2.getName() );
+				setLineage2 = true;
+
+				scalar_input3 = sec.getScalarInput(input3.getName(), input3.getValueType(), input3.isLiteral()).getDoubleValue();
+				inputMBs = in1.cogroup(in2).mapToPair(new MapTwoMBIterableIntoAL());
+				
+				ctables = inputMBs.mapToPair(new PerformCTableMapSideOperation(ctableOp, scalar_input2, 
+						scalar_input3, this.instString, (SimpleOperator)_optr, _ignoreZeros));
+				break;
+				
+			case CTABLE_TRANSFORM_HISTOGRAM: //(VECTOR)
+				// F=ctable(A,1) or F = ctable(A,1,1)
+				scalar_input2 = sec.getScalarInput(input2.getName(), input2.getValueType(), input2.isLiteral()).getDoubleValue();
+				scalar_input3 = sec.getScalarInput(input3.getName(), input3.getValueType(), input3.isLiteral()).getDoubleValue();
+				inputMBs = in1.mapToPair(new MapMBIntoAL());
+				
+				ctables = inputMBs.mapToPair(new PerformCTableMapSideOperation(ctableOp, scalar_input2, 
+						scalar_input3, this.instString, (SimpleOperator)_optr, _ignoreZeros));
+				break;
+				
+			case CTABLE_TRANSFORM_WEIGHTED_HISTOGRAM: //(VECTOR)
+				// F=ctable(A,1,W)
+				in3 = sec.getBinaryBlockRDDHandleForVariable( input3.getName() );
+				setLineage3 = true;
+				
+				scalar_input2 = sec.getScalarInput(input2.getName(), input2.getValueType(), input2.isLiteral()).getDoubleValue();
+				inputMBs = in1.cogroup(in3).mapToPair(new MapTwoMBIterableIntoAL());
+				
+				ctables = inputMBs.mapToPair(new PerformCTableMapSideOperation(ctableOp, scalar_input2, 
+						scalar_input3, this.instString, (SimpleOperator)_optr, _ignoreZeros));
+				break;
+			
+			default:
+				throw new DMLRuntimeException("Encountered an invalid ctable operation ("+ctableOp+") while executing instruction: " + this.toString());
+		}
+		
+		// Now perform aggregation on ctables to get binaryCells 
+		if(bincellsNoFilter == null && ctables != null) {
+			bincellsNoFilter =  
+					ctables.values()
+					.flatMapToPair(new ExtractBinaryCellsFromCTable());
+			bincellsNoFilter = RDDAggregateUtils.sumCellsByKeyStable(bincellsNoFilter);
+		}
+		else if(!(bincellsNoFilter != null && ctables == null)) {
+			throw new DMLRuntimeException("Incorrect ctable operation");
+		}
+		
+		// handle known/unknown dimensions
+		long outputDim1 = (_dim1Literal ? (long) Double.parseDouble(_outDim1) : (sec.getScalarInput(_outDim1, ValueType.DOUBLE, false)).getLongValue());
+		long outputDim2 = (_dim2Literal ? (long) Double.parseDouble(_outDim2) : (sec.getScalarInput(_outDim2, ValueType.DOUBLE, false)).getLongValue());
+		MatrixCharacteristics mcBinaryCells = null;
+		boolean findDimensions = (outputDim1 == -1 && outputDim2 == -1); 
+		
+		if( !findDimensions ) {
+			if((outputDim1 == -1 && outputDim2 != -1) || (outputDim1 != -1 && outputDim2 == -1))
+				throw new DMLRuntimeException("Incorrect output dimensions passed to TernarySPInstruction:" + outputDim1 + " " + outputDim2);
+			else 
+				mcBinaryCells = new MatrixCharacteristics(outputDim1, outputDim2, brlen, bclen);	
+			
+			// filtering according to given dimensions
+			bincellsNoFilter = bincellsNoFilter
+					.filter(new FilterCells(mcBinaryCells.getRows(), mcBinaryCells.getCols()));
+		}
+		
+		// convert double values to matrix cell
+		JavaPairRDD<MatrixIndexes, MatrixCell> binaryCells = bincellsNoFilter
+				.mapToPair(new ConvertToBinaryCell());
+		
+		// find dimensions if necessary (w/ cache for reblock)
+		if( findDimensions ) {						
+			binaryCells = SparkUtils.cacheBinaryCellRDD(binaryCells);
+			mcBinaryCells = SparkUtils.computeMatrixCharacteristics(binaryCells);
+		}
+		
+		//store output rdd handle
+		sec.setRDDHandleForVariable(output.getName(), binaryCells);
+		mcOut.set(mcBinaryCells);
+		// Since we are outputing binary cells, we set block sizes = -1
+		mcOut.setRowsPerBlock(-1); mcOut.setColsPerBlock(-1);
+		sec.addLineageRDD(output.getName(), input1.getName());
+		if(setLineage2)
+			sec.addLineageRDD(output.getName(), input2.getName());
+		if(setLineage3)
+			sec.addLineageRDD(output.getName(), input3.getName());
+	}	
+
+	private static class ExpandScalarCtableOperation implements PairFlatMapFunction<Tuple2<MatrixIndexes,MatrixBlock>, MatrixIndexes, Double> 
+	{
+		private static final long serialVersionUID = -12552669148928288L;
+	
+		private int _brlen;
+		
+		public ExpandScalarCtableOperation(int brlen) {
+			_brlen = brlen;
+		}
+
+		@Override
+		public Iterator<Tuple2<MatrixIndexes, Double>> call(Tuple2<MatrixIndexes, MatrixBlock> arg0) 
+			throws Exception 
+		{
+			MatrixIndexes ix = arg0._1();
+			MatrixBlock mb = arg0._2(); //col-vector
+			
+			//create an output cell per matrix block row (aligned w/ original source position)
+			ArrayList<Tuple2<MatrixIndexes, Double>> retVal = new ArrayList<>();
+			CTable ctab = CTable.getCTableFnObject();
+			for( int i=0; i<mb.getNumRows(); i++ )
+			{
+				//compute global target indexes (via ctable obj for error handling consistency)
+				long row = UtilFunctions.computeCellIndex(ix.getRowIndex(), _brlen, i);
+				double v2 = mb.quickGetValue(i, 0);
+				Pair<MatrixIndexes,Double> p = ctab.execute(row, v2, 1.0);
+				
+				//indirect construction over pair to avoid tuple2 dependency in general ctable obj
+				if( p.getKey().getRowIndex() >= 1 ) //filter rejected entries
+					retVal.add(new Tuple2<>(p.getKey(), p.getValue()));
+			}
+			
+			return retVal.iterator();
+		}
+	}
+	
+	private static class MapTwoMBIterableIntoAL implements PairFunction<Tuple2<MatrixIndexes,Tuple2<Iterable<MatrixBlock>,Iterable<MatrixBlock>>>, MatrixIndexes, ArrayList<MatrixBlock>> {
+
+		private static final long serialVersionUID = 271459913267735850L;
+
+		private static MatrixBlock extractBlock(Iterable<MatrixBlock> blks, MatrixBlock retVal) throws Exception {
+			for(MatrixBlock blk1 : blks) {
+				if(retVal != null) {
+					throw new Exception("ERROR: More than 1 matrixblock found for one of the inputs at a given index");
+				}
+				retVal = blk1;
+			}
+			if(retVal == null) {
+				throw new Exception("ERROR: No matrixblock found for one of the inputs at a given index");
+			}
+			return retVal;
+		}
+		
+		@Override
+		public Tuple2<MatrixIndexes, ArrayList<MatrixBlock>> call(
+				Tuple2<MatrixIndexes, Tuple2<Iterable<MatrixBlock>, Iterable<MatrixBlock>>> kv)
+				throws Exception {
+			MatrixBlock in1 = null; MatrixBlock in2 = null;
+			in1 = extractBlock(kv._2._1, in1);
+			in2 = extractBlock(kv._2._2, in2);
+			// Now return unflatten AL
+			ArrayList<MatrixBlock> inputs = new ArrayList<>();
+			inputs.add(in1); inputs.add(in2);  
+			return new Tuple2<>(kv._1, inputs);
+		}
+		
+	}
+	
+	private static class MapThreeMBIterableIntoAL implements PairFunction<Tuple2<MatrixIndexes,Tuple2<Iterable<Tuple2<Iterable<MatrixBlock>,Iterable<MatrixBlock>>>,Iterable<MatrixBlock>>>, MatrixIndexes, ArrayList<MatrixBlock>> {
+
+		private static final long serialVersionUID = -4873754507037646974L;
+		
+		private static MatrixBlock extractBlock(Iterable<MatrixBlock> blks, MatrixBlock retVal) throws Exception {
+			for(MatrixBlock blk1 : blks) {
+				if(retVal != null) {
+					throw new Exception("ERROR: More than 1 matrixblock found for one of the inputs at a given index");
+				}
+				retVal = blk1;
+			}
+			if(retVal == null) {
+				throw new Exception("ERROR: No matrixblock found for one of the inputs at a given index");
+			}
+			return retVal;
+		}
+
+		@Override
+		public Tuple2<MatrixIndexes, ArrayList<MatrixBlock>> call(
+				Tuple2<MatrixIndexes, Tuple2<Iterable<Tuple2<Iterable<MatrixBlock>, Iterable<MatrixBlock>>>, Iterable<MatrixBlock>>> kv)
+				throws Exception {
+			MatrixBlock in1 = null; MatrixBlock in2 = null; MatrixBlock in3 = null;
+			
+			for(Tuple2<Iterable<MatrixBlock>, Iterable<MatrixBlock>> blks : kv._2._1) {
+				in1 = extractBlock(blks._1, in1);
+				in2 = extractBlock(blks._2, in2);
+			}
+			in3 = extractBlock(kv._2._2, in3);
+			
+			// Now return unflatten AL
+			ArrayList<MatrixBlock> inputs = new ArrayList<>();
+			inputs.add(in1); inputs.add(in2); inputs.add(in3);  
+			return new Tuple2<>(kv._1, inputs);
+		}
+		
+	}
+	
+	private static class PerformCTableMapSideOperation implements PairFunction<Tuple2<MatrixIndexes,ArrayList<MatrixBlock>>, MatrixIndexes, CTableMap> {
+
+		private static final long serialVersionUID = 5348127596473232337L;
+
+		Ctable.OperationTypes ctableOp;
+		double scalar_input2; double scalar_input3;
+		String instString;
+		Operator optr;
+		boolean ignoreZeros;
+		
+		public PerformCTableMapSideOperation(Ctable.OperationTypes ctableOp, double scalar_input2, double scalar_input3, String instString, Operator optr, boolean ignoreZeros) {
+			this.ctableOp = ctableOp;
+			this.scalar_input2 = scalar_input2;
+			this.scalar_input3 = scalar_input3;
+			this.instString = instString;
+			this.optr = optr;
+			this.ignoreZeros = ignoreZeros;
+		}
+		
+		private static void expectedALSize(int length, ArrayList<MatrixBlock> al) throws Exception {
+			if(al.size() != length) {
+				throw new Exception("Expected arraylist of size:" + length + ", but found " + al.size());
+			}
+		}
+		
+		@Override
+		public Tuple2<MatrixIndexes, CTableMap> call(
+				Tuple2<MatrixIndexes, ArrayList<MatrixBlock>> kv) throws Exception {
+			CTableMap ctableResult = new CTableMap();
+			MatrixBlock ctableResultBlock = null;
+			
+			IndexedMatrixValue in1, in2, in3 = null;
+			in1 = new IndexedMatrixValue(kv._1, kv._2.get(0));
+			MatrixBlock matBlock1 = kv._2.get(0);
+			
+			switch( ctableOp )
+			{
+				case CTABLE_TRANSFORM: {
+					in2 = new IndexedMatrixValue(kv._1, kv._2.get(1));
+					in3 = new IndexedMatrixValue(kv._1, kv._2.get(2));
+					expectedALSize(3, kv._2);
+					
+					if(in1==null || in2==null || in3 == null )
+						break;	
+					else
+						OperationsOnMatrixValues.performCtable(in1.getIndexes(), in1.getValue(), in2.getIndexes(),
+							in2.getValue(), in3.getIndexes(), in3.getValue(), ctableResult, ctableResultBlock, optr);
+					break;
+				}
+				case CTABLE_TRANSFORM_SCALAR_WEIGHT: 
+				case CTABLE_EXPAND_SCALAR_WEIGHT:
+				{
+					// 3rd input is a scalar
+					in2 = new IndexedMatrixValue(kv._1, kv._2.get(1));
+					expectedALSize(2, kv._2);
+					if(in1==null || in2==null )
+						break;
+					else
+						matBlock1.ctableOperations((SimpleOperator)optr, kv._2.get(1), scalar_input3, ignoreZeros, ctableResult, ctableResultBlock);
+						break;
+				}
+				case CTABLE_TRANSFORM_HISTOGRAM: {
+					expectedALSize(1, kv._2);
+					OperationsOnMatrixValues.performCtable(in1.getIndexes(), in1.getValue(), scalar_input2, 
+							scalar_input3, ctableResult, ctableResultBlock, optr);
+					break;
+				}
+				case CTABLE_TRANSFORM_WEIGHTED_HISTOGRAM: {
+					// 2nd and 3rd inputs are scalars
+					expectedALSize(2, kv._2);
+					in3 = new IndexedMatrixValue(kv._1, kv._2.get(1)); // Note: kv._2.get(1), not kv._2.get(2)
+					
+					if(in1==null || in3==null)
+						break;
+					else
+						OperationsOnMatrixValues.performCtable(in1.getIndexes(), in1.getValue(), scalar_input2, 
+								in3.getIndexes(), in3.getValue(), ctableResult, ctableResultBlock, optr);		
+					break;
+				}
+				default:
+					throw new DMLRuntimeException("Unrecognized opcode in Tertiary Instruction: " + instString);
+			}
+			return new Tuple2<>(kv._1, ctableResult);
+		}
+		
+	}
+	
+	private static class MapMBIntoAL implements PairFunction<Tuple2<MatrixIndexes, MatrixBlock>, MatrixIndexes, ArrayList<MatrixBlock>> {
+
+		private static final long serialVersionUID = 2068398913653350125L;
+
+		@Override
+		public Tuple2<MatrixIndexes, ArrayList<MatrixBlock>> call(
+				Tuple2<MatrixIndexes, MatrixBlock> kv) throws Exception {
+			ArrayList<MatrixBlock> retVal = new ArrayList<>();
+			retVal.add(kv._2);
+			return new Tuple2<>(kv._1, retVal);
+		}
+		
+	}
+	
+	private static class ExtractBinaryCellsFromCTable implements PairFlatMapFunction<CTableMap, MatrixIndexes, Double> {
+
+		private static final long serialVersionUID = -5933677686766674444L;
+		
+		@Override
+		public Iterator<Tuple2<MatrixIndexes, Double>> call(CTableMap ctableMap)
+				throws Exception {
+			ArrayList<Tuple2<MatrixIndexes, Double>> retVal = new ArrayList<>();
+			Iterator<ADoubleEntry> iter = ctableMap.getIterator();
+			while( iter.hasNext() ) {
+				ADoubleEntry ijv = iter.next();
+				long i = ijv.getKey1();
+				long j =  ijv.getKey2();
+				double v =  ijv.value;
+				retVal.add(new Tuple2<>(new MatrixIndexes(i, j), v));
+			}
+			return retVal.iterator();
+		}
+		
+	}
+	
+	private static class ConvertToBinaryCell implements PairFunction<Tuple2<MatrixIndexes,Double>, MatrixIndexes, MatrixCell> {
+
+		private static final long serialVersionUID = 7481186480851982800L;
+		
+		@Override
+		public Tuple2<MatrixIndexes, MatrixCell> call(
+				Tuple2<MatrixIndexes, Double> kv) throws Exception {
+			
+			MatrixCell cell = new MatrixCell(kv._2().doubleValue());
+			return new Tuple2<>(kv._1(), cell);
+		}
+		
+	}
+	
+	private static class FilterCells implements Function<Tuple2<MatrixIndexes,Double>, Boolean> {
+		private static final long serialVersionUID = 108448577697623247L;
+
+		long rlen; long clen;
+		public FilterCells(long rlen, long clen) {
+			this.rlen = rlen;
+			this.clen = clen;
+		}
+		
+		@Override
+		public Boolean call(Tuple2<MatrixIndexes, Double> kv) throws Exception {
+			if(kv._1.getRowIndex() <= 0 || kv._1.getColumnIndex() <= 0) {
+				throw new Exception("Incorrect cell values in TernarySPInstruction:" + kv._1);
+			}
+			if(kv._1.getRowIndex() <= rlen && kv._1.getColumnIndex() <= clen) {
+				return true;
+			}
+			return false;
+		}
+		
+	}
+}

http://git-wip-us.apache.org/repos/asf/systemml/blob/57438103/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 d1c12ef..ef7158c 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
@@ -36,7 +36,7 @@ public abstract class SPInstruction extends Instruction {
 		Builtin, Unary, BuiltinNary, MultiReturnBuiltin, Checkpoint, Compression, Cast,
 		CentralMoment, Covariance, QSort, QPick, 
 		ParameterizedBuiltin, MAppend, RAppend, GAppend, GAlignedAppend, Rand, 
-		MatrixReshape, Ternary, Quaternary, CumsumAggregate, CumsumOffset, BinUaggChain, UaggOuterChain, 
+		MatrixReshape, Ctable, Quaternary, CumsumAggregate, CumsumOffset, BinUaggChain, UaggOuterChain, 
 		Write, SpoofFused, Convolution
 	}
 

http://git-wip-us.apache.org/repos/asf/systemml/blob/57438103/src/main/java/org/apache/sysml/runtime/instructions/spark/TernarySPInstruction.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/sysml/runtime/instructions/spark/TernarySPInstruction.java b/src/main/java/org/apache/sysml/runtime/instructions/spark/TernarySPInstruction.java
deleted file mode 100644
index 7bc4cb5..0000000
--- a/src/main/java/org/apache/sysml/runtime/instructions/spark/TernarySPInstruction.java
+++ /dev/null
@@ -1,510 +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 java.util.ArrayList;
-import java.util.Iterator;
-
-import org.apache.spark.api.java.JavaPairRDD;
-import org.apache.spark.api.java.function.Function;
-import org.apache.spark.api.java.function.PairFlatMapFunction;
-import org.apache.spark.api.java.function.PairFunction;
-
-import scala.Tuple2;
-
-import org.apache.sysml.lops.Ternary;
-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.functionobjects.CTable;
-import org.apache.sysml.runtime.instructions.Instruction;
-import org.apache.sysml.runtime.instructions.InstructionUtils;
-import org.apache.sysml.runtime.instructions.cp.CPOperand;
-import org.apache.sysml.runtime.instructions.spark.utils.RDDAggregateUtils;
-import org.apache.sysml.runtime.instructions.spark.utils.SparkUtils;
-import org.apache.sysml.runtime.matrix.MatrixCharacteristics;
-import org.apache.sysml.runtime.matrix.data.CTableMap;
-import org.apache.sysml.runtime.matrix.data.MatrixBlock;
-import org.apache.sysml.runtime.matrix.data.MatrixCell;
-import org.apache.sysml.runtime.matrix.data.MatrixIndexes;
-import org.apache.sysml.runtime.matrix.data.OperationsOnMatrixValues;
-import org.apache.sysml.runtime.matrix.data.Pair;
-import org.apache.sysml.runtime.matrix.mapred.IndexedMatrixValue;
-import org.apache.sysml.runtime.matrix.operators.Operator;
-import org.apache.sysml.runtime.matrix.operators.SimpleOperator;
-import org.apache.sysml.runtime.util.LongLongDoubleHashMap.ADoubleEntry;
-import org.apache.sysml.runtime.util.UtilFunctions;
-
-public class TernarySPInstruction extends ComputationSPInstruction {
-	private String _outDim1;
-	private String _outDim2;
-	private boolean _dim1Literal;
-	private boolean _dim2Literal;
-	private boolean _isExpand;
-	private boolean _ignoreZeros;
-
-	private TernarySPInstruction(Operator op, CPOperand in1, CPOperand in2, CPOperand in3, CPOperand out,
-			String outputDim1, boolean dim1Literal, String outputDim2, boolean dim2Literal, boolean isExpand,
-			boolean ignoreZeros, String opcode, String istr) {
-		super(SPType.Ternary, op, in1, in2, in3, out, opcode, istr);
-		_outDim1 = outputDim1;
-		_dim1Literal = dim1Literal;
-		_outDim2 = outputDim2;
-		_dim2Literal = dim2Literal;
-		_isExpand = isExpand;
-		_ignoreZeros = ignoreZeros;
-	}
-
-	public static TernarySPInstruction parseInstruction(String inst) 
-		throws DMLRuntimeException
-	{	
-		String[] parts = InstructionUtils.getInstructionPartsWithValueType(inst);
-		InstructionUtils.checkNumFields ( parts, 7 );
-		
-		String opcode = parts[0];
-		
-		//handle opcode
-		if ( !(opcode.equalsIgnoreCase("ctable") || opcode.equalsIgnoreCase("ctableexpand")) ) {
-			throw new DMLRuntimeException("Unexpected opcode in TertiarySPInstruction: " + inst);
-		}
-		boolean isExpand = opcode.equalsIgnoreCase("ctableexpand");
-		
-		//handle operands
-		CPOperand in1 = new CPOperand(parts[1]);
-		CPOperand in2 = new CPOperand(parts[2]);
-		CPOperand in3 = new CPOperand(parts[3]);
-		
-		//handle known dimension information
-		String[] dim1Fields = parts[4].split(Instruction.LITERAL_PREFIX);
-		String[] dim2Fields = parts[5].split(Instruction.LITERAL_PREFIX);
-
-		CPOperand out = new CPOperand(parts[6]);
-		boolean ignoreZeros = Boolean.parseBoolean(parts[7]);
-		
-		// ctable does not require any operator, so we simply pass-in a dummy operator with null functionobject
-		return new TernarySPInstruction(new SimpleOperator(null), in1, in2, in3, out, dim1Fields[0], Boolean.parseBoolean(dim1Fields[1]), dim2Fields[0], Boolean.parseBoolean(dim2Fields[1]), isExpand, ignoreZeros, opcode, inst);
-	}
-
-
-	@Override
-	public void processInstruction(ExecutionContext ec) 
-		throws DMLRuntimeException 
-	{	
-		SparkExecutionContext sec = (SparkExecutionContext)ec;
-	
-		//get input rdd handle
-		JavaPairRDD<MatrixIndexes,MatrixBlock> in1 = sec.getBinaryBlockRDDHandleForVariable( input1.getName() );
-		JavaPairRDD<MatrixIndexes,MatrixBlock> in2 = null;
-		JavaPairRDD<MatrixIndexes,MatrixBlock> in3 = null;
-		double scalar_input2 = -1, scalar_input3 = -1;
-		
-		Ternary.OperationTypes ctableOp = Ternary.findCtableOperationByInputDataTypes(
-				input1.getDataType(), input2.getDataType(), input3.getDataType());
-		ctableOp = _isExpand ? Ternary.OperationTypes.CTABLE_EXPAND_SCALAR_WEIGHT : ctableOp;
-		
-		MatrixCharacteristics mc1 = sec.getMatrixCharacteristics(input1.getName());
-		MatrixCharacteristics mcOut = sec.getMatrixCharacteristics(output.getName());
-		
-		// First get the block sizes and then set them as -1 to allow for binary cell reblock
-		int brlen = mc1.getRowsPerBlock();
-		int bclen = mc1.getColsPerBlock();
-		
-		JavaPairRDD<MatrixIndexes, ArrayList<MatrixBlock>> inputMBs = null;
-		JavaPairRDD<MatrixIndexes, CTableMap> ctables = null;
-		JavaPairRDD<MatrixIndexes, Double> bincellsNoFilter = null;
-		boolean setLineage2 = false;
-		boolean setLineage3 = false;
-		switch(ctableOp) {
-			case CTABLE_TRANSFORM: //(VECTOR)
-				// F=ctable(A,B,W) 
-				in2 = sec.getBinaryBlockRDDHandleForVariable( input2.getName() );
-				in3 = sec.getBinaryBlockRDDHandleForVariable( input3.getName() );
-				setLineage2 = true;
-				setLineage3 = true;
-				
-				inputMBs = in1.cogroup(in2).cogroup(in3)
-							.mapToPair(new MapThreeMBIterableIntoAL());
-				
-				ctables = inputMBs.mapToPair(new PerformCTableMapSideOperation(ctableOp, scalar_input2, 
-							scalar_input3, this.instString, (SimpleOperator)_optr, _ignoreZeros));
-				break;
-			
-				
-			case CTABLE_EXPAND_SCALAR_WEIGHT: //(VECTOR)
-				// F = ctable(seq,A) or F = ctable(seq,B,1)
-				scalar_input3 = sec.getScalarInput(input3.getName(), input3.getValueType(), input3.isLiteral()).getDoubleValue();
-				if(scalar_input3 == 1) {
-					in2 = sec.getBinaryBlockRDDHandleForVariable( input2.getName() );
-					setLineage2 = true;
-					bincellsNoFilter = in2.flatMapToPair(new ExpandScalarCtableOperation(brlen));
-					break;
-				}
-			case CTABLE_TRANSFORM_SCALAR_WEIGHT: //(VECTOR/MATRIX)
-				// F = ctable(A,B) or F = ctable(A,B,1)
-				in2 = sec.getBinaryBlockRDDHandleForVariable( input2.getName() );
-				setLineage2 = true;
-
-				scalar_input3 = sec.getScalarInput(input3.getName(), input3.getValueType(), input3.isLiteral()).getDoubleValue();
-				inputMBs = in1.cogroup(in2).mapToPair(new MapTwoMBIterableIntoAL());
-				
-				ctables = inputMBs.mapToPair(new PerformCTableMapSideOperation(ctableOp, scalar_input2, 
-						scalar_input3, this.instString, (SimpleOperator)_optr, _ignoreZeros));
-				break;
-				
-			case CTABLE_TRANSFORM_HISTOGRAM: //(VECTOR)
-				// F=ctable(A,1) or F = ctable(A,1,1)
-				scalar_input2 = sec.getScalarInput(input2.getName(), input2.getValueType(), input2.isLiteral()).getDoubleValue();
-				scalar_input3 = sec.getScalarInput(input3.getName(), input3.getValueType(), input3.isLiteral()).getDoubleValue();
-				inputMBs = in1.mapToPair(new MapMBIntoAL());
-				
-				ctables = inputMBs.mapToPair(new PerformCTableMapSideOperation(ctableOp, scalar_input2, 
-						scalar_input3, this.instString, (SimpleOperator)_optr, _ignoreZeros));
-				break;
-				
-			case CTABLE_TRANSFORM_WEIGHTED_HISTOGRAM: //(VECTOR)
-				// F=ctable(A,1,W)
-				in3 = sec.getBinaryBlockRDDHandleForVariable( input3.getName() );
-				setLineage3 = true;
-				
-				scalar_input2 = sec.getScalarInput(input2.getName(), input2.getValueType(), input2.isLiteral()).getDoubleValue();
-				inputMBs = in1.cogroup(in3).mapToPair(new MapTwoMBIterableIntoAL());
-				
-				ctables = inputMBs.mapToPair(new PerformCTableMapSideOperation(ctableOp, scalar_input2, 
-						scalar_input3, this.instString, (SimpleOperator)_optr, _ignoreZeros));
-				break;
-			
-			default:
-				throw new DMLRuntimeException("Encountered an invalid ctable operation ("+ctableOp+") while executing instruction: " + this.toString());
-		}
-		
-		// Now perform aggregation on ctables to get binaryCells 
-		if(bincellsNoFilter == null && ctables != null) {
-			bincellsNoFilter =  
-					ctables.values()
-					.flatMapToPair(new ExtractBinaryCellsFromCTable());
-			bincellsNoFilter = RDDAggregateUtils.sumCellsByKeyStable(bincellsNoFilter);
-		}
-		else if(!(bincellsNoFilter != null && ctables == null)) {
-			throw new DMLRuntimeException("Incorrect ctable operation");
-		}
-		
-		// handle known/unknown dimensions
-		long outputDim1 = (_dim1Literal ? (long) Double.parseDouble(_outDim1) : (sec.getScalarInput(_outDim1, ValueType.DOUBLE, false)).getLongValue());
-		long outputDim2 = (_dim2Literal ? (long) Double.parseDouble(_outDim2) : (sec.getScalarInput(_outDim2, ValueType.DOUBLE, false)).getLongValue());
-		MatrixCharacteristics mcBinaryCells = null;
-		boolean findDimensions = (outputDim1 == -1 && outputDim2 == -1); 
-		
-		if( !findDimensions ) {
-			if((outputDim1 == -1 && outputDim2 != -1) || (outputDim1 != -1 && outputDim2 == -1))
-				throw new DMLRuntimeException("Incorrect output dimensions passed to TernarySPInstruction:" + outputDim1 + " " + outputDim2);
-			else 
-				mcBinaryCells = new MatrixCharacteristics(outputDim1, outputDim2, brlen, bclen);	
-			
-			// filtering according to given dimensions
-			bincellsNoFilter = bincellsNoFilter
-					.filter(new FilterCells(mcBinaryCells.getRows(), mcBinaryCells.getCols()));
-		}
-		
-		// convert double values to matrix cell
-		JavaPairRDD<MatrixIndexes, MatrixCell> binaryCells = bincellsNoFilter
-				.mapToPair(new ConvertToBinaryCell());
-		
-		// find dimensions if necessary (w/ cache for reblock)
-		if( findDimensions ) {						
-			binaryCells = SparkUtils.cacheBinaryCellRDD(binaryCells);
-			mcBinaryCells = SparkUtils.computeMatrixCharacteristics(binaryCells);
-		}
-		
-		//store output rdd handle
-		sec.setRDDHandleForVariable(output.getName(), binaryCells);
-		mcOut.set(mcBinaryCells);
-		// Since we are outputing binary cells, we set block sizes = -1
-		mcOut.setRowsPerBlock(-1); mcOut.setColsPerBlock(-1);
-		sec.addLineageRDD(output.getName(), input1.getName());
-		if(setLineage2)
-			sec.addLineageRDD(output.getName(), input2.getName());
-		if(setLineage3)
-			sec.addLineageRDD(output.getName(), input3.getName());
-	}	
-
-	private static class ExpandScalarCtableOperation implements PairFlatMapFunction<Tuple2<MatrixIndexes,MatrixBlock>, MatrixIndexes, Double> 
-	{
-		private static final long serialVersionUID = -12552669148928288L;
-	
-		private int _brlen;
-		
-		public ExpandScalarCtableOperation(int brlen) {
-			_brlen = brlen;
-		}
-
-		@Override
-		public Iterator<Tuple2<MatrixIndexes, Double>> call(Tuple2<MatrixIndexes, MatrixBlock> arg0) 
-			throws Exception 
-		{
-			MatrixIndexes ix = arg0._1();
-			MatrixBlock mb = arg0._2(); //col-vector
-			
-			//create an output cell per matrix block row (aligned w/ original source position)
-			ArrayList<Tuple2<MatrixIndexes, Double>> retVal = new ArrayList<>();
-			CTable ctab = CTable.getCTableFnObject();
-			for( int i=0; i<mb.getNumRows(); i++ )
-			{
-				//compute global target indexes (via ctable obj for error handling consistency)
-				long row = UtilFunctions.computeCellIndex(ix.getRowIndex(), _brlen, i);
-				double v2 = mb.quickGetValue(i, 0);
-				Pair<MatrixIndexes,Double> p = ctab.execute(row, v2, 1.0);
-				
-				//indirect construction over pair to avoid tuple2 dependency in general ctable obj
-				if( p.getKey().getRowIndex() >= 1 ) //filter rejected entries
-					retVal.add(new Tuple2<>(p.getKey(), p.getValue()));
-			}
-			
-			return retVal.iterator();
-		}
-	}
-	
-	private static class MapTwoMBIterableIntoAL implements PairFunction<Tuple2<MatrixIndexes,Tuple2<Iterable<MatrixBlock>,Iterable<MatrixBlock>>>, MatrixIndexes, ArrayList<MatrixBlock>> {
-
-		private static final long serialVersionUID = 271459913267735850L;
-
-		private static MatrixBlock extractBlock(Iterable<MatrixBlock> blks, MatrixBlock retVal) throws Exception {
-			for(MatrixBlock blk1 : blks) {
-				if(retVal != null) {
-					throw new Exception("ERROR: More than 1 matrixblock found for one of the inputs at a given index");
-				}
-				retVal = blk1;
-			}
-			if(retVal == null) {
-				throw new Exception("ERROR: No matrixblock found for one of the inputs at a given index");
-			}
-			return retVal;
-		}
-		
-		@Override
-		public Tuple2<MatrixIndexes, ArrayList<MatrixBlock>> call(
-				Tuple2<MatrixIndexes, Tuple2<Iterable<MatrixBlock>, Iterable<MatrixBlock>>> kv)
-				throws Exception {
-			MatrixBlock in1 = null; MatrixBlock in2 = null;
-			in1 = extractBlock(kv._2._1, in1);
-			in2 = extractBlock(kv._2._2, in2);
-			// Now return unflatten AL
-			ArrayList<MatrixBlock> inputs = new ArrayList<>();
-			inputs.add(in1); inputs.add(in2);  
-			return new Tuple2<>(kv._1, inputs);
-		}
-		
-	}
-	
-	private static class MapThreeMBIterableIntoAL implements PairFunction<Tuple2<MatrixIndexes,Tuple2<Iterable<Tuple2<Iterable<MatrixBlock>,Iterable<MatrixBlock>>>,Iterable<MatrixBlock>>>, MatrixIndexes, ArrayList<MatrixBlock>> {
-
-		private static final long serialVersionUID = -4873754507037646974L;
-		
-		private static MatrixBlock extractBlock(Iterable<MatrixBlock> blks, MatrixBlock retVal) throws Exception {
-			for(MatrixBlock blk1 : blks) {
-				if(retVal != null) {
-					throw new Exception("ERROR: More than 1 matrixblock found for one of the inputs at a given index");
-				}
-				retVal = blk1;
-			}
-			if(retVal == null) {
-				throw new Exception("ERROR: No matrixblock found for one of the inputs at a given index");
-			}
-			return retVal;
-		}
-
-		@Override
-		public Tuple2<MatrixIndexes, ArrayList<MatrixBlock>> call(
-				Tuple2<MatrixIndexes, Tuple2<Iterable<Tuple2<Iterable<MatrixBlock>, Iterable<MatrixBlock>>>, Iterable<MatrixBlock>>> kv)
-				throws Exception {
-			MatrixBlock in1 = null; MatrixBlock in2 = null; MatrixBlock in3 = null;
-			
-			for(Tuple2<Iterable<MatrixBlock>, Iterable<MatrixBlock>> blks : kv._2._1) {
-				in1 = extractBlock(blks._1, in1);
-				in2 = extractBlock(blks._2, in2);
-			}
-			in3 = extractBlock(kv._2._2, in3);
-			
-			// Now return unflatten AL
-			ArrayList<MatrixBlock> inputs = new ArrayList<>();
-			inputs.add(in1); inputs.add(in2); inputs.add(in3);  
-			return new Tuple2<>(kv._1, inputs);
-		}
-		
-	}
-	
-	private static class PerformCTableMapSideOperation implements PairFunction<Tuple2<MatrixIndexes,ArrayList<MatrixBlock>>, MatrixIndexes, CTableMap> {
-
-		private static final long serialVersionUID = 5348127596473232337L;
-
-		Ternary.OperationTypes ctableOp;
-		double scalar_input2; double scalar_input3;
-		String instString;
-		Operator optr;
-		boolean ignoreZeros;
-		
-		public PerformCTableMapSideOperation(Ternary.OperationTypes ctableOp, double scalar_input2, double scalar_input3, String instString, Operator optr, boolean ignoreZeros) {
-			this.ctableOp = ctableOp;
-			this.scalar_input2 = scalar_input2;
-			this.scalar_input3 = scalar_input3;
-			this.instString = instString;
-			this.optr = optr;
-			this.ignoreZeros = ignoreZeros;
-		}
-		
-		private static void expectedALSize(int length, ArrayList<MatrixBlock> al) throws Exception {
-			if(al.size() != length) {
-				throw new Exception("Expected arraylist of size:" + length + ", but found " + al.size());
-			}
-		}
-		
-		@Override
-		public Tuple2<MatrixIndexes, CTableMap> call(
-				Tuple2<MatrixIndexes, ArrayList<MatrixBlock>> kv) throws Exception {
-			CTableMap ctableResult = new CTableMap();
-			MatrixBlock ctableResultBlock = null;
-			
-			IndexedMatrixValue in1, in2, in3 = null;
-			in1 = new IndexedMatrixValue(kv._1, kv._2.get(0));
-			MatrixBlock matBlock1 = kv._2.get(0);
-			
-			switch( ctableOp )
-			{
-				case CTABLE_TRANSFORM: {
-					in2 = new IndexedMatrixValue(kv._1, kv._2.get(1));
-					in3 = new IndexedMatrixValue(kv._1, kv._2.get(2));
-					expectedALSize(3, kv._2);
-					
-					if(in1==null || in2==null || in3 == null )
-						break;	
-					else
-						OperationsOnMatrixValues.performTernary(in1.getIndexes(), in1.getValue(), in2.getIndexes(), in2.getValue(), 
-                                in3.getIndexes(), in3.getValue(), ctableResult, ctableResultBlock, optr);
-					break;
-				}
-				case CTABLE_TRANSFORM_SCALAR_WEIGHT: 
-				case CTABLE_EXPAND_SCALAR_WEIGHT:
-				{
-					// 3rd input is a scalar
-					in2 = new IndexedMatrixValue(kv._1, kv._2.get(1));
-					expectedALSize(2, kv._2);
-					if(in1==null || in2==null )
-						break;
-					else
-						matBlock1.ternaryOperations((SimpleOperator)optr, kv._2.get(1), scalar_input3, ignoreZeros, ctableResult, ctableResultBlock);
-						break;
-				}
-				case CTABLE_TRANSFORM_HISTOGRAM: {
-					expectedALSize(1, kv._2);
-					OperationsOnMatrixValues.performTernary(in1.getIndexes(), in1.getValue(), scalar_input2, 
-							scalar_input3, ctableResult, ctableResultBlock, optr);
-					break;
-				}
-				case CTABLE_TRANSFORM_WEIGHTED_HISTOGRAM: {
-					// 2nd and 3rd inputs are scalars
-					expectedALSize(2, kv._2);
-					in3 = new IndexedMatrixValue(kv._1, kv._2.get(1)); // Note: kv._2.get(1), not kv._2.get(2)
-					
-					if(in1==null || in3==null)
-						break;
-					else
-						OperationsOnMatrixValues.performTernary(in1.getIndexes(), in1.getValue(), scalar_input2, 
-								in3.getIndexes(), in3.getValue(), ctableResult, ctableResultBlock, optr);		
-					break;
-				}
-				default:
-					throw new DMLRuntimeException("Unrecognized opcode in Tertiary Instruction: " + instString);
-			}
-			return new Tuple2<>(kv._1, ctableResult);
-		}
-		
-	}
-	
-	private static class MapMBIntoAL implements PairFunction<Tuple2<MatrixIndexes, MatrixBlock>, MatrixIndexes, ArrayList<MatrixBlock>> {
-
-		private static final long serialVersionUID = 2068398913653350125L;
-
-		@Override
-		public Tuple2<MatrixIndexes, ArrayList<MatrixBlock>> call(
-				Tuple2<MatrixIndexes, MatrixBlock> kv) throws Exception {
-			ArrayList<MatrixBlock> retVal = new ArrayList<>();
-			retVal.add(kv._2);
-			return new Tuple2<>(kv._1, retVal);
-		}
-		
-	}
-	
-	private static class ExtractBinaryCellsFromCTable implements PairFlatMapFunction<CTableMap, MatrixIndexes, Double> {
-
-		private static final long serialVersionUID = -5933677686766674444L;
-		
-		@Override
-		public Iterator<Tuple2<MatrixIndexes, Double>> call(CTableMap ctableMap)
-				throws Exception {
-			ArrayList<Tuple2<MatrixIndexes, Double>> retVal = new ArrayList<>();
-			Iterator<ADoubleEntry> iter = ctableMap.getIterator();
-			while( iter.hasNext() ) {
-				ADoubleEntry ijv = iter.next();
-				long i = ijv.getKey1();
-				long j =  ijv.getKey2();
-				double v =  ijv.value;
-				retVal.add(new Tuple2<>(new MatrixIndexes(i, j), v));
-			}
-			return retVal.iterator();
-		}
-		
-	}
-	
-	private static class ConvertToBinaryCell implements PairFunction<Tuple2<MatrixIndexes,Double>, MatrixIndexes, MatrixCell> {
-
-		private static final long serialVersionUID = 7481186480851982800L;
-		
-		@Override
-		public Tuple2<MatrixIndexes, MatrixCell> call(
-				Tuple2<MatrixIndexes, Double> kv) throws Exception {
-			
-			MatrixCell cell = new MatrixCell(kv._2().doubleValue());
-			return new Tuple2<>(kv._1(), cell);
-		}
-		
-	}
-	
-	private static class FilterCells implements Function<Tuple2<MatrixIndexes,Double>, Boolean> {
-		private static final long serialVersionUID = 108448577697623247L;
-
-		long rlen; long clen;
-		public FilterCells(long rlen, long clen) {
-			this.rlen = rlen;
-			this.clen = clen;
-		}
-		
-		@Override
-		public Boolean call(Tuple2<MatrixIndexes, Double> kv) throws Exception {
-			if(kv._1.getRowIndex() <= 0 || kv._1.getColumnIndex() <= 0) {
-				throw new Exception("Incorrect cell values in TernarySPInstruction:" + kv._1);
-			}
-			if(kv._1.getRowIndex() <= rlen && kv._1.getColumnIndex() <= clen) {
-				return true;
-			}
-			return false;
-		}
-		
-	}
-}

http://git-wip-us.apache.org/repos/asf/systemml/blob/57438103/src/main/java/org/apache/sysml/runtime/instructions/spark/ZipmmSPInstruction.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/sysml/runtime/instructions/spark/ZipmmSPInstruction.java b/src/main/java/org/apache/sysml/runtime/instructions/spark/ZipmmSPInstruction.java
index 491934d..d31dce5 100644
--- a/src/main/java/org/apache/sysml/runtime/instructions/spark/ZipmmSPInstruction.java
+++ b/src/main/java/org/apache/sysml/runtime/instructions/spark/ZipmmSPInstruction.java
@@ -129,7 +129,7 @@ public class ZipmmSPInstruction extends BinarySPInstruction {
 			MatrixBlock tmp = (MatrixBlock)in2.reorgOperations(_rop, new MatrixBlock(), 0, 0, 0);
 				
 			//core matrix multiplication (for t(y)%*%X or t(X)%*%y)
-			return (MatrixBlock)tmp.aggregateBinaryOperations(tmp, in1, new MatrixBlock(), _abop);
+			return tmp.aggregateBinaryOperations(tmp, in1, new MatrixBlock(), _abop);
 		}
 	}
 }

http://git-wip-us.apache.org/repos/asf/systemml/blob/57438103/src/main/java/org/apache/sysml/runtime/matrix/MatrixCharacteristics.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/sysml/runtime/matrix/MatrixCharacteristics.java b/src/main/java/org/apache/sysml/runtime/matrix/MatrixCharacteristics.java
index a0e81ed..b447b23 100644
--- a/src/main/java/org/apache/sysml/runtime/matrix/MatrixCharacteristics.java
+++ b/src/main/java/org/apache/sysml/runtime/matrix/MatrixCharacteristics.java
@@ -56,7 +56,7 @@ import org.apache.sysml.runtime.instructions.mr.ReorgInstruction;
 import org.apache.sysml.runtime.instructions.mr.ReplicateInstruction;
 import org.apache.sysml.runtime.instructions.mr.ScalarInstruction;
 import org.apache.sysml.runtime.instructions.mr.SeqInstruction;
-import org.apache.sysml.runtime.instructions.mr.TernaryInstruction;
+import org.apache.sysml.runtime.instructions.mr.CtableInstruction;
 import org.apache.sysml.runtime.instructions.mr.UaggOuterChainInstruction;
 import org.apache.sysml.runtime.instructions.mr.UnaryInstruction;
 import org.apache.sysml.runtime.instructions.mr.UnaryMRInstructionBase;
@@ -377,7 +377,7 @@ public class MatrixCharacteristics implements Serializable
 			}
 		}
 		else if (ins instanceof CombineTernaryInstruction ) {
-			TernaryInstruction realIns=(TernaryInstruction)ins;
+			CtableInstruction realIns=(CtableInstruction)ins;
 			dimOut.set(dims.get(realIns.input1));
 		}
 		else if (ins instanceof CombineUnaryInstruction ) {
@@ -393,8 +393,8 @@ public class MatrixCharacteristics implements Serializable
 			MatrixCharacteristics dimIn = dims.get(realIns.input);
 			realIns.computeOutputCharacteristics(dimIn, dimOut);
 		}
-		else if (ins instanceof TernaryInstruction) {
-			TernaryInstruction realIns = (TernaryInstruction)ins;
+		else if (ins instanceof CtableInstruction) {
+			CtableInstruction realIns = (CtableInstruction)ins;
 			MatrixCharacteristics in_dim=dims.get(realIns.input1);
 			dimOut.set(realIns.getOutputDim1(), realIns.getOutputDim2(), in_dim.numRowsPerBlock, in_dim.numColumnsPerBlock);
 		}

http://git-wip-us.apache.org/repos/asf/systemml/blob/57438103/src/main/java/org/apache/sysml/runtime/matrix/data/CM_N_COVCell.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/sysml/runtime/matrix/data/CM_N_COVCell.java b/src/main/java/org/apache/sysml/runtime/matrix/data/CM_N_COVCell.java
index b74ff10..a01d1bb 100644
--- a/src/main/java/org/apache/sysml/runtime/matrix/data/CM_N_COVCell.java
+++ b/src/main/java/org/apache/sysml/runtime/matrix/data/CM_N_COVCell.java
@@ -30,12 +30,10 @@ import org.apache.hadoop.io.WritableComparable;
 import org.apache.sysml.runtime.DMLRuntimeException;
 import org.apache.sysml.runtime.instructions.cp.CM_COV_Object;
 import org.apache.sysml.runtime.matrix.mapred.IndexedMatrixValue;
-import org.apache.sysml.runtime.matrix.operators.AggregateBinaryOperator;
 import org.apache.sysml.runtime.matrix.operators.AggregateOperator;
 import org.apache.sysml.runtime.matrix.operators.AggregateUnaryOperator;
 import org.apache.sysml.runtime.matrix.operators.BinaryOperator;
 import org.apache.sysml.runtime.matrix.operators.Operator;
-import org.apache.sysml.runtime.matrix.operators.QuaternaryOperator;
 import org.apache.sysml.runtime.matrix.operators.ReorgOperator;
 import org.apache.sysml.runtime.matrix.operators.ScalarOperator;
 import org.apache.sysml.runtime.matrix.operators.UnaryOperator;
@@ -50,42 +48,35 @@ public class CM_N_COVCell extends MatrixValue implements WritableComparable
 	public String toString() {
 		return cm.toString();
 	}
-
-	@Override
-	public MatrixValue aggregateBinaryOperations(MatrixValue m1Value,
-			MatrixValue m2Value, MatrixValue result, AggregateBinaryOperator op)
-			throws DMLRuntimeException {
-		throw new DMLRuntimeException("operation not supported fro WeightedCell");
-	}
-
+	
 	@Override
 	public MatrixValue aggregateUnaryOperations(AggregateUnaryOperator op,
 			MatrixValue result, int brlen, int bclen, MatrixIndexes indexesIn)
 			throws DMLRuntimeException {
-		throw new DMLRuntimeException("operation not supported fro WeightedCell");
+		throw new DMLRuntimeException("operation not supported for CM_N_COVCell");
 	}
 
 	@Override
 	public MatrixValue binaryOperations(BinaryOperator op,
 			MatrixValue thatValue, MatrixValue result)
 			throws DMLRuntimeException {
-		throw new DMLRuntimeException("operation not supported fro WeightedCell");
+		throw new DMLRuntimeException("operation not supported for CM_N_COVCell");
 	}
 
 	@Override
 	public void binaryOperationsInPlace(BinaryOperator op, MatrixValue thatValue)
 			throws DMLRuntimeException {
-		throw new DMLRuntimeException("operation not supported fro WeightedCell");		
+		throw new DMLRuntimeException("operation not supported for CM_N_COVCell");
 	}
 
 	@Override
 	public void copy(MatrixValue that, boolean sp) {
-		throw new RuntimeException("operation not supported fro WeightedCell");
+		throw new RuntimeException("operation not supported for CM_N_COVCell");
 	}
 
 	@Override
 	public void copy(MatrixValue that) {
-		throw new RuntimeException("operation not supported fro WeightedCell");
+		throw new RuntimeException("operation not supported for CM_N_COVCell");
 	}
 
 	@Override
@@ -105,21 +96,21 @@ public class CM_N_COVCell extends MatrixValue implements WritableComparable
 
 	@Override
 	public double getValue(int r, int c) {
-		throw new RuntimeException("operation not supported fro WeightedCell");
+		throw new RuntimeException("operation not supported for CM_N_COVCell");
 	}
 
 	@Override
 	public void incrementalAggregate(AggregateOperator aggOp,
 			MatrixValue correction, MatrixValue newWithCorrection)
 			throws DMLRuntimeException {
-		throw new RuntimeException("operation not supported fro WeightedCell");
+		throw new RuntimeException("operation not supported for CM_N_COVCell");
 	}
 	
 	@Override
 	public void incrementalAggregate(AggregateOperator aggOp,
 			MatrixValue newWithCorrection)
 			throws DMLRuntimeException {
-		throw new RuntimeException("operation not supported fro WeightedCell");
+		throw new RuntimeException("operation not supported for CM_N_COVCell");
 	}
 
 	@Override
@@ -136,7 +127,7 @@ public class CM_N_COVCell extends MatrixValue implements WritableComparable
 	public MatrixValue reorgOperations(ReorgOperator op, MatrixValue result,
 			int startRow, int startColumn, int length)
 			throws DMLRuntimeException {
-		throw new RuntimeException("operation not supported fro WeightedCell");
+		throw new RuntimeException("operation not supported for CM_N_COVCell");
 	}
 
 	@Override
@@ -157,18 +148,18 @@ public class CM_N_COVCell extends MatrixValue implements WritableComparable
 	@Override
 	public MatrixValue scalarOperations(ScalarOperator op, MatrixValue result)
 			throws DMLRuntimeException {
-		throw new DMLRuntimeException("operation not supported fro WeightedCell");
+		throw new DMLRuntimeException("operation not supported for CM_N_COVCell");
 	}
 
 	@Override
 	public void setValue(int r, int c, double v) {
-		throw new RuntimeException("operation not supported fro WeightedCell");
+		throw new RuntimeException("operation not supported for CM_N_COVCell");
 	}
 
 	@Override
 	public MatrixValue unaryOperations(UnaryOperator op, MatrixValue result)
 			throws DMLRuntimeException {
-		throw new DMLRuntimeException("operation not supported fro WeightedCell");
+		throw new DMLRuntimeException("operation not supported for CM_N_COVCell");
 	}
 
 	@Override
@@ -241,65 +232,56 @@ public class CM_N_COVCell extends MatrixValue implements WritableComparable
 	@Override
 	public MatrixValue zeroOutOperations(MatrixValue result, IndexRange range, boolean complementary)
 			throws DMLRuntimeException {
-		throw new DMLRuntimeException("operation not supported fro WeightedCell");
+		throw new DMLRuntimeException("operation not supported for CM_N_COVCell");
 	}
 
 	@Override
-	public void ternaryOperations(Operator op, MatrixValue that,
+	public void ctableOperations(Operator op, MatrixValue that,
 			MatrixValue that2, CTableMap resultMap, MatrixBlock resultBlock)
 			throws DMLRuntimeException {
-		throw new DMLRuntimeException("operation not supported fro WeightedCell");
-		
+		throw new DMLRuntimeException("operation not supported for CM_N_COVCell");
 	}
 
 	@Override
-	public void ternaryOperations(Operator op, MatrixValue that,
+	public void ctableOperations(Operator op, MatrixValue that,
 			double scalarThat2, boolean ignoreZeros, CTableMap resultMap, MatrixBlock resultBlock)
 			throws DMLRuntimeException {
-		throw new DMLRuntimeException("operation not supported fro WeightedCell");
+		throw new DMLRuntimeException("operation not supported for CM_N_COVCell");
 	}
 
 	@Override
-	public void ternaryOperations(Operator op, double scalarThat,
+	public void ctableOperations(Operator op, double scalarThat,
 			double scalarThat2, CTableMap resultMap, MatrixBlock resultBlock)
 			throws DMLRuntimeException {
-		throw new DMLRuntimeException("operation not supported fro WeightedCell");
+		throw new DMLRuntimeException("operation not supported for CM_N_COVCell");
 	}
 	
 	@Override
-	public void ternaryOperations(Operator op, MatrixIndexes ix1, double scalarThat, boolean left, int brlen,
+	public void ctableOperations(Operator op, MatrixIndexes ix1, double scalarThat, boolean left, int brlen,
 			CTableMap resultMap, MatrixBlock resultBlock)
 			throws DMLRuntimeException {
-		throw new DMLRuntimeException("operation not supported fro WeightedCell");
+		throw new DMLRuntimeException("operation not supported for CM_N_COVCell");
 	}
 
 	@Override
-	public void ternaryOperations(Operator op, double scalarThat,
+	public void ctableOperations(Operator op, double scalarThat,
 			MatrixValue that2, CTableMap resultMap, MatrixBlock resultBlock)
 			throws DMLRuntimeException {
-		throw new DMLRuntimeException("operation not supported fro WeightedCell");
-	}
-	
-	@Override
-	public MatrixValue quaternaryOperations(QuaternaryOperator qop, MatrixValue um, MatrixValue vm, MatrixValue wm, MatrixValue out)
-		throws DMLRuntimeException
-	{
-		throw new DMLRuntimeException("operation not supported fro WeightedCell");
+		throw new DMLRuntimeException("operation not supported for CM_N_COVCell");
 	}
-		
 
 	@Override
 	public void sliceOperations(ArrayList<IndexedMatrixValue> outlist,
 			IndexRange range, int rowCut, int colCut, int blockRowFactor,
 			int blockColFactor, int boundaryRlen, int boundaryClen)
 			throws DMLRuntimeException {
-		throw new DMLRuntimeException("operation not supported fro WeightedCell");		
+		throw new DMLRuntimeException("operation not supported for CM_N_COVCell");
 	}
 	
 	@Override
 	public MatrixValue replaceOperations(MatrixValue result, double pattern, double replacement)
 			throws DMLRuntimeException {
-		throw new DMLRuntimeException("operation not supported fro WeightedCell");		
+		throw new DMLRuntimeException("operation not supported for CM_N_COVCell");
 	}
 
 	@Override
@@ -307,21 +289,13 @@ public class CM_N_COVCell extends MatrixValue implements WritableComparable
 			MatrixValue result, int blockingFactorRow, int blockingFactorCol,
 			MatrixIndexes indexesIn, boolean inCP)
 			throws DMLRuntimeException {
-		throw new DMLRuntimeException("operation not supported fro WeightedCell");
-	}
-
-	@Override
-	public MatrixValue aggregateBinaryOperations(MatrixIndexes m1Index,
-			MatrixValue m1Value, MatrixIndexes m2Index, MatrixValue m2Value,
-			MatrixValue result, AggregateBinaryOperator op)
-			throws DMLRuntimeException {
-		throw new DMLRuntimeException("operation not supported fro WeightedCell");
+		throw new DMLRuntimeException("operation not supported for CM_N_COVCell");
 	}
 
 	@Override
 	public void appendOperations(MatrixValue valueIn2, ArrayList<IndexedMatrixValue> outlist,
 			int blockRowFactor, int blockColFactor, boolean cbind, boolean m2IsLast, int nextNCol)
 	throws DMLRuntimeException   {
-		throw new DMLRuntimeException("operation not supported fro WeightedCell");
+		throw new DMLRuntimeException("operation not supported for CM_N_COVCell");
 	}
 }

http://git-wip-us.apache.org/repos/asf/systemml/blob/57438103/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 08f5c85..098427f 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
@@ -4625,21 +4625,16 @@ public class MatrixBlock extends MatrixValue implements CacheBlock, Externalizab
 		return sum_wt;
 	}
 	
-	@Override
-	public MatrixValue aggregateBinaryOperations(MatrixIndexes m1Index, MatrixValue m1Value, MatrixIndexes m2Index, MatrixValue m2Value, 
-			MatrixValue result, AggregateBinaryOperator op ) throws DMLRuntimeException
+	public MatrixBlock aggregateBinaryOperations(MatrixIndexes m1Index, MatrixBlock m1, MatrixIndexes m2Index, MatrixBlock m2, 
+		MatrixBlock ret, AggregateBinaryOperator op ) throws DMLRuntimeException
 	{
-		return aggregateBinaryOperations(m1Value, m2Value, result, op);
+		return aggregateBinaryOperations(m1, m2, ret, op);
 	}
 
-	@Override
-	public MatrixValue aggregateBinaryOperations(MatrixValue m1Value, MatrixValue m2Value, MatrixValue result, AggregateBinaryOperator op) 
+	public MatrixBlock aggregateBinaryOperations(MatrixBlock m1, MatrixBlock m2, MatrixBlock ret, AggregateBinaryOperator op) 
 		throws DMLRuntimeException
 	{
 		//check input types, dimensions, configuration
-		MatrixBlock m1 = checkType(m1Value);
-		MatrixBlock m2 = checkType(m2Value);
-		MatrixBlock ret = checkType(result);
 		if( m1.clen != m2.rlen ) {
 			throw new RuntimeException("Dimensions do not match for matrix multiplication ("+m1.clen+"!="+m2.rlen+").");
 		}
@@ -4923,7 +4918,7 @@ public class MatrixBlock extends MatrixValue implements CacheBlock, Externalizab
 	 * (i3,j3,w)  from input3 (that2) 
 	 */
 	@Override
-	public void ternaryOperations(Operator op, double scalarThat,
+	public void ctableOperations(Operator op, double scalarThat,
 			MatrixValue that2Val, CTableMap resultMap, MatrixBlock resultBlock)
 		throws DMLRuntimeException 
 	{
@@ -4954,7 +4949,7 @@ public class MatrixBlock extends MatrixValue implements CacheBlock, Externalizab
 	 * (w)  from scalar_input3 (scalarThat2)
 	 */
 	@Override
-	public void ternaryOperations(Operator op, double scalarThat,
+	public void ctableOperations(Operator op, double scalarThat,
 			double scalarThat2, CTableMap resultMap, MatrixBlock resultBlock)
 			throws DMLRuntimeException 
 	{
@@ -4982,7 +4977,7 @@ public class MatrixBlock extends MatrixValue implements CacheBlock, Externalizab
 	 * 
 	 */
 	@Override
-	public void ternaryOperations(Operator op, MatrixIndexes ix1, double scalarThat,
+	public void ctableOperations(Operator op, MatrixIndexes ix1, double scalarThat,
 			boolean left, int brlen, CTableMap resultMap, MatrixBlock resultBlock)
 		throws DMLRuntimeException 
 	{	
@@ -5018,7 +5013,7 @@ public class MatrixBlock extends MatrixValue implements CacheBlock, Externalizab
 	 * we can also use a sparse-safe implementation
 	 */
 	@Override
-	public void ternaryOperations(Operator op, MatrixValue thatVal, double scalarThat2, boolean ignoreZeros,
+	public void ctableOperations(Operator op, MatrixValue thatVal, double scalarThat2, boolean ignoreZeros,
 			     CTableMap resultMap, MatrixBlock resultBlock)
 			throws DMLRuntimeException 
 	{	
@@ -5082,7 +5077,7 @@ public class MatrixBlock extends MatrixValue implements CacheBlock, Externalizab
 	 * @param resultBlock result matrix block
 	 * @throws DMLRuntimeException if DMLRuntimeException occurs
 	 */
-	public void ternaryOperations(Operator op, MatrixValue thatMatrix, double thatScalar, MatrixBlock resultBlock)
+	public void ctableOperations(Operator op, MatrixValue thatMatrix, double thatScalar, MatrixBlock resultBlock)
 			throws DMLRuntimeException 
 	{	
 		MatrixBlock that = checkType(thatMatrix);
@@ -5118,14 +5113,14 @@ public class MatrixBlock extends MatrixValue implements CacheBlock, Externalizab
 	 * @param resultMap table map
 	 * @throws DMLRuntimeException if DMLRuntimeException occurs
 	 */
-	public void ternaryOperations(Operator op, MatrixValue thatVal, MatrixValue that2Val, CTableMap resultMap)
+	public void ctableOperations(Operator op, MatrixValue thatVal, MatrixValue that2Val, CTableMap resultMap)
 		throws DMLRuntimeException 
 	{
-		ternaryOperations(op, thatVal, that2Val, resultMap, null);
+		ctableOperations(op, thatVal, that2Val, resultMap, null);
 	}
 		
 	@Override
-	public void ternaryOperations(Operator op, MatrixValue thatVal, MatrixValue that2Val, CTableMap resultMap, MatrixBlock resultBlock)
+	public void ctableOperations(Operator op, MatrixValue thatVal, MatrixValue that2Val, CTableMap resultMap, MatrixBlock resultBlock)
 		throws DMLRuntimeException
 	{	
 		MatrixBlock that = checkType(thatVal);
@@ -5159,26 +5154,23 @@ public class MatrixBlock extends MatrixValue implements CacheBlock, Externalizab
 		}
 	}
 	
-	@Override
-	public MatrixValue quaternaryOperations(QuaternaryOperator qop, MatrixValue um, MatrixValue vm, MatrixValue wm, MatrixValue out)
+	public MatrixBlock quaternaryOperations(QuaternaryOperator qop, MatrixBlock um, MatrixBlock vm, MatrixBlock wm, MatrixBlock out)
 		throws DMLRuntimeException
 	{
 		return quaternaryOperations(qop, um, vm, wm, out, 1);
 	}
 
-	public MatrixValue quaternaryOperations(QuaternaryOperator qop, MatrixValue um, MatrixValue vm, MatrixValue wm, MatrixValue out, int k)
+	public MatrixBlock quaternaryOperations(QuaternaryOperator qop, MatrixBlock U, MatrixBlock V, MatrixBlock wm, MatrixBlock out, int k)
 		throws DMLRuntimeException
 	{
 		//check input dimensions
-		if( getNumRows() != um.getNumRows() )
-			throw new DMLRuntimeException("Dimension mismatch rows on quaternary operation: "+getNumRows()+"!="+um.getNumRows());
-		if( getNumColumns() != vm.getNumRows() )
-			throw new DMLRuntimeException("Dimension mismatch columns quaternary operation: "+getNumColumns()+"!="+vm.getNumRows());
+		if( getNumRows() != U.getNumRows() )
+			throw new DMLRuntimeException("Dimension mismatch rows on quaternary operation: "+getNumRows()+"!="+U.getNumRows());
+		if( getNumColumns() != V.getNumRows() )
+			throw new DMLRuntimeException("Dimension mismatch columns quaternary operation: "+getNumColumns()+"!="+V.getNumRows());
 		
 		//check input data types
 		MatrixBlock X = this;
-		MatrixBlock U = checkType(um);
-		MatrixBlock V = checkType(vm);
 		MatrixBlock R = checkType(out);
 		
 		//prepare intermediates and output
@@ -5230,7 +5222,7 @@ public class MatrixBlock extends MatrixValue implements CacheBlock, Externalizab
 			if( k > 1 )
 				LibMatrixMult.matrixMultWuMM(X, U, V, R, qop.wtype5, qop.fn, k);
 			else
-				LibMatrixMult.matrixMultWuMM(X, U, V, R, qop.wtype5, qop.fn);	
+				LibMatrixMult.matrixMultWuMM(X, U, V, R, qop.wtype5, qop.fn);
 		}
 		
 		return R;


[4/5] systemml git commit: [SYSTEMML-2055] New ifelse builtin function, ternary inst framework

Posted by mb...@apache.org.
http://git-wip-us.apache.org/repos/asf/systemml/blob/ce9e42fe/src/main/java/org/apache/sysml/runtime/instructions/spark/TernarySPInstruction.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/sysml/runtime/instructions/spark/TernarySPInstruction.java b/src/main/java/org/apache/sysml/runtime/instructions/spark/TernarySPInstruction.java
new file mode 100644
index 0000000..96fd310
--- /dev/null
+++ b/src/main/java/org/apache/sysml/runtime/instructions/spark/TernarySPInstruction.java
@@ -0,0 +1,200 @@
+/*
+ * 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 java.io.Serializable;
+
+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.InstructionUtils;
+import org.apache.sysml.runtime.instructions.cp.CPOperand;
+import org.apache.sysml.runtime.matrix.MatrixCharacteristics;
+import org.apache.sysml.runtime.matrix.data.MatrixBlock;
+import org.apache.sysml.runtime.matrix.data.MatrixIndexes;
+import org.apache.sysml.runtime.matrix.operators.TernaryOperator;
+
+import scala.Tuple2;
+
+public class TernarySPInstruction extends ComputationSPInstruction {
+	private TernarySPInstruction(TernaryOperator op, CPOperand in1, CPOperand in2, CPOperand in3, CPOperand out,
+			String opcode, String str) throws DMLRuntimeException {
+		super(SPType.Ternary, op, in1, in2, in3, out, opcode, str);
+	}
+
+	public static TernarySPInstruction parseInstruction(String str) throws DMLRuntimeException
+	{
+		String[] parts = InstructionUtils.getInstructionPartsWithValueType(str);
+		String opcode=parts[0];
+		CPOperand operand1 = new CPOperand(parts[1]);
+		CPOperand operand2 = new CPOperand(parts[2]);
+		CPOperand operand3 = new CPOperand(parts[3]);
+		CPOperand outOperand = new CPOperand(parts[4]);
+		TernaryOperator op = InstructionUtils.parseTernaryOperator(opcode);
+		return new TernarySPInstruction(op,operand1, operand2, operand3, outOperand, opcode,str);
+	}
+	
+	@Override
+	public void processInstruction(ExecutionContext ec) 
+		throws DMLRuntimeException
+	{
+		SparkExecutionContext sec = (SparkExecutionContext)ec;
+		JavaPairRDD<MatrixIndexes,MatrixBlock> in1 = !input1.isMatrix() ? null :
+			sec.getBinaryBlockRDDHandleForVariable(input1.getName());
+		JavaPairRDD<MatrixIndexes,MatrixBlock> in2 = !input2.isMatrix() ? null :
+			sec.getBinaryBlockRDDHandleForVariable(input2.getName());
+		JavaPairRDD<MatrixIndexes,MatrixBlock> in3 = !input3.isMatrix() ? null :
+			sec.getBinaryBlockRDDHandleForVariable(input3.getName());
+		MatrixBlock m1 = input1.isMatrix() ? null :
+			new MatrixBlock(ec.getScalarInput(input1).getDoubleValue());
+		MatrixBlock m2 = input2.isMatrix() ? null :
+			new MatrixBlock(ec.getScalarInput(input2).getDoubleValue());
+		MatrixBlock m3 = input3.isMatrix() ? null :
+			new MatrixBlock(ec.getScalarInput(input3).getDoubleValue());
+		
+		TernaryOperator op = (TernaryOperator) _optr;
+		
+		JavaPairRDD<MatrixIndexes,MatrixBlock> out = null;
+		if( input1.isMatrix() && !input2.isMatrix() && !input3.isMatrix() )
+			out = in1.mapValues(new TernaryFunctionMSS(op, m1, m2, m3));
+		else if( !input1.isMatrix() && input2.isMatrix() && !input3.isMatrix() )
+			out = in2.mapValues(new TernaryFunctionSMS(op, m1, m2, m3));
+		else if( !input1.isMatrix() && !input2.isMatrix() && input3.isMatrix() )
+			out = in3.mapValues(new TernaryFunctionSSM(op, m1, m2, m3));
+		else if( input1.isMatrix() && input2.isMatrix() && !input3.isMatrix() )
+			out = in1.join(in2).mapValues(new TernaryFunctionMMS(op, m1, m2, m3));
+		else if( input1.isMatrix() && !input2.isMatrix() && input3.isMatrix() )
+			out = in1.join(in3).mapValues(new TernaryFunctionMSM(op, m1, m2, m3));
+		else if( !input1.isMatrix() && input2.isMatrix() && input3.isMatrix() )
+			out = in2.join(in3).mapValues(new TernaryFunctionSMM(op, m1, m2, m3));
+		else // all matrices
+			out = in1.join(in2).join(in3).mapValues(new TernaryFunctionMMM(op, m1, m2, m3));
+		
+		//set output RDD
+		updateTernaryOutputMatrixCharacteristics(sec);
+		sec.setRDDHandleForVariable(output.getName(), out);
+		if( input1.isMatrix() )
+			sec.addLineageRDD(output.getName(), input1.getName());
+		if( input2.isMatrix() )
+			sec.addLineageRDD(output.getName(), input2.getName());
+		if( input3.isMatrix() )
+			sec.addLineageRDD(output.getName(), input3.getName());
+	}
+	
+	protected void updateTernaryOutputMatrixCharacteristics(SparkExecutionContext sec) 
+		throws DMLRuntimeException
+	{
+		MatrixCharacteristics mcOut = sec.getMatrixCharacteristics(output.getName());
+		for(CPOperand input : new CPOperand[]{input1, input2, input3})
+			if( input.isMatrix() ) {
+				MatrixCharacteristics mc = sec.getMatrixCharacteristics(input.getName());
+				if( mc.dimsKnown() )
+					mcOut.set(mc);
+			}
+	}
+	
+	private static abstract class TernaryFunction implements Serializable {
+		private static final long serialVersionUID = 8345737737972434426L;
+		protected final TernaryOperator _op;
+		protected final MatrixBlock _m1, _m2, _m3;
+		public TernaryFunction(TernaryOperator op, MatrixBlock m1, MatrixBlock m2, MatrixBlock m3) {
+			_op = op; _m1 = m1; _m2 = m2; _m3 = m3;
+		}
+	}
+	
+	private static class TernaryFunctionMSS extends TernaryFunction implements Function<MatrixBlock, MatrixBlock> {
+		private static final long serialVersionUID = 1L;
+		public TernaryFunctionMSS(TernaryOperator op, MatrixBlock m1, MatrixBlock m2, MatrixBlock m3) {
+			super(op, m1, m2, m3);
+		}
+		@Override
+		public MatrixBlock call(MatrixBlock v1) throws Exception {
+			return v1.ternaryOperations(_op, _m2, _m3, new MatrixBlock());
+		}
+	}
+	
+	private static class TernaryFunctionSMS extends TernaryFunction implements Function<MatrixBlock, MatrixBlock> {
+		private static final long serialVersionUID = 1L;
+		public TernaryFunctionSMS(TernaryOperator op, MatrixBlock m1, MatrixBlock m2, MatrixBlock m3) {
+			super(op, m1, m2, m3);
+		}
+		@Override
+		public MatrixBlock call(MatrixBlock v1) throws Exception {
+			return _m1.ternaryOperations(_op, v1, _m3, new MatrixBlock());
+		}
+	}
+	
+	private static class TernaryFunctionSSM extends TernaryFunction implements Function<MatrixBlock, MatrixBlock> {
+		private static final long serialVersionUID = 1L;
+		public TernaryFunctionSSM(TernaryOperator op, MatrixBlock m1, MatrixBlock m2, MatrixBlock m3) {
+			super(op, m1, m2, m3);
+		}
+		@Override
+		public MatrixBlock call(MatrixBlock v1) throws Exception {
+			return _m1.ternaryOperations(_op, _m2, v1, new MatrixBlock());
+		}
+	}
+	
+	private static class TernaryFunctionMMS extends TernaryFunction implements Function<Tuple2<MatrixBlock,MatrixBlock>, MatrixBlock> {
+		private static final long serialVersionUID = 1L;
+		public TernaryFunctionMMS(TernaryOperator op, MatrixBlock m1, MatrixBlock m2, MatrixBlock m3) {
+			super(op, m1, m2, m3);
+		}
+		@Override
+		public MatrixBlock call(Tuple2<MatrixBlock, MatrixBlock> v1) throws Exception {
+			return v1._1().ternaryOperations(_op, v1._2(), _m3, new MatrixBlock());
+		}
+	}
+	
+	private static class TernaryFunctionMSM extends TernaryFunction implements Function<Tuple2<MatrixBlock,MatrixBlock>, MatrixBlock> {
+		private static final long serialVersionUID = 1L;
+		public TernaryFunctionMSM(TernaryOperator op, MatrixBlock m1, MatrixBlock m2, MatrixBlock m3) {
+			super(op, m1, m2, m3);
+		}
+		@Override
+		public MatrixBlock call(Tuple2<MatrixBlock, MatrixBlock> v1) throws Exception {
+			return v1._1().ternaryOperations(_op, _m2, v1._2(), new MatrixBlock());
+		}
+	}
+	
+	private static class TernaryFunctionSMM extends TernaryFunction implements Function<Tuple2<MatrixBlock,MatrixBlock>, MatrixBlock> {
+		private static final long serialVersionUID = 1L;
+		public TernaryFunctionSMM(TernaryOperator op, MatrixBlock m1, MatrixBlock m2, MatrixBlock m3) {
+			super(op, m1, m2, m3);
+		}
+		@Override
+		public MatrixBlock call(Tuple2<MatrixBlock, MatrixBlock> v1) throws Exception {
+			return _m1.ternaryOperations(_op, v1._1(), v1._2(), new MatrixBlock());
+		}
+	}
+	
+	private static class TernaryFunctionMMM extends TernaryFunction implements Function<Tuple2<Tuple2<MatrixBlock,MatrixBlock>,MatrixBlock>, MatrixBlock> {
+		private static final long serialVersionUID = 1L;
+		public TernaryFunctionMMM(TernaryOperator op, MatrixBlock m1, MatrixBlock m2, MatrixBlock m3) {
+			super(op, m1, m2, m3);
+		}
+		@Override
+		public MatrixBlock call(Tuple2<Tuple2<MatrixBlock,MatrixBlock>,MatrixBlock> v1) throws Exception {
+			return v1._1()._1().ternaryOperations(_op, v1._1()._2(), v1._2(), new MatrixBlock());
+		}
+	}
+}

http://git-wip-us.apache.org/repos/asf/systemml/blob/ce9e42fe/src/main/java/org/apache/sysml/runtime/matrix/MatrixCharacteristics.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/sysml/runtime/matrix/MatrixCharacteristics.java b/src/main/java/org/apache/sysml/runtime/matrix/MatrixCharacteristics.java
index b447b23..0c06424 100644
--- a/src/main/java/org/apache/sysml/runtime/matrix/MatrixCharacteristics.java
+++ b/src/main/java/org/apache/sysml/runtime/matrix/MatrixCharacteristics.java
@@ -56,6 +56,7 @@ import org.apache.sysml.runtime.instructions.mr.ReorgInstruction;
 import org.apache.sysml.runtime.instructions.mr.ReplicateInstruction;
 import org.apache.sysml.runtime.instructions.mr.ScalarInstruction;
 import org.apache.sysml.runtime.instructions.mr.SeqInstruction;
+import org.apache.sysml.runtime.instructions.mr.TernaryInstruction;
 import org.apache.sysml.runtime.instructions.mr.CtableInstruction;
 import org.apache.sysml.runtime.instructions.mr.UaggOuterChainInstruction;
 import org.apache.sysml.runtime.instructions.mr.UnaryInstruction;
@@ -376,6 +377,9 @@ public class MatrixCharacteristics implements Serializable
 				dimOut.set(mc1);	
 			}
 		}
+		else if( ins instanceof TernaryInstruction ) {
+			dimOut.set(dims.get(ins.getInputIndexes()[0]));
+		}
 		else if (ins instanceof CombineTernaryInstruction ) {
 			CtableInstruction realIns=(CtableInstruction)ins;
 			dimOut.set(dims.get(realIns.input1));

http://git-wip-us.apache.org/repos/asf/systemml/blob/ce9e42fe/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 098427f..81cd3a9 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
@@ -78,6 +78,7 @@ import org.apache.sysml.runtime.matrix.operators.Operator;
 import org.apache.sysml.runtime.matrix.operators.QuaternaryOperator;
 import org.apache.sysml.runtime.matrix.operators.ReorgOperator;
 import org.apache.sysml.runtime.matrix.operators.ScalarOperator;
+import org.apache.sysml.runtime.matrix.operators.TernaryOperator;
 import org.apache.sysml.runtime.matrix.operators.UnaryOperator;
 import org.apache.sysml.runtime.util.DataConverter;
 import org.apache.sysml.runtime.util.FastBufferedDataInputStream;
@@ -157,6 +158,11 @@ public class MatrixBlock extends MatrixValue implements CacheBlock, Externalizab
 		copy(that);
 	}
 	
+	public MatrixBlock(double val) {
+		reset(1, 1, false, 1, val);
+		nonZeros = (val != 0) ? 1 : 0;
+	}
+	
 	/**
 	 * Constructs a sparse {@link MatrixBlock} with a given instance of a {@link SparseBlock} 
 	 * @param rl number of rows
@@ -2777,6 +2783,49 @@ public class MatrixBlock extends MatrixValue implements CacheBlock, Externalizab
 		LibMatrixBincell.bincellOpInPlace(this, that, op);
 	}
 	
+	public MatrixBlock ternaryOperations(TernaryOperator op, MatrixBlock m2, MatrixBlock m3, MatrixBlock ret)
+		throws DMLRuntimeException
+	{
+		//TODO perf for special cases like ifelse
+		
+		final int m = Math.max(Math.max(rlen, m2.rlen), m3.rlen);
+		final int n = Math.max(Math.max(clen, m2.clen), m3.clen);
+		
+		//error handling 
+		if( (rlen != 1 && rlen != m) || (clen != 1 && clen != n)
+			|| (m2.rlen != 1 && m2.rlen != m) || (m2.clen != 1 && m2.clen != n)
+			|| (m3.rlen != 1 && m3.rlen != m) || (m3.clen != 1 && m3.clen != n) ) {
+			throw new DMLRuntimeException("Block sizes are not matched for ternary cell operations: "
+				+ rlen + "x" + clen + " vs " + m2.rlen + "x" + m2.clen + " vs " + m3.rlen + "x" + m3.clen);
+		}
+		
+		//prepare inputs
+		final boolean s1 = (rlen==1 && clen==1);
+		final boolean s2 = (m2.rlen==1 && m2.clen==1);
+		final boolean s3 = (m3.rlen==1 && m3.clen==1);
+		final double d1 = s1 ? quickGetValue(0, 0) : Double.NaN;
+		final double d2 = s2 ? m2.quickGetValue(0, 0) : Double.NaN;
+		final double d3 = s3 ? m3.quickGetValue(0, 0) : Double.NaN;
+		
+		//prepare result
+		ret.reset(m, n, false);
+		ret.allocateDenseBlock();
+		
+		//basic ternary operations
+		for( int i=0; i<m; i++ )
+			for( int j=0; j<n; j++ ) {
+				double in1 = s1 ? d1 : quickGetValue(i, j);
+				double in2 = s2 ? d2 : m2.quickGetValue(i, j);
+				double in3 = s3 ? d3 : m3.quickGetValue(i, j);
+				ret.appendValue(i, j, op.fn.execute(in1, in2, in3));
+			}
+		
+		//ensure correct output representation
+		ret.examSparsity();
+		
+		return ret;
+	}
+	
 	@Override
 	public void incrementalAggregate(AggregateOperator aggOp, MatrixValue correction, 
 			MatrixValue newWithCorrection)

http://git-wip-us.apache.org/repos/asf/systemml/blob/ce9e42fe/src/main/java/org/apache/sysml/runtime/matrix/operators/BinaryOperator.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/sysml/runtime/matrix/operators/BinaryOperator.java b/src/main/java/org/apache/sysml/runtime/matrix/operators/BinaryOperator.java
index 48af5e1..5245db5 100644
--- a/src/main/java/org/apache/sysml/runtime/matrix/operators/BinaryOperator.java
+++ b/src/main/java/org/apache/sysml/runtime/matrix/operators/BinaryOperator.java
@@ -39,14 +39,12 @@ import org.apache.sysml.runtime.functionobjects.IntegerDivide;
 import org.apache.sysml.runtime.functionobjects.LessThan;
 import org.apache.sysml.runtime.functionobjects.LessThanEquals;
 import org.apache.sysml.runtime.functionobjects.Minus;
-import org.apache.sysml.runtime.functionobjects.MinusMultiply;
 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.NotEquals;
 import org.apache.sysml.runtime.functionobjects.Or;
 import org.apache.sysml.runtime.functionobjects.Plus;
-import org.apache.sysml.runtime.functionobjects.PlusMultiply;
 import org.apache.sysml.runtime.functionobjects.Power;
 import org.apache.sysml.runtime.functionobjects.ValueFunction;
 import org.apache.sysml.runtime.functionobjects.Xor;
@@ -61,7 +59,6 @@ public class BinaryOperator  extends Operator implements Serializable
 		//binaryop is sparse-safe iff (0 op 0) == 0
 		super (p instanceof Plus || p instanceof Multiply || p instanceof Minus
 			|| p instanceof And || p instanceof Or || p instanceof Xor
-			|| p instanceof PlusMultiply || p instanceof MinusMultiply
 			|| p instanceof BitwAnd || p instanceof BitwOr || p instanceof BitwXor
 			|| p instanceof BitwShiftL || p instanceof BitwShiftR);
 		fn = p;

http://git-wip-us.apache.org/repos/asf/systemml/blob/ce9e42fe/src/main/java/org/apache/sysml/runtime/matrix/operators/TernaryOperator.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/sysml/runtime/matrix/operators/TernaryOperator.java b/src/main/java/org/apache/sysml/runtime/matrix/operators/TernaryOperator.java
new file mode 100644
index 0000000..45be887
--- /dev/null
+++ b/src/main/java/org/apache/sysml/runtime/matrix/operators/TernaryOperator.java
@@ -0,0 +1,46 @@
+/*
+ * 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.matrix.operators;
+
+import java.io.Serializable;
+
+import org.apache.sysml.runtime.functionobjects.IfElse;
+import org.apache.sysml.runtime.functionobjects.MinusMultiply;
+import org.apache.sysml.runtime.functionobjects.PlusMultiply;
+import org.apache.sysml.runtime.functionobjects.TernaryValueFunction;
+
+public class TernaryOperator  extends Operator implements Serializable
+{
+	private static final long serialVersionUID = 3456088891054083634L;
+	
+	public final TernaryValueFunction fn;
+	
+	public TernaryOperator(TernaryValueFunction p) {
+		//ternaryop is sparse-safe iff (op 0 0 0) == 0
+		super (p instanceof PlusMultiply || p instanceof MinusMultiply || p instanceof IfElse);
+		fn = p;
+	}
+	
+	@Override
+	public String toString() {
+		return "TernaryOperator("+fn.getClass().getSimpleName()+")";
+	}
+}

http://git-wip-us.apache.org/repos/asf/systemml/blob/ce9e42fe/src/test/java/org/apache/sysml/test/integration/functions/ternary/FullIfElseTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/sysml/test/integration/functions/ternary/FullIfElseTest.java b/src/test/java/org/apache/sysml/test/integration/functions/ternary/FullIfElseTest.java
new file mode 100644
index 0000000..581a6cd
--- /dev/null
+++ b/src/test/java/org/apache/sysml/test/integration/functions/ternary/FullIfElseTest.java
@@ -0,0 +1,349 @@
+/*
+ * 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.test.integration.functions.ternary;
+
+import java.util.HashMap;
+
+import org.junit.Test;
+
+import org.apache.sysml.api.DMLScript;
+import org.apache.sysml.api.DMLScript.RUNTIME_PLATFORM;
+import org.apache.sysml.lops.LopProperties.ExecType;
+import org.apache.sysml.runtime.matrix.data.MatrixValue.CellIndex;
+import org.apache.sysml.test.integration.AutomatedTestBase;
+import org.apache.sysml.test.integration.TestConfiguration;
+import org.apache.sysml.test.utils.TestUtils;
+
+public class FullIfElseTest extends AutomatedTestBase 
+{
+	private final static String TEST_NAME1 = "TernaryIfElse";
+	
+	private final static String TEST_DIR = "functions/ternary/";
+	private final static String TEST_CLASS_DIR = TEST_DIR + FullIfElseTest.class.getSimpleName() + "/";
+	
+	private final static int rows = 2111;
+	private final static int cols = 30;
+	
+	private final static double sparsity1 = 0.6;
+	private final static double sparsity2 = 0.1;
+	
+	@Override
+	public void setUp() {
+		TestUtils.clearAssertionInformation();
+		addTestConfiguration(TEST_NAME1, new TestConfiguration(TEST_CLASS_DIR, TEST_NAME1, new String[] { "R" }) );
+	}
+
+	@Test
+	public void testScalarScalarScalarDenseCP() {
+		runIfElseTest(false, false, false, false, ExecType.CP);
+	}
+	
+	@Test
+	public void testMatrixScalarScalarDenseCP() {
+		runIfElseTest(true, false, false, false, ExecType.CP);
+	}
+	
+	@Test
+	public void testScalarMatrixScalarDenseCP() {
+		runIfElseTest(false, true, false, false, ExecType.CP);
+	}
+	
+	@Test
+	public void testMatrixMatrixScalarDenseCP() {
+		runIfElseTest(true, true, false, false, ExecType.CP);
+	}
+	
+	@Test
+	public void testScalarScalarMatrixDenseCP() {
+		runIfElseTest(false, false, true, false, ExecType.CP);
+	}
+	
+	@Test
+	public void testMatrixScalarMatrixDenseCP() {
+		runIfElseTest(true, false, true, false, ExecType.CP);
+	}
+	
+	@Test
+	public void testScalarMatrixMatrixDenseCP() {
+		runIfElseTest(false, true, true, false, ExecType.CP);
+	}
+	
+	@Test
+	public void testMatrixMatrixMatrixDenseCP() {
+		runIfElseTest(true, true, true, false, ExecType.CP);
+	}
+
+	@Test
+	public void testScalarScalarScalarSparseCP() {
+		runIfElseTest(false, false, false, true, ExecType.CP);
+	}
+	
+	@Test
+	public void testMatrixScalarScalarSparseCP() {
+		runIfElseTest(true, false, false, true, ExecType.CP);
+	}
+	
+	@Test
+	public void testScalarMatrixScalarSparseCP() {
+		runIfElseTest(false, true, false, true, ExecType.CP);
+	}
+	
+	@Test
+	public void testMatrixMatrixScalarSparseCP() {
+		runIfElseTest(true, true, false, true, ExecType.CP);
+	}
+	
+	@Test
+	public void testScalarScalarMatrixSparseCP() {
+		runIfElseTest(false, false, true, true, ExecType.CP);
+	}
+	
+	@Test
+	public void testMatrixScalarMatrixSparseCP() {
+		runIfElseTest(true, false, true, true, ExecType.CP);
+	}
+	
+	@Test
+	public void testScalarMatrixMatrixSparseCP() {
+		runIfElseTest(false, true, true, true, ExecType.CP);
+	}
+	
+	@Test
+	public void testMatrixMatrixMatrixSparseCP() {
+		runIfElseTest(true, true, true, true, ExecType.CP);
+	}
+
+	//SPARK
+	
+	@Test
+	public void testScalarScalarScalarDenseSP() {
+		runIfElseTest(false, false, false, false, ExecType.SPARK);
+	}
+	
+	@Test
+	public void testMatrixScalarScalarDenseSP() {
+		runIfElseTest(true, false, false, false, ExecType.SPARK);
+	}
+	
+	@Test
+	public void testScalarMatrixScalarDenseSP() {
+		runIfElseTest(false, true, false, false, ExecType.SPARK);
+	}
+	
+	@Test
+	public void testMatrixMatrixScalarDenseSP() {
+		runIfElseTest(true, true, false, false, ExecType.SPARK);
+	}
+	
+	@Test
+	public void testScalarScalarMatrixDenseSP() {
+		runIfElseTest(false, false, true, false, ExecType.SPARK);
+	}
+	
+	@Test
+	public void testMatrixScalarMatrixDenseSP() {
+		runIfElseTest(true, false, true, false, ExecType.SPARK);
+	}
+	
+	@Test
+	public void testScalarMatrixMatrixDenseSP() {
+		runIfElseTest(false, true, true, false, ExecType.SPARK);
+	}
+	
+	@Test
+	public void testMatrixMatrixMatrixDenseSP() {
+		runIfElseTest(true, true, true, false, ExecType.SPARK);
+	}
+
+	@Test
+	public void testScalarScalarScalarSparseSP() {
+		runIfElseTest(false, false, false, true, ExecType.SPARK);
+	}
+	
+	@Test
+	public void testMatrixScalarScalarSparseSP() {
+		runIfElseTest(true, false, false, true, ExecType.SPARK);
+	}
+	
+	@Test
+	public void testScalarMatrixScalarSparseSP() {
+		runIfElseTest(false, true, false, true, ExecType.SPARK);
+	}
+	
+	@Test
+	public void testMatrixMatrixScalarSparseSP() {
+		runIfElseTest(true, true, false, true, ExecType.SPARK);
+	}
+	
+	@Test
+	public void testScalarScalarMatrixSparseSP() {
+		runIfElseTest(false, false, true, true, ExecType.SPARK);
+	}
+	
+	@Test
+	public void testMatrixScalarMatrixSparseSP() {
+		runIfElseTest(true, false, true, true, ExecType.SPARK);
+	}
+	
+	@Test
+	public void testScalarMatrixMatrixSparseSP() {
+		runIfElseTest(false, true, true, true, ExecType.SPARK);
+	}
+	
+	@Test
+	public void testMatrixMatrixMatrixSparseSP() {
+		runIfElseTest(true, true, true, true, ExecType.SPARK);
+	}
+
+	//MR
+	
+	@Test
+	public void testScalarScalarScalarDenseMR() {
+		runIfElseTest(false, false, false, false, ExecType.MR);
+	}
+	
+	@Test
+	public void testMatrixScalarScalarDenseMR() {
+		runIfElseTest(true, false, false, false, ExecType.MR);
+	}
+	
+	@Test
+	public void testScalarMatrixScalarDenseMR() {
+		runIfElseTest(false, true, false, false, ExecType.MR);
+	}
+	
+	@Test
+	public void testMatrixMatrixScalarDenseMR() {
+		runIfElseTest(true, true, false, false, ExecType.MR);
+	}
+	
+	@Test
+	public void testScalarScalarMatrixDenseMR() {
+		runIfElseTest(false, false, true, false, ExecType.MR);
+	}
+	
+	@Test
+	public void testMatrixScalarMatrixDenseMR() {
+		runIfElseTest(true, false, true, false, ExecType.MR);
+	}
+	
+	@Test
+	public void testScalarMatrixMatrixDenseMR() {
+		runIfElseTest(false, true, true, false, ExecType.MR);
+	}
+	
+	@Test
+	public void testMatrixMatrixMatrixDenseMR() {
+		runIfElseTest(true, true, true, false, ExecType.MR);
+	}
+
+	@Test
+	public void testScalarScalarScalarSparseMR() {
+		runIfElseTest(false, false, false, true, ExecType.MR);
+	}
+	
+	@Test
+	public void testMatrixScalarScalarSparseMR() {
+		runIfElseTest(true, false, false, true, ExecType.MR);
+	}
+	
+	@Test
+	public void testScalarMatrixScalarSparseMR() {
+		runIfElseTest(false, true, false, true, ExecType.MR);
+	}
+	
+	@Test
+	public void testMatrixMatrixScalarSparseMR() {
+		runIfElseTest(true, true, false, true, ExecType.MR);
+	}
+	
+	@Test
+	public void testScalarScalarMatrixSparseMR() {
+		runIfElseTest(false, false, true, true, ExecType.MR);
+	}
+	
+	@Test
+	public void testMatrixScalarMatrixSparseMR() {
+		runIfElseTest(true, false, true, true, ExecType.MR);
+	}
+	
+	@Test
+	public void testScalarMatrixMatrixSparseMR() {
+		runIfElseTest(false, true, true, true, ExecType.MR);
+	}
+	
+	@Test
+	public void testMatrixMatrixMatrixSparseMR() {
+		runIfElseTest(true, true, true, true, ExecType.MR);
+	}
+	
+	private void runIfElseTest(boolean matrix1, boolean matrix2, boolean matrix3, boolean sparse, ExecType et)
+	{
+		//rtplatform for MR
+		RUNTIME_PLATFORM platformOld = rtplatform;
+		switch( et ){
+			case MR: rtplatform = RUNTIME_PLATFORM.HADOOP; break;
+			case SPARK: rtplatform = RUNTIME_PLATFORM.SPARK; break;
+			default: rtplatform = RUNTIME_PLATFORM.HYBRID_SPARK; break;
+		}
+	
+		boolean sparkConfigOld = DMLScript.USE_LOCAL_SPARK_CONFIG;
+		if( rtplatform == RUNTIME_PLATFORM.SPARK || rtplatform == RUNTIME_PLATFORM.HYBRID_SPARK )
+			DMLScript.USE_LOCAL_SPARK_CONFIG = true;
+		
+		try
+		{
+			TestConfiguration config = getTestConfiguration(TEST_NAME1);
+			loadTestConfiguration(config);
+			
+			String HOME = SCRIPT_DIR + TEST_DIR;
+			fullDMLScriptName = HOME + TEST_NAME1 + ".dml";
+			programArgs = new String[]{"-explain","-args", input("A"), input("B"), input("C"), output("R")};
+			fullRScriptName = HOME + TEST_NAME1 + ".R";
+			rCmd = "Rscript" + " " + fullRScriptName + " " + inputDir() + " " + expectedDir();
+	
+			//generate actual datasets (matrices and scalars)
+			double sparsity = sparse ? sparsity2 : sparsity1;
+			double[][] A = matrix1 ? getRandomMatrix(rows, cols, 0, 1, sparsity, 1) : getScalar(1);
+			writeInputMatrixWithMTD("A", A, true);
+			double[][] B = matrix2 ? getRandomMatrix(rows, cols, 0, 1, sparsity, 2) : getScalar(2);
+			writeInputMatrixWithMTD("B", B, true);
+			double[][] C = matrix3 ? getRandomMatrix(rows, cols, 0, 1, sparsity, 3) : getScalar(3);
+			writeInputMatrixWithMTD("C", C, true);
+			
+			//run test cases
+			runTest(true, false, null, -1);
+			runRScript(true); 
+			
+			//compare output matrices 
+			HashMap<CellIndex, Double> dmlfile = readDMLMatrixFromHDFS("R");
+			HashMap<CellIndex, Double> rfile  = readRMatrixFromFS("R");
+			TestUtils.compareMatrices(dmlfile, rfile, 0, "Stat-DML", "Stat-R");
+		}
+		finally {
+			rtplatform = platformOld;
+			DMLScript.USE_LOCAL_SPARK_CONFIG = sparkConfigOld;
+		}
+	}
+	
+	private double[][] getScalar(int input) {
+		return new double[][]{{7d*input}};
+	}
+}

http://git-wip-us.apache.org/repos/asf/systemml/blob/ce9e42fe/src/test/scripts/functions/ternary/TernaryIfElse.R
----------------------------------------------------------------------
diff --git a/src/test/scripts/functions/ternary/TernaryIfElse.R b/src/test/scripts/functions/ternary/TernaryIfElse.R
new file mode 100644
index 0000000..a1e8a03
--- /dev/null
+++ b/src/test/scripts/functions/ternary/TernaryIfElse.R
@@ -0,0 +1,45 @@
+#-------------------------------------------------------------
+#
+# 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.
+#
+#-------------------------------------------------------------
+
+args <- commandArgs(TRUE)
+options(digits=22)
+
+library("Matrix")
+
+A = as.matrix(readMM(paste(args[1], "A.mtx", sep="")))
+B = as.matrix(readMM(paste(args[1], "B.mtx", sep="")))
+C = as.matrix(readMM(paste(args[1], "C.mtx", sep="")))
+m = max(max(nrow(A), nrow(B)), nrow(C))
+n = max(max(ncol(A), ncol(B)), ncol(C))
+
+if( nrow(A)==1 ) {
+  A = matrix(A, m, n);
+}
+if( nrow(B)==1 ) {
+  B = matrix(B, m, n);
+}
+if( nrow(C)==1 ) {
+  C = matrix(C, m, n);
+}  
+
+R = matrix(ifelse(as.vector(A), as.vector(B), as.vector(C)), m, n);
+
+writeMM(as(R, "CsparseMatrix"), paste(args[2], "R", sep="")); 

http://git-wip-us.apache.org/repos/asf/systemml/blob/ce9e42fe/src/test/scripts/functions/ternary/TernaryIfElse.dml
----------------------------------------------------------------------
diff --git a/src/test/scripts/functions/ternary/TernaryIfElse.dml b/src/test/scripts/functions/ternary/TernaryIfElse.dml
new file mode 100644
index 0000000..12a11cc
--- /dev/null
+++ b/src/test/scripts/functions/ternary/TernaryIfElse.dml
@@ -0,0 +1,43 @@
+#-------------------------------------------------------------
+#
+# 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.
+#
+#-------------------------------------------------------------
+
+A = read($1);
+B = read($2);
+C = read($3);
+
+if( nrow(A)==1 & nrow(B)==1 & nrow(C)==1 )
+  R = as.matrix(ifelse(as.scalar(A), as.scalar(B), as.scalar(C)));
+else if( nrow(A)>1 & nrow(B)==1 & nrow(C)==1 )  
+  R = ifelse(A, as.scalar(B), as.scalar(C));
+else if( nrow(A)==1 & nrow(B)>1 & nrow(C)==1 )
+  R = ifelse(as.scalar(A), B, as.scalar(C));
+else if( nrow(A)>1 & nrow(B)>1 & nrow(C)==1 )  
+  R = ifelse(A, B, as.scalar(C));
+else if( nrow(A)==1 & nrow(B)==1 & nrow(C)>1 )
+  R = ifelse(as.scalar(A), as.scalar(B), C);
+else if( nrow(A)>1 & nrow(B)==1 & nrow(C)>1 )  
+  R = ifelse(A, as.scalar(B), C);
+else if( nrow(A)==1 & nrow(B)>1 & nrow(C)>1 )
+  R = ifelse(as.scalar(A), B, C);
+else if( nrow(A)>1 & nrow(B)>1 & nrow(C)>1 )  
+  R = ifelse(A, B, C);
+
+write(R, $4);

http://git-wip-us.apache.org/repos/asf/systemml/blob/ce9e42fe/src/test_suites/java/org/apache/sysml/test/integration/functions/ternary/ZPackageSuite.java
----------------------------------------------------------------------
diff --git a/src/test_suites/java/org/apache/sysml/test/integration/functions/ternary/ZPackageSuite.java b/src/test_suites/java/org/apache/sysml/test/integration/functions/ternary/ZPackageSuite.java
index ee14359..4b83df3 100644
--- a/src/test_suites/java/org/apache/sysml/test/integration/functions/ternary/ZPackageSuite.java
+++ b/src/test_suites/java/org/apache/sysml/test/integration/functions/ternary/ZPackageSuite.java
@@ -31,6 +31,7 @@ import org.junit.runners.Suite;
 	CovarianceWeightsTest.class,
 	CTableMatrixIgnoreZerosTest.class,
 	CTableSequenceTest.class,
+	FullIfElseTest.class,
 	QuantileWeightsTest.class,
 	TableOutputTest.class,
 	TernaryAggregateTest.class,


[5/5] systemml git commit: [SYSTEMML-2055] New ifelse builtin function, ternary inst framework

Posted by mb...@apache.org.
[SYSTEMML-2055] New ifelse builtin function, ternary inst framework

This patch introduces a vectorized D = ifelse(A, B, C) builtin function
which output is defined as Dij = (Aij!=0) ? Bij : Cij for matrix inputs
but it also supports all combinations of matrix-scalar operations.
Similar to the binary cellwise instruction framework, this patch also
introduces a new ternary instruction framework for all backends. A
current restriction is that all matrix inputs need to be of equal shape
- in the future we will extend this to matrix-vector operations as well.
This new instruction framework now also hosts the exiting physical
operators for plus_mult and minus_mult (axpy).


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

Branch: refs/heads/master
Commit: ce9e42fefb6277b2b8c04db65658e955c7444e2d
Parents: 5743810
Author: Matthias Boehm <mb...@gmail.com>
Authored: Wed Jan 17 17:52:50 2018 -0800
Committer: Matthias Boehm <mb...@gmail.com>
Committed: Thu Jan 18 11:20:08 2018 -0800

----------------------------------------------------------------------
 src/main/java/org/apache/sysml/hops/Hop.java    |  11 +-
 .../java/org/apache/sysml/hops/TernaryOp.java   | 105 +++---
 .../hops/cost/CostEstimatorStaticRuntime.java   |  19 +-
 .../sysml/hops/rewrite/HopRewriteUtils.java     |   8 +
 .../RewriteAlgebraicSimplificationDynamic.java  |  10 +-
 src/main/java/org/apache/sysml/lops/Ctable.java |   2 +-
 src/main/java/org/apache/sysml/lops/Lop.java    |   2 +-
 .../java/org/apache/sysml/lops/PlusMult.java    | 118 -------
 .../java/org/apache/sysml/lops/Ternary.java     | 115 ++++++
 .../java/org/apache/sysml/lops/compile/Dag.java |   6 +-
 .../sysml/parser/BuiltinFunctionExpression.java |  42 ++-
 .../org/apache/sysml/parser/DMLTranslator.java  |   7 +-
 .../org/apache/sysml/parser/Expression.java     |   1 +
 .../sysml/runtime/functionobjects/IfElse.java   |  44 +++
 .../runtime/functionobjects/MinusMultiply.java  |  21 +-
 .../runtime/functionobjects/PlusMultiply.java   |  23 +-
 .../functionobjects/TernaryValueFunction.java   |  32 ++
 .../ValueFunctionWithConstant.java              |  36 --
 .../instructions/CPInstructionParser.java       |  30 +-
 .../runtime/instructions/InstructionUtils.java  |  11 +-
 .../instructions/MRInstructionParser.java       |  13 +-
 .../instructions/SPInstructionParser.java       |  20 +-
 .../runtime/instructions/cp/CPInstruction.java  |   2 +-
 .../runtime/instructions/cp/CPOperand.java      |  11 +
 .../instructions/cp/PlusMultCPInstruction.java  |  72 ----
 .../instructions/cp/ScalarObjectFactory.java    |  10 +
 .../instructions/cp/TernaryCPInstruction.java   |  81 +++++
 .../runtime/instructions/mr/MRInstruction.java  |   2 +-
 .../instructions/mr/PlusMultInstruction.java    |  61 ----
 .../instructions/mr/TernaryInstruction.java     | 105 ++++++
 .../spark/PlusMultSPInstruction.java            |  71 ----
 .../instructions/spark/SPInstruction.java       |   2 +-
 .../spark/TernarySPInstruction.java             | 200 +++++++++++
 .../runtime/matrix/MatrixCharacteristics.java   |   4 +
 .../sysml/runtime/matrix/data/MatrixBlock.java  |  49 +++
 .../matrix/operators/BinaryOperator.java        |   3 -
 .../matrix/operators/TernaryOperator.java       |  46 +++
 .../functions/ternary/FullIfElseTest.java       | 349 +++++++++++++++++++
 .../scripts/functions/ternary/TernaryIfElse.R   |  45 +++
 .../scripts/functions/ternary/TernaryIfElse.dml |  43 +++
 .../functions/ternary/ZPackageSuite.java        |   1 +
 41 files changed, 1348 insertions(+), 485 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/systemml/blob/ce9e42fe/src/main/java/org/apache/sysml/hops/Hop.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/sysml/hops/Hop.java b/src/main/java/org/apache/sysml/hops/Hop.java
index ae8c559..3ed837e 100644
--- a/src/main/java/org/apache/sysml/hops/Hop.java
+++ b/src/main/java/org/apache/sysml/hops/Hop.java
@@ -40,6 +40,7 @@ import org.apache.sysml.lops.LopProperties.ExecType;
 import org.apache.sysml.lops.LopsException;
 import org.apache.sysml.lops.Nary;
 import org.apache.sysml.lops.ReBlock;
+import org.apache.sysml.lops.Ternary;
 import org.apache.sysml.lops.Unary;
 import org.apache.sysml.lops.UnaryCP;
 import org.apache.sysml.parser.Expression.DataType;
@@ -1074,7 +1075,7 @@ public abstract class Hop implements ParseInfo
 
 	// Operations that require 3 operands
 	public enum OpOp3 {
-		QUANTILE, INTERQUANTILE, CTABLE, CENTRALMOMENT, COVARIANCE, PLUS_MULT, MINUS_MULT
+		QUANTILE, INTERQUANTILE, CTABLE, CENTRALMOMENT, COVARIANCE, PLUS_MULT, MINUS_MULT, IFELSE
 	}
 	
 	// Operations that require 4 operands
@@ -1349,6 +1350,14 @@ public abstract class Hop implements ParseInfo
 		HopsOpOp1LopsUS.put(OpOp1.STOP, org.apache.sysml.lops.UnaryCP.OperationTypes.STOP);
 	}
 
+	protected static final HashMap<OpOp3, Ternary.OperationType> HopsOpOp3Lops;
+	static {
+		HopsOpOp3Lops = new HashMap<>();
+		HopsOpOp3Lops.put(OpOp3.PLUS_MULT, Ternary.OperationType.PLUS_MULT);
+		HopsOpOp3Lops.put(OpOp3.MINUS_MULT, Ternary.OperationType.MINUS_MULT);
+		HopsOpOp3Lops.put(OpOp3.IFELSE, Ternary.OperationType.IFELSE);
+	}
+	
 	/**
 	 * Maps from a multiple (variable number of operands) Hop operation type to
 	 * the corresponding Lop operation type. This is called in the MultipleOp

http://git-wip-us.apache.org/repos/asf/systemml/blob/ce9e42fe/src/main/java/org/apache/sysml/hops/TernaryOp.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/sysml/hops/TernaryOp.java b/src/main/java/org/apache/sysml/hops/TernaryOp.java
index dc40982..49f67b1 100644
--- a/src/main/java/org/apache/sysml/hops/TernaryOp.java
+++ b/src/main/java/org/apache/sysml/hops/TernaryOp.java
@@ -31,8 +31,7 @@ import org.apache.sysml.lops.Group;
 import org.apache.sysml.lops.Lop;
 import org.apache.sysml.lops.LopsException;
 import org.apache.sysml.lops.PickByCount;
-import org.apache.sysml.lops.PlusMult;
-import org.apache.sysml.lops.RepMat;
+import org.apache.sysml.lops.Ternary;
 import org.apache.sysml.lops.SortKeys;
 import org.apache.sysml.lops.Ctable;
 import org.apache.sysml.lops.UnaryCP;
@@ -136,6 +135,7 @@ public class TernaryOp extends Hop
 			case CTABLE:
 			case INTERQUANTILE:
 			case QUANTILE:
+			case IFELSE:
 				return false;
 			case MINUS_MULT:
 			case PLUS_MULT:
@@ -175,7 +175,8 @@ public class TernaryOp extends Hop
 				
 				case PLUS_MULT:
 				case MINUS_MULT:
-					constructLopsPlusMult();
+				case IFELSE:
+					constructLopsTernaryDefault();
 					break;
 					
 				default:
@@ -186,10 +187,10 @@ public class TernaryOp extends Hop
 		catch(LopsException e) {
 			throw new HopsException(this.printErrorLocation() + "error constructing Lops for TernaryOp Hop " , e);
 		}
-
+		
 		//add reblock/checkpoint lops if necessary
 		constructAndSetLopsDataFlowProperties();
-				
+		
 		return getLops();
 	}
 
@@ -643,45 +644,49 @@ public class TernaryOp extends Hop
 		}
 	}
 
-	private void constructLopsPlusMult() 
+	private void constructLopsTernaryDefault() 
 		throws HopsException, LopsException 
 	{
-		if ( _op != OpOp3.PLUS_MULT && _op != OpOp3.MINUS_MULT )
-			throw new HopsException("Unexpected operation: " + _op + ", expecting " + OpOp3.PLUS_MULT + " or" +  OpOp3.MINUS_MULT);
-		
 		ExecType et = optFindExecType();
-		PlusMult plusmult = null;
+		if( getInput().stream().allMatch(h -> h.getDataType().isScalar()) )
+			et = ExecType.CP; //always CP for pure scalar operations
+		
+		Ternary plusmult = null;
 		
 		if( et == ExecType.CP || et == ExecType.SPARK || et == ExecType.GPU ) {
-			plusmult = new PlusMult(
-					getInput().get(0).constructLops(),
-					getInput().get(1).constructLops(),
-					getInput().get(2).constructLops(), 
-					_op, getDataType(),getValueType(), et );	
+			plusmult = new Ternary(HopsOpOp3Lops.get(_op),
+				getInput().get(0).constructLops(),
+				getInput().get(1).constructLops(),
+				getInput().get(2).constructLops(), 
+				getDataType(),getValueType(), et );
 		}
 		else { //MR
-			Hop left = getInput().get(0);
-			Hop right = getInput().get(2);
-			boolean requiresRep = BinaryOp.requiresReplication(left, right);
+			Hop first = getInput().get(0);
+			Hop second = getInput().get(1);
+			Hop third = getInput().get(2);
 			
-			Lop rightLop = right.constructLops();
-			if( requiresRep ) {
-				Lop offset = createOffsetLop(left, (right.getDim2()<=1)); //ncol of left input (determines num replicates)
-				rightLop = new RepMat(rightLop, offset, (right.getDim2()<=1), right.getDataType(), right.getValueType());
-				setOutputDimensions(rightLop);
-				setLineNumbers(rightLop);	
+			Lop firstLop = first.constructLops(); 
+			if( first.getDataType().isMatrix() ) {
+				firstLop = new Group(firstLop, Group.OperationTypes.Sort, getDataType(), getValueType());
+				setLineNumbers(firstLop);
+				setOutputDimensions(firstLop);
+			}
+			Lop secondLop = second.constructLops(); 
+			if( second.getDataType().isMatrix() ) {
+				secondLop = new Group(secondLop, Group.OperationTypes.Sort, getDataType(), getValueType());
+				setLineNumbers(secondLop);
+				setOutputDimensions(secondLop);
+			}
+			Lop thirdLop = third.constructLops(); 
+			if( third.getDataType().isMatrix() ) {
+				thirdLop = new Group(thirdLop, Group.OperationTypes.Sort, getDataType(), getValueType());
+				setLineNumbers(thirdLop);
+				setOutputDimensions(thirdLop);
 			}
-		
-			Group group1 = new Group(left.constructLops(), Group.OperationTypes.Sort, getDataType(), getValueType());
-			setLineNumbers(group1);
-			setOutputDimensions(group1);
-		
-			Group group2 = new Group(rightLop, Group.OperationTypes.Sort, getDataType(), getValueType());
-			setLineNumbers(group2);
-			setOutputDimensions(group2);
 			
-			plusmult = new PlusMult(group1, getInput().get(1).constructLops(), 
-					group2, _op, getDataType(),getValueType(), et );	
+			plusmult = new Ternary(HopsOpOp3Lops.get(_op),
+				firstLop, secondLop, thirdLop,
+				getDataType(),getValueType(), et );
 		}
 		
 		setOutputDimensions(plusmult);
@@ -697,8 +702,7 @@ public class TernaryOp extends Hop
 	}
 
 	@Override
-	public boolean allowsAllExecTypes()
-	{
+	public boolean allowsAllExecTypes() {
 		return true;
 	}
 
@@ -722,7 +726,8 @@ public class TernaryOp extends Hop
 				// Output is a vector of length = #of quantiles to be computed, and it is likely to be dense.
 				return OptimizerUtils.estimateSizeExactSparsity(dim1, dim2, 1.0);
 			case PLUS_MULT:
-			case MINUS_MULT: {
+			case MINUS_MULT:
+			case IFELSE: {
 				if (isGPUEnabled()) {
 					// For the GPU, the input is converted to dense
 					sparsity = 1.0;
@@ -805,6 +810,11 @@ public class TernaryOp extends Hop
 				if( mc[2].dimsKnown() )
 					return new long[]{mc[2].getRows(), 1, mc[2].getRows()};
 				break;
+			case IFELSE:
+				for(MatrixCharacteristics lmc : mc)
+					if( lmc.dimsKnown() && lmc.getRows() > 0 ) //known matrix
+						return new long[]{lmc.getRows(), lmc.getCols(), -1};
+				break;
 			case PLUS_MULT:
 			case MINUS_MULT:
 				//compute back NNz
@@ -827,20 +837,17 @@ public class TernaryOp extends Hop
 
 		ExecType REMOTE = OptimizerUtils.isSparkExecutionMode() ? ExecType.SPARK : ExecType.MR;
 		
-		if( _etypeForced != null ) 			
-		{
+		if( _etypeForced != null ) {
 			_etype = _etypeForced;
 		}
 		else
-		{	
+		{
 			if ( OptimizerUtils.isMemoryBasedOptLevel() ) {
 				_etype = findExecTypeByMemEstimate();
 			}
 			else if ( (getInput().get(0).areDimsBelowThreshold() 
 					&& getInput().get(1).areDimsBelowThreshold()
-					&& getInput().get(2).areDimsBelowThreshold()) 
-					//|| (getInput().get(0).isVector() && getInput().get(1).isVector() && getInput().get(1).isVector() )
-				)
+					&& getInput().get(2).areDimsBelowThreshold()) )
 				_etype = ExecType.CP;
 			else
 				_etype = REMOTE;
@@ -908,17 +915,21 @@ public class TernaryOp extends Hop
 					// This part of the code is executed only when a vector of quantiles are computed
 					// Output is a vector of length = #of quantiles to be computed, and it is likely to be dense.
 					// TODO qx1
-					break;	
+					break;
 				
+				//default ternary operations
+				case IFELSE:
 				case PLUS_MULT:
 				case MINUS_MULT:
-					setDim1( getInput().get(0)._dim1 );
-					setDim2( getInput().get(0)._dim2 );
+					if( getDataType() == DataType.MATRIX ) {
+						setDim1( HopRewriteUtils.getMaxNrowInput(this) );
+						setDim2( HopRewriteUtils.getMaxNcolInput(this) );
+					}
 					break;
 				default:
 					throw new RuntimeException("Size information for operation (" + _op + ") can not be updated.");
 			}
-		}	
+		}
 	}
 	
 	@Override

http://git-wip-us.apache.org/repos/asf/systemml/blob/ce9e42fe/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 aebfa9d..e6ff4be 100644
--- a/src/main/java/org/apache/sysml/hops/cost/CostEstimatorStaticRuntime.java
+++ b/src/main/java/org/apache/sysml/hops/cost/CostEstimatorStaticRuntime.java
@@ -54,6 +54,7 @@ import org.apache.sysml.runtime.instructions.mr.MRInstruction;
 import org.apache.sysml.runtime.instructions.mr.MapMultChainInstruction;
 import org.apache.sysml.runtime.instructions.mr.PickByCountInstruction;
 import org.apache.sysml.runtime.instructions.mr.RemoveEmptyMRInstruction;
+import org.apache.sysml.runtime.instructions.mr.TernaryInstruction;
 import org.apache.sysml.runtime.instructions.mr.CtableInstruction;
 import org.apache.sysml.runtime.instructions.mr.UnaryMRInstructionBase;
 import org.apache.sysml.runtime.instructions.mr.MRInstruction.MRType;
@@ -395,7 +396,7 @@ public class CostEstimatorStaticRuntime extends CostEstimator
 				vs[0] = stats[ binst.input1 ];
 				vs[1] = stats[ binst.input2 ];
 				vs[2] = stats[ binst.output ];
-								
+				
 				if( vs[0] == null ) //scalar input, 
 					vs[0] = _scalarStats;
 				if( vs[1] == null ) //scalar input, 
@@ -408,6 +409,19 @@ public class CostEstimatorStaticRuntime extends CostEstimator
 					attr = new String[]{rbinst.isRemoveRows()?"0":"1"};
 				}
 			}
+			else if( mrinst instanceof TernaryInstruction ) {
+				TernaryInstruction tinst = (TernaryInstruction) mrinst;
+				byte[] ix = tinst.getAllIndexes();
+				for(int i=0; i<ix.length-1; i++)
+					vs[0] = stats[ ix[i] ];
+				vs[2] = stats[ ix[ix.length-1] ];
+				if( vs[0] == null ) //scalar input,
+					vs[0] = _scalarStats;
+				if( vs[1] == null ) //scalar input,
+					vs[1] = _scalarStats;
+				if( vs[2] == null ) //scalar output
+					vs[2] = _scalarStats;
+			}
 			else if( mrinst instanceof CtableInstruction )
 			{
 				CtableInstruction tinst = (CtableInstruction) mrinst;
@@ -884,6 +898,9 @@ public class CostEstimatorStaticRuntime extends CostEstimator
 					else
 						return d3m*d3n;
 				
+				case Ternary: //opcodes: +*, -*, ifelse
+					return 2 * d1m * d1n;
+					
 				case Ctable: //opcodes: ctable
 					if( optype.equals("ctable") ){
 						if( leftSparse )

http://git-wip-us.apache.org/repos/asf/systemml/blob/ce9e42fe/src/main/java/org/apache/sysml/hops/rewrite/HopRewriteUtils.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/sysml/hops/rewrite/HopRewriteUtils.java b/src/main/java/org/apache/sysml/hops/rewrite/HopRewriteUtils.java
index a7373fb..d9d9120 100644
--- a/src/main/java/org/apache/sysml/hops/rewrite/HopRewriteUtils.java
+++ b/src/main/java/org/apache/sysml/hops/rewrite/HopRewriteUtils.java
@@ -1343,6 +1343,14 @@ public class HopRewriteUtils
 			|| sb instanceof ForStatementBlock); //incl parfor
 	}
 	
+	public static long getMaxNrowInput(Hop hop) {
+		return getMaxInputDim(hop, true);
+	}
+	
+	public static long getMaxNcolInput(Hop hop) {
+		return getMaxInputDim(hop, false);
+	}
+	
 	public static long getMaxInputDim(Hop hop, boolean dim1) {
 		return hop.getInput().stream().mapToLong(
 			h -> (dim1?h.getDim1():h.getDim2())).max().orElse(-1);

http://git-wip-us.apache.org/repos/asf/systemml/blob/ce9e42fe/src/main/java/org/apache/sysml/hops/rewrite/RewriteAlgebraicSimplificationDynamic.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/sysml/hops/rewrite/RewriteAlgebraicSimplificationDynamic.java b/src/main/java/org/apache/sysml/hops/rewrite/RewriteAlgebraicSimplificationDynamic.java
index e07f97c..d60b60f 100644
--- a/src/main/java/org/apache/sysml/hops/rewrite/RewriteAlgebraicSimplificationDynamic.java
+++ b/src/main/java/org/apache/sysml/hops/rewrite/RewriteAlgebraicSimplificationDynamic.java
@@ -2174,7 +2174,7 @@ public class RewriteAlgebraicSimplificationDynamic extends HopRewriteRule
 	
 	private static Hop fuseAxpyBinaryOperationChain(Hop parent, Hop hi, int pos) 
 	{
-		//patterns: (a) X + s*Y -> X +* sY, (b) s*Y+X -> X +* sY, (c) X - s*Y -> X -* sY		
+		//patterns: (a) X + s*Y -> X +* sY, (b) s*Y+X -> X +* sY, (c) X - s*Y -> X -* sY
 		if( hi instanceof BinaryOp && !((BinaryOp) hi).isOuterVectorOperator()
 			&& (((BinaryOp)hi).getOp()==OpOp2.PLUS || ((BinaryOp)hi).getOp()==OpOp2.MINUS) )
 		{
@@ -2186,6 +2186,7 @@ public class RewriteAlgebraicSimplificationDynamic extends HopRewriteRule
 			//pattern (a) X + s*Y -> X +* sY
 			if( bop.getOp() == OpOp2.PLUS && left.getDataType()==DataType.MATRIX 
 				&& HopRewriteUtils.isScalarMatrixBinaryMult(right)
+				&& HopRewriteUtils.isEqualSize(left, right)
 				&& right.getParent().size() == 1 )           //single consumer s*Y
 			{
 				Hop smid = right.getInput().get( (right.getInput().get(0).getDataType()==DataType.SCALAR) ? 0 : 1); 
@@ -2197,18 +2198,19 @@ public class RewriteAlgebraicSimplificationDynamic extends HopRewriteRule
 			//pattern (b) s*Y + X -> X +* sY
 			else if( bop.getOp() == OpOp2.PLUS && right.getDataType()==DataType.MATRIX 
 				&& HopRewriteUtils.isScalarMatrixBinaryMult(left)
-				&& left.getParent().size() == 1              //single consumer s*Y
-				&& HopRewriteUtils.isEqualSize(left, right)) //correctness matrix-vector
+				&& HopRewriteUtils.isEqualSize(left, right)
+				&& left.getParent().size() == 1 )            //single consumer s*Y
 			{
 				Hop smid = left.getInput().get( (left.getInput().get(0).getDataType()==DataType.SCALAR) ? 0 : 1); 
 				Hop mright = left.getInput().get( (left.getInput().get(0).getDataType()==DataType.SCALAR) ? 1 : 0);
 				ternop = (smid instanceof LiteralOp && HopRewriteUtils.getDoubleValueSafe((LiteralOp)smid)==0) ? 
 						right : HopRewriteUtils.createTernaryOp(right, smid, mright, OpOp3.PLUS_MULT);
-				LOG.debug("Applied fuseAxpyBinaryOperationChain2. (line " +hi.getBeginLine()+")");	
+				LOG.debug("Applied fuseAxpyBinaryOperationChain2. (line " +hi.getBeginLine()+")");
 			}
 			//pattern (c) X - s*Y -> X -* sY
 			else if( bop.getOp() == OpOp2.MINUS && left.getDataType()==DataType.MATRIX 
 				&& HopRewriteUtils.isScalarMatrixBinaryMult(right)
+				&& HopRewriteUtils.isEqualSize(left, right)
 				&& right.getParent().size() == 1 )           //single consumer s*Y
 			{
 				Hop smid = right.getInput().get( (right.getInput().get(0).getDataType()==DataType.SCALAR) ? 0 : 1); 

http://git-wip-us.apache.org/repos/asf/systemml/blob/ce9e42fe/src/main/java/org/apache/sysml/lops/Ctable.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/sysml/lops/Ctable.java b/src/main/java/org/apache/sysml/lops/Ctable.java
index 7eec1bb..8aac934 100644
--- a/src/main/java/org/apache/sysml/lops/Ctable.java
+++ b/src/main/java/org/apache/sysml/lops/Ctable.java
@@ -53,7 +53,7 @@ public class Ctable extends Lop
 	}
 	
 	public Ctable(Lop[] inputLops, OperationTypes op, DataType dt, ValueType vt, boolean ignoreZeros, ExecType et) {
-		super(Lop.Type.Ternary, dt, vt);
+		super(Lop.Type.Ctable, dt, vt);
 		init(inputLops, op, et);
 		_ignoreZeros = ignoreZeros;
 	}

http://git-wip-us.apache.org/repos/asf/systemml/blob/ce9e42fe/src/main/java/org/apache/sysml/lops/Lop.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/sysml/lops/Lop.java b/src/main/java/org/apache/sysml/lops/Lop.java
index e105362..8d79224 100644
--- a/src/main/java/org/apache/sysml/lops/Lop.java
+++ b/src/main/java/org/apache/sysml/lops/Lop.java
@@ -55,7 +55,7 @@ public abstract class Lop
 		FunctionCallCP, FunctionCallCPSingle,               //CP function calls 
 		CumulativePartialAggregate, CumulativeSplitAggregate, CumulativeOffsetBinary, //MR cumsum/cumprod/cummin/cummax
 		WeightedSquaredLoss, WeightedSigmoid, WeightedDivMM, WeightedCeMM, WeightedUMM,
-		SortKeys, PickValues,
+		SortKeys, PickValues, Ctable,
 		Checkpoint,                                         //Spark persist into storage level
 		PlusMult, MinusMult,                                //CP
 		SpoofFused,                                         //CP/SP generated fused operator

http://git-wip-us.apache.org/repos/asf/systemml/blob/ce9e42fe/src/main/java/org/apache/sysml/lops/PlusMult.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/sysml/lops/PlusMult.java b/src/main/java/org/apache/sysml/lops/PlusMult.java
deleted file mode 100644
index 0649f49..0000000
--- a/src/main/java/org/apache/sysml/lops/PlusMult.java
+++ /dev/null
@@ -1,118 +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.lops;
-
-import org.apache.sysml.hops.Hop.OpOp3;
-import org.apache.sysml.lops.LopProperties.ExecLocation;
-import org.apache.sysml.lops.LopProperties.ExecType;
-import org.apache.sysml.lops.compile.JobType;
-import org.apache.sysml.parser.Expression.DataType;
-import org.apache.sysml.parser.Expression.ValueType;
-
-
-/**
- * Lop to perform Sum of a matrix with another matrix multiplied by Scalar.
- */
-public class PlusMult extends Lop 
-{
-	public PlusMult(Lop input1, Lop input2, Lop input3, OpOp3 op, DataType dt, ValueType vt, ExecType et) {
-		super(Lop.Type.PlusMult, dt, vt);
-		if(op == OpOp3.MINUS_MULT)
-			type=Lop.Type.MinusMult;
-		init(input1, input2, input3, et);
-	}
-
-	private void init(Lop input1, Lop input2, Lop input3, ExecType et) {
-		addInput(input1);
-		addInput(input2);
-		addInput(input3);
-		input1.addOutput(this);	
-		input2.addOutput(this);	
-		input3.addOutput(this);	
-		
-		boolean breaksAlignment = false;
-		boolean aligner = false;
-		boolean definesMRJob = false;
-		
-		if ( et == ExecType.CP ||  et == ExecType.SPARK || et == ExecType.GPU ){
-			lps.addCompatibility(JobType.INVALID);
-			lps.setProperties( inputs, et, ExecLocation.ControlProgram, breaksAlignment, aligner, definesMRJob );
-		}
-		else if( et == ExecType.MR ) {
-			lps.addCompatibility(JobType.GMR);
-			lps.addCompatibility(JobType.DATAGEN);
-			lps.addCompatibility(JobType.REBLOCK);
-			lps.setProperties( inputs, et, ExecLocation.Reduce, breaksAlignment, aligner, definesMRJob );
-		}
-	}
-	
-	@Override
-	public String toString() {
-		return "Operation = PlusMult";
-	}
-	
-	public String getOpString() {
-		return (type==Lop.Type.PlusMult) ? "+*" : "-*";
-	}
-	
-	/**
-	 * Function to generate CP/Spark axpy.
-	 * 
-	 * input1: matrix1
-	 * input2: Scalar
-	 * input3: matrix2
-	 */
-	@Override
-	public String getInstructions(String input1, String input2, String input3, String output) 
-	{
-		StringBuilder sb = new StringBuilder();
-		
-		sb.append( getExecType() );
-		
-		sb.append( OPERAND_DELIMITOR );
-		sb.append( getOpString() );
-		
-		//matrix 1
-		sb.append( OPERAND_DELIMITOR );
-		sb.append( getInputs().get(0).prepInputOperand(input1) );
-		
-		//scalar
-		sb.append( OPERAND_DELIMITOR );
-		if( getExecType()==ExecType.MR )
-			sb.append( getInputs().get(1).prepScalarLabel() );
-		else
-			sb.append( getInputs().get(1).prepScalarInputOperand(input2) );
-		
-		//matrix 2
-		sb.append( OPERAND_DELIMITOR );
-		sb.append( getInputs().get(2).prepInputOperand(input3) );
-		
-		sb.append( OPERAND_DELIMITOR );
-		sb.append( prepOutputOperand(output) );
-		
-		return sb.toString();
-	}
-	
-	@Override
-	public String getInstructions(int input1, int input2, int input3, int output) {
-		return getInstructions(String.valueOf(input1), String.valueOf(input2), 
-				String.valueOf(input3), String.valueOf(output));
-	}
-}

http://git-wip-us.apache.org/repos/asf/systemml/blob/ce9e42fe/src/main/java/org/apache/sysml/lops/Ternary.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/sysml/lops/Ternary.java b/src/main/java/org/apache/sysml/lops/Ternary.java
new file mode 100644
index 0000000..be83cbd
--- /dev/null
+++ b/src/main/java/org/apache/sysml/lops/Ternary.java
@@ -0,0 +1,115 @@
+/*
+ * 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.lops;
+
+import org.apache.sysml.lops.LopProperties.ExecLocation;
+import org.apache.sysml.lops.LopProperties.ExecType;
+import org.apache.sysml.lops.compile.JobType;
+import org.apache.sysml.parser.Expression.DataType;
+import org.apache.sysml.parser.Expression.ValueType;
+
+
+/**
+ * Lop to perform Sum of a matrix with another matrix multiplied by Scalar.
+ */
+public class Ternary extends Lop 
+{
+	public enum OperationType {
+		PLUS_MULT,
+		MINUS_MULT,
+		IFELSE,
+	}
+	
+	private final OperationType _type;
+		
+	public Ternary(OperationType op, Lop input1, Lop input2, Lop input3, DataType dt, ValueType vt, ExecType et) {
+		super(Lop.Type.Ternary, dt, vt);
+		_type = op;
+		init(input1, input2, input3, et);
+	}
+
+	private void init(Lop input1, Lop input2, Lop input3, ExecType et) {
+		addInput(input1);
+		addInput(input2);
+		addInput(input3);
+		input1.addOutput(this);
+		input2.addOutput(this);
+		input3.addOutput(this);
+		
+		boolean breaksAlignment = false;
+		boolean aligner = false;
+		boolean definesMRJob = false;
+		
+		if ( et == ExecType.CP ||  et == ExecType.SPARK || et == ExecType.GPU ){
+			lps.addCompatibility(JobType.INVALID);
+			lps.setProperties( inputs, et, ExecLocation.ControlProgram, breaksAlignment, aligner, definesMRJob );
+		}
+		else if( et == ExecType.MR ) {
+			lps.addCompatibility(JobType.GMR);
+			lps.setProperties( inputs, et, ExecLocation.Reduce, breaksAlignment, aligner, definesMRJob );
+		}
+	}
+	
+	@Override
+	public String toString() {
+		return "Operation = t("+_type.name().toLowerCase()+")";
+	}
+	
+	public String getOpString() {
+		switch( _type ) {
+			case PLUS_MULT: return "+*";
+			case MINUS_MULT: return "-*";
+			case IFELSE: return "ifelse";
+		}
+		return null;
+	}
+	
+	@Override
+	public String getInstructions(String input1, String input2, String input3, String output) 
+	{
+		StringBuilder sb = new StringBuilder();
+		
+		sb.append( getExecType() );
+		
+		sb.append( OPERAND_DELIMITOR );
+		sb.append( getOpString() );
+		
+		//process three operands
+		String[] inputs = new String[]{input1, input2, input3};
+		for( int i=0; i<3; i++ ) {
+			sb.append( OPERAND_DELIMITOR );
+			if( getExecType()==ExecType.MR && getInputs().get(i).getDataType().isScalar() )
+				sb.append( getInputs().get(i).prepScalarLabel() );
+			else
+				sb.append( getInputs().get(i).prepInputOperand(inputs[i]) );
+		}
+		
+		sb.append( OPERAND_DELIMITOR );
+		sb.append( prepOutputOperand(output) );
+		
+		return sb.toString();
+	}
+	
+	@Override
+	public String getInstructions(int input1, int input2, int input3, int output) {
+		return getInstructions(String.valueOf(input1), String.valueOf(input2), 
+				String.valueOf(input3), String.valueOf(output));
+	}
+}

http://git-wip-us.apache.org/repos/asf/systemml/blob/ce9e42fe/src/main/java/org/apache/sysml/lops/compile/Dag.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/sysml/lops/compile/Dag.java b/src/main/java/org/apache/sysml/lops/compile/Dag.java
index 28db4ea..d662124 100644
--- a/src/main/java/org/apache/sysml/lops/compile/Dag.java
+++ b/src/main/java/org/apache/sysml/lops/compile/Dag.java
@@ -1402,7 +1402,7 @@ public class Dag<N extends Lop>
 								node.getInputs().get(1).getOutputParameters().getLabel(),
 								node.getOutputParameters().getLabel());
 					} 
-					else if (node.getInputs().size() == 3 || node.getType() == Type.Ternary) {
+					else if (node.getInputs().size() == 3 || node.getType() == Type.Ctable) {
 						inst_string = node.getInstructions(
 								node.getInputs().get(0).getOutputParameters().getLabel(),
 								node.getInputs().get(1).getOutputParameters().getLabel(),
@@ -3119,11 +3119,11 @@ public class Dag<N extends Lop>
 				}
 
 				return output_index;
-			} else if (inputIndices.size() == 3 || node.getType() == Type.Ternary) {
+			} else if (inputIndices.size() == 3 || node.getType() == Type.Ctable) {
 				int output_index = start_index[0];
 				start_index[0]++;
 
-				if (node.getType() == Type.Ternary ) {
+				if (node.getType() == Type.Ctable ) {
 					// in case of CTABLE_TRANSFORM_SCALAR_WEIGHT: inputIndices.get(2) would be -1
 					otherInstructionsReducer.add(node.getInstructions(
 							inputIndices.get(0), inputIndices.get(1),

http://git-wip-us.apache.org/repos/asf/systemml/blob/ce9e42fe/src/main/java/org/apache/sysml/parser/BuiltinFunctionExpression.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/sysml/parser/BuiltinFunctionExpression.java b/src/main/java/org/apache/sysml/parser/BuiltinFunctionExpression.java
index e0f4e86..3cb882c 100644
--- a/src/main/java/org/apache/sysml/parser/BuiltinFunctionExpression.java
+++ b/src/main/java/org/apache/sysml/parser/BuiltinFunctionExpression.java
@@ -548,6 +548,11 @@ public class BuiltinFunctionExpression extends DataIdentifier
 			output.setValueType(ValueType.BOOLEAN);
 			break;
 			
+		case IFELSE:
+			checkNumParameters(3);
+			setTernaryOutputProperties(output, conditional);
+			break;
+			
 		case CBIND:
 		case RBIND:
 			//scalar string append (string concatenation with \n)
@@ -1289,6 +1294,29 @@ public class BuiltinFunctionExpression extends DataIdentifier
 		output.setBlockDimensions (dims[2], dims[3]);
 	}
 	
+	private void setTernaryOutputProperties(DataIdentifier output, boolean conditional) 
+		throws LanguageException 
+	{
+		DataType dt1 = getFirstExpr().getOutput().getDataType();
+		DataType dt2 = getSecondExpr().getOutput().getDataType();
+		DataType dt3 = getThirdExpr().getOutput().getDataType();
+		DataType dtOut = (dt1.isMatrix() || dt2.isMatrix() || dt3.isMatrix()) ?
+			DataType.MATRIX : DataType.SCALAR;
+		if( dt1==DataType.MATRIX && dt2==DataType.MATRIX )
+			checkMatchingDimensions(getFirstExpr(), getSecondExpr(), false, conditional);
+		if( dt1==DataType.MATRIX && dt3==DataType.MATRIX )
+			checkMatchingDimensions(getFirstExpr(), getThirdExpr(), false, conditional);
+		if( dt2==DataType.MATRIX && dt3==DataType.MATRIX )
+			checkMatchingDimensions(getSecondExpr(), getThirdExpr(), false, conditional);
+		long[] dims1 = getBinaryMatrixCharacteristics(getFirstExpr(), getSecondExpr());
+		long[] dims2 = getBinaryMatrixCharacteristics(getSecondExpr(), getThirdExpr());
+		output.setDataType(dtOut);
+		output.setValueType(dtOut==DataType.MATRIX ? ValueType.DOUBLE :
+			computeValueType(getSecondExpr(), getThirdExpr(), true));
+		output.setDimensions(Math.max(dims1[0], dims2[0]), Math.max(dims1[1], dims2[1]));
+		output.setBlockDimensions (Math.max(dims1[2], dims2[2]), Math.max(dims1[3], dims2[3]));
+	}
+	
 	private void expandArguments() {
 	
 		if ( _args == null ) {
@@ -1517,13 +1545,15 @@ public class BuiltinFunctionExpression extends DataIdentifier
 		}
 	}
 
-	private void checkMatchingDimensions(Expression expr1, Expression expr2) 
-		throws LanguageException 
-	{
+	private void checkMatchingDimensions(Expression expr1, Expression expr2) throws LanguageException {
 		checkMatchingDimensions(expr1, expr2, false);
 	}
 	
-	private void checkMatchingDimensions(Expression expr1, Expression expr2, boolean allowsMV) 
+	private void checkMatchingDimensions(Expression expr1, Expression expr2, boolean allowsMV) throws LanguageException {
+		checkMatchingDimensions(expr1, expr2, allowsMV, false);
+	}
+	
+	private void checkMatchingDimensions(Expression expr1, Expression expr2, boolean allowsMV, boolean conditional) 
 		throws LanguageException 
 	{
 		if (expr1 != null && expr2 != null) {
@@ -1540,7 +1570,7 @@ public class BuiltinFunctionExpression extends DataIdentifier
 				  || (allowsMV && expr1.getOutput().getDim2() != expr2.getOutput().getDim2() && expr2.getOutput().getDim2() != 1) ) 
 			{
 				raiseValidateError("Mismatch in matrix dimensions of parameters for function "
-						+ this.getOpCode(), false, LanguageErrorCodes.INVALID_PARAMETERS);
+						+ this.getOpCode(), conditional, LanguageErrorCodes.INVALID_PARAMETERS);
 			}
 		}
 	}
@@ -1757,6 +1787,8 @@ public class BuiltinFunctionExpression extends DataIdentifier
 			bifop = Expression.BuiltinFunctionOp.BITWISE_SHIFTL;
 		else if ( functionName.equals("bitwShiftR") )
 			bifop = Expression.BuiltinFunctionOp.BITWISE_SHIFTR;
+		else if ( functionName.equals("ifelse") )
+			bifop = Expression.BuiltinFunctionOp.IFELSE;
 		else
 			return null;
 		

http://git-wip-us.apache.org/repos/asf/systemml/blob/ce9e42fe/src/main/java/org/apache/sysml/parser/DMLTranslator.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/sysml/parser/DMLTranslator.java b/src/main/java/org/apache/sysml/parser/DMLTranslator.java
index 76ea23d..4422147 100644
--- a/src/main/java/org/apache/sysml/parser/DMLTranslator.java
+++ b/src/main/java/org/apache/sysml/parser/DMLTranslator.java
@@ -2881,8 +2881,13 @@ public class DMLTranslator
 				currBuiltinOp=new BinaryOp(target.getName(), target.getDataType(), target.getValueType(), 
 					Hop.OpOp2.MEDIAN, expr, expr2);
 			}
-			break;	
+			break;
 		
+		case IFELSE:
+			currBuiltinOp=new TernaryOp(target.getName(), target.getDataType(), target.getValueType(), 
+				Hop.OpOp3.IFELSE, expr, expr2, expr3);
+			break;
+			
 		case SEQ:
 			HashMap<String,Hop> randParams = new HashMap<>();
 			randParams.put(Statement.SEQ_FROM, expr);

http://git-wip-us.apache.org/repos/asf/systemml/blob/ce9e42fe/src/main/java/org/apache/sysml/parser/Expression.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/sysml/parser/Expression.java b/src/main/java/org/apache/sysml/parser/Expression.java
index 19d4d3e..8263206 100644
--- a/src/main/java/org/apache/sysml/parser/Expression.java
+++ b/src/main/java/org/apache/sysml/parser/Expression.java
@@ -90,6 +90,7 @@ public abstract class Expression implements ParseInfo
 		MAX_POOL, AVG_POOL, MAX_POOL_BACKWARD,
 		EXP,
 		FLOOR,
+		IFELSE,
 		INTERQUANTILE,
 		INVERSE,
 		IQM,

http://git-wip-us.apache.org/repos/asf/systemml/blob/ce9e42fe/src/main/java/org/apache/sysml/runtime/functionobjects/IfElse.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/sysml/runtime/functionobjects/IfElse.java b/src/main/java/org/apache/sysml/runtime/functionobjects/IfElse.java
new file mode 100644
index 0000000..da6e04e
--- /dev/null
+++ b/src/main/java/org/apache/sysml/runtime/functionobjects/IfElse.java
@@ -0,0 +1,44 @@
+/*
+ * 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.functionobjects;
+
+import java.io.Serializable;
+
+public class IfElse extends TernaryValueFunction implements Serializable
+{
+	private static final long serialVersionUID = -8660124936856173978L;
+	
+	private static IfElse singleObj = null;
+
+	private IfElse() {
+		// nothing to do here
+	}
+
+	public static IfElse getFnObject() {
+		if ( singleObj == null )
+			singleObj = new IfElse();
+		return singleObj;
+	}
+	
+	@Override
+	public double execute(double in1, double in2, double in3) {
+		return (in1 != 0) ? in2 : in3;
+	}
+}

http://git-wip-us.apache.org/repos/asf/systemml/blob/ce9e42fe/src/main/java/org/apache/sysml/runtime/functionobjects/MinusMultiply.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/sysml/runtime/functionobjects/MinusMultiply.java b/src/main/java/org/apache/sysml/runtime/functionobjects/MinusMultiply.java
index 9a98194..794571f 100644
--- a/src/main/java/org/apache/sysml/runtime/functionobjects/MinusMultiply.java
+++ b/src/main/java/org/apache/sysml/runtime/functionobjects/MinusMultiply.java
@@ -21,27 +21,24 @@ package org.apache.sysml.runtime.functionobjects;
 
 import java.io.Serializable;
 
-public class MinusMultiply extends ValueFunctionWithConstant implements Serializable
+public class MinusMultiply extends TernaryValueFunction implements Serializable
 {
 	private static final long serialVersionUID = 2801982061205871665L;
 	
+	private static MinusMultiply singleObj = null;
+
 	private MinusMultiply() {
 		// nothing to do here
 	}
 
-	public static MinusMultiply getMinusMultiplyFnObject() {
-		//create new object as the constant is modified and hence 
-		//cannot be shared across multiple threads (e.g., in parfor)
-		return new MinusMultiply();
-	}
-	
-	@Override
-	public double execute(double in1, double in2) {
-		return in1 - _constant * in2;	
+	public static MinusMultiply getFnObject() {
+		if ( singleObj == null )
+			singleObj = new MinusMultiply();
+		return singleObj;
 	}
 	
 	@Override
-	public double execute(long in1, long in2) {
-		return in1 - _constant * in2;	
+	public double execute(double in1, double in2, double in3) {
+		return in1 - in2 * in3;
 	}
 }

http://git-wip-us.apache.org/repos/asf/systemml/blob/ce9e42fe/src/main/java/org/apache/sysml/runtime/functionobjects/PlusMultiply.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/sysml/runtime/functionobjects/PlusMultiply.java b/src/main/java/org/apache/sysml/runtime/functionobjects/PlusMultiply.java
index 5f9b6e7..cb821f5 100644
--- a/src/main/java/org/apache/sysml/runtime/functionobjects/PlusMultiply.java
+++ b/src/main/java/org/apache/sysml/runtime/functionobjects/PlusMultiply.java
@@ -21,27 +21,24 @@ package org.apache.sysml.runtime.functionobjects;
 
 import java.io.Serializable;
 
-public class PlusMultiply extends ValueFunctionWithConstant implements Serializable
+public class PlusMultiply extends TernaryValueFunction implements Serializable
 {
 	private static final long serialVersionUID = 2801982061205871665L;
-	
+
+	private static PlusMultiply singleObj = null;
+
 	private PlusMultiply() {
 		// nothing to do here
 	}
 
-	public static PlusMultiply getPlusMultiplyFnObject() {
-		//create new object as the constant is modified and hence 
-		//cannot be shared across multiple threads (e.g., in parfor)
-		return new PlusMultiply();
-	}
-	
-	@Override
-	public double execute(double in1, double in2) {
-		return in1 + _constant * in2;	
+	public static PlusMultiply getFnObject() {
+		if ( singleObj == null )
+			singleObj = new PlusMultiply();
+		return singleObj;
 	}
 	
 	@Override
-	public double execute(long in1, long in2) {
-		return in1 + _constant * in2;	
+	public double execute(double in1, double in2, double in3) {
+		return in1 + in2 * in3;
 	}
 }

http://git-wip-us.apache.org/repos/asf/systemml/blob/ce9e42fe/src/main/java/org/apache/sysml/runtime/functionobjects/TernaryValueFunction.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/sysml/runtime/functionobjects/TernaryValueFunction.java b/src/main/java/org/apache/sysml/runtime/functionobjects/TernaryValueFunction.java
new file mode 100644
index 0000000..c317010
--- /dev/null
+++ b/src/main/java/org/apache/sysml/runtime/functionobjects/TernaryValueFunction.java
@@ -0,0 +1,32 @@
+/*
+ * 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.functionobjects;
+
+import java.io.Serializable;
+
+import org.apache.sysml.runtime.DMLRuntimeException;
+
+public abstract class TernaryValueFunction extends ValueFunction implements Serializable
+{
+	private static final long serialVersionUID = 4837616587192612216L;
+	
+	public abstract double execute ( double in1, double in2, double in3 )
+		throws DMLRuntimeException;
+}

http://git-wip-us.apache.org/repos/asf/systemml/blob/ce9e42fe/src/main/java/org/apache/sysml/runtime/functionobjects/ValueFunctionWithConstant.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/sysml/runtime/functionobjects/ValueFunctionWithConstant.java b/src/main/java/org/apache/sysml/runtime/functionobjects/ValueFunctionWithConstant.java
deleted file mode 100644
index f23c29a..0000000
--- a/src/main/java/org/apache/sysml/runtime/functionobjects/ValueFunctionWithConstant.java
+++ /dev/null
@@ -1,36 +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.functionobjects;
-
-import java.io.Serializable;
-
-public abstract class ValueFunctionWithConstant extends ValueFunction implements Serializable
-{
-	private static final long serialVersionUID = -4985988545393861058L;
-	protected double _constant;
-	
-	public void setConstant(double constant) {
-		_constant = constant;
-	}
-	
-	public double getConstant() {
-		return _constant;
-	}
-}

http://git-wip-us.apache.org/repos/asf/systemml/blob/ce9e42fe/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 6b2e3d6..4928ac3 100644
--- a/src/main/java/org/apache/sysml/runtime/instructions/CPInstructionParser.java
+++ b/src/main/java/org/apache/sysml/runtime/instructions/CPInstructionParser.java
@@ -52,7 +52,7 @@ import org.apache.sysml.runtime.instructions.cp.MultiReturnBuiltinCPInstruction;
 import org.apache.sysml.runtime.instructions.cp.MultiReturnParameterizedBuiltinCPInstruction;
 import org.apache.sysml.runtime.instructions.cp.PMMJCPInstruction;
 import org.apache.sysml.runtime.instructions.cp.ParameterizedBuiltinCPInstruction;
-import org.apache.sysml.runtime.instructions.cp.PlusMultCPInstruction;
+import org.apache.sysml.runtime.instructions.cp.TernaryCPInstruction;
 import org.apache.sysml.runtime.instructions.cp.QuantilePickCPInstruction;
 import org.apache.sysml.runtime.instructions.cp.QuantileSortCPInstruction;
 import org.apache.sysml.runtime.instructions.cp.QuaternaryCPInstruction;
@@ -122,8 +122,6 @@ public class CPInstructionParser extends InstructionParser
 		String2CPInstructionType.put( "^2"   , CPType.Binary); //special ^ case
 		String2CPInstructionType.put( "*2"   , CPType.Binary); //special * case
 		String2CPInstructionType.put( "-nz"  , CPType.Binary); //special - case
-		String2CPInstructionType.put( "+*"   , CPType.Binary);
-		String2CPInstructionType.put( "-*"   , CPType.Binary);
 		
 		// Boolean Instruction Opcodes 
 		String2CPInstructionType.put( "&&"   , CPType.Binary);
@@ -198,6 +196,11 @@ public class CPInstructionParser extends InstructionParser
 		String2CPInstructionType.put( "transformmeta",CPType.ParameterizedBuiltin);
 		String2CPInstructionType.put( "transformencode",CPType.MultiReturnParameterizedBuiltin);
 		
+		// Ternary Instruction Opcodes
+		String2CPInstructionType.put( "+*",      CPType.Ternary);
+		String2CPInstructionType.put( "-*",      CPType.Ternary);
+		String2CPInstructionType.put( "ifelse",  CPType.Ternary);
+		
 		// Variable Instruction Opcodes 
 		String2CPInstructionType.put( "assignvar"   , CPType.Variable);
 		String2CPInstructionType.put( "cpvar"    	, CPType.Variable);
@@ -319,26 +322,25 @@ public class CPInstructionParser extends InstructionParser
 	
 			case AggregateTernary:
 				return AggregateTernaryCPInstruction.parseInstruction(str);
-				
+			
+			case Unary:
+				return UnaryCPInstruction.parseInstruction(str);
+			
 			case Binary:
-				String opcode = InstructionUtils.getOpCode(str);
-				if( opcode.equals("+*") || opcode.equals("-*")  )
-					return PlusMultCPInstruction.parseInstruction(str);
-				else
-					return BinaryCPInstruction.parseInstruction(str);
+				return BinaryCPInstruction.parseInstruction(str);
 			
-			case Ctable:
-				return CtableCPInstruction.parseInstruction(str);
+			case Ternary:
+				return TernaryCPInstruction.parseInstruction(str);
 			
 			case Quaternary:
 				return QuaternaryCPInstruction.parseInstruction(str);
-				
-			case Unary:
-				return UnaryCPInstruction.parseInstruction(str);
 			
 			case BuiltinNary:
 				return BuiltinNaryCPInstruction.parseInstruction(str);
 			
+			case Ctable:
+				return CtableCPInstruction.parseInstruction(str);
+			
 			case Reorg:
 				return ReorgCPInstruction.parseInstruction(str);
 				

http://git-wip-us.apache.org/repos/asf/systemml/blob/ce9e42fe/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 e6affa0..85a45ff 100644
--- a/src/main/java/org/apache/sysml/runtime/instructions/InstructionUtils.java
+++ b/src/main/java/org/apache/sysml/runtime/instructions/InstructionUtils.java
@@ -54,6 +54,7 @@ import org.apache.sysml.runtime.functionobjects.Divide;
 import org.apache.sysml.runtime.functionobjects.Equals;
 import org.apache.sysml.runtime.functionobjects.GreaterThan;
 import org.apache.sysml.runtime.functionobjects.GreaterThanEquals;
+import org.apache.sysml.runtime.functionobjects.IfElse;
 import org.apache.sysml.runtime.functionobjects.IndexFunction;
 import org.apache.sysml.runtime.functionobjects.IntegerDivide;
 import org.apache.sysml.runtime.functionobjects.KahanPlus;
@@ -95,6 +96,7 @@ import org.apache.sysml.runtime.matrix.operators.LeftScalarOperator;
 import org.apache.sysml.runtime.matrix.operators.Operator;
 import org.apache.sysml.runtime.matrix.operators.RightScalarOperator;
 import org.apache.sysml.runtime.matrix.operators.ScalarOperator;
+import org.apache.sysml.runtime.matrix.operators.TernaryOperator;
 import org.apache.sysml.runtime.matrix.operators.UnaryOperator;
 
 
@@ -580,14 +582,15 @@ public class InstructionUtils
 			return new BinaryOperator(Builtin.getBuiltinFnObject("max"));
 		else if ( opcode.equalsIgnoreCase("min") ) 
 			return new BinaryOperator(Builtin.getBuiltinFnObject("min"));
-		else if ( opcode.equalsIgnoreCase("+*") )
-			return new BinaryOperator(PlusMultiply.getPlusMultiplyFnObject());
-		else if ( opcode.equalsIgnoreCase("-*") )
-			return new BinaryOperator(MinusMultiply.getMinusMultiplyFnObject());
 		
 		throw new DMLRuntimeException("Unknown binary opcode " + opcode);
 	}
 	
+	public static TernaryOperator parseTernaryOperator(String opcode) {
+		return new TernaryOperator(opcode.equals("+*") ? PlusMultiply.getFnObject() :
+			opcode.equals("-*") ? MinusMultiply.getFnObject() : IfElse.getFnObject());
+	}
+	
 	/**
 	 * scalar-matrix operator
 	 * 

http://git-wip-us.apache.org/repos/asf/systemml/blob/ce9e42fe/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 fae93f3..6777d51 100644
--- a/src/main/java/org/apache/sysml/runtime/instructions/MRInstructionParser.java
+++ b/src/main/java/org/apache/sysml/runtime/instructions/MRInstructionParser.java
@@ -64,7 +64,7 @@ import org.apache.sysml.runtime.instructions.mr.MatrixReshapeMRInstruction;
 import org.apache.sysml.runtime.instructions.mr.PMMJMRInstruction;
 import org.apache.sysml.runtime.instructions.mr.ParameterizedBuiltinMRInstruction;
 import org.apache.sysml.runtime.instructions.mr.PickByCountInstruction;
-import org.apache.sysml.runtime.instructions.mr.PlusMultInstruction;
+import org.apache.sysml.runtime.instructions.mr.TernaryInstruction;
 import org.apache.sysml.runtime.instructions.mr.QuaternaryInstruction;
 import org.apache.sysml.runtime.instructions.mr.RandInstruction;
 import org.apache.sysml.runtime.instructions.mr.RangeBasedReIndexInstruction;
@@ -192,8 +192,6 @@ public class MRInstructionParser extends InstructionParser
 		String2MRInstructionType.put( "bitwXor", MRType.Binary);
 		String2MRInstructionType.put( "bitwShiftL", MRType.Binary);
 		String2MRInstructionType.put( "bitwShiftR", MRType.Binary);
-		String2MRInstructionType.put( "+*"   , MRType.Binary2); 
-		String2MRInstructionType.put( "-*"   , MRType.Binary2); 
 		
 		String2MRInstructionType.put( "map+"    , MRType.Binary);
 		String2MRInstructionType.put( "map-"    , MRType.Binary);
@@ -220,6 +218,11 @@ public class MRInstructionParser extends InstructionParser
 		String2MRInstructionType.put( "mapbitwShiftL", MRType.Binary);
 		String2MRInstructionType.put( "mapbitwShiftR", MRType.Binary);
 		
+		// Ternary Instruction Opcodes
+		String2MRInstructionType.put( "+*",     MRType.Ternary); 
+		String2MRInstructionType.put( "-*",     MRType.Ternary); 
+		String2MRInstructionType.put( "ifelse", MRType.Ternary); 
+		
 		String2MRInstructionType.put( "uaggouterchain", MRType.UaggOuterChain);
 		
 		// REORG Instruction Opcodes 
@@ -353,8 +356,8 @@ public class MRInstructionParser extends InstructionParser
 				}
 			}
 			
-			case Binary2:
-				return PlusMultInstruction.parseInstruction(str);
+			case Ternary:
+				return TernaryInstruction.parseInstruction(str);
 			
 			case AggregateBinary:
 				return AggregateBinaryInstruction.parseInstruction(str);

http://git-wip-us.apache.org/repos/asf/systemml/blob/ce9e42fe/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 3098c10..a9155c9 100644
--- a/src/main/java/org/apache/sysml/runtime/instructions/SPInstructionParser.java
+++ b/src/main/java/org/apache/sysml/runtime/instructions/SPInstructionParser.java
@@ -63,7 +63,7 @@ import org.apache.sysml.runtime.instructions.spark.MatrixReshapeSPInstruction;
 import org.apache.sysml.runtime.instructions.spark.MultiReturnParameterizedBuiltinSPInstruction;
 import org.apache.sysml.runtime.instructions.spark.PMapmmSPInstruction;
 import org.apache.sysml.runtime.instructions.spark.ParameterizedBuiltinSPInstruction;
-import org.apache.sysml.runtime.instructions.spark.PlusMultSPInstruction;
+import org.apache.sysml.runtime.instructions.spark.TernarySPInstruction;
 import org.apache.sysml.runtime.instructions.spark.PmmSPInstruction;
 import org.apache.sysml.runtime.instructions.spark.QuantilePickSPInstruction;
 import org.apache.sysml.runtime.instructions.spark.QuaternarySPInstruction;
@@ -162,8 +162,6 @@ public class SPInstructionParser extends InstructionParser
 		String2SPInstructionType.put( "^"    , SPType.Binary);
 		String2SPInstructionType.put( "^2"   , SPType.Binary);
 		String2SPInstructionType.put( "*2"   , SPType.Binary);
-		String2SPInstructionType.put( "+*"   , SPType.Binary);
-		String2SPInstructionType.put( "-*"   , SPType.Binary);
 		String2SPInstructionType.put( "map+"    , SPType.Binary);
 		String2SPInstructionType.put( "map-"    , SPType.Binary);
 		String2SPInstructionType.put( "map*"    , SPType.Binary);
@@ -271,6 +269,11 @@ public class SPInstructionParser extends InstructionParser
 		String2SPInstructionType.put( "ctable", SPType.Ctable);
 		String2SPInstructionType.put( "ctableexpand", SPType.Ctable);
 		
+		//ternary instruction opcodes
+		String2SPInstructionType.put( "+*",     SPType.Ternary);
+		String2SPInstructionType.put( "-*",     SPType.Ternary);
+		String2SPInstructionType.put( "ifelse", SPType.Ternary);
+		
 		//quaternary instruction opcodes
 		String2SPInstructionType.put( WeightedSquaredLoss.OPCODE,  SPType.Quaternary);
 		String2SPInstructionType.put( WeightedSquaredLossR.OPCODE, SPType.Quaternary);
@@ -374,12 +377,11 @@ public class SPInstructionParser extends InstructionParser
 				return ReorgSPInstruction.parseInstruction(str);
 				
 			case Binary:
-				String opcode = InstructionUtils.getOpCode(str);
-				if( opcode.equals("+*") || opcode.equals("-*")  )
-					return PlusMultSPInstruction.parseInstruction(str);
-				else
-					return BinarySPInstruction.parseInstruction(str);
-				
+				return BinarySPInstruction.parseInstruction(str);
+			
+			case Ternary:
+				return TernarySPInstruction.parseInstruction(str);
+			
 			//ternary instructions
 			case Ctable:
 				return CtableSPInstruction.parseInstruction(str);

http://git-wip-us.apache.org/repos/asf/systemml/blob/ce9e42fe/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 6b86029..7a2053b 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,7 +31,7 @@ public abstract class CPInstruction extends Instruction
 {
 	public enum CPType {
 		AggregateUnary, AggregateBinary, AggregateTernary,
-		Unary, Binary, Ctable, Quaternary, BuiltinNary,
+		Unary, Binary, Ternary, Quaternary, BuiltinNary, Ctable, 
 		MultiReturnParameterizedBuiltin, ParameterizedBuiltin, MultiReturnBuiltin,
 		Builtin, Reorg, Variable, External, Append, Rand, QSort, QPick,
 		MatrixIndexing, MMTSJ, PMMJ, MMChain, MatrixReshape, Partition, Compression, SpoofFused,

http://git-wip-us.apache.org/repos/asf/systemml/blob/ce9e42fe/src/main/java/org/apache/sysml/runtime/instructions/cp/CPOperand.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/sysml/runtime/instructions/cp/CPOperand.java b/src/main/java/org/apache/sysml/runtime/instructions/cp/CPOperand.java
index c39b617..2f9b64d 100644
--- a/src/main/java/org/apache/sysml/runtime/instructions/cp/CPOperand.java
+++ b/src/main/java/org/apache/sysml/runtime/instructions/cp/CPOperand.java
@@ -65,6 +65,10 @@ public class CPOperand
 		return _dataType;
 	}
 	
+	public boolean isMatrix() {
+		return _dataType.isMatrix();
+	}
+	
 	public boolean isLiteral() {
 		return _isLiteral;
 	}
@@ -87,6 +91,13 @@ public class CPOperand
 			_valueType = ValueType.valueOf(opr[2]);
 			_isLiteral = false;
 		}
+		else if ( opr.length == 1 ) {
+			//note: for literals in MR instructions
+			_name = opr[0];
+			_dataType = DataType.SCALAR;
+			_valueType = ValueType.DOUBLE;
+			_isLiteral = true;
+		}
 		else {
 			_name = opr[0];
 			_valueType = ValueType.valueOf(opr[1]);

http://git-wip-us.apache.org/repos/asf/systemml/blob/ce9e42fe/src/main/java/org/apache/sysml/runtime/instructions/cp/PlusMultCPInstruction.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/sysml/runtime/instructions/cp/PlusMultCPInstruction.java b/src/main/java/org/apache/sysml/runtime/instructions/cp/PlusMultCPInstruction.java
deleted file mode 100644
index c74786c..0000000
--- a/src/main/java/org/apache/sysml/runtime/instructions/cp/PlusMultCPInstruction.java
+++ /dev/null
@@ -1,72 +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.functionobjects.MinusMultiply;
-import org.apache.sysml.runtime.functionobjects.PlusMultiply;
-import org.apache.sysml.runtime.functionobjects.ValueFunctionWithConstant;
-import org.apache.sysml.runtime.instructions.InstructionUtils;
-import org.apache.sysml.runtime.matrix.data.MatrixBlock;
-import org.apache.sysml.runtime.matrix.operators.BinaryOperator;
-
-public class PlusMultCPInstruction extends ComputationCPInstruction {
-	
-	private PlusMultCPInstruction(BinaryOperator op, CPOperand in1, CPOperand in2, CPOperand in3, CPOperand out,
-			String opcode, String str) {
-		super(CPType.Binary, op, in1, in2, in3, out, opcode, str);
-	}
-
-	public static PlusMultCPInstruction parseInstruction(String str)
-	{
-		String[] parts = InstructionUtils.getInstructionPartsWithValueType(str);
-		String opcode=parts[0];
-		CPOperand operand1 = new CPOperand(parts[1]);
-		CPOperand operand2 = new CPOperand(parts[3]); //put the second matrix (parts[3]) in Operand2 to make using Binary matrix operations easier
-		CPOperand operand3 = new CPOperand(parts[2]); 
-		CPOperand outOperand = new CPOperand(parts[4]);
-		BinaryOperator bOperator = new BinaryOperator(opcode.equals("+*") ? 
-				PlusMultiply.getPlusMultiplyFnObject():MinusMultiply.getMinusMultiplyFnObject());
-		return new PlusMultCPInstruction(bOperator,operand1, operand2, operand3, outOperand, opcode,str);
-	}
-	
-	@Override
-	public void processInstruction( ExecutionContext ec )
-		throws DMLRuntimeException
-	{		
-		String output_name = output.getName();
-
-		//get all the inputs
-		MatrixBlock matrix1 = ec.getMatrixInput(input1.getName(), getExtendedOpcode());
-		MatrixBlock matrix2 = ec.getMatrixInput(input2.getName(), getExtendedOpcode());
-		ScalarObject scalar = ec.getScalarInput(input3.getName(), input3.getValueType(), input3.isLiteral()); 
-		
-		//execution
-		((ValueFunctionWithConstant) ((BinaryOperator)_optr).fn).setConstant(scalar.getDoubleValue());
-		MatrixBlock out = (MatrixBlock) matrix1.binaryOperations((BinaryOperator) _optr, matrix2, new MatrixBlock());
-		
-		//release the matrices
-		ec.releaseMatrixInput(input1.getName(), getExtendedOpcode());
-		ec.releaseMatrixInput(input2.getName(), getExtendedOpcode());
-		
-		ec.setMatrixOutput(output_name, out, getExtendedOpcode());
-	}
-}

http://git-wip-us.apache.org/repos/asf/systemml/blob/ce9e42fe/src/main/java/org/apache/sysml/runtime/instructions/cp/ScalarObjectFactory.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/sysml/runtime/instructions/cp/ScalarObjectFactory.java b/src/main/java/org/apache/sysml/runtime/instructions/cp/ScalarObjectFactory.java
index ea2c169..ffad8ff 100644
--- a/src/main/java/org/apache/sysml/runtime/instructions/cp/ScalarObjectFactory.java
+++ b/src/main/java/org/apache/sysml/runtime/instructions/cp/ScalarObjectFactory.java
@@ -46,6 +46,16 @@ public abstract class ScalarObjectFactory
 		}
 	}
 	
+	public static ScalarObject createScalarObject(ValueType vt, double value) {
+		switch( vt ) {
+			case INT:     return new IntObject(UtilFunctions.toLong(value));
+			case DOUBLE:  return new DoubleObject(value);
+			case BOOLEAN: return new BooleanObject(value != 0);
+			case STRING:  return new StringObject(String.valueOf(value));
+			default: throw new RuntimeException("Unsupported scalar value type: "+vt.name());
+		}
+	}
+	
 	public static ScalarObject createScalarObject(ValueType vt, ScalarObject so) {
 		switch( vt ) {
 			case DOUBLE:  return new DoubleObject(so.getDoubleValue());

http://git-wip-us.apache.org/repos/asf/systemml/blob/ce9e42fe/src/main/java/org/apache/sysml/runtime/instructions/cp/TernaryCPInstruction.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/sysml/runtime/instructions/cp/TernaryCPInstruction.java b/src/main/java/org/apache/sysml/runtime/instructions/cp/TernaryCPInstruction.java
new file mode 100644
index 0000000..4f8aeb0
--- /dev/null
+++ b/src/main/java/org/apache/sysml/runtime/instructions/cp/TernaryCPInstruction.java
@@ -0,0 +1,81 @@
+/*
+ * 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.instructions.InstructionUtils;
+import org.apache.sysml.runtime.matrix.data.MatrixBlock;
+import org.apache.sysml.runtime.matrix.operators.TernaryOperator;
+
+public class TernaryCPInstruction extends ComputationCPInstruction {
+	
+	private TernaryCPInstruction(TernaryOperator op, CPOperand in1, CPOperand in2, CPOperand in3, CPOperand out, String opcode, String str) {
+		super(CPType.Ternary, op, in1, in2, in3, out, opcode, str);
+	}
+
+	public static TernaryCPInstruction parseInstruction(String str)
+	{
+		String[] parts = InstructionUtils.getInstructionPartsWithValueType(str);
+		String opcode=parts[0];
+		CPOperand operand1 = new CPOperand(parts[1]);
+		CPOperand operand2 = new CPOperand(parts[2]);
+		CPOperand operand3 = new CPOperand(parts[3]);
+		CPOperand outOperand = new CPOperand(parts[4]);
+		TernaryOperator op = InstructionUtils.parseTernaryOperator(opcode);
+		return new TernaryCPInstruction(op, operand1, operand2, operand3, outOperand, opcode,str);
+	}
+	
+	@Override
+	public void processInstruction( ExecutionContext ec )
+		throws DMLRuntimeException
+	{
+		if( input1.isMatrix() || input2.isMatrix() || input3.isMatrix() )
+		{
+			//get all inputs as matrix blocks
+			MatrixBlock m1 = input1.isMatrix() ? ec.getMatrixInput(input1.getName()) :
+				new MatrixBlock(ec.getScalarInput(input1).getDoubleValue());
+			MatrixBlock m2 = input2.isMatrix() ? ec.getMatrixInput(input2.getName()) :
+				new MatrixBlock(ec.getScalarInput(input2).getDoubleValue());
+			MatrixBlock m3 = input3.isMatrix() ? ec.getMatrixInput(input3.getName()) :
+				new MatrixBlock(ec.getScalarInput(input3).getDoubleValue());
+			
+			//execution
+			MatrixBlock out = m1.ternaryOperations((TernaryOperator)_optr, m2, m3, new MatrixBlock());
+			
+			//release the inputs and output
+			if( input1.isMatrix() )
+				ec.releaseMatrixInput(input1.getName());
+			if( input2.isMatrix() )
+				ec.releaseMatrixInput(input2.getName());
+			if( input3.isMatrix() )
+				ec.releaseMatrixInput(input3.getName());
+			ec.setMatrixOutput(output.getName(), out);
+		}
+		else { //SCALARS
+			double value = ((TernaryOperator)_optr).fn.execute(
+				ec.getScalarInput(input1).getDoubleValue(),
+				ec.getScalarInput(input2).getDoubleValue(),
+				ec.getScalarInput(input3).getDoubleValue());
+			ec.setScalarOutput(output.getName(), ScalarObjectFactory
+				.createScalarObject(output.getValueType(), value));
+		}
+	}
+}

http://git-wip-us.apache.org/repos/asf/systemml/blob/ce9e42fe/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 355ccb4..0cc105c 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, Binary, Binary2, AggregateBinary, AggregateUnary, Rand,
+		Append, Aggregate, Binary, Ternary, AggregateBinary, AggregateUnary, Rand,
 		Seq, CSVReblock, CSVWrite, Reblock, Reorg, Replicate, Unary, CombineBinary, CombineUnary, CombineTernary,
 		PickByCount, Partition, Ctable, 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/ce9e42fe/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
deleted file mode 100644
index ff9a426..0000000
--- a/src/main/java/org/apache/sysml/runtime/instructions/mr/PlusMultInstruction.java
+++ /dev/null
@@ -1,61 +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.mr;
-
-import org.apache.sysml.runtime.DMLRuntimeException;
-import org.apache.sysml.runtime.functionobjects.ValueFunctionWithConstant;
-import org.apache.sysml.runtime.instructions.InstructionUtils;
-import org.apache.sysml.runtime.matrix.data.MatrixValue;
-import org.apache.sysml.runtime.matrix.mapred.CachedValueMap;
-import org.apache.sysml.runtime.matrix.mapred.IndexedMatrixValue;
-import org.apache.sysml.runtime.matrix.operators.BinaryOperator;
-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.Binary2, op, in1, in2, out, istr);
-	}
-
-	public static PlusMultInstruction parseInstruction ( String str ) 
-		throws DMLRuntimeException 
-	{	
-		InstructionUtils.checkNumFields ( str, 4 );
-		
-		String[] parts = InstructionUtils.getInstructionParts ( str );
-		String opcode = parts[0];
-		byte in1 = Byte.parseByte(parts[1]);
-		double scalar = Double.parseDouble(parts[2]);
-		byte in2 = Byte.parseByte(parts[3]);
-		byte out = Byte.parseByte(parts[4]);
-		
-		BinaryOperator bop = InstructionUtils.parseBinaryOperator(opcode);
-		((ValueFunctionWithConstant) bop.fn).setConstant(scalar);
-		return new PlusMultInstruction(bop, in1, in2, out, str);
-	}
-	
-	@Override
-	public void processInstruction(Class<? extends MatrixValue> valueClass, CachedValueMap cachedValues, 
-			IndexedMatrixValue tempValue, IndexedMatrixValue zeroInput, int blockRowFactor, int blockColFactor)
-		throws DMLRuntimeException 
-	{
-		//default binary mr instruction execution (custom logic encoded in operator)
-		super.processInstruction(valueClass, cachedValues, tempValue, zeroInput, blockRowFactor, blockColFactor);
-	}
-}

http://git-wip-us.apache.org/repos/asf/systemml/blob/ce9e42fe/src/main/java/org/apache/sysml/runtime/instructions/mr/TernaryInstruction.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/sysml/runtime/instructions/mr/TernaryInstruction.java b/src/main/java/org/apache/sysml/runtime/instructions/mr/TernaryInstruction.java
new file mode 100644
index 0000000..d23c826
--- /dev/null
+++ b/src/main/java/org/apache/sysml/runtime/instructions/mr/TernaryInstruction.java
@@ -0,0 +1,105 @@
+/*
+ * 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.mr;
+
+import java.util.Arrays;
+
+import org.apache.commons.lang3.ArrayUtils;
+import org.apache.sysml.runtime.DMLRuntimeException;
+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.data.MatrixValue;
+import org.apache.sysml.runtime.matrix.mapred.CachedValueMap;
+import org.apache.sysml.runtime.matrix.mapred.IndexedMatrixValue;
+import org.apache.sysml.runtime.matrix.operators.Operator;
+import org.apache.sysml.runtime.matrix.operators.TernaryOperator;
+
+public class TernaryInstruction extends MRInstruction {
+	
+	private final CPOperand input1, input2, input3, output;
+	private final byte ixinput1, ixinput2, ixinput3, ixoutput;
+	private final MatrixBlock m1, m2, m3;
+	
+	private TernaryInstruction(Operator op, CPOperand in1, CPOperand in2, CPOperand in3, CPOperand out, String istr) {
+		super(MRType.Ternary, op, Byte.parseByte(out.getName()));
+		instString = istr;
+		input1 = in1; input2 = in2; input3 = in3; output = out;
+		ixinput1 = input1.isMatrix() ? Byte.parseByte(input1.getName()) : -1;
+		ixinput2 = input2.isMatrix() ? Byte.parseByte(input2.getName()) : -1;
+		ixinput3 = input3.isMatrix() ? Byte.parseByte(input3.getName()) : -1;
+		ixoutput = output.isMatrix() ? Byte.parseByte(output.getName()) : -1;
+		m1 = input1.isMatrix() ? null :new MatrixBlock(Double.parseDouble(input1.getName()));
+		m2 = input2.isMatrix() ? null :new MatrixBlock(Double.parseDouble(input2.getName()));
+		m3 = input3.isMatrix() ? null :new MatrixBlock(Double.parseDouble(input3.getName()));
+	}
+
+	public static TernaryInstruction parseInstruction ( String str )
+		throws DMLRuntimeException
+	{
+		InstructionUtils.checkNumFields ( str, 4 );
+		String[] parts = InstructionUtils.getInstructionPartsWithValueType(str);
+		String opcode = parts[0];
+		CPOperand in1 = new CPOperand(parts[1]);
+		CPOperand in2 = new CPOperand(parts[2]);
+		CPOperand in3 = new CPOperand(parts[3]);
+		CPOperand out = new CPOperand(parts[4]);
+		TernaryOperator op = InstructionUtils.parseTernaryOperator(opcode);
+		return new TernaryInstruction(op, in1, in2, in3, out, str);
+	}
+	
+	@Override
+	public void processInstruction(Class<? extends MatrixValue> valueClass, CachedValueMap cachedValues,
+			IndexedMatrixValue tempValue, IndexedMatrixValue zeroInput, int blockRowFactor, int blockColFactor)
+		throws DMLRuntimeException 
+	{
+		MatrixBlock lm1 = input1.isMatrix() ? (MatrixBlock) cachedValues.getFirst(ixinput1).getValue() : m1;
+		MatrixBlock lm2 = input2.isMatrix() ? (MatrixBlock) cachedValues.getFirst(ixinput2).getValue() : m2;
+		MatrixBlock lm3 = input3.isMatrix() ? (MatrixBlock) cachedValues.getFirst(ixinput3).getValue() : m3;
+		MatrixIndexes ixin = input1.isMatrix() ? cachedValues.getFirst(ixinput1).getIndexes() : input2.isMatrix() ?
+			cachedValues.getFirst(ixinput2).getIndexes() : cachedValues.getFirst(ixinput3).getIndexes();
+		
+		//prepare output
+		IndexedMatrixValue out = new IndexedMatrixValue(new MatrixIndexes(), new MatrixBlock());
+		out.getIndexes().setIndexes(ixin);
+		
+		//process instruction
+		TernaryOperator op = (TernaryOperator)optr;
+		lm1.ternaryOperations(op, lm2, lm3, (MatrixBlock)out.getValue());
+		
+		//put the output value in the cache
+		cachedValues.add(ixoutput, out);
+	}
+	
+	@Override
+	public byte[] getInputIndexes() {
+		byte[] tmp = getAllIndexes();
+		return Arrays.copyOfRange(tmp, 0, tmp.length-1);
+	}
+
+	@Override
+	public byte[] getAllIndexes() {
+		return ArrayUtils.toPrimitive(
+			Arrays.stream(new CPOperand[]{input1, input2, input3, output})
+			.filter(in -> in.isMatrix()).map(in -> Byte.parseByte(in.getName()))
+			.toArray(Byte[]::new));
+	}
+}

http://git-wip-us.apache.org/repos/asf/systemml/blob/ce9e42fe/src/main/java/org/apache/sysml/runtime/instructions/spark/PlusMultSPInstruction.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/sysml/runtime/instructions/spark/PlusMultSPInstruction.java b/src/main/java/org/apache/sysml/runtime/instructions/spark/PlusMultSPInstruction.java
deleted file mode 100644
index e12cd3e..0000000
--- a/src/main/java/org/apache/sysml/runtime/instructions/spark/PlusMultSPInstruction.java
+++ /dev/null
@@ -1,71 +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.runtime.DMLRuntimeException;
-import org.apache.sysml.runtime.controlprogram.context.ExecutionContext;
-import org.apache.sysml.runtime.controlprogram.context.SparkExecutionContext;
-import org.apache.sysml.runtime.functionobjects.MinusMultiply;
-import org.apache.sysml.runtime.functionobjects.PlusMultiply;
-import org.apache.sysml.runtime.functionobjects.ValueFunctionWithConstant;
-import org.apache.sysml.runtime.instructions.InstructionUtils;
-import org.apache.sysml.runtime.instructions.cp.CPOperand;
-import org.apache.sysml.runtime.instructions.cp.ScalarObject;
-import org.apache.sysml.runtime.matrix.operators.BinaryOperator;
-
-public class PlusMultSPInstruction extends BinarySPInstruction {
-	private PlusMultSPInstruction(BinaryOperator op, CPOperand in1, CPOperand in2, CPOperand in3, CPOperand out,
-			String opcode, String str) throws DMLRuntimeException {
-		super(SPType.Binary, op, in1, in2, out, opcode, str);
-		input3 = in3;
-
-		// sanity check opcodes
-		if (!(opcode.equalsIgnoreCase("+*") || opcode.equalsIgnoreCase("-*"))) {
-			throw new DMLRuntimeException("Unknown opcode in PlusMultSPInstruction: " + toString());
-		}
-	}
-
-	public static PlusMultSPInstruction parseInstruction(String str) throws DMLRuntimeException
-	{
-		String[] parts = InstructionUtils.getInstructionPartsWithValueType(str);
-		String opcode=parts[0];
-		CPOperand operand1 = new CPOperand(parts[1]);
-		CPOperand operand2 = new CPOperand(parts[3]);	//put the second matrix (parts[3]) in Operand2 to make using Binary matrix operations easier
-		CPOperand operand3 = new CPOperand(parts[2]);
-		CPOperand outOperand = new CPOperand(parts[4]);
-		BinaryOperator bOperator = new BinaryOperator(opcode.equals("+*") ? 
-				PlusMultiply.getPlusMultiplyFnObject():MinusMultiply.getMinusMultiplyFnObject());
-		return new PlusMultSPInstruction(bOperator,operand1, operand2, operand3, outOperand, opcode,str);
-	}
-	
-	@Override
-	public void processInstruction(ExecutionContext ec) 
-		throws DMLRuntimeException
-	{
-		SparkExecutionContext sec = (SparkExecutionContext)ec;
-		
-		//pass the scalar
-		ScalarObject constant = (ScalarObject) ec.getScalarInput(input3.getName(), input3.getValueType(), input3.isLiteral());
-		((ValueFunctionWithConstant) ((BinaryOperator)_optr).fn).setConstant(constant.getDoubleValue());
-
-		super.processMatrixMatrixBinaryInstruction(sec);
-	
-	}
-}

http://git-wip-us.apache.org/repos/asf/systemml/blob/ce9e42fe/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 ef7158c..e3d8dfd 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
@@ -31,7 +31,7 @@ public abstract class SPInstruction extends Instruction {
 
 	public enum SPType { 
 		MAPMM, MAPMMCHAIN, CPMM, RMM, TSMM, TSMM2, PMM, ZIPMM, PMAPMM, //matrix multiplication instructions  
-		MatrixIndexing, Reorg, Binary,
+		MatrixIndexing, Reorg, Binary, Ternary,
 		AggregateUnary, AggregateTernary, Reblock, CSVReblock, 
 		Builtin, Unary, BuiltinNary, MultiReturnBuiltin, Checkpoint, Compression, Cast,
 		CentralMoment, Covariance, QSort, QPick, 


[3/5] systemml git commit: [MINOR] Refactoring agg-binary, ternary, and quaternary instructions

Posted by mb...@apache.org.
[MINOR] Refactoring agg-binary, ternary, and quaternary instructions

This patch makes a number of refactorings to cleanup the MatrixValue
abstraction and prepare the addition of a "real" ternary instruction
framework. So far ternary instructions were only used for ctable and are
renamed accordingly.


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

Branch: refs/heads/master
Commit: 57438103b36f5c5ac09a3b09fe8d97ae4955439a
Parents: 5b0fb0c
Author: Matthias Boehm <mb...@gmail.com>
Authored: Tue Jan 16 17:27:07 2018 -0800
Committer: Matthias Boehm <mb...@gmail.com>
Committed: Wed Jan 17 15:04:11 2018 -0800

----------------------------------------------------------------------
 A.csv                                           |   3 +
 A.csv.mtd                                       |  12 +
 B.csv                                           |   3 +
 B.csv.mtd                                       |  12 +
 functions/jmlc/temp/m.binary                    | Bin 0 -> 255 bytes
 functions/jmlc/temp/m.binary.mtd                |  12 +
 functions/jmlc/temp/m.csv                       |   4 +
 functions/jmlc/temp/m.csv.mtd                   |  12 +
 functions/jmlc/temp/m.mm                        |   8 +
 functions/jmlc/temp/m.txt                       |   6 +
 functions/jmlc/temp/m.txt.mtd                   |  10 +
 functions/jmlc/temp/scoring-example.dml         |  12 +
 functions/jmlc/temp/x.csv                       |   2 +
 functions/jmlc/temp/x.csv.mtd                   |  12 +
 .../java/org/apache/sysml/hops/TernaryOp.java   |  42 +-
 .../hops/cost/CostEstimatorStaticRuntime.java   |  10 +-
 src/main/java/org/apache/sysml/lops/Ctable.java | 354 +++++++++++++
 .../java/org/apache/sysml/lops/Ternary.java     | 354 -------------
 .../runtime/compress/CompressedMatrixBlock.java |  49 +-
 .../instructions/CPInstructionParser.java       |  10 +-
 .../instructions/MRInstructionParser.java       |  16 +-
 .../instructions/SPInstructionParser.java       |  10 +-
 .../cp/AggregateBinaryCPInstruction.java        |   2 +-
 .../runtime/instructions/cp/CPInstruction.java  |   2 +-
 .../instructions/cp/CtableCPInstruction.java    | 186 +++++++
 .../cp/QuaternaryCPInstruction.java             |   7 +-
 .../instructions/cp/TernaryCPInstruction.java   | 186 -------
 .../mr/AggregateBinaryInstruction.java          |  37 +-
 .../mr/CombineTernaryInstruction.java           |   4 +-
 .../instructions/mr/CtableInstruction.java      | 264 ++++++++++
 .../runtime/instructions/mr/MRInstruction.java  |   2 +-
 .../instructions/mr/QuaternaryInstruction.java  |   8 +-
 .../instructions/mr/TernaryInstruction.java     | 264 ----------
 .../instructions/spark/CtableSPInstruction.java | 510 +++++++++++++++++++
 .../instructions/spark/SPInstruction.java       |   2 +-
 .../spark/TernarySPInstruction.java             | 510 -------------------
 .../instructions/spark/ZipmmSPInstruction.java  |   2 +-
 .../runtime/matrix/MatrixCharacteristics.java   |   8 +-
 .../sysml/runtime/matrix/data/CM_N_COVCell.java |  82 +--
 .../sysml/runtime/matrix/data/MatrixBlock.java  |  46 +-
 .../sysml/runtime/matrix/data/MatrixCell.java   |  59 +--
 .../sysml/runtime/matrix/data/MatrixValue.java  |  25 +-
 .../matrix/data/OperationsOnMatrixValues.java   |  31 +-
 .../sysml/runtime/matrix/mapred/GMRReducer.java |   8 +-
 .../mapred/MMCJMRReducerWithAggregator.java     |   8 +-
 .../runtime/matrix/mapred/MMRJMRReducer.java    |   9 +-
 .../sysml/runtime/matrix/mapred/ReduceBase.java |  10 +-
 .../compress/BasicMatrixVectorMultTest.java     |   4 +-
 .../compress/BasicVectorMatrixMultTest.java     |   4 +-
 .../compress/LargeMatrixVectorMultTest.java     |   4 +-
 .../compress/LargeParMatrixVectorMultTest.java  |   4 +-
 .../compress/LargeVectorMatrixMultTest.java     |   4 +-
 .../compress/ParMatrixVectorMultTest.java       |   4 +-
 .../compress/ParVectorMatrixMultTest.java       |   4 +-
 54 files changed, 1637 insertions(+), 1616 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/systemml/blob/57438103/A.csv
----------------------------------------------------------------------
diff --git a/A.csv b/A.csv
new file mode 100644
index 0000000..e214f8a
--- /dev/null
+++ b/A.csv
@@ -0,0 +1,3 @@
+0.0146817556045713,0.5112049868172497
+1.8844105118944472,1.575721197916694
+0.31179904293595984,1.9060943669721677

http://git-wip-us.apache.org/repos/asf/systemml/blob/57438103/A.csv.mtd
----------------------------------------------------------------------
diff --git a/A.csv.mtd b/A.csv.mtd
new file mode 100644
index 0000000..268160d
--- /dev/null
+++ b/A.csv.mtd
@@ -0,0 +1,12 @@
+{
+    "data_type": "matrix",
+    "value_type": "double",
+    "rows": 3,
+    "cols": 2,
+    "nnz": 6,
+    "format": "csv",
+    "header": false,
+    "sep": ",",
+    "author": "mboehm",
+    "created": "2018-01-16 16:09:58 PST"
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/systemml/blob/57438103/B.csv
----------------------------------------------------------------------
diff --git a/B.csv b/B.csv
new file mode 100644
index 0000000..58a7cea
--- /dev/null
+++ b/B.csv
@@ -0,0 +1,3 @@
+0.0146817556045713,0.5112049868172497,1.0,0,1.0,2.5258867424218208
+1.8844105118944472,1.575721197916694,1.0,1.0,1.8844105118944472,7.344542221705589
+0.31179904293595984,1.9060943669721677,1.0,2.0,2.0,7.217893409908127

http://git-wip-us.apache.org/repos/asf/systemml/blob/57438103/B.csv.mtd
----------------------------------------------------------------------
diff --git a/B.csv.mtd b/B.csv.mtd
new file mode 100644
index 0000000..961e590
--- /dev/null
+++ b/B.csv.mtd
@@ -0,0 +1,12 @@
+{
+    "data_type": "matrix",
+    "value_type": "double",
+    "rows": 3,
+    "cols": 6,
+    "nnz": 17,
+    "format": "csv",
+    "header": false,
+    "sep": ",",
+    "author": "mboehm",
+    "created": "2018-01-16 16:09:58 PST"
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/systemml/blob/57438103/functions/jmlc/temp/m.binary
----------------------------------------------------------------------
diff --git a/functions/jmlc/temp/m.binary b/functions/jmlc/temp/m.binary
new file mode 100644
index 0000000..4455f9e
Binary files /dev/null and b/functions/jmlc/temp/m.binary differ

http://git-wip-us.apache.org/repos/asf/systemml/blob/57438103/functions/jmlc/temp/m.binary.mtd
----------------------------------------------------------------------
diff --git a/functions/jmlc/temp/m.binary.mtd b/functions/jmlc/temp/m.binary.mtd
new file mode 100644
index 0000000..31f6d06
--- /dev/null
+++ b/functions/jmlc/temp/m.binary.mtd
@@ -0,0 +1,12 @@
+{
+    "data_type": "matrix",
+    "value_type": "double",
+    "rows": 4,
+    "cols": 3,
+    "rows_in_block": 1000,
+    "cols_in_block": 1000,
+    "nnz": 6,
+    "format": "binary",
+    "author": "mboehm",
+    "created": "2018-01-16 16:10:56 PST"
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/systemml/blob/57438103/functions/jmlc/temp/m.csv
----------------------------------------------------------------------
diff --git a/functions/jmlc/temp/m.csv b/functions/jmlc/temp/m.csv
new file mode 100644
index 0000000..37aea08
--- /dev/null
+++ b/functions/jmlc/temp/m.csv
@@ -0,0 +1,4 @@
+1.0,2.0,3.0
+0,0,0
+7.0,8.0,9.0
+0,0,0

http://git-wip-us.apache.org/repos/asf/systemml/blob/57438103/functions/jmlc/temp/m.csv.mtd
----------------------------------------------------------------------
diff --git a/functions/jmlc/temp/m.csv.mtd b/functions/jmlc/temp/m.csv.mtd
new file mode 100644
index 0000000..2dce672
--- /dev/null
+++ b/functions/jmlc/temp/m.csv.mtd
@@ -0,0 +1,12 @@
+{
+    "data_type": "matrix",
+    "value_type": "double",
+    "rows": 4,
+    "cols": 3,
+    "nnz": 6,
+    "format": "csv",
+    "header": false,
+    "sep": ",",
+    "author": "mboehm",
+    "created": "2018-01-16 16:10:56 PST"
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/systemml/blob/57438103/functions/jmlc/temp/m.mm
----------------------------------------------------------------------
diff --git a/functions/jmlc/temp/m.mm b/functions/jmlc/temp/m.mm
new file mode 100644
index 0000000..f7ae3bf
--- /dev/null
+++ b/functions/jmlc/temp/m.mm
@@ -0,0 +1,8 @@
+%%MatrixMarket matrix coordinate real general
+4 3 6
+1 1 1.0
+1 2 2.0
+1 3 3.0
+3 1 7.0
+3 2 8.0
+3 3 9.0

http://git-wip-us.apache.org/repos/asf/systemml/blob/57438103/functions/jmlc/temp/m.txt
----------------------------------------------------------------------
diff --git a/functions/jmlc/temp/m.txt b/functions/jmlc/temp/m.txt
new file mode 100644
index 0000000..e11b21c
--- /dev/null
+++ b/functions/jmlc/temp/m.txt
@@ -0,0 +1,6 @@
+1 1 1.0
+1 2 2.0
+1 3 3.0
+3 1 7.0
+3 2 8.0
+3 3 9.0

http://git-wip-us.apache.org/repos/asf/systemml/blob/57438103/functions/jmlc/temp/m.txt.mtd
----------------------------------------------------------------------
diff --git a/functions/jmlc/temp/m.txt.mtd b/functions/jmlc/temp/m.txt.mtd
new file mode 100644
index 0000000..ed6376f
--- /dev/null
+++ b/functions/jmlc/temp/m.txt.mtd
@@ -0,0 +1,10 @@
+{
+    "data_type": "matrix",
+    "value_type": "double",
+    "rows": 4,
+    "cols": 3,
+    "nnz": 6,
+    "format": "text",
+    "author": "mboehm",
+    "created": "2018-01-16 16:10:56 PST"
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/systemml/blob/57438103/functions/jmlc/temp/scoring-example.dml
----------------------------------------------------------------------
diff --git a/functions/jmlc/temp/scoring-example.dml b/functions/jmlc/temp/scoring-example.dml
new file mode 100644
index 0000000..d45d95d
--- /dev/null
+++ b/functions/jmlc/temp/scoring-example.dml
@@ -0,0 +1,12 @@
+X = read("./tmp/X", rows=-1, cols=-1);
+W = read("./tmp/W", rows=-1, cols=-1);
+
+numRows = nrow(X);
+numCols = ncol(X);
+b = W[numCols+1,]
+scores = X %*% W[1:numCols,] + b;
+predicted_y = rowIndexMax(scores);
+
+print('pred:' + toString(predicted_y))
+
+write(predicted_y, "./tmp", format="text");

http://git-wip-us.apache.org/repos/asf/systemml/blob/57438103/functions/jmlc/temp/x.csv
----------------------------------------------------------------------
diff --git a/functions/jmlc/temp/x.csv b/functions/jmlc/temp/x.csv
new file mode 100644
index 0000000..e055049
--- /dev/null
+++ b/functions/jmlc/temp/x.csv
@@ -0,0 +1,2 @@
+1.0,2.0
+3.0,4.0

http://git-wip-us.apache.org/repos/asf/systemml/blob/57438103/functions/jmlc/temp/x.csv.mtd
----------------------------------------------------------------------
diff --git a/functions/jmlc/temp/x.csv.mtd b/functions/jmlc/temp/x.csv.mtd
new file mode 100644
index 0000000..a350176
--- /dev/null
+++ b/functions/jmlc/temp/x.csv.mtd
@@ -0,0 +1,12 @@
+{
+    "data_type": "matrix",
+    "value_type": "double",
+    "rows": 2,
+    "cols": 2,
+    "nnz": 4,
+    "format": "csv",
+    "header": false,
+    "sep": ",",
+    "author": "mboehm",
+    "created": "2018-01-16 16:10:56 PST"
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/systemml/blob/57438103/src/main/java/org/apache/sysml/hops/TernaryOp.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/sysml/hops/TernaryOp.java b/src/main/java/org/apache/sysml/hops/TernaryOp.java
index 47b012e..dc40982 100644
--- a/src/main/java/org/apache/sysml/hops/TernaryOp.java
+++ b/src/main/java/org/apache/sysml/hops/TernaryOp.java
@@ -34,7 +34,7 @@ import org.apache.sysml.lops.PickByCount;
 import org.apache.sysml.lops.PlusMult;
 import org.apache.sysml.lops.RepMat;
 import org.apache.sysml.lops.SortKeys;
-import org.apache.sysml.lops.Ternary;
+import org.apache.sysml.lops.Ctable;
 import org.apache.sysml.lops.UnaryCP;
 import org.apache.sysml.lops.CombineBinary.OperationTypes;
 import org.apache.sysml.lops.LopProperties.ExecType;
@@ -412,7 +412,7 @@ public class TernaryOp extends Hop
 		DataType dt1 = getInput().get(0).getDataType(); 
 		DataType dt2 = getInput().get(1).getDataType(); 
 		DataType dt3 = getInput().get(2).getDataType(); 
-		Ternary.OperationTypes ternaryOpOrig = Ternary.findCtableOperationByInputDataTypes(dt1, dt2, dt3);
+		Ctable.OperationTypes ternaryOpOrig = Ctable.findCtableOperationByInputDataTypes(dt1, dt2, dt3);
  		
 		// Compute lops for all inputs
 		Lop[] inputLops = new Lop[getInput().size()];
@@ -428,8 +428,8 @@ public class TernaryOp extends Hop
 		if ( et == ExecType.CP  || et == ExecType.SPARK) 
 		{	
 			//for CP we support only ctable expand left
-			Ternary.OperationTypes ternaryOp = isSequenceRewriteApplicable(true) ? 
-				Ternary.OperationTypes.CTABLE_EXPAND_SCALAR_WEIGHT : ternaryOpOrig;
+			Ctable.OperationTypes ternaryOp = isSequenceRewriteApplicable(true) ? 
+				Ctable.OperationTypes.CTABLE_EXPAND_SCALAR_WEIGHT : ternaryOpOrig;
 			boolean ignoreZeros = false;
 			
 			if( isMatrixIgnoreZeroRewriteApplicable() ) { 
@@ -438,7 +438,7 @@ public class TernaryOp extends Hop
 				inputLops[1] = ((ParameterizedBuiltinOp)getInput().get(1)).getTargetHop().getInput().get(0).constructLops();
 			}
 			
-			Ternary ternary = new Ternary(inputLops, ternaryOp, getDataType(), getValueType(), ignoreZeros, et);
+			Ctable ternary = new Ctable(inputLops, ternaryOp, getDataType(), getValueType(), ignoreZeros, et);
 			
 			ternary.getOutputParameters().setDimensions(_dim1, _dim2, getRowsInBlock(), getColsInBlock(), -1);
 			setLineNumbers(ternary);
@@ -459,8 +459,8 @@ public class TernaryOp extends Hop
 		else //MR
 		{
 			//for MR we support both ctable expand left and right
-			Ternary.OperationTypes ternaryOp = isSequenceRewriteApplicable() ? 
-				Ternary.OperationTypes.CTABLE_EXPAND_SCALAR_WEIGHT : ternaryOpOrig;
+			Ctable.OperationTypes ternaryOp = isSequenceRewriteApplicable() ? 
+				Ctable.OperationTypes.CTABLE_EXPAND_SCALAR_WEIGHT : ternaryOpOrig;
 			
 			Group group1 = null, group2 = null, group3 = null, group4 = null;
 			group1 = new Group(inputLops[0], Group.OperationTypes.Sort, getDataType(), getValueType());
@@ -468,7 +468,7 @@ public class TernaryOp extends Hop
 					getDim2(), getRowsInBlock(), getColsInBlock(), getNnz());
 			setLineNumbers(group1);
 			
-			Ternary ternary = null;
+			Ctable ternary = null;
 			// create "group" lops for MATRIX inputs
 			switch (ternaryOp) 
 			{
@@ -493,12 +493,12 @@ public class TernaryOp extends Hop
 					setLineNumbers(group3);
 					
 					if ( inputLops.length == 3 )
-						ternary = new Ternary(
+						ternary = new Ctable(
 							new Lop[] {group1, group2, group3}, ternaryOp,
 							getDataType(), getValueType(), et);	
 					else 
 						// output dimensions are given
-						ternary = new Ternary(
+						ternary = new Ctable(
 							new Lop[] {group1, group2, group3, inputLops[3], inputLops[4]},
 							ternaryOp, getDataType(), getValueType(), et);	
 					break;
@@ -515,11 +515,11 @@ public class TernaryOp extends Hop
 					setLineNumbers(group2);
 					
 					if ( inputLops.length == 3)
-						ternary = new Ternary(
+						ternary = new Ctable(
 								new Lop[] {group1,group2,inputLops[2]},
 								ternaryOp, getDataType(), getValueType(), et);
 					else
-						ternary = new Ternary(
+						ternary = new Ctable(
 								new Lop[] {group1,group2,inputLops[2], inputLops[3], inputLops[4]},
 								ternaryOp, getDataType(), getValueType(), et);
 						
@@ -539,14 +539,14 @@ public class TernaryOp extends Hop
 					//TODO remove group, whenever we push it into the map task
 					
 					if (inputLops.length == 3)
-						ternary = new Ternary(
+						ternary = new Ctable(
 								new Lop[] {group, //matrix
 									getInput().get(2).constructLops(), //weight
 									new LiteralOp(left).constructLops() //left
 								},
 								ternaryOp, getDataType(), getValueType(), et);
 					else
-						ternary = new Ternary(
+						ternary = new Ctable(
 								new Lop[] {group, //matrix
 									getInput().get(2).constructLops(), //weight
 									new LiteralOp(left).constructLops(), //left
@@ -558,14 +558,14 @@ public class TernaryOp extends Hop
 				case CTABLE_TRANSFORM_HISTOGRAM:
 					// F=ctable(A,1) or F = ctable(A,1,1)
 					if ( inputLops.length == 3 )
-						ternary = new Ternary(
+						ternary = new Ctable(
 								new Lop[] {group1, 
 									getInput().get(1).constructLops(),
 									getInput().get(2).constructLops()
 								},
 								ternaryOp, getDataType(), getValueType(), et);
 					else
-						ternary = new Ternary(
+						ternary = new Ctable(
 								new Lop[] {group1, 
 									getInput().get(1).constructLops(),
 									getInput().get(2).constructLops(),
@@ -587,13 +587,13 @@ public class TernaryOp extends Hop
 					setLineNumbers(group3);
 					
 					if ( inputLops.length == 3)
-						ternary = new Ternary(
+						ternary = new Ctable(
 								new Lop[] {group1,
 									getInput().get(1).constructLops(),
 									group3},
 								ternaryOp, getDataType(), getValueType(), et);
 					else
-						ternary = new Ternary(
+						ternary = new Ctable(
 								new Lop[] {group1,
 									getInput().get(1).constructLops(),
 									group3, inputLops[3], inputLops[4] },
@@ -611,7 +611,7 @@ public class TernaryOp extends Hop
 			
 			Lop lctable = ternary;
 			
-			if( !(_disjointInputs || ternaryOp == Ternary.OperationTypes.CTABLE_EXPAND_SCALAR_WEIGHT) ) 
+			if( !(_disjointInputs || ternaryOp == Ctable.OperationTypes.CTABLE_EXPAND_SCALAR_WEIGHT) ) 
 			{ 
 				//no need for aggregation if (1) input indexed disjoint	or one side is sequence	w/ 1 increment
 				
@@ -885,9 +885,9 @@ public class TernaryOp extends Hop
 						else if( isSequenceRewriteApplicable(false) )
 							setDim2( input2._dim1 );
 						//for ctable_histogram also one dimension is known
-						Ternary.OperationTypes ternaryOp = Ternary.findCtableOperationByInputDataTypes(
+						Ctable.OperationTypes ternaryOp = Ctable.findCtableOperationByInputDataTypes(
 																input1.getDataType(), input2.getDataType(), input3.getDataType());
-						if(  ternaryOp==Ternary.OperationTypes.CTABLE_TRANSFORM_HISTOGRAM
+						if(  ternaryOp==Ctable.OperationTypes.CTABLE_TRANSFORM_HISTOGRAM
 							&& input2 instanceof LiteralOp )
 						{
 							setDim2( HopRewriteUtils.getIntValueSafe((LiteralOp)input2) );

http://git-wip-us.apache.org/repos/asf/systemml/blob/57438103/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 364ff09..aebfa9d 100644
--- a/src/main/java/org/apache/sysml/hops/cost/CostEstimatorStaticRuntime.java
+++ b/src/main/java/org/apache/sysml/hops/cost/CostEstimatorStaticRuntime.java
@@ -54,7 +54,7 @@ import org.apache.sysml.runtime.instructions.mr.MRInstruction;
 import org.apache.sysml.runtime.instructions.mr.MapMultChainInstruction;
 import org.apache.sysml.runtime.instructions.mr.PickByCountInstruction;
 import org.apache.sysml.runtime.instructions.mr.RemoveEmptyMRInstruction;
-import org.apache.sysml.runtime.instructions.mr.TernaryInstruction;
+import org.apache.sysml.runtime.instructions.mr.CtableInstruction;
 import org.apache.sysml.runtime.instructions.mr.UnaryMRInstructionBase;
 import org.apache.sysml.runtime.instructions.mr.MRInstruction.MRType;
 import org.apache.sysml.runtime.matrix.data.MatrixBlock;
@@ -408,9 +408,9 @@ public class CostEstimatorStaticRuntime extends CostEstimator
 					attr = new String[]{rbinst.isRemoveRows()?"0":"1"};
 				}
 			}
-			else if( mrinst instanceof TernaryInstruction )
+			else if( mrinst instanceof CtableInstruction )
 			{
-				TernaryInstruction tinst = (TernaryInstruction) mrinst;
+				CtableInstruction tinst = (CtableInstruction) mrinst;
 				vs[0] = stats[ tinst.input1 ];
 				vs[1] = stats[ tinst.input2 ];
 				vs[2] = stats[ tinst.input3 ];
@@ -884,7 +884,7 @@ public class CostEstimatorStaticRuntime extends CostEstimator
 					else
 						return d3m*d3n;
 				
-				case Ternary: //opcodes: ctable
+				case Ctable: //opcodes: ctable
 					if( optype.equals("ctable") ){
 						if( leftSparse )
 							return d1m * d1n * d1s; //add
@@ -1139,7 +1139,7 @@ public class CostEstimatorStaticRuntime extends CostEstimator
 					//note: covers scalar, matrix, matrix-scalar
 					return d3m * d3n;
 					
-				case Ternary: //opcodes: ctabletransform, ctabletransformscalarweight, ctabletransformhistogram, ctabletransformweightedhistogram
+				case Ctable: //opcodes: ctabletransform, ctabletransformscalarweight, ctabletransformhistogram, ctabletransformweightedhistogram
 					//note: copy from cp
 					if( leftSparse )
 						return d1m * d1n * d1s; //add

http://git-wip-us.apache.org/repos/asf/systemml/blob/57438103/src/main/java/org/apache/sysml/lops/Ctable.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/sysml/lops/Ctable.java b/src/main/java/org/apache/sysml/lops/Ctable.java
new file mode 100644
index 0000000..7eec1bb
--- /dev/null
+++ b/src/main/java/org/apache/sysml/lops/Ctable.java
@@ -0,0 +1,354 @@
+/*
+ * 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.lops;
+
+import org.apache.sysml.lops.LopProperties.ExecLocation;
+import org.apache.sysml.lops.LopProperties.ExecType;
+import org.apache.sysml.lops.compile.JobType;
+import org.apache.sysml.parser.Expression.*;
+
+
+/**
+ * Lop to perform ternary operation. All inputs must be matrices or vectors. 
+ * For example, this lop is used in evaluating A = ctable(B,C,W)
+ * 
+ * Currently, this lop is used only in case of CTABLE functionality.
+ */
+
+public class Ctable extends Lop 
+{
+	private boolean _ignoreZeros = false;
+	
+	public enum OperationTypes { 
+		CTABLE_TRANSFORM, 
+		CTABLE_TRANSFORM_SCALAR_WEIGHT, 
+		CTABLE_TRANSFORM_HISTOGRAM, 
+		CTABLE_TRANSFORM_WEIGHTED_HISTOGRAM, 
+		CTABLE_EXPAND_SCALAR_WEIGHT, 
+		INVALID 
+	}
+	
+	OperationTypes operation;
+	
+
+	public Ctable(Lop[] inputLops, OperationTypes op, DataType dt, ValueType vt, ExecType et) {
+		this(inputLops, op, dt, vt, false, et);
+	}
+	
+	public Ctable(Lop[] inputLops, OperationTypes op, DataType dt, ValueType vt, boolean ignoreZeros, ExecType et) {
+		super(Lop.Type.Ternary, dt, vt);
+		init(inputLops, op, et);
+		_ignoreZeros = ignoreZeros;
+	}
+	
+	private void init(Lop[] inputLops, OperationTypes op, ExecType et) {
+		operation = op;
+		
+		for(int i=0; i < inputLops.length; i++) {
+			this.addInput(inputLops[i]);
+			inputLops[i].addOutput(this);
+		}
+		
+		boolean breaksAlignment = true;
+		boolean aligner = false;
+		boolean definesMRJob = false;
+		
+		if ( et == ExecType.MR ) {
+			lps.addCompatibility(JobType.GMR);
+			//lps.addCompatibility(JobType.DATAGEN); MB: disabled due to piggybacking issues
+			//lps.addCompatibility(JobType.REBLOCK); MB: disabled since no runtime support
+			
+			if( operation==OperationTypes.CTABLE_EXPAND_SCALAR_WEIGHT )
+				this.lps.setProperties( inputs, et, ExecLocation.Reduce, breaksAlignment, aligner, definesMRJob );
+				//TODO create runtime for ctable in gmr mapper and switch to maporreduce.
+				//this.lps.setProperties( inputs, et, ExecLocation.MapOrReduce, breaksAlignment, aligner, definesMRJob );
+			else
+				this.lps.setProperties( inputs, et, ExecLocation.Reduce, breaksAlignment, aligner, definesMRJob );
+		}
+		else {
+			lps.addCompatibility(JobType.INVALID);
+			this.lps.setProperties( inputs, et, ExecLocation.ControlProgram, breaksAlignment, aligner, definesMRJob );
+		}
+	}
+	
+	@Override
+	public String toString() {
+	
+		return " Operation: " + operation;
+
+	}
+
+	public static OperationTypes findCtableOperationByInputDataTypes(DataType dt1, DataType dt2, DataType dt3) 
+	{
+		if ( dt1 == DataType.MATRIX ) {
+			if (dt2 == DataType.MATRIX && dt3 == DataType.SCALAR) {
+				// F = ctable(A,B) or F = ctable(A,B,1)
+				return OperationTypes.CTABLE_TRANSFORM_SCALAR_WEIGHT;
+			} else if (dt2 == DataType.SCALAR && dt3 == DataType.SCALAR) {
+				// F=ctable(A,1) or F = ctable(A,1,1)
+				return OperationTypes.CTABLE_TRANSFORM_HISTOGRAM;
+			} else if (dt2 == DataType.SCALAR && dt3 == DataType.MATRIX) {
+				// F=ctable(A,1,W)
+				return OperationTypes.CTABLE_TRANSFORM_WEIGHTED_HISTOGRAM;
+			} else {
+				// F=ctable(A,B,W)
+				return OperationTypes.CTABLE_TRANSFORM;
+			}
+		}
+		else {
+			return OperationTypes.INVALID;
+		}
+	}
+
+	/**
+	 * method to get operation type
+	 * @return operation type
+	 */
+	 
+	public OperationTypes getOperationType()
+	{
+		return operation;
+	}
+
+	@Override
+	public String getInstructions(String input1, String input2, String input3, String output) throws LopsException
+	{
+		StringBuilder sb = new StringBuilder();
+		sb.append( getExecType() );
+		sb.append( Lop.OPERAND_DELIMITOR );
+		if( operation != Ctable.OperationTypes.CTABLE_EXPAND_SCALAR_WEIGHT )
+			sb.append( "ctable" );
+		else
+			sb.append( "ctableexpand" );
+		sb.append( OPERAND_DELIMITOR );
+		
+		if ( getInputs().get(0).getDataType() == DataType.SCALAR ) {
+			sb.append ( getInputs().get(0).prepScalarInputOperand(getExecType()) );
+		}
+		else {
+			sb.append( getInputs().get(0).prepInputOperand(input1));
+		}
+		sb.append( OPERAND_DELIMITOR );
+		
+		if ( getInputs().get(1).getDataType() == DataType.SCALAR ) {
+			sb.append ( getInputs().get(1).prepScalarInputOperand(getExecType()) );
+		}
+		else {
+			sb.append( getInputs().get(1).prepInputOperand(input2));
+		}
+		sb.append( OPERAND_DELIMITOR );
+		
+		if ( getInputs().get(2).getDataType() == DataType.SCALAR ) {
+			sb.append ( getInputs().get(2).prepScalarInputOperand(getExecType()) );
+		}
+		else {
+			sb.append( getInputs().get(2).prepInputOperand(input3));
+		}
+		sb.append( OPERAND_DELIMITOR );
+		
+		if ( this.getInputs().size() > 3 ) {
+			sb.append(getInputs().get(3).getOutputParameters().getLabel());
+			sb.append(LITERAL_PREFIX);
+			sb.append((getInputs().get(3).getType() == Type.Data && ((Data)getInputs().get(3)).isLiteral()) );
+			sb.append( OPERAND_DELIMITOR );
+
+			sb.append(getInputs().get(4).getOutputParameters().getLabel());
+			sb.append(LITERAL_PREFIX);
+			sb.append((getInputs().get(4).getType() == Type.Data && ((Data)getInputs().get(4)).isLiteral()) );
+			sb.append( OPERAND_DELIMITOR );
+		}
+		else {
+			sb.append(-1);
+			sb.append(LITERAL_PREFIX);
+			sb.append(true);
+			sb.append( OPERAND_DELIMITOR );
+			
+			sb.append(-1);
+			sb.append(LITERAL_PREFIX);
+			sb.append(true);
+			sb.append( OPERAND_DELIMITOR ); 
+		}
+		sb.append( this.prepOutputOperand(output));
+		
+		sb.append( OPERAND_DELIMITOR );
+		sb.append( _ignoreZeros );
+		
+		return sb.toString();
+	}
+
+	@Override
+	public String getInstructions(int input_index1, int input_index2, int input_index3, int output_index) throws LopsException
+	{
+		StringBuilder sb = new StringBuilder();
+		sb.append( getExecType() );
+		sb.append( Lop.OPERAND_DELIMITOR );
+		switch(operation) {
+		/* Arithmetic */
+		case CTABLE_TRANSFORM:
+			// F = ctable(A,B,W)
+			sb.append( "ctabletransform" );
+			sb.append( OPERAND_DELIMITOR );
+			sb.append( getInputs().get(0).prepInputOperand(input_index1));
+			sb.append( OPERAND_DELIMITOR );
+			sb.append( getInputs().get(1).prepInputOperand(input_index2));
+			sb.append( OPERAND_DELIMITOR );
+			sb.append( getInputs().get(2).prepInputOperand(input_index3));
+			sb.append( OPERAND_DELIMITOR );
+			
+			break;
+		
+		case CTABLE_TRANSFORM_SCALAR_WEIGHT:
+			// F = ctable(A,B) or F = ctable(A,B,1)
+			// third input must be a scalar, and hence input_index3 == -1
+			if ( input_index3 != -1 ) {
+				throw new LopsException(this.printErrorLocation() + "In Tertiary Lop, Unexpected input while computing the instructions for op: " + operation + " \n");
+			}
+			
+			int scalarIndex = 2; // index of the scalar input
+			
+			sb.append( "ctabletransformscalarweight" );
+			sb.append( OPERAND_DELIMITOR );
+			
+			sb.append( getInputs().get(0).prepInputOperand(input_index1));
+			sb.append( OPERAND_DELIMITOR );
+			
+			sb.append( getInputs().get(1).prepInputOperand(input_index2));
+			sb.append( OPERAND_DELIMITOR );
+			
+			sb.append( getInputs().get(scalarIndex).prepScalarInputOperand(getExecType()));
+			sb.append( OPERAND_DELIMITOR );
+			
+			break;
+		
+		case CTABLE_EXPAND_SCALAR_WEIGHT:
+			// F = ctable(seq,B) or F = ctable(seq,B,1)
+			// second and third inputs must be scalars, and hence input_index2 == -1, input_index3 == -1
+			if ( input_index3 != -1 ) {
+				throw new LopsException(this.printErrorLocation() + "In Tertiary Lop, Unexpected input while computing the instructions for op: " + operation + " \n");
+			}
+			
+			int scalarIndex2 = 1; // index of the scalar input
+			int scalarIndex3 = 2; // index of the scalar input
+			
+			sb.append( "ctableexpandscalarweight" );
+			sb.append( OPERAND_DELIMITOR );
+			//get(0) because input under group
+			sb.append( getInputs().get(0).prepInputOperand(input_index1));
+			sb.append( OPERAND_DELIMITOR );
+			
+			sb.append( getInputs().get(scalarIndex2).prepScalarInputOperand(getExecType()));
+			sb.append( OPERAND_DELIMITOR );
+			
+			sb.append( getInputs().get(scalarIndex3).prepScalarInputOperand(getExecType()));
+			sb.append( OPERAND_DELIMITOR );
+			
+			break;
+		
+		case CTABLE_TRANSFORM_HISTOGRAM:
+			// F=ctable(A,1) or F = ctable(A,1,1)
+			if ( input_index2 != -1 || input_index3 != -1)
+				throw new LopsException(this.printErrorLocation() + "In Tertiary Lop, Unexpected input while computing the instructions for op: " + operation);
+			
+			// 2nd and 3rd inputs are scalar inputs 
+			
+			sb.append( "ctabletransformhistogram" );
+			sb.append( OPERAND_DELIMITOR );
+			
+			sb.append( getInputs().get(0).prepInputOperand(input_index1));
+			sb.append( OPERAND_DELIMITOR );
+			
+			sb.append( getInputs().get(1).prepScalarInputOperand(getExecType()) );
+			sb.append( OPERAND_DELIMITOR );
+			
+			sb.append( getInputs().get(2).prepScalarInputOperand(getExecType()) );
+			sb.append( OPERAND_DELIMITOR );
+			
+			break;
+		
+		case CTABLE_TRANSFORM_WEIGHTED_HISTOGRAM:
+			// F=ctable(A,1,W)
+			if ( input_index2 != -1 )
+				throw new LopsException(this.printErrorLocation() + "In Tertiary Lop, Unexpected input while computing the instructions for op: " + operation);
+			
+			// 2nd input is the scalar input
+			
+			sb.append( "ctabletransformweightedhistogram" );
+			sb.append( OPERAND_DELIMITOR );
+			
+			sb.append( getInputs().get(0).prepInputOperand(input_index1));
+			sb.append( OPERAND_DELIMITOR );
+			
+			sb.append( getInputs().get(1).prepScalarInputOperand(getExecType()));
+			sb.append( OPERAND_DELIMITOR );
+			
+			sb.append( getInputs().get(2).prepInputOperand(input_index3));
+			sb.append( OPERAND_DELIMITOR );
+			
+			break;
+			
+		default:
+			throw new UnsupportedOperationException(this.printErrorLocation() + "Instruction is not defined for Tertiary operation: " + operation);
+		}
+		
+		long outputDim1=-1, outputDim2=-1;
+		if ( getInputs().size() > 3 ) {
+			sb.append(getInputs().get(3).prepScalarLabel());
+			sb.append( OPERAND_DELIMITOR );
+
+			sb.append(getInputs().get(4).prepScalarLabel());
+			sb.append( OPERAND_DELIMITOR );
+			/*if ( input3 instanceof Data && ((Data)input3).isLiteral() 
+					&& input4 instanceof Data && ((Data)input4).isLiteral() ) {
+				outputDim1 = ((Data)input3).getLongValue();
+				outputDim2 = ((Data)input4).getLongValue();
+			}*/
+		}
+		else {
+			sb.append( outputDim1 );
+			sb.append( OPERAND_DELIMITOR );
+			
+			sb.append( outputDim2 );
+			sb.append( OPERAND_DELIMITOR ); 
+		}
+		sb.append( this.prepOutputOperand(output_index));
+		
+		return sb.toString();
+	}
+	
+	public static OperationTypes getOperationType(String opcode)
+	{
+		OperationTypes op = null;
+		
+		if( opcode.equals("ctabletransform") )
+			op = OperationTypes.CTABLE_TRANSFORM;
+		else if( opcode.equals("ctabletransformscalarweight") )
+			op = OperationTypes.CTABLE_TRANSFORM_SCALAR_WEIGHT;
+		else if( opcode.equals("ctableexpandscalarweight") )
+			op = OperationTypes.CTABLE_EXPAND_SCALAR_WEIGHT;
+		else if( opcode.equals("ctabletransformhistogram") )
+			op = OperationTypes.CTABLE_TRANSFORM_HISTOGRAM;
+		else if( opcode.equals("ctabletransformweightedhistogram") )
+			op = OperationTypes.CTABLE_TRANSFORM_WEIGHTED_HISTOGRAM;
+		else
+			throw new UnsupportedOperationException("Tertiary operation code is not defined: " + opcode);
+		
+		return op;
+	}
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/systemml/blob/57438103/src/main/java/org/apache/sysml/lops/Ternary.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/sysml/lops/Ternary.java b/src/main/java/org/apache/sysml/lops/Ternary.java
deleted file mode 100644
index ba3f884..0000000
--- a/src/main/java/org/apache/sysml/lops/Ternary.java
+++ /dev/null
@@ -1,354 +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.lops;
-
-import org.apache.sysml.lops.LopProperties.ExecLocation;
-import org.apache.sysml.lops.LopProperties.ExecType;
-import org.apache.sysml.lops.compile.JobType;
-import org.apache.sysml.parser.Expression.*;
-
-
-/**
- * Lop to perform ternary operation. All inputs must be matrices or vectors. 
- * For example, this lop is used in evaluating A = ctable(B,C,W)
- * 
- * Currently, this lop is used only in case of CTABLE functionality.
- */
-
-public class Ternary extends Lop 
-{
-	private boolean _ignoreZeros = false;
-	
-	public enum OperationTypes { 
-		CTABLE_TRANSFORM, 
-		CTABLE_TRANSFORM_SCALAR_WEIGHT, 
-		CTABLE_TRANSFORM_HISTOGRAM, 
-		CTABLE_TRANSFORM_WEIGHTED_HISTOGRAM, 
-		CTABLE_EXPAND_SCALAR_WEIGHT, 
-		INVALID 
-	}
-	
-	OperationTypes operation;
-	
-
-	public Ternary(Lop[] inputLops, OperationTypes op, DataType dt, ValueType vt, ExecType et) {
-		this(inputLops, op, dt, vt, false, et);
-	}
-	
-	public Ternary(Lop[] inputLops, OperationTypes op, DataType dt, ValueType vt, boolean ignoreZeros, ExecType et) {
-		super(Lop.Type.Ternary, dt, vt);
-		init(inputLops, op, et);
-		_ignoreZeros = ignoreZeros;
-	}
-	
-	private void init(Lop[] inputLops, OperationTypes op, ExecType et) {
-		operation = op;
-		
-		for(int i=0; i < inputLops.length; i++) {
-			this.addInput(inputLops[i]);
-			inputLops[i].addOutput(this);
-		}
-		
-		boolean breaksAlignment = true;
-		boolean aligner = false;
-		boolean definesMRJob = false;
-		
-		if ( et == ExecType.MR ) {
-			lps.addCompatibility(JobType.GMR);
-			//lps.addCompatibility(JobType.DATAGEN); MB: disabled due to piggybacking issues
-			//lps.addCompatibility(JobType.REBLOCK); MB: disabled since no runtime support
-			
-			if( operation==OperationTypes.CTABLE_EXPAND_SCALAR_WEIGHT )
-				this.lps.setProperties( inputs, et, ExecLocation.Reduce, breaksAlignment, aligner, definesMRJob );
-				//TODO create runtime for ctable in gmr mapper and switch to maporreduce.
-				//this.lps.setProperties( inputs, et, ExecLocation.MapOrReduce, breaksAlignment, aligner, definesMRJob );
-			else
-				this.lps.setProperties( inputs, et, ExecLocation.Reduce, breaksAlignment, aligner, definesMRJob );
-		}
-		else {
-			lps.addCompatibility(JobType.INVALID);
-			this.lps.setProperties( inputs, et, ExecLocation.ControlProgram, breaksAlignment, aligner, definesMRJob );
-		}
-	}
-	
-	@Override
-	public String toString() {
-	
-		return " Operation: " + operation;
-
-	}
-
-	public static OperationTypes findCtableOperationByInputDataTypes(DataType dt1, DataType dt2, DataType dt3) 
-	{
-		if ( dt1 == DataType.MATRIX ) {
-			if (dt2 == DataType.MATRIX && dt3 == DataType.SCALAR) {
-				// F = ctable(A,B) or F = ctable(A,B,1)
-				return OperationTypes.CTABLE_TRANSFORM_SCALAR_WEIGHT;
-			} else if (dt2 == DataType.SCALAR && dt3 == DataType.SCALAR) {
-				// F=ctable(A,1) or F = ctable(A,1,1)
-				return OperationTypes.CTABLE_TRANSFORM_HISTOGRAM;
-			} else if (dt2 == DataType.SCALAR && dt3 == DataType.MATRIX) {
-				// F=ctable(A,1,W)
-				return OperationTypes.CTABLE_TRANSFORM_WEIGHTED_HISTOGRAM;
-			} else {
-				// F=ctable(A,B,W)
-				return OperationTypes.CTABLE_TRANSFORM;
-			}
-		}
-		else {
-			return OperationTypes.INVALID;
-		}
-	}
-
-	/**
-	 * method to get operation type
-	 * @return operation type
-	 */
-	 
-	public OperationTypes getOperationType()
-	{
-		return operation;
-	}
-
-	@Override
-	public String getInstructions(String input1, String input2, String input3, String output) throws LopsException
-	{
-		StringBuilder sb = new StringBuilder();
-		sb.append( getExecType() );
-		sb.append( Lop.OPERAND_DELIMITOR );
-		if( operation != Ternary.OperationTypes.CTABLE_EXPAND_SCALAR_WEIGHT )
-			sb.append( "ctable" );
-		else
-			sb.append( "ctableexpand" );
-		sb.append( OPERAND_DELIMITOR );
-		
-		if ( getInputs().get(0).getDataType() == DataType.SCALAR ) {
-			sb.append ( getInputs().get(0).prepScalarInputOperand(getExecType()) );
-		}
-		else {
-			sb.append( getInputs().get(0).prepInputOperand(input1));
-		}
-		sb.append( OPERAND_DELIMITOR );
-		
-		if ( getInputs().get(1).getDataType() == DataType.SCALAR ) {
-			sb.append ( getInputs().get(1).prepScalarInputOperand(getExecType()) );
-		}
-		else {
-			sb.append( getInputs().get(1).prepInputOperand(input2));
-		}
-		sb.append( OPERAND_DELIMITOR );
-		
-		if ( getInputs().get(2).getDataType() == DataType.SCALAR ) {
-			sb.append ( getInputs().get(2).prepScalarInputOperand(getExecType()) );
-		}
-		else {
-			sb.append( getInputs().get(2).prepInputOperand(input3));
-		}
-		sb.append( OPERAND_DELIMITOR );
-		
-		if ( this.getInputs().size() > 3 ) {
-			sb.append(getInputs().get(3).getOutputParameters().getLabel());
-			sb.append(LITERAL_PREFIX);
-			sb.append((getInputs().get(3).getType() == Type.Data && ((Data)getInputs().get(3)).isLiteral()) );
-			sb.append( OPERAND_DELIMITOR );
-
-			sb.append(getInputs().get(4).getOutputParameters().getLabel());
-			sb.append(LITERAL_PREFIX);
-			sb.append((getInputs().get(4).getType() == Type.Data && ((Data)getInputs().get(4)).isLiteral()) );
-			sb.append( OPERAND_DELIMITOR );
-		}
-		else {
-			sb.append(-1);
-			sb.append(LITERAL_PREFIX);
-			sb.append(true);
-			sb.append( OPERAND_DELIMITOR );
-			
-			sb.append(-1);
-			sb.append(LITERAL_PREFIX);
-			sb.append(true);
-			sb.append( OPERAND_DELIMITOR ); 
-		}
-		sb.append( this.prepOutputOperand(output));
-		
-		sb.append( OPERAND_DELIMITOR );
-		sb.append( _ignoreZeros );
-		
-		return sb.toString();
-	}
-
-	@Override
-	public String getInstructions(int input_index1, int input_index2, int input_index3, int output_index) throws LopsException
-	{
-		StringBuilder sb = new StringBuilder();
-		sb.append( getExecType() );
-		sb.append( Lop.OPERAND_DELIMITOR );
-		switch(operation) {
-		/* Arithmetic */
-		case CTABLE_TRANSFORM:
-			// F = ctable(A,B,W)
-			sb.append( "ctabletransform" );
-			sb.append( OPERAND_DELIMITOR );
-			sb.append( getInputs().get(0).prepInputOperand(input_index1));
-			sb.append( OPERAND_DELIMITOR );
-			sb.append( getInputs().get(1).prepInputOperand(input_index2));
-			sb.append( OPERAND_DELIMITOR );
-			sb.append( getInputs().get(2).prepInputOperand(input_index3));
-			sb.append( OPERAND_DELIMITOR );
-			
-			break;
-		
-		case CTABLE_TRANSFORM_SCALAR_WEIGHT:
-			// F = ctable(A,B) or F = ctable(A,B,1)
-			// third input must be a scalar, and hence input_index3 == -1
-			if ( input_index3 != -1 ) {
-				throw new LopsException(this.printErrorLocation() + "In Tertiary Lop, Unexpected input while computing the instructions for op: " + operation + " \n");
-			}
-			
-			int scalarIndex = 2; // index of the scalar input
-			
-			sb.append( "ctabletransformscalarweight" );
-			sb.append( OPERAND_DELIMITOR );
-			
-			sb.append( getInputs().get(0).prepInputOperand(input_index1));
-			sb.append( OPERAND_DELIMITOR );
-			
-			sb.append( getInputs().get(1).prepInputOperand(input_index2));
-			sb.append( OPERAND_DELIMITOR );
-			
-			sb.append( getInputs().get(scalarIndex).prepScalarInputOperand(getExecType()));
-			sb.append( OPERAND_DELIMITOR );
-			
-			break;
-		
-		case CTABLE_EXPAND_SCALAR_WEIGHT:
-			// F = ctable(seq,B) or F = ctable(seq,B,1)
-			// second and third inputs must be scalars, and hence input_index2 == -1, input_index3 == -1
-			if ( input_index3 != -1 ) {
-				throw new LopsException(this.printErrorLocation() + "In Tertiary Lop, Unexpected input while computing the instructions for op: " + operation + " \n");
-			}
-			
-			int scalarIndex2 = 1; // index of the scalar input
-			int scalarIndex3 = 2; // index of the scalar input
-			
-			sb.append( "ctableexpandscalarweight" );
-			sb.append( OPERAND_DELIMITOR );
-			//get(0) because input under group
-			sb.append( getInputs().get(0).prepInputOperand(input_index1));
-			sb.append( OPERAND_DELIMITOR );
-			
-			sb.append( getInputs().get(scalarIndex2).prepScalarInputOperand(getExecType()));
-			sb.append( OPERAND_DELIMITOR );
-			
-			sb.append( getInputs().get(scalarIndex3).prepScalarInputOperand(getExecType()));
-			sb.append( OPERAND_DELIMITOR );
-			
-			break;
-		
-		case CTABLE_TRANSFORM_HISTOGRAM:
-			// F=ctable(A,1) or F = ctable(A,1,1)
-			if ( input_index2 != -1 || input_index3 != -1)
-				throw new LopsException(this.printErrorLocation() + "In Tertiary Lop, Unexpected input while computing the instructions for op: " + operation);
-			
-			// 2nd and 3rd inputs are scalar inputs 
-			
-			sb.append( "ctabletransformhistogram" );
-			sb.append( OPERAND_DELIMITOR );
-			
-			sb.append( getInputs().get(0).prepInputOperand(input_index1));
-			sb.append( OPERAND_DELIMITOR );
-			
-			sb.append( getInputs().get(1).prepScalarInputOperand(getExecType()) );
-			sb.append( OPERAND_DELIMITOR );
-			
-			sb.append( getInputs().get(2).prepScalarInputOperand(getExecType()) );
-			sb.append( OPERAND_DELIMITOR );
-			
-			break;
-		
-		case CTABLE_TRANSFORM_WEIGHTED_HISTOGRAM:
-			// F=ctable(A,1,W)
-			if ( input_index2 != -1 )
-				throw new LopsException(this.printErrorLocation() + "In Tertiary Lop, Unexpected input while computing the instructions for op: " + operation);
-			
-			// 2nd input is the scalar input
-			
-			sb.append( "ctabletransformweightedhistogram" );
-			sb.append( OPERAND_DELIMITOR );
-			
-			sb.append( getInputs().get(0).prepInputOperand(input_index1));
-			sb.append( OPERAND_DELIMITOR );
-			
-			sb.append( getInputs().get(1).prepScalarInputOperand(getExecType()));
-			sb.append( OPERAND_DELIMITOR );
-			
-			sb.append( getInputs().get(2).prepInputOperand(input_index3));
-			sb.append( OPERAND_DELIMITOR );
-			
-			break;
-			
-		default:
-			throw new UnsupportedOperationException(this.printErrorLocation() + "Instruction is not defined for Tertiary operation: " + operation);
-		}
-		
-		long outputDim1=-1, outputDim2=-1;
-		if ( getInputs().size() > 3 ) {
-			sb.append(getInputs().get(3).prepScalarLabel());
-			sb.append( OPERAND_DELIMITOR );
-
-			sb.append(getInputs().get(4).prepScalarLabel());
-			sb.append( OPERAND_DELIMITOR );
-			/*if ( input3 instanceof Data && ((Data)input3).isLiteral() 
-					&& input4 instanceof Data && ((Data)input4).isLiteral() ) {
-				outputDim1 = ((Data)input3).getLongValue();
-				outputDim2 = ((Data)input4).getLongValue();
-			}*/
-		}
-		else {
-			sb.append( outputDim1 );
-			sb.append( OPERAND_DELIMITOR );
-			
-			sb.append( outputDim2 );
-			sb.append( OPERAND_DELIMITOR ); 
-		}
-		sb.append( this.prepOutputOperand(output_index));
-		
-		return sb.toString();
-	}
-	
-	public static OperationTypes getOperationType(String opcode)
-	{
-		OperationTypes op = null;
-		
-		if( opcode.equals("ctabletransform") )
-			op = OperationTypes.CTABLE_TRANSFORM;
-		else if( opcode.equals("ctabletransformscalarweight") )
-			op = OperationTypes.CTABLE_TRANSFORM_SCALAR_WEIGHT;
-		else if( opcode.equals("ctableexpandscalarweight") )
-			op = OperationTypes.CTABLE_EXPAND_SCALAR_WEIGHT;
-		else if( opcode.equals("ctabletransformhistogram") )
-			op = OperationTypes.CTABLE_TRANSFORM_HISTOGRAM;
-		else if( opcode.equals("ctabletransformweightedhistogram") )
-			op = OperationTypes.CTABLE_TRANSFORM_WEIGHTED_HISTOGRAM;
-		else
-			throw new UnsupportedOperationException("Tertiary operation code is not defined: " + opcode);
-		
-		return op;
-	}
-}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/systemml/blob/57438103/src/main/java/org/apache/sysml/runtime/compress/CompressedMatrixBlock.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/sysml/runtime/compress/CompressedMatrixBlock.java b/src/main/java/org/apache/sysml/runtime/compress/CompressedMatrixBlock.java
index 52ed2b4..0ce3dc8 100644
--- a/src/main/java/org/apache/sysml/runtime/compress/CompressedMatrixBlock.java
+++ b/src/main/java/org/apache/sysml/runtime/compress/CompressedMatrixBlock.java
@@ -1086,18 +1086,18 @@ public class CompressedMatrixBlock extends MatrixBlock implements Externalizable
 	}
 	
 	@Override
-	public MatrixValue aggregateBinaryOperations(MatrixValue mv1, MatrixValue mv2, MatrixValue result, AggregateBinaryOperator op)
+	public MatrixBlock aggregateBinaryOperations(MatrixBlock mv1, MatrixBlock mv2, MatrixBlock ret, AggregateBinaryOperator op)
 			throws DMLRuntimeException 
 	{
 		//call uncompressed matrix mult if necessary
 		if( !isCompressed() ) {
-			return super.aggregateBinaryOperations(mv1, mv2, result, op);
+			return super.aggregateBinaryOperations(mv1, mv2, ret, op);
 		}
 	
 		//multi-threaded mm of single uncompressed colgroup
 		if( isSingleUncompressedGroup() ){
 			MatrixBlock tmp = ((ColGroupUncompressed)_colGroups.get(0)).getData();
-			return tmp.aggregateBinaryOperations(this==mv1?tmp:mv1, this==mv2?tmp:mv2, result, op);
+			return tmp.aggregateBinaryOperations(this==mv1?tmp:mv1, this==mv2?tmp:mv2, ret, op);
 		}
 		
 		Timing time = LOG.isDebugEnabled() ? new Timing(true) : null;
@@ -1107,7 +1107,6 @@ public class CompressedMatrixBlock extends MatrixBlock implements Externalizable
 		int cl = mv2.getNumColumns();
 		
 		//create output matrix block
-		MatrixBlock ret = (MatrixBlock) result;
 		if( ret==null )
 			ret = new MatrixBlock(rl, cl, false, rl*cl);
 		else
@@ -2119,9 +2118,9 @@ public class CompressedMatrixBlock extends MatrixBlock implements Externalizable
 	}
 
 	@Override
-	public MatrixValue aggregateBinaryOperations(MatrixIndexes m1Index,
-			MatrixValue m1Value, MatrixIndexes m2Index, MatrixValue m2Value,
-			MatrixValue result, AggregateBinaryOperator op)
+	public MatrixBlock aggregateBinaryOperations(MatrixIndexes m1Index,
+			MatrixBlock m1Value, MatrixIndexes m2Index, MatrixBlock m2Value,
+			MatrixBlock result, AggregateBinaryOperator op)
 			throws DMLRuntimeException {
 		printDecompressWarning("aggregateBinaryOperations");
 		MatrixBlock left = isCompressed() ? decompress() : this;
@@ -2199,84 +2198,84 @@ public class CompressedMatrixBlock extends MatrixBlock implements Externalizable
 	}
 
 	@Override
-	public void ternaryOperations(Operator op, double scalar,
+	public void ctableOperations(Operator op, double scalar,
 			MatrixValue that, CTableMap resultMap, MatrixBlock resultBlock)
 			throws DMLRuntimeException {
 		printDecompressWarning("ternaryOperations");
 		MatrixBlock left = isCompressed() ? decompress() : this;
 		MatrixBlock right = getUncompressed(that);
-		left.ternaryOperations(op, scalar, right, resultMap, resultBlock);
+		left.ctableOperations(op, scalar, right, resultMap, resultBlock);
 	}
 
 	@Override
-	public void ternaryOperations(Operator op, double scalar,
+	public void ctableOperations(Operator op, double scalar,
 			double scalar2, CTableMap resultMap, MatrixBlock resultBlock)
 			throws DMLRuntimeException {
 		printDecompressWarning("ternaryOperations");
 		MatrixBlock tmp = isCompressed() ? decompress() : this;
-		tmp.ternaryOperations(op, scalar, scalar2, resultMap, resultBlock);
+		tmp.ctableOperations(op, scalar, scalar2, resultMap, resultBlock);
 	}
 
 	@Override
-	public void ternaryOperations(Operator op, MatrixIndexes ix1,
+	public void ctableOperations(Operator op, MatrixIndexes ix1,
 			double scalar, boolean left, int brlen, CTableMap resultMap,
 			MatrixBlock resultBlock) throws DMLRuntimeException {
 		printDecompressWarning("ternaryOperations");
 		MatrixBlock tmp = isCompressed() ? decompress() : this;
-		tmp.ternaryOperations(op, ix1, scalar, left, brlen, resultMap, resultBlock);
+		tmp.ctableOperations(op, ix1, scalar, left, brlen, resultMap, resultBlock);
 	}
 
 	@Override
-	public void ternaryOperations(Operator op, MatrixValue that,
+	public void ctableOperations(Operator op, MatrixValue that,
 			double scalar, boolean ignoreZeros, CTableMap resultMap,
 			MatrixBlock resultBlock) throws DMLRuntimeException {
 		printDecompressWarning("ternaryOperations");
 		MatrixBlock left = isCompressed() ? decompress() : this;
 		MatrixBlock right = getUncompressed(that);
-		left.ternaryOperations(op, right, scalar, ignoreZeros, resultMap, resultBlock);
+		left.ctableOperations(op, right, scalar, ignoreZeros, resultMap, resultBlock);
 	}
 
 	@Override
-	public void ternaryOperations(Operator op, MatrixValue that, double scalar, MatrixBlock resultBlock)
+	public void ctableOperations(Operator op, MatrixValue that, double scalar, MatrixBlock resultBlock)
 			throws DMLRuntimeException {
 		printDecompressWarning("ternaryOperations");
 		MatrixBlock left = isCompressed() ? decompress() : this;
 		MatrixBlock right = getUncompressed(that);
-		left.ternaryOperations(op, right, scalar, resultBlock);
+		left.ctableOperations(op, right, scalar, resultBlock);
 	}
 
 	@Override
-	public void ternaryOperations(Operator op, MatrixValue that,
+	public void ctableOperations(Operator op, MatrixValue that,
 			MatrixValue that2, CTableMap resultMap)
 			throws DMLRuntimeException {
 		printDecompressWarning("ternaryOperations");
 		MatrixBlock left = isCompressed() ? decompress() : this;
 		MatrixBlock right1 = getUncompressed(that);
 		MatrixBlock right2 = getUncompressed(that2);
-		left.ternaryOperations(op, right1, right2, resultMap);
+		left.ctableOperations(op, right1, right2, resultMap);
 	}
 
 	@Override
-	public void ternaryOperations(Operator op, MatrixValue that,
+	public void ctableOperations(Operator op, MatrixValue that,
 			MatrixValue that2, CTableMap resultMap, MatrixBlock resultBlock)
 			throws DMLRuntimeException {
 		printDecompressWarning("ternaryOperations");
 		MatrixBlock left = isCompressed() ? decompress() : this;
 		MatrixBlock right1 = getUncompressed(that);
 		MatrixBlock right2 = getUncompressed(that2);
-		left.ternaryOperations(op, right1, right2, resultMap, resultBlock);
+		left.ctableOperations(op, right1, right2, resultMap, resultBlock);
 	}
 
 	@Override
-	public MatrixValue quaternaryOperations(QuaternaryOperator qop,
-			MatrixValue um, MatrixValue vm, MatrixValue wm, MatrixValue out)
+	public MatrixBlock quaternaryOperations(QuaternaryOperator qop,
+			MatrixBlock um, MatrixBlock vm, MatrixBlock wm, MatrixBlock out)
 			throws DMLRuntimeException {
 		return quaternaryOperations(qop, um, vm, wm, out, 1);
 	}
 
 	@Override
-	public MatrixValue quaternaryOperations(QuaternaryOperator qop, MatrixValue um, 
-			MatrixValue vm, MatrixValue wm, MatrixValue out, int k) 
+	public MatrixBlock quaternaryOperations(QuaternaryOperator qop, MatrixBlock um, 
+			MatrixBlock vm, MatrixBlock wm, MatrixBlock out, int k) 
 			throws DMLRuntimeException {
 		printDecompressWarning("quaternaryOperations");
 		MatrixBlock left = isCompressed() ? decompress() : this;

http://git-wip-us.apache.org/repos/asf/systemml/blob/57438103/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 e2bb20a..6b2e3d6 100644
--- a/src/main/java/org/apache/sysml/runtime/instructions/CPInstructionParser.java
+++ b/src/main/java/org/apache/sysml/runtime/instructions/CPInstructionParser.java
@@ -59,7 +59,7 @@ import org.apache.sysml.runtime.instructions.cp.QuaternaryCPInstruction;
 import org.apache.sysml.runtime.instructions.cp.ReorgCPInstruction;
 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.CtableCPInstruction;
 import org.apache.sysml.runtime.instructions.cp.UaggOuterChainCPInstruction;
 import org.apache.sysml.runtime.instructions.cp.UnaryCPInstruction;
 import org.apache.sysml.runtime.instructions.cp.VariableCPInstruction;
@@ -254,8 +254,8 @@ public class CPInstructionParser extends InstructionParser
 		String2CPInstructionType.put( DataGen.SINIT_OPCODE  , CPType.StringInit);
 		String2CPInstructionType.put( DataGen.SAMPLE_OPCODE , CPType.Rand);
 		
-		String2CPInstructionType.put( "ctable", 		CPType.Ternary);
-		String2CPInstructionType.put( "ctableexpand", 	CPType.Ternary);
+		String2CPInstructionType.put( "ctable", 		CPType.Ctable);
+		String2CPInstructionType.put( "ctableexpand", 	CPType.Ctable);
 		
 		//central moment, covariance, quantiles (sort/pick)
 		String2CPInstructionType.put( "cm"    , CPType.CentralMoment);
@@ -327,8 +327,8 @@ public class CPInstructionParser extends InstructionParser
 				else
 					return BinaryCPInstruction.parseInstruction(str);
 			
-			case Ternary:
-				return TernaryCPInstruction.parseInstruction(str);
+			case Ctable:
+				return CtableCPInstruction.parseInstruction(str);
 			
 			case Quaternary:
 				return QuaternaryCPInstruction.parseInstruction(str);

http://git-wip-us.apache.org/repos/asf/systemml/blob/57438103/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 debafe1..fae93f3 100644
--- a/src/main/java/org/apache/sysml/runtime/instructions/MRInstructionParser.java
+++ b/src/main/java/org/apache/sysml/runtime/instructions/MRInstructionParser.java
@@ -74,7 +74,7 @@ import org.apache.sysml.runtime.instructions.mr.ReorgInstruction;
 import org.apache.sysml.runtime.instructions.mr.ReplicateInstruction;
 import org.apache.sysml.runtime.instructions.mr.ScalarInstruction;
 import org.apache.sysml.runtime.instructions.mr.SeqInstruction;
-import org.apache.sysml.runtime.instructions.mr.TernaryInstruction;
+import org.apache.sysml.runtime.instructions.mr.CtableInstruction;
 import org.apache.sysml.runtime.instructions.mr.UaggOuterChainInstruction;
 import org.apache.sysml.runtime.instructions.mr.UnaryInstruction;
 import org.apache.sysml.runtime.instructions.mr.ZeroOutInstruction;
@@ -239,11 +239,11 @@ public class MRInstructionParser extends InstructionParser
 		String2MRInstructionType.put( "csvrblk", MRType.CSVReblock);
 		
 		// Ternary Reorg Instruction Opcodes 
-		String2MRInstructionType.put( "ctabletransform", MRType.Ternary);
-		String2MRInstructionType.put( "ctabletransformscalarweight", MRType.Ternary);
-		String2MRInstructionType.put( "ctableexpandscalarweight", MRType.Ternary);
-		String2MRInstructionType.put( "ctabletransformhistogram", MRType.Ternary);
-		String2MRInstructionType.put( "ctabletransformweightedhistogram", MRType.Ternary);
+		String2MRInstructionType.put( "ctabletransform", MRType.Ctable);
+		String2MRInstructionType.put( "ctabletransformscalarweight", MRType.Ctable);
+		String2MRInstructionType.put( "ctableexpandscalarweight", MRType.Ctable);
+		String2MRInstructionType.put( "ctabletransformhistogram", MRType.Ctable);
+		String2MRInstructionType.put( "ctabletransformweightedhistogram", MRType.Ctable);
 		
 		// Quaternary Instruction Opcodes
 		String2MRInstructionType.put( WeightedSquaredLoss.OPCODE,  MRType.Quaternary);
@@ -362,8 +362,8 @@ public class MRInstructionParser extends InstructionParser
 			case AggregateUnary:
 				return AggregateUnaryInstruction.parseInstruction(str);
 				
-			case Ternary: 
-				return TernaryInstruction.parseInstruction(str);
+			case Ctable: 
+				return CtableInstruction.parseInstruction(str);
 			
 			case Quaternary: 
 				return QuaternaryInstruction.parseInstruction(str);

http://git-wip-us.apache.org/repos/asf/systemml/blob/57438103/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 1ba7776..3098c10 100644
--- a/src/main/java/org/apache/sysml/runtime/instructions/SPInstructionParser.java
+++ b/src/main/java/org/apache/sysml/runtime/instructions/SPInstructionParser.java
@@ -74,7 +74,7 @@ import org.apache.sysml.runtime.instructions.spark.RmmSPInstruction;
 import org.apache.sysml.runtime.instructions.spark.SPInstruction;
 import org.apache.sysml.runtime.instructions.spark.SPInstruction.SPType;
 import org.apache.sysml.runtime.instructions.spark.SpoofSPInstruction;
-import org.apache.sysml.runtime.instructions.spark.TernarySPInstruction;
+import org.apache.sysml.runtime.instructions.spark.CtableSPInstruction;
 import org.apache.sysml.runtime.instructions.spark.Tsmm2SPInstruction;
 import org.apache.sysml.runtime.instructions.spark.TsmmSPInstruction;
 import org.apache.sysml.runtime.instructions.spark.QuantileSortSPInstruction;
@@ -268,8 +268,8 @@ public class SPInstructionParser extends InstructionParser
 		String2SPInstructionType.put( DataGen.SAMPLE_OPCODE, SPType.Rand);
 		
 		//ternary instruction opcodes
-		String2SPInstructionType.put( "ctable", SPType.Ternary);
-		String2SPInstructionType.put( "ctableexpand", SPType.Ternary);
+		String2SPInstructionType.put( "ctable", SPType.Ctable);
+		String2SPInstructionType.put( "ctableexpand", SPType.Ctable);
 		
 		//quaternary instruction opcodes
 		String2SPInstructionType.put( WeightedSquaredLoss.OPCODE,  SPType.Quaternary);
@@ -381,8 +381,8 @@ public class SPInstructionParser extends InstructionParser
 					return BinarySPInstruction.parseInstruction(str);
 				
 			//ternary instructions
-			case Ternary:
-				return TernarySPInstruction.parseInstruction(str);
+			case Ctable:
+				return CtableSPInstruction.parseInstruction(str);
 				
 			//quaternary instructions
 			case Quaternary:

http://git-wip-us.apache.org/repos/asf/systemml/blob/57438103/src/main/java/org/apache/sysml/runtime/instructions/cp/AggregateBinaryCPInstruction.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/sysml/runtime/instructions/cp/AggregateBinaryCPInstruction.java b/src/main/java/org/apache/sysml/runtime/instructions/cp/AggregateBinaryCPInstruction.java
index 4f542d6..32732d3 100644
--- a/src/main/java/org/apache/sysml/runtime/instructions/cp/AggregateBinaryCPInstruction.java
+++ b/src/main/java/org/apache/sysml/runtime/instructions/cp/AggregateBinaryCPInstruction.java
@@ -74,7 +74,7 @@ public class AggregateBinaryCPInstruction extends BinaryCPInstruction {
 		//compute matrix multiplication
 		AggregateBinaryOperator ab_op = (AggregateBinaryOperator) _optr;
 		MatrixBlock main = (matBlock2 instanceof CompressedMatrixBlock) ? matBlock2 : matBlock1;
-		MatrixBlock ret = (MatrixBlock) main.aggregateBinaryOperations(matBlock1, matBlock2, new MatrixBlock(), ab_op);
+		MatrixBlock ret = main.aggregateBinaryOperations(matBlock1, matBlock2, new MatrixBlock(), ab_op);
 		
 		//release inputs/outputs
 		ec.releaseMatrixInput(input1.getName(), getExtendedOpcode());

http://git-wip-us.apache.org/repos/asf/systemml/blob/57438103/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 bebf2ca..6b86029 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,7 +31,7 @@ public abstract class CPInstruction extends Instruction
 {
 	public enum CPType {
 		AggregateUnary, AggregateBinary, AggregateTernary,
-		Unary, Binary, Ternary, Quaternary, BuiltinNary,
+		Unary, Binary, Ctable, Quaternary, BuiltinNary,
 		MultiReturnParameterizedBuiltin, ParameterizedBuiltin, MultiReturnBuiltin,
 		Builtin, Reorg, Variable, External, Append, Rand, QSort, QPick,
 		MatrixIndexing, MMTSJ, PMMJ, MMChain, MatrixReshape, Partition, Compression, SpoofFused,

http://git-wip-us.apache.org/repos/asf/systemml/blob/57438103/src/main/java/org/apache/sysml/runtime/instructions/cp/CtableCPInstruction.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/sysml/runtime/instructions/cp/CtableCPInstruction.java b/src/main/java/org/apache/sysml/runtime/instructions/cp/CtableCPInstruction.java
new file mode 100644
index 0000000..0879b81
--- /dev/null
+++ b/src/main/java/org/apache/sysml/runtime/instructions/cp/CtableCPInstruction.java
@@ -0,0 +1,186 @@
+/*
+ * 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.lops.Ctable;
+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.Instruction;
+import org.apache.sysml.runtime.instructions.InstructionUtils;
+import org.apache.sysml.runtime.matrix.data.CTableMap;
+import org.apache.sysml.runtime.matrix.data.MatrixBlock;
+import org.apache.sysml.runtime.matrix.operators.Operator;
+import org.apache.sysml.runtime.matrix.operators.SimpleOperator;
+import org.apache.sysml.runtime.util.DataConverter;
+import org.apache.sysml.runtime.util.LongLongDoubleHashMap.EntryType;
+
+public class CtableCPInstruction extends ComputationCPInstruction {
+	private final String _outDim1;
+	private final String _outDim2;
+	private final boolean _dim1Literal;
+	private final boolean _dim2Literal;
+	private final boolean _isExpand;
+	private final boolean _ignoreZeros;
+
+	private CtableCPInstruction(Operator op, CPOperand in1, CPOperand in2, CPOperand in3, CPOperand out,
+			String outputDim1, boolean dim1Literal, String outputDim2, boolean dim2Literal, boolean isExpand,
+			boolean ignoreZeros, String opcode, String istr) {
+		super(CPType.Ctable, op, in1, in2, in3, out, opcode, istr);
+		_outDim1 = outputDim1;
+		_dim1Literal = dim1Literal;
+		_outDim2 = outputDim2;
+		_dim2Literal = dim2Literal;
+		_isExpand = isExpand;
+		_ignoreZeros = ignoreZeros;
+	}
+
+	public static CtableCPInstruction parseInstruction(String inst) 
+		throws DMLRuntimeException
+	{
+		String[] parts = InstructionUtils.getInstructionPartsWithValueType(inst);
+		InstructionUtils.checkNumFields ( parts, 7 );
+		
+		String opcode = parts[0];
+		
+		//handle opcode
+		if ( !(opcode.equalsIgnoreCase("ctable") || opcode.equalsIgnoreCase("ctableexpand")) ) {
+			throw new DMLRuntimeException("Unexpected opcode in TertiaryCPInstruction: " + inst);
+		}
+		boolean isExpand = opcode.equalsIgnoreCase("ctableexpand");
+		
+		//handle operands
+		CPOperand in1 = new CPOperand(parts[1]);
+		CPOperand in2 = new CPOperand(parts[2]);
+		CPOperand in3 = new CPOperand(parts[3]);
+		
+		//handle known dimension information
+		String[] dim1Fields = parts[4].split(Instruction.LITERAL_PREFIX);
+		String[] dim2Fields = parts[5].split(Instruction.LITERAL_PREFIX);
+
+		CPOperand out = new CPOperand(parts[6]);
+		boolean ignoreZeros = Boolean.parseBoolean(parts[7]);
+		
+		// ctable does not require any operator, so we simply pass-in a dummy operator with null functionobject
+		return new CtableCPInstruction(new SimpleOperator(null), in1, in2, in3, out, dim1Fields[0], Boolean.parseBoolean(dim1Fields[1]), dim2Fields[0], Boolean.parseBoolean(dim2Fields[1]), isExpand, ignoreZeros, opcode, inst);
+	}
+
+	private Ctable.OperationTypes findCtableOperation() {
+		DataType dt1 = input1.getDataType();
+		DataType dt2 = input2.getDataType();
+		DataType dt3 = input3.getDataType();
+		return Ctable.findCtableOperationByInputDataTypes(dt1, dt2, dt3);
+	}
+	
+	@Override
+	public void processInstruction(ExecutionContext ec) 
+		throws DMLRuntimeException {
+		
+		MatrixBlock matBlock1 = ec.getMatrixInput(input1.getName(), getExtendedOpcode());
+		MatrixBlock matBlock2=null, wtBlock=null;
+		double cst1, cst2;
+		
+		CTableMap resultMap = new CTableMap(EntryType.INT);
+		MatrixBlock resultBlock = null;
+		Ctable.OperationTypes ctableOp = findCtableOperation();
+		ctableOp = _isExpand ? Ctable.OperationTypes.CTABLE_EXPAND_SCALAR_WEIGHT : ctableOp;
+		
+		long outputDim1 = (_dim1Literal ? (long) Double.parseDouble(_outDim1) : (ec.getScalarInput(_outDim1, ValueType.DOUBLE, false)).getLongValue());
+		long outputDim2 = (_dim2Literal ? (long) Double.parseDouble(_outDim2) : (ec.getScalarInput(_outDim2, ValueType.DOUBLE, false)).getLongValue());
+		
+		boolean outputDimsKnown = (outputDim1 != -1 && outputDim2 != -1);
+		if ( outputDimsKnown ) {
+			int inputRows = matBlock1.getNumRows();
+			int inputCols = matBlock1.getNumColumns();
+			boolean sparse = MatrixBlock.evalSparseFormatInMemory(outputDim1, outputDim2, inputRows*inputCols);
+			//only create result block if dense; it is important not to aggregate on sparse result
+			//blocks because it would implicitly turn the O(N) algorithm into O(N log N). 
+			if( !sparse )
+				resultBlock = new MatrixBlock((int)outputDim1, (int)outputDim2, false); 
+		}
+		if( _isExpand ){
+			resultBlock = new MatrixBlock( matBlock1.getNumRows(), Integer.MAX_VALUE, true );
+		}
+		
+		switch(ctableOp) {
+			case CTABLE_TRANSFORM: //(VECTOR)
+				// F=ctable(A,B,W)
+				matBlock2 = ec.getMatrixInput(input2.getName(), getExtendedOpcode());
+				wtBlock = ec.getMatrixInput(input3.getName(), getExtendedOpcode());
+				matBlock1.ctableOperations((SimpleOperator)_optr, matBlock2, wtBlock, resultMap, resultBlock);
+				break;
+			case CTABLE_TRANSFORM_SCALAR_WEIGHT: //(VECTOR/MATRIX)
+				// F = ctable(A,B) or F = ctable(A,B,1)
+				matBlock2 = ec.getMatrixInput(input2.getName(), getExtendedOpcode());
+				cst1 = ec.getScalarInput(input3.getName(), input3.getValueType(), input3.isLiteral()).getDoubleValue();
+				matBlock1.ctableOperations((SimpleOperator)_optr, matBlock2, cst1, _ignoreZeros, resultMap, resultBlock);
+				break;
+			case CTABLE_EXPAND_SCALAR_WEIGHT: //(VECTOR)
+				// F = ctable(seq,A) or F = ctable(seq,B,1)
+				matBlock2 = ec.getMatrixInput(input2.getName(), getExtendedOpcode());
+				cst1 = ec.getScalarInput(input3.getName(), input3.getValueType(), input3.isLiteral()).getDoubleValue();
+				// only resultBlock.rlen known, resultBlock.clen set in operation
+				matBlock1.ctableOperations((SimpleOperator)_optr, matBlock2, cst1, resultBlock);
+				break;
+			case CTABLE_TRANSFORM_HISTOGRAM: //(VECTOR)
+				// F=ctable(A,1) or F = ctable(A,1,1)
+				cst1 = ec.getScalarInput(input2.getName(), input2.getValueType(), input2.isLiteral()).getDoubleValue();
+				cst2 = ec.getScalarInput(input3.getName(), input3.getValueType(), input3.isLiteral()).getDoubleValue();
+				matBlock1.ctableOperations((SimpleOperator)_optr, cst1, cst2, resultMap, resultBlock);
+				break;
+			case CTABLE_TRANSFORM_WEIGHTED_HISTOGRAM: //(VECTOR)
+				// F=ctable(A,1,W)
+				wtBlock = ec.getMatrixInput(input3.getName(), getExtendedOpcode());
+				cst1 = ec.getScalarInput(input2.getName(), input2.getValueType(), input2.isLiteral()).getDoubleValue();
+				matBlock1.ctableOperations((SimpleOperator)_optr, cst1, wtBlock, resultMap, resultBlock);
+				break;
+			
+			default:
+				throw new DMLRuntimeException("Encountered an invalid ctable operation ("+ctableOp+") while executing instruction: " + this.toString());
+		}
+		
+		if(input1.getDataType() == DataType.MATRIX)
+			ec.releaseMatrixInput(input1.getName(), getExtendedOpcode());
+		if(input2.getDataType() == DataType.MATRIX)
+			ec.releaseMatrixInput(input2.getName(), getExtendedOpcode());
+		if(input3.getDataType() == DataType.MATRIX)
+			ec.releaseMatrixInput(input3.getName(), getExtendedOpcode());
+		
+		if ( resultBlock == null ){
+			//we need to respect potentially specified output dimensions here, because we might have 
+			//decided for hash-aggregation just to prevent inefficiency in case of sparse outputs.  
+			if( outputDimsKnown )
+				resultBlock = DataConverter.convertToMatrixBlock( resultMap, (int)outputDim1, (int)outputDim2 );
+			else
+				resultBlock = DataConverter.convertToMatrixBlock( resultMap );
+		}
+		else
+			resultBlock.examSparsity();
+		
+		// Ensure right dense/sparse output representation for special cases
+		// such as ctable expand (guarded by released input memory)
+		if( checkGuardedRepresentationChange(matBlock1, matBlock2, resultBlock) ) {
+			resultBlock.examSparsity();
+		}
+		
+		ec.setMatrixOutput(output.getName(), resultBlock, getExtendedOpcode());
+	}
+}

http://git-wip-us.apache.org/repos/asf/systemml/blob/57438103/src/main/java/org/apache/sysml/runtime/instructions/cp/QuaternaryCPInstruction.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/sysml/runtime/instructions/cp/QuaternaryCPInstruction.java b/src/main/java/org/apache/sysml/runtime/instructions/cp/QuaternaryCPInstruction.java
index b0bb392..ce6dd47 100644
--- a/src/main/java/org/apache/sysml/runtime/instructions/cp/QuaternaryCPInstruction.java
+++ b/src/main/java/org/apache/sysml/runtime/instructions/cp/QuaternaryCPInstruction.java
@@ -29,7 +29,6 @@ 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.data.MatrixBlock;
-import org.apache.sysml.runtime.matrix.data.MatrixValue;
 import org.apache.sysml.runtime.matrix.operators.Operator;
 import org.apache.sysml.runtime.matrix.operators.QuaternaryOperator;
 
@@ -122,7 +121,7 @@ public class QuaternaryCPInstruction extends ComputationCPInstruction {
 		}
 		
 		//core execute
-		MatrixValue out = matBlock1.quaternaryOperations(qop, matBlock2, matBlock3, matBlock4, new MatrixBlock(), _numThreads);
+		MatrixBlock out = matBlock1.quaternaryOperations(qop, matBlock2, matBlock3, matBlock4, new MatrixBlock(), _numThreads);
 		
 		//release inputs and output
 		ec.releaseMatrixInput(input1.getName(), getExtendedOpcode());
@@ -134,14 +133,14 @@ public class QuaternaryCPInstruction extends ComputationCPInstruction {
 				if (input4.getDataType() == DataType.MATRIX) {
 					ec.releaseMatrixInput(input4.getName(), getExtendedOpcode());
 				}
-			ec.setVariable(output.getName(), new DoubleObject(out.getValue(0, 0)));
+			ec.setVariable(output.getName(), new DoubleObject(out.quickGetValue(0, 0)));
 		}
 		else { //wsigmoid / wdivmm / wumm
 			if( qop.wtype3 != null && qop.wtype3.hasFourInputs() )
 				if (input4.getDataType() == DataType.MATRIX) {
 					ec.releaseMatrixInput(input4.getName(), getExtendedOpcode());
 				}
-			ec.setMatrixOutput(output.getName(), (MatrixBlock)out, getExtendedOpcode());
+			ec.setMatrixOutput(output.getName(), out, getExtendedOpcode());
 		}
 	}	
 }

http://git-wip-us.apache.org/repos/asf/systemml/blob/57438103/src/main/java/org/apache/sysml/runtime/instructions/cp/TernaryCPInstruction.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/sysml/runtime/instructions/cp/TernaryCPInstruction.java b/src/main/java/org/apache/sysml/runtime/instructions/cp/TernaryCPInstruction.java
deleted file mode 100644
index a927bba..0000000
--- a/src/main/java/org/apache/sysml/runtime/instructions/cp/TernaryCPInstruction.java
+++ /dev/null
@@ -1,186 +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.lops.Ternary;
-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.Instruction;
-import org.apache.sysml.runtime.instructions.InstructionUtils;
-import org.apache.sysml.runtime.matrix.data.CTableMap;
-import org.apache.sysml.runtime.matrix.data.MatrixBlock;
-import org.apache.sysml.runtime.matrix.operators.Operator;
-import org.apache.sysml.runtime.matrix.operators.SimpleOperator;
-import org.apache.sysml.runtime.util.DataConverter;
-import org.apache.sysml.runtime.util.LongLongDoubleHashMap.EntryType;
-
-public class TernaryCPInstruction extends ComputationCPInstruction {
-	private final String _outDim1;
-	private final String _outDim2;
-	private final boolean _dim1Literal;
-	private final boolean _dim2Literal;
-	private final boolean _isExpand;
-	private final boolean _ignoreZeros;
-
-	private TernaryCPInstruction(Operator op, CPOperand in1, CPOperand in2, CPOperand in3, CPOperand out,
-			String outputDim1, boolean dim1Literal, String outputDim2, boolean dim2Literal, boolean isExpand,
-			boolean ignoreZeros, String opcode, String istr) {
-		super(CPType.Ternary, op, in1, in2, in3, out, opcode, istr);
-		_outDim1 = outputDim1;
-		_dim1Literal = dim1Literal;
-		_outDim2 = outputDim2;
-		_dim2Literal = dim2Literal;
-		_isExpand = isExpand;
-		_ignoreZeros = ignoreZeros;
-	}
-
-	public static TernaryCPInstruction parseInstruction(String inst) 
-		throws DMLRuntimeException
-	{
-		String[] parts = InstructionUtils.getInstructionPartsWithValueType(inst);
-		InstructionUtils.checkNumFields ( parts, 7 );
-		
-		String opcode = parts[0];
-		
-		//handle opcode
-		if ( !(opcode.equalsIgnoreCase("ctable") || opcode.equalsIgnoreCase("ctableexpand")) ) {
-			throw new DMLRuntimeException("Unexpected opcode in TertiaryCPInstruction: " + inst);
-		}
-		boolean isExpand = opcode.equalsIgnoreCase("ctableexpand");
-		
-		//handle operands
-		CPOperand in1 = new CPOperand(parts[1]);
-		CPOperand in2 = new CPOperand(parts[2]);
-		CPOperand in3 = new CPOperand(parts[3]);
-		
-		//handle known dimension information
-		String[] dim1Fields = parts[4].split(Instruction.LITERAL_PREFIX);
-		String[] dim2Fields = parts[5].split(Instruction.LITERAL_PREFIX);
-
-		CPOperand out = new CPOperand(parts[6]);
-		boolean ignoreZeros = Boolean.parseBoolean(parts[7]);
-		
-		// ctable does not require any operator, so we simply pass-in a dummy operator with null functionobject
-		return new TernaryCPInstruction(new SimpleOperator(null), in1, in2, in3, out, dim1Fields[0], Boolean.parseBoolean(dim1Fields[1]), dim2Fields[0], Boolean.parseBoolean(dim2Fields[1]), isExpand, ignoreZeros, opcode, inst);
-	}
-
-	private Ternary.OperationTypes findCtableOperation() {
-		DataType dt1 = input1.getDataType();
-		DataType dt2 = input2.getDataType();
-		DataType dt3 = input3.getDataType();
-		return Ternary.findCtableOperationByInputDataTypes(dt1, dt2, dt3);
-	}
-	
-	@Override
-	public void processInstruction(ExecutionContext ec) 
-		throws DMLRuntimeException {
-		
-		MatrixBlock matBlock1 = ec.getMatrixInput(input1.getName(), getExtendedOpcode());
-		MatrixBlock matBlock2=null, wtBlock=null;
-		double cst1, cst2;
-		
-		CTableMap resultMap = new CTableMap(EntryType.INT);
-		MatrixBlock resultBlock = null;
-		Ternary.OperationTypes ctableOp = findCtableOperation();
-		ctableOp = _isExpand ? Ternary.OperationTypes.CTABLE_EXPAND_SCALAR_WEIGHT : ctableOp;
-		
-		long outputDim1 = (_dim1Literal ? (long) Double.parseDouble(_outDim1) : (ec.getScalarInput(_outDim1, ValueType.DOUBLE, false)).getLongValue());
-		long outputDim2 = (_dim2Literal ? (long) Double.parseDouble(_outDim2) : (ec.getScalarInput(_outDim2, ValueType.DOUBLE, false)).getLongValue());
-		
-		boolean outputDimsKnown = (outputDim1 != -1 && outputDim2 != -1);
-		if ( outputDimsKnown ) {
-			int inputRows = matBlock1.getNumRows();
-			int inputCols = matBlock1.getNumColumns();
-			boolean sparse = MatrixBlock.evalSparseFormatInMemory(outputDim1, outputDim2, inputRows*inputCols);
-			//only create result block if dense; it is important not to aggregate on sparse result
-			//blocks because it would implicitly turn the O(N) algorithm into O(N log N). 
-			if( !sparse )
-				resultBlock = new MatrixBlock((int)outputDim1, (int)outputDim2, false); 
-		}
-		if( _isExpand ){
-			resultBlock = new MatrixBlock( matBlock1.getNumRows(), Integer.MAX_VALUE, true );
-		}
-		
-		switch(ctableOp) {
-		case CTABLE_TRANSFORM: //(VECTOR)
-			// F=ctable(A,B,W)
-			matBlock2 = ec.getMatrixInput(input2.getName(), getExtendedOpcode());
-			wtBlock = ec.getMatrixInput(input3.getName(), getExtendedOpcode());
-			matBlock1.ternaryOperations((SimpleOperator)_optr, matBlock2, wtBlock, resultMap, resultBlock);
-			break;
-		case CTABLE_TRANSFORM_SCALAR_WEIGHT: //(VECTOR/MATRIX)
-			// F = ctable(A,B) or F = ctable(A,B,1)
-			matBlock2 = ec.getMatrixInput(input2.getName(), getExtendedOpcode());
-			cst1 = ec.getScalarInput(input3.getName(), input3.getValueType(), input3.isLiteral()).getDoubleValue();
-			matBlock1.ternaryOperations((SimpleOperator)_optr, matBlock2, cst1, _ignoreZeros, resultMap, resultBlock);
-			break;
-		case CTABLE_EXPAND_SCALAR_WEIGHT: //(VECTOR)
-			// F = ctable(seq,A) or F = ctable(seq,B,1)
-			matBlock2 = ec.getMatrixInput(input2.getName(), getExtendedOpcode());
-			cst1 = ec.getScalarInput(input3.getName(), input3.getValueType(), input3.isLiteral()).getDoubleValue();
-			// only resultBlock.rlen known, resultBlock.clen set in operation
-			matBlock1.ternaryOperations((SimpleOperator)_optr, matBlock2, cst1, resultBlock);
-			break;
-		case CTABLE_TRANSFORM_HISTOGRAM: //(VECTOR)
-			// F=ctable(A,1) or F = ctable(A,1,1)
-			cst1 = ec.getScalarInput(input2.getName(), input2.getValueType(), input2.isLiteral()).getDoubleValue();
-			cst2 = ec.getScalarInput(input3.getName(), input3.getValueType(), input3.isLiteral()).getDoubleValue();
-			matBlock1.ternaryOperations((SimpleOperator)_optr, cst1, cst2, resultMap, resultBlock);
-			break;
-		case CTABLE_TRANSFORM_WEIGHTED_HISTOGRAM: //(VECTOR)
-			// F=ctable(A,1,W)
-			wtBlock = ec.getMatrixInput(input3.getName(), getExtendedOpcode());
-			cst1 = ec.getScalarInput(input2.getName(), input2.getValueType(), input2.isLiteral()).getDoubleValue();
-			matBlock1.ternaryOperations((SimpleOperator)_optr, cst1, wtBlock, resultMap, resultBlock);
-			break;
-		
-		default:
-			throw new DMLRuntimeException("Encountered an invalid ctable operation ("+ctableOp+") while executing instruction: " + this.toString());
-		}
-		
-		if(input1.getDataType() == DataType.MATRIX)
-			ec.releaseMatrixInput(input1.getName(), getExtendedOpcode());
-		if(input2.getDataType() == DataType.MATRIX)
-			ec.releaseMatrixInput(input2.getName(), getExtendedOpcode());
-		if(input3.getDataType() == DataType.MATRIX)
-			ec.releaseMatrixInput(input3.getName(), getExtendedOpcode());
-		
-		if ( resultBlock == null ){
-			//we need to respect potentially specified output dimensions here, because we might have 
-			//decided for hash-aggregation just to prevent inefficiency in case of sparse outputs.  
-			if( outputDimsKnown )
-				resultBlock = DataConverter.convertToMatrixBlock( resultMap, (int)outputDim1, (int)outputDim2 );
-			else
-				resultBlock = DataConverter.convertToMatrixBlock( resultMap );
-		}
-		else
-			resultBlock.examSparsity();
-		
-		// Ensure right dense/sparse output representation for special cases
-		// such as ctable expand (guarded by released input memory)
-		if( checkGuardedRepresentationChange(matBlock1, matBlock2, resultBlock) ) {
-			resultBlock.examSparsity();
-		}
-		
-		ec.setMatrixOutput(output.getName(), resultBlock, getExtendedOpcode());
-	}
-}

http://git-wip-us.apache.org/repos/asf/systemml/blob/57438103/src/main/java/org/apache/sysml/runtime/instructions/mr/AggregateBinaryInstruction.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/sysml/runtime/instructions/mr/AggregateBinaryInstruction.java b/src/main/java/org/apache/sysml/runtime/instructions/mr/AggregateBinaryInstruction.java
index 2766062..1c3d8bc 100644
--- a/src/main/java/org/apache/sysml/runtime/instructions/mr/AggregateBinaryInstruction.java
+++ b/src/main/java/org/apache/sysml/runtime/instructions/mr/AggregateBinaryInstruction.java
@@ -28,6 +28,7 @@ import org.apache.sysml.runtime.DMLRuntimeException;
 import org.apache.sysml.runtime.functionobjects.Multiply;
 import org.apache.sysml.runtime.functionobjects.Plus;
 import org.apache.sysml.runtime.instructions.InstructionUtils;
+import org.apache.sysml.runtime.matrix.data.MatrixBlock;
 import org.apache.sysml.runtime.matrix.data.MatrixIndexes;
 import org.apache.sysml.runtime.matrix.data.MatrixValue;
 import org.apache.sysml.runtime.matrix.data.OperationsOnMatrixValues;
@@ -158,14 +159,14 @@ public class AggregateBinaryInstruction extends BinaryMRInstructionBase implemen
 
 			//process instruction
 			OperationsOnMatrixValues.performAggregateBinary(
-					    in1.getIndexes(), in1.getValue(), 
-						in2.getIndexes(), in2.getValue(), 
-						out.getIndexes(), out.getValue(), 
-						((AggregateBinaryOperator)optr));
+				in1.getIndexes(), (MatrixBlock) in1.getValue(),
+				in2.getIndexes(), (MatrixBlock) in2.getValue(),
+				out.getIndexes(), (MatrixBlock) out.getValue(),
+				((AggregateBinaryOperator)optr));
 			
 			//put the output value in the cache
 			if(out==tempValue)
-				cachedValues.add(output, out);				
+				cachedValues.add(output, out);
 		}
 	}
 	
@@ -192,13 +193,12 @@ public class AggregateBinaryInstruction extends BinaryMRInstructionBase implemen
 			long in2_cols = dcInput.getNumCols();
 			long  in2_colBlocks = (long)Math.ceil(((double)in2_cols)/dcInput.getNumColsPerBlock());
 			
-			for(int bidx=1; bidx <= in2_colBlocks; bidx++) 
-			{	
+			for(int bidx=1; bidx <= in2_colBlocks; bidx++) {
 				// Matrix multiply A[i,k] %*% B[k,bid]
 				
 				// Setup input2 block
 				IndexedMatrixValue in2Block = dcInput.getDataBlock((int)in1.getIndexes().getColumnIndex(), bidx);
-							
+				
 				MatrixValue in2BlockValue = in2Block.getValue(); 
 				MatrixIndexes in2BlockIndex = in2Block.getIndexes();
 				
@@ -206,10 +206,9 @@ public class AggregateBinaryInstruction extends BinaryMRInstructionBase implemen
 				IndexedMatrixValue out = cachedValues.holdPlace(output, valueClass);
 				
 				//process instruction
-				OperationsOnMatrixValues.performAggregateBinary(in1.getIndexes(), in1.getValue(), 
-							in2BlockIndex, in2BlockValue, out.getIndexes(), out.getValue(), 
-							((AggregateBinaryOperator)optr));	
-				
+				OperationsOnMatrixValues.performAggregateBinary(in1.getIndexes(), (MatrixBlock)in1.getValue(), 
+					in2BlockIndex, (MatrixBlock) in2BlockValue, out.getIndexes(), (MatrixBlock)out.getValue(), 
+					((AggregateBinaryOperator)optr));
 				removeOutput &= ( !_outputEmptyBlocks && out.getValue().isEmpty() );
 			}
 		}
@@ -226,7 +225,7 @@ public class AggregateBinaryInstruction extends BinaryMRInstructionBase implemen
 				
 				// Setup input2 block
 				IndexedMatrixValue in1Block = dcInput.getDataBlock(bidx, (int)in2.getIndexes().getRowIndex());
-							
+				
 				MatrixValue in1BlockValue = in1Block.getValue(); 
 				MatrixIndexes in1BlockIndex = in1Block.getIndexes();
 				
@@ -234,14 +233,14 @@ public class AggregateBinaryInstruction extends BinaryMRInstructionBase implemen
 				IndexedMatrixValue out = cachedValues.holdPlace(output, valueClass);
 				
 				//process instruction
-				OperationsOnMatrixValues.performAggregateBinary(in1BlockIndex, in1BlockValue, 
-						in2.getIndexes(), in2.getValue(),
-						out.getIndexes(), out.getValue(), 
-							((AggregateBinaryOperator)optr));
-			
+				OperationsOnMatrixValues.performAggregateBinary(
+					in1BlockIndex, (MatrixBlock)in1BlockValue,
+					in2.getIndexes(), (MatrixBlock)in2.getValue(),
+					out.getIndexes(), (MatrixBlock)out.getValue(),
+					((AggregateBinaryOperator)optr));
 				removeOutput &= ( !_outputEmptyBlocks && out.getValue().isEmpty() );
 			}
-		}		
+		}
 		
 		//empty block output filter (enabled by compiler consumer operation is in CP)
 		if( removeOutput )

http://git-wip-us.apache.org/repos/asf/systemml/blob/57438103/src/main/java/org/apache/sysml/runtime/instructions/mr/CombineTernaryInstruction.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/sysml/runtime/instructions/mr/CombineTernaryInstruction.java b/src/main/java/org/apache/sysml/runtime/instructions/mr/CombineTernaryInstruction.java
index 4bfc94e..7db8616 100644
--- a/src/main/java/org/apache/sysml/runtime/instructions/mr/CombineTernaryInstruction.java
+++ b/src/main/java/org/apache/sysml/runtime/instructions/mr/CombineTernaryInstruction.java
@@ -19,14 +19,14 @@
 
 package org.apache.sysml.runtime.instructions.mr;
 
-import org.apache.sysml.lops.Ternary.OperationTypes;
+import org.apache.sysml.lops.Ctable.OperationTypes;
 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.mapred.CachedValueMap;
 import org.apache.sysml.runtime.matrix.mapred.IndexedMatrixValue;
 
-public class CombineTernaryInstruction extends TernaryInstruction {
+public class CombineTernaryInstruction extends CtableInstruction {
 
 	private CombineTernaryInstruction(OperationTypes op, byte in1, byte in2, byte in3, byte out, String istr) {
 		super(MRType.CombineTernary, op, in1, in2, in3, out, -1, -1, istr);