MFEM v4.8.0
Finite element discretization library
Loading...
Searching...
No Matches
batched.hpp
Go to the documentation of this file.
1// Copyright (c) 2010-2025, Lawrence Livermore National Security, LLC. Produced
2// at the Lawrence Livermore National Laboratory. All Rights reserved. See files
3// LICENSE and NOTICE for details. LLNL-CODE-806117.
4//
5// This file is part of the MFEM library. For more information and source code
6// availability visit https://mfem.org.
7//
8// MFEM is free software; you can redistribute it and/or modify it under the
9// terms of the BSD-3 license. We welcome feedback and contributions, see file
10// CONTRIBUTING.md for details.
11
12#ifndef MFEM_BATCHED_LINALG
13#define MFEM_BATCHED_LINALG
14
16#include "../densemat.hpp"
17#include <array>
18#include <memory>
19
20namespace mfem
21{
22
23/// @brief Class for performing batched linear algebra operations, potentially
24/// using accelerated algorithms (GPU BLAS or MAGMA). Accessed using static
25/// member functions.
26///
27/// The static member functions will delegate to the active backend (which can
28/// be set using SetActiveBackend(), see BatchedLinAlg::Backend for all
29/// available backends and the order in which they will be chosen initially).
30/// Operations can be performed directly with a specific backend using Get().
32{
33public:
34 /// @brief Available backends for implementations of batched algorithms.
35 ///
36 /// The initially active backend will be the first available backend in this
37 /// order: MAGMA, GPU_BLAS, NATIVE.
39 {
40 /// @brief The standard MFEM backend, implemented using mfem::forall
41 /// kernels. Not as performant as the other kernels.
43 /// @brief Either cuBLAS or hipBLAS, depending on whether MFEM is using
44 /// CUDA or HIP. Not available otherwise.
46 /// MAGMA backend, only available if MFEM is compiled with MAGMA support.
48 /// Counter for the number of backends.
50 };
51
52 /// Operation type (transposed or not transposed)
53 enum Op
54 {
55 N, ///< Not transposed.
56 T ///< Transposed.
57 };
58
59private:
60 /// All available backends. Unavailable backends will be nullptr.
61 std::array<std::unique_ptr<class BatchedLinAlgBase>,
62 Backend::NUM_BACKENDS> backends;
63 Backend active_backend;
64 /// Default constructor. Private.
66 /// Return the singleton instance.
67 static BatchedLinAlg &Instance();
68public:
69 /// @brief Computes $y = \alpha A^{op} x + \beta y$.
70 ///
71 /// $A^{op}$ is either $A$ or $A^T$ depending on the value of @a op.
72 /// $A$ is a block diagonal matrix, represented by the DenseTensor @a A with
73 /// shape (m, n, n_mat). $x$ has shape (tr?m:n, k, n_mat), and $y$ has shape
74 /// (tr?n:m, k, n_mat), where 'tr' is true in the transposed case.
75 static void AddMult(const DenseTensor &A, const Vector &x, Vector &y,
76 real_t alpha = 1.0, real_t beta = 1.0,
77 Op op = Op::N);
78 /// Computes $y = A x$ (e.g. by calling @ref AddMult "AddMult(A,x,y,1,0,Op::N)").
79 static void Mult(const DenseTensor &A, const Vector &x, Vector &y);
80 /// Computes $y = A^T x$ (e.g. by calling @ref AddMult "AddMult(A,x,y,1,0,Op::T)").
81 static void MultTranspose(const DenseTensor &A, const Vector &x, Vector &y);
82 /// @brief Replaces the block diagonal matrix $A$ with its inverse $A^{-1}$.
83 ///
84 /// $A$ is represented by the DenseTensor @a A with shape (m, m, n_mat).
85 static void Invert(DenseTensor &A);
86 /// @brief Replaces the block diagonal matrix $A$ with its LU factors. The
87 /// pivots are stored in @a P.
88 ///
89 /// $A$ is represented by the DenseTensor @a A with shape (n, n, n_mat). On
90 /// output, $P$ has shape (n, n_mat).
91 static void LUFactor(DenseTensor &A, Array<int> &P);
92 /// @brief Replaces $x$ with $A^{-1} x$, given the LU factors @a A and pivots
93 /// @a P of the block-diagonal matrix $A$.
94 ///
95 /// The LU factors and pivots of $A$ should be obtained by first calling
96 /// LUFactor(). $A$ has shape (n, n, n_mat) and $x$ has shape (n, n_rhs,
97 /// n_mat).
98 ///
99 /// @warning LUSolve() and LUFactor() should be called using the same backend
100 /// because of potential incompatibilities (e.g. 0-based or 1-based
101 /// indexing).
102 static void LUSolve(const DenseTensor &A, const Array<int> &P, Vector &x);
103 /// @brief Returns true if the requested backend is available.
104 ///
105 /// The available backends depend on which third-party libraries MFEM is
106 /// compiled with, and whether the CUDA/HIP device is enabled.
107 static bool IsAvailable(Backend backend);
108 /// Set the default backend for batched linear algebra operations.
109 static void SetActiveBackend(Backend backend);
110 /// Get the default backend for batched linear algebra operations.
111 static Backend GetActiveBackend();
112 /// @brief Get the BatchedLinAlgBase object associated with a specific
113 /// backend.
114 ///
115 /// This allows the user to perform specific operations with a backend
116 /// different from the active backend.
117 static const BatchedLinAlgBase &Get(Backend backend);
118};
119
120/// Abstract base clase for batched linear algebra operations.
122{
123public:
125 /// See BatchedLinAlg::AddMult.
126 virtual void AddMult(const DenseTensor &A, const Vector &x, Vector &y,
127 real_t alpha = 1.0, real_t beta = 1.0,
128 Op op = Op::N) const = 0;
129 /// See BatchedLinAlg::Mult.
130 virtual void Mult(const DenseTensor &A, const Vector &x, Vector &y) const;
131 /// See BatchedLinAlg::MultTranspose.
132 virtual void MultTranspose(const DenseTensor &A, const Vector &x,
133 Vector &y) const;
134 /// See BatchedLinAlg::Invert.
135 virtual void Invert(DenseTensor &A) const = 0;
136 /// See BatchedLinAlg::LUFactor.
137 virtual void LUFactor(DenseTensor &A, Array<int> &P) const = 0;
138 /// See BatchedLinAlg::LUSolve.
139 virtual void LUSolve(const DenseTensor &LU, const Array<int> &P,
140 Vector &x) const = 0;
141 /// Virtual destructor.
142 virtual ~BatchedLinAlgBase() { }
143};
144
145} // namespace mfem
146
147#endif
Abstract base clase for batched linear algebra operations.
Definition batched.hpp:122
virtual void Mult(const DenseTensor &A, const Vector &x, Vector &y) const
See BatchedLinAlg::Mult.
Definition batched.cpp:110
virtual ~BatchedLinAlgBase()
Virtual destructor.
Definition batched.hpp:142
virtual void Invert(DenseTensor &A) const =0
See BatchedLinAlg::Invert.
virtual void LUFactor(DenseTensor &A, Array< int > &P) const =0
See BatchedLinAlg::LUFactor.
virtual void MultTranspose(const DenseTensor &A, const Vector &x, Vector &y) const
See BatchedLinAlg::MultTranspose.
Definition batched.cpp:116
virtual void LUSolve(const DenseTensor &LU, const Array< int > &P, Vector &x) const =0
See BatchedLinAlg::LUSolve.
virtual void AddMult(const DenseTensor &A, const Vector &x, Vector &y, real_t alpha=1.0, real_t beta=1.0, Op op=Op::N) const =0
See BatchedLinAlg::AddMult.
Class for performing batched linear algebra operations, potentially using accelerated algorithms (GPU...
Definition batched.hpp:32
static void Mult(const DenseTensor &A, const Vector &x, Vector &y)
Computes (e.g. by calling AddMult(A,x,y,1,0,Op::N)).
Definition batched.cpp:60
Backend
Available backends for implementations of batched algorithms.
Definition batched.hpp:39
@ GPU_BLAS
Either cuBLAS or hipBLAS, depending on whether MFEM is using CUDA or HIP. Not available otherwise.
Definition batched.hpp:45
@ MAGMA
MAGMA backend, only available if MFEM is compiled with MAGMA support.
Definition batched.hpp:47
@ NUM_BACKENDS
Counter for the number of backends.
Definition batched.hpp:49
@ NATIVE
The standard MFEM backend, implemented using mfem::forall kernels. Not as performant as the other ker...
Definition batched.hpp:42
static const BatchedLinAlgBase & Get(Backend backend)
Get the BatchedLinAlgBase object associated with a specific backend.
Definition batched.cpp:103
static void MultTranspose(const DenseTensor &A, const Vector &x, Vector &y)
Computes (e.g. by calling AddMult(A,x,y,1,0,Op::T)).
Definition batched.cpp:65
static Backend GetActiveBackend()
Get the default backend for batched linear algebra operations.
Definition batched.cpp:98
static bool IsAvailable(Backend backend)
Returns true if the requested backend is available.
Definition batched.cpp:87
static void LUFactor(DenseTensor &A, Array< int > &P)
Replaces the block diagonal matrix with its LU factors. The pivots are stored in P.
Definition batched.cpp:76
static void AddMult(const DenseTensor &A, const Vector &x, Vector &y, real_t alpha=1.0, real_t beta=1.0, Op op=Op::N)
Computes .
Definition batched.cpp:54
static void SetActiveBackend(Backend backend)
Set the default backend for batched linear algebra operations.
Definition batched.cpp:92
Op
Operation type (transposed or not transposed)
Definition batched.hpp:54
@ T
Transposed.
Definition batched.hpp:56
@ N
Not transposed.
Definition batched.hpp:55
static void LUSolve(const DenseTensor &A, const Array< int > &P, Vector &x)
Replaces with , given the LU factors A and pivots P of the block-diagonal matrix .
Definition batched.cpp:81
static void Invert(DenseTensor &A)
Replaces the block diagonal matrix with its inverse .
Definition batched.cpp:71
Rank 3 tensor (array of matrices)
Vector data type.
Definition vector.hpp:82
Vector beta
const real_t alpha
Definition ex15.cpp:369
float real_t
Definition config.hpp:43
MFEM backends.
Definition device.hpp:28