summaryrefslogtreecommitdiff
path: root/Simulator/include/simulation/schedulers/ShortestRemainingTimeScheduler.h
blob: 51673fb5e7f0eef03152f952d6b26f82939ef966 (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
47
48
49
50
51
52
#pragma once
#include "Scheduler.h"
#include <algorithm>

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

	public:
		/**
		* \brief 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;

			for (auto workload : workloads)
			{
				workload->setCoresUsed(0);
			}

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

			int taskIndex = 0;

			for (auto machine : machines)
			{
				while (!workloads.at(taskIndex)->dependencyFinished)
					taskIndex = (++taskIndex) % workloads.size();

				machine.get().giveTask(workloads.at(taskIndex));
				workloads.at(taskIndex)->setCoresUsed(
					workloads.at(taskIndex)->getCoresUsed() + machine.get().getNumberOfCores()
				);

				taskIndex = (++taskIndex) % workloads.size();
			}
		}
	};
}