-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathtest_spawner.py
More file actions
585 lines (521 loc) · 18.8 KB
/
Copy pathtest_spawner.py
File metadata and controls
585 lines (521 loc) · 18.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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
from concurrent.futures import CancelledError, Future
import importlib.util
from queue import Queue
from time import sleep
import shutil
import os
from sys import platform
import unittest
import numpy as np
from executorlib.task_scheduler.base import TaskSchedulerBase
from executorlib.standalone.interactive.spawner import MpiExecSpawner, set_current_directory_in_environment
from executorlib.task_scheduler.interactive.blockallocation import BlockAllocationTaskScheduler, _execute_multiple_tasks
from executorlib.task_scheduler.interactive.onetoone import OneProcessTaskScheduler
from executorlib.standalone.interactive.backend import call_funct
from executorlib.standalone.serialize import cloudpickle_register
try:
import h5py
skip_h5py_test = False
except ImportError:
skip_h5py_test = True
skip_mpi4py_test = importlib.util.find_spec("mpi4py") is None
def calc(i):
return i
def calc_array(i):
return np.array(i**2)
def echo_funct(i):
return i
def get_global(memory=None):
return memory
def set_global():
return {"memory": np.array([5])}
def mpi_funct(i):
from mpi4py import MPI
size = MPI.COMM_WORLD.Get_size()
rank = MPI.COMM_WORLD.Get_rank()
return i, size, rank
def raise_error():
raise RuntimeError
def sleep_one(i):
sleep(1)
return i
class TestBlockAllocationTaskSchedulerSerial(unittest.TestCase):
def test_two_workers_submit_serial_tasks(self):
with BlockAllocationTaskScheduler(
max_workers=2,
executor_kwargs={},
spawner=MpiExecSpawner,
) as exe:
cloudpickle_register(ind=1)
fs_1 = exe.submit(calc, 1)
fs_2 = exe.submit(calc, 2)
self.assertEqual(fs_1.result(), 1)
self.assertEqual(fs_2.result(), 2)
self.assertTrue(fs_1.done())
self.assertTrue(fs_2.done())
def test_max_workers(self):
with BlockAllocationTaskScheduler(
max_workers=2,
executor_kwargs={},
spawner=MpiExecSpawner,
) as exe:
self.assertEqual(exe.max_workers, 2)
def test_one_worker_submit_serial_tasks(self):
with BlockAllocationTaskScheduler(
max_workers=1,
executor_kwargs={},
spawner=MpiExecSpawner,
) as exe:
cloudpickle_register(ind=1)
fs_1 = exe.submit(calc, 1)
fs_2 = exe.submit(calc, 2)
self.assertEqual(fs_1.result(), 1)
self.assertEqual(fs_2.result(), 2)
self.assertTrue(fs_1.done())
self.assertTrue(fs_2.done())
class TestOneProcessTaskSchedulerSerial(unittest.TestCase):
def test_two_workers_submit_serial_tasks(self):
with OneProcessTaskScheduler(
max_cores=2,
executor_kwargs={},
spawner=MpiExecSpawner,
) as exe:
cloudpickle_register(ind=1)
fs_1 = exe.submit(calc, 1)
fs_2 = exe.submit(calc, 2)
self.assertEqual(fs_1.result(), 1)
self.assertEqual(fs_2.result(), 2)
self.assertTrue(fs_1.done())
self.assertTrue(fs_2.done())
def test_max_workers(self):
with OneProcessTaskScheduler(
max_workers=2,
executor_kwargs={},
spawner=MpiExecSpawner,
) as exe:
self.assertEqual(exe.max_workers, 2)
def test_one_worker_submit_serial_tasks(self):
with OneProcessTaskScheduler(
max_cores=1,
executor_kwargs={},
spawner=MpiExecSpawner,
) as exe:
cloudpickle_register(ind=1)
fs_1 = exe.submit(calc, 1)
fs_2 = exe.submit(calc, 2)
self.assertEqual(fs_1.result(), 1)
self.assertEqual(fs_2.result(), 2)
self.assertTrue(fs_1.done())
self.assertTrue(fs_2.done())
@unittest.skipIf(
skip_mpi4py_test, "mpi4py is not installed, so the mpi4py tests are skipped."
)
class TestBlockAllocationTaskSchedulerMPI(unittest.TestCase):
def test_block_allocation_mpi_two_cores(self):
with BlockAllocationTaskScheduler(
max_workers=1,
executor_kwargs={"cores": 2},
spawner=MpiExecSpawner,
) as exe:
cloudpickle_register(ind=1)
fs_1 = exe.submit(mpi_funct, 1)
self.assertEqual(fs_1.result(), [(1, 2, 0), (1, 2, 1)])
self.assertTrue(fs_1.done())
def test_block_allocation_mpi_multiple_submissions(self):
with BlockAllocationTaskScheduler(
max_workers=1,
executor_kwargs={"cores": 2},
spawner=MpiExecSpawner,
) as p:
cloudpickle_register(ind=1)
fs1 = p.submit(mpi_funct, 1)
fs2 = p.submit(mpi_funct, 2)
fs3 = p.submit(mpi_funct, 3)
output = [
fs1.result(),
fs2.result(),
fs3.result(),
]
self.assertEqual(
output,
[[(1, 2, 0), (1, 2, 1)], [(2, 2, 0), (2, 2, 1)], [(3, 2, 0), (3, 2, 1)]],
)
def test_block_allocation_mpi_echo_broadcast(self):
with BlockAllocationTaskScheduler(
max_workers=1,
executor_kwargs={"cores": 2},
spawner=MpiExecSpawner,
) as p:
cloudpickle_register(ind=1)
output = p.submit(echo_funct, 2).result()
self.assertEqual(output, [2, 2])
@unittest.skipIf(
skip_mpi4py_test, "mpi4py is not installed, so the mpi4py tests are skipped."
)
class TestOneProcessTaskSchedulerMPI(unittest.TestCase):
def test_one_process_mpi_two_cores(self):
with OneProcessTaskScheduler(
max_cores=2,
executor_kwargs={"cores": 2},
spawner=MpiExecSpawner,
) as exe:
cloudpickle_register(ind=1)
fs_1 = exe.submit(mpi_funct, 1)
self.assertEqual(fs_1.result(), [(1, 2, 0), (1, 2, 1)])
self.assertTrue(fs_1.done())
def test_one_process_mpi_multiple_submissions(self):
with OneProcessTaskScheduler(
max_cores=2,
executor_kwargs={"cores": 2},
spawner=MpiExecSpawner,
) as p:
cloudpickle_register(ind=1)
fs1 = p.submit(mpi_funct, 1)
fs2 = p.submit(mpi_funct, 2)
fs3 = p.submit(mpi_funct, 3)
output = [
fs1.result(),
fs2.result(),
fs3.result(),
]
self.assertEqual(
output,
[[(1, 2, 0), (1, 2, 1)], [(2, 2, 0), (2, 2, 1)], [(3, 2, 0), (3, 2, 1)]],
)
def test_one_process_mpi_echo_broadcast(self):
with OneProcessTaskScheduler(
max_cores=2,
executor_kwargs={"cores": 2},
spawner=MpiExecSpawner,
) as p:
cloudpickle_register(ind=1)
output = p.submit(echo_funct, 2).result()
self.assertEqual(output, [2, 2])
class TestBlockAllocationTaskSchedulerInitFunction(unittest.TestCase):
def test_internal_memory(self):
with BlockAllocationTaskScheduler(
max_workers=1,
executor_kwargs={
"cores": 1,
"init_function": set_global,
},
spawner=MpiExecSpawner,
) as p:
f = p.submit(get_global)
self.assertFalse(f.done())
self.assertEqual(f.result(), np.array([5]))
self.assertTrue(f.done())
def test_call_funct(self):
self.assertEqual(
call_funct(
input_dict={"fn": get_global, "args": (), "kwargs": {}},
memory={"memory": 4},
),
4,
)
def test_execute_task(self):
f = Future()
q = Queue()
q.put({"fn": get_global, "args": (), "kwargs": {}, "future": f})
q.put({"shutdown": True, "wait": True})
cloudpickle_register(ind=1)
_execute_multiple_tasks(
future_queue=q,
cores=1,
openmpi_oversubscribe=False,
spawner=MpiExecSpawner,
init_function=set_global,
)
self.assertEqual(f.result(), np.array([5]))
q.join()
@unittest.skipIf(
skip_mpi4py_test, "mpi4py is not installed, so the mpi4py tests are skipped."
)
def test_internal_memory_mpi(self):
with BlockAllocationTaskScheduler(
max_workers=1,
executor_kwargs={
"cores": 2,
"init_function": set_global,
},
spawner=MpiExecSpawner,
) as p:
cloudpickle_register(ind=1)
f = p.submit(get_global)
result = f.result()
self.assertEqual(len(result), 2)
np.testing.assert_array_equal(result[0], np.array([5]))
np.testing.assert_array_equal(result[1], np.array([5]))
class TestBlockAllocationTaskScheduler(unittest.TestCase):
def test_submit_tracks_future_state(self):
with BlockAllocationTaskScheduler(
max_workers=1,
executor_kwargs={"cores": 1},
spawner=MpiExecSpawner,
) as p:
output = p.submit(calc_array, i=2)
self.assertEqual(len(p), 1)
self.assertTrue(isinstance(output, Future))
self.assertFalse(output.done())
sleep(1)
self.assertTrue(output.done())
self.assertEqual(len(p), 0)
self.assertEqual(output.result(), np.array(4))
def test_executor_multi_submission(self):
with BlockAllocationTaskScheduler(
max_workers=1,
executor_kwargs={"cores": 1},
spawner=MpiExecSpawner,
) as p:
fs_1 = p.submit(calc_array, i=2)
fs_2 = p.submit(calc_array, i=2)
self.assertEqual(fs_1.result(), np.array(4))
self.assertEqual(fs_2.result(), np.array(4))
self.assertTrue(fs_1.done())
self.assertTrue(fs_2.done())
@unittest.skipIf(platform == "darwin", "Skipping test on macOS due to known issues")
def test_shutdown(self):
p = BlockAllocationTaskScheduler(
max_workers=1,
executor_kwargs={"cores": 1},
spawner=MpiExecSpawner,
)
fs1 = p.submit(sleep_one, i=2)
fs2 = p.submit(sleep_one, i=4)
sleep(1)
p.shutdown(wait=True, cancel_futures=True)
self.assertTrue(fs1.done())
self.assertTrue(fs2.done())
self.assertEqual(fs1.result(), 2)
with self.assertRaises(CancelledError):
fs2.result()
def test_map_returns_array_results(self):
with BlockAllocationTaskScheduler(
max_workers=1,
executor_kwargs={"cores": 1},
spawner=MpiExecSpawner,
) as p:
output = list(p.map(calc_array, [1, 2, 3]))
self.assertEqual(output, [np.array(1), np.array(4), np.array(9)])
def test_executor_exception(self):
with self.assertRaises(RuntimeError):
with BlockAllocationTaskScheduler(
max_workers=1,
executor_kwargs={"cores": 1},
spawner=MpiExecSpawner,
) as p:
fs = p.submit(raise_error)
fs.result()
def test_executor_exception_future(self):
with self.assertRaises(RuntimeError):
with BlockAllocationTaskScheduler(
max_workers=1,
executor_kwargs={"cores": 1},
spawner=MpiExecSpawner,
) as p:
fs = p.submit(raise_error)
fs.result()
@unittest.skipIf(
skip_mpi4py_test, "mpi4py is not installed, so the mpi4py tests are skipped."
)
def test_block_allocation_task_scheduler_info(self):
meta_data_exe_dict = {
"cores": 2,
"spawner": "<class 'executorlib.standalone.interactive.spawner.MpiExecSpawner'>",
"hostname_localhost": True,
"init_function": None,
"cwd": None,
"openmpi_oversubscribe": False,
"max_workers": 1,
}
with BlockAllocationTaskScheduler(
max_workers=1,
executor_kwargs={
"cores": 2,
"hostname_localhost": True,
"init_function": None,
"cwd": None,
"openmpi_oversubscribe": False,
},
spawner=MpiExecSpawner,
) as exe:
for k, v in meta_data_exe_dict.items():
if k != "spawner":
self.assertEqual(exe.info[k], v)
else:
self.assertEqual(str(exe.info[k]), v)
with TaskSchedulerBase() as exe:
self.assertIsNone(exe.info)
def test_one_process_task_scheduler_info(self):
meta_data_exe_dict = {
"cores": 2,
"spawner": "<class 'executorlib.standalone.interactive.spawner.MpiExecSpawner'>",
"hostname_localhost": True,
"cwd": None,
"openmpi_oversubscribe": False,
"max_cores": 2,
}
with OneProcessTaskScheduler(
max_cores=2,
executor_kwargs={
"cores": 2,
"hostname_localhost": True,
"cwd": None,
"openmpi_oversubscribe": False,
},
spawner=MpiExecSpawner,
) as exe:
for k, v in meta_data_exe_dict.items():
if k != "spawner":
self.assertEqual(exe.info[k], v)
else:
self.assertEqual(str(exe.info[k]), v)
@unittest.skipIf(
skip_mpi4py_test, "mpi4py is not installed, so the mpi4py tests are skipped."
)
def test_submit_mpi_task_tracks_future_state(self):
with BlockAllocationTaskScheduler(
max_workers=1,
executor_kwargs={"cores": 2},
spawner=MpiExecSpawner,
) as p:
output = p.submit(mpi_funct, i=2)
self.assertEqual(len(p), 1)
self.assertTrue(isinstance(output, Future))
self.assertFalse(output.done())
sleep(2)
self.assertTrue(output.done())
self.assertEqual(len(p), 0)
self.assertEqual(output.result(), [(2, 2, 0), (2, 2, 1)])
@unittest.skipIf(
skip_mpi4py_test, "mpi4py is not installed, so the mpi4py tests are skipped."
)
def test_map_mpi_tasks(self):
with BlockAllocationTaskScheduler(
max_workers=1,
executor_kwargs={"cores": 2},
spawner=MpiExecSpawner,
) as p:
output = list(p.map(mpi_funct, [1, 2, 3]))
self.assertEqual(
output,
[[(1, 2, 0), (1, 2, 1)], [(2, 2, 0), (2, 2, 1)], [(3, 2, 0), (3, 2, 1)]],
)
def test_execute_task_failed_no_argument(self):
f = Future()
q = Queue()
q.put({"fn": calc_array, "args": (), "kwargs": {}, "future": f})
q.put({"shutdown": True, "wait": True})
cloudpickle_register(ind=1)
_execute_multiple_tasks(
future_queue=q,
cores=1,
openmpi_oversubscribe=False,
spawner=MpiExecSpawner,
)
with self.assertRaises(TypeError):
f.result()
q.join()
def test_execute_task_failed_wrong_argument(self):
f = Future()
q = Queue()
q.put({"fn": calc_array, "args": (), "kwargs": {"j": 4}, "future": f})
q.put({"shutdown": True, "wait": True})
cloudpickle_register(ind=1)
_execute_multiple_tasks(
future_queue=q,
cores=1,
openmpi_oversubscribe=False,
spawner=MpiExecSpawner,
)
with self.assertRaises(TypeError):
f.result()
q.join()
def test_execute_task(self):
f = Future()
q = Queue()
q.put({"fn": calc_array, "args": (), "kwargs": {"i": 2}, "future": f})
q.put({"shutdown": True, "wait": True})
cloudpickle_register(ind=1)
_execute_multiple_tasks(
future_queue=q,
cores=1,
openmpi_oversubscribe=False,
spawner=MpiExecSpawner,
)
self.assertEqual(f.result(), np.array(4))
q.join()
@unittest.skipIf(
skip_mpi4py_test, "mpi4py is not installed, so the mpi4py tests are skipped."
)
def test_execute_task_parallel(self):
f = Future()
q = Queue()
q.put({"fn": calc_array, "args": (), "kwargs": {"i": 2}, "future": f})
q.put({"shutdown": True, "wait": True})
cloudpickle_register(ind=1)
_execute_multiple_tasks(
future_queue=q,
cores=2,
openmpi_oversubscribe=False,
spawner=MpiExecSpawner,
)
self.assertEqual(f.result(), [np.array(4), np.array(4)])
q.join()
class TestBlockAllocationTaskSchedulerCache(unittest.TestCase):
def tearDown(self):
shutil.rmtree("executorlib_cache", ignore_errors=True)
@unittest.skipIf(
skip_h5py_test, "h5py is not installed, so the h5py tests are skipped."
)
def test_execute_task_cache(self):
f = Future()
q = Queue()
q.put({"fn": calc, "args": (), "kwargs": {"i": 1}, "future": f})
q.put({"shutdown": True, "wait": True})
cloudpickle_register(ind=1)
_execute_multiple_tasks(
future_queue=q,
cores=1,
openmpi_oversubscribe=False,
spawner=MpiExecSpawner,
cache_directory="executorlib_cache",
)
self.assertEqual(f.result(), 1)
q.join()
@unittest.skipIf(
skip_h5py_test, "h5py is not installed, so the h5py tests are skipped."
)
def test_execute_task_cache_failed_no_argument(self):
f = Future()
q = Queue()
q.put({"fn": calc_array, "args": (), "kwargs": {}, "future": f})
q.put({"shutdown": True, "wait": True})
cloudpickle_register(ind=1)
_execute_multiple_tasks(
future_queue=q,
cores=1,
openmpi_oversubscribe=False,
spawner=MpiExecSpawner,
cache_directory="executorlib_cache",
)
with self.assertRaises(TypeError):
f.result()
q.join()
class TestEnvManipulation(unittest.TestCase):
def test_set_current_directory_in_environment(self):
env = os.environ
if "PYTHONPATH" in env:
python_path = env["PYTHONPATH"]
del env["PYTHONPATH"]
else:
python_path = None
self.assertFalse("PYTHONPATH" in env)
set_current_directory_in_environment()
self.assertTrue("PYTHONPATH" in env)
self.assertEqual(env["PYTHONPATH"], os.getcwd())
env["PYTHONPATH"] = "/my/special/path"
set_current_directory_in_environment()
self.assertEqual(env["PYTHONPATH"], os.getcwd() + ":/my/special/path")
if python_path is not None:
env["PYTHONPATH"] = python_path