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 #include "mex.h"
23 
24 namespace MueLu {
25 
26 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
28  : hasDeclaredInput_(false) {}
29 
30 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
32  RCP<ParameterList> validParamList = getInputParamList();
33  validParamList->set<std::string>("Provides", "", "A comma-separated list of objects provided by the SingleLevelMatlabFactory");
34  validParamList->set<std::string>("Needs", "", "A comma-separated list of objects needed by the SingleLevelMatlabFactory");
35  validParamList->set<std::string>("Function", "", "The name of the Matlab MEX function to call for Build()");
36  return validParamList;
37 }
38 
39 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
41  const Teuchos::ParameterList& pL = GetParameterList();
42  needs_ = tokenizeList(pL.get<std::string>("Needs"));
43  // Declare inputs
44  for (size_t i = 0; i < needs_.size(); i++) {
45  if (!IsParamMuemexVariable(needs_[i]) && needs_[i] != "Level")
46  this->Input(currentLevel, needs_[i]);
47  }
48  hasDeclaredInput_ = true;
49 }
50 
51 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
53  FactoryMonitor m(*this, "Build", currentLevel);
54 
55  const Teuchos::ParameterList& pL = GetParameterList();
56  using Teuchos::rcp;
57  using namespace std;
58  // NOTE: mexOutput[0] is the "Provides." Might want to modify to allow for additional outputs
59  string needsList = pL.get<string>("Needs");
60  vector<RCP<MuemexArg>> InputArgs = processNeeds<Scalar, LocalOrdinal, GlobalOrdinal, Node>(this, needsList, currentLevel);
61  string providesList = pL.get<std::string>("Provides");
62  size_t numProvides = tokenizeList(providesList).size();
63  // Call mex function
64  string matlabFunction = pL.get<std::string>("Function");
65  if (!matlabFunction.length())
66  throw std::runtime_error("Invalid matlab function name");
67  vector<Teuchos::RCP<MuemexArg>> mexOutput = callMatlab(matlabFunction, numProvides, InputArgs);
68  // Set output in level
69  processProvides<Scalar, LocalOrdinal, GlobalOrdinal, Node>(mexOutput, this, providesList, currentLevel);
70 }
71 
72 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
74  std::ostringstream out;
75  const Teuchos::ParameterList& pL = GetParameterList();
76  out << "SingleLevelMatlabFactory[" << pL.get<std::string>("Function") << "]";
77  return out.str();
78 }
79 
80 } // namespace MueLu
81 
82 #define MUELU_SINGLELEVELMATLABFACTORY_SHORT
83 #endif // HAVE_MUELU_MATLAB
84 
85 #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.