|
| 1 | +"""Test the MPIFredholm1 class |
| 2 | +Designed to run with n GPUs (with 1 MPI process per GPU) |
| 3 | +$ mpiexec -n 3 pytest test_fredholm_nccl.py --with-mpi |
| 4 | +
|
| 5 | +This file employs the same test sets as test_fredholm under NCCL environment |
| 6 | +""" |
| 7 | +import numpy as np |
| 8 | +import cupy as cp |
| 9 | +from numpy.testing import assert_allclose |
| 10 | +from mpi4py import MPI |
| 11 | +import pytest |
| 12 | + |
| 13 | +import pylops |
| 14 | +import pylops_mpi |
| 15 | + |
| 16 | +from pylops_mpi import DistributedArray |
| 17 | +from pylops_mpi.DistributedArray import local_split, Partition |
| 18 | +from pylops_mpi.signalprocessing import MPIFredholm1 |
| 19 | +from pylops_mpi.utils.dottest import dottest |
| 20 | +from pylops_mpi.utils._nccl import initialize_nccl_comm |
| 21 | + |
| 22 | +np.random.seed(42) |
| 23 | +rank = MPI.COMM_WORLD.Get_rank() |
| 24 | +size = MPI.COMM_WORLD.Get_size() |
| 25 | + |
| 26 | +nccl_comm = initialize_nccl_comm() |
| 27 | + |
| 28 | +par1 = { |
| 29 | + "nsl": 12, |
| 30 | + "ny": 6, |
| 31 | + "nx": 4, |
| 32 | + "nz": 5, |
| 33 | + "usematmul": False, |
| 34 | + "saveGt": True, |
| 35 | + "imag": 0, |
| 36 | + "dtype": "float32", |
| 37 | +} # real, saved Gt |
| 38 | +par2 = { |
| 39 | + "nsl": 12, |
| 40 | + "ny": 6, |
| 41 | + "nx": 4, |
| 42 | + "nz": 5, |
| 43 | + "usematmul": True, |
| 44 | + "saveGt": False, |
| 45 | + "imag": 0, |
| 46 | + "dtype": "float32", |
| 47 | +} # real, unsaved Gt |
| 48 | +par3 = { |
| 49 | + "nsl": 12, |
| 50 | + "ny": 6, |
| 51 | + "nx": 4, |
| 52 | + "nz": 5, |
| 53 | + "usematmul": False, |
| 54 | + "saveGt": True, |
| 55 | + "imag": 1j, |
| 56 | + "dtype": "complex64", |
| 57 | +} # complex, saved Gt |
| 58 | +par4 = { |
| 59 | + "nsl": 12, |
| 60 | + "ny": 6, |
| 61 | + "nx": 4, |
| 62 | + "nz": 5, |
| 63 | + "saveGt": False, |
| 64 | + "usematmul": False, |
| 65 | + "imag": 1j, |
| 66 | + "dtype": "complex64", |
| 67 | +} # complex, unsaved Gt |
| 68 | +par5 = { |
| 69 | + "nsl": 12, |
| 70 | + "ny": 6, |
| 71 | + "nx": 4, |
| 72 | + "nz": 1, |
| 73 | + "usematmul": True, |
| 74 | + "saveGt": True, |
| 75 | + "imag": 0, |
| 76 | + "dtype": "float32", |
| 77 | +} # real, saved Gt, nz=1 |
| 78 | +par6 = { |
| 79 | + "nsl": 12, |
| 80 | + "ny": 6, |
| 81 | + "nx": 4, |
| 82 | + "nz": 1, |
| 83 | + "usematmul": True, |
| 84 | + "saveGt": False, |
| 85 | + "imag": 0, |
| 86 | + "dtype": "float32", |
| 87 | +} # real, unsaved Gt, nz=1 |
| 88 | + |
| 89 | + |
| 90 | +"""Seems to stop next tests from running |
| 91 | +@pytest.mark.mpi(min_size=2) |
| 92 | +@pytest.mark.parametrize("par", [(par1)]) |
| 93 | +def test_Gsize1(par): |
| 94 | + #Check error is raised if G has size 1 in any of the ranks |
| 95 | + with pytest.raises(NotImplementedError): |
| 96 | + _ = MPIFredholm1( |
| 97 | + np.ones((1, par["nx"], par["ny"])), |
| 98 | + nz=par["nz"], |
| 99 | + saveGt=par["saveGt"], |
| 100 | + usematmul=par["usematmul"], |
| 101 | + dtype=par["dtype"], |
| 102 | + ) |
| 103 | +""" |
| 104 | + |
| 105 | + |
| 106 | +@pytest.mark.mpi(min_size=2) |
| 107 | +@pytest.mark.parametrize("par", [(par1), (par2), (par3), (par4), (par5), (par6)]) |
| 108 | +def test_Fredholm1_nccl(par): |
| 109 | + """Fredholm1 operator""" |
| 110 | + np.random.seed(42) |
| 111 | + |
| 112 | + _F = cp.arange(par["nsl"] * par["nx"] * par["ny"]).reshape( |
| 113 | + par["nsl"], par["nx"], par["ny"] |
| 114 | + ).astype(par["dtype"]) |
| 115 | + F = _F - par["imag"] * _F |
| 116 | + |
| 117 | + # split across ranks |
| 118 | + nsl_rank = local_split((par["nsl"], ), MPI.COMM_WORLD, Partition.SCATTER, 0) |
| 119 | + nsl_ranks = np.concatenate(MPI.COMM_WORLD.allgather(nsl_rank)) |
| 120 | + islin_rank = np.insert(np.cumsum(nsl_ranks)[:-1] , 0, 0)[rank] |
| 121 | + islend_rank = np.cumsum(nsl_ranks)[rank] |
| 122 | + Frank = F[islin_rank:islend_rank] |
| 123 | + |
| 124 | + Fop_MPI = MPIFredholm1( |
| 125 | + Frank, |
| 126 | + nz=par["nz"], |
| 127 | + saveGt=par["saveGt"], |
| 128 | + usematmul=par["usematmul"], |
| 129 | + dtype=par["dtype"], |
| 130 | + ) |
| 131 | + |
| 132 | + x = DistributedArray(global_shape=par["nsl"] * par["ny"] * par["nz"], |
| 133 | + base_comm_nccl=nccl_comm, |
| 134 | + partition=pylops_mpi.Partition.BROADCAST, |
| 135 | + dtype=par["dtype"], |
| 136 | + engine="cupy") |
| 137 | + x[:] = 1. + par["imag"] * 1. |
| 138 | + x_global = x.asarray() |
| 139 | + # Forward |
| 140 | + y_dist = Fop_MPI @ x |
| 141 | + y = y_dist.asarray() |
| 142 | + # Adjoint |
| 143 | + y_adj_dist = Fop_MPI.H @ y_dist |
| 144 | + y_adj = y_adj_dist.asarray() |
| 145 | + # Dot test |
| 146 | + dottest(Fop_MPI, x, y_dist, par["nsl"] * par["nx"] * par["nz"], par["nsl"] * par["ny"] * par["nz"]) |
| 147 | + |
| 148 | + if rank == 0: |
| 149 | + Fop = pylops.signalprocessing.Fredholm1( |
| 150 | + F.get(), |
| 151 | + nz=par["nz"], |
| 152 | + saveGt=par["saveGt"], |
| 153 | + usematmul=par["usematmul"], |
| 154 | + dtype=par["dtype"], |
| 155 | + ) |
| 156 | + |
| 157 | + assert Fop_MPI.shape == Fop.shape |
| 158 | + y_np = Fop @ x_global.get() |
| 159 | + y_adj_np = Fop.H @ y_np |
| 160 | + assert_allclose(y.get(), y_np, rtol=1e-14) |
| 161 | + assert_allclose(y_adj.get(), y_adj_np, rtol=1e-14) |
0 commit comments