[MINOR] Consolidate all cp/spark binary instructions (deduplication) Project: http://git-wip-us.apache.org/repos/asf/systemml/repo Commit: http://git-wip-us.apache.org/repos/asf/systemml/commit/cc90a0e5 Tree: http://git-wip-us.apache.org/repos/asf/systemml/tree/cc90a0e5 Diff: http://git-wip-us.apache.org/repos/asf/systemml/diff/cc90a0e5
Branch: refs/heads/master Commit: cc90a0e59e9d4fe54c5cc27a32d5c77034190726 Parents: 4d1ee8e Author: Matthias Boehm <[email protected]> Authored: Thu Jan 4 15:31:26 2018 -0800 Committer: Matthias Boehm <[email protected]> Committed: Thu Jan 4 15:31:26 2018 -0800 ---------------------------------------------------------------------- .../hops/cost/CostEstimatorStaticRuntime.java | 28 ++--- .../parfor/opt/ProgramRecompiler.java | 4 +- .../sysml/runtime/functionobjects/Builtin.java | 7 +- .../instructions/CPInstructionParser.java | 60 +++++------ .../runtime/instructions/InstructionUtils.java | 24 +++++ .../instructions/SPInstructionParser.java | 106 +++++++++---------- .../cp/ArithmeticBinaryCPInstruction.java | 57 ---------- .../instructions/cp/BinaryCPInstruction.java | 24 ++++- .../cp/BinaryMatrixMatrixCPInstruction.java | 69 ++++++++++++ .../cp/BinaryMatrixScalarCPInstruction.java | 60 +++++++++++ .../cp/BinaryScalarScalarCPInstruction.java | 65 ++++++++++++ .../cp/BooleanBinaryCPInstruction.java | 4 +- .../cp/BuiltinBinaryCPInstruction.java | 57 ---------- .../runtime/instructions/cp/CPInstruction.java | 6 +- .../cp/MatrixMatrixArithmeticCPInstruction.java | 59 ----------- .../cp/MatrixMatrixBuiltinCPInstruction.java | 67 ------------ .../cp/MatrixMatrixRelationalCPInstruction.java | 57 ---------- .../cp/MatrixScalarBuiltinCPInstruction.java | 60 ----------- .../instructions/cp/PlusMultCPInstruction.java | 2 +- .../cp/RelationalBinaryCPInstruction.java | 55 ---------- .../cp/ScalarMatrixArithmeticCPInstruction.java | 61 ----------- .../cp/ScalarMatrixRelationalCPInstruction.java | 61 ----------- .../cp/ScalarScalarArithmeticCPInstruction.java | 66 ------------ .../cp/ScalarScalarBuiltinCPInstruction.java | 57 ---------- .../cp/ScalarScalarRelationalCPInstruction.java | 57 ---------- .../spark/ArithmeticBinarySPInstruction.java | 84 --------------- .../spark/BinaryMatrixBVectorSPInstruction.java | 44 ++++++++ .../spark/BinaryMatrixMatrixSPInstruction.java | 41 +++++++ .../spark/BinaryMatrixScalarSPInstruction.java | 41 +++++++ .../instructions/spark/BinarySPInstruction.java | 46 +++++++- .../spark/BuiltinBinarySPInstruction.java | 88 --------------- .../MatrixBVectorArithmeticSPInstruction.java | 51 --------- .../MatrixBVectorBuiltinSPInstruction.java | 49 --------- .../MatrixBVectorRelationalSPInstruction.java | 51 --------- .../MatrixMatrixArithmeticSPInstruction.java | 41 ------- .../spark/MatrixMatrixBuiltinSPInstruction.java | 41 ------- .../MatrixMatrixRelationalSPInstruction.java | 47 -------- .../MatrixScalarArithmeticSPInstruction.java | 51 --------- .../spark/MatrixScalarBuiltinSPInstruction.java | 49 --------- .../MatrixScalarRelationalSPInstruction.java | 48 --------- .../spark/PlusMultSPInstruction.java | 6 +- .../spark/RelationalBinarySPInstruction.java | 83 --------------- .../instructions/spark/SPInstruction.java | 4 +- 43 files changed, 514 insertions(+), 1524 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/systemml/blob/cc90a0e5/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 40bfe04..c2627ef 100644 --- a/src/main/java/org/apache/sysml/hops/cost/CostEstimatorStaticRuntime.java +++ b/src/main/java/org/apache/sysml/hops/cost/CostEstimatorStaticRuntime.java @@ -870,16 +870,20 @@ public class CostEstimatorStaticRuntime extends CostEstimator || optype.equals("uarimax") || optype.equals("ua*") ) return d1m * d1n; - return 0; + return 0; - case ArithmeticBinary: //opcodes: +, -, *, /, ^ (incl. ^2, *2) + case Binary: //opcodes: +, -, *, /, ^ (incl. ^2, *2), + //max, min, solve, ==, !=, <, >, <=, >= + //note: all relational ops are not sparsesafe //note: covers scalar-scalar, scalar-matrix, matrix-matrix if( optype.equals("+") || optype.equals("-") //sparse safe && ( leftSparse || rightSparse ) ) return d1m*d1n*d1s + d2m*d2n*d2s; + else if( optype.equals("solve") ) //see also MultiReturnBuiltin + return d1m * d1n * d1n; //for 1kx1k ~ 1GFLOP -> 0.5s else return d3m*d3n; - + case Ternary: //opcodes: ctable if( optype.equals("ctable") ){ if( leftSparse ) @@ -888,10 +892,10 @@ public class CostEstimatorStaticRuntime extends CostEstimator return d1m * d1n; } return 0; - + case BooleanBinary: //opcodes: &&, || return 1; //always scalar-scalar - + case BooleanUnary: //opcodes: ! return 1; //always scalar-scalar @@ -903,14 +907,6 @@ public class CostEstimatorStaticRuntime extends CostEstimator else //unary return d3m * d3n; - case BuiltinBinary: //opcodes: max, min, solve - //note: covers scalar-scalar, scalar-matrix, matrix-matrix - if( optype.equals("solve") ) //see also MultiReturnBuiltin - return d1m * d1n * d1n; //for 1kx1k ~ 1GFLOP -> 0.5s - else //default - return d3m * d3n; - - case BuiltinUnary: //opcodes: exp, abs, sin, cos, tan, sign, sqrt, plogp, print, round, sprop, sigmoid //TODO add cost functions for commons math builtins: inverse, cholesky if( optype.equals("print") ) //scalar only @@ -945,11 +941,7 @@ public class CostEstimatorStaticRuntime extends CostEstimator return DEFAULT_NFLOP_CP * (((leftSparse) ? d1m * d1n * d1s : d1m * d1n ) + ((rightSparse) ? d2m * d2n * d2s : d2m * d2n )); - - case RelationalBinary: //opcodes: ==, !=, <, >, <=, >= - //note: all relational ops are not sparsesafe - return d3m * d3n; //covers all combinations of scalar and matrix - + case Variable: //opcodes: assignvar, cpvar, rmvar, rmfilevar, assignvarwithfile, attachfiletovar, valuepick, iqsize, read, write, createvar, setfilename, castAsMatrix if( optype.equals("write") ){ boolean text = args[0].equals("textcell") || args[0].equals("csv"); http://git-wip-us.apache.org/repos/asf/systemml/blob/cc90a0e5/src/main/java/org/apache/sysml/runtime/controlprogram/parfor/opt/ProgramRecompiler.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/sysml/runtime/controlprogram/parfor/opt/ProgramRecompiler.java b/src/main/java/org/apache/sysml/runtime/controlprogram/parfor/opt/ProgramRecompiler.java index 98b104f..d16d708 100644 --- a/src/main/java/org/apache/sysml/runtime/controlprogram/parfor/opt/ProgramRecompiler.java +++ b/src/main/java/org/apache/sysml/runtime/controlprogram/parfor/opt/ProgramRecompiler.java @@ -50,7 +50,7 @@ import org.apache.sysml.runtime.controlprogram.ProgramBlock; import org.apache.sysml.runtime.controlprogram.WhileProgramBlock; import org.apache.sysml.runtime.controlprogram.context.ExecutionContext; import org.apache.sysml.runtime.instructions.Instruction; -import org.apache.sysml.runtime.instructions.cp.ArithmeticBinaryCPInstruction; +import org.apache.sysml.runtime.instructions.cp.BinaryCPInstruction; import org.apache.sysml.runtime.instructions.cp.Data; import org.apache.sysml.runtime.instructions.cp.FunctionCallCPInstruction; import org.apache.sysml.runtime.instructions.cp.ScalarObject; @@ -477,7 +477,7 @@ public class ProgramRecompiler //create instruction set ArrayList<Instruction> tmp = new ArrayList<>(); - Instruction inst = ArithmeticBinaryCPInstruction.parseInstruction(str); + Instruction inst = BinaryCPInstruction.parseInstruction(str); tmp.add(inst); return tmp; http://git-wip-us.apache.org/repos/asf/systemml/blob/cc90a0e5/src/main/java/org/apache/sysml/runtime/functionobjects/Builtin.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/sysml/runtime/functionobjects/Builtin.java b/src/main/java/org/apache/sysml/runtime/functionobjects/Builtin.java index 4310781..12da2bd 100644 --- a/src/main/java/org/apache/sysml/runtime/functionobjects/Builtin.java +++ b/src/main/java/org/apache/sysml/runtime/functionobjects/Builtin.java @@ -114,8 +114,11 @@ public class Builtin extends ValueFunction return bFunc; } - public static Builtin getBuiltinFnObject (String str) - { + public static boolean isBuiltinFnObject(String str) { + return String2BuiltinCode.containsKey(str); + } + + public static Builtin getBuiltinFnObject(String str) { BuiltinCode code = String2BuiltinCode.get(str); return getBuiltinFnObject( code ); } http://git-wip-us.apache.org/repos/asf/systemml/blob/cc90a0e5/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 c1fc7c5..b739ef0 100644 --- a/src/main/java/org/apache/sysml/runtime/instructions/CPInstructionParser.java +++ b/src/main/java/org/apache/sysml/runtime/instructions/CPInstructionParser.java @@ -33,10 +33,9 @@ import org.apache.sysml.runtime.instructions.cp.AggregateBinaryCPInstruction; import org.apache.sysml.runtime.instructions.cp.AggregateTernaryCPInstruction; import org.apache.sysml.runtime.instructions.cp.AggregateUnaryCPInstruction; import org.apache.sysml.runtime.instructions.cp.AppendCPInstruction; -import org.apache.sysml.runtime.instructions.cp.ArithmeticBinaryCPInstruction; +import org.apache.sysml.runtime.instructions.cp.BinaryCPInstruction; import org.apache.sysml.runtime.instructions.cp.BooleanBinaryCPInstruction; import org.apache.sysml.runtime.instructions.cp.BooleanUnaryCPInstruction; -import org.apache.sysml.runtime.instructions.cp.BuiltinBinaryCPInstruction; import org.apache.sysml.runtime.instructions.cp.BuiltinNaryCPInstruction; import org.apache.sysml.runtime.instructions.cp.BuiltinUnaryCPInstruction; import org.apache.sysml.runtime.instructions.cp.CPInstruction; @@ -60,7 +59,6 @@ import org.apache.sysml.runtime.instructions.cp.PlusMultCPInstruction; import org.apache.sysml.runtime.instructions.cp.QuantilePickCPInstruction; import org.apache.sysml.runtime.instructions.cp.QuantileSortCPInstruction; import org.apache.sysml.runtime.instructions.cp.QuaternaryCPInstruction; -import org.apache.sysml.runtime.instructions.cp.RelationalBinaryCPInstruction; import org.apache.sysml.runtime.instructions.cp.ReorgCPInstruction; import org.apache.sysml.runtime.instructions.cp.SpoofCPInstruction; import org.apache.sysml.runtime.instructions.cp.StringInitCPInstruction; @@ -115,19 +113,19 @@ public class CPInstructionParser extends InstructionParser String2CPInstructionType.put( "uaggouterchain", CPType.UaggOuterChain); // Arithmetic Instruction Opcodes - String2CPInstructionType.put( "+" , CPType.ArithmeticBinary); - String2CPInstructionType.put( "-" , CPType.ArithmeticBinary); - String2CPInstructionType.put( "*" , CPType.ArithmeticBinary); - String2CPInstructionType.put( "/" , CPType.ArithmeticBinary); - String2CPInstructionType.put( "%%" , CPType.ArithmeticBinary); - String2CPInstructionType.put( "%/%" , CPType.ArithmeticBinary); - String2CPInstructionType.put( "^" , CPType.ArithmeticBinary); - String2CPInstructionType.put( "1-*" , CPType.ArithmeticBinary); //special * case - String2CPInstructionType.put( "^2" , CPType.ArithmeticBinary); //special ^ case - String2CPInstructionType.put( "*2" , CPType.ArithmeticBinary); //special * case - String2CPInstructionType.put( "-nz" , CPType.ArithmeticBinary); //special - case - String2CPInstructionType.put( "+*" , CPType.ArithmeticBinary); - String2CPInstructionType.put( "-*" , CPType.ArithmeticBinary); + String2CPInstructionType.put( "+" , CPType.Binary); + String2CPInstructionType.put( "-" , CPType.Binary); + String2CPInstructionType.put( "*" , CPType.Binary); + String2CPInstructionType.put( "/" , CPType.Binary); + String2CPInstructionType.put( "%%" , CPType.Binary); + String2CPInstructionType.put( "%/%" , CPType.Binary); + String2CPInstructionType.put( "^" , CPType.Binary); + String2CPInstructionType.put( "1-*" , CPType.Binary); //special * case + 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.BooleanBinary); @@ -136,20 +134,20 @@ public class CPInstructionParser extends InstructionParser String2CPInstructionType.put( "!" , CPType.BooleanUnary); // Relational Instruction Opcodes - String2CPInstructionType.put( "==" , CPType.RelationalBinary); - String2CPInstructionType.put( "!=" , CPType.RelationalBinary); - String2CPInstructionType.put( "<" , CPType.RelationalBinary); - String2CPInstructionType.put( ">" , CPType.RelationalBinary); - String2CPInstructionType.put( "<=" , CPType.RelationalBinary); - String2CPInstructionType.put( ">=" , CPType.RelationalBinary); + String2CPInstructionType.put( "==" , CPType.Binary); + String2CPInstructionType.put( "!=" , CPType.Binary); + String2CPInstructionType.put( "<" , CPType.Binary); + String2CPInstructionType.put( ">" , CPType.Binary); + String2CPInstructionType.put( "<=" , CPType.Binary); + String2CPInstructionType.put( ">=" , CPType.Binary); // Builtin Instruction Opcodes String2CPInstructionType.put( "log" , CPType.Builtin); String2CPInstructionType.put( "log_nz" , CPType.Builtin); - String2CPInstructionType.put( "max" , CPType.BuiltinBinary); - String2CPInstructionType.put( "min" , CPType.BuiltinBinary); - String2CPInstructionType.put( "solve" , CPType.BuiltinBinary); + String2CPInstructionType.put( "max" , CPType.Binary); + String2CPInstructionType.put( "min" , CPType.Binary); + String2CPInstructionType.put( "solve" , CPType.Binary); String2CPInstructionType.put( "exp" , CPType.BuiltinUnary); String2CPInstructionType.put( "abs" , CPType.BuiltinUnary); @@ -320,12 +318,12 @@ public class CPInstructionParser extends InstructionParser case AggregateTernary: return AggregateTernaryCPInstruction.parseInstruction(str); - case ArithmeticBinary: + case Binary: String opcode = InstructionUtils.getOpCode(str); if( opcode.equals("+*") || opcode.equals("-*") ) return PlusMultCPInstruction.parseInstruction(str); else - return ArithmeticBinaryCPInstruction.parseInstruction(str); + return BinaryCPInstruction.parseInstruction(str); case Ternary: return TernaryCPInstruction.parseInstruction(str); @@ -339,9 +337,6 @@ public class CPInstructionParser extends InstructionParser case BooleanUnary: return BooleanUnaryCPInstruction.parseInstruction(str); - case BuiltinBinary: - return BuiltinBinaryCPInstruction.parseInstruction(str); - case BuiltinUnary: return BuiltinUnaryCPInstruction.parseInstruction(str); @@ -362,9 +357,6 @@ public class CPInstructionParser extends InstructionParser case Append: return AppendCPInstruction.parseInstruction(str); - - case RelationalBinary: - return RelationalBinaryCPInstruction.parseInstruction(str); case Variable: return VariableCPInstruction.parseInstruction(str); @@ -412,7 +404,7 @@ public class CPInstructionParser extends InstructionParser return BuiltinUnaryCPInstruction.parseInstruction(str); } else if ( parts.length == 4 ) { // B=log(A,10), y=log(x,10) - return BuiltinBinaryCPInstruction.parseInstruction(str); + return BinaryCPInstruction.parseInstruction(str); } } else { http://git-wip-us.apache.org/repos/asf/systemml/blob/cc90a0e5/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 633d80f..14f4d49 100644 --- a/src/main/java/org/apache/sysml/runtime/instructions/InstructionUtils.java +++ b/src/main/java/org/apache/sysml/runtime/instructions/InstructionUtils.java @@ -74,6 +74,7 @@ import org.apache.sysml.runtime.functionobjects.ReduceDiag; import org.apache.sysml.runtime.functionobjects.ReduceRow; import org.apache.sysml.runtime.functionobjects.Xor; import org.apache.sysml.runtime.instructions.cp.CPInstruction.CPType; +import org.apache.sysml.runtime.instructions.cp.CPOperand; import org.apache.sysml.runtime.instructions.gpu.GPUInstruction.GPUINSTRUCTION_TYPE; import org.apache.sysml.runtime.instructions.mr.MRInstruction.MRType; import org.apache.sysml.runtime.instructions.spark.SPInstruction.SPType; @@ -82,6 +83,7 @@ import org.apache.sysml.runtime.matrix.operators.AggregateTernaryOperator; import org.apache.sysml.runtime.matrix.operators.AggregateUnaryOperator; import org.apache.sysml.runtime.matrix.operators.BinaryOperator; 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.CMOperator.AggregateOperationTypes; @@ -481,6 +483,28 @@ public class InstructionUtils return aggun; } + public static Operator parseBinaryOrBuiltinOperator(String opcode, CPOperand in1, CPOperand in2) + throws DMLRuntimeException + { + boolean matrixScalar = (in1.getDataType() != in2.getDataType()); + return Builtin.isBuiltinFnObject(opcode) ? + (matrixScalar ? new RightScalarOperator( Builtin.getBuiltinFnObject(opcode), 0) : + new BinaryOperator( Builtin.getBuiltinFnObject(opcode))) : + (matrixScalar ? parseScalarBinaryOperator(opcode, in1.getDataType().isScalar()) : + parseBinaryOperator(opcode)); + } + + public static Operator parseExtendedBinaryOrBuiltinOperator(String opcode, CPOperand in1, CPOperand in2) + throws DMLRuntimeException + { + boolean matrixScalar = (in1.getDataType() != in2.getDataType()); + return Builtin.isBuiltinFnObject(opcode) ? + (matrixScalar ? new RightScalarOperator( Builtin.getBuiltinFnObject(opcode), 0) : + new BinaryOperator( Builtin.getBuiltinFnObject(opcode))) : + (matrixScalar ? parseScalarBinaryOperator(opcode, in1.getDataType().isScalar()) : + parseExtendedBinaryOperator(opcode)); + } + public static BinaryOperator parseBinaryOperator(String opcode) throws DMLRuntimeException { http://git-wip-us.apache.org/repos/asf/systemml/blob/cc90a0e5/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 703e585..c60756f 100644 --- a/src/main/java/org/apache/sysml/runtime/instructions/SPInstructionParser.java +++ b/src/main/java/org/apache/sysml/runtime/instructions/SPInstructionParser.java @@ -43,9 +43,8 @@ import org.apache.sysml.runtime.instructions.spark.AppendGAlignedSPInstruction; import org.apache.sysml.runtime.instructions.spark.AppendGSPInstruction; import org.apache.sysml.runtime.instructions.spark.AppendMSPInstruction; import org.apache.sysml.runtime.instructions.spark.AppendRSPInstruction; -import org.apache.sysml.runtime.instructions.spark.ArithmeticBinarySPInstruction; import org.apache.sysml.runtime.instructions.spark.BinUaggChainSPInstruction; -import org.apache.sysml.runtime.instructions.spark.BuiltinBinarySPInstruction; +import org.apache.sysml.runtime.instructions.spark.BinarySPInstruction; import org.apache.sysml.runtime.instructions.spark.BuiltinNarySPInstruction; import org.apache.sysml.runtime.instructions.spark.BuiltinUnarySPInstruction; import org.apache.sysml.runtime.instructions.spark.CSVReblockSPInstruction; @@ -71,7 +70,6 @@ import org.apache.sysml.runtime.instructions.spark.QuantilePickSPInstruction; import org.apache.sysml.runtime.instructions.spark.QuaternarySPInstruction; import org.apache.sysml.runtime.instructions.spark.RandSPInstruction; import org.apache.sysml.runtime.instructions.spark.ReblockSPInstruction; -import org.apache.sysml.runtime.instructions.spark.RelationalBinarySPInstruction; import org.apache.sysml.runtime.instructions.spark.ReorgSPInstruction; import org.apache.sysml.runtime.instructions.spark.RmmSPInstruction; import org.apache.sysml.runtime.instructions.spark.SPInstruction; @@ -154,51 +152,51 @@ public class SPInstructionParser extends InstructionParser String2SPInstructionType.put( "rshape" , SPType.MatrixReshape); String2SPInstructionType.put( "rsort" , SPType.Reorg); - String2SPInstructionType.put( "+" , SPType.ArithmeticBinary); - String2SPInstructionType.put( "-" , SPType.ArithmeticBinary); - String2SPInstructionType.put( "*" , SPType.ArithmeticBinary); - String2SPInstructionType.put( "/" , SPType.ArithmeticBinary); - String2SPInstructionType.put( "%%" , SPType.ArithmeticBinary); - String2SPInstructionType.put( "%/%" , SPType.ArithmeticBinary); - String2SPInstructionType.put( "1-*" , SPType.ArithmeticBinary); - String2SPInstructionType.put( "^" , SPType.ArithmeticBinary); - String2SPInstructionType.put( "^2" , SPType.ArithmeticBinary); - String2SPInstructionType.put( "*2" , SPType.ArithmeticBinary); - String2SPInstructionType.put( "+*" , SPType.ArithmeticBinary); - String2SPInstructionType.put( "-*" , SPType.ArithmeticBinary); - String2SPInstructionType.put( "map+" , SPType.ArithmeticBinary); - String2SPInstructionType.put( "map-" , SPType.ArithmeticBinary); - String2SPInstructionType.put( "map*" , SPType.ArithmeticBinary); - String2SPInstructionType.put( "map/" , SPType.ArithmeticBinary); - String2SPInstructionType.put( "map%%" , SPType.ArithmeticBinary); - String2SPInstructionType.put( "map%/%" , SPType.ArithmeticBinary); - String2SPInstructionType.put( "map1-*" , SPType.ArithmeticBinary); - String2SPInstructionType.put( "map^" , SPType.ArithmeticBinary); - String2SPInstructionType.put( "map+*" , SPType.ArithmeticBinary); - String2SPInstructionType.put( "map-*" , SPType.ArithmeticBinary); + String2SPInstructionType.put( "+" , SPType.Binary); + String2SPInstructionType.put( "-" , SPType.Binary); + String2SPInstructionType.put( "*" , SPType.Binary); + String2SPInstructionType.put( "/" , SPType.Binary); + String2SPInstructionType.put( "%%" , SPType.Binary); + String2SPInstructionType.put( "%/%" , SPType.Binary); + String2SPInstructionType.put( "1-*" , SPType.Binary); + 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); + String2SPInstructionType.put( "map/" , SPType.Binary); + String2SPInstructionType.put( "map%%" , SPType.Binary); + String2SPInstructionType.put( "map%/%" , SPType.Binary); + String2SPInstructionType.put( "map1-*" , SPType.Binary); + String2SPInstructionType.put( "map^" , SPType.Binary); + String2SPInstructionType.put( "map+*" , SPType.Binary); + String2SPInstructionType.put( "map-*" , SPType.Binary); // Relational Instruction Opcodes - String2SPInstructionType.put( "==" , SPType.RelationalBinary); - String2SPInstructionType.put( "!=" , SPType.RelationalBinary); - String2SPInstructionType.put( "<" , SPType.RelationalBinary); - String2SPInstructionType.put( ">" , SPType.RelationalBinary); - String2SPInstructionType.put( "<=" , SPType.RelationalBinary); - String2SPInstructionType.put( ">=" , SPType.RelationalBinary); - String2SPInstructionType.put( "map>" , SPType.RelationalBinary); - String2SPInstructionType.put( "map>=" , SPType.RelationalBinary); - String2SPInstructionType.put( "map<" , SPType.RelationalBinary); - String2SPInstructionType.put( "map<=" , SPType.RelationalBinary); - String2SPInstructionType.put( "map==" , SPType.RelationalBinary); - String2SPInstructionType.put( "map!=" , SPType.RelationalBinary); + String2SPInstructionType.put( "==" , SPType.Binary); + String2SPInstructionType.put( "!=" , SPType.Binary); + String2SPInstructionType.put( "<" , SPType.Binary); + String2SPInstructionType.put( ">" , 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); + String2SPInstructionType.put( "map<=" , SPType.Binary); + String2SPInstructionType.put( "map==" , SPType.Binary); + String2SPInstructionType.put( "map!=" , SPType.Binary); // Boolean Instruction Opcodes - String2SPInstructionType.put( "&&" , SPType.ArithmeticBinary); - String2SPInstructionType.put( "||" , SPType.ArithmeticBinary); - String2SPInstructionType.put( "xor" , SPType.ArithmeticBinary); + String2SPInstructionType.put( "&&" , SPType.Binary); + String2SPInstructionType.put( "||" , SPType.Binary); + String2SPInstructionType.put( "xor" , SPType.Binary); String2SPInstructionType.put( "!" , SPType.BuiltinUnary); - String2SPInstructionType.put( "map&&" , SPType.ArithmeticBinary); - String2SPInstructionType.put( "map||" , SPType.ArithmeticBinary); - String2SPInstructionType.put( "mapxor" , SPType.ArithmeticBinary); + String2SPInstructionType.put( "map&&" , SPType.Binary); + String2SPInstructionType.put( "map||" , SPType.Binary); + String2SPInstructionType.put( "mapxor" , SPType.Binary); String2SPInstructionType.put( "map!" , SPType.BuiltinUnary); // REBLOCK Instruction Opcodes @@ -214,10 +212,10 @@ public class SPInstructionParser extends InstructionParser String2SPInstructionType.put( "log_nz" , SPType.Builtin); // Boolean Binary builtin - String2SPInstructionType.put( "max" , SPType.BuiltinBinary); - String2SPInstructionType.put( "min" , SPType.BuiltinBinary); - String2SPInstructionType.put( "mapmax" , SPType.BuiltinBinary); - String2SPInstructionType.put( "mapmin" , SPType.BuiltinBinary); + String2SPInstructionType.put( "max" , SPType.Builtin); + String2SPInstructionType.put( "min" , SPType.Builtin); + String2SPInstructionType.put( "mapmax" , SPType.Builtin); + String2SPInstructionType.put( "mapmin" , SPType.Builtin); String2SPInstructionType.put( "exp" , SPType.BuiltinUnary); String2SPInstructionType.put( "abs" , SPType.BuiltinUnary); @@ -367,15 +365,12 @@ public class SPInstructionParser extends InstructionParser case Reorg: return ReorgSPInstruction.parseInstruction(str); - case ArithmeticBinary: + case Binary: String opcode = InstructionUtils.getOpCode(str); if( opcode.equals("+*") || opcode.equals("-*") ) return PlusMultSPInstruction.parseInstruction(str); - else - return ArithmeticBinarySPInstruction.parseInstruction(str); - - case RelationalBinary: - return RelationalBinarySPInstruction.parseInstruction(str); + else + return BinarySPInstruction.parseInstruction(str); //ternary instructions case Ternary: @@ -400,16 +395,13 @@ public class SPInstructionParser extends InstructionParser return BuiltinUnarySPInstruction.parseInstruction(str); } else if ( parts.length == 4 ) { // B=log(A,10), y=log(x,10) - return BuiltinBinarySPInstruction.parseInstruction(str); + return BinarySPInstruction.parseInstruction(str); } } else { throw new DMLRuntimeException("Invalid Builtin Instruction: " + str ); } - case BuiltinBinary: - return BuiltinBinarySPInstruction.parseInstruction(str); - case BuiltinUnary: return BuiltinUnarySPInstruction.parseInstruction(str); http://git-wip-us.apache.org/repos/asf/systemml/blob/cc90a0e5/src/main/java/org/apache/sysml/runtime/instructions/cp/ArithmeticBinaryCPInstruction.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/sysml/runtime/instructions/cp/ArithmeticBinaryCPInstruction.java b/src/main/java/org/apache/sysml/runtime/instructions/cp/ArithmeticBinaryCPInstruction.java deleted file mode 100644 index ade199a..0000000 --- a/src/main/java/org/apache/sysml/runtime/instructions/cp/ArithmeticBinaryCPInstruction.java +++ /dev/null @@ -1,57 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package org.apache.sysml.runtime.instructions.cp; - -import org.apache.sysml.parser.Expression.DataType; -import org.apache.sysml.parser.Expression.ValueType; -import org.apache.sysml.runtime.DMLRuntimeException; -import org.apache.sysml.runtime.instructions.InstructionUtils; -import org.apache.sysml.runtime.matrix.operators.Operator; - - -public abstract class ArithmeticBinaryCPInstruction extends BinaryCPInstruction { - - protected ArithmeticBinaryCPInstruction(Operator op, CPOperand in1, CPOperand in2, CPOperand out, String opcode, - String istr) { - super(CPType.ArithmeticBinary, op, in1, in2, out, opcode, istr); - } - - public static ArithmeticBinaryCPInstruction parseInstruction ( String str ) - throws DMLRuntimeException - { - CPOperand in1 = new CPOperand("", ValueType.UNKNOWN, DataType.UNKNOWN); - CPOperand in2 = new CPOperand("", ValueType.UNKNOWN, DataType.UNKNOWN); - CPOperand out = new CPOperand("", ValueType.UNKNOWN, DataType.UNKNOWN); - String opcode = parseBinaryInstruction(str, in1, in2, out); - - checkOutputDataType(in1, in2, out); - - Operator operator = (in1.getDataType() != in2.getDataType()) ? - InstructionUtils.parseScalarBinaryOperator(opcode, (in1.getDataType() == DataType.SCALAR)) : - InstructionUtils.parseBinaryOperator(opcode); - - if( in1.getDataType() == DataType.SCALAR && in2.getDataType() == DataType.SCALAR ) - return new ScalarScalarArithmeticCPInstruction(operator, in1, in2, out, opcode, str); - else if( in1.getDataType() == DataType.MATRIX && in2.getDataType() == DataType.MATRIX ) - return new MatrixMatrixArithmeticCPInstruction(operator, in1, in2, out, opcode, str); - else - return new ScalarMatrixArithmeticCPInstruction(operator, in1, in2, out, opcode, str); - } -} http://git-wip-us.apache.org/repos/asf/systemml/blob/cc90a0e5/src/main/java/org/apache/sysml/runtime/instructions/cp/BinaryCPInstruction.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/sysml/runtime/instructions/cp/BinaryCPInstruction.java b/src/main/java/org/apache/sysml/runtime/instructions/cp/BinaryCPInstruction.java index 7d7382b..f0bdbd1 100644 --- a/src/main/java/org/apache/sysml/runtime/instructions/cp/BinaryCPInstruction.java +++ b/src/main/java/org/apache/sysml/runtime/instructions/cp/BinaryCPInstruction.java @@ -20,14 +20,14 @@ package org.apache.sysml.runtime.instructions.cp; import org.apache.sysml.parser.Expression.DataType; +import org.apache.sysml.parser.Expression.ValueType; import org.apache.sysml.runtime.DMLRuntimeException; import org.apache.sysml.runtime.instructions.InstructionUtils; import org.apache.sysml.runtime.matrix.operators.Operator; public abstract class BinaryCPInstruction extends ComputationCPInstruction { - protected BinaryCPInstruction(CPType type, Operator op, CPOperand in1, CPOperand in2, CPOperand out, String opcode, - String istr) { + protected BinaryCPInstruction(CPType type, Operator op, CPOperand in1, CPOperand in2, CPOperand out, String opcode, String istr) { super(type, op, in1, in2, out, opcode, istr); } @@ -36,6 +36,26 @@ public abstract class BinaryCPInstruction extends ComputationCPInstruction { super(type, op, in1, in2, in3, out, opcode, istr); } + public static BinaryCPInstruction parseInstruction( String str ) + throws DMLRuntimeException + { + CPOperand in1 = new CPOperand("", ValueType.UNKNOWN, DataType.UNKNOWN); + CPOperand in2 = new CPOperand("", ValueType.UNKNOWN, DataType.UNKNOWN); + CPOperand out = new CPOperand("", ValueType.UNKNOWN, DataType.UNKNOWN); + String opcode = parseBinaryInstruction(str, in1, in2, out); + + checkOutputDataType(in1, in2, out); + + Operator operator = InstructionUtils.parseBinaryOrBuiltinOperator(opcode, in1, in2); + + if( in1.getDataType() == DataType.SCALAR && in2.getDataType() == DataType.SCALAR ) + return new BinaryScalarScalarCPInstruction(operator, in1, in2, out, opcode, str); + else if( in1.getDataType() == DataType.MATRIX && in2.getDataType() == DataType.MATRIX ) + return new BinaryMatrixMatrixCPInstruction(operator, in1, in2, out, opcode, str); + else + return new BinaryMatrixScalarCPInstruction(operator, in1, in2, out, opcode, str); + } + protected static String parseBinaryInstruction(String instr, CPOperand in1, CPOperand in2, CPOperand out) throws DMLRuntimeException { http://git-wip-us.apache.org/repos/asf/systemml/blob/cc90a0e5/src/main/java/org/apache/sysml/runtime/instructions/cp/BinaryMatrixMatrixCPInstruction.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/sysml/runtime/instructions/cp/BinaryMatrixMatrixCPInstruction.java b/src/main/java/org/apache/sysml/runtime/instructions/cp/BinaryMatrixMatrixCPInstruction.java new file mode 100644 index 0000000..e584a85 --- /dev/null +++ b/src/main/java/org/apache/sysml/runtime/instructions/cp/BinaryMatrixMatrixCPInstruction.java @@ -0,0 +1,69 @@ +/* + * 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.caching.MatrixObject; +import org.apache.sysml.runtime.controlprogram.context.ExecutionContext; +import org.apache.sysml.runtime.matrix.data.LibCommonsMath; +import org.apache.sysml.runtime.matrix.data.MatrixBlock; +import org.apache.sysml.runtime.matrix.operators.BinaryOperator; +import org.apache.sysml.runtime.matrix.operators.Operator; + +public class BinaryMatrixMatrixCPInstruction extends BinaryCPInstruction { + + protected BinaryMatrixMatrixCPInstruction(Operator op, CPOperand in1, CPOperand in2, CPOperand out, + String opcode, String istr) { + super(CPType.Binary, op, in1, in2, out, opcode, istr); + } + + @Override + public void processInstruction(ExecutionContext ec) + throws DMLRuntimeException + { + String opcode = getOpcode(); + if ( LibCommonsMath.isSupportedMatrixMatrixOperation(opcode) ) { + MatrixBlock solution = LibCommonsMath.matrixMatrixOperations( + ec.getMatrixObject(input1.getName()), (MatrixObject)ec.getVariable(input2.getName()), opcode); + ec.setMatrixOutput(output.getName(), solution, getExtendedOpcode()); + return; + } + + // Read input matrices + MatrixBlock inBlock1 = ec.getMatrixInput(input1.getName(), getExtendedOpcode()); + MatrixBlock inBlock2 = ec.getMatrixInput(input2.getName(), getExtendedOpcode()); + + // Perform computation using input matrices, and produce the result matrix + BinaryOperator bop = (BinaryOperator) _optr; + MatrixBlock retBlock = (MatrixBlock) (inBlock1.binaryOperations (bop, inBlock2, new MatrixBlock())); + + // Release the memory occupied by input matrices + ec.releaseMatrixInput(input1.getName(), getExtendedOpcode()); + ec.releaseMatrixInput(input2.getName(), getExtendedOpcode()); + + // Ensure right dense/sparse output representation (guarded by released input memory) + if( checkGuardedRepresentationChange(inBlock1, inBlock2, retBlock) ) { + retBlock.examSparsity(); + } + + // Attach result matrix with MatrixObject associated with output_name + ec.setMatrixOutput(output.getName(), retBlock, getExtendedOpcode()); + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/systemml/blob/cc90a0e5/src/main/java/org/apache/sysml/runtime/instructions/cp/BinaryMatrixScalarCPInstruction.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/sysml/runtime/instructions/cp/BinaryMatrixScalarCPInstruction.java b/src/main/java/org/apache/sysml/runtime/instructions/cp/BinaryMatrixScalarCPInstruction.java new file mode 100644 index 0000000..c809049 --- /dev/null +++ b/src/main/java/org/apache/sysml/runtime/instructions/cp/BinaryMatrixScalarCPInstruction.java @@ -0,0 +1,60 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.sysml.runtime.instructions.cp; + +import org.apache.sysml.parser.Expression.DataType; +import org.apache.sysml.runtime.DMLRuntimeException; +import org.apache.sysml.runtime.controlprogram.context.ExecutionContext; +import org.apache.sysml.runtime.matrix.data.MatrixBlock; +import org.apache.sysml.runtime.matrix.operators.Operator; +import org.apache.sysml.runtime.matrix.operators.ScalarOperator; + +public class BinaryMatrixScalarCPInstruction extends BinaryCPInstruction { + + protected BinaryMatrixScalarCPInstruction(Operator op, CPOperand in1, CPOperand in2, CPOperand out, + String opcode, String istr) { + super(CPType.Binary, op, in1, in2, out, opcode, istr); + } + + @Override + public void processInstruction(ExecutionContext ec) + throws DMLRuntimeException + { + CPOperand mat = ( input1.getDataType() == DataType.MATRIX ) ? input1 : input2; + CPOperand scalar = ( input1.getDataType() == DataType.MATRIX ) ? input2 : input1; + + MatrixBlock inBlock = ec.getMatrixInput(mat.getName(), getExtendedOpcode()); + ScalarObject constant = (ScalarObject) ec.getScalarInput(scalar.getName(), scalar.getValueType(), scalar.isLiteral()); + + ScalarOperator sc_op = (ScalarOperator) _optr; + sc_op = sc_op.setConstant(constant.getDoubleValue()); + + MatrixBlock retBlock = (MatrixBlock) inBlock.scalarOperations(sc_op, new MatrixBlock()); + + ec.releaseMatrixInput(mat.getName(), getExtendedOpcode()); + + // Ensure right dense/sparse output representation (guarded by released input memory) + if( checkGuardedRepresentationChange(inBlock, retBlock) ) { + retBlock.examSparsity(); + } + + ec.setMatrixOutput(output.getName(), retBlock, getExtendedOpcode()); + } +} http://git-wip-us.apache.org/repos/asf/systemml/blob/cc90a0e5/src/main/java/org/apache/sysml/runtime/instructions/cp/BinaryScalarScalarCPInstruction.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/sysml/runtime/instructions/cp/BinaryScalarScalarCPInstruction.java b/src/main/java/org/apache/sysml/runtime/instructions/cp/BinaryScalarScalarCPInstruction.java new file mode 100644 index 0000000..a577938 --- /dev/null +++ b/src/main/java/org/apache/sysml/runtime/instructions/cp/BinaryScalarScalarCPInstruction.java @@ -0,0 +1,65 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.sysml.runtime.instructions.cp; + +import org.apache.sysml.parser.Expression.ValueType; +import org.apache.sysml.runtime.DMLRuntimeException; +import org.apache.sysml.runtime.controlprogram.context.ExecutionContext; +import org.apache.sysml.runtime.matrix.operators.BinaryOperator; +import org.apache.sysml.runtime.matrix.operators.Operator; + +public class BinaryScalarScalarCPInstruction extends BinaryCPInstruction { + + protected BinaryScalarScalarCPInstruction(Operator op, CPOperand in1, CPOperand in2, CPOperand out, String opcode, String istr) { + super(CPType.Binary, op, in1, in2, out, opcode, istr); + } + + @Override + public void processInstruction(ExecutionContext ec) throws DMLRuntimeException{ + ScalarObject so1 = ec.getScalarInput(input1.getName(), input1.getValueType(), input1.isLiteral()); + ScalarObject so2 = ec.getScalarInput(input2.getName(), input2.getValueType(), input2.isLiteral() ); + + BinaryOperator dop = (BinaryOperator) _optr; + ScalarObject sores = null; + + //compute output value, incl implicit type promotion if necessary + if( so1 instanceof StringObject || so2 instanceof StringObject ) { + if( !getOpcode().equals("+") ) //not string concatenation + throw new DMLRuntimeException("Arithmetic '"+getOpcode()+"' not supported over string inputs."); + sores = new StringObject( dop.fn.execute( + so1.getLanguageSpecificStringValue(), so2.getLanguageSpecificStringValue()) ); + } + else if( so1 instanceof DoubleObject || so2 instanceof DoubleObject || output.getValueType()==ValueType.DOUBLE ) { + sores = new DoubleObject( dop.fn.execute(so1.getDoubleValue(), so2.getDoubleValue()) ); + } + else if( so1 instanceof IntObject || so2 instanceof IntObject ) { + double tmp = dop.fn.execute(so1.getLongValue(), so2.getLongValue()); + if( tmp > Long.MAX_VALUE ) //cast to long if no overflow, otherwise controlled exception + throw new DMLRuntimeException("Integer operation created numerical result overflow ("+tmp+" > "+Long.MAX_VALUE+")."); + sores = new IntObject((long) tmp); + } + else { //all boolean + //NOTE: boolean-boolean arithmetic treated as double for consistency with R + sores = new DoubleObject( dop.fn.execute(so1.getDoubleValue(), so2.getDoubleValue()) ); + } + + ec.setScalarOutput(output.getName(), sores); + } +} http://git-wip-us.apache.org/repos/asf/systemml/blob/cc90a0e5/src/main/java/org/apache/sysml/runtime/instructions/cp/BooleanBinaryCPInstruction.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/sysml/runtime/instructions/cp/BooleanBinaryCPInstruction.java b/src/main/java/org/apache/sysml/runtime/instructions/cp/BooleanBinaryCPInstruction.java index 53a62b6..dad7d97 100644 --- a/src/main/java/org/apache/sysml/runtime/instructions/cp/BooleanBinaryCPInstruction.java +++ b/src/main/java/org/apache/sysml/runtime/instructions/cp/BooleanBinaryCPInstruction.java @@ -46,9 +46,9 @@ public class BooleanBinaryCPInstruction extends BinaryCPInstruction { if ( in1.getDataType() == DataType.SCALAR && in2.getDataType() == DataType.SCALAR ) return new BooleanBinaryCPInstruction(bop, in1, in2, out, opcode, str); else if ( in1.getDataType() == DataType.MATRIX && in2.getDataType() == DataType.MATRIX ) - return new MatrixMatrixArithmeticCPInstruction(bop, in1, in2, out, opcode, str); + return new BinaryMatrixMatrixCPInstruction(bop, in1, in2, out, opcode, str); else - return new ScalarMatrixArithmeticCPInstruction(bop, in1, in2, out, opcode, str); + return new BinaryMatrixScalarCPInstruction(bop, in1, in2, out, opcode, str); } @Override http://git-wip-us.apache.org/repos/asf/systemml/blob/cc90a0e5/src/main/java/org/apache/sysml/runtime/instructions/cp/BuiltinBinaryCPInstruction.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/sysml/runtime/instructions/cp/BuiltinBinaryCPInstruction.java b/src/main/java/org/apache/sysml/runtime/instructions/cp/BuiltinBinaryCPInstruction.java deleted file mode 100644 index eca6e94..0000000 --- a/src/main/java/org/apache/sysml/runtime/instructions/cp/BuiltinBinaryCPInstruction.java +++ /dev/null @@ -1,57 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package org.apache.sysml.runtime.instructions.cp; - -import org.apache.sysml.parser.Expression.DataType; -import org.apache.sysml.parser.Expression.ValueType; -import org.apache.sysml.runtime.DMLRuntimeException; -import org.apache.sysml.runtime.functionobjects.Builtin; -import org.apache.sysml.runtime.functionobjects.ValueFunction; -import org.apache.sysml.runtime.matrix.operators.BinaryOperator; -import org.apache.sysml.runtime.matrix.operators.Operator; -import org.apache.sysml.runtime.matrix.operators.RightScalarOperator; - -public abstract class BuiltinBinaryCPInstruction extends BinaryCPInstruction { - - protected BuiltinBinaryCPInstruction(Operator op, CPOperand in1, CPOperand in2, CPOperand out, - String opcode, String istr) { - super(CPType.BuiltinBinary, op, in1, in2, out, opcode, istr); - } - - public static BuiltinBinaryCPInstruction parseInstruction ( String str ) - throws DMLRuntimeException { - CPOperand in1 = new CPOperand("", ValueType.UNKNOWN, DataType.UNKNOWN); - CPOperand in2 = new CPOperand("", ValueType.UNKNOWN, DataType.UNKNOWN); - CPOperand out = new CPOperand("", ValueType.UNKNOWN, DataType.UNKNOWN); - String opcode = parseBinaryInstruction(str, in1, in2, out); - - checkOutputDataType(in1, in2, out); - - // Determine appropriate Function Object based on opcode - ValueFunction func = Builtin.getBuiltinFnObject(opcode); - - if ( in1.getDataType() == DataType.SCALAR && in2.getDataType() == DataType.SCALAR ) - return new ScalarScalarBuiltinCPInstruction(new BinaryOperator(func), in1, in2, out, opcode, str); - else if ( in1.getDataType() == DataType.MATRIX && in2.getDataType() == DataType.MATRIX ) - return new MatrixMatrixBuiltinCPInstruction(new BinaryOperator(func), in1, in2, out, opcode, str); - else - return new MatrixScalarBuiltinCPInstruction(new RightScalarOperator(func, 0), in1, in2, out, opcode, str); - } -} http://git-wip-us.apache.org/repos/asf/systemml/blob/cc90a0e5/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 5a20b22..cdf5805 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 @@ -30,10 +30,10 @@ import org.apache.sysml.runtime.matrix.operators.Operator; public abstract class CPInstruction extends Instruction { public enum CPType { - AggregateUnary, AggregateBinary, AggregateTernary, ArithmeticBinary, - Ternary, Quaternary, BooleanBinary, BooleanUnary, BuiltinBinary, BuiltinUnary, BuiltinNary, + AggregateUnary, AggregateBinary, AggregateTernary, + Binary, Ternary, Quaternary, BooleanBinary, BooleanUnary, BuiltinUnary, BuiltinNary, MultiReturnParameterizedBuiltin, ParameterizedBuiltin, MultiReturnBuiltin, - Builtin, Reorg, RelationalBinary, Variable, External, Append, Rand, QSort, QPick, + Builtin, Reorg, Variable, External, Append, Rand, QSort, QPick, MatrixIndexing, MMTSJ, PMMJ, MMChain, MatrixReshape, Partition, Compression, SpoofFused, StringInit, CentralMoment, Covariance, UaggOuterChain, Convolution } http://git-wip-us.apache.org/repos/asf/systemml/blob/cc90a0e5/src/main/java/org/apache/sysml/runtime/instructions/cp/MatrixMatrixArithmeticCPInstruction.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/sysml/runtime/instructions/cp/MatrixMatrixArithmeticCPInstruction.java b/src/main/java/org/apache/sysml/runtime/instructions/cp/MatrixMatrixArithmeticCPInstruction.java deleted file mode 100644 index 6333f9b..0000000 --- a/src/main/java/org/apache/sysml/runtime/instructions/cp/MatrixMatrixArithmeticCPInstruction.java +++ /dev/null @@ -1,59 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package org.apache.sysml.runtime.instructions.cp; - -import org.apache.sysml.runtime.DMLRuntimeException; -import org.apache.sysml.runtime.controlprogram.context.ExecutionContext; -import org.apache.sysml.runtime.matrix.data.MatrixBlock; -import org.apache.sysml.runtime.matrix.operators.BinaryOperator; -import org.apache.sysml.runtime.matrix.operators.Operator; - -public class MatrixMatrixArithmeticCPInstruction extends ArithmeticBinaryCPInstruction { - - protected MatrixMatrixArithmeticCPInstruction(Operator op, CPOperand in1, CPOperand in2, CPOperand out, - String opcode, String istr) { - super(op, in1, in2, out, opcode, istr); - } - - @Override - public void processInstruction(ExecutionContext ec) - throws DMLRuntimeException - { - // Read input matrices - MatrixBlock inBlock1 = ec.getMatrixInput(input1.getName(), getExtendedOpcode()); - MatrixBlock inBlock2 = ec.getMatrixInput(input2.getName(), getExtendedOpcode()); - - // Perform computation using input matrices, and produce the result matrix - BinaryOperator bop = (BinaryOperator) _optr; - MatrixBlock retBlock = (MatrixBlock) (inBlock1.binaryOperations (bop, inBlock2, new MatrixBlock())); - - // Release the memory occupied by input matrices - ec.releaseMatrixInput(input1.getName(), getExtendedOpcode()); - ec.releaseMatrixInput(input2.getName(), getExtendedOpcode()); - - // Ensure right dense/sparse output representation (guarded by released input memory) - if( checkGuardedRepresentationChange(inBlock1, inBlock2, retBlock) ) { - retBlock.examSparsity(); - } - - // Attach result matrix with MatrixObject associated with output_name - ec.setMatrixOutput(output.getName(), retBlock, getExtendedOpcode()); - } -} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/systemml/blob/cc90a0e5/src/main/java/org/apache/sysml/runtime/instructions/cp/MatrixMatrixBuiltinCPInstruction.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/sysml/runtime/instructions/cp/MatrixMatrixBuiltinCPInstruction.java b/src/main/java/org/apache/sysml/runtime/instructions/cp/MatrixMatrixBuiltinCPInstruction.java deleted file mode 100644 index 24af9b5..0000000 --- a/src/main/java/org/apache/sysml/runtime/instructions/cp/MatrixMatrixBuiltinCPInstruction.java +++ /dev/null @@ -1,67 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package org.apache.sysml.runtime.instructions.cp; - -import org.apache.sysml.runtime.DMLRuntimeException; -import org.apache.sysml.runtime.controlprogram.caching.MatrixObject; -import org.apache.sysml.runtime.controlprogram.context.ExecutionContext; -import org.apache.sysml.runtime.matrix.data.LibCommonsMath; -import org.apache.sysml.runtime.matrix.data.MatrixBlock; -import org.apache.sysml.runtime.matrix.operators.BinaryOperator; -import org.apache.sysml.runtime.matrix.operators.Operator; - -public class MatrixMatrixBuiltinCPInstruction extends BuiltinBinaryCPInstruction { - - protected MatrixMatrixBuiltinCPInstruction(Operator op, CPOperand in1, CPOperand in2, CPOperand out, String opcode, - String istr) { - super(op, in1, in2, out, opcode, istr); - } - - @Override - public void processInstruction(ExecutionContext ec) - throws DMLRuntimeException - { - String opcode = getOpcode(); - - if ( LibCommonsMath.isSupportedMatrixMatrixOperation(opcode) ) { - MatrixBlock solution = LibCommonsMath.matrixMatrixOperations(ec.getMatrixObject(input1.getName()), (MatrixObject)ec.getVariable(input2.getName()), opcode); - ec.setMatrixOutput(output.getName(), solution, getExtendedOpcode()); - return; - } - - String output_name = output.getName(); - BinaryOperator bop = (BinaryOperator) _optr; - - MatrixBlock inBlock1 = ec.getMatrixInput(input1.getName(), getExtendedOpcode()); - MatrixBlock inBlock2 = ec.getMatrixInput(input2.getName(), getExtendedOpcode()); - - MatrixBlock retBlock = (MatrixBlock) inBlock1.binaryOperations(bop, inBlock2, new MatrixBlock()); - - ec.releaseMatrixInput(input1.getName(), getExtendedOpcode()); - ec.releaseMatrixInput(input2.getName(), getExtendedOpcode()); - - // Ensure right dense/sparse output representation (guarded by released input memory) - if( checkGuardedRepresentationChange(inBlock1, inBlock2, retBlock) ) { - retBlock.examSparsity(); - } - - ec.setMatrixOutput(output_name, retBlock, getExtendedOpcode()); - } -} http://git-wip-us.apache.org/repos/asf/systemml/blob/cc90a0e5/src/main/java/org/apache/sysml/runtime/instructions/cp/MatrixMatrixRelationalCPInstruction.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/sysml/runtime/instructions/cp/MatrixMatrixRelationalCPInstruction.java b/src/main/java/org/apache/sysml/runtime/instructions/cp/MatrixMatrixRelationalCPInstruction.java deleted file mode 100644 index c45f758..0000000 --- a/src/main/java/org/apache/sysml/runtime/instructions/cp/MatrixMatrixRelationalCPInstruction.java +++ /dev/null @@ -1,57 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package org.apache.sysml.runtime.instructions.cp; - -import org.apache.sysml.runtime.DMLRuntimeException; -import org.apache.sysml.runtime.controlprogram.context.ExecutionContext; -import org.apache.sysml.runtime.matrix.data.MatrixBlock; -import org.apache.sysml.runtime.matrix.operators.BinaryOperator; -import org.apache.sysml.runtime.matrix.operators.Operator; - -public class MatrixMatrixRelationalCPInstruction extends RelationalBinaryCPInstruction { - - protected MatrixMatrixRelationalCPInstruction(Operator op, CPOperand in1, CPOperand in2, CPOperand out, - String opcode, String istr) { - super(op, in1, in2, out, opcode, istr); - } - - @Override - public void processInstruction(ExecutionContext ec) - throws DMLRuntimeException - { - MatrixBlock inBlock1 = ec.getMatrixInput(input1.getName(), getExtendedOpcode()); - MatrixBlock inBlock2 = ec.getMatrixInput(input2.getName(), getExtendedOpcode()); - - String output_name = output.getName(); - BinaryOperator bop = (BinaryOperator) _optr; - - MatrixBlock retBlock = (MatrixBlock) inBlock1.binaryOperations(bop, inBlock2, new MatrixBlock()); - - ec.releaseMatrixInput(input1.getName(), getExtendedOpcode()); - ec.releaseMatrixInput(input2.getName(), getExtendedOpcode()); - - // Ensure right dense/sparse output representation (guarded by released input memory) - if( checkGuardedRepresentationChange(inBlock1, inBlock2, retBlock) ) { - retBlock.examSparsity(); - } - - ec.setMatrixOutput(output_name, retBlock, getExtendedOpcode()); - } -} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/systemml/blob/cc90a0e5/src/main/java/org/apache/sysml/runtime/instructions/cp/MatrixScalarBuiltinCPInstruction.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/sysml/runtime/instructions/cp/MatrixScalarBuiltinCPInstruction.java b/src/main/java/org/apache/sysml/runtime/instructions/cp/MatrixScalarBuiltinCPInstruction.java deleted file mode 100644 index 964957e..0000000 --- a/src/main/java/org/apache/sysml/runtime/instructions/cp/MatrixScalarBuiltinCPInstruction.java +++ /dev/null @@ -1,60 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package org.apache.sysml.runtime.instructions.cp; - -import org.apache.sysml.parser.Expression.DataType; -import org.apache.sysml.runtime.DMLRuntimeException; -import org.apache.sysml.runtime.controlprogram.context.ExecutionContext; -import org.apache.sysml.runtime.matrix.data.MatrixBlock; -import org.apache.sysml.runtime.matrix.operators.Operator; -import org.apache.sysml.runtime.matrix.operators.ScalarOperator; - -public class MatrixScalarBuiltinCPInstruction extends BuiltinBinaryCPInstruction { - - protected MatrixScalarBuiltinCPInstruction(Operator op, CPOperand in1, CPOperand in2, CPOperand out, String opcode, - String instr) { - super(op, in1, in2, out, opcode, instr); - } - - @Override - public void processInstruction(ExecutionContext ec) - throws DMLRuntimeException - { - CPOperand mat = ( input1.getDataType() == DataType.MATRIX ) ? input1 : input2; - CPOperand scalar = ( input1.getDataType() == DataType.MATRIX ) ? input2 : input1; - - MatrixBlock inBlock = ec.getMatrixInput(mat.getName(), getExtendedOpcode()); - ScalarObject constant = (ScalarObject) ec.getScalarInput(scalar.getName(), scalar.getValueType(), scalar.isLiteral()); - - ScalarOperator sc_op = (ScalarOperator) _optr; - sc_op = sc_op.setConstant(constant.getDoubleValue()); - - MatrixBlock retBlock = (MatrixBlock) inBlock.scalarOperations(sc_op, new MatrixBlock()); - - ec.releaseMatrixInput(mat.getName(), getExtendedOpcode()); - - // Ensure right dense/sparse output representation (guarded by released input memory) - if( checkGuardedRepresentationChange(inBlock, retBlock) ) { - retBlock.examSparsity(); - } - - ec.setMatrixOutput(output.getName(), retBlock, getExtendedOpcode()); - } -} http://git-wip-us.apache.org/repos/asf/systemml/blob/cc90a0e5/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 index 27f8521..c74786c 100644 --- a/src/main/java/org/apache/sysml/runtime/instructions/cp/PlusMultCPInstruction.java +++ b/src/main/java/org/apache/sysml/runtime/instructions/cp/PlusMultCPInstruction.java @@ -32,7 +32,7 @@ public class PlusMultCPInstruction extends ComputationCPInstruction { private PlusMultCPInstruction(BinaryOperator op, CPOperand in1, CPOperand in2, CPOperand in3, CPOperand out, String opcode, String str) { - super(CPType.ArithmeticBinary, op, in1, in2, in3, out, opcode, str); + super(CPType.Binary, op, in1, in2, in3, out, opcode, str); } public static PlusMultCPInstruction parseInstruction(String str) http://git-wip-us.apache.org/repos/asf/systemml/blob/cc90a0e5/src/main/java/org/apache/sysml/runtime/instructions/cp/RelationalBinaryCPInstruction.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/sysml/runtime/instructions/cp/RelationalBinaryCPInstruction.java b/src/main/java/org/apache/sysml/runtime/instructions/cp/RelationalBinaryCPInstruction.java deleted file mode 100644 index b04aa9f..0000000 --- a/src/main/java/org/apache/sysml/runtime/instructions/cp/RelationalBinaryCPInstruction.java +++ /dev/null @@ -1,55 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package org.apache.sysml.runtime.instructions.cp; - -import org.apache.sysml.parser.Expression.DataType; -import org.apache.sysml.parser.Expression.ValueType; -import org.apache.sysml.runtime.DMLRuntimeException; -import org.apache.sysml.runtime.instructions.InstructionUtils; -import org.apache.sysml.runtime.matrix.operators.Operator; - -public abstract class RelationalBinaryCPInstruction extends BinaryCPInstruction { - - protected RelationalBinaryCPInstruction(Operator op, CPOperand in1, CPOperand in2, CPOperand out, String opcode, - String istr) { - super(CPType.RelationalBinary, op, in1, in2, out, opcode, istr); - } - - public static RelationalBinaryCPInstruction parseInstruction ( String str ) throws DMLRuntimeException { - InstructionUtils.checkNumFields (str, 3); - CPOperand in1 = new CPOperand("", ValueType.UNKNOWN, DataType.UNKNOWN); - CPOperand in2 = new CPOperand("", ValueType.UNKNOWN, DataType.UNKNOWN); - CPOperand out = new CPOperand("", ValueType.UNKNOWN, DataType.UNKNOWN); - String opcode = parseBinaryInstruction(str, in1, in2, out); - - checkOutputDataType(in1, in2, out); - - Operator operator = (in1.getDataType() != in2.getDataType()) ? - InstructionUtils.parseScalarBinaryOperator(opcode, (in1.getDataType() == DataType.SCALAR)) : - InstructionUtils.parseBinaryOperator(opcode); - - if ( in1.getDataType() == DataType.SCALAR && in2.getDataType() == DataType.SCALAR ) - return new ScalarScalarRelationalCPInstruction(operator, in1, in2, out, opcode, str); - else if ( in1.getDataType() == DataType.MATRIX && in2.getDataType() == DataType.MATRIX ) - return new MatrixMatrixRelationalCPInstruction(operator, in1, in2, out, opcode, str); - else - return new ScalarMatrixRelationalCPInstruction(operator, in1, in2, out, opcode, str); - } -} http://git-wip-us.apache.org/repos/asf/systemml/blob/cc90a0e5/src/main/java/org/apache/sysml/runtime/instructions/cp/ScalarMatrixArithmeticCPInstruction.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/sysml/runtime/instructions/cp/ScalarMatrixArithmeticCPInstruction.java b/src/main/java/org/apache/sysml/runtime/instructions/cp/ScalarMatrixArithmeticCPInstruction.java deleted file mode 100644 index e1f177f..0000000 --- a/src/main/java/org/apache/sysml/runtime/instructions/cp/ScalarMatrixArithmeticCPInstruction.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.cp; - -import org.apache.sysml.parser.Expression.DataType; -import org.apache.sysml.runtime.DMLRuntimeException; -import org.apache.sysml.runtime.controlprogram.context.ExecutionContext; -import org.apache.sysml.runtime.matrix.data.MatrixBlock; -import org.apache.sysml.runtime.matrix.operators.Operator; -import org.apache.sysml.runtime.matrix.operators.ScalarOperator; - -// TODO rename to MatrixScalar... -public class ScalarMatrixArithmeticCPInstruction extends ArithmeticBinaryCPInstruction { - - protected ScalarMatrixArithmeticCPInstruction(Operator op, CPOperand in1, CPOperand in2, CPOperand out, - String opcode, String istr) { - super(op, in1, in2, out, opcode, istr); - } - - @Override - public void processInstruction(ExecutionContext ec) - throws DMLRuntimeException - { - CPOperand mat = ( input1.getDataType() == DataType.MATRIX ) ? input1 : input2; - CPOperand scalar = ( input1.getDataType() == DataType.MATRIX ) ? input2 : input1; - - MatrixBlock inBlock = ec.getMatrixInput(mat.getName(), getExtendedOpcode()); - ScalarObject constant = (ScalarObject) ec.getScalarInput(scalar.getName(), scalar.getValueType(), scalar.isLiteral()); - - ScalarOperator sc_op = (ScalarOperator) _optr; - sc_op = sc_op.setConstant(constant.getDoubleValue()); - - MatrixBlock retBlock = (MatrixBlock) inBlock.scalarOperations(sc_op, new MatrixBlock()); - - ec.releaseMatrixInput(mat.getName(), getExtendedOpcode()); - - // Ensure right dense/sparse output representation (guarded by released input memory) - if( checkGuardedRepresentationChange(inBlock, retBlock) ) { - retBlock.examSparsity(); - } - - ec.setMatrixOutput(output.getName(), retBlock, getExtendedOpcode()); - } -} http://git-wip-us.apache.org/repos/asf/systemml/blob/cc90a0e5/src/main/java/org/apache/sysml/runtime/instructions/cp/ScalarMatrixRelationalCPInstruction.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/sysml/runtime/instructions/cp/ScalarMatrixRelationalCPInstruction.java b/src/main/java/org/apache/sysml/runtime/instructions/cp/ScalarMatrixRelationalCPInstruction.java deleted file mode 100644 index 12f545d..0000000 --- a/src/main/java/org/apache/sysml/runtime/instructions/cp/ScalarMatrixRelationalCPInstruction.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.cp; - -import org.apache.sysml.parser.Expression.DataType; -import org.apache.sysml.runtime.DMLRuntimeException; -import org.apache.sysml.runtime.controlprogram.context.ExecutionContext; -import org.apache.sysml.runtime.matrix.data.MatrixBlock; -import org.apache.sysml.runtime.matrix.operators.Operator; -import org.apache.sysml.runtime.matrix.operators.ScalarOperator; - -//TODO rename to MatrixScalar... -public class ScalarMatrixRelationalCPInstruction extends RelationalBinaryCPInstruction { - - protected ScalarMatrixRelationalCPInstruction(Operator op, CPOperand in1, CPOperand in2, CPOperand out, - String opcode, String istr) { - super(op, in1, in2, out, opcode, istr); - } - - @Override - public void processInstruction(ExecutionContext ec) - throws DMLRuntimeException - { - CPOperand mat = ( input1.getDataType() == DataType.MATRIX ) ? input1 : input2; - CPOperand scalar = ( input1.getDataType() == DataType.MATRIX ) ? input2 : input1; - - MatrixBlock inBlock = ec.getMatrixInput(mat.getName(), getExtendedOpcode()); - ScalarObject constant = (ScalarObject) ec.getScalarInput(scalar.getName(), scalar.getValueType(), scalar.isLiteral()); - - ScalarOperator sc_op = (ScalarOperator) _optr; - sc_op = sc_op.setConstant(constant.getDoubleValue()); - - MatrixBlock retBlock = (MatrixBlock) inBlock.scalarOperations(sc_op, new MatrixBlock()); - - ec.releaseMatrixInput(mat.getName(), getExtendedOpcode()); - - // Ensure right dense/sparse output representation (guarded by released input memory) - if( checkGuardedRepresentationChange(inBlock, retBlock) ) { - retBlock.examSparsity(); - } - - ec.setMatrixOutput(output.getName(), retBlock, getExtendedOpcode()); - } -} http://git-wip-us.apache.org/repos/asf/systemml/blob/cc90a0e5/src/main/java/org/apache/sysml/runtime/instructions/cp/ScalarScalarArithmeticCPInstruction.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/sysml/runtime/instructions/cp/ScalarScalarArithmeticCPInstruction.java b/src/main/java/org/apache/sysml/runtime/instructions/cp/ScalarScalarArithmeticCPInstruction.java deleted file mode 100644 index 9afef27..0000000 --- a/src/main/java/org/apache/sysml/runtime/instructions/cp/ScalarScalarArithmeticCPInstruction.java +++ /dev/null @@ -1,66 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package org.apache.sysml.runtime.instructions.cp; - -import org.apache.sysml.parser.Expression.ValueType; -import org.apache.sysml.runtime.DMLRuntimeException; -import org.apache.sysml.runtime.controlprogram.context.ExecutionContext; -import org.apache.sysml.runtime.matrix.operators.BinaryOperator; -import org.apache.sysml.runtime.matrix.operators.Operator; - -public class ScalarScalarArithmeticCPInstruction extends ArithmeticBinaryCPInstruction { - - protected ScalarScalarArithmeticCPInstruction(Operator op, CPOperand in1, CPOperand in2, CPOperand out, - String opcode, String istr) { - super(op, in1, in2, out, opcode, istr); - } - - @Override - public void processInstruction(ExecutionContext ec) throws DMLRuntimeException{ - ScalarObject so1 = ec.getScalarInput(input1.getName(), input1.getValueType(), input1.isLiteral()); - ScalarObject so2 = ec.getScalarInput(input2.getName(), input2.getValueType(), input2.isLiteral() ); - - BinaryOperator dop = (BinaryOperator) _optr; - ScalarObject sores = null; - - //compute output value, incl implicit type promotion if necessary - if( so1 instanceof StringObject || so2 instanceof StringObject ) { - if( !getOpcode().equals("+") ) //not string concatenation - throw new DMLRuntimeException("Arithmetic '"+getOpcode()+"' not supported over string inputs."); - sores = new StringObject( dop.fn.execute( - so1.getLanguageSpecificStringValue(), so2.getLanguageSpecificStringValue()) ); - } - else if( so1 instanceof DoubleObject || so2 instanceof DoubleObject || output.getValueType()==ValueType.DOUBLE ) { - sores = new DoubleObject( dop.fn.execute(so1.getDoubleValue(), so2.getDoubleValue()) ); - } - else if( so1 instanceof IntObject || so2 instanceof IntObject ) { - double tmp = dop.fn.execute(so1.getLongValue(), so2.getLongValue()); - if( tmp > Long.MAX_VALUE ) //cast to long if no overflow, otherwise controlled exception - throw new DMLRuntimeException("Integer operation created numerical result overflow ("+tmp+" > "+Long.MAX_VALUE+")."); - sores = new IntObject((long) tmp); - } - else { //all boolean - //NOTE: boolean-boolean arithmetic treated as double for consistency with R - sores = new DoubleObject( dop.fn.execute(so1.getDoubleValue(), so2.getDoubleValue()) ); - } - - ec.setScalarOutput(output.getName(), sores); - } -} http://git-wip-us.apache.org/repos/asf/systemml/blob/cc90a0e5/src/main/java/org/apache/sysml/runtime/instructions/cp/ScalarScalarBuiltinCPInstruction.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/sysml/runtime/instructions/cp/ScalarScalarBuiltinCPInstruction.java b/src/main/java/org/apache/sysml/runtime/instructions/cp/ScalarScalarBuiltinCPInstruction.java deleted file mode 100644 index 217bb96..0000000 --- a/src/main/java/org/apache/sysml/runtime/instructions/cp/ScalarScalarBuiltinCPInstruction.java +++ /dev/null @@ -1,57 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package org.apache.sysml.runtime.instructions.cp; - -import org.apache.sysml.parser.Expression.ValueType; -import org.apache.sysml.runtime.DMLRuntimeException; -import org.apache.sysml.runtime.controlprogram.context.ExecutionContext; -import org.apache.sysml.runtime.matrix.operators.BinaryOperator; -import org.apache.sysml.runtime.matrix.operators.Operator; - -public class ScalarScalarBuiltinCPInstruction extends BuiltinBinaryCPInstruction { - - protected ScalarScalarBuiltinCPInstruction(Operator op, CPOperand in1, CPOperand in2, CPOperand out, String opcode, - String instr) { - super(op, in1, in2, out, opcode, instr); - } - - @Override - public void processInstruction(ExecutionContext ec) - throws DMLRuntimeException - { - ScalarObject so1 = ec.getScalarInput( input1.getName(), input1.getValueType(), input1.isLiteral() ); - ScalarObject so2 = ec.getScalarInput(input2.getName(), input2.getValueType(), input2.isLiteral() ); - - BinaryOperator dop = (BinaryOperator) _optr; - ScalarObject sores = null; - - //compute output value, incl implicit type promotion if necessary - if( so1 instanceof StringObject || so2 instanceof StringObject ) - throw new DMLRuntimeException("Binary builtin '"+getOpcode()+"' not supported over string inputs."); - else if( so1 instanceof DoubleObject || so2 instanceof DoubleObject || output.getValueType()==ValueType.DOUBLE ) - sores = new DoubleObject(dop.fn.execute( so1.getDoubleValue(), so2.getDoubleValue() )); - else if( so1 instanceof IntObject || so2 instanceof IntObject ) - sores = new IntObject((long)dop.fn.execute( so1.getLongValue(), so2.getLongValue() )); - else //all boolean - throw new DMLRuntimeException("Binary builtin '"+getOpcode()+"' not supported over boolean inputs."); - - ec.setScalarOutput(output.getName(), sores); - } -} http://git-wip-us.apache.org/repos/asf/systemml/blob/cc90a0e5/src/main/java/org/apache/sysml/runtime/instructions/cp/ScalarScalarRelationalCPInstruction.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/sysml/runtime/instructions/cp/ScalarScalarRelationalCPInstruction.java b/src/main/java/org/apache/sysml/runtime/instructions/cp/ScalarScalarRelationalCPInstruction.java deleted file mode 100644 index fa51249..0000000 --- a/src/main/java/org/apache/sysml/runtime/instructions/cp/ScalarScalarRelationalCPInstruction.java +++ /dev/null @@ -1,57 +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.ValueComparisonFunction; -import org.apache.sysml.runtime.matrix.operators.BinaryOperator; -import org.apache.sysml.runtime.matrix.operators.Operator; - -public class ScalarScalarRelationalCPInstruction extends RelationalBinaryCPInstruction { - protected ScalarScalarRelationalCPInstruction(Operator op, CPOperand in1, CPOperand in2, CPOperand out, - String opcode, String istr) { - super(op, in1, in2, out, opcode, istr); - } - - @Override - public void processInstruction(ExecutionContext ec) - throws DMLRuntimeException - { - ScalarObject so1 = ec.getScalarInput(input1.getName(), input1.getValueType(), input1.isLiteral()); - ScalarObject so2 = ec.getScalarInput(input2.getName(), input2.getValueType(), input2.isLiteral() ); - - ValueComparisonFunction vcomp = ((ValueComparisonFunction)((BinaryOperator)_optr).fn); - boolean rval = false; - - //compute output value, incl implicit type promotion if necessary - if( so1 instanceof StringObject || so2 instanceof StringObject ) - rval = vcomp.compare ( so1.getStringValue(), so2.getStringValue() ); - else if( so1 instanceof DoubleObject || so2 instanceof DoubleObject ) - rval = vcomp.compare( so1.getDoubleValue(), so2.getDoubleValue() ); - else if( so1 instanceof IntObject || so2 instanceof IntObject ) - rval = vcomp.compare( so1.getLongValue(), so2.getLongValue() ); - else //all boolean - rval = vcomp.compare( so1.getBooleanValue(), so2.getBooleanValue() ); - - //set boolean output value - ec.setScalarOutput(output.getName(), new BooleanObject(rval)); - } -} http://git-wip-us.apache.org/repos/asf/systemml/blob/cc90a0e5/src/main/java/org/apache/sysml/runtime/instructions/spark/ArithmeticBinarySPInstruction.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/sysml/runtime/instructions/spark/ArithmeticBinarySPInstruction.java b/src/main/java/org/apache/sysml/runtime/instructions/spark/ArithmeticBinarySPInstruction.java deleted file mode 100644 index 9c4c789..0000000 --- a/src/main/java/org/apache/sysml/runtime/instructions/spark/ArithmeticBinarySPInstruction.java +++ /dev/null @@ -1,84 +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.lops.BinaryM.VectorType; -import org.apache.sysml.lops.Lop; -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.instructions.InstructionUtils; -import org.apache.sysml.runtime.instructions.cp.CPOperand; -import org.apache.sysml.runtime.matrix.operators.Operator; - -public abstract class ArithmeticBinarySPInstruction extends BinarySPInstruction { - - protected ArithmeticBinarySPInstruction(Operator op, CPOperand in1, CPOperand in2, CPOperand out, String opcode, String istr) { - super(SPType.ArithmeticBinary, op, in1, in2, out, opcode, istr); - } - - public static ArithmeticBinarySPInstruction parseInstruction ( String str ) - throws DMLRuntimeException - { - CPOperand in1 = new CPOperand("", ValueType.UNKNOWN, DataType.UNKNOWN); - CPOperand in2 = new CPOperand("", ValueType.UNKNOWN, DataType.UNKNOWN); - CPOperand out = new CPOperand("", ValueType.UNKNOWN, DataType.UNKNOWN); - String opcode = null; - boolean isBroadcast = false; - VectorType vtype = null; - - if(str.startsWith("SPARK"+Lop.OPERAND_DELIMITOR+"map")) { - String[] parts = InstructionUtils.getInstructionPartsWithValueType(str); - InstructionUtils.checkNumFields ( parts, 5 ); - - opcode = parts[0]; - in1.split(parts[1]); - in2.split(parts[2]); - out.split(parts[3]); - vtype = VectorType.valueOf(parts[5]); - isBroadcast = true; - } - else { - opcode = parseBinaryInstruction(str, in1, in2, out); - } - - // Arithmetic operations must be performed on DOUBLE or INT - DataType dt1 = in1.getDataType(); - DataType dt2 = in2.getDataType(); - - Operator operator = (dt1 != dt2) ? - InstructionUtils.parseScalarBinaryOperator(opcode, (dt1 == DataType.SCALAR)) - : InstructionUtils.parseExtendedBinaryOperator(opcode); - - if (dt1 == DataType.MATRIX || dt2 == DataType.MATRIX) - { - if(dt1 == DataType.MATRIX && dt2 == DataType.MATRIX) { - if(isBroadcast) - return new MatrixBVectorArithmeticSPInstruction(operator, in1, in2, out, vtype, opcode, str); - else - return new MatrixMatrixArithmeticSPInstruction(operator, in1, in2, out, opcode, str); - } - else - return new MatrixScalarArithmeticSPInstruction(operator, in1, in2, out, opcode, str); - } - - return null; - } -}
