-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJob.cpp
More file actions
371 lines (335 loc) · 10.9 KB
/
Job.cpp
File metadata and controls
371 lines (335 loc) · 10.9 KB
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
35
36
37
38
39
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
/*
* File: Job.cpp
* Author: alex
*
* Created on 20 gennaio 2013, 14.18
*/
# include "Job/Job.h"
#include "master.h"
#include "slave.h"
using namespace std;
std::map<const std::string, tache*> *Job::tMap;
std::map<const int, std::vector<string>* > *Job::FileCache;
std::deque<tache*> *Job::tAvailable;
std::deque<tache*> *Job::tWaiting;
Job::Job() {
Job::nTaches = 1;
tMap = new std::map<const std::string, tache*>;
tAvailable = new std::deque<tache*>;
Job::FileCache = new std::map<const int, std::vector<string>* >;
}
Job::Job(const Job& orig) {
}
Job::~Job() {
}
void Job::createNewJob(std::string name){
std::fstream myfile(name.c_str(), ios::in);
string line,firstLine,secondLine;
int dimension;
int i=0;
// each task is represented on two different lines
// the first one represents name : \t dependencies
// the second one the proper command to execute
// read the line about all different task
// HERE FIRST LINE
if (myfile.good()){
getline(myfile,line);
vector<string> tokenFirst = tokenize(line," ");
this->id=Job::getNewId();
vector<string> tokens = tokenize(tokenFirst[0],":");
cout << tokens[0] << " \n";
this->name = tokens[0];
cout << this->name << "\n";
tokenFirst.erase(tokenFirst.begin());
dimension = tokenFirst.size();
while (i != dimension){
string dep = tokenFirst[i];
cout << dep << "\n";
this->dependencies.push_back(dep);
i++;
}
// delete(&name);
// delete(&tokens);
}
// HERE SECOND LINE
if (myfile.good()){
//getline(myfile,this->finalizeCommand);
getline(myfile,line);
// if (line.substr(0).compare("\t")){
// this->command=line.substr(1,line.length());
// }
// else {
this->command = line;
// }
this->completed=false;
cout << this->command;
}
//TachePair pair(this->name,this);
string toAdd = this->name;
addTacheToMap(this);
this->init = false;
this->done = false;
//Read the subtaches
while(myfile.good()){
getline(myfile,line); //first line of the tache
if(line.length() == 0){ // Skip any blank lines
continue;
}
else if(line[0] == '#'){ // Skip any comment lines
continue;
}
else if(line.compare("clean:") == 0){
getline(myfile,this->finalizeCommand);
}
else if(line.compare("check:") == 0){
getline(myfile,this->testCommand);
}
else {
getline(myfile,secondLine);
tache* toInsert=createNewTache(line,secondLine);
Job::addTacheToMap(toInsert);
putInWaiting(toInsert);
this->nTaches++;
}
}
putInWaiting(this);
}
void Job::addTacheToMap(tache* tache1){
Job::tMap->insert(Mappair(tache1->name,tache1));
//cout << "add to the map another tache " << tache1->name << "\n";
}
bool Job::testJobDeps(){
bool result;
long i=0;
if (tWaiting->size() == 0){
return false;
}
long size = tWaiting->size();
while (i<size){
tache *toEvaluate = tWaiting->front();
tWaiting->pop_front();
if (toEvaluate->completed == false){
if (toEvaluate->testTacheDeps()){
scheduleNewTache(toEvaluate);
}
else {
putInWaiting(toEvaluate);
}
}
i++;
}
return true;
}
tache* Job::getNewTache(){
tache *toReturn;
if (tAvailable->size() == 0 ){
toReturn = NULL;
}
else {
toReturn = tAvailable->front();
tAvailable->pop_front();
}
return toReturn;
}
void Job::scheduleNewTache(tache* toAdd){
if (tAvailable == NULL){
tAvailable = new std::deque<tache*>;
}
tAvailable->push_back(toAdd);
}
tache* Job::getWaitingTache(){
tache *toReturn;
if (tWaiting->front() == NULL ){
toReturn = NULL;
}
else {
toReturn = tWaiting->front();
tWaiting->pop_front();
}
return toReturn;
}
void Job::putInWaiting(tache* toAdd){
if (tWaiting == NULL){
tWaiting = new std::deque<tache*>;
}
tWaiting->push_back(toAdd);
}
bool Job::run(const long id, const long p){
int total = 0;
int totalReceived=0;
int target_host=1;
string mapping[p];
int busy[p];
int candidate;
memset(busy,0,p);
MPI::Status status;
cout << "tAvailable status: " <<tAvailable->size() << "\n";
cout << "nTaches status: " << this->nTaches << "\n";
cout << "p status: " << p << "\n";
while(target_host<p){
tache* toRun = getNewTache();
if (toRun == NULL){
cout << "Unexpected behaviour\n";
return false;
}
// cout << "SENDER: tAvailable status: " <<tAvailable->size() << "\n";
// cout << "SENDER: total status: " << total << "\n";
// cout << "SENDER: totalReceived status: " << totalReceived << "\n";
// cout << "SENDER: ntaches status: " << this->nTaches << "\n";
// cout << "SENDER:New Tache taken " << toRun->name << " \n";
// cout << "SENDER: Scheduling " << toRun->name << " to machine " << target_host << "\n";
toRun->sendTache(target_host,false);
mapping[target_host]=toRun->name;
master::SetBusy(target_host);
target_host++;
total++;
}
while( totalReceived < this->nTaches-1){
// if(totalReceived < total && totalReceived < nTaches){
// cout << "SENDER: tAvailable status: " <<tAvailable->size() << "\n";
// cout << "SENDER: total status: " << total << "\n";
// cout << "SENDER: totalReceived status: " << totalReceived << "\n";
// cout << "SENDER: ntaches status: " << this->nTaches << "\n";
if (MPI::COMM_WORLD.Iprobe(MPI::ANY_SOURCE,MPI::ANY_TAG,status)){
int tag = status.Get_tag();
int source = status.Get_source();
int dim = status.Get_count(MPI::CHAR);
// cout << "SENDER: Receiving results from" << source <<" \n";
if (tag != RESULT){
cout << "Protocol error!";
return false;
}
else {
// cout << "SENDER: Completing tache " << tMap->at(mapping[source])->name << "\n";
tache *t = tMap->at(mapping[source]);
t->completed=true;
char *buff=(char*)malloc(sizeof(char)*dim+1);
MPI::COMM_WORLD.Recv(buff,dim,MPI::CHAR,MPI::ANY_SOURCE,MPI::ANY_TAG);
savefile(buff,t->name);
this->notifyDependant(t);
// if ( tAvailable->size() < p){
//cout << "Refill 1\n" ;
// }
totalReceived++;
master::SetFree(source);
free(buff);
}
if (tAvailable->size() == 0){
this->testJobDeps();
}
//
tache* toRun;
if ( tAvailable->size() > 0){
toRun = getNewTache();
toRun->sendTache(source,false);
mapping[source]=toRun->name;
master::SetBusy(source);
total++;
}
}
// if ( tAvailable->size() < 3*p){
//cout << "Refill 2\n" ;
//this->testJobDeps();
// }
// while (tAvailable->size() > 0){
// tache* toRun = getNewTache();
// if (toRun == NULL){
// cout << "Unexpected behaviour\n";
// return false;
// }
// if ((candidate = master::GetFree()) == 0 ){
// break;
// }
// toRun->sendTache(candidate,false);
// mapping[candidate]=toRun->name;
// master::SetBusy(candidate);
// total++;
// }
}
this->finalizeCommand;
cout << "The job is done! Bye!";
this->signalEnd(p,id);
}
bool Job::finalize(){
system(this->finalizeCommand.c_str());
return true;
}
void Job::signalEnd(const long p,const long id){
int i=1;
string message("die");
while(i<p){
sendData(message,i,DIE);
i++;
}
};
void Job::ComputeDependant(){
map<string, tache*>::iterator p;
vector<string>::iterator it;
//cout << "Map DIMENSION " << tMap->size() << "\n";
for(p = tMap->begin(); p != tMap->end(); p++) {
for(it = p->second->dependencies.begin(); it != p->second->dependencies.end(); it++) {
if (tMap->count(*it) == 0 ){
p->second->TobeTaches--;
// cout << p->first << "\n";
// cout << "ToBe DIMENSION " << p->second->TobeTaches << "\n";
if (p->second->TobeTaches == 0){
scheduleNewTache(p->second);
}
continue;
}
tMap->at(*it)->dependant.push_back(p->first);
// cout << p->first << "é dipendente da" << *it << "\n";
}
}
}
//#ifdef LOCAL
// while(total < this->nTaches){
// tache* toRun = getNewTache();
// toRun->run();
// toRun->completed=true;
//#endif LOCAL
//#ifdef MPI
bool Job::CheckPresenceOnHost(int target_host,string file){
//cout << "SENDER: Cache result for "<< target_host << "\n";
if (FileCache->count(target_host) == 0){
vector<string> *listFile = new vector<string>;
listFile->push_back(file);
FileCache->insert(pair<int, vector<string>* >(target_host, listFile));
return false;
}
else {
vector<string> *listFile = FileCache->at(target_host);
for (std::vector<string>::iterator it = listFile->begin() ; it != listFile->end(); ++it){
if (file.compare(*it) == 0){
return true;
}
}
listFile->push_back(file);
//map.insert(pair<int, vector<string>* >(target_host, listFile));
return false;
}
}
void Job::notifyDependant(tache *t){
vector<string>::iterator it;
//cout << "tache name " << t->name << "\n";
for(it = t->dependant.begin(); it != t->dependant.end(); it++) {
//cout << "Mes dependants " << (*it) << "\n";
}
for(it = t->dependant.begin(); it != t->dependant.end(); it++) {
if (tMap->count(*it) == 0 ){
continue;
}
tMap->at(*it)->TobeTaches--;
if (tMap->at(*it)->TobeTaches == 0){
Job::scheduleNewTache(tMap->at(*it));
}
}
}
void Job::checkStarters(){
map<string,tache*>::iterator it;
for(it =tMap->begin(); it != tMap->end(); it++) {
if ((*it).second->TobeTaches == 0 ){
cout << "I'm ready from the begin " << (*it).second->name <<"!\n";
}
}
}