|
| 1 | +package org.apache.sysds.runtime.instructions.ooc; |
| 2 | + |
| 3 | +import org.apache.commons.lang3.NotImplementedException; |
| 4 | +import org.apache.sysds.common.Opcodes; |
| 5 | +import org.apache.sysds.common.Types; |
| 6 | +import org.apache.sysds.runtime.DMLRuntimeException; |
| 7 | +import org.apache.sysds.runtime.controlprogram.context.ExecutionContext; |
| 8 | +import org.apache.sysds.runtime.instructions.InstructionUtils; |
| 9 | +import org.apache.sysds.runtime.instructions.cp.CPOperand; |
| 10 | +import org.apache.sysds.runtime.instructions.spark.data.IndexedMatrixValue; |
| 11 | +import org.apache.sysds.runtime.matrix.data.LibMatrixDatagen; |
| 12 | +import org.apache.sysds.runtime.matrix.data.MatrixBlock; |
| 13 | +import org.apache.sysds.runtime.matrix.data.MatrixIndexes; |
| 14 | +import org.apache.sysds.runtime.matrix.operators.UnaryOperator; |
| 15 | +import org.apache.sysds.runtime.util.UtilFunctions; |
| 16 | + |
| 17 | +public class DataGenOOCInstruction extends UnaryOOCInstruction { |
| 18 | + |
| 19 | + private final int blen; |
| 20 | + private Types.OpOpDG method; |
| 21 | + |
| 22 | + // sequence specific attributes |
| 23 | + private final CPOperand seq_from, seq_to, seq_incr; |
| 24 | + |
| 25 | + public DataGenOOCInstruction(UnaryOperator op, Types.OpOpDG mthd, CPOperand in, CPOperand out, int blen, CPOperand seqFrom, |
| 26 | + CPOperand seqTo, CPOperand seqIncr, String opcode, String istr) { |
| 27 | + super(OOCType.Rand, op, in, out, opcode, istr); |
| 28 | + this.blen = blen; |
| 29 | + this.method = mthd; |
| 30 | + this.seq_from = seqFrom; |
| 31 | + this.seq_to = seqTo; |
| 32 | + this.seq_incr = seqIncr; |
| 33 | + } |
| 34 | + |
| 35 | + public static DataGenOOCInstruction parseInstruction(String str) { |
| 36 | + Types.OpOpDG method = null; |
| 37 | + String[] s = InstructionUtils.getInstructionPartsWithValueType(str); |
| 38 | + String opcode = s[0]; |
| 39 | + |
| 40 | + if(opcode.equalsIgnoreCase(Opcodes.SEQUENCE.toString())) { |
| 41 | + method = Types.OpOpDG.SEQ; |
| 42 | + // 8 operands: rows, cols, blen, from, to, incr, outvar |
| 43 | + InstructionUtils.checkNumFields(s, 7); |
| 44 | + } |
| 45 | + else |
| 46 | + throw new NotImplementedException(); // TODO |
| 47 | + |
| 48 | + CPOperand out = new CPOperand(s[s.length - 1]); |
| 49 | + UnaryOperator op = null; |
| 50 | + |
| 51 | + if(method == Types.OpOpDG.SEQ) { |
| 52 | + int blen = Integer.parseInt(s[3]); |
| 53 | + CPOperand from = new CPOperand(s[4]); |
| 54 | + CPOperand to = new CPOperand(s[5]); |
| 55 | + CPOperand incr = new CPOperand(s[6]); |
| 56 | + |
| 57 | + return new DataGenOOCInstruction(op, method, null, out, blen, from, to, incr, opcode, str); |
| 58 | + } |
| 59 | + else |
| 60 | + throw new NotImplementedException(); |
| 61 | + } |
| 62 | + |
| 63 | + @Override |
| 64 | + public void processInstruction(ExecutionContext ec) { |
| 65 | + final OOCStream<IndexedMatrixValue> qOut = createWritableStream(); |
| 66 | + |
| 67 | + // process specific datagen operator |
| 68 | + if(method == Types.OpOpDG.SEQ) { |
| 69 | + double lfrom = ec.getScalarInput(seq_from).getDoubleValue(); |
| 70 | + double lto = ec.getScalarInput(seq_to).getDoubleValue(); |
| 71 | + double lincr = ec.getScalarInput(seq_incr).getDoubleValue(); |
| 72 | + |
| 73 | + // handle default 1 to -1 for special case of from>to |
| 74 | + lincr = LibMatrixDatagen.updateSeqIncr(lfrom, lto, lincr); |
| 75 | + |
| 76 | + if(LOG.isTraceEnabled()) |
| 77 | + LOG.trace( |
| 78 | + "Process DataGenOOCInstruction seq with seqFrom=" + lfrom + ", seqTo=" + lto + ", seqIncr" + lincr); |
| 79 | + |
| 80 | + final int maxK = (int) UtilFunctions.getSeqLength(lfrom, lto, lincr); |
| 81 | + final double finalLincr = lincr; |
| 82 | + |
| 83 | + |
| 84 | + submitOOCTask(() -> { |
| 85 | + int k = 0; |
| 86 | + double curFrom = lfrom; |
| 87 | + double curTo; |
| 88 | + MatrixBlock mb; |
| 89 | + |
| 90 | + while (k < maxK) { |
| 91 | + long desiredLen = Math.min(blen, maxK - k); |
| 92 | + curTo = curFrom + (desiredLen - 1) * finalLincr; |
| 93 | + long actualLen = UtilFunctions.getSeqLength(curFrom, curTo, finalLincr); |
| 94 | + |
| 95 | + if (actualLen != desiredLen) { |
| 96 | + // Then we add / subtract a small correction term |
| 97 | + curTo += (actualLen < desiredLen) ? finalLincr / 2 : -finalLincr / 2; |
| 98 | + |
| 99 | + if (UtilFunctions.getSeqLength(curFrom, curTo, finalLincr) != desiredLen) |
| 100 | + throw new DMLRuntimeException("OOC seq could not construct the right number of elements."); |
| 101 | + } |
| 102 | + |
| 103 | + mb = MatrixBlock.seqOperations(curFrom, curTo, finalLincr); |
| 104 | + qOut.enqueue(new IndexedMatrixValue(new MatrixIndexes(1 + k / blen, 1), mb)); |
| 105 | + curFrom = mb.get(mb.getNumRows() - 1, 0) + finalLincr; |
| 106 | + k += blen; |
| 107 | + } |
| 108 | + |
| 109 | + qOut.closeInput(); |
| 110 | + }, qOut); |
| 111 | + } |
| 112 | + else |
| 113 | + throw new NotImplementedException(); |
| 114 | + |
| 115 | + ec.getMatrixObject(output).setStreamHandle(qOut); |
| 116 | + } |
| 117 | +} |
0 commit comments