-
Notifications
You must be signed in to change notification settings - Fork 15
/
cluster.py
173 lines (136 loc) · 4.72 KB
/
cluster.py
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import math
class Machine:
def __init__(self, id):
self.id = id
self.running_job_id = -1
self.is_free = True
self.job_history = []
def taken_by_job(self, job_id):
if self.is_free:
self.running_job_id = job_id
self.is_free = False
self.job_history.append(job_id)
return True
else:
return False
def release(self):
if self.is_free:
return -1
else:
self.is_free = True
self.running_job_id = -1
return 1
def reset(self):
self.is_free = True
self.running_job_id = -1
self.job_history = []
def __eq__(self, other):
return self.id == other.id
def __str__(self):
return "M["+str(self.id)+"] "
class Cluster:
def __init__(self, cluster_name, node_num, num_procs_per_node):
self.name = cluster_name
self.total_node = node_num
self.free_node = node_num
self.used_node = 0
self.num_procs_per_node = num_procs_per_node
self.all_nodes = []
for i in range(self.total_node):
self.all_nodes.append(Machine(i))
def feature(self):
return [self.free_node]
def can_allocated(self, job):
if job.request_number_of_nodes != -1 and job.request_number_of_nodes > self.free_node:
return False
if job.request_number_of_nodes != -1 and job.request_number_of_nodes <= self.free_node:
return True
request_node = int(math.ceil(float(job.request_number_of_processors)/float(self.num_procs_per_node)))
job.request_number_of_nodes = request_node
if request_node > self.free_node:
return False
else:
return True
def allocate(self, job_id, request_num_procs):
allocated_nodes = []
request_node = int(math.ceil(float(request_num_procs) / float(self.num_procs_per_node)))
if request_node > self.free_node:
return []
allocated = 0
for m in self.all_nodes:
if allocated == request_node:
return allocated_nodes
if m.taken_by_job(job_id):
allocated += 1
self.used_node += 1
self.free_node -= 1
allocated_nodes.append(m)
if allocated == request_node:
return allocated_nodes
print ("Error in allocation, there are enough free resources but can not allocated!")
return []
def release(self, releases):
self.used_node -= len(releases)
self.free_node += len(releases)
for m in releases:
m.release()
def is_idle(self):
if self.used_node == 0:
return True
return False
def reset(self):
self.used_node = 0
self.free_node = self.total_node
for m in self.all_nodes:
m.reset()
class FakeList:
def __init__(self, l):
self.len = l
def __len__(self):
return self.len
class SimpleCluster:
def __init__(self, cluster_name, node_num, num_procs_per_node):
self.name = cluster_name
self.total_node = node_num
self.free_node = node_num
self.used_node = 0
self.num_procs_per_node = num_procs_per_node
self.all_nodes = []
def feature(self):
return [self.free_node]
def can_allocated(self, job):
if job.request_number_of_nodes != -1:
if job.request_number_of_nodes > self.free_node:
return False
else:
return True
request_node = int(math.ceil(float(job.request_number_of_processors)/float(self.num_procs_per_node)))
job.request_number_of_nodes = request_node
if request_node > self.free_node:
return False
else:
return True
def allocate(self, job_id, request_num_procs):
allocated_nodes = FakeList(0)
request_node = int(math.ceil(float(request_num_procs) / float(self.num_procs_per_node)))
if request_node > self.free_node:
return []
allocated = request_node
self.used_node += allocated
self.free_node -= allocated
allocated_nodes.len = allocated
if allocated == request_node:
return allocated_nodes
print ("Error in allocation, there are enough free resources but can not allocated!")
return []
def release(self, releases):
self.used_node -= len(releases)
self.free_node += len(releases)
def is_idle(self):
if self.used_node == 0:
return True
return False
def reset(self):
self.used_node = 0
self.free_node = self.total_node
Cluster = SimpleCluster