|
| 1 | +from itertools import chain, product |
| 2 | + |
| 3 | +import numpy as np |
1 | 4 | import pytest
|
2 | 5 |
|
3 |
| -from pyoperators import MPI |
| 6 | +from pyoperators import MPI, MPIDistributionIdentityOperator |
| 7 | +from pyoperators.utils.testing import assert_same |
4 | 8 | from pysimulators import Acquisition, Instrument, PackedTable, Sampling, Scene
|
| 9 | +from pysimulators.operators import ProjectionOperator |
| 10 | +from pysimulators.sparse import FSRMatrix |
| 11 | + |
| 12 | +pytestmark = pytest.mark.mpi |
| 13 | + |
| 14 | +RANK = MPI.COMM_WORLD.rank |
| 15 | +SIZE = MPI.COMM_WORLD.size |
| 16 | +NPROCS_INSTRUMENT = sorted( |
| 17 | + {int(n) for n in [1, SIZE / 3, SIZE / 2, SIZE] if int(n) == n} |
| 18 | +) |
| 19 | +NSCENE = 10 |
| 20 | +NSAMPLING_GLOBAL = 100 |
| 21 | +NDETECTOR_GLOBAL = 16 |
| 22 | +SCENE = Scene(NSCENE) |
| 23 | +SAMPLING = Sampling(NSAMPLING_GLOBAL, period=1.0) |
| 24 | +INSTRUMENT = Instrument('', PackedTable(NDETECTOR_GLOBAL)) |
| 25 | + |
| 26 | + |
| 27 | +class MyAcquisition(Acquisition): |
| 28 | + def get_projection_operator(self): |
| 29 | + dtype = [('index', int), ('value', float)] |
| 30 | + data = np.recarray((len(self.instrument), len(self.sampling), 1), dtype=dtype) |
| 31 | + for ilocal, iglobal in enumerate(self.instrument.detector.index): |
| 32 | + data[ilocal].value = iglobal |
| 33 | + data[ilocal, :, 0].index = [ |
| 34 | + (iglobal + int(t)) % NSCENE for t in self.sampling.time |
| 35 | + ] |
| 36 | + |
| 37 | + matrix = FSRMatrix( |
| 38 | + (len(self.instrument) * len(self.sampling), NSCENE), |
| 39 | + data=data.reshape((-1, 1)), |
| 40 | + ) |
| 41 | + |
| 42 | + return ProjectionOperator( |
| 43 | + matrix, dtype=float, shapeout=(len(self.instrument), len(self.sampling)) |
| 44 | + ) |
| 45 | + |
| 46 | + |
| 47 | +def get_acquisition(comm, nprocs_instrument): |
| 48 | + return MyAcquisition( |
| 49 | + INSTRUMENT, SAMPLING, SCENE, comm=comm, nprocs_instrument=nprocs_instrument |
| 50 | + ) |
| 51 | + |
| 52 | + |
| 53 | +@pytest.mark.parametrize('nprocs_instrument', NPROCS_INSTRUMENT) |
| 54 | +def test_communicators(nprocs_instrument): |
| 55 | + sky = SCENE.ones() |
| 56 | + nprocs_sampling = SIZE // nprocs_instrument |
| 57 | + serial_acq = get_acquisition(MPI.COMM_SELF, 1) |
| 58 | + assert serial_acq.comm.size == 1 |
| 59 | + assert serial_acq.instrument.comm.size == 1 |
| 60 | + assert serial_acq.sampling.comm.size == 1 |
| 61 | + assert len(serial_acq.instrument) == NDETECTOR_GLOBAL |
| 62 | + assert len(serial_acq.sampling) == NSAMPLING_GLOBAL |
| 63 | + |
| 64 | + parallel_acq = get_acquisition(MPI.COMM_WORLD, nprocs_instrument) |
| 65 | + assert parallel_acq.comm.size == SIZE |
| 66 | + assert parallel_acq.instrument.comm.size == nprocs_instrument |
| 67 | + assert parallel_acq.sampling.comm.size == nprocs_sampling |
| 68 | + assert ( |
| 69 | + parallel_acq.instrument.comm.allreduce(len(parallel_acq.instrument)) |
| 70 | + == NDETECTOR_GLOBAL |
| 71 | + ) |
| 72 | + assert ( |
| 73 | + parallel_acq.sampling.comm.allreduce(len(parallel_acq.sampling)) |
| 74 | + == NSAMPLING_GLOBAL |
| 75 | + ) |
5 | 76 |
|
6 |
| -rank = MPI.COMM_WORLD.rank |
7 |
| -size = MPI.COMM_WORLD.size |
8 |
| - |
9 |
| - |
10 |
| -def test(): |
11 |
| - scene = Scene(1024) |
12 |
| - instrument = Instrument('instrument', PackedTable((32, 32))) |
13 |
| - sampling = Sampling(1000) |
14 |
| - acq = Acquisition(instrument, sampling, scene, nprocs_sampling=max(size // 2, 1)) |
15 |
| - print( |
16 |
| - acq.comm.rank, |
17 |
| - acq.instrument.detector.comm.rank, |
18 |
| - '/', |
19 |
| - acq.instrument.detector.comm.size, |
20 |
| - acq.sampling.comm.rank, |
21 |
| - '/', |
22 |
| - acq.sampling.comm.size, |
| 77 | + serial_H = serial_acq.get_projection_operator() |
| 78 | + ref_tod = serial_H(sky) |
| 79 | + |
| 80 | + parallel_H = ( |
| 81 | + parallel_acq.get_projection_operator() |
| 82 | + * MPIDistributionIdentityOperator(parallel_acq.comm) |
| 83 | + ) |
| 84 | + local_tod = parallel_H(sky) |
| 85 | + actual_tod = np.vstack( |
| 86 | + parallel_acq.instrument.comm.allgather( |
| 87 | + np.hstack(parallel_acq.sampling.comm.allgather(local_tod)) |
| 88 | + ) |
23 | 89 | )
|
24 |
| - pytest.xfail('the test is not finished.') |
| 90 | + assert_same(actual_tod, ref_tod, atol=20) |
| 91 | + |
| 92 | + ref_backproj = serial_H.T(ref_tod) |
| 93 | + actual_backproj = parallel_H.T(local_tod) |
| 94 | + assert_same(actual_backproj, ref_backproj, atol=20) |
| 95 | + |
| 96 | + |
| 97 | +@pytest.mark.parametrize('nprocs_instrument', NPROCS_INSTRUMENT) |
| 98 | +@pytest.mark.parametrize( |
| 99 | + 'selection', |
| 100 | + [ |
| 101 | + Ellipsis, |
| 102 | + slice(None), |
| 103 | + ] |
| 104 | + + list(chain(*(product([slice(None), Ellipsis], repeat=n) for n in [1, 2, 3]))), |
| 105 | +) |
| 106 | +def test_communicators_getitem_all(nprocs_instrument, selection): |
| 107 | + acq = get_acquisition(MPI.COMM_WORLD, nprocs_instrument) |
| 108 | + assert acq.instrument.comm.size == nprocs_instrument |
| 109 | + assert acq.sampling.comm.size == MPI.COMM_WORLD.size / nprocs_instrument |
| 110 | + assert acq.comm.size == MPI.COMM_WORLD.size |
| 111 | + restricted_acq = acq[selection] |
| 112 | + assert restricted_acq.instrument.comm.size == nprocs_instrument |
| 113 | + assert restricted_acq.sampling.comm.size == MPI.COMM_WORLD.size / nprocs_instrument |
| 114 | + assert restricted_acq.comm.size == MPI.COMM_WORLD.size |
| 115 | + |
| 116 | + |
| 117 | +@pytest.mark.parametrize('nprocs_instrument', NPROCS_INSTRUMENT) |
| 118 | +@pytest.mark.parametrize('selection', [0, slice(None, 1), np.array]) |
| 119 | +def test_communicators_getitem_instrument(nprocs_instrument, selection): |
| 120 | + acq = get_acquisition(MPI.COMM_WORLD, nprocs_instrument) |
| 121 | + if selection is np.array: |
| 122 | + selection = np.zeros(len(acq.instrument), bool) |
| 123 | + selection[0] = True |
| 124 | + restricted_acq = acq[selection] |
| 125 | + assert restricted_acq.instrument.comm.size == 1 |
| 126 | + assert restricted_acq.sampling.comm.size == acq.sampling.comm.size |
| 127 | + assert restricted_acq.comm.size == acq.sampling.comm.size |
| 128 | + |
| 129 | + |
| 130 | +SELECTION_GETITEM_SAMPLING = np.zeros(NSAMPLING_GLOBAL, bool) |
| 131 | +SELECTION_GETITEM_SAMPLING[0] = True |
| 132 | + |
| 133 | + |
| 134 | +@pytest.mark.parametrize('nprocs_instrument', NPROCS_INSTRUMENT) |
| 135 | +@pytest.mark.parametrize('selection', [0, slice(None, 1), np.array]) |
| 136 | +def test_communicators_getitem_sampling(nprocs_instrument, selection): |
| 137 | + acq = get_acquisition(MPI.COMM_WORLD, nprocs_instrument) |
| 138 | + if selection is np.array: |
| 139 | + selection = np.zeros(len(acq.sampling), bool) |
| 140 | + selection[0] = True |
| 141 | + restricted_acq = acq[:, selection] |
| 142 | + assert restricted_acq.instrument.comm.size == acq.instrument.comm.size |
| 143 | + assert restricted_acq.sampling.comm.size == 1 |
| 144 | + assert restricted_acq.comm.size == acq.instrument.comm.size |
0 commit comments