-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathtest_flux_job.py
More file actions
175 lines (150 loc) · 5.22 KB
/
Copy pathtest_flux_job.py
File metadata and controls
175 lines (150 loc) · 5.22 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
import os
import unittest
import numpy as np
from executorlib import FluxJobExecutor
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 TestFluxBackend(unittest.TestCase):
def setUp(self):
self.executor = flux.job.FluxExecutor()
def test_flux_executor_serial(self):
with FluxJobExecutor(
max_cores=2,
flux_executor=self.executor,
block_allocation=True,
) 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_serial_no_depencies(self):
with FluxJobExecutor(
max_cores=2,
flux_executor=self.executor,
block_allocation=True,
disable_dependencies=True,
) 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 FluxJobExecutor(
max_cores=1,
resource_dict={"threads_per_core": 2},
flux_executor=self.executor,
block_allocation=True,
) 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 FluxJobExecutor(
max_cores=2,
resource_dict={"cores": 2},
flux_executor=self.executor,
block_allocation=True,
pmi_mode=pmi,
) 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 FluxJobExecutor(
max_cores=2,
resource_dict={"cores": 2},
flux_executor=self.executor,
block_allocation=True,
pmi_mode=pmi,
) 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_output_files_cwd(self):
dirname = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", ".."))
os.makedirs(dirname, exist_ok=True)
file_stdout = os.path.join(dirname, "flux.out")
file_stderr = os.path.join(dirname, "flux.err")
with FluxJobExecutor(
max_cores=1,
resource_dict={"cores": 1, "cwd": dirname},
flux_executor=self.executor,
block_allocation=True,
flux_log_files=True,
) as p:
output = list(p.map(calc, [1, 2, 3]))
self.assertEqual(
output,
[1, 2, 3],
)
self.assertTrue(os.path.exists(file_stdout))
self.assertTrue(os.path.exists(file_stderr))
os.remove(file_stdout)
os.remove(file_stderr)
def test_output_files_abs(self):
file_stdout = os.path.abspath("flux.out")
file_stderr = os.path.abspath("flux.err")
with FluxJobExecutor(
max_cores=1,
resource_dict={"cores": 1},
flux_executor=self.executor,
block_allocation=True,
flux_log_files=True,
) as p:
output = list(p.map(calc, [1, 2, 3]))
self.assertEqual(
output,
[1, 2, 3],
)
self.assertTrue(os.path.exists(file_stdout))
self.assertTrue(os.path.exists(file_stderr))
os.remove(file_stdout)
os.remove(file_stderr)
def test_internal_memory(self):
with FluxJobExecutor(
max_cores=1,
resource_dict={"cores": 1},
init_function=set_global,
flux_executor=self.executor,
block_allocation=True,
) as p:
f = p.submit(get_global)
self.assertFalse(f.done())
self.assertEqual(f.result(), np.array([5]))
self.assertTrue(f.done())
def test_validate_max_workers(self):
with self.assertRaises(ValueError):
FluxJobExecutor(
max_workers=10,
resource_dict={"cores": 10, "threads_per_core": 10},
flux_executor=self.executor,
block_allocation=True,
)