xsuite / xobjects

Library to create and manipulate serialized objects in CPU and GPU memory and to compile code on these platforms.
https://xsuite.readthedocs.io
Apache License 2.0
4 stars 15 forks source link

INVALID_BUFFER_SIZE error in `ContextPyopencl` #19

Closed fsoubelet closed 1 year ago

fsoubelet commented 3 years ago

Hi, I am posting this here as to my understanding it is an issue coming from xobjects. Feel free to move the issue to the appropriate repository should I be mistaken.

I am encountering a failure with the ContextPyopencl when increasing the number of tracked turns above a certain threshold. Here is a script that reproduces the issue for me based on the documentation's FODO example:

from typing import Union

import numpy as np

import xline as xl
import xobjects as xo
import xtrack as xt

SEQUENCE = xl.Line(
    elements=[
        xl.Drift(length=2.0),
        xl.Multipole(knl=[0, 1.0], ksl=[0, 0]),
        xl.Drift(length=1.0),
        xl.Multipole(knl=[0, -1.0], ksl=[0, 0]),
    ],
    element_names=["drift_0", "quad_0", "drift_1", "quad_1"],
)

N_PART = 200
STARTING_COORDINATES = {
    "p0c": 6500e9,
    "x": np.random.uniform(-1e-3, 1e-3, N_PART),
    "px": np.random.uniform(-1e-5, 1e-5, N_PART),
    "y": np.random.uniform(-2e-3, 2e-3, N_PART),
    "py": np.random.uniform(-3e-5, 3e-5, N_PART),
    "zeta": np.random.uniform(-1e-2, 1e-2, N_PART),
    "delta": np.random.uniform(-1e-4, 1e-4, N_PART),
}

def track_on_context(
    _context: Union[xo.ContextCpu, xo.ContextCupy, xo.ContextPyopencl],
    starting_coordinates: dict,
    n_turns: int = 10_000,
) -> np.ndarray:
    """
    Convenience function to track as the documentation's simple example on a given context, returning the
    resulting horizontal coordinates.

    Args:
        _context (Union[xo.ContextCpu, xo.ContextCupy, xo.ContextPyopencl]): `xobjects` context to build for.
        starting_coordinates (dict): initial coordinates of particles to track.
        n_turns (int): number of turns to track for, defaults to 10000.

    Returns:
        Resulting X coordinates from tracking, as a `numpy.ndarray`.
    """
    # Transfer lattice on context and compile tracking code
    tracker = xt.Tracker(_context=_context, sequence=SEQUENCE)

    # Build particle object on context
    particles = xt.Particles(_context=_context, **starting_coordinates)

    # Track and return turn-by-turn
    tracker.track(particles, num_turns=n_turns, turn_by_turn_monitor=True)
    return tracker.record_last_track.x

if __name__ == "__main__":
    # macOS machine, using the OpenGL context as Metal is not supported
    context_gpu = xo.ContextPyopencl()
    _ = track_on_context(context_gpu, STARTING_COORDINATES)  # this is fine
    _ = track_on_context(context_gpu, STARTING_COORDINATES, 100_000)  # this is fine
    _ = track_on_context(context_gpu, STARTING_COORDINATES, 300_000)  # this is not

I am on a macbook pro machine (Intel CPU + Radeon Pro Vega 20 GPU). The first two trackings run fine but the third one raises at the tracker.track(...) step, the error message giving

pyopencl._cl.LogicError: create_buffer failed: INVALID_BUFFER_SIZE
giadarol commented 3 years ago

Hi @fsoubelet , sorry I had not seen this! You are trying to allocate memory to save turn-by-turn data for all particles. Probably you run out of memory. Does it work if you set turn_by_turn_monitor=False? In that case you have only the coordinates at the last turn (directly in particles)