Attention une mise à jour du serveur va être effectuée le lundi 17 mai entre 13h et 13h30. Cette mise à jour va générer une interruption du service de quelques minutes.

schedAction.cpp 6.86 KB
Newer Older
EYRAUD-DUBOIS Lionel's avatar
EYRAUD-DUBOIS Lionel committed
1
#include "schedAction.h"
EYRAUD-DUBOIS Lionel's avatar
EYRAUD-DUBOIS Lionel committed
2
#include "util.h"
3
#include <iostream>
EYRAUD-DUBOIS Lionel's avatar
EYRAUD-DUBOIS Lionel committed
4

EYRAUD-DUBOIS Lionel's avatar
EYRAUD-DUBOIS Lionel committed
5 6
using namespace std;

7 8
/* Action Sequence: list of actions, performed in sequence */

9 10 11
void ActionSequence::add(SchedAction* a) {
  actions.push_back(a);
}
EYRAUD-DUBOIS Lionel's avatar
EYRAUD-DUBOIS Lionel committed
12 13 14 15 16 17 18 19 20 21 22 23 24 25

void ActionSequence::remove(SchedAction* a) {
  auto it = actions.begin(); 
  while(it != actions.end()) {
    if (a == *it) break; 
    ++it; 
  }
  if(it != actions.end())
    actions.erase(it);
  else {
    cerr << "Warning: unable to remove action " << a << " from list of actions" << endl; 
  }
}

26 27 28
void ActionSequence::onSchedule(int i, int w, double s, double f) {
  for (std::vector<SchedAction*>::iterator it = actions.begin() ; it != actions.end(); ++it) {
    (*it)->onSchedule(i, w, s, f);
EYRAUD-DUBOIS Lionel's avatar
EYRAUD-DUBOIS Lionel committed
29
  }
30 31 32 33 34
}
void ActionSequence::onTaskPush(int t) {
  for (std::vector<SchedAction*>::iterator it = actions.begin() ; it != actions.end(); ++it)
    (*it)->onTaskPush(t);
}
35
int ActionSequence::chooseTask(int wType, double now) {
36 37
  int tmp; 
  for (std::vector<SchedAction*>::iterator it = actions.begin() ; it != actions.end(); ++it)
38
    if( (tmp = (*it)->chooseTask(wType, now)) != SCHEDACTION_NONE) 
39
      return tmp; 
40
  return SCHEDACTION_NONE; 
41
}
EYRAUD-DUBOIS Lionel's avatar
EYRAUD-DUBOIS Lionel committed
42

43 44
/* ExportSchedule: exports a schedule */

EYRAUD-DUBOIS Lionel's avatar
EYRAUD-DUBOIS Lionel committed
45 46 47 48 49 50 51 52
ExportSchedule::ExportSchedule(ostream *stream, Instance* ins, bool header, string name) : output(stream), instance(ins), name(name) { 
  if(header) outputHeader(); 
}
ExportSchedule::ExportSchedule(Instance* ins, string name) 
  : instance(ins), name(name) {
}

void ExportSchedule::outputHeader() {
53
  if(name != "")
EYRAUD-DUBOIS Lionel's avatar
EYRAUD-DUBOIS Lionel committed
54
    *output << "sched ";
55 56 57
  *output << "Tid   worker taskType ";
  if(instance->taskIDs.size() > 0)
    *output << "JobId ";
EYRAUD-DUBOIS Lionel's avatar
EYRAUD-DUBOIS Lionel committed
58
  *output << "start duration end" << endl;
59
}
60

EYRAUD-DUBOIS Lionel's avatar
EYRAUD-DUBOIS Lionel committed
61
void ExportSchedule::changeName(string newName) {
62
  if((name == "" && newName != "")) {
EYRAUD-DUBOIS Lionel's avatar
EYRAUD-DUBOIS Lionel committed
63
    cerr << "ExportSchedule: Warning, adding a name after the start breaks the header" << endl;
64 65
  }
  if(name != "" && newName == "") newName = "NA";
66
  name = newName;
67 68
}

EYRAUD-DUBOIS Lionel's avatar
EYRAUD-DUBOIS Lionel committed
69
void ExportSchedule::onSchedule(int i, int w, double s, double f) {
70
  if(name != "")
71 72 73 74 75 76 77 78
    *output << name << " ";
  *output << i << " " << w << " ";
  if(instance->taskTypeNames.size() > 0)
    *output << instance->taskTypeNames[instance->taskTypes[i]];
  else
    *output << instance->taskTypes[i];
  if(instance->taskIDs.size() > 0)
    *output << " " << instance->taskIDs[i];
EYRAUD-DUBOIS Lionel's avatar
EYRAUD-DUBOIS Lionel committed
79
  *output << " " << s << " " << (f - s) << " " << f << endl;
80
}
EYRAUD-DUBOIS Lionel's avatar
EYRAUD-DUBOIS Lionel committed
81

82 83 84 85 86 87 88 89

/* ExportToFile */

ExportToFile::ExportToFile(string filename, Instance* ins, bool header, string name): ExportSchedule(ins, name), f(new ofstream(filename, ios::app)) {
  output = f;
  if(header) outputHeader(); 
}

90
ExportToFile::~ExportToFile() {
EYRAUD-DUBOIS Lionel's avatar
EYRAUD-DUBOIS Lionel committed
91 92 93
  f->close();
}

94 95 96 97 98 99 100 101
/* ExportToString */

ExportToString::ExportToString(Instance* ins, bool header, string name)
  : ExportSchedule(ins, name), f(new ostringstream()) {
  output = f;
  if(header) outputHeader(); 
}

EYRAUD-DUBOIS Lionel's avatar
EYRAUD-DUBOIS Lionel committed
102 103
string ExportToString::getResult() {
  return f->str(); 
EYRAUD-DUBOIS Lionel's avatar
EYRAUD-DUBOIS Lionel committed
104
}
105

106 107
/* ExportAlloc: exports in .rec format */

108 109
ExportAlloc::ExportAlloc(string filename, Instance* ins, bool submitOrder, bool outputType)
  : output(filename), instance(ins), submitOrder(submitOrder),  outputType(outputType) {
110 111 112 113
}

void ExportAlloc::onSchedule(int i, int w, double s, double f) {
  if(instance->taskIDs.size() > 0) {
114 115 116 117 118 119 120 121 122
    string id = instance->taskIDs[i];
    vector<string> parts;
    split(id, ':', parts);
    if(parts.size() == 1)
      output << (submitOrder ? "SubmitOrder: " : "JobId: ") << instance->taskIDs[i] << endl;
    else {
      output << (submitOrder ? "SubmitOrder: " : "JobId: ") << parts[0] << endl;
      output << "Tag:" << parts[1] << endl;
    }
123
  }
124
  output << "TaskId: " << i << endl;
125 126 127
  if(instance->taskTypeNames.size() > 0) {
    output << "TaskType: " << instance->taskTypeNames[instance->taskTypes[i]] << endl; 
  }
128 129 130

  int type = instance->getType(w);

131 132 133 134 135 136 137 138
  if (outputType && instance->workerIDs[type].size() > 1) {
    output << "Workers:";
    for(auto& i: instance->workerIDs[type])
      output << " " << i;
    output << endl;
  } else {
    output << "SpecificWorker: " << w << endl;
  }
139

140
  
141
  if(instance->workerNames.size() > 0) {
142
    output << "Architecture: "  << instance->workerNames[type] << endl;
143
  } else {
144
    output << "WorkerType: " << type << endl;
145 146 147 148 149 150 151 152
  }
  output << endl; 
}

ExportAlloc::~ExportAlloc() {
  output.close();
}

153 154
/* UtilAnalysis: compute stats about a schedule */

155

EYRAUD-DUBOIS Lionel's avatar
EYRAUD-DUBOIS Lionel committed
156 157 158
UtilAnalysis::UtilAnalysis(Instance* _ins, string saveFile) :
  ins(_ins), repartition(_ins->nbWorkerTypes, 
			 std::vector<int>(ins->nbTaskTypes, 0)), 
159
  output(saveFile, ios::app) {
EYRAUD-DUBOIS Lionel's avatar
EYRAUD-DUBOIS Lionel committed
160 161 162 163 164 165 166
  
  }
UtilAnalysis::~UtilAnalysis(){
  output.close(); 
}

void UtilAnalysis::onSchedule(int i, int w, double s, double f) {
167
  repartition[ins->getType(w)][ins->taskTypes[i]] += 1; 
EYRAUD-DUBOIS Lionel's avatar
EYRAUD-DUBOIS Lionel committed
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
}

std::vector<std::vector<int> > UtilAnalysis::getRepartition() {
  return repartition; 
}

void UtilAnalysis::reset() {
  for(auto &v: repartition)
    for(auto &n: v)
      n = 0; 
}

void UtilAnalysis::write(string prefix) {
  for(int i = 0; i < (int) repartition.size(); i++) {
    for(int j = 0; j < (int) repartition[i].size(); j++) {
      output << prefix << " " << i << " " << j << " " 
	     << repartition[i][j] << " " 
	     << repartition[i][j] * ins->execTimes[i][j] << endl;
    }
  }
  reset(); 
}
190

191
/* InternalShare: sharing schedules internally between algorithms */
192 193

InternalShare::InternalShare(string name, Instance* ins) : 
194 195 196 197
  shareName(name), instance(ins), allocation(ins->nbTasks, -1),
  worker(ins->nbTasks, -1), 
  startTimes(ins->nbTasks, -1),
  endTimes(ins->nbTasks, -1) {
198 199 200
}

void InternalShare::onSchedule(int i, int w, double s, double f) {
201 202 203
  allocation[i] = instance->getType(w);
  worker[i] = w; 
  startTimes[i] = s;
204
  endTimes[i] = f;
205 206 207
}

void InternalShare::finish() {
208 209 210 211
  instance->extraData.insert(shareName + "[worker]", new vector<int>(worker)); 
  instance->extraData.insert(shareName + "[alloc]", new vector<int>(allocation)); 
  instance->extraData.insert(shareName + "[start]", new vector<double>(startTimes)); 
  instance->extraData.insert(shareName + "[end]", new vector<double>(endTimes)); 
212
}
213

214 215
/* ExportBubble: deprecated */

216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
ExportBubble::ExportBubble(string filename, Instance* ins, int btt) : 
  InternalShare(filename, ins), output(filename), bubbleTaskType(btt) {
}

void ExportBubble::finish() {
  vector<double> bubbleStarts; 
  vector<int> topOrder = instance->getTopOrder(); 
  int nbBubbles; 
  for(int i = 0; i < instance->nbTasks; i++) {
    if(instance->taskTypes[topOrder[i]] == bubbleTaskType) 
      bubbleStarts.push_back(startTimes[topOrder[i]]);  
  }
  nbBubbles = bubbleStarts.size(); 
  for(int i = 0; i < instance->nbTasks; i++) {
    double s = startTimes[i], e = startTimes[i] + instance->execType(allocation[i], i);
    int firstBubble = -1, lastBubble = -1; 
    for(int j = 0; j < nbBubbles; j++) {
      if(s >= bubbleStarts[j]) firstBubble = j; 
      if(e >= bubbleStarts[j]) lastBubble = j; 
      else break; 
    }
    output << firstBubble << " " << lastBubble << " " << allocation[i] << endl; 
  }
  output.close(); 
}