use of es.bsc.compss.scheduler.types.AllocatableAction in project compss by bsc-wdc.
the class MOResourceScheduler method scheduleUsingGaps.
private void scheduleUsingGaps(AllocatableAction action, List<Gap> gaps) {
long expectedStart = 0;
// Compute start time due to data dependencies
for (AllocatableAction predecessor : action.getDataPredecessors()) {
MOSchedulingInformation predDSI = ((MOSchedulingInformation) predecessor.getSchedulingInfo());
if (predDSI.isScheduled()) {
long predEnd = predDSI.getExpectedEnd();
expectedStart = Math.max(expectedStart, predEnd);
}
}
MOSchedulingInformation schedInfo = (MOSchedulingInformation) action.getSchedulingInfo();
if (expectedStart == Long.MAX_VALUE) {
// There is some data dependency with blocked tasks in some resource
Gap opActionGap = new Gap(0, 0, dataBlockingAction, action.getAssignedImplementation().getRequirements().copy(), 0);
MOSchedulingInformation dbaDSI = (MOSchedulingInformation) dataBlockingAction.getSchedulingInfo();
dbaDSI.lock();
schedInfo.lock();
dbaDSI.addSuccessor(action);
schedInfo.addPredecessor(opActionGap);
schedInfo.setExpectedStart(Long.MAX_VALUE);
schedInfo.setExpectedEnd(Long.MAX_VALUE);
schedInfo.scheduled();
dbaDSI.unlock();
schedInfo.unlock();
return;
}
Implementation impl = action.getAssignedImplementation();
MOProfile p = (MOProfile) getProfile(impl);
ResourceDescription constraints = impl.getRequirements().copy();
List<Gap> predecessors = new LinkedList<>();
Iterator<Gap> gapIt = ((LinkedList<Gap>) gaps).descendingIterator();
boolean fullyCoveredReqs = false;
// Check gaps before data start
while (gapIt.hasNext() && !fullyCoveredReqs) {
Gap gap = gapIt.next();
if (gap.getInitialTime() <= expectedStart) {
useGap(gap, constraints, predecessors);
fullyCoveredReqs = constraints.isDynamicUseless();
if (gap.getResources().isDynamicUseless()) {
gapIt.remove();
}
}
}
// Check gaps after data start
gapIt = gaps.iterator();
while (gapIt.hasNext() && !fullyCoveredReqs) {
Gap gap = gapIt.next();
if (gap.getInitialTime() > expectedStart) {
if (gap.getInitialTime() < Long.MAX_VALUE) {
useGap(gap, constraints, predecessors);
fullyCoveredReqs = constraints.isDynamicUseless();
if (gap.getResources().isDynamicUseless()) {
gapIt.remove();
}
}
}
}
if (!fullyCoveredReqs) {
// Action gets blocked due to lack of resources
for (Gap pGap : predecessors) {
addGap(pGap);
AllocatableAction predecessor = (AllocatableAction) pGap.getOrigin();
if (predecessor != null) {
MOSchedulingInformation predDSI = ((MOSchedulingInformation) predecessor.getSchedulingInfo());
predDSI.unlock();
}
}
Gap opActionGap = new Gap(0, 0, resourceBlockingAction, action.getAssignedImplementation().getRequirements(), 0);
MOSchedulingInformation rbaDSI = (MOSchedulingInformation) resourceBlockingAction.getSchedulingInfo();
rbaDSI.lock();
schedInfo.lock();
rbaDSI.addSuccessor(action);
schedInfo.addPredecessor(opActionGap);
schedInfo.scheduled();
schedInfo.setExpectedStart(Long.MAX_VALUE);
schedInfo.setExpectedEnd(Long.MAX_VALUE);
rbaDSI.unlock();
schedInfo.unlock();
return;
}
// Lock acces to the current task
schedInfo.lock();
schedInfo.scheduled();
// Add dependencies
// Unlock access to predecessor
StringBuilder sb = null;
if (IS_DEBUG) {
sb = new StringBuilder("Predecessors: ");
}
for (Gap pGap : predecessors) {
AllocatableAction predecessor = pGap.getOrigin();
if (predecessor != null) {
MOSchedulingInformation predDSI = ((MOSchedulingInformation) predecessor.getSchedulingInfo());
if (predDSI.isScheduled()) {
long predEnd = predDSI.getExpectedEnd();
expectedStart = Math.max(expectedStart, predEnd);
predDSI.addSuccessor(action);
}
predDSI.unlock();
}
schedInfo.addPredecessor(pGap);
if (IS_DEBUG) {
sb.append(pGap.getOrigin()).append(" with ").append(pGap.getResources().getDynamicDescription()).append(", ");
}
}
// Compute end time
schedInfo.setExpectedStart(expectedStart);
long expectedEnd = expectedStart;
if (p != null) {
expectedEnd += p.getAverageExecutionTime();
pendingActionsCost += p.getPrice() * p.getAverageExecutionTime();
pendingActionsEnergy += p.getPower() * p.getAverageExecutionTime();
}
schedInfo.setExpectedEnd(expectedEnd);
// Unlock access to current task
schedInfo.unlock();
if (action.isToReleaseResources()) {
// Create new Gap correspondin to the resources released by the action
addGap(new Gap(expectedEnd, Long.MAX_VALUE, action, impl.getRequirements().copy(), 0));
} else {
addGap(new Gap(Long.MAX_VALUE, Long.MAX_VALUE, action, impl.getRequirements().copy(), 0));
}
if (IS_DEBUG) {
LOGGER.debug(LOG_PREFIX + "Scheduled " + action.toString() + ". Interval [ " + expectedStart + " - " + expectedEnd + "] " + sb.toString());
}
}
use of es.bsc.compss.scheduler.types.AllocatableAction in project compss by bsc-wdc.
the class MOResourceScheduler method unscheduleAction.
@Override
public List<AllocatableAction> unscheduleAction(AllocatableAction action) throws ActionNotFoundException {
super.unscheduleAction(action);
List<AllocatableAction> freeActions = new LinkedList<>();
MOSchedulingInformation actionDSI = (MOSchedulingInformation) action.getSchedulingInfo();
List<Gap> resources = new LinkedList<>();
// Block all predecessors
for (Gap pGap : actionDSI.getPredecessors()) {
AllocatableAction pred = pGap.getOrigin();
if (pred != null) {
MOSchedulingInformation predDSI = (MOSchedulingInformation) pred.getSchedulingInfo();
predDSI.lock();
}
}
// Block Action
actionDSI.lock();
if (!actionDSI.isScheduled() || action.getAssignedResource() != this) {
for (Gap pGap : actionDSI.getPredecessors()) {
AllocatableAction pred = pGap.getOrigin();
if (pred != null) {
MOSchedulingInformation predDSI = (MOSchedulingInformation) pred.getSchedulingInfo();
predDSI.unlock();
}
}
actionDSI.unscheduled();
actionDSI.unlock();
throw new ActionNotFoundException();
}
ResourceDescription unassignedResources = action.getAssignedImplementation().getRequirements().copy();
// Remove the scheduling dependency on the predecessor
for (Gap pGap : actionDSI.getPredecessors()) {
AllocatableAction pred = pGap.getOrigin();
if (pred != null) {
if (!(pred instanceof OptimizationAction)) {
resources.add(new Gap(pGap.getInitialTime(), Long.MAX_VALUE, pred, pGap.getResources().copy(), 0));
unassignedResources.reduceDynamic(pGap.getResources());
}
MOSchedulingInformation predDSI = (MOSchedulingInformation) pred.getSchedulingInfo();
predDSI.removeSuccessor(action);
}
}
resources.add(new Gap(Long.MIN_VALUE, Long.MAX_VALUE, null, unassignedResources, 0));
// Remove all predecessors for
actionDSI.clearPredecessors();
// Block all successors
List<MOSchedulingInformation> successorsDSIs = new LinkedList<MOSchedulingInformation>();
for (AllocatableAction successor : actionDSI.getSuccessors()) {
MOSchedulingInformation succDSI = (MOSchedulingInformation) successor.getSchedulingInfo();
succDSI.lock();
successorsDSIs.add(succDSI);
}
// For each successor look for the resources
for (AllocatableAction successor : actionDSI.getSuccessors()) {
MOSchedulingInformation succDSI = (MOSchedulingInformation) successor.getSchedulingInfo();
// Gets the resources that was supposed to get from the task and remove the dependency
Gap toCover = succDSI.removePredecessor(action);
if (toCover != null) {
ResourceDescription resToCover = toCover.getResources();
// Scans the resources related to the task to cover its requirements
Iterator<Gap> gIt = resources.iterator();
while (gIt.hasNext()) {
Gap availableGap = gIt.next();
// Takes the resources from a predecessor,
ResourceDescription availableDesc = availableGap.getResources();
ResourceDescription usedResources = ResourceDescription.reduceCommonDynamics(availableDesc, resToCover);
// If all the resources required for the successor are covered -> move to the next successor
if (!usedResources.isDynamicUseless()) {
AllocatableAction availableOrigin = availableGap.getOrigin();
MOSchedulingInformation availableDSI = null;
if (availableOrigin != null) {
availableDSI = (MOSchedulingInformation) availableOrigin.getSchedulingInfo();
availableDSI.addSuccessor(successor);
succDSI.addPredecessor(new Gap(availableGap.getInitialTime(), Long.MAX_VALUE, availableOrigin, usedResources, 0));
}
if (availableDesc.isDynamicUseless()) {
gIt.remove();
if (availableDSI != null) {
availableDSI.unlock();
}
}
if (resToCover.isDynamicUseless()) {
break;
}
}
}
}
if (succDSI.isExecutable()) {
freeActions.add(successor);
}
}
// Clear action's successors
actionDSI.clearSuccessors();
// Indicate that the task is fully unsheduled
actionDSI.unscheduled();
// Register those resources occupied by the task that haven't been used as free
synchronized (gaps) {
if (actionDSI.isOnOptimization()) {
pendingUnschedulings.add(action);
}
Iterator<Gap> gIt = gaps.iterator();
while (gIt.hasNext()) {
Gap g = gIt.next();
if (g.getOrigin() == action) {
gIt.remove();
}
}
for (Gap newGap : resources) {
AllocatableAction gapAction = newGap.getOrigin();
addGap(newGap);
if (gapAction != null) {
((MOSchedulingInformation) gapAction.getSchedulingInfo()).unlock();
}
}
}
Implementation impl = action.getAssignedImplementation();
MOProfile p = (MOProfile) getProfile(impl);
if (p != null) {
long length = actionDSI.getExpectedEnd() - (actionDSI.getExpectedStart() < 0 ? 0 : actionDSI.getExpectedStart());
pendingActionsCost -= p.getPrice() * length;
pendingActionsEnergy -= p.getPower() * length;
}
actionDSI.unlock();
for (MOSchedulingInformation successorsDSI : successorsDSIs) {
successorsDSI.unlock();
}
return freeActions;
}
use of es.bsc.compss.scheduler.types.AllocatableAction in project compss by bsc-wdc.
the class MOResourceScheduler method useGap.
private void useGap(Gap gap, ResourceDescription resources, List<Gap> predecessors) {
AllocatableAction predecessor = (AllocatableAction) gap.getOrigin();
ResourceDescription gapResource = gap.getResources();
ResourceDescription usedResources = ResourceDescription.reduceCommonDynamics(gapResource, resources);
if (usedResources.isDynamicConsuming()) {
if (predecessor != null) {
MOSchedulingInformation predDSI = ((MOSchedulingInformation) predecessor.getSchedulingInfo());
predDSI.lock();
}
Gap g = new Gap(gap.getInitialTime(), Long.MAX_VALUE, predecessor, usedResources, 0);
predecessors.add(g);
}
}
use of es.bsc.compss.scheduler.types.AllocatableAction in project compss by bsc-wdc.
the class MOResourceScheduler method rescheduleTasks.
@SuppressWarnings("unchecked")
public List<Gap> rescheduleTasks(LocalOptimizationState state, PriorityQueue<AllocatableAction> rescheduledActions) {
this.runActionsCost = 0;
this.runActionsEnergy = 0;
for (int coreId = 0; coreId < CoreManager.getCoreCount(); coreId++) {
for (int implId = 0; implId < CoreManager.getNumberCoreImplementations(coreId); implId++) {
MOProfile profile = (MOProfile) this.getProfile(coreId, implId);
runActionsEnergy += profile.getPower() * profile.getExecutionCount() * profile.getAverageExecutionTime();
runActionsCost += profile.getPrice() * profile.getExecutionCount() * profile.getAverageExecutionTime();
}
}
/*
*
* ReadyActions contains those actions that have no dependencies with other actions scheduled on the node, but
* they have data dependencies with tasks on other resources. They are sorted by the expected time when these
* dependencies will be solved.
*
* SelectableActions contains those actions that have no data dependencies with other actions but they wait for
* resources to be released.
*
* Running actions contains a list of Actions that are executing or potentially executing at the moment.
*
* All Actions that need to be rescheduled have the onOptimization and scheduled flags on.
*
* Those actions that are running or could potentially be started ( no dependencies with other actions in the
* resource) are already locked to avoid their start without being on the runningActions set.
*/
Gap gap = state.peekFirstGap();
ResourceDescription gapResource = gap.getResources();
PriorityQueue<SchedulingEvent> schedulingQueue = new PriorityQueue<>();
// For every running action we create a start event on their real start timeStamp
for (AllocatableAction action : state.getRunningActions()) {
manageRunningAction(action, state);
MOSchedulingInformation actionDSI = (MOSchedulingInformation) action.getSchedulingInfo();
schedulingQueue.offer(new SchedulingEvent.End(actionDSI.getExpectedEnd(), action));
}
while (state.areRunnableActions() && !gapResource.isDynamicUseless()) {
AllocatableAction top = state.getMostPrioritaryRunnableAction();
state.replaceAction(top);
if (state.canActionRun()) {
state.removeMostPrioritaryRunnableAction();
// Start the current action
MOSchedulingInformation topDSI = (MOSchedulingInformation) top.getSchedulingInfo();
topDSI.lock();
topDSI.clearPredecessors();
manageRunningAction(top, state);
if (tryToLaunch(top)) {
schedulingQueue.offer(new SchedulingEvent.End(topDSI.getExpectedEnd(), top));
}
} else {
break;
}
}
while (!schedulingQueue.isEmpty() || state.areActionsToBeRescheduled()) {
while (!schedulingQueue.isEmpty()) {
SchedulingEvent e = schedulingQueue.poll();
/*
* Start Event: - sets the expected start and end times - adds resource dependencies with the previous
* actions - if there's a gap before the dependency -tries to fill it with other tasks - if all the
* resources released by the predecessor are used later - the action is unlocked
*
* End Event:
*
*/
List<SchedulingEvent> result = e.process(state, (MOResourceScheduler<WorkerResourceDescription>) this, rescheduledActions);
for (SchedulingEvent r : result) {
schedulingQueue.offer(r);
}
}
if (state.areActionsToBeRescheduled()) {
AllocatableAction topAction = state.getEarliestActionToBeRescheduled();
MOSchedulingInformation topActionDSI = (MOSchedulingInformation) topAction.getSchedulingInfo();
topActionDSI.lock();
topActionDSI.setToReschedule(false);
schedulingQueue.offer(new SchedulingEvent.Start(topActionDSI.getExpectedStart(), topAction));
}
}
for (Gap g : state.getGaps()) {
state.removeTmpGap(g);
}
this.pendingActionsCost = state.getTotalCost();
this.pendingActionsEnergy = state.getTotalEnergy();
this.implementationsCount = state.getImplementationsCount();
this.expectedEndTimeRunning = state.getEndRunningTime();
this.runningImplementationsCount = state.getRunningImplementations();
this.runningActionsEnergy = state.getRunningEnergy();
this.runningActionsCost = state.getRunningCost();
this.resourceBlockingAction = state.getResourceBlockingAction();
this.dataBlockingAction = state.getDataBlockingAction();
return state.getGaps();
}
use of es.bsc.compss.scheduler.types.AllocatableAction in project compss by bsc-wdc.
the class MOResourceScheduler method scanActions.
// Classifies actions according to their start times. Selectable actions are
// those that can be selected to run from t=0. Ready actions are those actions
// that have data dependencies with tasks scheduled in other nodes. Actions
// with dependencies with actions scheduled in the same node, are not
// classified in any list since we cannot know the start time.
public List<AllocatableAction> scanActions(LocalOptimizationState state) {
List<AllocatableAction> pendingToUnlockActions = new LinkedList<AllocatableAction>();
PriorityQueue<AllocatableAction> actions = new PriorityQueue<AllocatableAction>(1, getScanComparator());
MOSchedulingInformation blockSI = (MOSchedulingInformation) dataBlockingAction.getSchedulingInfo();
List<AllocatableAction> blockActions = blockSI.getSuccessors();
for (AllocatableAction gapAction : blockActions) {
MOSchedulingInformation dsi = (MOSchedulingInformation) gapAction.getSchedulingInfo();
dsi.lock();
dsi.setOnOptimization(true);
actions.add(gapAction);
}
blockSI = (MOSchedulingInformation) resourceBlockingAction.getSchedulingInfo();
blockActions = blockSI.getSuccessors();
for (AllocatableAction gapAction : blockActions) {
MOSchedulingInformation dsi = (MOSchedulingInformation) gapAction.getSchedulingInfo();
dsi.lock();
dsi.setOnOptimization(true);
actions.add(gapAction);
}
LinkedList<AllocatableAction> modified = new LinkedList<>();
while (true) {
try {
for (Gap g : gaps) {
AllocatableAction gapAction = g.getOrigin();
if (gapAction != null) {
MOSchedulingInformation dsi = (MOSchedulingInformation) gapAction.getSchedulingInfo();
dsi.lock();
dsi.setOnOptimization(true);
modified.add(gapAction);
}
}
break;
} catch (ConcurrentModificationException cme) {
for (AllocatableAction action : modified) {
MOSchedulingInformation dsi = (MOSchedulingInformation) action.getSchedulingInfo();
dsi.setOnOptimization(false);
dsi.unlock();
}
modified.clear();
}
}
actions.addAll(modified);
AllocatableAction action;
while ((action = actions.poll()) != null) {
MOSchedulingInformation actionDSI = (MOSchedulingInformation) action.getSchedulingInfo();
// System.out.println(" ** Evaluating Action "+ action + " with SI: "+ actionDSI.hashCode());
if (!actionDSI.isScheduled()) {
try {
actionDSI.unlock();
} catch (IllegalMonitorStateException e) {
LOGGER.debug(LOG_PREFIX + "Illegal Monitor Exception scaning actions. Ignoring...");
}
// scheduled ");
continue;
}
// Data Dependencies analysis
boolean hasInternal = false;
boolean hasExternal = false;
long startTime = 0;
while (true) {
List<MOSchedulingInformation> managedDSIs = new LinkedList<>();
try {
List<AllocatableAction> dPreds = action.getDataPredecessors();
for (AllocatableAction dPred : dPreds) {
MOSchedulingInformation dPredDSI = (MOSchedulingInformation) dPred.getSchedulingInfo();
if (dPred.getAssignedResource() == this) {
if (dPredDSI.tryToLock()) {
if (dPredDSI.isScheduled()) {
hasInternal = true;
dPredDSI.addOptimizingSuccessor(action);
managedDSIs.add(dPredDSI);
}
dPredDSI.unlock();
}
// else
// The predecessor is trying to be unscheduled but it is
// blocked by another successor reschedule.
} else {
hasExternal = true;
startTime = Math.max(startTime, dPredDSI.getExpectedEnd());
}
}
break;
} catch (ConcurrentModificationException cme) {
for (MOSchedulingInformation dsi : managedDSIs) {
dsi.removeOptimizingSuccessor(action);
}
}
}
// Resource Dependencies analysis
boolean hasResourcePredecessors = false;
List<Gap> rPredGaps = actionDSI.getPredecessors();
for (Gap rPredGap : rPredGaps) {
AllocatableAction rPred = rPredGap.getOrigin();
if (rPred != null) {
MOSchedulingInformation rPredDSI = (MOSchedulingInformation) rPred.getSchedulingInfo();
if (rPredDSI.tryToLock()) {
if (rPredDSI.isScheduled()) {
hasResourcePredecessors = true;
if (!rPredDSI.isOnOptimization()) {
rPredDSI.setOnOptimization(true);
actions.add(rPred);
} else {
rPredDSI.unlock();
}
} else {
rPredDSI.unlock();
}
}
}
// else the predecessor was already executed
}
actionDSI.setExpectedStart(startTime);
actionDSI.setToReschedule(true);
// System.out.println(" ** Classifiying Action "+ action + " with SI: "+ actionDSI.hashCode());
state.classifyAction(action, hasInternal, hasExternal, hasResourcePredecessors, startTime);
if (hasResourcePredecessors || hasInternal) {
// The action has a blocked predecessor in the resource that will block its execution
actionDSI.unlock();
} else {
pendingToUnlockActions.add(action);
}
// System.out.println(" ** END evaluation Action "+ action + " with SI: "+ actionDSI.hashCode());
}
return pendingToUnlockActions;
}
Aggregations