DPsim
Loading...
Searching...
No Matches
ThreadScheduler.cpp
Go to the documentation of this file.
1/* Copyright 2017-2021 Institute for Automation of Complex Power Systems,
2 * EONERC, RWTH Aachen University
3 *
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at https://mozilla.org/MPL/2.0/.
7 *********************************************************************************/
8
10
11#include <iostream>
12
13using namespace CPS;
14using namespace DPsim;
15
16ThreadScheduler::ThreadScheduler(Int threads, String outMeasurementFile,
17 Bool useConditionVariable)
18 : mNumThreads(threads), mOutMeasurementFile(outMeasurementFile),
19 mStartBarrier(threads, useConditionVariable) {
20 if (threads < 1)
21 throw SchedulingException();
22 mTempSchedules.resize(threads);
23 mSchedules.resize(threads, nullptr);
24 mBatches.resize(threads);
25}
26
28 for (int i = 0; i < mNumThreads; i++)
29 delete[] mSchedules[i];
30}
31
33 mTempSchedules[thread].push_back(task);
34}
35
37 std::map<CPS::Task::Ptr, Counter *> counters;
38 for (int thread = 0; thread < mNumThreads; thread++) {
39 // std::cout << "Thread " << thread << std::endl;
40 // for (auto& entry : mSchedules[thread]) {
41 // Task* t = entry.task.get();
42 // char *refpos = reinterpret_cast<char*>(reinterpret_cast<void*>(t)) + sizeof(Task);
43 // void *ref = *(reinterpret_cast<void**>(refpos));
44 // std::cout << entry.task->toString() << " " << ref << std::endl;
45 // }
46 mSchedules[thread] = new ScheduleEntry[mTempSchedules[thread].size()];
47 for (size_t i = 0; i < mTempSchedules[thread].size(); i++) {
48 auto &task = mTempSchedules[thread][i];
49 mSchedules[thread][i].task = task.get();
50 counters[task] = &mSchedules[thread][i].endCounter;
51 }
52 }
53 for (int thread = 0; thread < mNumThreads; thread++) {
54 auto &batches = mBatches[thread];
55 batches.clear();
56 const size_t count = mTempSchedules[thread].size();
57 size_t begin = 0;
58 while (begin < count) {
59 size_t end = begin + 1;
60 while (end < count && mSchedules[thread][end].reqCounters.empty())
61 ++end;
62 batches.push_back({begin, end});
63 begin = end;
64 }
65 }
66 for (int thread = 0; thread < mNumThreads; thread++) {
67 for (size_t i = 0; i < mTempSchedules[thread].size(); i++) {
68 auto &task = mTempSchedules[thread][i];
69 if (inEdges.find(task) != inEdges.end()) {
70 for (auto req : inEdges.at(task)) {
71 mSchedules[thread][i].reqCounters.push_back(counters[req]);
72 }
73 }
74 }
75 }
76 for (int i = 1; i < mNumThreads; i++) {
77 mThreads.emplace_back(threadFunction, this, i);
78 }
79}
80
81void ThreadScheduler::step(Real time, Int timeStepCount) {
82 mTime = time;
83 mTimeStepCount = timeStepCount;
84 mStartBarrier.wait();
85 doStep(0);
86 // since we don't have a final BarrierTask, wait for all threads to finish
87 // their last task explicitly
88 for (int thread = 1; thread < mNumThreads; thread++) {
89 if (mTempSchedules[thread].size() != 0)
90 mSchedules[thread][mTempSchedules[thread].size() - 1].endCounter.wait(
91 mTimeStepCount + 1);
92 }
93}
94
96 if (!mThreads.empty()) {
97 mJoining = true;
98 mStartBarrier.wait();
99 for (size_t thread = 0; thread < mThreads.size(); thread++) {
100 mThreads[thread].join();
101 }
102 }
103 if (!mOutMeasurementFile.empty()) {
104 writeMeasurements(mOutMeasurementFile);
105 }
106}
107
108void ThreadScheduler::threadFunction(ThreadScheduler *sched, Int idx) {
109 while (true) {
110 sched->mStartBarrier.wait();
111 if (sched->mJoining)
112 return;
113
114 sched->doStep(idx);
115 }
116}
117
118void ThreadScheduler::doStep(Int thread) {
119 if (mOutMeasurementFile.empty()) {
120 for (const auto &batch : mBatches[thread]) {
121 for (size_t i = batch.begin; i != batch.end; ++i) {
122 ScheduleEntry *entry = &mSchedules[thread][i];
123 for (Counter *counter : entry->reqCounters)
124 counter->wait(mTimeStepCount + 1);
125 entry->task->executeFast(mTime, mTimeStepCount);
126 entry->endCounter.inc();
127 }
128 }
129 } else {
130 for (size_t i = 0; i != mTempSchedules[thread].size(); i++) {
131 ScheduleEntry *entry = &mSchedules[thread][i];
132 for (Counter *counter : entry->reqCounters)
133 counter->wait(mTimeStepCount + 1);
134 auto start = std::chrono::steady_clock::now();
135 entry->task->executeFast(mTime, mTimeStepCount);
136 auto end = std::chrono::steady_clock::now();
137 updateMeasurement(entry->task, end - start);
138 entry->endCounter.inc();
139 }
140 }
141}
std::shared_ptr< Task > Ptr
Definition Task.h:27
void wait(Int value)
Definition Scheduler.h:222
void writeMeasurements(CPS::String filename)
Write measurement data to file.
Definition Scheduler.cpp:44
std::unordered_map< CPS::Task::Ptr, std::deque< CPS::Task::Ptr > > Edges
Definition Scheduler.h:32
void updateMeasurement(CPS::Task *task, TaskTime time)
Definition Scheduler.cpp:31
virtual void stop()
Called on simulation stop to reliably clean up e.g. running helper threads.
void step(Real time, Int timeStepCount)
Performs a single simulation step.
ThreadScheduler(Int threads, String outMeasurementFile, Bool useConditionVariable)
void scheduleTask(int thread, CPS::Task::Ptr task)
void finishSchedule(const Edges &inEdges)
CPS::Real Real
Definition Definitions.h:18
CPS::String String
Definition Definitions.h:20
CPS::Int Int
Definition Definitions.h:22
CPS::Bool Bool
Definition Definitions.h:21