Dmdas.cpp 6.72 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
#include "Dmdas.h"
#include "util.h"

#include <vector>
#include <set>
#include <string>
#include <iostream>
#include <algorithm>

using namespace std;


Dmdas::Dmdas(const AlgOptions& options): 
  ranker(options), verbosity(options.asInt("verbosity", 0)) {
  
}

double Dmdas::compute(Instance &ins, SchedAction* action) {
    // Compute ranks
  TaskSet* readyTasks = ranker.makeSet(ins);
  
  int nbWorkers = ins.totalWorkers; 
  
  int n = ins.nbTasks; 

  vector< vector<int> > revDependencies = ins.getRevDependencies(); 
  
  vector<TaskSet*> localQueues(nbWorkers); 
  for(auto && tq: localQueues) {
    tq = ranker.makeSet(ins); 
  }
  vector<double> endtimesWorkers(nbWorkers, 0); 
  vector<double> endtimesTasks(n, -1);
  vector<double> assignedLoad(nbWorkers, 0); 
35
  vector<int> currentRunningTask(nbWorkers, -1); 
36 37 38 39
  //  vector<bool> leaveIdle(ins.nbWorkerTypes, false); 

  vector<bool> finishedTasks(n, false); 
  vector<int> nbDep(n, 0);
40 41 42



43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
  
  /*
    vector<int> wTypeOrdering(ins.nbWorkerTypes); 
  vector<int> wStartIndex(ins.nbWorkerTypes); 

  vector<double> aggregateTimings(ins.nbWorkerTypes, 1); 
  int index = 0; 
  for(int i = 0; i < ins.nbWorkerTypes; i++) {
    wStartIndex[i] = index; 
    index += ins.nbWorkers[i]; 
    for(int j = 0; j < ins.nbTaskTypes; j++) {
      aggregateTimings[i] *= ins.execTimes[i][j]; 
      }
    aggregateTimings[i] = pow(aggregateTimings[i], 1.0 / ins.nbTaskTypes); 
  }

  for(int i = 0; i < ins.nbWorkerTypes; i++) 
    wTypeOrdering[i] = i; 
  sort(wTypeOrdering.begin(), wTypeOrdering.end(), 
       [&] (int a, int b) { return aggregateTimings[a] < aggregateTimings[b]; });
  
  if(verbosity >= 6)
    cout << "List: wTypeOrdering= " << wTypeOrdering << endl; 

  */

  for(int i = 0; i < n; i++) {
    nbDep[i] = ins.dependencies[i].size();
    if(nbDep[i] == 0){
      readyTasks->insert(i);
      if(action != NULL) action->onTaskPush(i);
    }
  }
  
  double currentTime = 0; 
  int tasksToSchedule = n; 
  
  
  // For now keep the "old" version of dmdas where the assigned load also
  // counts lower priority tasks
83 84 85
  auto getFinishedTime = [&] (int task, int w, int wType) {
    double execTime = ins.execType(wType, task);
    if(ins.isValidValue(execTime)) {
86 87
      double s = max(endtimesWorkers[w], currentTime); 
      return (s + assignedLoad[w] 
88
	      + execTime); 
89 90
    } else 
      return std::numeric_limits<double>::infinity();
91 92 93
  }; 

  auto getBestWorker = [&] (int task) {
94 95 96 97 98 99 100 101 102
    double bestSoFar = std::numeric_limits<double>::infinity(); 
    int bestWorker = -1;
    int i = 0; 
    for(int wType = 0; wType < ins.nbWorkerTypes; ++wType) {
      for(int j = 0; j < ins.nbWorkers[wType]; ++j, ++i) {
	double t = getFinishedTime(task, i, wType); 
	if(t < bestSoFar) {
	  bestSoFar = t; bestWorker = i; 
	}
103 104
      }
    }
105 106 107 108
    if(bestWorker < 0) {
      cerr << "Dmdas: no valid worker for task " << task << endl;
      throw(1); 
    }
109 110 111 112 113 114 115 116 117 118
    return bestWorker; 
  }; 
  
  auto assignFront = [&] ()  {
    int task = readyTasks->front(); 
    int w = getBestWorker(task); 
    if(verbosity >= 4) {
      cout << "dmdas @ " << currentTime << " assigning task " << task << " to worker " << w << endl; 
    }
    localQueues[w]->insert(task); 
119
    assignedLoad[w] += ins.execWorker(w, task); 
120 121 122 123 124
    readyTasks->eraseFront(); 
  };

  //  int idle = wStartIndex[wTypeOrdering[0]];  

125
  
126 127 128 129 130
  while(tasksToSchedule > 0) {
  
    while(!readyTasks->empty()) 
      assignFront(); 
    
131 132 133 134 135 136 137 138
    int w = 0; 
    for(int wType = 0; wType < ins.nbWorkerTypes; ++wType) {
      for(int j = 0; j < ins.nbWorkers[wType]; ++j, ++w) {
	if((endtimesWorkers[w] <= currentTime) && (!localQueues[w]->empty())) {
	  int task = localQueues[w]->front(); 
	  double finishTime = currentTime + ins.execType(wType, task); 
	  if(verbosity >= 1) 
	    cout << "dmdas: starting " << task << " of type " << ins.taskTypes[task] << " at " << currentTime << " to end at " << finishTime << " on worker " << w << " of type " << wType << endl;
139
	
140 141 142 143 144 145 146 147 148 149
	  tasksToSchedule --; 
	  if(action != NULL) 
	    action->onSchedule(task, w, currentTime, finishTime);

	  localQueues[w]->eraseFront(); 
	  assignedLoad[w] -= ins.execType(wType, task); 
	  endtimesWorkers[w] = finishTime;
	  endtimesTasks[task] = finishTime;
	  currentRunningTask[w] = task;
	}
150 151
      }
    }
152
    
153 154 155 156 157 158 159 160 161 162
    double nextTime = -1; 
    for(int w = 0; w < nbWorkers; w++) {
      if((endtimesWorkers[w] > currentTime) && 
	 ((endtimesWorkers[w] < nextTime) || (nextTime == -1))) {
	nextTime = endtimesWorkers[w]; 
      }
    }
    
    if(nextTime == -1){
      cerr << "dmdas: next Time == -1 !" << endl; 
163
      throw(-1); 
164 165 166 167 168
    } 
    
    currentTime = nextTime; 
    
    //Finish all tasks with finishTime <= currentTime
169 170 171
    for(int j = 0; j < nbWorkers; j++) {
      int i = currentRunningTask[j]; 
      if((i != -1) && (!finishedTasks[i]) && (endtimesTasks[i] != -1) && (endtimesTasks[i] <= currentTime)) {
172 173 174
	if(verbosity >= 1) 
	  cout << "List: Finishing task " << i << " at time " << currentTime << endl;
	finishedTasks[i] = true; 
175
	currentRunningTask[j] = -1; 
176
	for(int & k: revDependencies[i]) {
177 178 179 180 181 182 183 184 185 186 187
	  nbDep[k]--;
	  if(verbosity >= 7) 
	    cout << "      Dependent task: " << k << " remaining dependencies: " << nbDep[k] << endl;
	  if(nbDep[k] == 0){
	    readyTasks->insert(k);
	    if(action != NULL) {
	      action->onTaskPush(k);
	    }
	  }
	}
      }
188
    }
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
  }
  return getMax(endtimesTasks); 
  
}


// class Dmdas_Action : public SchedAction {
// protected: 
//   vector<TaskSet> allocated;
//   vector<double> allocatedLoad;
//   Instance& instance; 
  
//   vector<double> endtimesWorkers; 
//   int verbosity; 
  
// public: 
//   Dmdas_Action(Instance& ins, int verb) : 
//     allocated(ins.totalWorkers), allocatedLoad(ins.totalWorkers), 
//     instance(ins),
//     endtimesWorkers(ins.totalWorkers, 0), verbosity(verb) {
//   }
//   void onSchedule(int task, int worker, double start, double end) {
//     int t = getType(worker, instance.nbWorkers, NULL);
//     allocated[t].erase(find(allocated[t].begin(), allocated[t].end(), task)); 
//     allocatedLoad[t] -= ins.execTimes[ins.taskTypes[task]][t]; 
//     endtimesWorkers[worker] = end; 
//   }

//   void onTaskPush(int task) {
//     // choose worker w with min finish time 
//     // add task to allocated[w]
//   }
  
//   int chooseTask(int wType, double now) {
//     // Hmm. How do I manage this ? There is no notion of *this* worker
//     // in ListAlgorithm ... It's bad, cause I have to do "average
//     // load" in that case.
//     // Probably easier to recode as an "event-based" algorithm
//     // Or just find any idle worker of type wType and give the task 
//     // in its queue. 
//     // So: 
//     // take first worker of type wType with finish time <= now
//     // and whose queue is non empty. But that's like so wasteful: listAlgorithm knows  ;'(

//   }

// }