MueLu  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MueLu_MatlabSmoother_def.hpp
Go to the documentation of this file.
1 // @HEADER
2 //
3 // ***********************************************************************
4 //
5 // MueLu: A package for multigrid based preconditioning
6 // Copyright 2012 Sandia Corporation
7 //
8 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
9 // the U.S. Government retains certain rights in this software.
10 //
11 // Redistribution and use in source and binary forms, with or without
12 // modification, are permitted provided that the following conditions are
13 // met:
14 //
15 // 1. Redistributions of source code must retain the above copyright
16 // notice, this list of conditions and the following disclaimer.
17 //
18 // 2. Redistributions in binary form must reproduce the above copyright
19 // notice, this list of conditions and the following disclaimer in the
20 // documentation and/or other materials provided with the distribution.
21 //
22 // 3. Neither the name of the Corporation nor the names of the
23 // contributors may be used to endorse or promote products derived from
24 // this software without specific prior written permission.
25 //
26 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
27 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
30 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 //
38 // Questions? Contact
39 // Jonathan Hu (jhu@sandia.gov)
40 // Andrey Prokopenko (aprokop@sandia.gov)
41 // Ray Tuminaro (rstumin@sandia.gov)
42 //
43 // ***********************************************************************
44 //
45 // @HEADER
47 #ifndef MUELU_MATLABSMOOTHER_DEF_HPP
48 #define MUELU_MATLABSMOOTHER_DEF_HPP
50 
51 #if defined(HAVE_MUELU_TPETRA) && defined(HAVE_MUELU_MATLAB)
52 #include "MueLu_Monitor.hpp"
53 
54 
55 namespace MueLu {
56 
57  template <class Scalar,class LocalOrdinal, class GlobalOrdinal, class Node>
59  {
60  SetParameterList(paramList);
61  }
62 
63  template <class Scalar,class LocalOrdinal, class GlobalOrdinal, class Node>
65  {
66  Factory::SetParameterList(paramList);
67  ParameterList& pL = const_cast<ParameterList&>(this->GetParameterList());
68  setupFunction_ = pL.get("Setup Function","");
69  solveFunction_ = pL.get("Solve Function","");
70  solveDataSize_ = pL.get("Number of Solver Args", 0);
71  }
72 
73  template <class Scalar,class LocalOrdinal, class GlobalOrdinal, class Node>
75  {
76  using namespace std;
77  this->Input(currentLevel, "A");
78  ParameterList& pL = const_cast<ParameterList&>(this->GetParameterList());
79  needsSetup_ = pL.get<string>("Needs");
80  vector<string> needsList = tokenizeList(needsSetup_);
81  for(size_t i = 0; i < needsList.size(); i++)
82  {
83  if(!IsParamMuemexVariable(needsList[i]) && needsList[i] != "Level")
84  this->Input(currentLevel, needsList[i]);
85  }
86  }
87 
88  template <class Scalar,class LocalOrdinal, class GlobalOrdinal, class Node>
90  {
91  using namespace std;
92  FactoryMonitor m(*this, "Setup Smoother", currentLevel);
93  if (this->IsSetup() == true)
94  this->GetOStream(Warnings0) << "MueLu::MatlabSmoother::Setup(): Setup() has already been called";
95  vector<RCP<MuemexArg>> InputArgs = processNeeds<Scalar, LocalOrdinal, GlobalOrdinal, Node>(this, needsSetup_, currentLevel);
96  A_ = Factory::Get<RCP<Matrix>>(currentLevel, "A");
97  RCP<MuemexArg> AmatArg = rcp_implicit_cast<MuemexArg>(rcp(new MuemexData<RCP<Matrix>>(A_)));
98  //Always add A to the beginning of InputArgs
99  InputArgs.insert(InputArgs.begin(), AmatArg);
100  // Call mex function
101  if(!setupFunction_.length())
102  throw runtime_error("Invalid matlab function name");
103  solveData_= callMatlab(setupFunction_, solveDataSize_, InputArgs);
104  this->GetOStream(Statistics1) << description() << endl;
105  this->IsSetup(true); //mark the smoother as set up
106  }
107 
108  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
109  void MatlabSmoother<Scalar, LocalOrdinal, GlobalOrdinal, Node>::Apply(MultiVector& X, const MultiVector& B, bool InitialGuessIsZero) const
110  {
111  TEUCHOS_TEST_FOR_EXCEPTION(SmootherPrototype::IsSetup() == false, Exceptions::RuntimeError, "MueLu::MatlabSmoother::Apply(): Setup() has not been called");
112  using namespace Teuchos;
113  using namespace std;
114  // Push on A as first input
115  vector<RCP<MuemexArg>> InputArgs;
116  InputArgs.push_back(rcp(new MuemexData<RCP<Matrix>>(A_)));
117  // Push on LHS & RHS
118  RCP<MultiVector> Xrcp(&X, false);
119  MultiVector* BPtrNonConst = (MultiVector*) &B;
120  RCP<MultiVector> Brcp = rcp<MultiVector>(BPtrNonConst, false);
123  InputArgs.push_back(XData);
124  InputArgs.push_back(BData);
125  for(size_t i = 0; i < solveData_.size(); i++)
126  InputArgs.push_back(solveData_[i]);
127  if(!solveFunction_.length()) throw std::runtime_error("Invalid matlab function name");
128  vector<Teuchos::RCP<MuemexArg>> mexOutput = callMatlab(solveFunction_, 1, InputArgs);
129  RCP<MuemexData<RCP<MultiVector>>> mydata = Teuchos::rcp_static_cast<MuemexData<RCP<MultiVector>>>(mexOutput[0]);
130  X = *(mydata->getData());
131  }
132 
133  template <class Scalar,class LocalOrdinal, class GlobalOrdinal, class Node>
135  {
136  RCP<MatlabSmoother> smoother = rcp(new MatlabSmoother(*this) );
137  smoother->SetParameterList(this->GetParameterList());
138  return smoother;
139  }
140 
141  template <class Scalar,class LocalOrdinal, class GlobalOrdinal, class Node>
143  std::ostringstream out;
145  out << "Matlab Smoother("<<setupFunction_<<"/"<<solveFunction_<<")";
146  } else {
148  }
149  return out.str();
150  }
151 
152  template <class Scalar,class LocalOrdinal, class GlobalOrdinal, class Node>
155 
156  if (verbLevel & Parameters0)
157  out << "Matlab Smoother("<<setupFunction_<<"/"<<solveFunction_<<")";
158 
159  if (verbLevel & Parameters1) {
160  out0 << "Parameter list: " << std::endl;
161  Teuchos::OSTab tab2(out);
162  out << this->GetParameterList();
163  }
164 
165  if (verbLevel & Debug) {
166  out0 << "IsSetup: " << Teuchos::toString(SmootherPrototype::IsSetup()) << std::endl;
167  }
168  }
169 
170 
171 // Dummy specializations for GO = long long
172 /*template <>
173 void MatlabSmoother<double,int,long long>::Setup(Level& currentLevel) {
174  throw std::runtime_error("MatlabSmoother does not support GlobalOrdinal == long long.");
175 }
176 template <>
177 void MatlabSmoother<std::complex<double>,int,long long>::Setup(Level& currentLevel) {
178  throw std::runtime_error("MatlabSmoother does not support GlobalOrdinal == long long.");
179 }
180 
181 template <>
182 void MatlabSmoother<double,int,long long>::Apply(MultiVector& X, const MultiVector& B, bool InitialGuessIsZero) const {
183  throw std::runtime_error("MatlabSmoother does not support GlobalOrdinal == long long.");
184 }
185 template <>
186 void MatlabSmoother<std::complex<double>,int,long long>::Apply(MultiVector& X, const MultiVector& B, bool InitialGuessIsZero) const {
187  throw std::runtime_error("MatlabSmoother does not support GlobalOrdinal == long long.");
188 }*/
189 
190 
191 } // namespace MueLu
192 
193 #endif // HAVE_MUELU_TPETRA && HAVE_MUELU_MATLAB
194 #endif // MUELU_MATLABSMOOTHER_DEF_HPP
Important warning messages (one line)
void SetParameterList(const Teuchos::ParameterList &paramList)
Set parameters from a parameter list and return with default values.
void DeclareInput(Level &currentLevel) const
Input.
std::vector< std::string > tokenizeList(const std::string &params)
void Apply(MultiVector &X, const MultiVector &B, bool InitialGuessIsZero=false) const
Apply the preconditioner.
RCP< SmootherPrototype > Copy() const
T & get(const std::string &name, T def_value)
Timer to be used in factories. Similar to Monitor but with additional timers.
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Print more statistics.
void Setup(Level &currentLevel)
Set up the smoother.
Print additional debugging information.
bool IsParamMuemexVariable(const std::string &name)
virtual void SetParameterList(const Teuchos::ParameterList &paramList)
Set parameters from a parameter list and return with default values.
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Class that holds all level-specific information.
Definition: MueLu_Level.hpp:99
void print(Teuchos::FancyOStream &out, const VerbLevel verbLevel=Default) const
Print the object with some verbosity level to an FancyOStream object.
bool IsSetup() const
Get the state of a smoother prototype.
#define MUELU_DESCRIBE
Helper macro for implementing Describable::describe() for BaseClass objects.
friend class MatlabSmoother
Constructor.
Print class parameters.
Class that encapsulates Matlab smoothers.
std::string description() const
Return a simple one-line description of this object.
Print class parameters (more parameters, more verbose)
Exception throws to report errors in the internal logical of the program.
virtual std::string description() const
Return a simple one-line description of this object.
std::string toString(const T &t)
std::vector< RCP< MuemexArg > > callMatlab(std::string function, int numOutputs, std::vector< RCP< MuemexArg >> args)