-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathtest_spawner_flux.py
More file actions
159 lines (135 loc) · 4.74 KB
/
Copy pathtest_spawner_flux.py
File metadata and controls
159 lines (135 loc) · 4.74 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
from concurrent.futures import Future
import os
from queue import Queue
import unittest
import numpy as np
from executorlib.task_scheduler.interactive.blockallocation import BlockAllocationTaskScheduler, _execute_multiple_tasks
from executorlib.standalone.serialize import cloudpickle_register
try:
import flux.job
from executorlib.task_scheduler.interactive.spawner_flux import FluxPythonSpawner
skip_flux_test = "FLUX_URI" not in os.environ
pmi = os.environ.get("EXECUTORLIB_PMIX", None)
except ImportError:
skip_flux_test = True
def calc(i):
return i
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 get_global(memory=None):
return memory
def set_global():
return {"memory": np.array([5])}
@unittest.skipIf(
skip_flux_test, "Flux is not installed, so the flux tests are skipped."
)
class TestFlux(unittest.TestCase):
def setUp(self):
self.flux_executor = flux.job.FluxExecutor()
def test_flux_executor_serial(self):
with BlockAllocationTaskScheduler(
max_workers=2,
executor_kwargs={"flux_executor": self.flux_executor, "priority": 20},
spawner=FluxPythonSpawner,
) as exe:
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_flux_executor_threads(self):
with BlockAllocationTaskScheduler(
max_workers=1,
executor_kwargs={
"flux_executor": self.flux_executor,
"threads_per_core": 2,
},
spawner=FluxPythonSpawner,
) as exe:
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_flux_executor_parallel(self):
with BlockAllocationTaskScheduler(
max_workers=1,
executor_kwargs={
"flux_executor": self.flux_executor,
"cores": 2,
"pmi_mode": pmi,
},
spawner=FluxPythonSpawner,
) as exe:
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_single_task(self):
with BlockAllocationTaskScheduler(
max_workers=1,
executor_kwargs={
"flux_executor": self.flux_executor,
"cores": 2,
"pmi_mode": pmi,
},
spawner=FluxPythonSpawner,
) 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(self):
f = Future()
q = Queue()
q.put({"fn": calc, "args": (), "kwargs": {"i": 2}, "future": f})
q.put({"shutdown": True, "wait": True})
cloudpickle_register(ind=1)
_execute_multiple_tasks(
future_queue=q,
cores=1,
flux_executor=self.flux_executor,
spawner=FluxPythonSpawner,
)
self.assertEqual(f.result(), 2)
q.join()
def test_execute_task_threads(self):
f = Future()
q = Queue()
q.put({"fn": calc, "args": (), "kwargs": {"i": 2}, "future": f})
q.put({"shutdown": True, "wait": True})
cloudpickle_register(ind=1)
_execute_multiple_tasks(
future_queue=q,
cores=1,
threads_per_core=1,
flux_executor=self.flux_executor,
spawner=FluxPythonSpawner,
)
self.assertEqual(f.result(), 2)
q.join()
def test_internal_memory(self):
with BlockAllocationTaskScheduler(
max_workers=1,
executor_kwargs={
"flux_executor": self.flux_executor,
"cores": 1,
"init_function": set_global,
},
spawner=FluxPythonSpawner,
) as p:
f = p.submit(get_global)
self.assertFalse(f.done())
self.assertEqual(f.result(), np.array([5]))
self.assertTrue(f.done())
def test_interface_exception(self):
with self.assertRaises(ValueError):
flux_interface = FluxPythonSpawner(
flux_executor=self.flux_executor, openmpi_oversubscribe=True
)
flux_interface.bootup(command_lst=[])