9 #include <dpsim/ThreadListScheduler.h>
14 using namespace DPsim;
16 ThreadListScheduler::ThreadListScheduler(Int threads, String outMeasurementFile,
17 String inMeasurementFile,
18 Bool useConditionVariables)
20 mInMeasurementFile(inMeasurementFile) {}
22 void ThreadListScheduler::createSchedule(
const Task::List &tasks,
24 const Edges &outEdges) {
28 Scheduler::initMeasurements(ordered);
30 std::unordered_map<Task::Ptr, int64_t> priorities;
31 std::unordered_map<String, TaskTime::rep> measurements;
32 if (!mInMeasurementFile.empty()) {
36 for (
auto task : ordered) {
37 if (measurements.find(task->toString()) == measurements.end())
42 for (
auto task : ordered) {
43 measurements[task->toString()] = 1;
48 for (
auto it = ordered.rbegin(); it != ordered.rend(); ++it) {
51 if (outEdges.find(task) != outEdges.end()) {
52 for (
auto dep : outEdges.at(task)) {
53 if (priorities[dep] > maxLevel) {
54 maxLevel = priorities[dep];
58 priorities[task] = measurements.at(task->toString()) + maxLevel;
61 auto cmp = [&priorities](
const Task::Ptr &p1,
const Task::Ptr &p2) ->
bool {
62 return priorities[p1] < priorities[p2];
64 std::priority_queue<Task::Ptr, std::deque<Task::Ptr>, decltype(cmp)> queue(
66 for (
auto task : ordered) {
67 if (inEdges.find(task) == inEdges.end() || inEdges.at(task).empty()) {
74 std::vector<TaskTime::rep> totalTimes(mNumThreads, 0);
75 Edges inEdgesCpy = inEdges;
76 while (!queue.empty()) {
77 auto task = queue.top();
80 auto minIt = std::min_element(totalTimes.begin(), totalTimes.end());
81 Int minIdx =
static_cast<UInt
>(minIt - totalTimes.begin());
82 scheduleTask(minIdx, task);
83 totalTimes[minIdx] += measurements.at(task->toString());
85 if (outEdges.find(task) != outEdges.end()) {
86 for (
auto after : outEdges.at(task)) {
87 for (
auto edgeIt = inEdgesCpy[after].begin();
88 edgeIt != inEdgesCpy[after].end(); ++edgeIt) {
89 if (*edgeIt == task) {
90 inEdgesCpy[after].erase(edgeIt);
94 if (inEdgesCpy[after].empty() &&
95 std::find(ordered.begin(), ordered.end(), after) != ordered.end()) {
102 ThreadScheduler::finishSchedule(inEdges);
void topologicalSort(const CPS::Task::List &tasks, const Edges &inEdges, const Edges &outEdges, CPS::Task::List &sortedTasks)
Simple topological sort, filtering out tasks that do not need to be executed.
void readMeasurements(CPS::String filename, std::unordered_map< CPS::String, TaskTime::rep > &measurements)
Read measurement data from file to use it for the scheduling.
std::unordered_map< CPS::Task::Ptr, std::deque< CPS::Task::Ptr > > Edges