MFEM v4.10.0
Finite element discretization library
Loading...
Searching...
No Matches
batched.cpp
Go to the documentation of this file.
1// Copyright (c) 2010-2026, 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"
17
18namespace mfem
19{
20
21BatchedLinAlg::BatchedLinAlg()
22{
23 backends[NATIVE].reset(new NativeBatchedLinAlg);
24
26 {
27#ifdef MFEM_USE_CUDA_OR_HIP
28 backends[GPU_BLAS].reset(new GPUBlasBatchedLinAlg);
29#endif
30
31#ifdef MFEM_USE_MAGMA
32 backends[MAGMA].reset(new MagmaBatchedLinAlg);
33#endif
34
35#if defined(MFEM_USE_MAGMA)
36 active_backend = MAGMA;
37#elif defined(MFEM_USE_CUDA_OR_HIP)
38 active_backend = GPU_BLAS;
39#else
40 active_backend = NATIVE;
41#endif
42 }
43 else
44 {
45 active_backend = NATIVE;
46 }
47}
48
49BatchedLinAlg &BatchedLinAlg::Instance()
50{
51 static BatchedLinAlg instance;
52 return instance;
53}
54
55void BatchedLinAlg::AddMult(const DenseTensor &A, const Vector &x, Vector &y,
56 real_t alpha, real_t beta, Op op)
57{
58 Get(Instance().active_backend).AddMult(A, x, y, alpha, beta, op);
59}
60
61void BatchedLinAlg::Mult(const DenseTensor &A, const Vector &x, Vector &y)
62{
63 Get(Instance().active_backend).Mult(A, x, y);
64}
65
67 Vector &y)
68{
69 Get(Instance().active_backend).MultTranspose(A, x, y);
70}
71
73{
74 Get(Instance().active_backend).Invert(A);
75}
76
78{
79 Get(Instance().active_backend).LUFactor(A, P);
80}
81
83 Vector &x)
84{
85 Get(Instance().active_backend).LUSolve(A, P, x);
86}
87
89{
90 return Instance().backends[backend] != nullptr;
91}
92
94{
95 MFEM_VERIFY(IsAvailable(backend), "Requested backend not supported.");
96 Instance().active_backend = backend;
97}
98
100{
101 return Instance().active_backend;
102}
103
105{
106 auto &backend_ptr = Instance().backends[backend];
107 MFEM_VERIFY(backend_ptr, "Requested backend not supported.")
108 return *backend_ptr;
109}
110
112 Vector &y) const
113{
114 AddMult(A, x, y, 1.0, 0.0);
115}
116
118 Vector &y) const
119{
120 AddMult(A, x, y, 1.0, 0.0, Op::T);
121}
122
123void VerifyBatchedLUInfo(const Array<int> &info_array, const char *message)
124{
125 static Array<int> workspace;
126 int status = 0;
127 const int *d_info = info_array.Read();
129 info_array.Size(), status,
130 [=] MFEM_HOST_DEVICE (int i, int &r) { r |= d_info[i]; },
131 BOrReducer<int> {}, true, workspace);
132 MFEM_VERIFY(status == 0, message);
133}
134
135}
int Size() const
Return the logical size of the array.
Definition array.hpp:192
const T * Read(bool on_dev=true) const
Shortcut for mfem::Read(a.GetMemory(), a.Size(), on_dev).
Definition array.hpp:410
Abstract base clase for batched linear algebra operations.
Definition batched.hpp:121
virtual void Mult(const DenseTensor &A, const Vector &x, Vector &y) const
See BatchedLinAlg::Mult.
Definition batched.cpp:111
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:117
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:61
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:104
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:66
static Backend GetActiveBackend()
Get the default backend for batched linear algebra operations.
Definition batched.cpp:99
static bool IsAvailable(Backend backend)
Returns true if the requested backend is available.
Definition batched.cpp:88
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:77
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:55
static void SetActiveBackend(Backend backend)
Set the default backend for batched linear algebra operations.
Definition batched.cpp:93
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:82
static void Invert(DenseTensor &A)
Replaces the block diagonal matrix with its inverse .
Definition batched.cpp:72
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:271
Vector data type.
Definition vector.hpp:82
const real_t alpha
Definition ex15.cpp:369
void reduce(int N, T &res, B &&body, const R &reducer, bool use_dev, Array< T > &workspace)
Performs a 1D reduction on the range [0,N). res initial value and where the result will be written....
Definition reducers.hpp:532
float real_t
Definition config.hpp:46
void VerifyBatchedLUInfo(const Array< int > &info_array, const char *message)
Check that all batched LU info values are zero.
Definition batched.cpp:123
@ HIP_MASK
Biwise-OR of all HIP backends.
Definition device.hpp:98
@ CUDA_MASK
Biwise-OR of all CUDA backends.
Definition device.hpp:96