|
| 1 | +"""Test the stacking classes |
| 2 | + Designed to run with n GPUs (with 1 MPI process per GPU) |
| 3 | + $ mpiexec -n 10 pytest test_stack_nccl.py --with-mpi |
| 4 | +
|
| 5 | +This file employs the same test sets as test_stack 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 | +from pylops_mpi.utils.dottest import dottest |
| 16 | +from pylops_mpi.utils._nccl import initialize_nccl_comm |
| 17 | + |
| 18 | +nccl_comm = initialize_nccl_comm() |
| 19 | + |
| 20 | +# imag part is left to future complex-number support |
| 21 | +par1 = {'ny': 101, 'nx': 101, 'imag': 0, 'dtype': np.float64} |
| 22 | +par2 = {'ny': 301, 'nx': 101, 'imag': 0, 'dtype': np.float64} |
| 23 | + |
| 24 | + |
| 25 | +@pytest.mark.mpi(min_size=2) |
| 26 | +@pytest.mark.parametrize("par", [(par1), (par2)]) |
| 27 | +def test_vstack_nccl(par): |
| 28 | + """Test the MPIVStack operator with NCCL""" |
| 29 | + size = MPI.COMM_WORLD.Get_size() |
| 30 | + rank = MPI.COMM_WORLD.Get_rank() |
| 31 | + A_gpu = cp.ones(shape=(par['ny'], par['nx'])) + par['imag'] * cp.ones(shape=(par['ny'], par['nx'])) |
| 32 | + Op = pylops.MatrixMult(A=((rank + 1) * A_gpu).astype(par['dtype'])) |
| 33 | + VStack_MPI = pylops_mpi.MPIVStack(ops=[Op, ], base_comm_nccl=nccl_comm) |
| 34 | + |
| 35 | + # Broadcasted DistributedArray(global_shape == local_shape) |
| 36 | + x = pylops_mpi.DistributedArray(global_shape=par['nx'], |
| 37 | + base_comm_nccl=nccl_comm, |
| 38 | + partition=pylops_mpi.Partition.BROADCAST, |
| 39 | + dtype=par['dtype'], |
| 40 | + engine="cupy") |
| 41 | + x[:] = cp.ones(shape=par['nx'], dtype=par['dtype']) |
| 42 | + x_global = x.asarray() |
| 43 | + |
| 44 | + # Scattered DistributedArray |
| 45 | + y = pylops_mpi.DistributedArray(global_shape=size * par['ny'], |
| 46 | + base_comm_nccl=nccl_comm, |
| 47 | + partition=pylops_mpi.Partition.SCATTER, |
| 48 | + dtype=par['dtype'], |
| 49 | + engine="cupy") |
| 50 | + y[:] = cp.ones(shape=par['ny'], dtype=par['dtype']) |
| 51 | + y_global = y.asarray() |
| 52 | + |
| 53 | + # Forward |
| 54 | + x_mat = VStack_MPI @ x |
| 55 | + # Adjoint |
| 56 | + y_rmat = VStack_MPI.H @ y |
| 57 | + assert isinstance(x_mat, pylops_mpi.DistributedArray) |
| 58 | + assert isinstance(y_rmat, pylops_mpi.DistributedArray) |
| 59 | + # Dot test |
| 60 | + dottest(VStack_MPI, x, y, size * par['ny'], par['nx']) |
| 61 | + |
| 62 | + x_mat_mpi = x_mat.asarray() |
| 63 | + y_rmat_mpi = y_rmat.asarray() |
| 64 | + |
| 65 | + if rank == 0: |
| 66 | + A = A_gpu.get() |
| 67 | + ops = [pylops.MatrixMult(A=((i + 1) * A).astype(par['dtype'])) for i in range(size)] |
| 68 | + VStack = pylops.VStack(ops=ops) |
| 69 | + x_mat_np = VStack @ x_global.get() |
| 70 | + y_rmat_np = VStack.H @ y_global.get() |
| 71 | + assert_allclose(x_mat_mpi.get(), x_mat_np, rtol=1e-14) |
| 72 | + assert_allclose(y_rmat_mpi.get(), y_rmat_np, rtol=1e-14) |
| 73 | + |
| 74 | + |
| 75 | +@pytest.mark.mpi(min_size=2) |
| 76 | +@pytest.mark.parametrize("par", [(par1), (par2)]) |
| 77 | +def test_stacked_vstack_nccl(par): |
| 78 | + """Test the MPIStackedVStack operator with NCCL""" |
| 79 | + size = MPI.COMM_WORLD.Get_size() |
| 80 | + rank = MPI.COMM_WORLD.Get_rank() |
| 81 | + A_gpu = cp.ones(shape=(par['ny'], par['nx'])) + par['imag'] * cp.ones(shape=(par['ny'], par['nx'])) |
| 82 | + Op = pylops.MatrixMult(A=((rank + 1) * A_gpu).astype(par['dtype'])) |
| 83 | + VStack_MPI = pylops_mpi.MPIVStack(ops=[Op, ], base_comm_nccl=nccl_comm) |
| 84 | + StackedVStack_MPI = pylops_mpi.MPIStackedVStack([VStack_MPI, VStack_MPI]) |
| 85 | + |
| 86 | + # Broadcasted DistributedArray(global_shape == local_shape) |
| 87 | + x = pylops_mpi.DistributedArray(global_shape=par['nx'], |
| 88 | + base_comm_nccl=nccl_comm, |
| 89 | + partition=pylops_mpi.Partition.BROADCAST, |
| 90 | + dtype=par['dtype'], |
| 91 | + engine="cupy") |
| 92 | + x[:] = cp.ones(shape=par['nx'], dtype=par['dtype']) |
| 93 | + x_global = x.asarray() |
| 94 | + |
| 95 | + # Stacked DistributedArray |
| 96 | + dist1 = pylops_mpi.DistributedArray(global_shape=size * par['ny'], base_comm_nccl=nccl_comm, dtype=par['dtype'], engine="cupy") |
| 97 | + dist1[:] = cp.ones(dist1.local_shape, dtype=par['dtype']) |
| 98 | + dist2 = pylops_mpi.DistributedArray(global_shape=size * par['ny'], base_comm_nccl=nccl_comm, dtype=par['dtype'], engine="cupy") |
| 99 | + dist2[:] = cp.ones(dist1.local_shape, dtype=par['dtype']) |
| 100 | + y = pylops_mpi.StackedDistributedArray(distarrays=[dist1, dist2]) |
| 101 | + y_global = y.asarray() |
| 102 | + |
| 103 | + x_mat = StackedVStack_MPI @ x |
| 104 | + y_rmat = StackedVStack_MPI.H @ y |
| 105 | + assert isinstance(x_mat, pylops_mpi.StackedDistributedArray) |
| 106 | + assert isinstance(y_rmat, pylops_mpi.DistributedArray) |
| 107 | + |
| 108 | + x_mat_mpi = x_mat.asarray() |
| 109 | + y_rmat_mpi = y_rmat.asarray() |
| 110 | + |
| 111 | + if rank == 0: |
| 112 | + A = A_gpu.get() |
| 113 | + ops = [pylops.MatrixMult(A=((i + 1) * A).astype(par['dtype'])) for i in range(size)] |
| 114 | + VStack = pylops.VStack(ops=ops) |
| 115 | + VStack_final = pylops.VStack(ops=[VStack, VStack]) |
| 116 | + x_mat_np = VStack_final @ x_global.get() |
| 117 | + y_rmat_np = VStack_final.H @ y_global.get() |
| 118 | + assert_allclose(x_mat_mpi.get(), x_mat_np, rtol=1e-14) |
| 119 | + assert_allclose(y_rmat_mpi.get(), y_rmat_np, rtol=1e-14) |
| 120 | + |
| 121 | + |
| 122 | +# TODO: Test of HStack |
0 commit comments