|
| 1 | +from collections.abc import Generator |
| 2 | +from concurrent.futures import Future |
| 3 | + |
| 4 | +from ...utils import ArrayProtocol, FutureProtocol, CudaArrayProtocol, cuda_to_numpy |
| 5 | + |
| 6 | + |
| 7 | +class FutureArray(Future): |
| 8 | + def __init__(self, shape, dtype, timeout: float = 1.0): |
| 9 | + self._shape = shape |
| 10 | + self._dtype = dtype |
| 11 | + self._timeout = timeout |
| 12 | + |
| 13 | + super().__init__() |
| 14 | + |
| 15 | + @property |
| 16 | + def shape(self) -> tuple[int, ...]: |
| 17 | + return self._shape |
| 18 | + |
| 19 | + @property |
| 20 | + def ndim(self) -> int: |
| 21 | + return len(self.shape) |
| 22 | + |
| 23 | + @property |
| 24 | + def dtype(self) -> str: |
| 25 | + return self._dtype |
| 26 | + |
| 27 | + def __getitem__(self, item) -> ArrayProtocol: |
| 28 | + return self.result(self._timeout)[item] |
| 29 | + |
| 30 | + def __array__(self) -> ArrayProtocol: |
| 31 | + return self.result(self._timeout) |
| 32 | + |
| 33 | + def __array_ufunc__(self, ufunc, method, *inputs, **kwargs): |
| 34 | + raise NotImplementedError |
| 35 | + |
| 36 | + def __array_function__(self, func, types, *args, **kwargs): |
| 37 | + raise NotImplementedError |
| 38 | + |
| 39 | + |
| 40 | +# inspired by https://www.dabeaz.com/coroutines/ |
| 41 | +def start_coroutine(func): |
| 42 | + """ |
| 43 | + Starts coroutines for async arrays wrapped by NDProcessor. |
| 44 | + Used by all NDGraphic.set_indices and NDGraphic._create_graphic. |
| 45 | +
|
| 46 | + It also immediately starts coroutines unless block=False is provided. It handles all the triage of possible |
| 47 | + sync vs. async (Future-like) objects. |
| 48 | +
|
| 49 | + The only time when block=False is when ReferenceIndex._render_indices uses it to loop through setting all |
| 50 | + indices, and then collect and send the results back down to NDProcessor.get(). |
| 51 | + """ |
| 52 | + |
| 53 | + def start( |
| 54 | + self, *args, **kwargs |
| 55 | + ) -> tuple[Generator, ArrayProtocol | CudaArrayProtocol | FutureProtocol] | None: |
| 56 | + cr = func(self, *args, **kwargs) |
| 57 | + try: |
| 58 | + # begin coroutine |
| 59 | + to_resolve: FutureProtocol | ArrayProtocol | CudaArrayProtocol = cr.send( |
| 60 | + None |
| 61 | + ) |
| 62 | + except StopIteration: |
| 63 | + # NDProcessor.get() has no `yield` expression, not async, nothing to return |
| 64 | + return None |
| 65 | + |
| 66 | + block = kwargs.get("block", True) |
| 67 | + timeout = kwargs.get("timeout", 1.0) |
| 68 | + |
| 69 | + if block: # resolve Future immediately |
| 70 | + try: |
| 71 | + if isinstance(to_resolve, FutureProtocol): |
| 72 | + # array is async, resolve future and send |
| 73 | + cr.send(to_resolve.result(timeout=timeout)) |
| 74 | + elif isinstance(to_resolve, CudaArrayProtocol): |
| 75 | + # array is on GPU, it is technically and on GPU, convert to numpy array on CPU |
| 76 | + cr.send(cuda_to_numpy(to_resolve)) |
| 77 | + else: |
| 78 | + # not async, just send the array |
| 79 | + cr.send(to_resolve) |
| 80 | + except StopIteration: |
| 81 | + pass |
| 82 | + |
| 83 | + else: # no block, probably resolving multiple futures simultaneously |
| 84 | + if isinstance(to_resolve, FutureProtocol): |
| 85 | + # data is async, return coroutine generator and future |
| 86 | + # ReferenceIndex._render_indices() will manage them and wait to gather all futures |
| 87 | + return cr, to_resolve |
| 88 | + elif isinstance(to_resolve, CudaArrayProtocol): |
| 89 | + # it is async technically, but it's a GPU array, ReferenceIndex._render_indices will manage it |
| 90 | + return cr, to_resolve |
| 91 | + else: |
| 92 | + # not async, just send the array |
| 93 | + try: |
| 94 | + cr.send(to_resolve) |
| 95 | + except ( |
| 96 | + StopIteration |
| 97 | + ): # has to be here because of the yield expression, i.e. it's a generator |
| 98 | + pass |
| 99 | + |
| 100 | + return start |
0 commit comments