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.26 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 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
ExportAlloc::ExportAlloc(string filename, Instance* ins) : output(filename), instance(ins) {
}

void ExportAlloc::onSchedule(int i, int w, double s, double f) {
  if(instance->taskIDs.size() > 0) {
    output << "JobId: " << instance->taskIDs[i] << endl; 
  } else {
    output << "TaskId: " << i << endl; 
  }
  if(instance->taskTypeNames.size() > 0) {
    output << "TaskType: " << instance->taskTypeNames[instance->taskTypes[i]] << endl; 
  }
  if(instance->workerNames.size() > 0) {
    output << "Architecture: "  << instance->workerNames[instance->getType(w)] << endl; 
  } else {
    output << "WorkerType: " << instance->getType(w) << endl; 
  }
  output << endl; 
}

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

132 133
/* UtilAnalysis: compute stats about a schedule */

134

EYRAUD-DUBOIS Lionel's avatar
EYRAUD-DUBOIS Lionel committed
135 136 137
UtilAnalysis::UtilAnalysis(Instance* _ins, string saveFile) :
  ins(_ins), repartition(_ins->nbWorkerTypes, 
			 std::vector<int>(ins->nbTaskTypes, 0)), 
138
  output(saveFile, ios::app) {
EYRAUD-DUBOIS Lionel's avatar
EYRAUD-DUBOIS Lionel committed
139 140 141 142 143 144 145
  
  }
UtilAnalysis::~UtilAnalysis(){
  output.close(); 
}

void UtilAnalysis::onSchedule(int i, int w, double s, double f) {
146
  repartition[ins->getType(w)][ins->taskTypes[i]] += 1; 
EYRAUD-DUBOIS Lionel's avatar
EYRAUD-DUBOIS Lionel committed
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
}

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

170
/* InternalShare: sharing schedules internally between algorithms */
171 172

InternalShare::InternalShare(string name, Instance* ins) : 
173 174 175 176
  shareName(name), instance(ins), allocation(ins->nbTasks, -1),
  worker(ins->nbTasks, -1), 
  startTimes(ins->nbTasks, -1),
  endTimes(ins->nbTasks, -1) {
177 178 179
}

void InternalShare::onSchedule(int i, int w, double s, double f) {
180 181 182
  allocation[i] = instance->getType(w);
  worker[i] = w; 
  startTimes[i] = s;
183
  endTimes[i] = f;
184 185 186
}

void InternalShare::finish() {
187 188 189 190
  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)); 
191
}
192

193 194
/* ExportBubble: deprecated */

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