summaryrefslogtreecommitdiff
path: root/Simulator/include/simulation/schedulers/ShortestRemainingTimeScheduler.h
blob: 15265985455f1ac42220acde310ce4b814b4a764 (plain)
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
#pragma once
#include "Scheduler.h"
#include <algorithm>

namespace Simulation
{
	class ShortestRemainingTimeScheduler : public Scheduler
	{
	protected:
		~ShortestRemainingTimeScheduler()
		{
		}

	public:
		/*
			Distribute workloads according to the srtf principle
		*/
		void schedule(std::vector<std::reference_wrapper<Modeling::Machine>>& machines, std::vector<Workload*> workloads) override
		{
			if (workloads.size() == 0)
				return;

			std::sort(
				workloads.begin(), 
				workloads.end(), 
				[](Workload* a, Workload* b) -> bool {
					return a->getRemainingOperations() < b->getRemainingOperations();
				}
			);

			int taskIndex = 0;

			std::for_each(
				machines.begin(),
				machines.end(),
				[&workloads, &taskIndex](Modeling::Machine& machine) {
					while (!workloads.at(taskIndex)->dependencyFinished)
						taskIndex = (++taskIndex) % workloads.size();

					machine.giveTask(workloads.at(taskIndex));
					taskIndex = (++taskIndex) % workloads.size();
				}
			);
		}
	};
}