18#include <condition_variable>
22#include <unordered_map>
32 typedef std::unordered_map<CPS::Task::Ptr, std::deque<CPS::Task::Ptr>>
Edges;
34 typedef std::chrono::steady_clock::duration
TaskTime;
41 mSLog->set_pattern(
"[%L] %v");
50 const Edges &inEdges,
const Edges &outEdges) = 0;
90 const Edges &outEdges,
91 std::vector<CPS::Task::List> &levels);
102 std::unordered_map<CPS::String, TaskTime::rep> &measurements);
115 struct TaskMeasurement {
119 std::size_t count = 0;
121 std::unordered_map<CPS::Task *, TaskMeasurement> mMeasurements;
134 : mLimit(limit), mCount(0), mGeneration(0), mUseCondition(useCondition) {}
141 std::unique_lock<std::mutex> lk(mMutex);
142 Int gen = mGeneration;
144 if (mCount == mLimit) {
148 mCondition.notify_all();
151 while (gen == mGeneration)
155 Int gen = mGeneration.load(std::memory_order_acquire);
160 if (mCount.fetch_add(1, std::memory_order_acq_rel) == mLimit - 1) {
161 mCount.store(0, std::memory_order_relaxed);
162 mGeneration.fetch_add(1, std::memory_order_release);
164 while (mGeneration.load(std::memory_order_acquire) == gen)
176 std::unique_lock<std::mutex> lk(mMutex);
178 if (mCount == mLimit) {
182 mCondition.notify_all();
186 if (mCount.fetch_add(1, std::memory_order_acquire) == mLimit - 1) {
187 mCount.store(0, std::memory_order_relaxed);
188 mGeneration.fetch_add(1, std::memory_order_release);
197 std::atomic<Int> mCount;
199 std::atomic<Int> mGeneration;
203 std::condition_variable mCondition;
208 typedef std::shared_ptr<BarrierTask>
Ptr;
213 std::vector<Barrier *> mBarriers;
220 void inc() { mValue.fetch_add(1, std::memory_order_release); }
223 for (
Int spin = 0; mValue.load(std::memory_order_acquire) != value;
225 if ((spin & 63) == 63)
226 std::this_thread::yield();
231 std::atomic<Int> mValue;
AttributePointer< AttributeBase > Ptr
spdlog::level::level_enum Level
std::shared_ptr< spdlog::logger > Log
Tasks to be defined by every component.
std::vector< AttributeBase::Ptr > mAttributeDependencies
std::shared_ptr< Task > Ptr
Barrier()=delete
Constructor without parameters is forbidden.
Barrier(Int limit, Bool useCondition=false)
std::shared_ptr< BarrierTask > Ptr
void addBarrier(Barrier *b)
void execute(Real time, Int timeStepCount)
void execute(Real time, Int timeStepCount)
void resolveDeps(CPS::Task::List &tasks, Edges &inEdges, Edges &outEdges)
void writeMeasurements(CPS::String filename)
Write measurement data to file.
virtual Bool runSimulation(Real, Int, UInt)
Scheduler(CPS::Logger::Level logLevel=CPS::Logger::Level::off)
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.
static void levelSchedule(const CPS::Task::List &tasks, const Edges &inEdges, const Edges &outEdges, std::vector< CPS::Task::List > &levels)
CPS::Logger::Level mLogLevel
Log level.
void readMeasurements(CPS::String filename, std::unordered_map< CPS::String, TaskTime::rep > &measurements)
Read measurement data from file to use it for the scheduling.
static CPS::AttributeBase::Ptr external
TaskTime getAveragedMeasurement(CPS::Task::Ptr task)
void initMeasurements(const CPS::Task::List &tasks)
virtual void step(Real time, Int timeStepCount)=0
Performs a single simulation step.
std::unordered_map< CPS::Task::Ptr, std::deque< CPS::Task::Ptr > > Edges
virtual void createSchedule(const CPS::Task::List &tasks, const Edges &inEdges, const Edges &outEdges)=0
Creates the schedule for the given dependency graph.
void updateMeasurement(CPS::Task *task, TaskTime time)
virtual void stop()
Called on simulation stop to reliably clean up e.g. running helper threads.
std::chrono::steady_clock::duration TaskTime
Time measurement for the task execution.
CPS::Logger::Log mSLog
Logger.