MueLu  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MueLu_SingleLevelMatlabFactory_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_SINGLELEVELMATLABFACTORY_DEF_HPP
11 #define MUELU_SINGLELEVELMATLABFACTORY_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 #ifdef HAVE_MUELU_MATLAB
22 
23 namespace MueLu {
24 
25 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
27  : hasDeclaredInput_(false) {}
28 
29 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
31  RCP<ParameterList> validParamList = getInputParamList();
32  validParamList->set<std::string>("Provides", "", "A comma-separated list of objects provided by the SingleLevelMatlabFactory");
33  validParamList->set<std::string>("Needs", "", "A comma-separated list of objects needed by the SingleLevelMatlabFactory");
34  validParamList->set<std::string>("Function", "", "The name of the Matlab MEX function to call for Build()");
35  return validParamList;
36 }
37 
38 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
40  const Teuchos::ParameterList& pL = GetParameterList();
41  needs_ = tokenizeList(pL.get<std::string>("Needs"));
42  // Declare inputs
43  for (size_t i = 0; i < needs_.size(); i++) {
44  if (!IsParamMuemexVariable(needs_[i]) && needs_[i] != "Level")
45  this->Input(currentLevel, needs_[i]);
46  }
47  hasDeclaredInput_ = true;
48 }
49 
50 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
52  FactoryMonitor m(*this, "Build", currentLevel);
53 
54  const Teuchos::ParameterList& pL = GetParameterList();
55  using Teuchos::rcp;
56  using namespace std;
57  // NOTE: mexOutput[0] is the "Provides." Might want to modify to allow for additional outputs
58  string needsList = pL.get<string>("Needs");
59  vector<RCP<MuemexArg>> InputArgs = processNeeds<Scalar, LocalOrdinal, GlobalOrdinal, Node>(this, needsList, currentLevel);
60  string providesList = pL.get<std::string>("Provides");
61  size_t numProvides = tokenizeList(providesList).size();
62  // Call mex function
63  string matlabFunction = pL.get<std::string>("Function");
64  if (!matlabFunction.length())
65  throw std::runtime_error("Invalid matlab function name");
66  vector<Teuchos::RCP<MuemexArg>> mexOutput = callMatlab(matlabFunction, numProvides, InputArgs);
67  // Set output in level
68  processProvides<Scalar, LocalOrdinal, GlobalOrdinal, Node>(mexOutput, this, providesList, currentLevel);
69 }
70 
71 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
73  std::ostringstream out;
74  const Teuchos::ParameterList& pL = GetParameterList();
75  out << "SingleLevelMatlabFactory[" << pL.get<std::string>("Function") << "]";
76  return out.str();
77 }
78 
79 } // namespace MueLu
80 
81 #endif // HAVE_MUELU_MATLAB
82 
83 #endif // MUELU_SINGLELEVELMATLABFACTORY_DEF_HPP
std::vector< std::string > tokenizeList(const std::string &params)
void Build(Level &currentLevel) const
Build an object with this factory.
std::string description() const
@ name Description
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.
void DeclareInput(Level &currentLevel) const
Specifies the data that this class needs, and the factories that generate that data.
ParameterList & set(std::string const &name, T &&value, std::string const &docString="", RCP< const ParameterEntryValidator > const &validator=null)
bool IsParamMuemexVariable(const std::string &name)
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
RCP< const ParameterList > GetValidParameterList() const
Return a const parameter list of valid parameters that setParameterList() will accept.