MueLu  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MueLu_TwoLevelMatlabFactory_def.hpp
Go to the documentation of this file.
1 // @HEADER
2 // *****************************************************************************
3 // MueLu: A package for multigrid based preconditioning
4 //
5 // Copyright 2012 NTESS and the MueLu contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
10 #ifndef MUELU_TWOLEVELMATLABFACTORY_DEF_HPP
11 #define MUELU_TWOLEVELMATLABFACTORY_DEF_HPP
12 #include <Xpetra_Matrix.hpp>
13 #include <Xpetra_MultiVector.hpp>
14 
15 #include "MueLu_Monitor.hpp"
16 #include "MueLu_Aggregates.hpp"
17 #include "MueLu_AmalgamationInfo.hpp"
20 
21 #include <iostream>
22 
23 #ifdef HAVE_MUELU_MATLAB
24 
25 namespace MueLu {
26 
27 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
29  : hasDeclaredInput_(false) {}
30 
31 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
33  RCP<ParameterList> validParamList = getInputParamList();
34  validParamList->set<std::string>("Provides", "", "A comma-separated list of objects provided on the coarse level by the TwoLevelMatlabFactory");
35  validParamList->set<std::string>("Needs Fine", "", "A comma-separated list of objects needed on the fine level by the TwoLevelMatlabFactory");
36  validParamList->set<std::string>("Needs Coarse", "", "A comma-separated list of objects needed on the coarse level by the TwoLevelMatlabFactory");
37  validParamList->set<std::string>("Function", "", "The name of the Matlab MEX function to call for Build()");
38  return validParamList;
39 }
40 
41 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
43  using namespace std;
44  const Teuchos::ParameterList& pL = GetParameterList();
45  // Get needs strings
46  const std::string str_nf = pL.get<std::string>("Needs Fine");
47  const std::string str_nc = pL.get<std::string>("Needs Coarse");
48  needsFine_ = tokenizeList(str_nf);
49  needsCoarse_ = tokenizeList(str_nc);
50  for (auto fineNeed : needsFine_) {
51  if (!IsParamMuemexVariable(fineNeed) && fineNeed != "Level")
52  this->Input(fineLevel, fineNeed);
53  }
54  for (auto coarseNeed : needsCoarse_) {
55  if (!IsParamMuemexVariable(coarseNeed) && coarseNeed != "Level")
56  this->Input(coarseLevel, coarseNeed);
57  }
58  hasDeclaredInput_ = true;
59 }
60 
61 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
63  FactoryMonitor m(*this, "Build", coarseLevel);
64 
65  const Teuchos::ParameterList& pL = GetParameterList();
66  using Teuchos::rcp;
67  using Teuchos::RCP;
68  using namespace std;
69  string needsFine = pL.get<string>("Needs Fine");
70  string needsCoarse = pL.get<string>("Needs Coarse");
71  vector<RCP<MuemexArg>> InputArgs = processNeeds<Scalar, LocalOrdinal, GlobalOrdinal, Node>(this, needsFine, fineLevel);
72  vector<RCP<MuemexArg>> InputArgsCoarse = processNeeds<Scalar, LocalOrdinal, GlobalOrdinal, Node>(this, needsCoarse, coarseLevel);
73  // Add coarse args to the end of InputArgs
74  InputArgs.reserve(InputArgs.size() + InputArgsCoarse.size());
75  InputArgs.insert(InputArgs.begin(), InputArgsCoarse.begin(), InputArgsCoarse.end());
76 
77  // Determine output
78  string provides = pL.get<string>("Provides");
79  size_t numProvides = tokenizeList(provides).size();
80  // Call mex function
81  string matlabFunction = pL.get<string>("Function");
82  if (!matlabFunction.length())
83  throw runtime_error("Invalid matlab function name");
84  vector<RCP<MuemexArg>> mexOutput = callMatlab(matlabFunction, numProvides, InputArgs);
85  processProvides<Scalar, LocalOrdinal, GlobalOrdinal, Node>(mexOutput, this, provides, coarseLevel);
86 }
87 
88 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
90  std::ostringstream out;
91  const Teuchos::ParameterList& pL = GetParameterList();
92  out << "TwoLevelMatlabFactory[" << pL.get<std::string>("Function") << "]";
93  return out.str();
94 }
95 
96 } // namespace MueLu
97 
98 #endif // HAVE_MUELU_MATLAB
99 
100 #endif // MUELU_TWOLEVELMATLABFACTORY_DEF_HPP
std::vector< std::string > tokenizeList(const std::string &params)
RCP< const ParameterList > GetValidParameterList() const
Return a const parameter list of valid parameters that setParameterList() will accept.
std::vector< RCP< MuemexArg > > callMatlab(std::string function, int numOutputs, std::vector< RCP< MuemexArg > > args)
T & get(const std::string &name, T def_value)
Timer to be used in factories. Similar to Monitor but with additional timers.
ParameterList & set(std::string const &name, T &&value, std::string const &docString="", RCP< const ParameterEntryValidator > const &validator=null)
bool IsParamMuemexVariable(const std::string &name)
void DeclareInput(Level &fineLevel, Level &coarseLevel) const
Input.
Teuchos::RCP< Teuchos::ParameterList > getInputParamList()
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Class that holds all level-specific information.
Definition: MueLu_Level.hpp:63
std::string description() const
@ name Description
void Build(Level &fineLevel, Level &coarseLevel) const
Build an object with this factory.