use of org.apache.sysml.runtime.instructions.cp.DoubleObject in project incubator-systemml by apache.
the class ProgramBlock method executePredicateInstructions.
protected ScalarObject executePredicateInstructions(ArrayList<Instruction> inst, ValueType retType, ExecutionContext ec) throws DMLRuntimeException {
ScalarObject ret = null;
String retName = null;
//execute all instructions
for (int i = 0; i < inst.size(); i++) {
//indexed access required due to debug mode
Instruction currInst = inst.get(i);
if (!isRemoveVariableInstruction(currInst)) {
//execute instruction
ec.updateDebugState(i);
executeSingleInstruction(currInst, ec);
//get last return name
if (currInst instanceof ComputationCPInstruction)
retName = ((ComputationCPInstruction) currInst).getOutputVariableName();
else if (currInst instanceof VariableCPInstruction && ((VariableCPInstruction) currInst).getOutputVariableName() != null)
retName = ((VariableCPInstruction) currInst).getOutputVariableName();
}
}
//get return value TODO: how do we differentiate literals and variables?
ret = (ScalarObject) ec.getScalarInput(retName, retType, false);
//execute rmvar instructions
for (int i = 0; i < inst.size(); i++) {
//indexed access required due to debug mode
Instruction currInst = inst.get(i);
if (isRemoveVariableInstruction(currInst)) {
ec.updateDebugState(i);
executeSingleInstruction(currInst, ec);
}
}
//check and correct scalar ret type (incl save double to int)
if (ret.getValueType() != retType)
switch(retType) {
case BOOLEAN:
ret = new BooleanObject(ret.getName(), ret.getBooleanValue());
break;
case INT:
ret = new IntObject(ret.getName(), ret.getLongValue());
break;
case DOUBLE:
ret = new DoubleObject(ret.getName(), ret.getDoubleValue());
break;
case STRING:
ret = new StringObject(ret.getName(), ret.getStringValue());
break;
default:
}
return ret;
}
use of org.apache.sysml.runtime.instructions.cp.DoubleObject in project incubator-systemml by apache.
the class ExternalFunctionProgramBlock method verifyAndAttachOutputs.
/**
* Method to verify that function outputs match with declared outputs
*
* @param ec execution context
* @param returnFunc package function
* @param outputParams output parameters
* @throws DMLRuntimeException if DMLRuntimeException occurs
*/
protected void verifyAndAttachOutputs(ExecutionContext ec, PackageFunction returnFunc, String outputParams) throws DMLRuntimeException {
ArrayList<String> outputs = getParameters(outputParams);
if (outputs.size() != returnFunc.getNumFunctionOutputs()) {
throw new DMLRuntimeException("Number of function outputs (" + returnFunc.getNumFunctionOutputs() + ") " + "does not match with declaration (" + outputs.size() + ").");
}
// iterate over each output and verify that type matches
for (int i = 0; i < outputs.size(); i++) {
StringTokenizer tk = new StringTokenizer(outputs.get(i), ":");
ArrayList<String> tokens = new ArrayList<String>();
while (tk.hasMoreTokens()) {
tokens.add(tk.nextToken());
}
if (returnFunc.getFunctionOutput(i).getType() == FunctionParameterType.Matrix) {
Matrix m = (Matrix) returnFunc.getFunctionOutput(i);
if (!(tokens.get(0).equals(getFunctionParameterDataTypeString(FunctionParameterType.Matrix))) || !(tokens.get(2).equals(getMatrixValueTypeString(m.getValueType())))) {
throw new DMLRuntimeException("Function output '" + outputs.get(i) + "' does not match with declaration.");
}
// add result to variableMapping
String varName = tokens.get(1);
MatrixObject newVar = createOutputMatrixObject(m);
newVar.setVarName(varName);
//getVariables().put(varName, newVar); //put/override in local symbol table
ec.setVariable(varName, newVar);
continue;
}
if (returnFunc.getFunctionOutput(i).getType() == FunctionParameterType.Scalar) {
Scalar s = (Scalar) returnFunc.getFunctionOutput(i);
if (!tokens.get(0).equals(getFunctionParameterDataTypeString(FunctionParameterType.Scalar)) || !tokens.get(2).equals(getScalarValueTypeString(s.getScalarType()))) {
throw new DMLRuntimeException("Function output '" + outputs.get(i) + "' does not match with declaration.");
}
// allocate and set appropriate object based on type
ScalarObject scalarObject = null;
ScalarValueType type = s.getScalarType();
switch(type) {
case Integer:
scalarObject = new IntObject(tokens.get(1), Long.parseLong(s.getValue()));
break;
case Double:
scalarObject = new DoubleObject(tokens.get(1), Double.parseDouble(s.getValue()));
break;
case Boolean:
scalarObject = new BooleanObject(tokens.get(1), Boolean.parseBoolean(s.getValue()));
break;
case Text:
scalarObject = new StringObject(tokens.get(1), s.getValue());
break;
default:
throw new DMLRuntimeException("Unknown scalar value type '" + type + "' of output '" + outputs.get(i) + "'.");
}
//this.getVariables().put(tokens.get(1), scalarObject);
ec.setVariable(tokens.get(1), scalarObject);
continue;
}
if (returnFunc.getFunctionOutput(i).getType() == FunctionParameterType.Object) {
if (!tokens.get(0).equals(getFunctionParameterDataTypeString(FunctionParameterType.Object))) {
throw new DMLRuntimeException("Function output '" + outputs.get(i) + "' does not match with declaration.");
}
throw new DMLRuntimeException("Object types not yet supported");
// continue;
}
throw new DMLRuntimeException("Unknown data type '" + returnFunc.getFunctionOutput(i).getType() + "' " + "of output '" + outputs.get(i) + "'.");
}
}
use of org.apache.sysml.runtime.instructions.cp.DoubleObject in project incubator-systemml by apache.
the class ParForProgramBlock method createEmptyUnscopedVariables.
/**
* Create empty matrix objects and scalars for all unscoped vars
* (created within the parfor).
*
* NOTE: parfor gives no guarantees on the values of those objects - hence
* we return -1 for sclars and empty matrix objects.
*
* @param out local variable map
* @param sb statement block
* @throws DMLRuntimeException if DMLRuntimeException occurs
*/
private void createEmptyUnscopedVariables(LocalVariableMap out, StatementBlock sb) throws DMLRuntimeException {
VariableSet updated = sb.variablesUpdated();
VariableSet livein = sb.liveIn();
//for all vars IN <updated> AND NOT IN <livein>
for (String var : updated.getVariableNames()) if (!livein.containsVariable(var)) {
//create empty output
DataIdentifier dat = updated.getVariable(var);
DataType datatype = dat.getDataType();
ValueType valuetype = dat.getValueType();
Data dataObj = null;
switch(datatype) {
case SCALAR:
switch(valuetype) {
case BOOLEAN:
dataObj = new BooleanObject(var, false);
break;
case INT:
dataObj = new IntObject(var, -1);
break;
case DOUBLE:
dataObj = new DoubleObject(var, -1d);
break;
case STRING:
dataObj = new StringObject(var, "-1");
break;
default:
throw new DMLRuntimeException("Value type not supported: " + valuetype);
}
break;
case MATRIX:
//because metadata (e.g., outputinfo) not known at this place.
break;
case UNKNOWN:
break;
default:
throw new DMLRuntimeException("Data type not supported: " + datatype);
}
if (dataObj != null)
out.put(var, dataObj);
}
}
use of org.apache.sysml.runtime.instructions.cp.DoubleObject in project incubator-systemml by apache.
the class LibMatrixCUDA method unaryAggregate.
//********************************************************************/
//***************** END OF MATRIX MULTIPLY Functions *****************/
//********************************************************************/
//********************************************************************/
//**************** UNARY AGGREGATE Functions ************************/
//********************************************************************/
/**
* Entry point to perform Unary aggregate operations on the GPU.
* The execution context object is used to allocate memory for the GPU.
* @param ec Instance of {@link ExecutionContext}, from which the output variable will be allocated
* @param gCtx a valid {@link GPUContext}
* @param instName name of the invoking instruction to record{@link Statistics}.
* @param in1 input matrix
* @param output output matrix/scalar name
* @param op Instance of {@link AggregateUnaryOperator} which encapsulates the direction of reduction/aggregation and the reduction operation.
* @throws DMLRuntimeException if {@link DMLRuntimeException} occurs
*/
public static void unaryAggregate(ExecutionContext ec, GPUContext gCtx, String instName, MatrixObject in1, String output, AggregateUnaryOperator op) throws DMLRuntimeException {
if (ec.getGPUContext() != gCtx)
throw new DMLRuntimeException("GPU : Invalid internal state, the GPUContext set with the ExecutionContext is not the same used to run this LibMatrixCUDA function");
LOG.trace("GPU : unaryAggregate" + ", GPUContext=" + gCtx);
final int REDUCTION_ALL = 1;
final int REDUCTION_ROW = 2;
final int REDUCTION_COL = 3;
final int REDUCTION_DIAG = 4;
// A kahan sum implemention is not provided. is a "uak+" or other kahan operator is encountered,
// it just does regular summation reduction.
final int OP_PLUS = 1;
final int OP_PLUS_SQ = 2;
final int OP_MEAN = 3;
final int OP_VARIANCE = 4;
final int OP_MULTIPLY = 5;
final int OP_MAX = 6;
final int OP_MIN = 7;
final int OP_MAXINDEX = 8;
final int OP_MININDEX = 9;
// Sanity Checks
if (!in1.getGPUObject(gCtx).isAllocated())
throw new DMLRuntimeException("Internal Error - The input is not allocated for a GPU Aggregate Unary:" + in1.getGPUObject(gCtx).isAllocated());
boolean isSparse = in1.getGPUObject(gCtx).isSparse();
IndexFunction indexFn = op.indexFn;
AggregateOperator aggOp = op.aggOp;
// Convert Reduction direction to a number to pass to CUDA kernel
int reductionDirection = -1;
if (indexFn instanceof ReduceAll) {
reductionDirection = REDUCTION_ALL;
} else if (indexFn instanceof ReduceRow) {
reductionDirection = REDUCTION_ROW;
} else if (indexFn instanceof ReduceCol) {
reductionDirection = REDUCTION_COL;
} else if (indexFn instanceof ReduceDiag) {
reductionDirection = REDUCTION_DIAG;
} else {
throw new DMLRuntimeException("Internal Error - Invalid index function type, only reducing along rows, columns, diagonals or all elements is supported in Aggregate Unary operations");
}
assert reductionDirection != -1 : "Internal Error - Incorrect type of reduction direction set for aggregate unary GPU instruction";
// Convert function type to a number to pass to the CUDA Kernel
int opIndex = -1;
if (aggOp.increOp.fn instanceof KahanPlus) {
opIndex = OP_PLUS;
} else if (aggOp.increOp.fn instanceof KahanPlusSq) {
opIndex = OP_PLUS_SQ;
} else if (aggOp.increOp.fn instanceof Mean) {
opIndex = OP_MEAN;
} else if (aggOp.increOp.fn instanceof CM) {
assert ((CM) aggOp.increOp.fn).getAggOpType() == CMOperator.AggregateOperationTypes.VARIANCE : "Internal Error - Invalid Type of CM operator for Aggregate Unary operation on GPU";
opIndex = OP_VARIANCE;
} else if (aggOp.increOp.fn instanceof Plus) {
opIndex = OP_PLUS;
} else if (aggOp.increOp.fn instanceof Multiply) {
opIndex = OP_MULTIPLY;
} else if (aggOp.increOp.fn instanceof Builtin) {
Builtin b = (Builtin) aggOp.increOp.fn;
switch(b.bFunc) {
case MAX:
opIndex = OP_MAX;
break;
case MIN:
opIndex = OP_MIN;
break;
case MAXINDEX:
opIndex = OP_MAXINDEX;
break;
case MININDEX:
opIndex = OP_MININDEX;
break;
default:
new DMLRuntimeException("Internal Error - Unsupported Builtin Function for Aggregate unary being done on GPU");
}
} else {
throw new DMLRuntimeException("Internal Error - Aggregate operator has invalid Value function");
}
assert opIndex != -1 : "Internal Error - Incorrect type of operation set for aggregate unary GPU instruction";
int rlen = (int) in1.getNumRows();
int clen = (int) in1.getNumColumns();
if (isSparse) {
// The strategy for the time being is to convert sparse to dense
// until a sparse specific kernel is written.
in1.getGPUObject(gCtx).sparseToDense(instName);
// long nnz = in1.getNnz();
// assert nnz > 0 : "Internal Error - number of non zeroes set to " + nnz + " in Aggregate Binary for GPU";
// MatrixObject out = ec.getSparseMatrixOutputForGPUInstruction(output, nnz);
// throw new DMLRuntimeException("Internal Error - Not implemented");
}
Pointer out = null;
if (reductionDirection == REDUCTION_COL || reductionDirection == REDUCTION_ROW) {
// Matrix output
MatrixObject out1 = getDenseMatrixOutputForGPUInstruction(ec, instName, output);
out = getDensePointer(gCtx, out1, instName);
}
Pointer in = getDensePointer(gCtx, in1, instName);
int size = rlen * clen;
// For scalars, set the scalar output in the Execution Context object
switch(opIndex) {
case OP_PLUS:
{
switch(reductionDirection) {
case REDUCTION_ALL:
{
double result = reduceAll(gCtx, instName, "reduce_sum", in, size);
ec.setScalarOutput(output, new DoubleObject(result));
break;
}
case REDUCTION_COL:
{
// The names are a bit misleading, REDUCTION_COL refers to the direction (reduce all elements in a column)
reduceRow(gCtx, instName, "reduce_row_sum", in, out, rlen, clen);
break;
}
case REDUCTION_ROW:
{
reduceCol(gCtx, instName, "reduce_col_sum", in, out, rlen, clen);
break;
}
case REDUCTION_DIAG:
throw new DMLRuntimeException("Internal Error - Row, Column and Diag summation not implemented yet");
}
break;
}
case OP_PLUS_SQ:
{
// Calculate the squares in a temporary object tmp
Pointer tmp = gCtx.allocate(instName, size * Sizeof.DOUBLE);
squareMatrix(gCtx, instName, in, tmp, rlen, clen);
// Then do the sum on the temporary object and free it
switch(reductionDirection) {
case REDUCTION_ALL:
{
double result = reduceAll(gCtx, instName, "reduce_sum", tmp, size);
ec.setScalarOutput(output, new DoubleObject(result));
break;
}
case REDUCTION_COL:
{
// The names are a bit misleading, REDUCTION_COL refers to the direction (reduce all elements in a column)
reduceRow(gCtx, instName, "reduce_row_sum", tmp, out, rlen, clen);
break;
}
case REDUCTION_ROW:
{
reduceCol(gCtx, instName, "reduce_col_sum", tmp, out, rlen, clen);
break;
}
default:
throw new DMLRuntimeException("Internal Error - Unsupported reduction direction for summation squared");
}
gCtx.cudaFreeHelper(instName, tmp);
break;
}
case OP_MEAN:
{
switch(reductionDirection) {
case REDUCTION_ALL:
{
double result = reduceAll(gCtx, instName, "reduce_sum", in, size);
double mean = result / size;
ec.setScalarOutput(output, new DoubleObject(mean));
break;
}
case REDUCTION_COL:
{
reduceRow(gCtx, instName, "reduce_row_mean", in, out, rlen, clen);
break;
}
case REDUCTION_ROW:
{
reduceCol(gCtx, instName, "reduce_col_mean", in, out, rlen, clen);
break;
}
default:
throw new DMLRuntimeException("Internal Error - Unsupported reduction direction for mean");
}
break;
}
case OP_MULTIPLY:
{
switch(reductionDirection) {
case REDUCTION_ALL:
{
double result = reduceAll(gCtx, instName, "reduce_prod", in, size);
ec.setScalarOutput(output, new DoubleObject(result));
break;
}
default:
throw new DMLRuntimeException("Internal Error - Unsupported reduction direction for multiplication");
}
break;
}
case OP_MAX:
{
switch(reductionDirection) {
case REDUCTION_ALL:
{
double result = reduceAll(gCtx, instName, "reduce_max", in, size);
ec.setScalarOutput(output, new DoubleObject(result));
break;
}
case REDUCTION_COL:
{
reduceRow(gCtx, instName, "reduce_row_max", in, out, rlen, clen);
break;
}
case REDUCTION_ROW:
{
reduceCol(gCtx, instName, "reduce_col_max", in, out, rlen, clen);
break;
}
default:
throw new DMLRuntimeException("Internal Error - Unsupported reduction direction for max");
}
break;
}
case OP_MIN:
{
switch(reductionDirection) {
case REDUCTION_ALL:
{
double result = reduceAll(gCtx, instName, "reduce_min", in, size);
ec.setScalarOutput(output, new DoubleObject(result));
break;
}
case REDUCTION_COL:
{
reduceRow(gCtx, instName, "reduce_row_min", in, out, rlen, clen);
break;
}
case REDUCTION_ROW:
{
reduceCol(gCtx, instName, "reduce_col_min", in, out, rlen, clen);
break;
}
default:
throw new DMLRuntimeException("Internal Error - Unsupported reduction direction for min");
}
break;
}
case OP_VARIANCE:
{
// Temporary GPU array for
Pointer tmp = gCtx.allocate(instName, size * Sizeof.DOUBLE);
Pointer tmp2 = gCtx.allocate(instName, size * Sizeof.DOUBLE);
switch(reductionDirection) {
case REDUCTION_ALL:
{
double result = reduceAll(gCtx, instName, "reduce_sum", in, size);
double mean = result / size;
// Subtract mean from every element in the matrix
ScalarOperator minusOp = new RightScalarOperator(Minus.getMinusFnObject(), mean);
matrixScalarOp(gCtx, instName, in, mean, rlen, clen, tmp, minusOp);
squareMatrix(gCtx, instName, tmp, tmp2, rlen, clen);
double result2 = reduceAll(gCtx, instName, "reduce_sum", tmp2, size);
double variance = result2 / (size - 1);
ec.setScalarOutput(output, new DoubleObject(variance));
break;
}
case REDUCTION_COL:
{
reduceRow(gCtx, instName, "reduce_row_mean", in, out, rlen, clen);
// Subtract the row-wise mean from every element in the matrix
BinaryOperator minusOp = new BinaryOperator(Minus.getMinusFnObject());
matrixMatrixOp(gCtx, instName, in, out, rlen, clen, VectorShape.NONE.code(), VectorShape.COLUMN.code(), tmp, minusOp);
squareMatrix(gCtx, instName, tmp, tmp2, rlen, clen);
Pointer tmpRow = gCtx.allocate(instName, rlen * Sizeof.DOUBLE);
reduceRow(gCtx, instName, "reduce_row_sum", tmp2, tmpRow, rlen, clen);
ScalarOperator divideOp = new RightScalarOperator(Divide.getDivideFnObject(), clen - 1);
matrixScalarOp(gCtx, instName, tmpRow, clen - 1, rlen, 1, out, divideOp);
gCtx.cudaFreeHelper(instName, tmpRow);
break;
}
case REDUCTION_ROW:
{
reduceCol(gCtx, instName, "reduce_col_mean", in, out, rlen, clen);
// Subtract the columns-wise mean from every element in the matrix
BinaryOperator minusOp = new BinaryOperator(Minus.getMinusFnObject());
matrixMatrixOp(gCtx, instName, in, out, rlen, clen, VectorShape.NONE.code(), VectorShape.ROW.code(), tmp, minusOp);
squareMatrix(gCtx, instName, tmp, tmp2, rlen, clen);
Pointer tmpCol = gCtx.allocate(instName, clen * Sizeof.DOUBLE);
reduceCol(gCtx, instName, "reduce_col_sum", tmp2, tmpCol, rlen, clen);
ScalarOperator divideOp = new RightScalarOperator(Divide.getDivideFnObject(), rlen - 1);
matrixScalarOp(gCtx, instName, tmpCol, rlen - 1, 1, clen, out, divideOp);
gCtx.cudaFreeHelper(instName, tmpCol);
break;
}
default:
throw new DMLRuntimeException("Internal Error - Unsupported reduction direction for variance");
}
gCtx.cudaFreeHelper(instName, tmp);
gCtx.cudaFreeHelper(instName, tmp2);
break;
}
case OP_MAXINDEX:
{
switch(reductionDirection) {
case REDUCTION_COL:
throw new DMLRuntimeException("Internal Error - Column maxindex of matrix not implemented yet for GPU ");
default:
throw new DMLRuntimeException("Internal Error - Unsupported reduction direction for maxindex");
}
// break;
}
case OP_MININDEX:
{
switch(reductionDirection) {
case REDUCTION_COL:
throw new DMLRuntimeException("Internal Error - Column minindex of matrix not implemented yet for GPU ");
default:
throw new DMLRuntimeException("Internal Error - Unsupported reduction direction for minindex");
}
// break;
}
default:
throw new DMLRuntimeException("Internal Error - Invalid GPU Unary aggregate function!");
}
}
use of org.apache.sysml.runtime.instructions.cp.DoubleObject in project incubator-systemml by apache.
the class CovarianceSPInstruction method processInstruction.
@Override
public void processInstruction(ExecutionContext ec) throws DMLRuntimeException {
SparkExecutionContext sec = (SparkExecutionContext) ec;
COVOperator cop = ((COVOperator) _optr);
//get input
JavaPairRDD<MatrixIndexes, MatrixBlock> in1 = sec.getBinaryBlockRDDHandleForVariable(input1.getName());
JavaPairRDD<MatrixIndexes, MatrixBlock> in2 = sec.getBinaryBlockRDDHandleForVariable(input2.getName());
//process central moment instruction
CM_COV_Object cmobj = null;
if (//w/o weights
input3 == null) {
cmobj = in1.join(in2).values().map(new RDDCOVFunction(cop)).fold(new CM_COV_Object(), new RDDCOVReduceFunction(cop));
} else //with weights
{
JavaPairRDD<MatrixIndexes, MatrixBlock> in3 = sec.getBinaryBlockRDDHandleForVariable(input3.getName());
cmobj = in1.join(in2).join(in3).values().map(new RDDCOVWeightsFunction(cop)).fold(new CM_COV_Object(), new RDDCOVReduceFunction(cop));
}
//create scalar output (no lineage information required)
double val = cmobj.getRequiredResult(_optr);
DoubleObject ret = new DoubleObject(output.getName(), val);
ec.setScalarOutput(output.getName(), ret);
}
Aggregations