MueLu  Version of the Day
MueLu_MatlabUtils_decl.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
46 
47 #ifndef MUELU_MATLABUTILS_DECL_HPP
48 #define MUELU_MATLABUTILS_DECL_HPP
49 
50 #include "MueLu_ConfigDefs.hpp"
51 
52 #if !defined(HAVE_MUELU_MATLAB) || !defined(HAVE_MUELU_EPETRA) || !defined(HAVE_MUELU_TPETRA)
53 #error "Muemex requires MATLAB, Epetra and Tpetra."
54 #else
55 
56 #include "mex.h"
57 #include <string>
58 #include <complex>
59 #include <stdexcept>
60 #include <Teuchos_ParameterList.hpp>
61 #include <Teuchos_RCP.hpp>
62 #include <Teuchos_DefaultComm.hpp>
63 #include "MueLu_Factory.hpp"
64 #include "MueLu_Hierarchy_decl.hpp"
67 #include "MueLu_Utilities_decl.hpp"
68 #include "MueLu_Graph_decl.hpp"
69 #include "Epetra_MultiVector.h"
70 #include "Epetra_CrsMatrix.h"
71 #include "Tpetra_CrsMatrix_decl.hpp"
72 #include "Xpetra_EpetraCrsMatrix.hpp"
73 #include "Xpetra_MapFactory.hpp"
74 #include "Xpetra_VectorFactory.hpp"
75 #include <Tpetra_DefaultPlatform.hpp>
76 
77 #ifdef HAVE_MUELU_INTREPID2_REFACTOR
78 #include "Kokkos_DynRankView.hpp"
79 #else
80 #include "Intrepid2_FieldContainer.hpp"
81 #endif
82 
83 
84 namespace MueLu
85 {
86 
88 {
89  INT,
109 #ifdef HAVE_MUELU_INTREPID2
110 , FIELDCONTAINER_ORDINAL
111 #endif
112 };
113 
114 typedef Kokkos::Compat::KokkosDeviceWrapperNode<Kokkos::Serial, Kokkos::HostSpace> mm_node_t;
115 typedef int mm_LocalOrd; //these are used for LocalOrdinal and GlobalOrdinal of all xpetra/tpetra templated types
116 typedef int mm_GlobalOrd;
117 typedef std::complex<double> complex_t;
118 typedef Tpetra::Map<> muemex_map_type;
119 typedef Tpetra::CrsMatrix<double, mm_LocalOrd, mm_GlobalOrd, mm_node_t> Tpetra_CrsMatrix_double;
120 typedef Tpetra::CrsMatrix<complex_t, mm_LocalOrd, mm_GlobalOrd, mm_node_t> Tpetra_CrsMatrix_complex;
121 typedef Tpetra::MultiVector<double, mm_LocalOrd, mm_GlobalOrd, mm_node_t> Tpetra_MultiVector_double;
122 typedef Tpetra::MultiVector<complex_t, mm_LocalOrd, mm_GlobalOrd, mm_node_t> Tpetra_MultiVector_complex;
123 typedef Xpetra::Map<mm_LocalOrd, mm_GlobalOrd, mm_node_t> Xpetra_map;
124 typedef Xpetra::Vector<mm_LocalOrd, mm_LocalOrd, mm_GlobalOrd, mm_node_t> Xpetra_ordinal_vector;
125 typedef Xpetra::Matrix<double, mm_LocalOrd, mm_GlobalOrd, mm_node_t> Xpetra_Matrix_double;
126 typedef Xpetra::Matrix<complex_t, mm_LocalOrd, mm_GlobalOrd, mm_node_t> Xpetra_Matrix_complex;
127 typedef Xpetra::MultiVector<double, mm_LocalOrd, mm_GlobalOrd, mm_node_t> Xpetra_MultiVector_double;
128 typedef Xpetra::MultiVector<complex_t, mm_LocalOrd, mm_GlobalOrd, mm_node_t> Xpetra_MultiVector_complex;
134 
135 #ifdef HAVE_MUELU_INTREPID2
136 #ifdef HAVE_MUELU_INTREPID2_REFACTOR
137  typedef Kokkos::DynRankView<mm_LocalOrd,typename mm_node_t::device_type> FieldContainer_ordinal;
138 #else
139  typedef Intrepid2::FieldContainer<mm_LocalOrd> FieldContainer_ordinal;
140 #endif
141 #endif
142 
144 {
145  public:
146  MuemexArg(MuemexType dataType) {type = dataType;}
148 };
149 
150 template<typename T>
151 MuemexType getMuemexType(const T & data);
152 
153 template<typename T>
154 class MuemexData : public MuemexArg
155 {
156  public:
157  MuemexData(T& data); //Construct from pre-existing data, to pass to MATLAB.
158  MuemexData(T& data, MuemexType type); //Construct from pre-existing data, to pass to MATLAB.
159  MuemexData(const mxArray* mxa); //Construct from MATLAB array, to get from MATLAB.
160  mxArray* convertToMatlab(); //Create a MATLAB object and copy this data to it
161  T& getData(); //Set and get methods
162  void setData(T& data);
163  private:
164  T data;
165 };
166 
167 template<typename T>
168 MuemexType getMuemexType(const T & data);
169 
170 template<typename T>
172 
173 template<typename T>
174 T loadDataFromMatlab(const mxArray* mxa);
175 
176 template<typename T>
177 mxArray* saveDataToMatlab(T& data);
178 
179 //Add data to level. Set the keep flag on the data to "user-provided" so it's not deleted.
180 template<typename T>
181 void addLevelVariable(const T& data, std::string& name, Level& lvl, const FactoryBase *fact = NoFactory::get());
182 
183 template<typename T>
184 const T& getLevelVariable(std::string& name, Level& lvl);
185 
186 //Functions used to put data through matlab factories - first arg is "this" pointer of matlab factory
187 template<typename Scalar = double, typename LocalOrdinal = mm_LocalOrd, typename GlobalOrdinal = mm_GlobalOrd, typename Node = mm_node_t>
188 std::vector<Teuchos::RCP<MuemexArg>> processNeeds(const Factory* factory, std::string& needsParam, Level& lvl);
189 
190 template<typename Scalar = double, typename LocalOrdinal = mm_LocalOrd, typename GlobalOrdinal = mm_GlobalOrd, typename Node = mm_node_t>
191 void processProvides(std::vector<Teuchos::RCP<MuemexArg>>& mexOutput, const Factory* factory, std::string& providesParam, Level& lvl);
192 
193 //create a sparse array in Matlab
194 template<typename Scalar> mxArray* createMatlabSparse(int numRows, int numCols, int nnz);
195 template<typename Scalar> mxArray* createMatlabMultiVector(int numRows, int numCols);
196 template<typename Scalar> void fillMatlabArray(Scalar* array, const mxArray* mxa, int n);
197 int* mwIndex_to_int(int N, mwIndex* mwi_array);
198 bool isValidMatlabAggregates(const mxArray* mxa);
199 bool isValidMatlabGraph(const mxArray* mxa);
200 std::vector<std::string> tokenizeList(const std::string& param);
201 //The two callback functions that MueLu can call to run anything in MATLAB
202 void callMatlabNoArgs(std::string function);
203 std::vector<Teuchos::RCP<MuemexArg>> callMatlab(std::string function, int numOutputs, std::vector<Teuchos::RCP<MuemexArg>> args);
204 Teuchos::RCP<Teuchos::ParameterList> getInputParamList();
205 Teuchos::RCP<MuemexArg> convertMatlabVar(const mxArray* mxa);
206 
207 // trim from start
208 static inline std::string &ltrim(std::string &s) {
209  s.erase(s.begin(), std::find_if(s.begin(), s.end(), std::not1(std::ptr_fun<int, int>(std::isspace))));
210  return s;
211 }
212 
213 // trim from end
214 static inline std::string &rtrim(std::string &s) {
215  s.erase(std::find_if(s.rbegin(), s.rend(), std::not1(std::ptr_fun<int, int>(std::isspace))).base(), s.end());
216  return s;
217 }
218 
219 // trim from both ends
220 static inline std::string &trim(std::string &s) {
221  return ltrim(rtrim(s));
222 }
223 
224 }//end namespace
225 
226 #endif //HAVE_MUELU_MATLAB error handler
227 #endif //MUELU_MATLABUTILS_DECL_HPP guard
Xpetra::MultiVector< double, mm_LocalOrd, mm_GlobalOrd, mm_node_t > Xpetra_MultiVector_double
bool isValidMatlabAggregates(const mxArray *mxa)
template mxArray * saveDataToMatlab(bool &data)
std::vector< std::string > tokenizeList(const std::string &params)
Xpetra::MultiVector< complex_t, mm_LocalOrd, mm_GlobalOrd, mm_node_t > Xpetra_MultiVector_complex
Container class for aggregation information.
Tpetra::CrsMatrix< complex_t, mm_LocalOrd, mm_GlobalOrd, mm_node_t > Tpetra_CrsMatrix_complex
void fillMatlabArray(Scalar *array, const mxArray *mxa, int n)
T loadDataFromMatlab(const mxArray *mxa)
Namespace for MueLu classes and methods.
bool isValidMatlabGraph(const mxArray *mxa)
MueLu::Hierarchy< complex_t, mm_LocalOrd, mm_GlobalOrd, mm_node_t > Hierarchy_complex
MuemexType getMuemexType(const T &data)
std::vector< Teuchos::RCP< MuemexArg > > processNeeds(const Factory *factory, std::string &needsParam, Level &lvl)
void addLevelVariable(const T &data, std::string &name, Level &lvl, const FactoryBase *fact=NoFactory::get())
static const NoFactory * get()
Teuchos::RCP< Teuchos::ParameterList > getInputParamList()
Base class for factories (e.g., R, P, and A_coarse).
Xpetra::Vector< mm_LocalOrd, mm_LocalOrd, mm_GlobalOrd, mm_node_t > Xpetra_ordinal_vector
MuemexArg(MuemexType dataType)
MueLu::AmalgamationInfo< mm_LocalOrd, mm_GlobalOrd, mm_node_t > MAmalInfo
int * mwIndex_to_int(int N, mwIndex *mwi_array)
Class that holds all level-specific information.
Definition: MueLu_Level.hpp:99
MueLu::Hierarchy< double, mm_LocalOrd, mm_GlobalOrd, mm_node_t > Hierarchy_double
Kokkos::Compat::KokkosDeviceWrapperNode< Kokkos::Serial, Kokkos::HostSpace > mm_node_t
Xpetra::Matrix< double, mm_LocalOrd, mm_GlobalOrd, mm_node_t > Xpetra_Matrix_double
const T & getLevelVariable(std::string &name, Level &lvl)
Xpetra::Matrix< complex_t, mm_LocalOrd, mm_GlobalOrd, mm_node_t > Xpetra_Matrix_complex
static std::string & trim(std::string &s)
MueLu::GraphBase< mm_LocalOrd, mm_GlobalOrd, mm_node_t > MGraph
void callMatlabNoArgs(std::string function)
MueLu representation of a graph.
static std::string & ltrim(std::string &s)
std::complex< double > complex_t
static std::string & rtrim(std::string &s)
MueLu::Aggregates< mm_LocalOrd, mm_GlobalOrd, mm_node_t > MAggregates
Tpetra::Map muemex_map_type
void processProvides(std::vector< Teuchos::RCP< MuemexArg >> &mexOutput, const Factory *factory, std::string &providesParam, Level &lvl)
Tpetra::MultiVector< complex_t, mm_LocalOrd, mm_GlobalOrd, mm_node_t > Tpetra_MultiVector_complex
Xpetra::Map< mm_LocalOrd, mm_GlobalOrd, mm_node_t > Xpetra_map
mxArray * createMatlabSparse(int numRows, int numCols, int nnz)
Tpetra::MultiVector< double, mm_LocalOrd, mm_GlobalOrd, mm_node_t > Tpetra_MultiVector_double
int mwIndex
minimal container class for storing amalgamation information
mxArray * createMatlabMultiVector(int numRows, int numCols)
Tpetra::CrsMatrix< double, mm_LocalOrd, mm_GlobalOrd, mm_node_t > Tpetra_CrsMatrix_double
Provides methods to build a multigrid hierarchy and apply multigrid cycles.
std::vector< RCP< MuemexArg > > callMatlab(std::string function, int numOutputs, std::vector< RCP< MuemexArg >> args)
Teuchos::RCP< MuemexArg > convertMatlabVar(const mxArray *mxa)