Skip to content

SortingBoxes cannot be created on the CuPy backend #726

Description

@max-models

SortingBoxes cannot be created on the CuPy backend, so any species with SortingParameters(boxes_per_dim=...) fails during setup on the GPU.

Found in: #719 (end-to-end GPU test for LinearVlasovAmpereOneSpecies). The CuPy run was probed on cunumpy's fake CuPy with CUDA launches emulated on the CPU. With sorting boxes enabled it stops in SortingBoxes, so the test in #719 runs without sorting boxes.

What fails (src/struphy/pic/sorting.py, SortingBoxes._set_boxes, devel 25688c5):

  • sorting.py:228-230: n_cols = round(n_mkr * (1 + 1 / xp.sqrt(n_mkr) + self._box_bufsize)). On CuPy, xp.sqrt of a Python int returns a 0-d device array, and Python's round() does not accept it.
  • sorting.py:239: initialize_neighbours(self._neighbours, ...). This is the pyccel kernel from pic/sorting_kernels.py:68, called with the device array self._neighbours created by xp.zeros a few lines above.

SortingBoxes is created in Particles at src/struphy/pic/base.py:2597, so the failure happens at setup, before any box sorting in the time loop.

Suggested fix:

  • Compute n_cols with math.sqrt (it is host-side bookkeeping, not device data).
  • Build _neighbours on the host with the pyccel initialize_neighbours and copy it to the device once (setup data). Alternatively, add a CUDA version in its own folder, following CUDA_STRATEGY.md.
  • Add a fake-CuPy test that creates Particles with boxes_per_dim on CuPy.

This only fixes setup. Sorting the markers into boxes on the device (Particles.put_particles_in_boxes, assign_box_to_each_particle, sort_boxed_particles) is still part of #694, and the SPH models need it.

Related: #694, #719. Part of #650 (see CUDA_STRATEGY.md).

🤖 Generated with Claude Code

Activity

  1. added
    bugSomething isn't working
    GPURunning Struphy on GPUs
    CUDACUDA kernels and the CUDA porting (see #650)
    on Oct 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    CUDACUDA kernels and the CUDA porting (see #650)GPURunning Struphy on GPUsbugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions