MFEM v4.7.0
Finite element discretization library
Loading...
Searching...
No Matches
tassign.hpp
Go to the documentation of this file.
1// Copyright (c) 2010-2024, 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_TEMPLATE_ASSIGN
13#define MFEM_TEMPLATE_ASSIGN
14
15#include "../config/tconfig.hpp"
16#include "backends.hpp"
17
18namespace mfem
19{
20
21// Assignment operations
22
24{
25 enum Type
26 {
27 Set, // a = b
28 Add, // a += b
29 Mult, // a *= b
30 Div, // a /= b
31 rDiv // a = b/a
32 };
33};
34
35namespace internal
36{
37
38template <AssignOp::Type Op>
39struct AssignOp_Impl;
40
41template <>
42struct AssignOp_Impl<AssignOp::Set>
43{
44 template <typename lvalue_t, typename rvalue_t>
45 static inline lvalue_t &Assign(lvalue_t &a, const rvalue_t &b)
46 {
47 return (a = b);
48 }
49
50 template <typename lvalue_t, typename rvalue_t>
51 MFEM_HOST_DEVICE
52 static inline lvalue_t &AssignHD(lvalue_t &a, const rvalue_t &b)
53 {
54 return (a = b);
55 }
56};
57
58template <>
59struct AssignOp_Impl<AssignOp::Add>
60{
61 template <typename lvalue_t, typename rvalue_t>
62 static inline lvalue_t &Assign(lvalue_t &a, const rvalue_t &b)
63 {
64 MFEM_FLOPS_ADD(1);
65 return (a += b);
66 }
67
68 template <typename lvalue_t, typename rvalue_t>
69 MFEM_HOST_DEVICE
70 static inline lvalue_t &AssignHD(lvalue_t &a, const rvalue_t &b)
71 {
72 MFEM_FLOPS_ADD(1);
73 return (a += b);
74 }
75};
76
77template <>
78struct AssignOp_Impl<AssignOp::Mult>
79{
80 template <typename lvalue_t, typename rvalue_t>
81 static inline lvalue_t &Assign(lvalue_t &a, const rvalue_t &b)
82 {
83 MFEM_FLOPS_ADD(1);
84 return (a *= b);
85 }
86
87 template <typename lvalue_t, typename rvalue_t>
88 MFEM_HOST_DEVICE
89 static inline lvalue_t &AssignHD(lvalue_t &a, const rvalue_t &b)
90 {
91 MFEM_FLOPS_ADD(1);
92 return (a *= b);
93 }
94};
95
96template <>
97struct AssignOp_Impl<AssignOp::Div>
98{
99 template <typename lvalue_t, typename rvalue_t>
100 static inline lvalue_t &Assign(lvalue_t &a, const rvalue_t &b)
101 {
102 MFEM_FLOPS_ADD(1);
103 return (a /= b);
104 }
105
106 template <typename lvalue_t, typename rvalue_t>
107 MFEM_HOST_DEVICE
108 static inline lvalue_t &AssignHD(lvalue_t &a, const rvalue_t &b)
109 {
110 MFEM_FLOPS_ADD(1);
111 return (a /= b);
112 }
113};
114
115template <>
116struct AssignOp_Impl<AssignOp::rDiv>
117{
118 template <typename lvalue_t, typename rvalue_t>
119 static inline lvalue_t &Assign(lvalue_t &a, const rvalue_t &b)
120 {
121 MFEM_FLOPS_ADD(1);
122 return (a = b/a);
123 }
124
125 template <typename lvalue_t, typename rvalue_t>
126 MFEM_HOST_DEVICE
127 static inline lvalue_t &AssignHD(lvalue_t &a, const rvalue_t &b)
128 {
129 MFEM_FLOPS_ADD(1);
130 return (a = b/a);
131 }
132};
133
134} // namespace mfem::internal
135
136template <AssignOp::Type Op, typename lvalue_t, typename rvalue_t>
137inline lvalue_t &Assign(lvalue_t &a, const rvalue_t &b)
138{
139 return internal::AssignOp_Impl<Op>::Assign(a, b);
140}
141
142template <AssignOp::Type Op, typename lvalue_t, typename rvalue_t>
143MFEM_HOST_DEVICE
144inline lvalue_t &AssignHD(lvalue_t &a, const rvalue_t &b)
145{
146 return internal::AssignOp_Impl<Op>::AssignHD(a, b);
147}
148
149} // namespace mfem
150
151#endif // MFEM_TEMPLATE_ASSIGN
real_t b
Definition lissajous.cpp:42
real_t a
Definition lissajous.cpp:41
lvalue_t & Assign(lvalue_t &a, const rvalue_t &b)
Definition tassign.hpp:137
void Mult(const Table &A, const Table &B, Table &C)
C = A * B (as boolean matrices)
Definition table.cpp:476
void Add(const DenseMatrix &A, const DenseMatrix &B, real_t alpha, DenseMatrix &C)
C = A + alpha*B.
MFEM_HOST_DEVICE lvalue_t & AssignHD(lvalue_t &a, const rvalue_t &b)
Definition tassign.hpp:144