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 #include "mex.h"
25 
26 namespace MueLu {
27 
28 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
30  : hasDeclaredInput_(false) {}
31 
32 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
34  RCP<ParameterList> validParamList = getInputParamList();
35  validParamList->set<std::string>("Provides", "", "A comma-separated list of objects provided on the coarse level by the TwoLevelMatlabFactory");
36  validParamList->set<std::string>("Needs Fine", "", "A comma-separated list of objects needed on the fine level by the TwoLevelMatlabFactory");
37  validParamList->set<std::string>("Needs Coarse", "", "A comma-separated list of objects needed on the coarse level by the TwoLevelMatlabFactory");
38  validParamList->set<std::string>("Function", "", "The name of the Matlab MEX function to call for Build()");
39  return validParamList;
40 }
41 
42 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
44  using namespace std;
45  const Teuchos::ParameterList& pL = GetParameterList();
46  // Get needs strings
47  const std::string str_nf = pL.get<std::string>("Needs Fine");
48  const std::string str_nc = pL.get<std::string>("Needs Coarse");
49  needsFine_ = tokenizeList(str_nf);
50  needsCoarse_ = tokenizeList(str_nc);
51  for (auto fineNeed : needsFine_) {
52  if (!IsParamMuemexVariable(fineNeed) && fineNeed != "Level")
53  this->Input(fineLevel, fineNeed);
54  }
55  for (auto coarseNeed : needsCoarse_) {
56  if (!IsParamMuemexVariable(coarseNeed) && coarseNeed != "Level")
57  this->Input(coarseLevel, coarseNeed);
58  }
59  hasDeclaredInput_ = true;
60 }
61 
62 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
64  FactoryMonitor m(*this, "Build", coarseLevel);
65 
66  const Teuchos::ParameterList& pL = GetParameterList();
67  using Teuchos::rcp;
68  using Teuchos::RCP;
69  using namespace std;
70  string needsFine = pL.get<string>("Needs Fine");
71  string needsCoarse = pL.get<string>("Needs Coarse");
72  vector<RCP<MuemexArg>> InputArgs = processNeeds<Scalar, LocalOrdinal, GlobalOrdinal, Node>(this, needsFine, fineLevel);
73  vector<RCP<MuemexArg>> InputArgsCoarse = processNeeds<Scalar, LocalOrdinal, GlobalOrdinal, Node>(this, needsCoarse, coarseLevel);
74  // Add coarse args to the end of InputArgs
75  InputArgs.reserve(InputArgs.size() + InputArgsCoarse.size());
76  InputArgs.insert(InputArgs.begin(), InputArgsCoarse.begin(), InputArgsCoarse.end());
77 
78  // Determine output
79  string provides = pL.get<string>("Provides");
80  size_t numProvides = tokenizeList(provides).size();
81  // Call mex function
82  string matlabFunction = pL.get<string>("Function");
83  if (!matlabFunction.length())
84  throw runtime_error("Invalid matlab function name");
85  vector<RCP<MuemexArg>> mexOutput = callMatlab(matlabFunction, numProvides, InputArgs);
86  processProvides<Scalar, LocalOrdinal, GlobalOrdinal, Node>(mexOutput, this, provides, coarseLevel);
87 }
88 
89 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
91  std::ostringstream out;
92  const Teuchos::ParameterList& pL = GetParameterList();
93  out << "TwoLevelMatlabFactory[" << pL.get<std::string>("Function") << "]";
94  return out.str();
95 }
96 
97 } // namespace MueLu
98 
99 #define MUELU_TWOLEVELMATLABFACTORY_SHORT
100 #endif // HAVE_MUELU_MATLAB
101 
102 #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.