use of es.bsc.compss.types.implementations.Implementation in project compss by bsc-wdc.
the class MOResourceOptimizer method getContext.
// Get estimated cost (energy, price, time) of the current scheduling
private <T extends WorkerResourceDescription> ConfigurationCost getContext(Resource<?>[] allResources, int[] load, Collection<ResourceScheduler<? extends WorkerResourceDescription>> workers, List<ResourceCreationRequest> creations, HashMap<CloudInstanceTypeDescription, Integer> pendingCreations, HashMap<CloudInstanceTypeDescription, Integer> pendingDestructions, double[] elapsedTime, double[] elapsedEnergy, double[] elapsedCost, double[] elapsedPower, double[] elapsedPrice) {
elapsedTime[0] = 0;
elapsedEnergy[0] = 0;
elapsedCost[0] = 0;
elapsedPower[0] = 0;
elapsedPrice[0] = 0;
double time = 0;
double actionsCost = 0;
double idlePrice = 0;
double actionsEnergy = 0;
double idlePower = 0;
int resourceId = 0;
for (ResourceScheduler<?> w : workers) {
MOResourceScheduler<T> aw = (MOResourceScheduler<T>) w;
Resource<T> r = new Resource<>(aw);
allResources[resourceId] = r;
addToLog("\tName:" + aw.getName() + (r.hasPendingModifications() ? " (IS TO BE DELETED)" : "") + "\n");
time = Math.max(time, aw.getLastGapExpectedStart());
addToLog("\t\tTime:" + aw.getLastGapExpectedStart() + " ms -> total " + time + "\n");
elapsedCost[0] += aw.getRunActionsCost();
addToLog("\t\tExecuted Actions Cost:" + aw.getRunActionsCost() + " €.ms/h -> total " + elapsedCost[0] + "€.ms/h\n");
actionsCost += aw.getScheduledActionsCost();
addToLog("\t\tScheduled Actions Cost:" + aw.getScheduledActionsCost() + " €.ms/h -> total " + actionsCost + "€.ms/h\n");
r.idlePrice = aw.getIdlePrice();
idlePrice += r.idlePrice;
addToLog("\t\tIdle Price:" + r.idlePrice + " €/h -> total " + idlePrice + "€/h\n");
elapsedEnergy[0] += aw.getRunActionsEnergy();
addToLog("\t\tExecuted Actions Energy:" + aw.getRunActionsEnergy() + " mJ -> total " + elapsedEnergy[0] + "mJ\n");
actionsEnergy += aw.getScheduledActionsEnergy();
addToLog("\t\tScheduled Actions Energy:" + aw.getScheduledActionsEnergy() + " mJ -> total " + actionsEnergy + "mJ\n");
r.idlePower = aw.getIdlePower();
idlePower += r.idlePower;
addToLog("\t\tIdle Power:" + r.idlePower + " W -> total " + idlePower + "W\n");
r.startTime = aw.getExpectedEndTimeRunning();
r.startCost = aw.getRunningActionsCost();
r.startEnergy = aw.getRunningActionsEnergy();
int[][] implsCount = aw.getImplementationCounts();
int[][] runningCounts = aw.getRunningImplementationCounts();
addToLog("\t\tCore Information:\n");
StringBuilder[] coreInfo = new StringBuilder[CoreManager.getCoreCount()];
Implementation[] impls = new Implementation[CoreManager.getCoreCount()];
for (int coreId = 0; coreId < CoreManager.getCoreCount(); coreId++) {
coreInfo[coreId] = new StringBuilder("\t\t\tCore " + coreId + "\n");
int favId = 0;
int favCount = implsCount[coreId][0];
load[coreId] += implsCount[coreId][0] - runningCounts[coreId][0];
coreInfo[coreId].append("\t\t\t\tImplementation 0: " + implsCount[coreId][0] + ", " + runningCounts[coreId][0] + " of'em already running\n");
for (int implId = 1; implId < CoreManager.getCoreImplementations(coreId).size(); implId++) {
coreInfo[coreId].append("\t\t\t\tImplementation " + implId + ": " + implsCount[coreId][implId] + ", " + runningCounts[coreId][implId] + " of'em already running\n");
load[coreId] += implsCount[coreId][implId] - runningCounts[coreId][implId];
if (implsCount[coreId][implId] > favCount) {
favId = implId;
}
}
if (favCount > 0) {
impls[coreId] = CoreManager.getCoreImplementations(coreId).get(favId);
} else {
List<Implementation> coreImpls = CoreManager.getCoreImplementations(coreId);
MOProfile[] profiles = new MOProfile[coreImpls.size()];
for (int i = 0; i < profiles.length; i++) {
profiles[i] = (MOProfile) aw.getProfile(coreImpls.get(i));
}
impls[coreId] = getBestImplementation(coreImpls, profiles);
}
coreInfo[coreId].append("\t\t\t\tFavorite Implementation " + favId + "\n");
}
r.profiles = new MOProfile[implsCount.length];
r.capacity = new int[implsCount.length];
for (int coreId = 0; coreId < implsCount.length; coreId++) {
r.profiles[coreId] = (MOProfile) aw.getProfile(impls[coreId]);
coreInfo[coreId].append("\t\t\t\tProfile " + r.profiles[coreId] + "\n");
r.capacity[coreId] = aw.getSimultaneousCapacity(impls[coreId]);
coreInfo[coreId].append("\t\t\t\tCapacity " + r.capacity[coreId] + "\n");
addToLog(coreInfo[coreId].toString());
}
if (r.hasPendingModifications()) {
for (ResourceUpdate<T> ru : r.getPendingModifications()) {
Map<CloudInstanceTypeDescription, int[]> modificationComposition = ((CloudMethodResourceDescription) ru.getModification()).getTypeComposition();
for (Map.Entry<CloudInstanceTypeDescription, int[]> entry : modificationComposition.entrySet()) {
CloudInstanceTypeDescription componentType = entry.getKey();
int count = entry.getValue()[0];
Integer pendingDestruction = pendingDestructions.get(componentType);
if (pendingDestruction == null) {
pendingDestruction = 0;
}
pendingDestruction += count;
pendingDestructions.put(componentType, pendingDestruction);
}
}
}
resourceId++;
}
// Convert time to secs
elapsedTime[0] = (double) (System.currentTimeMillis() - initialTimeStamp) / 1000;
// Convert energy from mJ to Wh
elapsedEnergy[0] = elapsedEnergy[0] / 3_600_000;
// Convert energy from €ms/h to €
elapsedCost[0] = elapsedCost[0] / 3_600_000;
for (ResourceCreationRequest rcr : creations) {
for (Map.Entry<CloudInstanceTypeDescription, int[]> entry : rcr.getRequested().getTypeComposition().entrySet()) {
CloudInstanceTypeDescription componentType = entry.getKey();
int count = entry.getValue()[0];
addToLog("\tName: REQUESTED " + componentType.getName() + "\n");
Integer pendingCreation = pendingCreations.get(componentType);
if (pendingCreation == null) {
pendingCreation = 0;
}
pendingCreation += count;
pendingCreations.put(componentType, pendingCreation);
}
Resource<?> r = createResourceForCreationRequest(rcr);
allResources[resourceId] = r;
addToLog("\t\tTime: 0 ms -> total " + time + "\n");
addToLog("\t\tactions Cost: 0 € -> total " + actionsCost + "€\n");
idlePrice += r.idlePrice;
addToLog("\t\tIdle Price:" + r.idlePrice + " € -> total " + idlePrice + "€\n");
addToLog("\t\tactions Energy:0 mJ -> total " + actionsEnergy + "mJ\n");
idlePower += r.idlePower;
addToLog("\t\tIdle Power:" + r.idlePower + " W -> total " + idlePower + "W\n");
// r.startTime = 0;
r.startCost = 0;
r.startEnergy = 0;
addToLog("\t\tCore Information:\n");
StringBuilder[] coreInfo = new StringBuilder[CoreManager.getCoreCount()];
for (int coreId = 0; coreId < CoreManager.getCoreCount(); coreId++) {
coreInfo[coreId] = new StringBuilder("\t\t\tCore " + coreId + "\n");
load[coreId] += 0;
coreInfo[coreId].append("\t\t\t\tImplementation 0: 0, 0 of'em already running\n");
for (int implId = 1; implId < CoreManager.getCoreImplementations(coreId).size(); implId++) {
coreInfo[coreId].append("\t\t\t\tImplementation " + implId + ": 0, 0 of'em already running\n");
}
coreInfo[coreId].append("\t\t\t\tFavorite Implementation 0\n");
}
for (int coreId = 0; coreId < CoreManager.getCoreCount(); coreId++) {
coreInfo[coreId].append("\t\t\t\tProfile " + r.profiles[coreId] + "\n");
coreInfo[coreId].append("\t\t\t\tCapacity " + r.capacity[coreId] + "\n");
addToLog(coreInfo[coreId].toString());
}
resourceId++;
}
return new ConfigurationCost(time, idlePower, actionsEnergy, idlePrice, actionsCost);
}
use of es.bsc.compss.types.implementations.Implementation in project compss by bsc-wdc.
the class MOResourceOptimizer method createResourceForCreationRequest.
private Resource<?> createResourceForCreationRequest(ResourceCreationRequest rcr) {
CloudMethodResourceDescription cmrd = rcr.getRequested();
Resource<?> r = new Resource<>(null);
Map<CloudInstanceTypeDescription, int[]> composition = cmrd.getTypeComposition();
r.capacity = new int[CoreManager.getCoreCount()];
r.profiles = new MOProfile[CoreManager.getCoreCount()];
for (Map.Entry<CloudInstanceTypeDescription, int[]> component : composition.entrySet()) {
CloudInstanceTypeDescription type = component.getKey();
int count = component.getValue()[0];
MethodResourceDescription rd = type.getResourceDescription();
MOCloudTypeProfile moCloudTypeProf = (MOCloudTypeProfile) getCloudTypeProfile(type);
r.idlePower += moCloudTypeProf.getIdlePower() * count;
r.idlePrice += moCloudTypeProf.getIdlePrice() * count;
for (int coreId = 0; coreId < CoreManager.getCoreCount(); coreId++) {
List<Implementation> impls = CoreManager.getCoreImplementations(coreId);
MOProfile[] profiles = new MOProfile[impls.size()];
for (int i = 0; i < impls.size(); i++) {
profiles[i] = (MOProfile) moCloudTypeProf.getImplProfiles(coreId, impls.get(i).getImplementationId());
}
Implementation impl = getBestImplementation(impls, profiles);
r.capacity[coreId] += rd.canHostSimultaneously((MethodResourceDescription) impl.getRequirements()) * count;
MOProfile bestImplProf = (MOProfile) moCloudTypeProf.getImplProfiles(coreId, impl.getImplementationId());
if (r.profiles[coreId] == null) {
r.profiles[coreId] = bestImplProf;
} else {
r.profiles[coreId].accumulate(bestImplProf);
}
}
}
r.startTime = (cmrd.getImage().getCreationTime() * 1000) - (System.currentTimeMillis() - rcr.getRequestedTime());
if (r.startTime < 0) {
r.startTime = 0;
}
r.clear();
return r;
}
use of es.bsc.compss.types.implementations.Implementation in project compss by bsc-wdc.
the class MOResourceOptimizer method getBestImplementation.
private Implementation getBestImplementation(List<Implementation> impls, MOProfile[] profiles) {
Implementation impl = impls.get(0);
MOScore bestScore = new MOScore(0, 0, 0, profiles[0].getAverageExecutionTime(), profiles[0].getPower(), profiles[0].getPrice());
for (int i = 1; i < impls.size(); i++) {
Implementation candidate = impls.get(i);
long length = profiles[i].getAverageExecutionTime();
double power = profiles[i].getPower();
double price = profiles[i].getPrice();
MOScore score = new MOScore(0, 0, 0, length, power * length, price);
if (Score.isBetter(score, bestScore)) {
bestScore = score;
impl = candidate;
}
}
return impl;
}
use of es.bsc.compss.types.implementations.Implementation 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.types.implementations.Implementation 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;
}
Aggregations