Skip to content

Commit

Permalink
Scheduling tasks for processors
Browse files Browse the repository at this point in the history
  • Loading branch information
Wojtek-Rokicki committed Jan 22, 2022
1 parent 20cd8b1 commit da0f050
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 2 deletions.
Binary file added __pycache__/problem.cpython-38.pyc
Binary file not shown.
Binary file added __pycache__/solver.cpython-38.pyc
Binary file not shown.
3 changes: 2 additions & 1 deletion main.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@

dag_factory = problem.DagFactory(3, 5, 4, 6, 40)
dag = dag_factory.create_dag()
my_solver = solver.QlHeftSolver(1000, dag, 0, 0)
processors = [solver.Processor(5), solver.Processor(1), solver.Processor(3)]
my_solver = solver.QlHeftSolver(10000, dag, 0, 0, processors)
my_solver.solve()
57 changes: 56 additions & 1 deletion solver.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
from concurrent.futures import process
import numpy as np

import problem

np.random.seed(1)

class Processor:
def __init__(self, speed):
self.speed = speed
self.tasks = []
self.tasks_starts = []

class QlHeftSolver:
def __init__(self, max_iter, dag: problem.Dag, alfa, discount):
def __init__(self, max_iter, dag: problem.Dag, alfa, discount, processors: list):
self.max_iter = max_iter
self.problem = dag
self.alfa = alfa
self.discount = discount
self.processors = processors
self.mean_processors_speed = np.mean([i.speed for i in processors])

def __create_ranku_array(self) -> np.ndarray:
ranku_array = np.zeros(len(self.problem.W))
Expand Down Expand Up @@ -91,6 +99,51 @@ def __get_task_order(self, Q) -> list:
possible_tasks.append(task)
return task_order

def __ft(self, task):
for p in self.processors:
if task in p.tasks:
t_id = p.tasks.index(task)
if len(p.tasks_starts) == 0:
return self.problem.W[task]*self.mean_processors_speed/p.speed
return p.tasks_starts[t_id] + self.problem.W[task]*self.mean_processors_speed/p.speed

def __at(self, processor):
if len(processor.tasks) == 0:
return 0
return processor.tasks_starts[-1] + self.problem.W[processor.tasks[-1]]*self.mean_processors_speed/processor.speed

def __est(self, task, processor):
avaliable_predecessors = self.problem.E_C[:, task]
predecessors = []
for i in range(len(avaliable_predecessors)):
if(avaliable_predecessors[i] != 0):
predecessors.append(i)

# Choose the max finish time with communication cost
predecessors_fts = []
for i in predecessors:
predecessors_fts.append(self.__ft(i) + self.problem.E_C[i, task]) # E_c is also a communication cost

if len(predecessors_fts) == 0:
return self.__at(processor)
return max(max(predecessors_fts), self.__at(processor))

def __eft(self, task):
eft_list=[]
for p in self.processors:
speed_on_proc = self.problem.W[task] * self.mean_processors_speed / p.speed
eft_list.append(speed_on_proc + self.__est(task, p))
return eft_list

def __allocate_tasks_to_processors(self, tasks):
for task in tasks:
efts = self.__eft(task)
eft_min_id = efts.index(min(efts))
processor = self.processors[eft_min_id]
start_time = efts[eft_min_id] - self.problem.W[task] * self.mean_processors_speed / processor.speed
processor.tasks.append(task)
processor.tasks_starts.append(start_time)

def solve(self):
number_of_nodes = len(self.problem.W)
Q = np.zeros((number_of_nodes, number_of_nodes))
Expand Down Expand Up @@ -129,6 +182,8 @@ def solve(self):
# Q array is after learning process
# Create task order
task_order = self.__get_task_order(Q)
self.__allocate_tasks_to_processors(task_order)


print("OK")

Expand Down

0 comments on commit da0f050

Please sign in to comment.