| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
""" |
| 8 |
Test MPI utility functions in mpiplus.py. |
| 9 |
|
| 10 |
""" |
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
import pytest |
| 17 |
from mpiplus import * |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
NODE_RANK = 0 |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
def assert_is_equal(a, b): |
| 32 |
err_msg = '{} != {}'.format(a, b) |
| 33 |
try: |
| 34 |
assert a == b, err_msg |
| 35 |
except ValueError: |
| 36 |
|
| 37 |
try: |
| 38 |
for element_a, element_b in zip(a, b): |
| 39 |
assert_is_equal(element_a, element_b) |
| 40 |
except AssertionError: |
| 41 |
raise AssertionError(err_msg) |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
def square(x): |
| 49 |
return x**2 |
| 50 |
|
| 51 |
|
| 52 |
def multiply(a, b): |
| 53 |
return a * b |
| 54 |
|
| 55 |
|
| 56 |
@on_single_node(rank=NODE_RANK, broadcast_result=True) |
| 57 |
def multiply_decorated_broadcast(a, b): |
| 58 |
return multiply(a, b) |
| 59 |
|
| 60 |
|
| 61 |
@on_single_node(rank=NODE_RANK, broadcast_result=False) |
| 62 |
def multiply_decorated_nobroadcast(a, b): |
| 63 |
return multiply(a, b) |
| 64 |
|
| 65 |
|
| 66 |
class MyClass(object): |
| 67 |
|
| 68 |
def __init__(self, par): |
| 69 |
self.par = par |
| 70 |
|
| 71 |
@staticmethod |
| 72 |
def square_static(x): |
| 73 |
return x**2 |
| 74 |
|
| 75 |
@staticmethod |
| 76 |
def multiply_static(a, b): |
| 77 |
return a * b |
| 78 |
|
| 79 |
@staticmethod |
| 80 |
@on_single_node(rank=NODE_RANK, broadcast_result=True) |
| 81 |
def multiply_decorated_broadcast_static(a, b): |
| 82 |
return a * b |
| 83 |
|
| 84 |
@classmethod |
| 85 |
@on_single_node(rank=NODE_RANK, broadcast_result=False) |
| 86 |
def multiply_decorated_nobroadcast_static(cls, a, b): |
| 87 |
return cls.multiply_static(a, b) |
| 88 |
|
| 89 |
def multiply_by_par(self, a): |
| 90 |
return self.par * a |
| 91 |
|
| 92 |
@on_single_node(rank=NODE_RANK, broadcast_result=True) |
| 93 |
def multiply_by_par_decorated_broadcast(self, a): |
| 94 |
return self.multiply_by_par(a) |
| 95 |
|
| 96 |
@on_single_node(rank=NODE_RANK, broadcast_result=False) |
| 97 |
def multiply_by_par_decorated_nobroadcast(self, a): |
| 98 |
return self.multiply_by_par(a) |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
def test_run_single_node(): |
| 106 |
"""Test run_single_node function.""" |
| 107 |
mpicomm = get_mpicomm() |
| 108 |
my_instance = MyClass(3.0) |
| 109 |
|
| 110 |
|
| 111 |
test_cases = [ |
| 112 |
(multiply, (3, 4), {}), |
| 113 |
(multiply, (), {'a': 5, 'b': 4}), |
| 114 |
(multiply, (2, 'teststring',), {}), |
| 115 |
(multiply, (5, [3, 4],), {}), |
| 116 |
(multiply, (4, np.array([3, 4, 5.0]),), {}), |
| 117 |
(square, (5,), {}), |
| 118 |
(square, (), {'x': 2}), |
| 119 |
(MyClass.multiply_static, (3, 4), {}), |
| 120 |
(MyClass.multiply_static, (), {'a': 5, 'b': 4}), |
| 121 |
(MyClass.square_static, (5,), {}), |
| 122 |
(MyClass.square_static, (), {'x': 2}), |
| 123 |
(my_instance.multiply_by_par, (4,), {}), |
| 124 |
(my_instance.multiply_by_par, (), {'a': 2}), |
| 125 |
] |
| 126 |
|
| 127 |
for task, args, kwargs in test_cases: |
| 128 |
expected_result = task(*args, **kwargs) |
| 129 |
for broadcast_result in [True, False]: |
| 130 |
result = run_single_node(NODE_RANK, task, *args, |
| 131 |
broadcast_result=broadcast_result, **kwargs) |
| 132 |
if not broadcast_result and mpicomm is not None and mpicomm.rank != NODE_RANK: |
| 133 |
assert result is None |
| 134 |
else: |
| 135 |
assert_is_equal(result, expected_result) |
| 136 |
|
| 137 |
|
| 138 |
def test_on_single_node(): |
| 139 |
"""Test on_single_node decorator.""" |
| 140 |
mpicomm = get_mpicomm() |
| 141 |
my_instance = MyClass(3.0) |
| 142 |
|
| 143 |
|
| 144 |
test_cases = [ |
| 145 |
(multiply_decorated_broadcast, (3, 4), {}, True, 12), |
| 146 |
(multiply_decorated_nobroadcast, (), {'a': 5, 'b': 5}, False, 25), |
| 147 |
(MyClass.multiply_decorated_broadcast_static, (3, 4), {}, True, 12), |
| 148 |
(MyClass.multiply_decorated_nobroadcast_static, (), {'a': 5, 'b': 5}, False, 25), |
| 149 |
(my_instance.multiply_by_par_decorated_broadcast, (4,), {}, True, 4 * my_instance.par), |
| 150 |
(my_instance.multiply_by_par_decorated_broadcast, (), {'a': 5}, True, 5 * my_instance.par), |
| 151 |
] |
| 152 |
|
| 153 |
for task, args, kwargs, broadcast_result, expected_result in test_cases: |
| 154 |
result = task(*args, **kwargs) |
| 155 |
if not broadcast_result and mpicomm is not None and mpicomm.rank != NODE_RANK: |
| 156 |
assert result is None |
| 157 |
else: |
| 158 |
assert_is_equal(result, expected_result) |
| 159 |
|
| 160 |
|
| 161 |
def test_distribute(): |
| 162 |
"""Test distribute function.""" |
| 163 |
mpicomm = get_mpicomm() |
| 164 |
my_instance = MyClass(4) |
| 165 |
|
| 166 |
|
| 167 |
test_cases = [ |
| 168 |
(square, [1, 2, 3]), |
| 169 |
(MyClass.square_static, [1, 2, 3, 4]), |
| 170 |
(my_instance.multiply_by_par, [1, 2, 3, 4, 5]), |
| 171 |
(my_instance.multiply_by_par, ['a', 'b', 'c', 'd', 'e', 'f']), |
| 172 |
(my_instance.multiply_by_par, [[1, 2], [3, 4], [5, 6], [7, 8], [9, 10]]), |
| 173 |
(my_instance.multiply_by_par, [np.array([1, 2]), np.array([3, 4]), np.array([5, 6]), np.array([7, 8])]), |
| 174 |
] |
| 175 |
|
| 176 |
for task, distributed_args in test_cases: |
| 177 |
all_indices = list(range(len(distributed_args))) |
| 178 |
all_expected_results = [task(x) for x in distributed_args] |
| 179 |
|
| 180 |
|
| 181 |
if mpicomm is not None: |
| 182 |
partial_job_indices = list(range(mpicomm.rank, len(distributed_args), mpicomm.size)) |
| 183 |
else: |
| 184 |
partial_job_indices = all_indices |
| 185 |
partial_expected_results = [all_expected_results[i] for i in partial_job_indices] |
| 186 |
|
| 187 |
result = distribute(task, distributed_args, send_results_to='all') |
| 188 |
assert_is_equal(result, all_expected_results) |
| 189 |
|
| 190 |
result = distribute(task, distributed_args, send_results_to=NODE_RANK) |
| 191 |
if mpicomm is not None and mpicomm.rank != NODE_RANK: |
| 192 |
assert_is_equal(result, (partial_expected_results, partial_job_indices)) |
| 193 |
else: |
| 194 |
assert_is_equal(result, (all_expected_results, all_indices)) |
| 195 |
|
| 196 |
result = distribute(task, distributed_args, send_results_to=None) |
| 197 |
assert_is_equal(result, (partial_expected_results, partial_job_indices)) |
| 198 |
|
| 199 |
|
| 200 |
def test_distribute_groups(): |
| 201 |
"""Test distribute jobs among groups of nodes.""" |
| 202 |
|
| 203 |
group_size = 2 |
| 204 |
list_of_supertask_args = [[1, 2], [3, 4, 5], [6, 7, 8, 9]] |
| 205 |
|
| 206 |
def supertask(list_of_bases): |
| 207 |
"""Compute square of all the bases.""" |
| 208 |
squared_values = distribute(square, list_of_bases, send_results_to='all') |
| 209 |
mpicomm = get_mpicomm() |
| 210 |
if mpicomm is None: |
| 211 |
mpi_size = 0 |
| 212 |
else: |
| 213 |
mpi_size = mpicomm.size |
| 214 |
return squared_values, mpi_size |
| 215 |
|
| 216 |
|
| 217 |
all_results = distribute(supertask, distributed_args=list_of_supertask_args, sync_nodes=True, |
| 218 |
send_results_to='all', group_size=group_size) |
| 219 |
|
| 220 |
mpicomm = get_mpicomm() |
| 221 |
n_jobs = len(list_of_supertask_args) |
| 222 |
|
| 223 |
|
| 224 |
if mpicomm is not None: |
| 225 |
n_groups = int(np.ceil(mpicomm.size / group_size)) |
| 226 |
last_group_size = group_size - mpicomm.size % group_size |
| 227 |
last_group_job_ids = set(range(n_groups-1, n_jobs, n_groups)) |
| 228 |
|
| 229 |
|
| 230 |
for job_id, (supertask_args, job_result) in enumerate(zip(list_of_supertask_args, all_results)): |
| 231 |
squared_values, mpi_size = job_result |
| 232 |
|
| 233 |
|
| 234 |
assert len(supertask_args) == len(squared_values) |
| 235 |
for idx, value in enumerate(squared_values): |
| 236 |
assert value == supertask_args[idx]**2 |
| 237 |
|
| 238 |
|
| 239 |
if mpicomm is None: |
| 240 |
expected_mpi_size = 0 |
| 241 |
elif job_id in last_group_job_ids: |
| 242 |
expected_mpi_size = last_group_size |
| 243 |
else: |
| 244 |
expected_mpi_size = 2 |
| 245 |
assert mpi_size == expected_mpi_size |
| 246 |
|
| 247 |
|
| 248 |
def test_exception_handling(): |
| 249 |
"""distribute() propagates exceptions correctly.""" |
| 250 |
group_size = 2 |
| 251 |
mpicomm = get_mpicomm() |
| 252 |
|
| 253 |
def task(arg): |
| 254 |
if arg == 0 and (mpicomm is None or get_mpicomm().rank == 0): |
| 255 |
raise RuntimeError(str(arg)) |
| 256 |
return |
| 257 |
|
| 258 |
def call_distribute(propagate_exceptions_to, send_results_to=None): |
| 259 |
distribute(task, distributed_args=list(range(2*group_size)), send_results_to=send_results_to, |
| 260 |
propagate_exceptions_to=propagate_exceptions_to, group_size=group_size) |
| 261 |
|
| 262 |
|
| 263 |
with pytest.raises(RuntimeError): |
| 264 |
call_distribute(propagate_exceptions_to='all', send_results_to='all') |
| 265 |
|
| 266 |
|
| 267 |
if mpicomm is not None: |
| 268 |
with pytest.raises(ValueError): |
| 269 |
call_distribute(propagate_exceptions_to='group', send_results_to='all') |
| 270 |
|
| 271 |
|
| 272 |
if mpicomm is None or mpicomm.rank in list(range(group_size)): |
| 273 |
with pytest.raises(RuntimeError): |
| 274 |
call_distribute(propagate_exceptions_to='group') |
| 275 |
else: |
| 276 |
call_distribute(propagate_exceptions_to='group') |
| 277 |
|
| 278 |
|
| 279 |
if mpicomm is None or mpicomm.rank == 0: |
| 280 |
with pytest.raises(RuntimeError): |
| 281 |
call_distribute(propagate_exceptions_to=None) |
| 282 |
else: |
| 283 |
call_distribute(propagate_exceptions_to=None) |