-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMasterHub.py
412 lines (337 loc) · 12.6 KB
/
MasterHub.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
# -*- coding: utf-8 -*-
'''
Implementation of a Master-Hub-Worker structure for parallel tree search.
Copyright (C) 2012 Steven Laan
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see http://www.gnu.org/licenses/
'''
import multiprocessing as mp
from Queue import Empty
from copy import deepcopy
# Define signals
SIGNAL_NODE = 0
SIGNAL_DONE = 1
SIGNAL_IDLE = 2
SIGNAL_BUSY = 3
SIGNAL_ANSWERS = 4
# Define shorthands
SIG_IDLE = (SIGNAL_IDLE, None)
SIG_DONE = (SIGNAL_DONE, None)
SIG_BUSY = (SIGNAL_BUSY, None)
class Worker(mp.Process):
'''
The worker class. Instances of this class are the main processing power of
the system.
'''
def __init__(self, model, queue, feed_queue, max_len = 10):
'''
Initializes the worker.
Parameters
----------
model : Model object
The model of the problem that is being solved. The model contains
the code for processing of nodes.
queue : mp.Queue
The main queue to get jobs from. In the case of the worker this is
the queue of the hub.
feed_queue : mp.Queue
The queue to feed back results and nodes to the hub.
max_len : int
The maximum number of items that can be on a workers private queue,
before items are pushed back to the hub. Acts as a means of load
balancing. Default 10.
'''
# Base class initialization
mp.Process.__init__(self)
self.answers = []
self.main_queue = queue
self.feed_queue = feed_queue
self.stack = []
self.model = deepcopy(model)
self.max_stack_size = max_len
def run(self):
'''
Starts the worker process.
'''
for _, item in iter(self.main_queue.get, SIG_DONE):
self.stack.append(item)
self.feed_queue.put(SIG_BUSY)
# Don't bother the main queue while we got items
while len(self.stack):
node = self.stack.pop()
self.process_node(node)
# Check for overflow
if len(self.stack) > self.max_stack_size:
# Put half of the things on the stack on the main queue
for i in xrange(len(self.stack) / 2):
self.feed_queue.put((SIGNAL_NODE, self.stack.pop()))
# No more items in the stack, signal the main queue
self.feed_queue.put(SIG_IDLE)
# This process isn't going to put anything in the queues anymore
self.main_queue.close()
self.feed_queue.put((SIGNAL_ANSWERS, self.answers))
def process_node(self, node):
'''
Processes the given node. This results in either putting new nodes on
the private stack or appending answers to the answer.
Parameters
----------
node : node object
The node to be processed.
'''
new_nodes = self.model.process_node(node)
for new_node in new_nodes:
if new_node.terminal:
# Solution found!
self.answers.append(new_node)
else:
self.stack.append(new_node)
class Hub(mp.Process):
'''
The intermediate Hub class. This class has a couple of workers in its pool,
for which it provides jobs. If idle, the hub can perform some work as well.
'''
def __init__(self, model, queue, feed_queue, num_workers = 1, max_len = 10):
'''
Create an instance of a hub.
Parameters
----------
model : model object
The model of the problem that is solved. The model provides the
procedures to process a node.
queue : mp.Queue
The main queue to get jobs from. In the case of the hub, this is
the queue from the master.
feed_queue : mp.Queue
The queue to feed answers back to the master.
num_workers : int
The number of workers this hub has. Default 1.
max_len : int
The maximum length of the job queue before jobs are pushed back to
the master. Default 10.
'''
# Base class initialization
super(Hub, self).__init__()
self.workers = []
self.main_queue = queue
self.queue = mp.Queue()
self.feed_queue = feed_queue
self.model = deepcopy(model)
self.max_len = max_len
self.answers = []
self.idle = True
self.done = False
self.idle_workers = num_workers
self.tasks_busy = 0
self.tasks_accepted = 0
# Create all workers and start them
for i in range(num_workers):
q = mp.Queue()
worker = Worker(self.model, self.queue, q, max_len = max_len)
self.workers.append(q)
worker.start()
def run(self):
'''
Starts the hub process.
'''
# Get a first item
self.get_item()
while not self.done:
# Try to get another item
try:
self.get_item(False)
except Empty:
pass
if self.idle:
continue
if self.done:
break
# Handle input of workers
self.handle_queues()
# Check for idleness
if not self.idle:
if self.tasks_busy == 0:
# Signal master, that this chain is idle
self.feed_queue.put((SIGNAL_IDLE, self.tasks_accepted))
self.tasks_accepted = 0
self.idle = True
while self.tasks_busy > len(self.workers):
# Check for overflow
if self.queue.qsize() > self.max_len:
try:
for i in range(self.max_len / 2):
item = self.queue.get()
self.tasks_busy -= 1
self.feed_queue.put(item)
except Empty:
# Queue got empty during emptying
pass
# Handle input of workers
self.handle_queues()
# We're done! Signal workers we're done
for _ in self.workers:
self.queue.put(SIG_DONE)
# Retrieve answers from workers
for queue in self.workers:
signal, answers = queue.get()
if signal != SIGNAL_ANSWERS:
raise Exception('Wrong signal: got %d expected %d' % (signal, SIGNAL_ANSWERS))
self.answers.extend(answers)
# Send the answers to the master
self.feed_queue.put((SIGNAL_ANSWERS, self.answers))
def handle_queues(self):
'''
Retrieves one item of each worker queue and processes it.
'''
for queue in self.workers:
try:
sig, item = queue.get_nowait()
if sig == SIGNAL_NODE:
self.queue.put((sig, item))
self.tasks_busy += 1
elif sig == SIGNAL_IDLE:
self.idle_workers += 1
self.tasks_busy -= 1
elif sig == SIGNAL_BUSY:
self.idle_workers -= 1
elif sig == SIGNAL_ANSWERS:
self.answers.extend(item)
except Empty:
pass
def get_item(self, block = True):
'''
Tries to retrieve an item from the main queue and processes it.
Parameters
----------
block : bool
Whether to wait for an item, or continue if no item is
available. Default True.
'''
try:
sig, item = self.main_queue.get(block = block)
if sig == SIGNAL_NODE:
self.queue.put((sig, item))
self.tasks_accepted += 1
self.tasks_busy += 1
if self.idle:
self.feed_queue.put(SIG_BUSY)
self.idle = False
elif sig == SIGNAL_DONE:
self.done = True
else:
raise Exception('Wrong signal: got %d' % (sig,))
except Empty:
pass
class Master(object):
'''
The master of the search. Starts the hubs and collects the results.
'''
def __init__(self, model, hub_division, max_len = 10):
'''
Creates an instance of the master.
Parameters
----------
model : model object
The model of the problem that is solved. The model provides
the methods to process a node.
hub_division : list of ints
The specification of the hubs and workers. Each integer in
the list is a hub. The value of the integer is the number
of workers for that hub. For example [2,3] means two hubs,
one with 2 workers and one with 3.
max_len : int
The maximum length for a queue of the hubs and workers,
before they start pushing back. Default 10.
'''
self.model = model
self.queue = mp.Queue()
self.hubs = []
self.answers = []
self.idle_hubs = len(hub_division)
self.queue.put((SIGNAL_NODE, model.get_root()))
self.tasks_busy = 1
done = False
# Create all workers and start them
for i in hub_division:
q = mp.Queue()
hub = Hub(model, self.queue, q, num_workers = i, max_len = max_len)
self.hubs.append(q)
hub.start()
# Main loop
while not done:
# Handle input of workers
self.handle_queues()
# Check for completion
if self.tasks_busy == 0:
done = True
# We're done! Signal hubs we're done
for _ in self.hubs:
self.queue.put(SIG_DONE)
# Retrieve answers from hubs
for queue in self.hubs:
sig, answers = queue.get()
if sig != SIGNAL_ANSWERS:
raise Exception('Wrong signal: got %d expected %d' % (sig, SIGNAL_ANSWERS))
self.answers.extend(answers)
def handle_queues(self):
'''
Retrieves one item of each worker queue and processes it.
'''
for queue in self.hubs:
try:
sig, item = queue.get_nowait()
if sig == SIGNAL_NODE:
self.queue.put((sig, item))
self.tasks_busy += 1
elif sig == SIGNAL_IDLE:
self.idle_hubs += 1
self.tasks_busy -= item
elif sig == SIGNAL_BUSY:
self.idle_hubs -= 1
else:
raise Exception('Wrong signal: got %d' % (sig,))
except Empty:
pass
class Model(object):
'''
Abstract base class for a parallel tree search model.
'''
def process_node(self, node):
'''
Processes a (partial) solution node.
Parameters
----------
node_to_process : Node object
The node that is to be processed
Returns
-------
new_nodes : list
The list containing the children of the processed node.
Can be empty.
'''
raise NotImplementedError
def get_root_node(self):
'''
Returns the root node of the search tree.
'''
raise NotImplementedError
class Node(object):
'''
Base class for a node of the search tree.
'''
def __init__(self, terminal):
'''
Creates a Node of the search tree
Parameters
----------
terminal : bool
Whether this node is a leaf node of the tree.
'''
self.terminal = terminal