-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathtest_single_mpi.py
More file actions
153 lines (128 loc) · 4.9 KB
/
Copy pathtest_single_mpi.py
File metadata and controls
153 lines (128 loc) · 4.9 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
import os
import importlib.util
import shutil
import time
import unittest
from executorlib import SingleNodeExecutor, SlurmJobExecutor
from executorlib.standalone.serialize import cloudpickle_register
skip_mpi4py_test = importlib.util.find_spec("mpi4py") is None
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 mpi_funct_sleep(i):
from mpi4py import MPI
size = MPI.COMM_WORLD.Get_size()
rank = MPI.COMM_WORLD.Get_rank()
time.sleep(i)
return i, size, rank
class TestExecutorBackend(unittest.TestCase):
def test_block_allocation_serial_two_cores(self):
with SingleNodeExecutor(max_cores=2, block_allocation=True) 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_block_allocation_serial_one_core(self):
with SingleNodeExecutor(max_cores=1, block_allocation=True) 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_oversubscribe(self):
with self.assertRaises(ValueError):
with SingleNodeExecutor(max_cores=1, block_allocation=True) as exe:
cloudpickle_register(ind=1)
fs_1 = exe.submit(calc, 1, resource_dict={"cores": 2})
@unittest.skipIf(
skip_mpi4py_test, "mpi4py is not installed, so the mpi4py tests are skipped."
)
def test_block_allocation_mpi_two_cores(self):
with SingleNodeExecutor(
max_workers=2,
resource_dict={"cores": 2},
block_allocation=True,
) 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_invalid_constructor_arguments_raise_errors(self):
with self.assertRaises(TypeError):
SingleNodeExecutor(
max_cores=1,
resource_dict={"cores": 1, "gpus_per_core": 1},
)
class TestExecutorBackendCache(unittest.TestCase):
def tearDown(self):
shutil.rmtree("executorlib_cache", ignore_errors=True)
@unittest.skipIf(
skip_mpi4py_test, "mpi4py is not installed, so the mpi4py tests are skipped."
)
def test_block_allocation_mpi_two_cores_with_cache(self):
with SingleNodeExecutor(
max_workers=2,
resource_dict={"cores": 2},
block_allocation=True,
cache_directory="executorlib_cache",
) as exe:
cloudpickle_register(ind=1)
time_1 = time.time()
fs_1 = exe.submit(mpi_funct_sleep, 1)
self.assertEqual(fs_1.result(), [(1, 2, 0), (1, 2, 1)])
self.assertTrue(fs_1.done())
time_2 = time.time()
self.assertTrue(time_2 - time_1 > 1)
time_3 = time.time()
fs_2 = exe.submit(mpi_funct_sleep, 1)
self.assertEqual(fs_2.result(), [(1, 2, 0), (1, 2, 1)])
self.assertTrue(fs_2.done())
time_4 = time.time()
self.assertTrue(time_3 - time_4 < 1)
class TestWorkingDirectory(unittest.TestCase):
def test_output_files_cwd(self):
dirname = os.path.abspath(os.path.dirname(__file__))
os.makedirs(dirname, exist_ok=True)
with SingleNodeExecutor(
max_cores=1,
resource_dict={"cores": 1, "cwd": dirname},
block_allocation=True,
) as p:
output = list(p.map(calc, [1, 2, 3]))
self.assertEqual(
output,
[1, 2, 3],
)
def test_map_futures(self):
dirname = os.path.abspath(os.path.dirname(__file__))
os.makedirs(dirname, exist_ok=True)
with SingleNodeExecutor(
max_cores=1,
resource_dict={"cores": 1, "cwd": dirname},
block_allocation=True,
) as p:
calc_lst = p.submit(calc, [1, 2, 3])
output = list(p.map(calc, calc_lst))
self.assertEqual(
output,
[1, 2, 3],
)
class TestSLURMExecutor(unittest.TestCase):
def test_validate_max_workers(self):
os.environ["SLURM_NTASKS"] = "6"
os.environ["SLURM_CPUS_PER_TASK"] = "4"
with self.assertRaises(ValueError):
SlurmJobExecutor(
max_workers=10,
resource_dict={"cores": 10, "threads_per_core": 10},
block_allocation=True,
)