Search in sources :

Example 16 with IndexRange

use of org.apache.sysml.runtime.util.IndexRange in project systemml by apache.

the class MatrixIndexingCPInstruction method processInstruction.

@Override
public void processInstruction(ExecutionContext ec) {
    String opcode = getOpcode();
    IndexRange ixrange = getIndexRange(ec);
    // get original matrix
    MatrixObject mo = ec.getMatrixObject(input1.getName());
    // right indexing
    if (opcode.equalsIgnoreCase(RightIndex.OPCODE)) {
        MatrixBlock resultBlock = null;
        if (// via data partitioning
        mo.isPartitioned())
            resultBlock = mo.readMatrixPartition(ixrange.add(1));
        else // via slicing the in-memory matrix
        {
            // execute right indexing operation (with shallow row copies for range
            // of entire sparse rows, which is safe due to copy on update)
            MatrixBlock matBlock = ec.getMatrixInput(input1.getName(), getExtendedOpcode());
            resultBlock = matBlock.slice((int) ixrange.rowStart, (int) ixrange.rowEnd, (int) ixrange.colStart, (int) ixrange.colEnd, false, new MatrixBlock());
            // unpin rhs input
            ec.releaseMatrixInput(input1.getName(), getExtendedOpcode());
            // ensure correct sparse/dense output representation
            if (checkGuardedRepresentationChange(matBlock, resultBlock))
                resultBlock.examSparsity();
        }
        // unpin output
        ec.setMatrixOutput(output.getName(), resultBlock, getExtendedOpcode());
    } else // left indexing
    if (opcode.equalsIgnoreCase(LeftIndex.OPCODE)) {
        UpdateType updateType = mo.getUpdateType();
        if (DMLScript.STATISTICS) {
            if (updateType.isInPlace())
                Statistics.incrementTotalLixUIP();
            Statistics.incrementTotalLix();
        }
        MatrixBlock matBlock = ec.getMatrixInput(input1.getName(), getExtendedOpcode());
        MatrixBlock resultBlock = null;
        if (input2.getDataType() == DataType.MATRIX) {
            // MATRIX<-MATRIX
            MatrixBlock rhsMatBlock = ec.getMatrixInput(input2.getName(), getExtendedOpcode());
            resultBlock = matBlock.leftIndexingOperations(rhsMatBlock, ixrange, new MatrixBlock(), updateType);
            ec.releaseMatrixInput(input2.getName(), getExtendedOpcode());
        } else {
            // MATRIX<-SCALAR
            if (!ixrange.isScalar())
                throw new DMLRuntimeException("Invalid index range of scalar leftindexing: " + ixrange.toString() + ".");
            ScalarObject scalar = ec.getScalarInput(input2.getName(), ValueType.DOUBLE, input2.isLiteral());
            resultBlock = (MatrixBlock) matBlock.leftIndexingOperations(scalar, (int) ixrange.rowStart, (int) ixrange.colStart, new MatrixBlock(), updateType);
        }
        // unpin lhs input
        ec.releaseMatrixInput(input1.getName(), getExtendedOpcode());
        // ensure correct sparse/dense output representation
        // (memory guarded by release of input)
        resultBlock.examSparsity();
        // unpin output
        ec.setMatrixOutput(output.getName(), resultBlock, updateType, getExtendedOpcode());
    } else
        throw new DMLRuntimeException("Invalid opcode (" + opcode + ") encountered in MatrixIndexingCPInstruction.");
}
Also used : IndexRange(org.apache.sysml.runtime.util.IndexRange) MatrixBlock(org.apache.sysml.runtime.matrix.data.MatrixBlock) MatrixObject(org.apache.sysml.runtime.controlprogram.caching.MatrixObject) UpdateType(org.apache.sysml.runtime.controlprogram.caching.MatrixObject.UpdateType) DMLRuntimeException(org.apache.sysml.runtime.DMLRuntimeException)

Example 17 with IndexRange

use of org.apache.sysml.runtime.util.IndexRange in project systemml by apache.

the class MatrixIndexingCPFileInstruction method processInstruction.

@Override
public void processInstruction(ExecutionContext ec) {
    String opcode = getOpcode();
    IndexRange ixrange = getIndexRange(ec).add(1);
    MatrixObject mo = ec.getMatrixObject(input1.getName());
    if (mo.isPartitioned() && opcode.equalsIgnoreCase(RightIndex.OPCODE)) {
        MetaDataFormat meta = (MetaDataFormat) mo.getMetaData();
        MatrixCharacteristics mc = meta.getMatrixCharacteristics();
        String pfname = mo.getPartitionFileName(ixrange, mc.getRowsPerBlock(), mc.getColsPerBlock());
        if (MapReduceTool.existsFileOnHDFS(pfname)) {
            // create output matrix object
            MatrixObject mobj = new MatrixObject(mo.getValueType(), pfname);
            MatrixCharacteristics mcNew = null;
            switch(mo.getPartitionFormat()) {
                case ROW_WISE:
                    mcNew = new MatrixCharacteristics(1, mc.getCols(), mc.getRowsPerBlock(), mc.getColsPerBlock());
                    break;
                case ROW_BLOCK_WISE_N:
                    mcNew = new MatrixCharacteristics(mo.getPartitionSize(), mc.getCols(), mc.getRowsPerBlock(), mc.getColsPerBlock());
                    break;
                case COLUMN_WISE:
                    mcNew = new MatrixCharacteristics(mc.getRows(), 1, mc.getRowsPerBlock(), mc.getColsPerBlock());
                    break;
                case COLUMN_BLOCK_WISE_N:
                    mcNew = new MatrixCharacteristics(mc.getRows(), mo.getPartitionSize(), mc.getRowsPerBlock(), mc.getColsPerBlock());
                    break;
                default:
                    throw new DMLRuntimeException("Unsupported partition format for CP_FILE " + RightIndex.OPCODE + ": " + mo.getPartitionFormat());
            }
            MetaDataFormat metaNew = new MetaDataFormat(mcNew, meta.getOutputInfo(), meta.getInputInfo());
            mobj.setMetaData(metaNew);
            // put output object into symbol table
            ec.setVariable(output.getName(), mobj);
        } else {
            // will return an empty matrix partition
            MatrixBlock resultBlock = mo.readMatrixPartition(ixrange);
            ec.setMatrixOutput(output.getName(), resultBlock, getExtendedOpcode());
        }
    } else {
        throw new DMLRuntimeException("Invalid opcode or index predicate for MatrixIndexingCPFileInstruction: " + instString);
    }
}
Also used : IndexRange(org.apache.sysml.runtime.util.IndexRange) MetaDataFormat(org.apache.sysml.runtime.matrix.MetaDataFormat) MatrixBlock(org.apache.sysml.runtime.matrix.data.MatrixBlock) MatrixObject(org.apache.sysml.runtime.controlprogram.caching.MatrixObject) MatrixCharacteristics(org.apache.sysml.runtime.matrix.MatrixCharacteristics) DMLRuntimeException(org.apache.sysml.runtime.DMLRuntimeException)

Example 18 with IndexRange

use of org.apache.sysml.runtime.util.IndexRange in project systemml by apache.

the class RangeBasedReIndexInstruction method parseInstruction.

public static RangeBasedReIndexInstruction parseInstruction(String str) {
    InstructionUtils.checkNumFields(str, 8);
    String[] parts = InstructionUtils.getInstructionParts(str);
    String opcode = parts[0];
    boolean forLeft = false;
    if (opcode.equalsIgnoreCase(RightIndex.OPCODE + "ForLeft"))
        forLeft = true;
    else if (!opcode.equalsIgnoreCase(RightIndex.OPCODE))
        throw new DMLRuntimeException("Unknown opcode while parsing a Select: " + str);
    byte in = Byte.parseByte(parts[1]);
    IndexRange rng = new IndexRange(UtilFunctions.parseToLong(parts[2]), UtilFunctions.parseToLong(parts[3]), UtilFunctions.parseToLong(parts[4]), UtilFunctions.parseToLong(parts[5]));
    byte out = Byte.parseByte(parts[6]);
    long rlen = Long.parseLong(parts[7]);
    long clen = Long.parseLong(parts[8]);
    return new RangeBasedReIndexInstruction(new ReIndexOperator(), in, out, rng, forLeft, rlen, clen, str);
}
Also used : IndexRange(org.apache.sysml.runtime.util.IndexRange) ReIndexOperator(org.apache.sysml.runtime.matrix.operators.ReIndexOperator) DMLRuntimeException(org.apache.sysml.runtime.DMLRuntimeException)

Example 19 with IndexRange

use of org.apache.sysml.runtime.util.IndexRange in project systemml by apache.

the class MatrixIndexingGPUInstruction method processInstruction.

@Override
public void processInstruction(ExecutionContext ec) {
    GPUStatistics.incrementNoOfExecutedGPUInst();
    String opcode = getOpcode();
    IndexRange ixrange = getIndexRange(ec);
    if (opcode.equalsIgnoreCase(RightIndex.OPCODE)) {
        MatrixObject mat1 = getMatrixInputForGPUInstruction(ec, input1.getName());
        LibMatrixCUDA.sliceOperations(ec, ec.getGPUContext(0), getExtendedOpcode(), mat1, ixrange, output.getName());
        ec.releaseMatrixInputForGPUInstruction(input1.getName());
        ec.releaseMatrixOutputForGPUInstruction(output.getName());
    } else {
        throw new DMLRuntimeException("Unsupported GPU operator:" + opcode);
    }
}
Also used : IndexRange(org.apache.sysml.runtime.util.IndexRange) MatrixObject(org.apache.sysml.runtime.controlprogram.caching.MatrixObject) DMLRuntimeException(org.apache.sysml.runtime.DMLRuntimeException)

Example 20 with IndexRange

use of org.apache.sysml.runtime.util.IndexRange in project systemml by apache.

the class OperationsOnMatrixValues method performSlice.

public static void performSlice(IndexedMatrixValue in, IndexRange ixrange, int brlen, int bclen, ArrayList<IndexedMatrixValue> outlist) {
    long cellIndexTopRow = UtilFunctions.computeCellIndex(in.getIndexes().getRowIndex(), brlen, 0);
    long cellIndexBottomRow = UtilFunctions.computeCellIndex(in.getIndexes().getRowIndex(), brlen, in.getValue().getNumRows() - 1);
    long cellIndexLeftCol = UtilFunctions.computeCellIndex(in.getIndexes().getColumnIndex(), bclen, 0);
    long cellIndexRightCol = UtilFunctions.computeCellIndex(in.getIndexes().getColumnIndex(), bclen, in.getValue().getNumColumns() - 1);
    long cellIndexOverlapTop = Math.max(cellIndexTopRow, ixrange.rowStart);
    long cellIndexOverlapBottom = Math.min(cellIndexBottomRow, ixrange.rowEnd);
    long cellIndexOverlapLeft = Math.max(cellIndexLeftCol, ixrange.colStart);
    long cellIndexOverlapRight = Math.min(cellIndexRightCol, ixrange.colEnd);
    // check if block is outside the indexing range
    if (cellIndexOverlapTop > cellIndexOverlapBottom || cellIndexOverlapLeft > cellIndexOverlapRight) {
        return;
    }
    IndexRange tmpRange = new IndexRange(UtilFunctions.computeCellInBlock(cellIndexOverlapTop, brlen), UtilFunctions.computeCellInBlock(cellIndexOverlapBottom, brlen), UtilFunctions.computeCellInBlock(cellIndexOverlapLeft, bclen), UtilFunctions.computeCellInBlock(cellIndexOverlapRight, bclen));
    int rowCut = UtilFunctions.computeCellInBlock(ixrange.rowStart, brlen);
    int colCut = UtilFunctions.computeCellInBlock(ixrange.colStart, bclen);
    int rowsInLastBlock = (int) ((ixrange.rowEnd - ixrange.rowStart + 1) % brlen);
    if (rowsInLastBlock == 0)
        rowsInLastBlock = brlen;
    int colsInLastBlock = (int) ((ixrange.colEnd - ixrange.colStart + 1) % bclen);
    if (colsInLastBlock == 0)
        colsInLastBlock = bclen;
    long resultBlockIndexTop = UtilFunctions.computeBlockIndex(cellIndexOverlapTop - ixrange.rowStart + 1, brlen);
    long resultBlockIndexBottom = UtilFunctions.computeBlockIndex(cellIndexOverlapBottom - ixrange.rowStart + 1, brlen);
    long resultBlockIndexLeft = UtilFunctions.computeBlockIndex(cellIndexOverlapLeft - ixrange.colStart + 1, bclen);
    long resultBlockIndexRight = UtilFunctions.computeBlockIndex(cellIndexOverlapRight - ixrange.colStart + 1, bclen);
    int boundaryRlen = brlen;
    int boundaryClen = bclen;
    long finalBlockIndexBottom = UtilFunctions.computeBlockIndex(ixrange.rowEnd - ixrange.rowStart + 1, brlen);
    long finalBlockIndexRight = UtilFunctions.computeBlockIndex(ixrange.colEnd - ixrange.colStart + 1, bclen);
    if (resultBlockIndexBottom == finalBlockIndexBottom)
        boundaryRlen = rowsInLastBlock;
    if (resultBlockIndexRight == finalBlockIndexRight)
        boundaryClen = colsInLastBlock;
    // allocate space for the output value
    for (long r = resultBlockIndexTop; r <= resultBlockIndexBottom; r++) for (long c = resultBlockIndexLeft; c <= resultBlockIndexRight; c++) {
        IndexedMatrixValue out = new IndexedMatrixValue(new MatrixIndexes(), new MatrixBlock());
        out.getIndexes().setIndexes(r, c);
        outlist.add(out);
    }
    // execute actual slice operation
    in.getValue().slice(outlist, tmpRange, rowCut, colCut, brlen, bclen, boundaryRlen, boundaryClen);
}
Also used : IndexRange(org.apache.sysml.runtime.util.IndexRange) CompressedMatrixBlock(org.apache.sysml.runtime.compress.CompressedMatrixBlock) IndexedMatrixValue(org.apache.sysml.runtime.matrix.mapred.IndexedMatrixValue)

Aggregations

IndexRange (org.apache.sysml.runtime.util.IndexRange)23 DMLRuntimeException (org.apache.sysml.runtime.DMLRuntimeException)19 MatrixObject (org.apache.sysml.runtime.controlprogram.caching.MatrixObject)6 MatrixCharacteristics (org.apache.sysml.runtime.matrix.MatrixCharacteristics)6 MatrixBlock (org.apache.sysml.runtime.matrix.data.MatrixBlock)6 JavaPairRDD (org.apache.spark.api.java.JavaPairRDD)4 SparkExecutionContext (org.apache.sysml.runtime.controlprogram.context.SparkExecutionContext)4 PartitionedBroadcast (org.apache.sysml.runtime.instructions.spark.data.PartitionedBroadcast)4 FrameBlock (org.apache.sysml.runtime.matrix.data.FrameBlock)4 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3 Pair (org.apache.sysml.runtime.matrix.data.Pair)3 ValueType (org.apache.sysml.parser.Expression.ValueType)2 CompressedMatrixBlock (org.apache.sysml.runtime.compress.CompressedMatrixBlock)2 UpdateType (org.apache.sysml.runtime.controlprogram.caching.MatrixObject.UpdateType)2 IsFrameBlockInRange (org.apache.sysml.runtime.instructions.spark.functions.IsFrameBlockInRange)2 MetaDataFormat (org.apache.sysml.runtime.matrix.MetaDataFormat)2 MatrixIndexes (org.apache.sysml.runtime.matrix.data.MatrixIndexes)2 IndexedMatrixValue (org.apache.sysml.runtime.matrix.mapred.IndexedMatrixValue)2 ReIndexOperator (org.apache.sysml.runtime.matrix.operators.ReIndexOperator)2