|
1 | 1 | import abc |
| 2 | +import copy |
2 | 3 | import dataclasses |
3 | 4 | import os |
4 | | -from typing import Any, ClassVar, List |
5 | | - |
6 | | -from ndsl import MPIComm, NullComm |
7 | | -from ndsl.comm import CachingCommReader, CachingCommWriter, Comm |
| 5 | +from typing import Any, ClassVar, List, Mapping, TypeVar, cast |
| 6 | + |
| 7 | +from ndsl import MPIComm |
| 8 | +from ndsl.comm import ( |
| 9 | + CachingCommReader, |
| 10 | + CachingCommWriter, |
| 11 | + Comm, |
| 12 | + ReductionOperator, |
| 13 | + Request, |
| 14 | +) |
8 | 15 | from pace.registry import Registry |
9 | 16 |
|
10 | 17 |
|
| 18 | +T = TypeVar("T") |
| 19 | + |
| 20 | + |
| 21 | +class NullAsyncResult(Request): |
| 22 | + def __init__(self, recvbuf: Any = None) -> None: |
| 23 | + self._recvbuf = recvbuf |
| 24 | + |
| 25 | + def wait(self) -> None: |
| 26 | + if self._recvbuf is not None: |
| 27 | + self._recvbuf[:] = 0.0 |
| 28 | + |
| 29 | + |
| 30 | +class NullComm(Comm[T]): |
| 31 | + """ |
| 32 | + A class with a subset of the mpi4py Comm API, but which |
| 33 | + 'receives' a fill value (default zero) instead of using MPI. |
| 34 | + """ |
| 35 | + |
| 36 | + default_fill_value: T = cast(T, 0) |
| 37 | + |
| 38 | + def __init__(self, rank: int, total_ranks: int, fill_value: T = default_fill_value): |
| 39 | + """ |
| 40 | + Args: |
| 41 | + rank: rank to mock |
| 42 | + total_ranks: number of total MPI ranks to mock |
| 43 | + fill_value: fill halos with this value when performing |
| 44 | + halo updates. |
| 45 | + """ |
| 46 | + self.rank = rank |
| 47 | + self.total_ranks = total_ranks |
| 48 | + self._fill_value = fill_value |
| 49 | + self._split_comms: Mapping[Any, list[NullComm]] = {} |
| 50 | + |
| 51 | + def __repr__(self) -> str: |
| 52 | + return f"NullComm(rank={self.rank}, total_ranks={self.total_ranks})" |
| 53 | + |
| 54 | + def Get_rank(self) -> int: |
| 55 | + return self.rank |
| 56 | + |
| 57 | + def Get_size(self) -> int: |
| 58 | + return self.total_ranks |
| 59 | + |
| 60 | + def bcast(self, value: T | None, root: int = 0) -> T | None: |
| 61 | + return value |
| 62 | + |
| 63 | + def barrier(self) -> None: |
| 64 | + return |
| 65 | + |
| 66 | + def Barrier(self) -> None: |
| 67 | + return |
| 68 | + |
| 69 | + def Scatter(self, sendbuf, recvbuf, root: int = 0, **kwargs: dict): # type: ignore[no-untyped-def] |
| 70 | + if recvbuf is not None: |
| 71 | + recvbuf[:] = self._fill_value |
| 72 | + |
| 73 | + def Gather(self, sendbuf, recvbuf, root: int = 0, **kwargs: dict): # type: ignore[no-untyped-def] |
| 74 | + if recvbuf is not None: |
| 75 | + recvbuf[:] = self._fill_value |
| 76 | + |
| 77 | + def allgather(self, sendobj: T) -> list[T]: |
| 78 | + return [copy.deepcopy(sendobj) for _ in range(self.total_ranks)] |
| 79 | + |
| 80 | + def Send(self, sendbuf, dest, tag: int = 0, **kwargs: dict): # type: ignore[no-untyped-def] |
| 81 | + pass |
| 82 | + |
| 83 | + def Isend(self, sendbuf, dest, tag: int = 0, **kwargs: dict) -> Request: # type: ignore[no-untyped-def] |
| 84 | + return NullAsyncResult() |
| 85 | + |
| 86 | + def Recv(self, recvbuf, source, tag: int = 0, **kwargs: dict): # type: ignore[no-untyped-def] |
| 87 | + recvbuf[:] = self._fill_value |
| 88 | + |
| 89 | + def Irecv(self, recvbuf, source, tag: int = 0, **kwargs: dict) -> Request: # type: ignore[no-untyped-def] |
| 90 | + return NullAsyncResult(recvbuf) |
| 91 | + |
| 92 | + def sendrecv(self, sendbuf, dest, **kwargs: dict): # type: ignore[no-untyped-def] |
| 93 | + return sendbuf |
| 94 | + |
| 95 | + def Split(self, color, key) -> Comm: # type: ignore[no-untyped-def] |
| 96 | + # key argument is ignored, assumes we're calling the ranks from least to |
| 97 | + # greatest when mocking Split |
| 98 | + self._split_comms[color] = self._split_comms.get(color, []) # type: ignore[index] |
| 99 | + rank = len(self._split_comms[color]) |
| 100 | + total_ranks = rank + 1 |
| 101 | + new_comm = NullComm( |
| 102 | + rank=rank, total_ranks=total_ranks, fill_value=self._fill_value |
| 103 | + ) |
| 104 | + for comm in self._split_comms[color]: |
| 105 | + # won't know how many ranks there are until everything is split |
| 106 | + comm.total_ranks = total_ranks |
| 107 | + self._split_comms[color].append(new_comm) |
| 108 | + return new_comm |
| 109 | + |
| 110 | + def allreduce( |
| 111 | + self, sendobj: T, op: ReductionOperator = ReductionOperator.NO_OP |
| 112 | + ) -> T: |
| 113 | + return self._fill_value |
| 114 | + |
| 115 | + def Allreduce(self, sendobj: T, recvobj: T, op: ReductionOperator) -> T: |
| 116 | + # TODO: what about reduction operator `op`? |
| 117 | + recvobj = sendobj |
| 118 | + return recvobj |
| 119 | + |
| 120 | + def Allreduce_inplace(self, obj: T, op: ReductionOperator) -> T: |
| 121 | + raise NotImplementedError("NullComm.Allreduce_inplace") |
| 122 | + |
| 123 | + |
11 | 124 | class CreatesComm(abc.ABC): |
12 | 125 | """ |
13 | 126 | Retrieves and does cleanup for a mpi4py-style Comm object. |
|
0 commit comments