schedAction.cpp 7.14 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);
}
EYRAUD-DUBOIS Lionel's avatar
EYRAUD-DUBOIS Lionel committed
35

36 37
/* ExportSchedule: exports a schedule */

EYRAUD-DUBOIS Lionel's avatar
EYRAUD-DUBOIS Lionel committed
38 39 40 41 42 43 44 45
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() {
46
  if(name != "")
EYRAUD-DUBOIS Lionel's avatar
EYRAUD-DUBOIS Lionel committed
47
    *output << "sched ";
48 49
  *output << "Tid   worker taskType ";
  if(instance->taskIDs.size() > 0)
50
    *output << (submitOrder ? "SubmitOrder " : "JobId ");
EYRAUD-DUBOIS Lionel's avatar
EYRAUD-DUBOIS Lionel committed
51
  *output << "start duration end" << endl;
52
}
53

EYRAUD-DUBOIS Lionel's avatar
EYRAUD-DUBOIS Lionel committed
54
void ExportSchedule::changeName(string newName) {
55
  if((name == "" && newName != "")) {
EYRAUD-DUBOIS Lionel's avatar
EYRAUD-DUBOIS Lionel committed
56
    cerr << "ExportSchedule: Warning, adding a name after the start breaks the header" << endl;
57 58
  }
  if(name != "" && newName == "") newName = "NA";
59
  name = newName;
60 61
}

EYRAUD-DUBOIS Lionel's avatar
EYRAUD-DUBOIS Lionel committed
62
void ExportSchedule::onSchedule(int i, int w, double s, double f) {
63
  if(name != "")
64 65 66 67 68 69 70 71
    *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
72
  *output << " " << s << " " << (f - s) << " " << f << endl;
73
}
EYRAUD-DUBOIS Lionel's avatar
EYRAUD-DUBOIS Lionel committed
74

75 76 77

/* ExportToFile */

78 79
ExportToFile::ExportToFile(string filename, Instance* ins, bool header, string name, bool submitorder): ExportSchedule(ins, name), f(new ofstream(filename, ios::app)) {
  ExportSchedule::submitOrder = submitorder;
80 81 82 83
  output = f;
  if(header) outputHeader(); 
}

84
ExportToFile::~ExportToFile() {
EYRAUD-DUBOIS Lionel's avatar
EYRAUD-DUBOIS Lionel committed
85 86 87
  f->close();
}

88 89 90 91 92 93 94 95
/* 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
96 97
string ExportToString::getResult() {
  return f->str(); 
EYRAUD-DUBOIS Lionel's avatar
EYRAUD-DUBOIS Lionel committed
98
}
99

100 101
/* ExportAlloc: exports in .rec format */

102 103 104 105 106 107 108 109 110 111
ExportAlloc::ExportAlloc(string filename, Instance* ins, bool submitOrder, bool outputType, bool workerOrder)
  : output(filename), instance(ins), submitOrder(submitOrder),  outputType(outputType), workerOrder(workerOrder) {
  if (workerOrder) {
    if (!outputType) {
      workerTaskCount.resize(instance->totalWorkers, 0); 
    } else {
      cerr << "Export: workerOrder is not compatible with outputType, ignoring." << endl;
      workerOrder = false; 
    }
  }
112 113 114 115
}

void ExportAlloc::onSchedule(int i, int w, double s, double f) {
  if(instance->taskIDs.size() > 0) {
116 117 118 119 120 121 122 123 124
    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;
    }
125
  }
126
  output << "TaskId: " << i << endl;
127 128 129
  if(instance->taskTypeNames.size() > 0) {
    output << "TaskType: " << instance->taskTypeNames[instance->taskTypes[i]] << endl; 
  }
130

131 132
  int index; 
  int type = instance->getType(w, &index);
133

134 135 136 137 138 139
  if (outputType && instance->workerIDs[type].size() > 1) {
    output << "Workers:";
    for(auto& i: instance->workerIDs[type])
      output << " " << i;
    output << endl;
  } else {
140
    output << "SpecificWorker: " << instance->workerIDs[type][index] << endl;
141
    if (workerOrder) {
142
      output << "Workerorder: " << workerTaskCount[w]+1 << endl;
143 144
      ++workerTaskCount[w]; 
    }
145
  }
146

147
  
148
  if(instance->workerNames.size() > 0) {
149
    output << "Architecture: "  << instance->workerNames[type] << endl;
150
  } else {
151
    output << "WorkerType: " << type << endl;
152 153 154 155 156 157 158 159
  }
  output << endl; 
}

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

160 161
/* UtilAnalysis: compute stats about a schedule */

162

EYRAUD-DUBOIS Lionel's avatar
EYRAUD-DUBOIS Lionel committed
163 164 165
UtilAnalysis::UtilAnalysis(Instance* _ins, string saveFile) :
  ins(_ins), repartition(_ins->nbWorkerTypes, 
			 std::vector<int>(ins->nbTaskTypes, 0)), 
166
  output(saveFile, ios::app) {
EYRAUD-DUBOIS Lionel's avatar
EYRAUD-DUBOIS Lionel committed
167 168 169 170 171 172 173
  
  }
UtilAnalysis::~UtilAnalysis(){
  output.close(); 
}

void UtilAnalysis::onSchedule(int i, int w, double s, double f) {
174
  repartition[ins->getType(w)][ins->taskTypes[i]] += 1; 
EYRAUD-DUBOIS Lionel's avatar
EYRAUD-DUBOIS Lionel committed
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
}

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(); 
}
197

198
/* InternalShare: sharing schedules internally between algorithms */
199 200

InternalShare::InternalShare(string name, Instance* ins) : 
201 202 203 204
  shareName(name), instance(ins), allocation(ins->nbTasks, -1),
  worker(ins->nbTasks, -1), 
  startTimes(ins->nbTasks, -1),
  endTimes(ins->nbTasks, -1) {
205 206 207
}

void InternalShare::onSchedule(int i, int w, double s, double f) {
208 209 210
  allocation[i] = instance->getType(w);
  worker[i] = w; 
  startTimes[i] = s;
211
  endTimes[i] = f;
212 213 214
}

void InternalShare::finish() {
215 216 217 218
  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)); 
219
}
220

221 222
/* ExportBubble: deprecated */

223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
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(); 
}