llg3d.solvers.opencl

LLG3D solver using OpenCL.

Functions

get_context_and_device([device_selection])

Get the OpenCL context and device.

get_precision(device, precision)

Get the numpy float type based on the precision.

Classes

OpenCLSolver([element, N, dt, Jx, Jy, Jz, ...])

OpenCL-based LLG3D solver.

Program(solver)

Class to manage the OpenCL kernels for the LLG3D simulation.

TimedKernel(kernel, name, solver)

Wrapper for OpenCL kernels with automatic timing.

class TimedKernel(kernel, name, solver)[source]

Bases: object

Wrapper for OpenCL kernels with automatic timing.

Parameters:
  • kernel (cl.Kernel) – The underlying OpenCL kernel

  • name (str) – Name of the kernel for logging

  • solver (OpenCLSolver) – Reference to the solver for profiling storage

get_context_and_device(device_selection='auto')[source]

Get the OpenCL context and device.

Parameters:

device_selection (str) –

  • "auto": Let OpenCL choose automatically

  • "cpu": Select CPU device

  • "gpu": Select first available GPU

  • "gpu:N": Select specific GPU by index (e.g., "gpu:0", "gpu:1")

Returns:

  • The OpenCL context

  • The OpenCL device

Raises:
  • RuntimeError – If no suitable device is found

  • ValueError – If the device selection string is invalid

Return type:

tuple[Context, Device]

get_precision(device, precision)[source]

Get the numpy float type based on the precision.

Parameters:
  • device (Device) – OpenCL device

  • precision (str) – Precision of the simulation (single or double)

Returns:

The numpy float type (float32 or float64)

Raises:

RuntimeError – If double precision is asked while the device does not support it

Return type:

dtype

class Program(solver)[source]

Bases: object

Class to manage the OpenCL kernels for the LLG3D simulation.

Parameters:

solver (OpenCLSolver)

solver

The OpenCLSolver instance

cl_program: Program

The OpenCL program

_get_built_program()[source]

Return the OpenCL program built from the source code.

Returns:

The OpenCL program object

Return type:

Program

get_kernel(kernel_name, arg_types=[None])[source]

Returns the specified kernel by name, wrapped with timing.

Parameters:
  • kernel_name (str) – Name of the kernel to retrieve

  • arg_types (list) – List of argument types for the kernel

Returns:

The TimedKernel wrapper around the OpenCL kernel

Return type:

TimedKernel

class OpenCLSolver(element='Cobalt', N=5000, dt=1e-14, Jx=300, Jy=21, Jz=21, dx=1e-09, T=0.0, H_ext=0.0, init_type='0', result_file='run.npz', start_averaging=4000, n_mean=1, n_profile=0, solver='numpy', precision='double', blocking=False, seed=12345, device='auto', profiling=False, verbosity='INFO', np=1)[source]

Bases: BaseSolver

OpenCL-based LLG3D solver.

Parameters:
  • element (Literal['Cobalt', 'Iron', 'Nickel'])

  • N (int)

  • dt (float)

  • Jx (int)

  • Jy (int)

  • Jz (int)

  • dx (float)

  • T (float)

  • H_ext (float)

  • init_type (Literal['0', 'dw'])

  • result_file (str)

  • start_averaging (int)

  • n_mean (int)

  • n_profile (int)

  • solver (Literal['opencl', 'mpi', 'numpy', 'jax'])

  • precision (Literal['single', 'double'])

  • blocking (bool)

  • seed (int)

  • device (Literal['cpu', 'gpu', 'auto'])

  • profiling (bool)

  • verbosity (Literal['DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL'])

  • np (int)

solver_type: ClassVar[str] = 'opencl'

Solver type name

_init_rng()[source]

Initialize a random number generator for temperature fluctuations.

Returns:

An OpenCL random number generator

Return type:

Any

_compute_R_random(d_R_random)[source]

Compute random number array for thermal fluctuations.

Parameters:

d_R_random (Array) – Device array to fill with random numbers

_xyz_average(m, immediate=True)[source]

Compute the space average of m_n using a two-level GPU reduction.

Parameters:
  • m (Any) – Current magnetization device array

  • immediate (bool) – If True (default), read result immediately and return it. If False, accumulate on GPU without synchronization.

Returns:

The average value if immediate=True, None otherwise.

Return type:

float | None

_record_xyz_average(m_n, t, n)[source]

Override to defer reading results until simulation end.

Parameters:
  • m_n (Any)

  • t (float)

  • n (int)

Return type:

None

_check_errors(iteration)[source]

Check for non-finite values detected by the kernel.

Parameters:

iteration (int) – Current iteration number (for error message)

Raises:

RuntimeError – If non-finite values are detected

Return type:

None

_update_x_profiles(m_n, t)[source]

Update the x profiles of m_n at time t.

Reads the device buffer back to host and computes the y-z averaged profiles for each magnetization component, then stores them in self.x_profiles.

Parameters:
  • m_n (Any)

  • t (float)

_simulate()[source]

Simulates the system over N iterations.

Returns:

The time taken for the simulation

Return type:

float

_add_profiling_event(name, event)[source]

Add a profiling event to the list.

Parameters:
  • name (str)

  • event (Event)

Return type:

None

_process_profiling_events()[source]

Process pending profiling events and update stats.

Fill the profiling_stats dict with cumulative time and call counts for each kernel and function.

Return type:

None

_process_deferred_xyz_averages()[source]

Read back and process deferred xyz_average results.

  • Fill the xyz_average entry of the records dict with (time, value) pairs

  • Add a m1_mean entry to the observables dict.

Return type:

None