Skip to content
Edit this page View source of this page

Numerics

meanflux.numerics holds the discrete operators and linear solvers that the equilibrium solvers share. Every routine accepts leading batch dimensions, runs on any PyTorch device, and never synchronises the device with the host.

Tridiagonal systems

After k levels of parallel cyclic reduction a system of 16 unknowns couples rows i and i ± 2k only, and the Thomas algorithm sweeps the 2k resulting systems in parallel. The chart counts the work of one solve of 192 systems on 401 points.

tridiagonal

Batched tridiagonal linear solves by hybrid parallel cyclic reduction.

Every implicit time step of the finite-difference schemes in MeanFlux reduces to one tridiagonal system per scenario and per node. This module solves all of them at once. Row \(i\) of a system of size \(n\) reads

\[ a_i \, x_{i-1} + b_i \, x_i + c_i \, x_{i+1} = d_i , \qquad i = 0, \dots, n - 1 , \]

where \(a\) is the sub-diagonal, \(b\) the diagonal, \(c\) the super-diagonal and \(d\) the right-hand side. The four arrays have the same length \(n\), so that a stencil assembled per grid point can be passed as it is. The entries \(a_0\) and \(c_{n-1}\) multiply unknowns outside the system and are ignored.

The solver combines two classical algorithms. A level \(\ell\) of parallel cyclic reduction (Hockney and Jesshope, 1981) eliminates the couplings of every row to its neighbours at distance \(2^\ell\), at once for all rows. After \(k\) levels each system has split into \(2^k\) independent interleaved systems of about \(n / 2^k\) rows, which the Thomas algorithm then solves in \(n / 2^k\) sequential steps, each vectorised over all of them. Full reduction (\(k = \lceil \log_2 n \rceil\)) needs no sequential step but does \(O(n \log n)\) work, while the Thomas algorithm alone (\(k = 0\)) does \(O(n)\) work in \(n\) steps. The hybrid stops reducing once there are enough independent systems to occupy the device (Zhang, Cohen and Owens, 2010). On a GPU the target is 1024 systems per streaming multiprocessor, and on a CPU 256 per thread. Both values were measured on the MeanFlux benchmark workload. The number of levels depends only on shapes and on the device, so the solve never synchronises the device with the host. Results for different batch sizes can differ in the last bits, because the number of levels differs.

Every row is first divided by its diagonal entry. Both phases then work on three arrays instead of four, in place on buffers of their own, and the inputs are never modified. A reduction level takes eight tensor operations and a row of the Thomas phase four, whatever the size of the batch. The working memory is one buffer of \(3 (n + 2^k)\) numbers per system without reduction, and two such buffers with it.

Neither phase pivots. Both are stable when the matrix is diagonally dominant by rows (Higham, 2002, Section 9.5), and every reduction level preserves that property (Heller, 1976). The monotone schemes in MeanFlux produce such matrices.

Note

On a CPU under Linux, the default thresholds of the glibc allocator return these buffers to the operating system after every solve, and the next solve takes page faults for them again. For 192 systems on 401 points this is about 1000 page faults and a third of the time of a solve (2.8 ms against 2.0 ms, 4 threads, float64). Setting MALLOC_TRIM_THRESHOLD_=1073741824, MALLOC_MMAP_THRESHOLD_=33554432 and MALLOC_TOP_PAD_=268435456 in the environment keeps the buffers. A GPU is not affected, because PyTorch caches its device memory.

References

D. Heller (1976). Some aspects of the cyclic reduction algorithm for block tridiagonal linear systems. SIAM Journal on Numerical Analysis, 13(4), 484-496.

N. J. Higham (2002). Accuracy and Stability of Numerical Algorithms, 2nd edition. SIAM.

R. W. Hockney and C. R. Jesshope (1981). Parallel Computers: Architecture, Programming and Algorithms. Adam Hilger.

Y. Zhang, J. Cohen and J. D. Owens (2010). Fast tridiagonal solvers on the GPU. Proceedings of the 15th ACM SIGPLAN Symposium on Principles and Practice of Parallel Programming, 127-136.

solve_tridiagonal(lower, diagonal, upper, rhs)

Solve a batch of tridiagonal linear systems.

The four inputs broadcast against each other over their leading dimensions, so one matrix can be shared by many right-hand sides and the other way round. The solve is differentiable with respect to all four inputs, in reverse and in forward mode. Its backward pass costs one more tridiagonal solve, with the transposed matrix, and does not store intermediate results. A forward-mode tangent (torch.func.jvp) costs one more solve with the same matrix. Under torch.func.vmap the mapped dimension becomes one more batch dimension. The solve never synchronises the device with the host and never modifies its inputs. Half-precision inputs are solved in float32 and the solution is rounded back, see meanflux.core.working_dtype.

Parameters:

Name Type Description Default
lower Tensor

Sub-diagonal \(a\), shape (*batch, n). Entry lower[..., i] multiplies x[..., i - 1], and lower[..., 0] is ignored.

required
diagonal Tensor

Diagonal \(b\), shape (*batch, n).

required
upper Tensor

Super-diagonal \(c\), shape (*batch, n). Entry upper[..., i] multiplies x[..., i + 1], and upper[..., -1] is ignored.

required
rhs Tensor

Right-hand side \(d\), shape (*batch, n).

required

Returns:

Type Description
Tensor

The solution \(x\), with the broadcast batch shape of the inputs and

Tensor

their device and dtype.

Raises:

Type Description
TypeError

If the inputs are not real floating-point tensors of one dtype.

ValueError

If the inputs are on different devices, have no dimension, have different last dimensions, or have batch shapes that do not broadcast.

Example

A batch of 1000 systems on 401 grid points that share one matrix.

import torch from meanflux.numerics import solve_tridiagonal off = -torch.ones(401, dtype=torch.float64) rhs = torch.rand(1000, 401, dtype=torch.float64) x = solve_tridiagonal(off, 4.0 - 2.0 * off, off, rhs) x.shape torch.Size([1000, 401])