Skip to content

Fix ROCm/HIP compilation and performance with CuPy - #195

Open
ekatralis wants to merge 5 commits into
xsuite:mainfrom
ekatralis:fix-rocm
Open

ekatralis wants to merge 5 commits into
xsuite:mainfrom
ekatralis:fix-rocm

Conversation

@ekatralis

@ekatralis ekatralis commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

Description

On AMD GPUs (CuPy with ROCm), tracking was ~2x slower than with the OpenCL context. This PR fixes that, makes HIPRTC compile cleanly, and adds an optional setting for faster kernels.

Changes

1. Tell HIPRTC the real block size (main fix)

HIPRTC assumes a kernel may run with up to 1024 threads per block. On CDNA GPUs that limits each thread to 128 registers, so large kernels spill heavily to scratch memory. AMD's OpenCL compiler assumes 256, which is why OpenCL was faster.

xobjects always launches kernels with default_block_size, on HIP we now pass --gpu-max-threads-per-block={default_block_size}.

2. Fixed-width integer typedefs

HIPRTC already provides int64_t etc., so redefining them caused compilation errors. The typedefs in cudaheader and atomicadd.h are now limited to NVRTC.

3. New opt-in setting rocm_fast_kernels / XSUITE_ROCM_FAST_KERNELS (default off)

When enabled on HIP, it adds -mllvm -amdgpu-early-inline-all=true -mllvm -amdgpu-function-calls=false. This inlines every device function, removing real calls. The trade-offs are much slower compilation and more memory, so the setting is off by default and prints a warning when enabled. It is ignored on CUDA, with a warning.

Results

MI100 (gfx908) using sample script with 1e6 particles x 1000 turns:

Tracking Uncached compile Compile memory
Before 2.79 s — —
This PR (default) 1.39 s 24 s ~160 MiB
XSUITE_ROCM_FAST_KERNELS=1 1.08 s 130 s ~2.0 GiB
OpenCL (reference) 1.50 s — —

Checklist

Mandatory:

  • I have added tests to cover my changes
  • All the tests are passing, including my new ones
  • I described my changes in this PR description

Optional:

  • The code I wrote follows good style practices (see PEP 8 and PEP 20).
  • I have updated the docs in relation to my changes, if applicable
  • I have tested also GPU contexts

@ekatralis

Copy link
Copy Markdown
Contributor Author

Completed the xtrack tests on an MI50 (gfx906) and an MI100 (gfx908) without any noteworthy backend failures using CuPy 14.1.1 (AMD's fork) on ROCm 10.

Apptainer definitions used:

In the tests the xobjects and xtrack versions were swapped out with the branches in this PR as well as the corresponding xtrack PR: xsuite/xtrack#954

@ekatralis

ekatralis commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor Author

The aggressive inlining (XSUITE_ROCM_FAST_KERNELS) seems to help more on gfx906. (Radeon VII/MI50/MI60). Using the same example script, tracking time is halved. On MI100, we only gain ~20%.

Output from MI100
Apptainer> python example.py opencl
OpenCL: available platforms (1):
  0 AMD Accelerated Parallel Processing (Advanced Micro Devices, Inc.)
    OpenCL 2.1 AMD-APP (3686.0)
    0.0 GPU: gfx908:sramecc+:xnack-
Using device: 0.0 


Using context: ContextPyopencl:0.0
Setup completed in: 0.5587574050296098 s
Tracking 1e+06 particles over 1000 turns...
Tracking completed in: 1.4951766920275986 s
Test passed
Apptainer> python example.py cupy

Using context: ContextCupy:0
Setup completed in: 0.834391177049838 s
Tracking 1e+06 particles over 1000 turns...
Tracking completed in: 1.3791608570609242 s
Test passed
Apptainer> export XSUITE_ROCM_FAST_KERNELS=1
Apptainer> python example.py cupy

Using context: ContextCupy:0
/xsuite-env/lib/python3.12/site-packages/xobjects/context_cupy.py:580: UserWarning: XSUITE_ROCM_FAST_KERNELS is enabled: all device functions are force-inlined. Kernels run faster, but compilation will be much slower (minutes instead of seconds for large lines) and use more memory. Compiled kernels are cached, so this cost is paid once per kernel.
  warnings.warn(
Setup completed in: 126.86519926297478 s
Tracking 1e+06 particles over 1000 turns...
Tracking completed in: 1.1234985140617937 s
Test passed
Output from MI50
Apptainer> python example.py opencl
OpenCL: available platforms (1):
  0 AMD Accelerated Parallel Processing (Advanced Micro Devices, Inc.)
    OpenCL 2.1 AMD-APP (3688.0)
    0.0 GPU: gfx906:sramecc+:xnack-
Using device: 0.0 


Using context: ContextPyopencl:0.0
Setup completed in: 0.5625809179618955 s
Tracking 1e+06 particles over 1000 turns...
Tracking completed in: 3.787257549003698 s
Test passed
Apptainer> python example.py cupy

Using context: ContextCupy:0
Setup completed in: 0.8176831129239872 s
Tracking 1e+06 particles over 1000 turns...
Tracking completed in: 3.440783308004029 s
Test passed
Apptainer> export XSUITE_ROCM_FAST_KERNELS=1
Apptainer> python example.py cupy

Using context: ContextCupy:0
/xsuite-env/lib/python3.12/site-packages/xobjects/context_cupy.py:580: UserWarning: XSUITE_ROCM_FAST_KERNELS is enabled: all device functions are force-inlined. Kernels run faster, but compilation will be much slower (minutes instead of seconds for large lines) and use more memory. Compiled kernels are cached, so this cost is paid once per kernel.
  warnings.warn(
Setup completed in: 0.8210070210043341 s
Tracking 1e+06 particles over 1000 turns...
Tracking completed in: 1.7990606259554625 s
Test passed
Output from Radeon VII
(/home/xsuiteuser/xsuite-env) xsuiteuser@69cf096bc699:~/xsuite-on-gsi$ python example.py opencl
OpenCL: available platforms (1):
  0 AMD Accelerated Parallel Processing (Advanced Micro Devices, Inc.)
    OpenCL 2.1 AMD-APP (3688.0)
    0.0 GPU: gfx906:sramecc+:xnack-
Using device: 0.0 


Using context: ContextPyopencl:0.0
Setup completed in: 0.5834868110250682 s
Tracking 1e+06 particles over 1000 turns...
Tracking completed in: 3.7786934818141162 s
Test passed
(/home/xsuiteuser/xsuite-env) xsuiteuser@69cf096bc699:~/xsuite-on-gsi$ python example.py cupy  

Using context: ContextCupy:0
Setup completed in: 0.7816302608698606 s
Tracking 1e+06 particles over 1000 turns...
Tracking completed in: 3.403318722033873 s
Test passed
(/home/xsuiteuser/xsuite-env) xsuiteuser@69cf096bc699:~/xsuite-on-gsi$ export XSUITE_ROCM_FAST_KERNELS=1
(/home/xsuiteuser/xsuite-env) xsuiteuser@69cf096bc699:~/xsuite-on-gsi$ python example.py cupy

Using context: ContextCupy:0
/home/xsuiteuser/xsuite-env/lib/python3.13/site-packages/xobjects/context_cupy.py:580: UserWarning: XSUITE_ROCM_FAST_KERNELS is enabled: all device functions are force-inlined. Kernels run faster, but compilation will be much slower (minutes instead of seconds for large lines) and use more memory. Compiled kernels are cached, so this cost is paid once per kernel.
  warnings.warn(
Setup completed in: 0.8034772120881826 s
Tracking 1e+06 particles over 1000 turns...
Tracking completed in: 1.888432462932542 s
Test passed

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.

1 participant