MFEM v4.8.0
Finite element discretization library
Loading...
Searching...
No Matches
batched.cpp
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#include "batched.hpp"
13#include "native.hpp"
14#include "gpu_blas.hpp"
15#include "magma.hpp"
16
17namespace mfem
18{
19
20BatchedLinAlg::BatchedLinAlg()
21{
22 backends[NATIVE].reset(new NativeBatchedLinAlg);
23
25 {
26#ifdef MFEM_USE_CUDA_OR_HIP
27 backends[GPU_BLAS].reset(new GPUBlasBatchedLinAlg);
28#endif
29
30#ifdef MFEM_USE_MAGMA
31 backends[MAGMA].reset(new MagmaBatchedLinAlg);
32#endif
33
34#if defined(MFEM_USE_MAGMA)
35 active_backend = MAGMA;
36#elif defined(MFEM_USE_CUDA_OR_HIP)
37 active_backend = GPU_BLAS;
38#else
39 active_backend = NATIVE;
40#endif
41 }
42 else
43 {
44 active_backend = NATIVE;
45 }
46}
47
48BatchedLinAlg &BatchedLinAlg::Instance()
49{
50 static BatchedLinAlg instance;
51 return instance;
52}
53
54void BatchedLinAlg::AddMult(const DenseTensor &A, const Vector &x, Vector &y,
56{
57 Get(Instance().active_backend).AddMult(A, x, y, alpha, beta, op);
58}
59
60void BatchedLinAlg::Mult(const DenseTensor &A, const Vector &x, Vector &y)
61{
62 Get(Instance().active_backend).Mult(A, x, y);
63}
64
66 Vector &y)
67{
68 Get(Instance().active_backend).MultTranspose(A, x, y);
69}
70
72{
73 Get(Instance().active_backend).Invert(A);
74}
75
77{
78 Get(Instance().active_backend).LUFactor(A, P);
79}
80
82 Vector &x)
83{
84 Get(Instance().active_backend).LUSolve(A, P, x);
85}
86
88{
89 return Instance().backends[backend] != nullptr;
90}
91
93{
94 MFEM_VERIFY(IsAvailable(backend), "Requested backend not supported.");
95 Instance().active_backend = backend;
96}
97
99{
100 return Instance().active_backend;
101}
102
104{
105 auto &backend_ptr = Instance().backends[backend];
106 MFEM_VERIFY(backend_ptr, "Requested backend not supported.")
107 return *backend_ptr;
108}
109
111 Vector &y) const
112{
113 AddMult(A, x, y, 1.0, 0.0);
114}
115
117 Vector &y) const
118{
119 AddMult(A, x, y, 1.0, 0.0, Op::T);
120}
121
122}
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 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.
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
@ 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
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)
static bool Allows(unsigned long b_mask)
Return true if any of the backends in the backend mask, b_mask, are allowed.
Definition device.hpp:259
Vector data type.
Definition vector.hpp:82
Vector beta
const real_t alpha
Definition ex15.cpp:369
float real_t
Definition config.hpp:43
@ HIP_MASK
Biwise-OR of all HIP backends.
Definition device.hpp:91
@ CUDA_MASK
Biwise-OR of all CUDA backends.
Definition device.hpp:89