-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathtest_single_dependencies.py
More file actions
396 lines (353 loc) · 12.8 KB
/
Copy pathtest_single_dependencies.py
File metadata and controls
396 lines (353 loc) · 12.8 KB
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
from concurrent.futures import Future
import unittest
from time import sleep, time
from queue import Queue
from threading import Thread
from executorlib import SingleNodeExecutor
from executorlib.executor.single import create_single_node_executor
from executorlib.task_scheduler.interactive.dependency import _execute_tasks_with_dependencies
from executorlib.standalone.serialize import cloudpickle_register
from executorlib.standalone.interactive.spawner import MpiExecSpawner
try:
import pygraphviz
skip_graphviz_test = False
except ImportError:
skip_graphviz_test = True
def add_function(parameter_1, parameter_2):
sleep(0.2)
return parameter_1 + parameter_2
def generate_tasks(length):
sleep(0.2)
return range(length)
def calc_from_lst(lst, ind, parameter):
sleep(0.2)
return lst[ind] + parameter
def merge(lst):
sleep(0.2)
return sum(lst)
def return_input_dict(input_dict):
return input_dict
def raise_error(parameter):
raise RuntimeError
class TestExecutorWithDependencies(unittest.TestCase):
def test_future_chaining_resolves_dependency(self):
with SingleNodeExecutor(max_cores=1) as exe:
cloudpickle_register(ind=1)
future_1 = exe.submit(add_function, 1, parameter_2=2)
future_2 = exe.submit(add_function, 1, parameter_2=future_1)
self.assertEqual(future_2.result(), 4)
def test_shutdown_no_wait_still_resolves_futures(self):
exe = SingleNodeExecutor(max_cores=1)
cloudpickle_register(ind=1)
future_1 = exe.submit(add_function, 1, parameter_2=2)
future_2 = exe.submit(add_function, 1, parameter_2=future_1)
exe.shutdown(wait=False, cancel_futures=False)
self.assertEqual(future_2.result(), 4)
def test_batched(self):
with SingleNodeExecutor() as exe:
t1 = time()
future_first_lst = []
for i in range(10):
future_first_lst.append(exe.submit(return_input_dict, i))
future_second_lst = exe.batched(future_first_lst, n=3)
future_third_lst = []
for f in future_second_lst:
future_third_lst.append(exe.submit(sum, f))
t2 = time()
result_lst = [f.result() for f in future_third_lst]
t3 = time()
self.assertEqual(sum(result_lst), 45)
self.assertEqual(len(result_lst), 4)
self.assertTrue(t3-t2 > t2-t1)
def test_batched_error(self):
with self.assertRaises(TypeError):
with SingleNodeExecutor() as exe:
exe.batched([])
def test_dependency_steps(self):
cloudpickle_register(ind=1)
fs1 = Future()
fs2 = Future()
q = Queue()
q.put(
{
"fn": add_function,
"args": (),
"kwargs": {"parameter_1": 1, "parameter_2": 2},
"future": fs1,
"resource_dict": {"cores": 1},
}
)
q.put(
{
"fn": add_function,
"args": (),
"kwargs": {"parameter_1": 1, "parameter_2": fs1},
"future": fs2,
"resource_dict": {"cores": 1},
}
)
executor = create_single_node_executor(
max_workers=1,
max_cores=2,
executor_kwargs={
"cores": 1,
"threads_per_core": 1,
"gpus_per_core": 0,
"cwd": None,
"openmpi_oversubscribe": False,
"slurm_cmd_args": [],
},
)
process = Thread(
target=_execute_tasks_with_dependencies,
kwargs={
"future_queue": q,
"executor_queue": executor._future_queue,
"executor": executor,
"refresh_rate": 0.01,
},
)
process.start()
self.assertFalse(fs1.done())
self.assertFalse(fs2.done())
self.assertEqual(fs2.result(), 4)
self.assertTrue(fs1.done())
self.assertTrue(fs2.done())
q.put({"shutdown": True, "wait": True})
def test_dependency_steps_error(self):
cloudpickle_register(ind=1)
fs1 = Future()
fs2 = Future()
q = Queue()
q.put(
{
"fn": raise_error,
"args": (),
"kwargs": {"parameter": 0},
"future": fs1,
"resource_dict": {"cores": 1},
}
)
q.put(
{
"fn": add_function,
"args": (),
"kwargs": {"parameter_1": 1, "parameter_2": fs1},
"future": fs2,
"resource_dict": {"cores": 1},
}
)
executor = create_single_node_executor(
max_workers=1,
max_cores=2,
executor_kwargs={
"cores": 1,
"threads_per_core": 1,
"gpus_per_core": 0,
"cwd": None,
"openmpi_oversubscribe": False,
"slurm_cmd_args": [],
},
)
process = Thread(
target=_execute_tasks_with_dependencies,
kwargs={
"future_queue": q,
"executor_queue": executor._future_queue,
"executor": executor,
"refresh_rate": 0.01,
},
)
process.start()
self.assertFalse(fs1.done())
self.assertFalse(fs2.done())
self.assertTrue(fs1.exception() is not None)
self.assertTrue(fs2.exception() is not None)
with self.assertRaises(RuntimeError):
fs2.result()
q.put({"shutdown": True, "wait": True})
def test_dependency_steps_error_before(self):
cloudpickle_register(ind=1)
fs1 = Future()
fs1.set_exception(RuntimeError())
fs2 = Future()
q = Queue()
q.put(
{
"fn": add_function,
"args": (),
"kwargs": {"parameter_1": 1, "parameter_2": 2},
"future": fs1,
"resource_dict": {"cores": 1},
}
)
q.put(
{
"fn": add_function,
"args": (),
"kwargs": {"parameter_1": 1, "parameter_2": fs1},
"future": fs2,
"resource_dict": {"cores": 1},
}
)
executor = create_single_node_executor(
max_workers=1,
max_cores=2,
executor_kwargs={
"cores": 1,
"threads_per_core": 1,
"gpus_per_core": 0,
"cwd": None,
"openmpi_oversubscribe": False,
"slurm_cmd_args": [],
},
)
process = Thread(
target=_execute_tasks_with_dependencies,
kwargs={
"future_queue": q,
"executor_queue": executor._future_queue,
"executor": executor,
"refresh_rate": 0.01,
},
)
process.start()
self.assertTrue(fs1.exception() is not None)
self.assertTrue(fs2.exception() is not None)
with self.assertRaises(RuntimeError):
fs2.result()
executor.shutdown(wait=True)
q.put({"shutdown": True, "wait": True})
q.join()
process.join()
def test_many_to_one(self):
length = 5
parameter = 1
with SingleNodeExecutor(max_cores=2) as exe:
cloudpickle_register(ind=1)
future_lst = exe.submit(
generate_tasks,
length=length,
resource_dict={"cores": 1},
)
lst = []
for i in range(length):
lst.append(
exe.submit(
calc_from_lst,
lst=future_lst,
ind=i,
parameter=parameter,
resource_dict={"cores": 1},
)
)
future_sum = exe.submit(
merge,
lst=lst,
resource_dict={"cores": 1},
)
self.assertEqual(future_sum.result(), 15)
def test_future_input_dict(self):
with SingleNodeExecutor() as exe:
fs = exe.submit(
return_input_dict,
input_dict={"a": exe.submit(sum, [2, 2])},
)
self.assertEqual(fs.result()["a"], 4)
class TestExecutorErrors(unittest.TestCase):
def test_block_allocation_false_one_worker(self):
with self.assertRaises(RuntimeError):
with SingleNodeExecutor(max_cores=1, block_allocation=False) as exe:
cloudpickle_register(ind=1)
fs = exe.submit(raise_error, parameter=0)
fs.result()
def test_block_allocation_true_one_worker(self):
with self.assertRaises(RuntimeError):
with SingleNodeExecutor(max_cores=1, block_allocation=True) as exe:
cloudpickle_register(ind=1)
fs = exe.submit(raise_error, parameter=0)
fs.result()
def test_block_allocation_false_two_workers(self):
with self.assertRaises(RuntimeError):
with SingleNodeExecutor(max_cores=2, block_allocation=False) as exe:
cloudpickle_register(ind=1)
fs = exe.submit(raise_error, parameter=0)
fs.result()
def test_block_allocation_true_two_workers(self):
with self.assertRaises(RuntimeError):
with SingleNodeExecutor(max_cores=2, block_allocation=True) as exe:
cloudpickle_register(ind=1)
fs = exe.submit(raise_error, parameter=0)
fs.result()
def test_block_allocation_false_one_worker_loop(self):
with self.assertRaises(RuntimeError):
with SingleNodeExecutor(max_cores=1, block_allocation=False) as exe:
cloudpickle_register(ind=1)
lst = []
for i in range(1, 4):
lst = exe.submit(
raise_error,
parameter=lst,
)
lst.result()
def test_block_allocation_true_one_worker_loop(self):
with self.assertRaises(RuntimeError):
with SingleNodeExecutor(max_cores=1, block_allocation=True) as exe:
cloudpickle_register(ind=1)
lst = []
for i in range(1, 4):
lst = exe.submit(
raise_error,
parameter=lst,
)
lst.result()
def test_block_allocation_false_two_workers_loop(self):
with self.assertRaises(RuntimeError):
with SingleNodeExecutor(max_cores=2, block_allocation=False) as exe:
cloudpickle_register(ind=1)
lst = []
for i in range(1, 4):
lst = exe.submit(
raise_error,
parameter=lst,
)
lst.result()
def test_block_allocation_true_two_workers_loop(self):
with self.assertRaises(RuntimeError):
with SingleNodeExecutor(max_cores=2, block_allocation=True) as exe:
cloudpickle_register(ind=1)
lst = []
for i in range(1, 4):
lst = exe.submit(
raise_error,
parameter=lst,
)
lst.result()
class TestInfo(unittest.TestCase):
"""Test cases for the info property of SingleNodeExecutor."""
def setUp(self):
"""Set up the expected info dictionary."""
self.expected_info = {
'cores': 1,
'cwd': None,
'openmpi_oversubscribe': False,
'cache_directory': None,
'hostname_localhost': None,
'log_obj_size': False,
'spawner': MpiExecSpawner,
'max_cores': None,
'max_workers': None,
}
def test_info_disable_dependencies_true(self):
"""Test info property with dependencies disabled."""
with SingleNodeExecutor(disable_dependencies=True) as exe:
self.assertEqual(exe.info, self.expected_info)
def test_info_disable_dependencies_false(self):
"""Test info property with dependencies enabled."""
with SingleNodeExecutor(disable_dependencies=False) as exe:
self.assertEqual(exe.info, self.expected_info)
def test_info_error_handling(self):
"""Test info property error handling when executor is not running."""
exe = SingleNodeExecutor()
exe.shutdown(wait=True)
self.assertIsNone(exe.info)