Search in sources :

Example 11 with IndexRange

use of org.apache.sysml.runtime.util.IndexRange in project incubator-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 12 with IndexRange

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

the class PartitionedBlock method slice.

/**
 * Utility for slice operations over partitioned matrices, where the index range can cover
 * multiple blocks. The result is always a single result matrix block. All semantics are
 * equivalent to the core matrix block slice operations.
 *
 * @param rl row lower bound
 * @param ru row upper bound
 * @param cl column lower bound
 * @param cu column upper bound
 * @param block block object
 * @return block object
 */
@SuppressWarnings("unchecked")
public T slice(long rl, long ru, long cl, long cu, T block) {
    int lrl = (int) rl;
    int lru = (int) ru;
    int lcl = (int) cl;
    int lcu = (int) cu;
    ArrayList<Pair<?, ?>> allBlks = (ArrayList<Pair<?, ?>>) CacheBlockFactory.getPairList(block);
    int start_iix = (lrl - 1) / _brlen + 1;
    int end_iix = (lru - 1) / _brlen + 1;
    int start_jix = (lcl - 1) / _bclen + 1;
    int end_jix = (lcu - 1) / _bclen + 1;
    for (int iix = start_iix; iix <= end_iix; iix++) for (int jix = start_jix; jix <= end_jix; jix++) {
        IndexRange ixrange = new IndexRange(rl, ru, cl, cu);
        allBlks.addAll(OperationsOnMatrixValues.performSlice(ixrange, _brlen, _bclen, iix, jix, getBlock(iix, jix)));
    }
    if (allBlks.size() == 1) {
        return (T) allBlks.get(0).getValue();
    } else {
        // allocate output matrix
        Constructor<?> constr;
        try {
            constr = block.getClass().getConstructor(int.class, int.class, boolean.class);
            T ret = (T) constr.newInstance(lru - lrl + 1, lcu - lcl + 1, false);
            for (Pair<?, ?> kv : allBlks) {
                ret.merge((T) kv.getValue(), false);
            }
            return ret;
        } catch (Exception e) {
            throw new DMLRuntimeException(e);
        }
    }
}
Also used : IndexRange(org.apache.sysml.runtime.util.IndexRange) ArrayList(java.util.ArrayList) DMLRuntimeException(org.apache.sysml.runtime.DMLRuntimeException) IOException(java.io.IOException) Pair(org.apache.sysml.runtime.matrix.data.Pair) DMLRuntimeException(org.apache.sysml.runtime.DMLRuntimeException)

Example 13 with IndexRange

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

the class FrameIndexingCPInstruction method processInstruction.

@Override
public void processInstruction(ExecutionContext ec) {
    String opcode = getOpcode();
    IndexRange ixrange = getIndexRange(ec);
    // right indexing
    if (opcode.equalsIgnoreCase(RightIndex.OPCODE)) {
        // execute right indexing operation
        FrameBlock in = ec.getFrameInput(input1.getName());
        FrameBlock out = in.slice(ixrange, new FrameBlock());
        // unpin rhs input
        ec.releaseFrameInput(input1.getName());
        // unpin output
        ec.setFrameOutput(output.getName(), out);
    } else // left indexing
    if (opcode.equalsIgnoreCase(LeftIndex.OPCODE)) {
        FrameBlock lin = ec.getFrameInput(input1.getName());
        FrameBlock out = null;
        if (input2.getDataType() == DataType.FRAME) {
            // FRAME<-FRAME
            FrameBlock rin = ec.getFrameInput(input2.getName());
            out = lin.leftIndexingOperations(rin, ixrange, new FrameBlock());
            ec.releaseFrameInput(input2.getName());
        } else {
            // FRAME<-SCALAR
            if (!ixrange.isScalar())
                throw new DMLRuntimeException("Invalid index range of scalar leftindexing: " + ixrange.toString() + ".");
            ScalarObject scalar = ec.getScalarInput(input2.getName(), input2.getValueType(), input2.isLiteral());
            out = new FrameBlock(lin);
            out.set((int) ixrange.rowStart, (int) ixrange.colStart, scalar.getStringValue());
        }
        // unpin lhs input
        ec.releaseFrameInput(input1.getName());
        // unpin output
        ec.setFrameOutput(output.getName(), out);
    } else
        throw new DMLRuntimeException("Invalid opcode (" + opcode + ") encountered in FrameIndexingCPInstruction.");
}
Also used : IndexRange(org.apache.sysml.runtime.util.IndexRange) FrameBlock(org.apache.sysml.runtime.matrix.data.FrameBlock) DMLRuntimeException(org.apache.sysml.runtime.DMLRuntimeException)

Example 14 with IndexRange

use of org.apache.sysml.runtime.util.IndexRange in project incubator-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 15 with IndexRange

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

the class OperationsOnMatrixValues method performSlice.

/**
 * This function will get slice of the input frame block overlapping in overall slice(Range), slice has requested for.
 *
 * @param in ?
 * @param ixrange index range
 * @param brlen number of rows in a block
 * @param bclen number of columns in a block
 * @param outlist list of pairs of frame blocks
 */
public static void performSlice(Pair<Long, FrameBlock> in, IndexRange ixrange, int brlen, int bclen, ArrayList<Pair<Long, FrameBlock>> outlist) {
    long index = in.getKey();
    FrameBlock block = in.getValue();
    // Get Block indexes (rows and columns boundaries)
    long cellIndexTopRow = index;
    long cellIndexBottomRow = index + block.getNumRows() - 1;
    long cellIndexLeftCol = 1;
    long cellIndexRightCol = block.getNumColumns();
    // Calculate block boundaries with range of slice to be performed (Global index)
    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;
    }
    // Create IndexRange for the slice to be performed on this block.
    IndexRange tmpRange = new IndexRange(cellIndexOverlapTop - index, cellIndexOverlapBottom - index, cellIndexOverlapLeft - 1, cellIndexOverlapRight - 1);
    // Get Top Row and Left column cutting point.
    int rowCut = (int) (ixrange.rowStart - index);
    // Get indices for result block
    long resultBlockIndexTop = UtilFunctions.computeBlockIndex(cellIndexOverlapTop, brlen);
    long resultBlockIndexBottom = UtilFunctions.computeBlockIndex(cellIndexOverlapBottom, brlen);
    // allocate space for the output value
    for (long r = resultBlockIndexTop; r <= resultBlockIndexBottom; r++) {
        ValueType[] schema = Arrays.copyOfRange(block.getSchema(), (int) tmpRange.colStart, (int) tmpRange.colEnd + 1);
        long iResultIndex = Math.max(((r - 1) * brlen - ixrange.rowStart + 1), 0);
        Pair<Long, FrameBlock> out = new Pair<>(new Long(iResultIndex + 1), new FrameBlock(schema));
        outlist.add(out);
    }
    // execute actual slice operation
    block.slice(outlist, tmpRange, rowCut);
}
Also used : IndexRange(org.apache.sysml.runtime.util.IndexRange) ValueType(org.apache.sysml.parser.Expression.ValueType)

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