Search in sources :

Example 16 with MatrixValue

use of org.apache.sysml.runtime.matrix.data.MatrixValue in project incubator-systemml by apache.

the class UaggOuterChainInstruction method processInstruction.

@Override
public void processInstruction(Class<? extends MatrixValue> valueClass, CachedValueMap cachedValues, IndexedMatrixValue tempValue, IndexedMatrixValue zeroInput, int blockRowFactor, int blockColFactor) {
    ArrayList<IndexedMatrixValue> blkList = null;
    boolean rightCached = (_uaggOp.indexFn instanceof ReduceCol || _uaggOp.indexFn instanceof ReduceAll || !LibMatrixOuterAgg.isSupportedUaggOp(_uaggOp, _bOp));
    // get the main data input
    if (rightCached)
        blkList = cachedValues.get(input1);
    else
        // ReduceRow
        blkList = cachedValues.get(input2);
    if (blkList == null)
        return;
    for (IndexedMatrixValue imv : blkList) {
        if (imv == null)
            continue;
        MatrixIndexes in1Ix = imv.getIndexes();
        MatrixValue in1Val = imv.getValue();
        // allocate space for the intermediate and output value
        IndexedMatrixValue iout = cachedValues.holdPlace(output, valueClass);
        MatrixIndexes outIx = iout.getIndexes();
        MatrixValue outVal = iout.getValue();
        MatrixBlock corr = null;
        // get the distributed cache input
        byte dcInputIx = rightCached ? input2 : input1;
        DistributedCacheInput dcInput = MRBaseForCommonInstructions.dcValues.get(dcInputIx);
        // process instruction
        if (LibMatrixOuterAgg.isSupportedUaggOp(_uaggOp, _bOp)) {
            if ((LibMatrixOuterAgg.isRowIndexMax(_uaggOp)) || (LibMatrixOuterAgg.isRowIndexMin(_uaggOp))) {
                if (_bv == null) {
                    if (rightCached)
                        _bv = dcInput.getRowVectorArray();
                    else
                        _bv = dcInput.getColumnVectorArray();
                    _bvi = LibMatrixOuterAgg.prepareRowIndices(_bv.length, _bv, _bOp, _uaggOp);
                }
            } else {
                // step 1: prepare sorted rhs input (once per task)
                if (_bv == null) {
                    if (rightCached)
                        _bv = dcInput.getRowVectorArray();
                    else
                        _bv = dcInput.getColumnVectorArray();
                    Arrays.sort(_bv);
                }
            }
            LibMatrixOuterAgg.resetOutputMatrix(in1Ix, (MatrixBlock) in1Val, outIx, (MatrixBlock) outVal, _uaggOp);
            LibMatrixOuterAgg.aggregateMatrix((MatrixBlock) in1Val, (MatrixBlock) outVal, _bv, _bvi, _bOp, _uaggOp);
        } else // default case
        {
            long in2_cols = dcInput.getNumCols();
            long in2_colBlocks = (long) Math.ceil(((double) in2_cols) / dcInput.getNumColsPerBlock());
            for (int bidx = 1; bidx <= in2_colBlocks; bidx++) {
                IndexedMatrixValue imv2 = dcInput.getDataBlock(1, bidx);
                MatrixValue in2Val = imv2.getValue();
                // outer block operation
                OperationsOnMatrixValues.performBinaryIgnoreIndexes(in1Val, in2Val, _tmpVal1, _bOp);
                // unary aggregate operation
                OperationsOnMatrixValues.performAggregateUnary(in1Ix, _tmpVal1, outIx, _tmpVal2, _uaggOp, blockRowFactor, blockColFactor);
                // aggregate over all rhs blocks
                if (corr == null) {
                    outVal.reset(_tmpVal2.getNumRows(), _tmpVal2.getNumColumns(), false);
                    corr = new MatrixBlock(_tmpVal2.getNumRows(), _tmpVal2.getNumColumns(), false);
                }
                if (_aggOp.correctionExists)
                    OperationsOnMatrixValues.incrementalAggregation(outVal, corr, _tmpVal2, _aggOp, true);
                else
                    OperationsOnMatrixValues.incrementalAggregation(outVal, null, _tmpVal2, _aggOp, true);
            }
        }
    }
}
Also used : ReduceCol(org.apache.sysml.runtime.functionobjects.ReduceCol) DistributedCacheInput(org.apache.sysml.runtime.matrix.mapred.DistributedCacheInput) ReduceAll(org.apache.sysml.runtime.functionobjects.ReduceAll) MatrixBlock(org.apache.sysml.runtime.matrix.data.MatrixBlock) IndexedMatrixValue(org.apache.sysml.runtime.matrix.mapred.IndexedMatrixValue) MatrixValue(org.apache.sysml.runtime.matrix.data.MatrixValue) MatrixIndexes(org.apache.sysml.runtime.matrix.data.MatrixIndexes) IndexedMatrixValue(org.apache.sysml.runtime.matrix.mapred.IndexedMatrixValue)

Example 17 with MatrixValue

use of org.apache.sysml.runtime.matrix.data.MatrixValue in project incubator-systemml by apache.

the class CachedValueMap method set.

public void set(byte tag, MatrixIndexes indexes, MatrixValue value, boolean copy) {
    if (copy) {
        // create value copy
        MatrixValue tmp = null;
        try {
            tmp = value.getClass().newInstance();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        tmp.copy(value);
        set(tag, indexes, tmp);
    } else
        set(tag, indexes, value);
}
Also used : MatrixValue(org.apache.sysml.runtime.matrix.data.MatrixValue)

Example 18 with MatrixValue

use of org.apache.sysml.runtime.matrix.data.MatrixValue in project incubator-systemml by apache.

the class MMCJMRCombinerReducerBase method performAggregateInstructions.

protected MatrixValue performAggregateInstructions(TaggedFirstSecondIndexes indexes, Iterator<MatrixValue> values) throws IOException {
    // manipulation on the tags first
    byte realTag = indexes.getTag();
    byte representTag;
    if (realTag == tagForLeft)
        representTag = aggBinInstruction.input1;
    else
        representTag = aggBinInstruction.input2;
    ArrayList<AggregateInstruction> instructions = agg_instructions.get(representTag);
    AggregateInstruction ins;
    if (instructions == null) {
        defaultAggIns.input = realTag;
        defaultAggIns.output = realTag;
        ins = defaultAggIns;
    } else {
        if (instructions.size() > 1)
            throw new IOException("only one aggregate operation on input " + indexes.getTag() + " is allowed in BlockMMCJMR");
        ins = instructions.get(0);
        if (ins.input != ins.output)
            throw new IOException("input index and output index have to be " + "the same for aggregate instructions in BlockMMCJMR");
    }
    // performa aggregation before doing mmcj
    // TODO: customize the code, since aggregation for matrix multiplcation can only be sum
    boolean needStartAgg = true;
    try {
        while (values.hasNext()) {
            MatrixValue value = values.next();
            if (needStartAgg) {
                buffer.reset(value.getNumRows(), value.getNumColumns(), value.isInSparseFormat());
                needStartAgg = false;
            // LOG.info("initialize buffer: sparse="+buffer.isInSparseFormat()+", nonZero="+buffer.getNonZeros());
            }
            buffer.binaryOperationsInPlace(((AggregateOperator) ins.getOperator()).increOp, value);
        // LOG.info("increment buffer: sparse="+buffer.isInSparseFormat()+", nonZero="+buffer.getNonZeros());
        }
    } catch (Exception e) {
        throw new IOException(e);
    }
    if (needStartAgg)
        return null;
    else
        return buffer;
}
Also used : MatrixValue(org.apache.sysml.runtime.matrix.data.MatrixValue) IOException(java.io.IOException) AggregateInstruction(org.apache.sysml.runtime.instructions.mr.AggregateInstruction) DMLRuntimeException(org.apache.sysml.runtime.DMLRuntimeException) IOException(java.io.IOException)

Example 19 with MatrixValue

use of org.apache.sysml.runtime.matrix.data.MatrixValue in project incubator-systemml by apache.

the class MMCJMRReducerWithAggregator method reduce.

@Override
public void reduce(TaggedFirstSecondIndexes indexes, Iterator<MatrixValue> values, OutputCollector<Writable, Writable> out, Reporter report) throws IOException {
    long start = System.currentTimeMillis();
    commonSetup(report);
    // perform aggregate (if necessary, only for binary cell)
    MatrixValue aggregateValue = null;
    if (valueClass == MatrixBlock.class) {
        // multiple blocks for same indexes impossible
        aggregateValue = values.next();
    } else // MatrixCell.class
    {
        aggregateValue = performAggregateInstructions(indexes, values);
        if (aggregateValue == null)
            return;
    }
    int tag = indexes.getTag();
    long firstIndex = indexes.getFirstIndex();
    long secondIndex = indexes.getSecondIndex();
    // for a different k
    if (prevFirstIndex != firstIndex) {
        cache.resetCache(true);
        prevFirstIndex = firstIndex;
    } else if (prevTag > tag)
        throw new RuntimeException("tag is not ordered correctly: " + prevTag + " > " + tag);
    prevTag = tag;
    // perform cross-product binagg
    processJoin(tag, secondIndex, aggregateValue);
    report.incrCounter(Counters.COMBINE_OR_REDUCE_TIME, System.currentTimeMillis() - start);
}
Also used : MatrixValue(org.apache.sysml.runtime.matrix.data.MatrixValue)

Example 20 with MatrixValue

use of org.apache.sysml.runtime.matrix.data.MatrixValue in project systemml by apache.

the class UaggOuterChainInstruction method processInstruction.

@Override
public void processInstruction(Class<? extends MatrixValue> valueClass, CachedValueMap cachedValues, IndexedMatrixValue tempValue, IndexedMatrixValue zeroInput, int blockRowFactor, int blockColFactor) {
    ArrayList<IndexedMatrixValue> blkList = null;
    boolean rightCached = (_uaggOp.indexFn instanceof ReduceCol || _uaggOp.indexFn instanceof ReduceAll || !LibMatrixOuterAgg.isSupportedUaggOp(_uaggOp, _bOp));
    // get the main data input
    if (rightCached)
        blkList = cachedValues.get(input1);
    else
        // ReduceRow
        blkList = cachedValues.get(input2);
    if (blkList == null)
        return;
    for (IndexedMatrixValue imv : blkList) {
        if (imv == null)
            continue;
        MatrixIndexes in1Ix = imv.getIndexes();
        MatrixValue in1Val = imv.getValue();
        // allocate space for the intermediate and output value
        IndexedMatrixValue iout = cachedValues.holdPlace(output, valueClass);
        MatrixIndexes outIx = iout.getIndexes();
        MatrixValue outVal = iout.getValue();
        MatrixBlock corr = null;
        // get the distributed cache input
        byte dcInputIx = rightCached ? input2 : input1;
        DistributedCacheInput dcInput = MRBaseForCommonInstructions.dcValues.get(dcInputIx);
        // process instruction
        if (LibMatrixOuterAgg.isSupportedUaggOp(_uaggOp, _bOp)) {
            if ((LibMatrixOuterAgg.isRowIndexMax(_uaggOp)) || (LibMatrixOuterAgg.isRowIndexMin(_uaggOp))) {
                if (_bv == null) {
                    if (rightCached)
                        _bv = dcInput.getRowVectorArray();
                    else
                        _bv = dcInput.getColumnVectorArray();
                    _bvi = LibMatrixOuterAgg.prepareRowIndices(_bv.length, _bv, _bOp, _uaggOp);
                }
            } else {
                // step 1: prepare sorted rhs input (once per task)
                if (_bv == null) {
                    if (rightCached)
                        _bv = dcInput.getRowVectorArray();
                    else
                        _bv = dcInput.getColumnVectorArray();
                    Arrays.sort(_bv);
                }
            }
            LibMatrixOuterAgg.resetOutputMatrix(in1Ix, (MatrixBlock) in1Val, outIx, (MatrixBlock) outVal, _uaggOp);
            LibMatrixOuterAgg.aggregateMatrix((MatrixBlock) in1Val, (MatrixBlock) outVal, _bv, _bvi, _bOp, _uaggOp);
        } else // default case
        {
            long in2_cols = dcInput.getNumCols();
            long in2_colBlocks = (long) Math.ceil(((double) in2_cols) / dcInput.getNumColsPerBlock());
            for (int bidx = 1; bidx <= in2_colBlocks; bidx++) {
                IndexedMatrixValue imv2 = dcInput.getDataBlock(1, bidx);
                MatrixValue in2Val = imv2.getValue();
                // outer block operation
                OperationsOnMatrixValues.performBinaryIgnoreIndexes(in1Val, in2Val, _tmpVal1, _bOp);
                // unary aggregate operation
                OperationsOnMatrixValues.performAggregateUnary(in1Ix, _tmpVal1, outIx, _tmpVal2, _uaggOp, blockRowFactor, blockColFactor);
                // aggregate over all rhs blocks
                if (corr == null) {
                    outVal.reset(_tmpVal2.getNumRows(), _tmpVal2.getNumColumns(), false);
                    corr = new MatrixBlock(_tmpVal2.getNumRows(), _tmpVal2.getNumColumns(), false);
                }
                if (_aggOp.correctionExists)
                    OperationsOnMatrixValues.incrementalAggregation(outVal, corr, _tmpVal2, _aggOp, true);
                else
                    OperationsOnMatrixValues.incrementalAggregation(outVal, null, _tmpVal2, _aggOp, true);
            }
        }
    }
}
Also used : ReduceCol(org.apache.sysml.runtime.functionobjects.ReduceCol) DistributedCacheInput(org.apache.sysml.runtime.matrix.mapred.DistributedCacheInput) ReduceAll(org.apache.sysml.runtime.functionobjects.ReduceAll) MatrixBlock(org.apache.sysml.runtime.matrix.data.MatrixBlock) IndexedMatrixValue(org.apache.sysml.runtime.matrix.mapred.IndexedMatrixValue) MatrixValue(org.apache.sysml.runtime.matrix.data.MatrixValue) MatrixIndexes(org.apache.sysml.runtime.matrix.data.MatrixIndexes) IndexedMatrixValue(org.apache.sysml.runtime.matrix.mapred.IndexedMatrixValue)

Aggregations

MatrixValue (org.apache.sysml.runtime.matrix.data.MatrixValue)28 MatrixIndexes (org.apache.sysml.runtime.matrix.data.MatrixIndexes)22 IndexedMatrixValue (org.apache.sysml.runtime.matrix.mapred.IndexedMatrixValue)16 MatrixBlock (org.apache.sysml.runtime.matrix.data.MatrixBlock)10 ArrayList (java.util.ArrayList)6 DistributedCacheInput (org.apache.sysml.runtime.matrix.mapred.DistributedCacheInput)6 IOException (java.io.IOException)4 DMLRuntimeException (org.apache.sysml.runtime.DMLRuntimeException)2 ReduceAll (org.apache.sysml.runtime.functionobjects.ReduceAll)2 ReduceCol (org.apache.sysml.runtime.functionobjects.ReduceCol)2 AggregateInstruction (org.apache.sysml.runtime.instructions.mr.AggregateInstruction)2 ReblockInstruction (org.apache.sysml.runtime.instructions.mr.ReblockInstruction)2 MatrixCharacteristics (org.apache.sysml.runtime.matrix.MatrixCharacteristics)2 AggregateBinaryOperator (org.apache.sysml.runtime.matrix.operators.AggregateBinaryOperator)2 QuaternaryOperator (org.apache.sysml.runtime.matrix.operators.QuaternaryOperator)2 ReorgOperator (org.apache.sysml.runtime.matrix.operators.ReorgOperator)2