2023-12-03 19:43:41 +03:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#include <map>
|
|
|
|
|
#include <set>
|
|
|
|
|
#include <vector>
|
|
|
|
|
#include <queue>
|
2023-09-17 22:13:42 +03:00
|
|
|
#include "File.h"
|
2023-12-03 19:43:41 +03:00
|
|
|
#include "Task.h"
|
|
|
|
|
#include "Array.h"
|
2023-09-17 22:13:42 +03:00
|
|
|
|
|
|
|
|
enum SupervisorState {
|
|
|
|
|
WorkspacesCreation, //0
|
2023-12-03 19:43:41 +03:00
|
|
|
Preparation, //1
|
|
|
|
|
Execution, //2
|
2023-09-17 22:13:42 +03:00
|
|
|
Archivation, //3
|
2023-12-03 19:43:41 +03:00
|
|
|
End //4
|
2023-09-17 22:13:42 +03:00
|
|
|
};
|
2023-12-03 19:43:41 +03:00
|
|
|
|
2023-09-17 22:13:42 +03:00
|
|
|
template <class T>
|
2023-12-03 19:43:41 +03:00
|
|
|
class Supervisor : public Array <T> {
|
2023-09-17 22:13:42 +03:00
|
|
|
protected:
|
2023-12-03 19:43:41 +03:00
|
|
|
SupervisorState state;
|
|
|
|
|
public:
|
|
|
|
|
virtual String getStatePrefix() {
|
|
|
|
|
return String("");
|
|
|
|
|
}
|
|
|
|
|
String printState() {
|
|
|
|
|
switch (state) {
|
|
|
|
|
case WorkspacesCreation:
|
|
|
|
|
return String("WorkspacesCreation");
|
|
|
|
|
case Preparation:
|
|
|
|
|
return String("Preparation");
|
|
|
|
|
case Execution:
|
|
|
|
|
return String("Execution");
|
|
|
|
|
case Archivation:
|
|
|
|
|
return String("Archivation");
|
|
|
|
|
case End:
|
|
|
|
|
return String("End");
|
|
|
|
|
default:
|
|
|
|
|
return "?";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
//-
|
|
|
|
|
void print() {
|
|
|
|
|
for (auto& elem : this->getElements())
|
|
|
|
|
elem->print();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void init(const char* fileName, int recordSize) {
|
|
|
|
|
state = WorkspacesCreation;
|
|
|
|
|
File* packedTasks = new File(fileName);
|
|
|
|
|
Text* lines = packedTasks->readLines();
|
|
|
|
|
|
|
|
|
|
const long length = lines->getLength() / recordSize;
|
|
|
|
|
int offset = 0;
|
|
|
|
|
for (int i = 0; i < length; ++i) {
|
|
|
|
|
this->add(new T(lines, offset));
|
|
|
|
|
offset += recordSize;
|
|
|
|
|
}
|
|
|
|
|
delete packedTasks;
|
|
|
|
|
delete lines;
|
|
|
|
|
}
|
|
|
|
|
void changeState() {
|
|
|
|
|
switch (this->state) {
|
|
|
|
|
case WorkspacesCreation:
|
|
|
|
|
this->state = Preparation;
|
|
|
|
|
saveState();
|
|
|
|
|
break;
|
|
|
|
|
case Preparation:
|
|
|
|
|
this->state = Execution;
|
|
|
|
|
saveState();
|
|
|
|
|
break;
|
|
|
|
|
case Execution:
|
|
|
|
|
Finalize();
|
|
|
|
|
this->state = End;
|
|
|
|
|
saveState();
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
this->state = End;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
void DoWithSchedule(int maxKernels) {
|
|
|
|
|
saveState();
|
|
|
|
|
// подготовка тестов
|
|
|
|
|
while (this->state != Execution) {
|
|
|
|
|
for (auto& task : this->getElements()) {
|
|
|
|
|
switch (this->state) {
|
|
|
|
|
case WorkspacesCreation:
|
|
|
|
|
if (task->getState() == Waiting) {
|
|
|
|
|
task->createWorkspace();
|
|
|
|
|
task->setState(WorkspaceCreated);
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case Preparation:
|
|
|
|
|
if (task->getState() == WorkspaceCreated) {
|
|
|
|
|
task->prepareWorkspace();
|
|
|
|
|
task->createLaunchScript();
|
|
|
|
|
task->setState(WorkspaceReady);
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
//printf("id = %ld; state = %d\n", task->getId(), task->getState());
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
changeState();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
map<int, queue<T*>, std::greater<int>> sortedByKernelNeeds;
|
|
|
|
|
|
|
|
|
|
long activeTasks = 0;
|
|
|
|
|
long done = 0;
|
|
|
|
|
for (auto& task : this->getElements()) {
|
|
|
|
|
if (task->getState() == WorkspaceReady) {
|
|
|
|
|
activeTasks++;
|
|
|
|
|
sortedByKernelNeeds[task->getKernels()].push(task);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
printf("total tasks count = %ld, active task count %ld, maxkernels %d\n", this->getLength(), activeTasks, maxKernels);
|
|
|
|
|
|
|
|
|
|
int busyKernels = 0;
|
|
|
|
|
set<T*> activeTaskSet;
|
|
|
|
|
bool ignoreCheck = true;
|
|
|
|
|
|
2023-12-03 22:11:15 +03:00
|
|
|
String pathRes("results");
|
|
|
|
|
Utils::Mkdir(pathRes);
|
2023-12-04 01:43:08 +03:00
|
|
|
//string buf;
|
2023-12-03 22:11:15 +03:00
|
|
|
|
2023-12-03 19:43:41 +03:00
|
|
|
while (activeTasks) {
|
|
|
|
|
long oldActiveTasks = activeTasks;
|
|
|
|
|
vector<int> emptyKeys;
|
|
|
|
|
|
|
|
|
|
//ставим задачи от больших к меньшему по ядрам
|
|
|
|
|
for (auto& elem : sortedByKernelNeeds) {
|
|
|
|
|
int freeKernels = maxKernels - busyKernels;
|
|
|
|
|
int kernelsNeeded = elem.first;
|
|
|
|
|
|
|
|
|
|
while (kernelsNeeded <= freeKernels && elem.second.size()) {
|
|
|
|
|
T* task = elem.second.front();
|
|
|
|
|
elem.second.pop();
|
|
|
|
|
|
|
|
|
|
activeTaskSet.insert(task);
|
|
|
|
|
task->Start(ignoreCheck);
|
|
|
|
|
printf("start task with %d kernels and id %ld\n", task->getKernels(), task->getId());
|
|
|
|
|
|
|
|
|
|
busyKernels += task->getKernels();
|
|
|
|
|
freeKernels = maxKernels - busyKernels;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (elem.second.size() == 0)
|
|
|
|
|
emptyKeys.push_back(kernelsNeeded);
|
|
|
|
|
|
|
|
|
|
//если ядер не осталось, то нет смысла дальше смотреть
|
|
|
|
|
if (freeKernels == 0)
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// очищаем от пустых ключей
|
|
|
|
|
for (auto& empty : emptyKeys)
|
|
|
|
|
sortedByKernelNeeds.erase(empty);
|
|
|
|
|
|
|
|
|
|
// проверяем нет ли завершившихся задач
|
|
|
|
|
for (auto it = activeTaskSet.begin(); it != activeTaskSet.end(); )
|
|
|
|
|
{
|
|
|
|
|
T* task = *(it);
|
|
|
|
|
|
|
|
|
|
if (task->Check()) {
|
|
|
|
|
it++;
|
|
|
|
|
activeTaskSet.erase(task);
|
|
|
|
|
activeTasks--;
|
|
|
|
|
done++;
|
|
|
|
|
busyKernels -= task->getKernels();
|
|
|
|
|
printf(" done task with %d kernels and id %ld\n", task->getKernels(), task->getId());
|
|
|
|
|
|
2023-12-04 01:43:08 +03:00
|
|
|
//buf += to_string(task->getId()) + " " + string(task->printState().getCharArray()) + " " + to_string(task->getTotalTime()) + "\n";
|
2023-12-03 22:11:15 +03:00
|
|
|
task->copyResults(pathRes);
|
2023-12-03 19:43:41 +03:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
it++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (oldActiveTasks != activeTasks)
|
|
|
|
|
printf("done %ld / %ld\n", done, this->getLength());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
changeState();
|
2023-12-03 22:11:15 +03:00
|
|
|
|
2023-12-04 01:43:08 +03:00
|
|
|
//String outFile(pathRes + "/info.txt");
|
|
|
|
|
//File tmp(outFile, String(buf.c_str()));
|
2023-12-03 22:11:15 +03:00
|
|
|
|
|
|
|
|
Utils::ZipFolder(pathRes, pathRes + ".zip");
|
2023-12-03 19:43:41 +03:00
|
|
|
}
|
|
|
|
|
|
2023-12-03 22:11:15 +03:00
|
|
|
virtual void Finalize() { }
|
2023-12-03 19:43:41 +03:00
|
|
|
|
|
|
|
|
void saveState() {
|
|
|
|
|
Utils::Sleep(1); //чтобы не было одинаковых по дате файлов.
|
|
|
|
|
String stateFile = packageWorkspace + "/state/" + getStatePrefix() + printState();
|
|
|
|
|
//printf("stateFile=<%s>\n", stateFile.getCharArray());
|
2023-12-03 22:11:15 +03:00
|
|
|
File tmp(stateFile, Utils::getDate());
|
2023-12-03 19:43:41 +03:00
|
|
|
}
|
2023-09-17 22:13:42 +03:00
|
|
|
};
|