Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.apache.sysds.runtime.matrix.operators.BinaryOperator;
import org.apache.sysds.runtime.matrix.operators.Operator;
import org.apache.sysds.runtime.matrix.operators.ScalarOperator;
import org.apache.sysds.runtime.ooc.store.CountingLiveness;
import org.apache.sysds.runtime.ooc.util.OOCInstructionUtils;

public class BinaryOOCInstruction extends ComputationOOCInstruction {
Expand Down Expand Up @@ -67,8 +68,8 @@ protected void processMatrixMatrixInstruction(ExecutionContext ec) {
OOCStream<IndexedMatrixValue> qOut = new SubscribableTaskQueue<>();
ec.getMatrixObject(output).setStreamHandle(qOut);

final boolean known1 = (m1.getNumRows() >= 0 && m1.getNumColumns() >= 0);
final boolean known2 = (m2.getNumRows() >= 0 && m2.getNumColumns() >= 0);
final boolean known1 = m1.getNumRows() >= 0 && m1.getNumColumns() >= 0 && m1.getBlocksize() > 0;
final boolean known2 = m2.getNumRows() >= 0 && m2.getNumColumns() >= 0 && m2.getBlocksize() > 0;

// If dimensions are unknown, we cannot safely detect broadcasting.
// Fall back to strict key-based join and let downstream operators validate as needed.
Expand All @@ -94,36 +95,28 @@ protected void processMatrixMatrixInstruction(ExecutionContext ec) {
boolean isRowBroadcast = m1.getNumRows() > 1 && m2.getNumRows() == 1;

if (isColBroadcast && !isRowBroadcast) {
OOCStream<IndexedMatrixValue> qIn1 = m1.getStreamHandle();
OOCStream<IndexedMatrixValue> qIn2 = m2.getStreamHandle();
final long maxProcessesPerBroadcast = (m1.getNumColumns() + m1.getBlocksize() - 1) / m1.getBlocksize();

broadcastJoinOOC(qIn1, qIn2, qOut, (tmp1, b) -> {
IndexedMatrixValue tmpOut = new IndexedMatrixValue();
tmpOut.set(tmp1.getIndexes(),
tmp1.getValue().binaryOperations((BinaryOperator)_optr, b.getValue().getValue(), tmpOut.getValue()));

if (b.incrProcessCtrAndGet() >= maxProcessesPerBroadcast)
b.release();

return tmpOut;
}, tmp -> tmp.getIndexes().getRowIndex());
int broadcastBlocks = Math.toIntExact(m2.getDataCharacteristics().getNumRowBlocks());
int usesPerBlock = Math.toIntExact(m1.getDataCharacteristics().getNumColBlocks());
OOCInstructionUtils.indexedBroadcastMap(m1.getStreamable(), m2.getStreamable(), qOut,
tmp -> Math.toIntExact(tmp.getIndexes().getRowIndex() - 1),
() -> new CountingLiveness(broadcastBlocks, usesPerBlock), (tmp, broadcast) -> {
IndexedMatrixValue tmpOut = new IndexedMatrixValue();
tmpOut.set(tmp.getIndexes(), tmp.getValue().binaryOperations((BinaryOperator) _optr,
broadcast.getValue(), tmpOut.getValue()));
return tmpOut;
}, getContext());
}
else if (isRowBroadcast && !isColBroadcast) {
OOCStream<IndexedMatrixValue> qIn1 = m1.getStreamHandle();
OOCStream<IndexedMatrixValue> qIn2 = m2.getStreamHandle();
final long maxProcessesPerBroadcast = (m1.getNumRows() + m1.getBlocksize() - 1) / m1.getBlocksize();

broadcastJoinOOC(qIn1, qIn2, qOut, (tmp1, b) -> {
IndexedMatrixValue tmpOut = new IndexedMatrixValue();
tmpOut.set(tmp1.getIndexes(),
tmp1.getValue().binaryOperations((BinaryOperator)_optr, b.getValue().getValue(), tmpOut.getValue()));

if (b.incrProcessCtrAndGet() >= maxProcessesPerBroadcast)
b.release();

return tmpOut;
}, tmp -> tmp.getIndexes().getColumnIndex());
int broadcastBlocks = Math.toIntExact(m2.getDataCharacteristics().getNumColBlocks());
int usesPerBlock = Math.toIntExact(m1.getDataCharacteristics().getNumRowBlocks());
OOCInstructionUtils.indexedBroadcastMap(m1.getStreamable(), m2.getStreamable(), qOut,
tmp -> Math.toIntExact(tmp.getIndexes().getColumnIndex() - 1),
() -> new CountingLiveness(broadcastBlocks, usesPerBlock), (tmp, broadcast) -> {
IndexedMatrixValue tmpOut = new IndexedMatrixValue();
tmpOut.set(tmp.getIndexes(), tmp.getValue().binaryOperations((BinaryOperator) _optr,
broadcast.getValue(), tmpOut.getValue()));
return tmpOut;
}, getContext());
}
else {
if (m1.getNumColumns() != m2.getNumColumns() || m1.getNumRows() != m2.getNumRows())
Expand All @@ -144,15 +137,9 @@ protected void processScalarMatrixInstruction(ExecutionContext ec) {

//create thread and process binary operation
MatrixObject min = ec.getMatrixObject(input1.isMatrix() ? input1 : input2);
OOCStream<IndexedMatrixValue> qIn = min.getStreamHandle();
OOCStream<IndexedMatrixValue> qOut = createWritableStream();
ec.getMatrixObject(output).setStreamHandle(qOut);

mapOOC(qIn, qOut, tmp -> {
IndexedMatrixValue tmpOut = new IndexedMatrixValue();
tmpOut.set(tmp.getIndexes(),
tmp.getValue().scalarOperations(sc_op, new MatrixBlock()));
return tmpOut;
});
OOCInstructionUtils.equiMapBlock(min.getStreamable(), qOut,
block -> block.scalarOperations(sc_op, new MatrixBlock()), getContext());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,8 @@
import org.apache.sysds.runtime.instructions.spark.data.IndexedMatrixValue;
import org.apache.sysds.runtime.matrix.data.LibMatrixDatagen;
import org.apache.sysds.runtime.matrix.data.MatrixBlock;
import org.apache.sysds.runtime.matrix.data.MatrixIndexes;
import org.apache.sysds.runtime.matrix.data.RandomMatrixGenerator;
import org.apache.sysds.runtime.matrix.operators.UnaryOperator;
import org.apache.sysds.runtime.ooc.stream.StreamContext;
import org.apache.sysds.runtime.ooc.util.OOCInstructionUtils;
import org.apache.sysds.runtime.util.UtilFunctions;

Expand Down Expand Up @@ -251,34 +249,22 @@ else if(method == Types.OpOpDG.SEQ) {

final int maxK = (int) UtilFunctions.getSeqLength(lfrom, lto, lincr);
final double finalLincr = lincr;
ec.getDataCharacteristics(output.getName()).set(maxK, 1, blen, -1);

OOCInstructionUtils.submitOOCTask(() -> {
int k = 0;
double curFrom = lfrom;
double curTo;
MatrixBlock mb;

while (k < maxK) {
long desiredLen = Math.min(blen, maxK - k);
curTo = curFrom + (desiredLen - 1) * finalLincr;
long actualLen = UtilFunctions.getSeqLength(curFrom, curTo, finalLincr);

if (actualLen != desiredLen) {
// Then we add / subtract a small correction term
curTo += (actualLen < desiredLen) ? finalLincr / 2 : -finalLincr / 2;

if (UtilFunctions.getSeqLength(curFrom, curTo, finalLincr) != desiredLen)
throw new DMLRuntimeException("OOC seq could not construct the right number of elements.");
}

mb = MatrixBlock.seqOperations(curFrom, curTo, finalLincr);
qOut.enqueue(new IndexedMatrixValue(new MatrixIndexes(1 + k / blen, 1), mb));
curFrom = mb.get(mb.getNumRows() - 1, 0) + finalLincr;
k += blen;
OOCInstructionUtils.dataGen(qOut, idx -> {
long offset = (idx.getRowIndex() - 1) * blen;
long desiredLen = Math.min(blen, maxK - offset);
double curFrom = lfrom + offset * finalLincr;
double curTo = curFrom + (desiredLen - 1) * finalLincr;
long actualLen = UtilFunctions.getSeqLength(curFrom, curTo, finalLincr);

if(actualLen != desiredLen) {
curTo += actualLen < desiredLen ? finalLincr / 2 : -finalLincr / 2;
if(UtilFunctions.getSeqLength(curFrom, curTo, finalLincr) != desiredLen)
throw new DMLRuntimeException("OOC seq could not construct the right number of elements.");
}

qOut.closeInput();
}, new StreamContext(_callerId, getExtendedOpcode()).addOutStream(qOut));
return MatrixBlock.seqOperations(curFrom, curTo, finalLincr);
}, getContext());
}
else
throw new NotImplementedException();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import org.apache.sysds.runtime.matrix.data.MatrixBlock;
import org.apache.sysds.runtime.matrix.operators.Operator;
import org.apache.sysds.runtime.matrix.operators.SimpleOperator;
import org.apache.sysds.runtime.ooc.util.OOCInstructionUtils;
import org.apache.sysds.runtime.util.UtilFunctions;

import java.util.ArrayList;
Expand Down Expand Up @@ -92,13 +93,13 @@ public void processInstruction(ExecutionContext ec) {
throw new NotImplementedException();
} else{
MatrixObject targetObj = ec.getMatrixObject(params.get("target"));
OOCStream<IndexedMatrixValue> qIn = targetObj.getStreamHandle();
OOCStream<IndexedMatrixValue> qOut = createWritableStream();

double pattern = Double.parseDouble(params.get("pattern"));
double replacement = Double.parseDouble(params.get("replacement"));

mapOOC(qIn, qOut, tmp -> new IndexedMatrixValue(tmp.getIndexes(), tmp.getValue().replaceOperations(new MatrixBlock(), pattern, replacement)));
OOCInstructionUtils.equiMapBlock(targetObj.getStreamable(), qOut,
block -> block.replaceOperations(new MatrixBlock(), pattern, replacement), getContext());

ec.getMatrixObject(output).setStreamHandle(qOut);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import org.apache.sysds.runtime.matrix.data.MatrixBlock;
import org.apache.sysds.runtime.matrix.operators.Operator;
import org.apache.sysds.runtime.matrix.operators.TernaryOperator;
import org.apache.sysds.runtime.ooc.util.OOCInstructionUtils;

public class TernaryOOCInstruction extends ComputationOOCInstruction {

Expand Down Expand Up @@ -106,43 +107,47 @@ private void processSingleMatrixInstruction(ExecutionContext ec, int matrixPos)
MatrixBlock s2 = input2.isMatrix() ? null : getScalarInputBlock(ec, input2);
MatrixBlock s3 = input3.isMatrix() ? null : getScalarInputBlock(ec, input3);

OOCStream<IndexedMatrixValue> qIn = mo.getStreamHandle();
OOCStream<IndexedMatrixValue> qOut = createWritableStream();
ec.getMatrixObject(output).setStreamHandle(qOut);

mapOOC(qIn, qOut, tmp -> {
IndexedMatrixValue outVal = new IndexedMatrixValue();
MatrixBlock op1 = resolveOperandBlock(1, tmp, null, matrixPos, -1, s1, s2, s3);
MatrixBlock op2 = resolveOperandBlock(2, tmp, null, matrixPos, -1, s1, s2, s3);
MatrixBlock op3 = resolveOperandBlock(3, tmp, null, matrixPos, -1, s1, s2, s3);
outVal.set(tmp.getIndexes(),
op1.ternaryOperations((TernaryOperator)_optr, op2, op3, new MatrixBlock()));
return outVal;
});
OOCInstructionUtils.equiMapBlock(mo.getStreamable(), qOut, block -> {
MatrixBlock op1 = resolveOperandBlock(1, block, null, matrixPos, -1, s1, s2, s3);
MatrixBlock op2 = resolveOperandBlock(2, block, null, matrixPos, -1, s1, s2, s3);
MatrixBlock op3 = resolveOperandBlock(3, block, null, matrixPos, -1, s1, s2, s3);
return op1.ternaryOperations((TernaryOperator) _optr, op2, op3, new MatrixBlock());
}, getContext());
}

private void processTwoMatrixInstruction(ExecutionContext ec, int leftPos, int rightPos) {
MatrixObject left = getMatrixObject(ec, leftPos);
MatrixObject right = getMatrixObject(ec, rightPos);
OOCStream<IndexedMatrixValue> leftStream = left.getStreamHandle();
OOCStream<IndexedMatrixValue> rightStream = right.getStreamHandle();

MatrixBlock s1 = input1.isMatrix() ? null : getScalarInputBlock(ec, input1);
MatrixBlock s2 = input2.isMatrix() ? null : getScalarInputBlock(ec, input2);
MatrixBlock s3 = input3.isMatrix() ? null : getScalarInputBlock(ec, input3);

OOCStream<IndexedMatrixValue> qOut = createWritableStream();
ec.getMatrixObject(output).setStreamHandle(qOut);

joinOOC(leftStream, rightStream, qOut, (l, r) -> {
IndexedMatrixValue outVal = new IndexedMatrixValue();
MatrixBlock op1 = resolveOperandBlock(1, l, r, leftPos, rightPos, s1, s2, s3);
MatrixBlock op2 = resolveOperandBlock(2, l, r, leftPos, rightPos, s1, s2, s3);
MatrixBlock op3 = resolveOperandBlock(3, l, r, leftPos, rightPos, s1, s2, s3);
outVal.set(l.getIndexes(),
op1.ternaryOperations((TernaryOperator)_optr, op2, op3, new MatrixBlock()));
return outVal;
}, IndexedMatrixValue::getIndexes);
if(left.getDataCharacteristics().dimsKnown() && right.getDataCharacteristics().dimsKnown()) {
OOCInstructionUtils.equiJoin(left.getStreamable(), right.getStreamable(), qOut, (l, r) -> {
MatrixBlock op1 = resolveOperandBlock(1, l, r, leftPos, rightPos, s1, s2, s3);
MatrixBlock op2 = resolveOperandBlock(2, l, r, leftPos, rightPos, s1, s2, s3);
MatrixBlock op3 = resolveOperandBlock(3, l, r, leftPos, rightPos, s1, s2, s3);
return op1.ternaryOperations((TernaryOperator) _optr, op2, op3, new MatrixBlock());
}, getContext());
}
else {
OOCStream<IndexedMatrixValue> leftStream = left.getStreamHandle();
OOCStream<IndexedMatrixValue> rightStream = right.getStreamHandle();
joinOOC(leftStream, rightStream, qOut, (l, r) -> {
IndexedMatrixValue outVal = new IndexedMatrixValue();
MatrixBlock op1 = resolveOperandBlock(1, l, r, leftPos, rightPos, s1, s2, s3);
MatrixBlock op2 = resolveOperandBlock(2, l, r, leftPos, rightPos, s1, s2, s3);
MatrixBlock op3 = resolveOperandBlock(3, l, r, leftPos, rightPos, s1, s2, s3);
outVal.set(l.getIndexes(), op1.ternaryOperations((TernaryOperator) _optr, op2, op3, new MatrixBlock()));
return outVal;
}, IndexedMatrixValue::getIndexes);
}
}

private void processThreeMatrixInstruction(ExecutionContext ec) {
Expand Down Expand Up @@ -185,10 +190,16 @@ private MatrixBlock getScalarInputBlock(ExecutionContext ec, CPOperand operand)

private MatrixBlock resolveOperandBlock(int operandPos, IndexedMatrixValue left, IndexedMatrixValue right,
int leftPos, int rightPos, MatrixBlock s1, MatrixBlock s2, MatrixBlock s3) {
return resolveOperandBlock(operandPos, left == null ? null : (MatrixBlock) left.getValue(),
right == null ? null : (MatrixBlock) right.getValue(), leftPos, rightPos, s1, s2, s3);
}

private MatrixBlock resolveOperandBlock(int operandPos, MatrixBlock left, MatrixBlock right, int leftPos,
int rightPos, MatrixBlock s1, MatrixBlock s2, MatrixBlock s3) {
if(operandPos == leftPos && left != null)
return (MatrixBlock) left.getValue();
return left;
if(operandPos == rightPos && right != null)
return (MatrixBlock) right.getValue();
return right;

if(operandPos == 1)
return s1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ public void testSeq2() {
runSeqTest(0, 15.9, 0.01);
}

@Test
public void testDescendingSeq() {
runSeqTest(10, 0, -0.1);
}

private void runSeqTest(double from, double to, double incr) {
Types.ExecMode platformOld = setExecMode(Types.ExecMode.SINGLE_NODE);

Expand Down
Loading