Skip to content

Replace numpy with cunumpy - #44

Draft
max-models wants to merge 34 commits into
devel-tinyfrom
add-xp-backend
Draft

Replace numpy with cunumpy#44
max-models wants to merge 34 commits into
devel-tinyfrom
add-xp-backend

Conversation

@max-models

Copy link
Copy Markdown
Member

No description provided.

@max-models max-models changed the title Add xp backend and enable Cupy arrays Add Cupy support Oct 22, 2025
@max-models

Copy link
Copy Markdown
Member Author

@copilot resolve the merge conflicts in this pull request

# Conflicts:
#	feectools/core/bsplines.py
#	feectools/ddm/blocking_data_exchanger.py
#	feectools/ddm/tests/test_cart_1d.py
#	feectools/ddm/tests/test_cart_2d.py
#	feectools/ddm/tests/test_cart_3d.py
#	feectools/feec/global_geometric_projectors.py
#	feectools/fem/tensor.py
#	feectools/linalg/basic.py

Co-authored-by: max-models <181350288+max-models@users.noreply.github.com>

Copilot AI commented May 5, 2026

Copy link
Copy Markdown

@copilot resolve the merge conflicts in this pull request

Resolved all merge conflicts in commit c322ee3. The 8 conflicting files were resolved by keeping the cunumpy/array_backend imports from our branch while adopting the feectools package paths from devel-tiny. Notable changes:

  • feectools/fem/tensor.py: adopted the improved MPI-based global grid reconstruction (uses gathered_pcoords from all ranks rather than recomputing on root) and adds proper fig/ax initialization
  • feectools/linalg/basic.py: removed duplicate toarray and fixed np.zerosxp.zeros in SumLinearOperator.toarray
  • feectools/feec/global_geometric_projectors.py: added BlockVector to imports (added in devel-tiny)

@max-models max-models changed the title Add Cupy support Replace numpy with cunumpy Jul 28, 2026
@max-models
max-models requested a review from Copilot July 28, 2026 14:37

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR introduces cunumpy as the primary array API (imported as xp) across the codebase, aiming to enable a NumPy/CuPy-style backend switch while keeping existing numerical and MPI/PETSc workflows working.

Changes:

  • Added cunumpy to project dependencies and replaced many numpy imports/usages with cunumpy (xp) throughout core, FEM, DDM, and linalg modules.
  • Updated several CPU/GPU boundary points (e.g., SciPy sparse/LAPACK interactions, shape/index casting for MPI datatypes) to be more backend-aware.
  • Updated a broad set of tests to use cunumpy to match the new public array API.

Reviewed changes

Copilot reviewed 54 out of 54 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
pyproject.toml Adds cunumpy dependency (NumPy remains listed).
feectools/utilities/utils.py Switches array ops from numpy to cunumpy (xp).
feectools/utilities/quadratures.py Switches quadrature allocations/math to xp.
feectools/linalg/utilities.py Replaces numpy with xp in PETSc/Psydac conversion helpers and small linalg utilities.
feectools/linalg/topetsc.py Replaces numpy with xp in PETSc export path and indexing helpers.
feectools/linalg/tests/test_stencil_vector.py Updates tests to use xp for array creation/comparison/random.
feectools/linalg/tests/test_stencil_vector_space.py Updates tests to use xp arrays.
feectools/linalg/tests/test_stencil_interface_matrix.py Updates tests to use xp arrays.
feectools/linalg/tests/test_solvers.py Updates tests to use xp random/norm/equality checks.
feectools/linalg/tests/test_matrix_free.py Updates tests to use xp random and norms.
feectools/linalg/tests/test_linalg.py Updates tests to use xp for linear algebra checks and random data.
feectools/linalg/tests/test_kron_stencil_matrix.py Updates tests to use xp for array ops and equality.
feectools/linalg/tests/test_fft.py Updates tests to use xp random/allclose/dtype checks.
feectools/linalg/stencil.py Switches internals to xp, adds backend-aware handling for some CPU/GPU interop points.
feectools/linalg/solvers.py Switches solver internals from numpy to xp (finfo/infty/allocations).
feectools/linalg/kron.py Switches Kronecker operators/solvers from numpy to xp and adds some int-casting for shapes/perms.
feectools/linalg/fft.py Switches FFT wrappers to use xp array types/allocations.
feectools/linalg/direct_solvers.py Adds backend-aware CPU/GPU transfers around SciPy/LAPACK-based solvers.
feectools/linalg/block.py Switches block vector/operator utilities to xp, with CPU-side object-array construction for blocks.
feectools/linalg/basic.py Switches core linear-operator utilities to xp (isscalar, diag/zeros, conjugation).
feectools/fem/vector.py Switches FEM vector-space checks to xp.
feectools/fem/tests/utilities.py Switches test utilities to xp for grids/random.
feectools/fem/tests/test_vector_spaces.py Switches vector-space tests to xp.linspace.
feectools/fem/tests/test_splines.py Switches spline tests to xp.linspace.
feectools/fem/tests/test_splines_par.py Switches parallel spline tests to xp.linspace.
feectools/fem/tests/test_spline_interpolation.py Switches interpolation tests to xp for random/linspace/array ops.
feectools/fem/tests/test_spline_histopolation.py Switches histopolation tests to xp for arrays/allclose.
feectools/fem/tests/analytical_profiles_1d.py Switches analytical profiles to xp trig/random.
feectools/fem/tensor.py Switches tensor FEM evaluation routines to xp array operations; adds a local NumPy import for MPI gather buffers.
feectools/fem/splines.py Switches spline space implementation to xp and adds backend-aware CPU conversions for solver matrices.
feectools/fem/projectors.py Switches projector construction to xp for abs/diag/eye.
feectools/fem/partitioning.py Switches partitioning helpers to xp.
feectools/fem/grid.py Switches grid utilities to xp.
feectools/feec/hodge.py Removes unused NumPy import.
feectools/feec/global_geometric_projectors.py Switches to xp and adds backend-aware handling for matrix construction and indexing.
feectools/feec/derivatives.py Switches derivative operator implementations to xp.
feectools/ddm/tests/test_multicart_2d.py Switches DDM tests to xp arrays.
feectools/ddm/tests/test_cart_3d.py Switches DDM tests to xp arrays.
feectools/ddm/tests/test_cart_2d.py Switches DDM tests to xp arrays.
feectools/ddm/tests/test_cart_1d.py Switches DDM tests to xp arrays.
feectools/ddm/petsc.py Switches PETSc index helper to xp (prod/ravel_multi_index).
feectools/ddm/partition.py Switches partitioning logic to xp.
feectools/ddm/nonblocking_data_exchanger.py Switches to xp and adjusts MPI datatype creation argument casting.
feectools/ddm/cart.py Switches cart decomposition internals to xp and adds int-casting in some MPI-facing areas.
feectools/ddm/blocking_data_exchanger.py Switches to xp and adjusts MPI datatype creation argument casting.
feectools/core/tests/test_bsplines.py Switches bspline tests to xp.
feectools/core/tests/test_bsplines_kernel.py Switches kernel tests to xp.
feectools/core/bsplines.py Switches bspline public API to xp and adds backend-aware handling in a few utility functions.
feectools/core/bsplines_kernels.py Minor kernel adjustments (still NumPy-based) for shape handling and index casting.
feectools/api/fem_sum_form.py Switches sum-form assembly code to xp.
feectools/api/fem_common.py Switches common FEM helpers (e.g., diag len) to xp.
feectools/api/fem_bilinear_form.py Switches assembly setup logic (pads/args typing) to xp.
Comments suppressed due to low confidence (2)

feectools/ddm/nonblocking_data_exchanger.py:146

  • Translate_ranks is being called with an xp.array. If the cunumpy backend is CuPy, this creates a device array, which mpi4py cannot use for rank translation. Pass a plain Python list (or a NumPy array) of ints instead.
            if self._axis is not None:
                rank_dest = gcomm.group.Translate_ranks(xp.array([rank_dest]), comm.group)[0]

feectools/ddm/blocking_data_exchanger.py:160

  • Translate_ranks is being called with an xp.array. If the cunumpy backend is CuPy, this creates a device array, which mpi4py cannot use for rank translation. Pass a plain Python list (or a NumPy array) of ints instead.
            if self._axis is not None:
                rank_dest = gcomm.group.Translate_ranks(xp.array([rank_dest]), comm.group)[0]


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread pyproject.toml
Comment thread feectools/ddm/nonblocking_data_exchanger.py
Comment thread feectools/ddm/blocking_data_exchanger.py
Comment thread feectools/fem/splines.py
Comment thread feectools/api/fem_sum_form.py
@max-models
max-models marked this pull request as draft July 30, 2026 06:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants