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 @@ -28,7 +28,6 @@
import org.apache.sysds.runtime.DMLRuntimeException;
import org.apache.sysds.runtime.controlprogram.caching.MatrixObject;
import org.apache.sysds.runtime.controlprogram.context.ExecutionContext;
import org.apache.sysds.runtime.controlprogram.parfor.LocalTaskQueue;
import org.apache.sysds.runtime.functionobjects.Multiply;
import org.apache.sysds.runtime.functionobjects.Plus;
import org.apache.sysds.runtime.instructions.InstructionUtils;
Expand All @@ -41,6 +40,7 @@
import org.apache.sysds.runtime.matrix.operators.AggregateOperator;
import org.apache.sysds.runtime.matrix.operators.BinaryOperator;
import org.apache.sysds.runtime.matrix.operators.Operator;
import org.apache.sysds.runtime.ooc.util.OOCInstructionUtils;

public class TSMMOOCInstruction extends ComputationOOCInstruction {
private final MMTSJType _type;
Expand Down Expand Up @@ -109,24 +109,15 @@ public void processInstruction(ExecutionContext ec) {
}

private void processSingleOutputTileInstruction(ExecutionContext ec, MatrixObject min) {
OOCStream<IndexedMatrixValue> qIn = min.getStreamHandle();
OOCStream<IndexedMatrixValue> out = createWritableStream();
ec.getMatrixObject(output).setStreamHandle(out);
BinaryOperator plus = InstructionUtils.parseBinaryOperator(Opcodes.PLUS.toString());
MatrixBlock resultBlock = null;

OOCStream<MatrixBlock> tmpStream = createWritableStream();
mapOOC(qIn, tmpStream,
tmp -> ((MatrixBlock) tmp.getValue())
.transposeSelfMatrixMultOperations(new MatrixBlock(), _type));

MatrixBlock tmp;
while((tmp = tmpStream.dequeue()) != LocalTaskQueue.NO_MORE_TASKS) {
if(resultBlock == null)
resultBlock = tmp;
else
resultBlock.binaryOperationsInPlace(plus, tmp);
}

ec.setMatrixOutput(output.getName(), resultBlock);
OOCInstructionUtils.reduce(min.getStreamable(), out,
value -> new IndexedMatrixValue(new MatrixIndexes(1, 1),
((MatrixBlock) value.getValue()).transposeSelfMatrixMultOperations(new MatrixBlock(), _type)),
(left, right) -> new IndexedMatrixValue(new MatrixIndexes(1, 1),
((MatrixBlock) left.getValue()).binaryOperationsInPlace(plus, right.getValue())),
value -> ((MatrixBlock) value.getValue()).getExactSerializedSize(), getContext());
}

private long getJoinIndex(IndexedMatrixValue value) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,8 @@ public static boolean canClaimMemory() {
return getCache().isWithinLimits() && OOCInstruction.getComputeInFlight() <= OOCInstruction.getComputeBackpressureThreshold();
}

public static OOCCacheScheduler.HandoverHandle handover(BlockKey key, InMemoryQueueCallback callback) {
public static OOCCacheScheduler.HandoverHandle handover(BlockKey key,
InMemoryQueueCallback<IndexedMatrixValue> callback) {
return getCache().handover(key, callback);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ interface HandoverHandle {
OOCStream.QueueCallback<IndexedMatrixValue> reclaim();
}

HandoverHandle handover(BlockKey key, InMemoryQueueCallback callback);
HandoverHandle handover(BlockKey key, InMemoryQueueCallback<IndexedMatrixValue> callback);

/**
* Places a new source-backed block in the cache and registers the location with the IO handler. The entry is
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ public BlockEntry putAndPin(BlockKey key, Object data, long size) {
}

@Override
public HandoverHandle handover(BlockKey key, InMemoryQueueCallback callback) {
public HandoverHandle handover(BlockKey key, InMemoryQueueCallback<IndexedMatrixValue> callback) {
if(!this._running)
throw new IllegalStateException("Cache scheduler has been shut down.");
PendingHandover handover = new PendingHandover(key, callback);
Expand Down Expand Up @@ -1085,7 +1085,7 @@ private void registerWaiter(BlockKey key, DeferredReadRequest request, int index
}

private boolean commitHandover(PendingHandover pending) {
InMemoryQueueCallback callback = pending.takeForCommit();
InMemoryQueueCallback<IndexedMatrixValue> callback = pending.takeForCommit();
if(callback == null)
return false;
try {
Expand Down Expand Up @@ -1135,12 +1135,12 @@ private DeferredReadWaiter(DeferredReadRequest request, int index) {
private static class PendingHandover implements HandoverHandle {
private final BlockKey _key;
private final CompletableFuture<Boolean> _completionFuture;
private InMemoryQueueCallback _callback;
private InMemoryQueueCallback<IndexedMatrixValue> _callback;
private boolean _committed;
private boolean _cancelled;
private boolean _committing;

private PendingHandover(BlockKey key, InMemoryQueueCallback callback) {
private PendingHandover(BlockKey key, InMemoryQueueCallback<IndexedMatrixValue> callback) {
_key = key;
_completionFuture = new CompletableFuture<>();
_callback = callback;
Expand Down Expand Up @@ -1180,11 +1180,11 @@ private synchronized boolean isCancelled() {
return _cancelled;
}

private synchronized InMemoryQueueCallback takeForCommit() {
private synchronized InMemoryQueueCallback<IndexedMatrixValue> takeForCommit() {
if(_committed || _cancelled || _committing)
return null;
_committing = true;
InMemoryQueueCallback callback = _callback;
InMemoryQueueCallback<IndexedMatrixValue> callback = _callback;
_callback = null;
return callback;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,12 @@ public CachedAllowance(MemoryBroker broker) {
_handoverSchedulingRequested = false;
}

public void handover(InMemoryQueueCallback callback, int index) {
public void handover(InMemoryQueueCallback<IndexedMatrixValue> callback, int index) {
if(callback == null)
throw new IllegalArgumentException("Cannot hand over null callback.");
callback.transferOwnershipBlocking(this);

InMemoryQueueCallback root = (InMemoryQueueCallback) callback.keepOpen();
InMemoryQueueCallback<IndexedMatrixValue> root = callback.keepOpen();
callback.close();
root.getHandle().attachCachedAllowance(this, index);

Expand All @@ -88,7 +88,7 @@ public OOCStream.QueueCallback<IndexedMatrixValue> tryGet(int index) {
while(true) {
BlockKey cacheKey = null;
OOCCacheScheduler.HandoverHandle handover = null;
InMemoryQueueCallback local = null;
InMemoryQueueCallback<IndexedMatrixValue> local = null;

synchronized(entry) {
if(entry._local != null && entry._handover == null)
Expand Down Expand Up @@ -124,7 +124,7 @@ else if(entry._cacheKey != null)
if(!future.isDone())
return null;
boolean committed = future.join();
InMemoryQueueCallback localToClose = null;
InMemoryQueueCallback<IndexedMatrixValue> localToClose = null;
synchronized(entry) {
if(entry._handover != handover)
continue;
Expand Down Expand Up @@ -171,8 +171,8 @@ public CompletableFuture<OOCStream.QueueCallback<IndexedMatrixValue>> get(int in
throw DMLRuntimeException.of(ex.getCause() == null ? ex : ex.getCause());
return committed == true;
}).thenCompose(committed -> {
InMemoryQueueCallback localToClose = null;
InMemoryQueueCallback local = null;
InMemoryQueueCallback<IndexedMatrixValue> localToClose = null;
InMemoryQueueCallback<IndexedMatrixValue> local = null;
BlockKey key;

synchronized(entry) {
Expand Down Expand Up @@ -215,7 +215,7 @@ public void clear(int index) {
while(true) {
OOCCacheScheduler.HandoverHandle handover = null;
BlockKey forgetKey = null;
InMemoryQueueCallback localToClose = null;
InMemoryQueueCallback<IndexedMatrixValue> localToClose = null;

synchronized(entry) {
if(entry._local != null && entry._handover == null) {
Expand Down Expand Up @@ -410,7 +410,7 @@ private long tryStartCacheHandover(SlotEntry entry) {
if(bytes <= 0)
return 0;

InMemoryQueueCallback retained = (InMemoryQueueCallback) entry._local.keepOpen();
InMemoryQueueCallback<IndexedMatrixValue> retained = entry._local.keepOpen();
try {
entry._cacheKey = new BlockKey(_streamId, _nextBlockId.getAndIncrement());
entry._handover = OOCCacheManager.handover(entry._cacheKey, retained);
Expand Down Expand Up @@ -448,7 +448,7 @@ private void finishPendingHandover(SlotEntry entry) {
onFinishedHandover(bytes);
}

private void closeRoot(InMemoryQueueCallback local) {
private void closeRoot(InMemoryQueueCallback<IndexedMatrixValue> local) {
local.getHandle().detachCachedAllowance();
local.close();
}
Expand Down Expand Up @@ -486,12 +486,12 @@ private void ensureCapacity(int index) {
}

private static final class SlotEntry {
private InMemoryQueueCallback _local;
private InMemoryQueueCallback<IndexedMatrixValue> _local;
private BlockKey _cacheKey;
private OOCCacheScheduler.HandoverHandle _handover;
private long _pendingBytes;

private SlotEntry(InMemoryQueueCallback local) {
private SlotEntry(InMemoryQueueCallback<IndexedMatrixValue> local) {
_local = local;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,36 +21,38 @@

import org.apache.sysds.runtime.DMLRuntimeException;
import org.apache.sysds.runtime.instructions.ooc.OOCStream;
import org.apache.sysds.runtime.instructions.spark.data.IndexedMatrixValue;

import java.util.concurrent.atomic.AtomicInteger;

public class InMemoryQueueCallback implements OOCStream.QueueCallback<IndexedMatrixValue> {
private CallbackHandle _handle;
public class InMemoryQueueCallback<T> implements OOCStream.QueueCallback<T> {
private CallbackHandle<T> _handle;
private boolean _closed;

public InMemoryQueueCallback(IndexedMatrixValue result, DMLRuntimeException failure, MemoryAllowance allow,
long reservedBytes) {
_handle = new CallbackHandle(result, failure, allow, reservedBytes);
public InMemoryQueueCallback(T result, DMLRuntimeException failure, MemoryAllowance allow, long reservedBytes) {
_handle = new CallbackHandle<>(result, failure, allow, reservedBytes);
_closed = false;
}

private InMemoryQueueCallback(CallbackHandle handle) {
public InMemoryQueueCallback(ManagedPayload<T> payload) {
this(payload.value(), null, payload.owner(), payload.bytes());
payload.transfer();
}

private InMemoryQueueCallback(CallbackHandle<T> handle) {
_handle = handle;
_closed = false;
}

@Override
public IndexedMatrixValue get() {
public T get() {
return _handle.get();
}

@Override
public synchronized OOCStream.QueueCallback<IndexedMatrixValue> keepOpen() {
public synchronized InMemoryQueueCallback<T> keepOpen() {
if(_closed)
throw new IllegalStateException("Cannot keep open a closed callback");
_handle._refCtr.incrementAndGet();
return new InMemoryQueueCallback(_handle);
return new InMemoryQueueCallback<>(_handle);
}

@Override
Expand Down Expand Up @@ -126,20 +128,19 @@ public boolean isFailure() {
return _handle._failure != null;
}

CallbackHandle getHandle() {
CallbackHandle<T> getHandle() {
return _handle;
}

static final class CallbackHandle {
private volatile IndexedMatrixValue _result;
static final class CallbackHandle<T> {
private volatile T _result;
private final AtomicInteger _refCtr;
private MemoryAllowance _allow;
private long _reservedBytes;
private volatile DMLRuntimeException _failure;
private int _cacheIdx;

private CallbackHandle(IndexedMatrixValue result, DMLRuntimeException failure, MemoryAllowance allow,
long reservedBytes) {
private CallbackHandle(T result, DMLRuntimeException failure, MemoryAllowance allow, long reservedBytes) {
_result = result;
_failure = failure;
_refCtr = new AtomicInteger(1);
Expand All @@ -148,7 +149,7 @@ private CallbackHandle(IndexedMatrixValue result, DMLRuntimeException failure, M
_cacheIdx = -1;
}

private IndexedMatrixValue get() {
private T get() {
if(_failure != null)
throw _failure;
return _result;
Expand All @@ -174,8 +175,8 @@ boolean isExclusiveToRoot() {
return _refCtr.get() == 1;
}

private synchronized IndexedMatrixValue takeManagedResultForHandover() {
IndexedMatrixValue result = _result;
private synchronized T takeManagedResultForHandover() {
T result = _result;
_result = null;
return result;
}
Expand All @@ -188,22 +189,22 @@ private void closeFinal() {
}
}

public IndexedMatrixValue takeManagedResultForHandover() {
public T takeManagedResultForHandover() {
return _handle.takeManagedResultForHandover();
}

public synchronized ManagedPayload<IndexedMatrixValue> extractManagedPayload() {
public synchronized ManagedPayload<T> extractManagedPayload() {
if(_closed)
throw new IllegalStateException("Cannot extract a managed payload from a closed callback.");
CallbackHandle handle = _handle;
CallbackHandle<T> handle = _handle;
synchronized(handle) {
if(handle._failure != null)
throw handle._failure;
if(!handle.isExclusiveToRoot())
throw new IllegalStateException("Cannot extract a managed payload while callback aliases exist.");
if(handle._cacheIdx >= 0)
throw new IllegalStateException("Cannot extract a managed payload from a cached-slot callback.");
IndexedMatrixValue result = handle._result;
T result = handle._result;
if(result == null)
throw new IllegalStateException("Cannot extract a managed payload from an empty callback.");
long bytes = handle._reservedBytes;
Expand Down
Loading
Loading