use of org.apache.sysml.runtime.matrix.operators.ScalarOperator in project incubator-systemml by apache.
the class CPlanVectorPrimitivesTest method testVectorBinaryPrimitive.
private static void testVectorBinaryPrimitive(BinType bintype, InputType type1, InputType type2) {
try {
// generate input data (scalar later derived if needed)
double sparsityA = (type1 == InputType.VECTOR_DENSE) ? sparsity1 : sparsity2;
MatrixBlock inA = MatrixBlock.randOperations(m, n, sparsityA, -5, 5, "uniform", 3);
double sparsityB = (type2 == InputType.VECTOR_DENSE) ? sparsity1 : sparsity2;
MatrixBlock inB = MatrixBlock.randOperations(m, n, sparsityB, -5, 5, "uniform", 7);
// get vector primitive via reflection
String meName = "vect" + StringUtils.camelize(bintype.name().split("_")[1]) + "Write";
Method me = null;
if (type1 == InputType.SCALAR && type2 == InputType.VECTOR_DENSE)
me = LibSpoofPrimitives.class.getMethod(meName, new Class[] { double.class, double[].class, int.class, int.class });
else if (type1 == InputType.VECTOR_DENSE && type2 == InputType.SCALAR)
me = LibSpoofPrimitives.class.getMethod(meName, new Class[] { double[].class, double.class, int.class, int.class });
else if (type1 == InputType.VECTOR_DENSE && type2 == InputType.VECTOR_DENSE)
me = LibSpoofPrimitives.class.getMethod(meName, new Class[] { double[].class, double[].class, int.class, int.class, int.class });
else if (type1 == InputType.VECTOR_SPARSE && type2 == InputType.SCALAR)
me = LibSpoofPrimitives.class.getMethod(meName, new Class[] { double[].class, double.class, int[].class, int.class, int.class, int.class });
else if (type1 == InputType.SCALAR && type2 == InputType.VECTOR_SPARSE)
me = LibSpoofPrimitives.class.getMethod(meName, new Class[] { double.class, double[].class, int[].class, int.class, int.class, int.class });
else if (type1 == InputType.VECTOR_SPARSE && type2 == InputType.VECTOR_DENSE)
me = LibSpoofPrimitives.class.getMethod(meName, new Class[] { double[].class, double[].class, int[].class, int.class, int.class, int.class, int.class });
for (int i = 0; i < m; i++) {
// execute vector primitive via reflection
double[] ret1 = null;
if (type1 == InputType.SCALAR && type2 == InputType.VECTOR_DENSE)
ret1 = (double[]) me.invoke(null, inA.max(), inB.getDenseBlockValues(), i * n, n);
else if (type1 == InputType.VECTOR_DENSE && type2 == InputType.SCALAR)
ret1 = (double[]) me.invoke(null, inA.getDenseBlockValues(), inB.max(), i * n, n);
else if (type1 == InputType.VECTOR_DENSE && type2 == InputType.VECTOR_DENSE)
ret1 = (double[]) me.invoke(null, inA.getDenseBlockValues(), inB.getDenseBlockValues(), i * n, i * n, n);
else if (type1 == InputType.VECTOR_SPARSE && type2 == InputType.SCALAR)
ret1 = (double[]) me.invoke(null, inA.getSparseBlock().values(i), inB.max(), inA.getSparseBlock().indexes(i), inA.getSparseBlock().pos(i), inA.getSparseBlock().size(i), n);
else if (type1 == InputType.SCALAR && type2 == InputType.VECTOR_SPARSE)
ret1 = (double[]) me.invoke(null, inA.max(), inB.getSparseBlock().values(i), inB.getSparseBlock().indexes(i), inB.getSparseBlock().pos(i), inB.getSparseBlock().size(i), n);
else if (type1 == InputType.VECTOR_SPARSE && type2 == InputType.VECTOR_DENSE)
ret1 = (double[]) me.invoke(null, inA.getSparseBlock().values(i), inB.getDenseBlockValues(), inA.getSparseBlock().indexes(i), inA.getSparseBlock().pos(i), i * n, inA.getSparseBlock().size(i), n);
// execute comparison operation
String opcode = Hop.getBinaryOpCode(OpOp2.valueOf(bintype.name().split("_")[1]));
MatrixBlock in1 = inA.slice(i, i, 0, n - 1, new MatrixBlock());
MatrixBlock in2 = inB.slice(i, i, 0, n - 1, new MatrixBlock());
double[] ret2 = null;
if (type1 == InputType.SCALAR) {
ScalarOperator bop = InstructionUtils.parseScalarBinaryOperator(opcode, true);
bop = bop.setConstant(inA.max());
ret2 = DataConverter.convertToDoubleVector((MatrixBlock) in2.scalarOperations(bop, new MatrixBlock()), false);
} else if (type2 == InputType.SCALAR) {
ScalarOperator bop = InstructionUtils.parseScalarBinaryOperator(opcode, false);
bop = bop.setConstant(inB.max());
ret2 = DataConverter.convertToDoubleVector((MatrixBlock) in1.scalarOperations(bop, new MatrixBlock()), false);
} else {
// vector-vector
BinaryOperator bop = InstructionUtils.parseBinaryOperator(opcode);
ret2 = DataConverter.convertToDoubleVector((MatrixBlock) in1.binaryOperations(bop, in2, new MatrixBlock()), false);
}
// compare results
TestUtils.compareMatrices(ret1, ret2, eps);
}
} catch (Exception ex) {
throw new RuntimeException(ex);
}
}
use of org.apache.sysml.runtime.matrix.operators.ScalarOperator in project incubator-systemml by apache.
the class MatrixScalarBuiltinCPInstruction method processInstruction.
@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());
ScalarObject constant = (ScalarObject) ec.getScalarInput(scalar.getName(), scalar.getValueType(), scalar.isLiteral());
ScalarOperator sc_op = (ScalarOperator) _optr;
sc_op.setConstant(constant.getDoubleValue());
MatrixBlock retBlock = (MatrixBlock) inBlock.scalarOperations(sc_op, new MatrixBlock());
ec.releaseMatrixInput(mat.getName());
// Ensure right dense/sparse output representation (guarded by released input memory)
if (checkGuardedRepresentationChange(inBlock, retBlock)) {
retBlock.examSparsity();
}
ec.setMatrixOutput(output.getName(), retBlock);
}
use of org.apache.sysml.runtime.matrix.operators.ScalarOperator in project incubator-systemml by apache.
the class ScalarMatrixArithmeticCPInstruction method processInstruction.
@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());
ScalarObject constant = (ScalarObject) ec.getScalarInput(scalar.getName(), scalar.getValueType(), scalar.isLiteral());
ScalarOperator sc_op = (ScalarOperator) _optr;
sc_op.setConstant(constant.getDoubleValue());
MatrixBlock retBlock = (MatrixBlock) inBlock.scalarOperations(sc_op, new MatrixBlock());
ec.releaseMatrixInput(mat.getName());
// Ensure right dense/sparse output representation (guarded by released input memory)
if (checkGuardedRepresentationChange(inBlock, retBlock)) {
retBlock.examSparsity();
}
ec.setMatrixOutput(output.getName(), retBlock);
}
Aggregations