MFEM  v4.5.1
Finite element discretization library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Pages
operator.cpp
Go to the documentation of this file.
1 // Copyright (c) 2010-2022, 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 "operator.hpp"
13 
14 #include "../../../config/config.hpp"
15 #include "../../../linalg/vector.hpp"
16 #include "../../fespace.hpp"
17 #include "util.hpp"
18 #include "ceed.hpp"
19 
20 namespace mfem
21 {
22 
23 namespace ceed
24 {
25 
26 #ifdef MFEM_USE_CEED
27 Operator::Operator(CeedOperator op)
28 {
29  oper = op;
30  CeedSize in_len, out_len;
31  int ierr = CeedOperatorGetActiveVectorLengths(oper, &in_len, &out_len);
32  PCeedChk(ierr);
33  height = out_len;
34  width = in_len;
35  MFEM_VERIFY(height == out_len, "height overflow");
36  MFEM_VERIFY(width == in_len, "width overflow");
37  CeedVectorCreate(internal::ceed, height, &v);
38  CeedVectorCreate(internal::ceed, width, &u);
39 }
40 #endif
41 
42 void Operator::Mult(const mfem::Vector &x, mfem::Vector &y) const
43 {
44 #ifdef MFEM_USE_CEED
45  const CeedScalar *x_ptr;
46  CeedScalar *y_ptr;
47  CeedMemType mem;
48  CeedGetPreferredMemType(mfem::internal::ceed, &mem);
49  if ( Device::Allows(Backend::DEVICE_MASK) && mem==CEED_MEM_DEVICE )
50  {
51  x_ptr = x.Read();
52  y_ptr = y.Write();
53  }
54  else
55  {
56  x_ptr = x.HostRead();
57  y_ptr = y.HostWrite();
58  mem = CEED_MEM_HOST;
59  }
60  CeedVectorSetArray(u, mem, CEED_USE_POINTER, const_cast<CeedScalar*>(x_ptr));
61  CeedVectorSetArray(v, mem, CEED_USE_POINTER, y_ptr);
62 
63  CeedOperatorApply(oper, u, v, CEED_REQUEST_IMMEDIATE);
64 
65  CeedVectorTakeArray(u, mem, const_cast<CeedScalar**>(&x_ptr));
66  CeedVectorTakeArray(v, mem, &y_ptr);
67 #else
68  MFEM_ABORT("MFEM must be built with MFEM_USE_CEED=YES to use libCEED.");
69 #endif
70 }
71 
73 {
74 #ifdef MFEM_USE_CEED
75  const CeedScalar *x_ptr;
76  CeedScalar *y_ptr;
77  CeedMemType mem;
78  CeedGetPreferredMemType(mfem::internal::ceed, &mem);
79  if ( Device::Allows(Backend::DEVICE_MASK) && mem==CEED_MEM_DEVICE )
80  {
81  x_ptr = x.Read();
82  y_ptr = y.ReadWrite();
83  }
84  else
85  {
86  x_ptr = x.HostRead();
87  y_ptr = y.HostReadWrite();
88  mem = CEED_MEM_HOST;
89  }
90  CeedVectorSetArray(u, mem, CEED_USE_POINTER, const_cast<CeedScalar*>(x_ptr));
91  CeedVectorSetArray(v, mem, CEED_USE_POINTER, y_ptr);
92 
93  CeedOperatorApplyAdd(oper, u, v, CEED_REQUEST_IMMEDIATE);
94 
95  CeedVectorTakeArray(u, mem, const_cast<CeedScalar**>(&x_ptr));
96  CeedVectorTakeArray(v, mem, &y_ptr);
97 #else
98  MFEM_ABORT("MFEM must be built with MFEM_USE_CEED=YES to use libCEED.");
99 #endif
100 }
101 
103 {
104 #ifdef MFEM_USE_CEED
105  CeedScalar *d_ptr;
106  CeedMemType mem;
107  CeedGetPreferredMemType(mfem::internal::ceed, &mem);
108  if ( Device::Allows(Backend::DEVICE_MASK) && mem==CEED_MEM_DEVICE )
109  {
110  d_ptr = diag.ReadWrite();
111  }
112  else
113  {
114  d_ptr = diag.HostReadWrite();
115  mem = CEED_MEM_HOST;
116  }
117  CeedVectorSetArray(v, mem, CEED_USE_POINTER, d_ptr);
118 
119  CeedOperatorLinearAssembleAddDiagonal(oper, v, CEED_REQUEST_IMMEDIATE);
120 
121  CeedVectorTakeArray(v, mem, &d_ptr);
122 #else
123  MFEM_ABORT("MFEM must be built with MFEM_USE_CEED=YES to use libCEED.");
124 #endif
125 }
126 
127 } // namespace ceed
128 
129 } // namespace mfem
virtual double * HostWrite()
Shortcut for mfem::Write(vec.GetMemory(), vec.Size(), false).
Definition: vector.hpp:461
CeedOperator oper
Definition: operator.hpp:29
virtual double * Write(bool on_dev=true)
Shortcut for mfem::Write(vec.GetMemory(), vec.Size(), on_dev).
Definition: vector.hpp:457
void AddMult(const mfem::Vector &x, mfem::Vector &y) const
Definition: operator.cpp:72
virtual const double * HostRead() const
Shortcut for mfem::Read(vec.GetMemory(), vec.Size(), false).
Definition: vector.hpp:453
void GetDiagonal(mfem::Vector &diag) const
Definition: operator.cpp:102
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:258
int height
Dimension of the output / number of rows in the matrix.
Definition: operator.hpp:27
virtual double * ReadWrite(bool on_dev=true)
Shortcut for mfem::ReadWrite(vec.GetMemory(), vec.Size(), on_dev).
Definition: vector.hpp:465
void Mult(const mfem::Vector &x, mfem::Vector &y) const override
Operator application: y=A(x).
Definition: operator.cpp:42
Vector data type.
Definition: vector.hpp:60
Biwise-OR of all device backends.
Definition: device.hpp:96
virtual const double * Read(bool on_dev=true) const
Shortcut for mfem::Read(vec.GetMemory(), vec.Size(), on_dev).
Definition: vector.hpp:449
virtual double * HostReadWrite()
Shortcut for mfem::ReadWrite(vec.GetMemory(), vec.Size(), false).
Definition: vector.hpp:469
int width
Dimension of the input / number of columns in the matrix.
Definition: operator.hpp:28