Search in sources :

Example 1 with IndexedMatrixValue

use of org.apache.sysml.runtime.matrix.mapred.IndexedMatrixValue in project incubator-systemml by apache.

the class AggregateBinaryInstruction method processInstruction.

@Override
public void processInstruction(Class<? extends MatrixValue> valueClass, CachedValueMap cachedValues, IndexedMatrixValue tempValue, IndexedMatrixValue zeroInput, int blockRowFactor, int blockColFactor) {
    IndexedMatrixValue in1 = cachedValues.getFirst(input1);
    IndexedMatrixValue in2 = cachedValues.getFirst(input2);
    if (_opcode.equals(MapMult.OPCODE)) {
        // check empty inputs (data for different instructions)
        if (_cacheType.isRight() ? in1 == null : in2 == null)
            return;
        // one of the input is from distributed cache.
        processMapMultInstruction(valueClass, cachedValues, in1, in2, blockRowFactor, blockColFactor);
    } else // generic matrix mult
    {
        // check empty inputs (data for different instructions)
        if (in1 == null || in2 == null)
            return;
        // allocate space for the output value
        IndexedMatrixValue out;
        if (output == input1 || output == input2)
            out = tempValue;
        else
            out = cachedValues.holdPlace(output, valueClass);
        // process instruction
        OperationsOnMatrixValues.performAggregateBinary(in1.getIndexes(), (MatrixBlock) in1.getValue(), in2.getIndexes(), (MatrixBlock) in2.getValue(), out.getIndexes(), (MatrixBlock) out.getValue(), ((AggregateBinaryOperator) optr));
        // put the output value in the cache
        if (out == tempValue)
            cachedValues.add(output, out);
    }
}
Also used : AggregateBinaryOperator(org.apache.sysml.runtime.matrix.operators.AggregateBinaryOperator) IndexedMatrixValue(org.apache.sysml.runtime.matrix.mapred.IndexedMatrixValue)

Example 2 with IndexedMatrixValue

use of org.apache.sysml.runtime.matrix.mapred.IndexedMatrixValue in project incubator-systemml by apache.

the class AggregateBinaryInstruction method processMapMultInstruction.

/**
 * Helper function to perform map-side matrix-matrix multiplication.
 *
 * @param valueClass matrix value class
 * @param cachedValues cached value map
 * @param in1 indexed matrix value 1
 * @param in2 indexed matrix value 2
 * @param blockRowFactor ?
 * @param blockColFactor ?
 */
private void processMapMultInstruction(Class<? extends MatrixValue> valueClass, CachedValueMap cachedValues, IndexedMatrixValue in1, IndexedMatrixValue in2, int blockRowFactor, int blockColFactor) {
    boolean removeOutput = true;
    if (_cacheType.isRight()) {
        DistributedCacheInput dcInput = MRBaseForCommonInstructions.dcValues.get(input2);
        long in2_cols = dcInput.getNumCols();
        long in2_colBlocks = (long) Math.ceil(((double) in2_cols) / dcInput.getNumColsPerBlock());
        for (int bidx = 1; bidx <= in2_colBlocks; bidx++) {
            // Matrix multiply A[i,k] %*% B[k,bid]
            // Setup input2 block
            IndexedMatrixValue in2Block = dcInput.getDataBlock((int) in1.getIndexes().getColumnIndex(), bidx);
            MatrixValue in2BlockValue = in2Block.getValue();
            MatrixIndexes in2BlockIndex = in2Block.getIndexes();
            // allocate space for the output value
            IndexedMatrixValue out = cachedValues.holdPlace(output, valueClass);
            // process instruction
            OperationsOnMatrixValues.performAggregateBinary(in1.getIndexes(), (MatrixBlock) in1.getValue(), in2BlockIndex, (MatrixBlock) in2BlockValue, out.getIndexes(), (MatrixBlock) out.getValue(), ((AggregateBinaryOperator) optr));
            removeOutput &= (!_outputEmptyBlocks && out.getValue().isEmpty());
        }
    } else {
        DistributedCacheInput dcInput = MRBaseForCommonInstructions.dcValues.get(input1);
        long in1_rows = dcInput.getNumRows();
        long in1_rowsBlocks = (long) Math.ceil(((double) in1_rows) / dcInput.getNumRowsPerBlock());
        for (int bidx = 1; bidx <= in1_rowsBlocks; bidx++) {
            // Matrix multiply A[i,k] %*% B[k,bid]
            // Setup input2 block
            IndexedMatrixValue in1Block = dcInput.getDataBlock(bidx, (int) in2.getIndexes().getRowIndex());
            MatrixValue in1BlockValue = in1Block.getValue();
            MatrixIndexes in1BlockIndex = in1Block.getIndexes();
            // allocate space for the output value
            IndexedMatrixValue out = cachedValues.holdPlace(output, valueClass);
            // process instruction
            OperationsOnMatrixValues.performAggregateBinary(in1BlockIndex, (MatrixBlock) in1BlockValue, in2.getIndexes(), (MatrixBlock) in2.getValue(), out.getIndexes(), (MatrixBlock) out.getValue(), ((AggregateBinaryOperator) optr));
            removeOutput &= (!_outputEmptyBlocks && out.getValue().isEmpty());
        }
    }
    // empty block output filter (enabled by compiler consumer operation is in CP)
    if (removeOutput)
        cachedValues.remove(output);
}
Also used : DistributedCacheInput(org.apache.sysml.runtime.matrix.mapred.DistributedCacheInput) IndexedMatrixValue(org.apache.sysml.runtime.matrix.mapred.IndexedMatrixValue) MatrixValue(org.apache.sysml.runtime.matrix.data.MatrixValue) MatrixIndexes(org.apache.sysml.runtime.matrix.data.MatrixIndexes) AggregateBinaryOperator(org.apache.sysml.runtime.matrix.operators.AggregateBinaryOperator) IndexedMatrixValue(org.apache.sysml.runtime.matrix.mapred.IndexedMatrixValue)

Example 3 with IndexedMatrixValue

use of org.apache.sysml.runtime.matrix.mapred.IndexedMatrixValue in project incubator-systemml by apache.

the class AppendGInstruction method processInstruction.

@Override
public void processInstruction(Class<? extends MatrixValue> valueClass, CachedValueMap cachedValues, IndexedMatrixValue tempValue, IndexedMatrixValue zeroInput, int brlen, int bclen) {
    // setup basic meta data
    int blen = _cbind ? bclen : brlen;
    // Step 1: handle first input (forward blocks, change dim of last block)
    ArrayList<IndexedMatrixValue> blkList1 = cachedValues.get(input1);
    if (blkList1 != null)
        for (IndexedMatrixValue in1 : blkList1) {
            if (in1 == null)
                continue;
            if (_offset % blen == 0) {
                // special case: forward only
                cachedValues.add(output, in1);
            } else // general case: change dims and forward
            {
                MatrixIndexes tmpix = in1.getIndexes();
                // always block
                MatrixBlock tmpval = (MatrixBlock) in1.getValue();
                if (// border block
                _cbind && _offset / blen + 1 == tmpix.getColumnIndex() || !_cbind && _offset / blen + 1 == tmpix.getRowIndex()) {
                    IndexedMatrixValue data = cachedValues.holdPlace(output, valueClass);
                    // always block
                    MatrixBlock tmpvalNew = (MatrixBlock) data.getValue();
                    int lrlen = _cbind ? tmpval.getNumRows() : Math.min(blen, (int) (_len - (tmpix.getRowIndex() - 1) * blen));
                    int lclen = _cbind ? Math.min(blen, (int) (_len - (tmpix.getColumnIndex() - 1) * blen)) : tmpval.getNumColumns();
                    tmpvalNew.reset(lrlen, lclen);
                    tmpvalNew.copy(0, tmpval.getNumRows() - 1, 0, tmpval.getNumColumns() - 1, tmpval, true);
                    data.getIndexes().setIndexes(tmpix);
                } else // inner block
                {
                    cachedValues.add(output, in1);
                }
            }
        }
    // Step 2: handle second input (split/forward blocks with new index)
    ArrayList<IndexedMatrixValue> blkList2 = cachedValues.get(input2);
    if (blkList2 != null)
        for (IndexedMatrixValue in2 : blkList2) {
            if (in2 == null)
                continue;
            MatrixIndexes tmpix = in2.getIndexes();
            // always block
            MatrixBlock tmpval = (MatrixBlock) in2.getValue();
            if (// special case no split
            _offset % bclen == 0) {
                IndexedMatrixValue data = cachedValues.holdPlace(output, valueClass);
                MatrixIndexes ix1 = data.getIndexes();
                long rix = _cbind ? tmpix.getRowIndex() : _offset / blen + tmpix.getRowIndex();
                long cix = _cbind ? _offset / blen + tmpix.getColumnIndex() : tmpix.getColumnIndex();
                ix1.setIndexes(rix, cix);
                data.set(ix1, in2.getValue());
            } else // general case: split and forward
            {
                IndexedMatrixValue data1 = cachedValues.holdPlace(output, valueClass);
                MatrixIndexes ix1 = data1.getIndexes();
                // always block
                MatrixBlock tmpvalNew = (MatrixBlock) data1.getValue();
                if (_cbind) {
                    // first half
                    int cix1 = (int) (_offset / blen + tmpix.getColumnIndex());
                    int cols1 = Math.min(blen, (int) (_len - (long) (cix1 - 1) * blen));
                    ix1.setIndexes(tmpix.getRowIndex(), cix1);
                    tmpvalNew.reset(tmpval.getNumRows(), cols1);
                    tmpvalNew.copy(0, tmpval.getNumRows() - 1, (int) ((_offset + 1) % blen) - 1, cols1 - 1, tmpval.slice(0, tmpval.getNumRows() - 1, 0, (int) (cols1 - ((_offset) % blen) - 1), new MatrixBlock()), true);
                    data1.getIndexes().setIndexes(ix1);
                    if (cols1 - ((_offset) % blen) < tmpval.getNumColumns()) {
                        // second half (if required)
                        IndexedMatrixValue data2 = cachedValues.holdPlace(output, valueClass);
                        MatrixIndexes ix2 = data2.getIndexes();
                        // always block
                        MatrixBlock tmpvalNew2 = (MatrixBlock) data2.getValue();
                        int cix2 = (int) (_offset / blen + 1 + tmpix.getColumnIndex());
                        int cols2 = Math.min(blen, (int) (_len - (long) (cix2 - 1) * blen));
                        ix2.setIndexes(tmpix.getRowIndex(), cix2);
                        tmpvalNew2.reset(tmpval.getNumRows(), cols2);
                        tmpvalNew2.copy(0, tmpval.getNumRows() - 1, 0, cols2 - 1, tmpval.slice(0, tmpval.getNumRows() - 1, (int) (cols1 - ((_offset) % blen)), tmpval.getNumColumns() - 1, new MatrixBlock()), true);
                        data2.getIndexes().setIndexes(ix2);
                    }
                } else // rbind
                {
                    // first half
                    int rix1 = (int) (_offset / blen + tmpix.getRowIndex());
                    int rows1 = Math.min(blen, (int) (_len - (long) (rix1 - 1) * blen));
                    ix1.setIndexes(rix1, tmpix.getColumnIndex());
                    tmpvalNew.reset(rows1, tmpval.getNumColumns());
                    tmpvalNew.copy((int) ((_offset + 1) % blen) - 1, rows1 - 1, 0, tmpval.getNumColumns() - 1, tmpval.slice(0, (int) (rows1 - ((_offset) % blen) - 1), 0, tmpval.getNumColumns() - 1, new MatrixBlock()), true);
                    data1.getIndexes().setIndexes(ix1);
                    if (rows1 - ((_offset) % blen) < tmpval.getNumRows()) {
                        // second half (if required)
                        IndexedMatrixValue data2 = cachedValues.holdPlace(output, valueClass);
                        MatrixIndexes ix2 = data2.getIndexes();
                        // always block
                        MatrixBlock tmpvalNew2 = (MatrixBlock) data2.getValue();
                        int rix2 = (int) (_offset / blen + 1 + tmpix.getRowIndex());
                        int rows2 = Math.min(blen, (int) (_len - (long) (rix2 - 1) * blen));
                        ix2.setIndexes(rix2, tmpix.getColumnIndex());
                        tmpvalNew2.reset(rows2, tmpval.getNumColumns());
                        tmpvalNew2.copy(0, rows2 - 1, 0, tmpval.getNumColumns() - 1, tmpval.slice((int) (rows1 - ((_offset) % blen)), tmpval.getNumRows() - 1, 0, tmpval.getNumColumns() - 1, new MatrixBlock()), true);
                        data2.getIndexes().setIndexes(ix2);
                    }
                }
            }
        }
}
Also used : MatrixBlock(org.apache.sysml.runtime.matrix.data.MatrixBlock) MatrixIndexes(org.apache.sysml.runtime.matrix.data.MatrixIndexes) IndexedMatrixValue(org.apache.sysml.runtime.matrix.mapred.IndexedMatrixValue)

Example 4 with IndexedMatrixValue

use of org.apache.sysml.runtime.matrix.mapred.IndexedMatrixValue in project incubator-systemml by apache.

the class BinaryMInstruction method processInstruction.

@Override
public void processInstruction(Class<? extends MatrixValue> valueClass, CachedValueMap cachedValues, IndexedMatrixValue tempValue, IndexedMatrixValue zeroInput, int blockRowFactor, int blockColFactor) {
    ArrayList<IndexedMatrixValue> blkList = cachedValues.get(input1);
    if (blkList == null)
        return;
    for (IndexedMatrixValue in1 : blkList) {
        // allocate space for the output value
        // try to avoid coping as much as possible
        IndexedMatrixValue out;
        if ((output != input1 && output != input2))
            out = cachedValues.holdPlace(output, valueClass);
        else
            out = tempValue;
        // get second
        DistributedCacheInput dcInput = MRBaseForCommonInstructions.dcValues.get(input2);
        IndexedMatrixValue in2 = null;
        if (_vectorType == VectorType.COL_VECTOR)
            in2 = dcInput.getDataBlock((int) in1.getIndexes().getRowIndex(), 1);
        else
            // _vectorType == VectorType.ROW_VECTOR
            in2 = dcInput.getDataBlock(1, (int) in1.getIndexes().getColumnIndex());
        // process instruction
        out.getIndexes().setIndexes(in1.getIndexes());
        OperationsOnMatrixValues.performBinaryIgnoreIndexes(in1.getValue(), in2.getValue(), out.getValue(), ((BinaryOperator) optr));
        // put the output value in the cache
        if (out == tempValue)
            cachedValues.add(output, out);
    }
}
Also used : DistributedCacheInput(org.apache.sysml.runtime.matrix.mapred.DistributedCacheInput) BinaryOperator(org.apache.sysml.runtime.matrix.operators.BinaryOperator) IndexedMatrixValue(org.apache.sysml.runtime.matrix.mapred.IndexedMatrixValue)

Example 5 with IndexedMatrixValue

use of org.apache.sysml.runtime.matrix.mapred.IndexedMatrixValue in project incubator-systemml by apache.

the class QuaternaryInstruction method processInstruction.

@Override
public void processInstruction(Class<? extends MatrixValue> valueClass, CachedValueMap cachedValues, IndexedMatrixValue tempValue, IndexedMatrixValue zeroInput, int blockRowFactor, int blockColFactor) {
    QuaternaryOperator qop = (QuaternaryOperator) optr;
    ArrayList<IndexedMatrixValue> blkList = cachedValues.get(_input1);
    if (blkList != null)
        for (IndexedMatrixValue imv : blkList) {
            // Step 1: prepare inputs and output
            if (imv == null)
                continue;
            MatrixIndexes inIx = imv.getIndexes();
            MatrixBlock inVal = (MatrixBlock) imv.getValue();
            // allocate space for the output value
            IndexedMatrixValue iout = null;
            if (output == _input1)
                iout = tempValue;
            else
                iout = cachedValues.holdPlace(output, valueClass);
            MatrixIndexes outIx = iout.getIndexes();
            MatrixValue outVal = iout.getValue();
            // Step 2: get remaining inputs: Wij, Ui, Vj
            MatrixBlock Xij = inVal;
            // get Wij if existing (null of WeightsType.NONE or WSigmoid any type)
            IndexedMatrixValue iWij = (_input4 != -1) ? cachedValues.getFirst(_input4) : null;
            MatrixValue Wij = (iWij != null) ? iWij.getValue() : null;
            if (null == Wij && qop.hasFourInputs()) {
                MatrixBlock mb = new MatrixBlock(1, 1, false);
                String[] parts = InstructionUtils.getInstructionParts(instString);
                mb.quickSetValue(0, 0, Double.valueOf(parts[4]));
                Wij = mb;
            }
            // get Ui and Vj, potentially through distributed cache
            MatrixValue Ui = // U
            (!_cacheU) ? // U
            cachedValues.getFirst(_input2).getValue() : MRBaseForCommonInstructions.dcValues.get(_input2).getDataBlock((int) inIx.getRowIndex(), 1).getValue();
            MatrixValue Vj = // t(V)
            (!_cacheV) ? // t(V)
            cachedValues.getFirst(_input3).getValue() : MRBaseForCommonInstructions.dcValues.get(_input3).getDataBlock((int) inIx.getColumnIndex(), 1).getValue();
            // handle special input case: //V through shuffle -> t(V)
            if (Ui.getNumColumns() != Vj.getNumColumns()) {
                Vj = LibMatrixReorg.reorg((MatrixBlock) Vj, new MatrixBlock(Vj.getNumColumns(), Vj.getNumRows(), Vj.isInSparseFormat()), new ReorgOperator(SwapIndex.getSwapIndexFnObject()));
            }
            // Step 3: process instruction
            Xij.quaternaryOperations(qop, (MatrixBlock) Ui, (MatrixBlock) Vj, (MatrixBlock) Wij, (MatrixBlock) outVal);
            if (qop.wtype1 != null || qop.wtype4 != null)
                // wsloss
                outIx.setIndexes(1, 1);
            else if (qop.wtype2 != null || qop.wtype5 != null || qop.wtype3 != null && qop.wtype3.isBasic())
                // wsigmoid/wdivmm-basic
                outIx.setIndexes(inIx);
            else {
                // wdivmm
                boolean left = qop.wtype3.isLeft();
                outIx.setIndexes(left ? inIx.getColumnIndex() : inIx.getRowIndex(), 1);
            }
            // put the output value in the cache
            if (iout == tempValue)
                cachedValues.add(output, iout);
        }
}
Also used : QuaternaryOperator(org.apache.sysml.runtime.matrix.operators.QuaternaryOperator) 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) ReorgOperator(org.apache.sysml.runtime.matrix.operators.ReorgOperator) IndexedMatrixValue(org.apache.sysml.runtime.matrix.mapred.IndexedMatrixValue)

Aggregations

IndexedMatrixValue (org.apache.sysml.runtime.matrix.mapred.IndexedMatrixValue)64 MatrixIndexes (org.apache.sysml.runtime.matrix.data.MatrixIndexes)32 MatrixBlock (org.apache.sysml.runtime.matrix.data.MatrixBlock)31 MatrixValue (org.apache.sysml.runtime.matrix.data.MatrixValue)16 ArrayList (java.util.ArrayList)14 DistributedCacheInput (org.apache.sysml.runtime.matrix.mapred.DistributedCacheInput)12 DMLRuntimeException (org.apache.sysml.runtime.DMLRuntimeException)9 CompressedMatrixBlock (org.apache.sysml.runtime.compress.CompressedMatrixBlock)6 Path (org.apache.hadoop.fs.Path)4 AggregateBinaryOperator (org.apache.sysml.runtime.matrix.operators.AggregateBinaryOperator)4 BinaryOperator (org.apache.sysml.runtime.matrix.operators.BinaryOperator)4 ReorgOperator (org.apache.sysml.runtime.matrix.operators.ReorgOperator)4 CTableMap (org.apache.sysml.runtime.matrix.data.CTableMap)3 IOException (java.io.IOException)2 HashMap (java.util.HashMap)2 FileSystem (org.apache.hadoop.fs.FileSystem)2 SequenceFile (org.apache.hadoop.io.SequenceFile)2 JobConf (org.apache.hadoop.mapred.JobConf)2 SparkExecutionContext (org.apache.sysml.runtime.controlprogram.context.SparkExecutionContext)2 DiagIndex (org.apache.sysml.runtime.functionobjects.DiagIndex)2