-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathtest_flux_cluster.py
More file actions
324 lines (286 loc) · 11.3 KB
/
Copy pathtest_flux_cluster.py
File metadata and controls
324 lines (286 loc) · 11.3 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
import os
import importlib
import unittest
import shutil
from time import sleep
from executorlib import FluxClusterExecutor
from executorlib.standalone.serialize import cloudpickle_register
from executorlib.standalone.command import get_cache_execute_command
try:
import flux.job
from executorlib import terminate_tasks_in_cache, terminate_task_in_cache
from executorlib.standalone.hdf import dump
from executorlib.task_scheduler.file.spawner_pysqa import execute_with_pysqa
from executorlib.standalone.command_pysqa import pysqa_terminate
from executorlib.task_scheduler.interactive.spawner_pysqa import PysqaSpawner
skip_flux_test = "FLUX_URI" not in os.environ
pmi = os.environ.get("EXECUTORLIB_PMIX", None)
except ImportError:
skip_flux_test = True
skip_mpi4py_test = importlib.util.find_spec("mpi4py") is None
template = """\
#!/bin/bash
# flux: --job-name={{job_name}}
# flux: --env=CORES={{cores}}
# flux: --output=time.out
# flux: --error=error.out
# flux: --nslots={{cores}}
# flux: --queue={{queue}}
{%- if run_time_max %}
# flux: --time-limit={{run_time_max}}s
{%- endif %}
{%- if dependency_list %}
{%- for jobid in dependency_list %}
# flux: --dependency=afterok:{{jobid}}
{%- endfor %}
{%- endif %}
{{command}}
"""
def echo(i):
sleep(1)
return i
def long_running_function(i):
sleep(10)
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 stop_function():
return True
def add_with_sleep(parameter_1, parameter_2):
sleep(1)
return parameter_1 + parameter_2
@unittest.skipIf(
skip_flux_test or skip_mpi4py_test,
"h5py or mpi4py or flux are not installed, so the h5py, flux and mpi4py tests are skipped.",
)
class TestCacheExecutorPysqa(unittest.TestCase):
def test_executor(self):
with FluxClusterExecutor(
resource_dict={"cores": 2, "cwd": "executorlib_cache"},
block_allocation=False,
cache_directory="executorlib_cache",
pmi_mode=pmi,
) as exe:
cloudpickle_register(ind=1)
fs1 = exe.submit(mpi_funct, 1)
self.assertFalse(fs1.done())
self.assertEqual(fs1.result(), [(1, 2, 0), (1, 2, 1)])
self.assertEqual(len(os.listdir("executorlib_cache")), 4)
self.assertTrue(fs1.done())
def test_executor_wrong_queue_name(self):
with self.assertRaises(ValueError):
with FluxClusterExecutor(
resource_dict={
"cores": 2,
"cwd": "executorlib_cache",
"submission_template": template,
"queue": "test",
},
block_allocation=False,
cache_directory="executorlib_cache",
pmi_mode=pmi,
) as exe:
cloudpickle_register(ind=1)
fs1 = exe.submit(mpi_funct, 1)
print(fs1.result())
def test_executor_no_cwd(self):
with FluxClusterExecutor(
resource_dict={"cores": 2},
block_allocation=False,
cache_directory="executorlib_cache",
pmi_mode=pmi,
) as exe:
cloudpickle_register(ind=1)
fs1 = exe.submit(mpi_funct, 1)
self.assertFalse(fs1.done())
self.assertEqual(fs1.result(), [(1, 2, 0), (1, 2, 1)])
self.assertEqual(len(os.listdir("executorlib_cache")), 4)
self.assertTrue(fs1.done())
def test_executor_blockallocation(self):
with FluxClusterExecutor(
resource_dict={"cores": 2, "cwd": "executorlib_cache"},
block_allocation=True,
cache_directory="executorlib_cache",
pmi_mode=pmi,
max_workers=1,
) as exe:
cloudpickle_register(ind=1)
fs1 = exe.submit(mpi_funct, 1)
self.assertFalse(fs1.done())
self.assertEqual(fs1.result(), [(1, 2, 0), (1, 2, 1)])
self.assertEqual(len(os.listdir("executorlib_cache")), 2)
self.assertTrue(fs1.done())
def test_executor_blockallocation_no_cwd(self):
with FluxClusterExecutor(
resource_dict={"cores": 2},
block_allocation=True,
cache_directory="executorlib_cache",
pmi_mode=pmi,
max_workers=1,
) as exe:
cloudpickle_register(ind=1)
fs1 = exe.submit(mpi_funct, 1)
self.assertFalse(fs1.done())
self.assertEqual(fs1.result(), [(1, 2, 0), (1, 2, 1)])
self.assertEqual(len(os.listdir("executorlib_cache")), 2)
self.assertTrue(fs1.done())
def test_executor_dependencies(self):
with FluxClusterExecutor(
resource_dict={"cores": 1, "cwd": "executorlib_cache"},
block_allocation=False,
cache_directory="executorlib_cache",
pmi_mode=pmi,
) as exe:
fs1 = exe.submit(add_with_sleep, 1, 1)
fs2 = exe.submit(add_with_sleep, fs1, 1)
fs3 = exe.submit(add_with_sleep, fs1, fs2)
self.assertFalse(fs1.done())
self.assertFalse(fs2.done())
self.assertFalse(fs3.done())
self.assertEqual(fs1.result(), 2)
self.assertEqual(fs2.result(), 3)
self.assertEqual(fs3.result(), 5)
self.assertEqual(len(os.listdir("executorlib_cache")), 6)
self.assertTrue(fs1.done())
self.assertTrue(fs2.done())
self.assertTrue(fs3.done())
def test_executor_blockallocation_echo(self):
with FluxClusterExecutor(
resource_dict={"cores": 1, "cwd": "executorlib_cache"},
block_allocation=True,
cache_directory="executorlib_cache",
pmi_mode=pmi,
max_workers=2,
) as exe:
cloudpickle_register(ind=1)
fs1 = exe.submit(echo, 1)
fs2 = exe.submit(echo, 2)
self.assertFalse(fs1.done())
self.assertFalse(fs2.done())
self.assertEqual(fs1.result(), 1)
self.assertEqual(fs2.result(), 2)
self.assertEqual(len(os.listdir("executorlib_cache")), 4)
self.assertTrue(fs1.done())
self.assertTrue(fs2.done())
def test_executor_cancel_future_on_shutdown(self):
with FluxClusterExecutor(
resource_dict={"cores": 1, "cwd": "executorlib_cache"},
block_allocation=False,
cache_directory="executorlib_cache",
pmi_mode=pmi,
wait=False,
) as exe:
cloudpickle_register(ind=1)
fs1 = exe.submit(echo, 1)
self.assertFalse(fs1.done())
self.assertTrue(fs1.cancelled())
sleep(2)
with FluxClusterExecutor(
resource_dict={"cores": 1, "cwd": "executorlib_cache"},
block_allocation=False,
cache_directory="executorlib_cache",
pmi_mode=pmi,
wait=False,
) as exe:
cloudpickle_register(ind=1)
fs1 = exe.submit(echo, 1)
self.assertEqual(fs1.result(), 1)
self.assertEqual(len(os.listdir("executorlib_cache")), 4)
self.assertTrue(fs1.done())
def test_executor_no_cwd(self):
with FluxClusterExecutor(
resource_dict={"cores": 2},
block_allocation=False,
cache_directory="executorlib_cache",
pmi_mode=pmi,
) as exe:
cloudpickle_register(ind=1)
fs1 = exe.submit(mpi_funct, 1)
self.assertFalse(fs1.done())
self.assertEqual(fs1.result(), [(1, 2, 0), (1, 2, 1)])
self.assertEqual(len(os.listdir("executorlib_cache")), 2)
self.assertTrue(fs1.done())
def test_pysqa_interface(self):
queue_id = execute_with_pysqa(
command=get_cache_execute_command(
file_name="test_i.h5",
cores=1,
),
file_name="test_i.h5",
data_dict={"fn": sleep, "args": (10,)},
resource_dict={"cores": 1},
cache_directory="executorlib_cache",
backend="flux"
)
self.assertIsNone(pysqa_terminate(queue_id=queue_id, backend="flux"))
def test_executor_existing_files(self):
with FluxClusterExecutor(
resource_dict={"cores": 2, "cwd": "executorlib_cache"},
block_allocation=False,
cache_directory="executorlib_cache",
pmi_mode=pmi,
) as exe:
cloudpickle_register(ind=1)
fs1 = exe.submit(mpi_funct, 1)
self.assertFalse(fs1.done())
self.assertEqual(fs1.result(), [(1, 2, 0), (1, 2, 1)])
self.assertTrue(fs1.done())
self.assertEqual(len(os.listdir("executorlib_cache")), 4)
for file_name in os.listdir("executorlib_cache"):
file_path = os.path.join("executorlib_cache", file_name )
os.remove(file_path)
if ".h5" in file_path:
task_key = file_path[:-5] + "_i.h5"
dump(file_name=task_key, data_dict={"a": 1})
with FluxClusterExecutor(
resource_dict={"cores": 2, "cwd": "executorlib_cache"},
block_allocation=False,
cache_directory="executorlib_cache",
pmi_mode=pmi,
) as exe:
cloudpickle_register(ind=1)
fs1 = exe.submit(mpi_funct, 1)
self.assertFalse(fs1.done())
self.assertEqual(fs1.result(), [(1, 2, 0), (1, 2, 1)])
self.assertTrue(fs1.done())
self.assertEqual(len(os.listdir("executorlib_cache")), 4)
def test_terminate_tasks_in_cache(self):
file = os.path.join("executorlib_cache", "test_i.h5")
dump(file_name=file, data_dict={"queue_id": 1})
self.assertIsNone(terminate_tasks_in_cache(
cache_directory="executorlib_cache",
backend="flux",
))
def test_terminate_task_in_cache(self):
file = os.path.join("executorlib_cache", "test_i.h5")
dump(file_name=file, data_dict={"queue_id": 1})
self.assertTrue(os.path.exists(file))
self.assertIsNone(terminate_task_in_cache(
cache_directory="executorlib_cache",
cache_key="test",
backend="flux"
))
self.assertFalse(os.path.exists(file))
def tearDown(self):
shutil.rmtree("executorlib_cache", ignore_errors=True)
@unittest.skipIf(
skip_flux_test,
"flux is not installed, so the flux tests are skipped.",
)
class TestPysqaSpawner(unittest.TestCase):
def test_pysqa_spawner_sleep(self):
interface_flux = PysqaSpawner(backend="flux", cores=1)
self.assertTrue(interface_flux.bootup(command_lst=["sleep", "1"]))
self.assertTrue(interface_flux._check_process_helper(command_lst=[]))
self.assertTrue(interface_flux.poll())
process_id = interface_flux._process
interface_flux.shutdown(wait=True)
interface_flux._process = process_id
self.assertFalse(interface_flux.poll())
self.assertFalse(interface_flux._check_process_helper(command_lst=["sleep", "1"]))
def test_pysqa_spawner_big(self):
interface_flux = PysqaSpawner(backend="flux", cores=100)
self.assertFalse(interface_flux.bootup(command_lst=["sleep", "1"], stop_function=stop_function))