Debian Codegraph @

packages/muelu/matlab/bin/muemex.cpp

raw
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 #include "muemex.h"
11
12 #define IS_FALSE 0
13 #define IS_TRUE 1
14 #define MUEMEX_ERROR -1
15
16 // Do not compile MueMex if any of these aren't available
17 #if !defined HAVE_MUELU_TPETRA || !defined HAVE_MUELU_MATLAB
18 #error "MueMex requires Tpetra and MATLAB."
19 #endif
20
21 #include <Tpetra_Core.hpp>
22 #include "MueLu_MatlabUtils.hpp"
23 #include "MueLu_TwoLevelMatlabFactory.hpp"
24 #include "MueLu_SingleLevelMatlabFactory.hpp"
25
26 #include "BelosLinearProblem.hpp"
27 #include "BelosSolverFactory.hpp"
28 #include "BelosTpetraAdapter.hpp"
29
30 using namespace std;
31 using namespace Teuchos;
32
33 extern void _main();
34
35 /* MUEMEX Teuchos Parameters*/
36 #define MUEMEX_INTERFACE "Linear Algebra"
37
38 /* Default values */
39 #define MUEMEX_DEFAULT_LEVELS 10
40 #define MUEMEX_DEFAULT_NUMPDES 1
41 #define MUEMEX_DEFAULT_ADAPTIVEVECS 0
42 #define MUEMEX_DEFAULT_USEDEFAULTNS true
43 #define MMABS(x) ((x) > 0 ? (x) : (-(x)))
44 #define MMISINT(x) ((x) == 0 ? (((x - (int)(x)) < 1e-15) ? true : false) : (((x - (int)(x)) < 1e-15 * MMABS(x)) ? true : false))
45
46 /* Debugging */
47 //#define VERBOSE_OUTPUT
48
49 namespace MueLu {
50
51 // Need subclass of Hierarchy that gives public access to list of FactoryManagers
52 template <typename Scalar, typename LocalOrdinal, typename GlobalOrdinal, typename Node>
53 class OpenHierarchy : public Hierarchy<Scalar, LocalOrdinal, GlobalOrdinal, Node> {
54 public:
55 const RCP<const FactoryManagerBase>& GetFactoryManager(const int levelID) const;
56 };
57
58 template <typename Scalar, typename LocalOrdinal, typename GlobalOrdinal, typename Node>
59 const RCP<const FactoryManagerBase>& OpenHierarchy<Scalar, LocalOrdinal, GlobalOrdinal, Node>::GetFactoryManager(const int levelID) const {
60 TEUCHOS_TEST_FOR_EXCEPTION(levelID < 0 || levelID > this->GetNumLevels(), Exceptions::RuntimeError,
61 "MueLu::Hierarchy::GetFactoryManager(): invalid input parameter value: LevelID = " << levelID);
62 return this->GetLevelManager(levelID);
63 }
64
65 // Declare and call default constructor for data_pack_list vector (starts empty)
66 vector<RCP<MuemexSystem>> MuemexSystemList::list;
67 int MuemexSystemList::nextID = 0;
68
69 // Need a global flag to keep track of Epetra vs. Tpetra for constructing multivectors for param lists
70 bool useEpetra = false;
71
72 // Parse a string to get each bit of Belos verbosity
73 int strToMsgType(const char* str) {
74 if (str == NULL)
75 return Belos::Errors;
76 else if (strstr(str, "Warnings") != NULL)
77 return Belos::Warnings;
78 else if (strstr(str, "IterationDetails") != NULL)
79 return Belos::IterationDetails;
80 else if (strstr(str, "OrthoDetails") != NULL)
81 return Belos::OrthoDetails;
82 else if (strstr(str, "FinalSummary") != NULL)
83 return Belos::FinalSummary;
84 else if (strstr(str, "TimingDetails") != NULL)
85 return Belos::TimingDetails;
86 else if (strstr(str, "StatusTestDetails") != NULL)
87 return Belos::StatusTestDetails;
88 else if (strstr(str, "Debug") != NULL)
89 return Belos::Debug;
90 // This has no effect when added/OR'd with flags
91 return Belos::Errors;
92 }
93
94 MuemexType strToDataType(const char* str, char* typeName, bool complexFlag = false) {
95 std::string temp(str);
96 std::string myStr = trim(temp);
97 MuemexType matrixType, multivectorType, scalarType;
98 if (!complexFlag) {
99 matrixType = XPETRA_MATRIX_DOUBLE;
100 multivectorType = XPETRA_MULTIVECTOR_DOUBLE;
101 scalarType = DOUBLE;
102 } else {
103 matrixType = XPETRA_MATRIX_COMPLEX;
104 multivectorType = XPETRA_MULTIVECTOR_COMPLEX;
105 scalarType = COMPLEX;
106 }
107 size_t npos = string::npos;
108 if (myStr == "A" ||
109 myStr == "P" || myStr == "Ptent" ||
110 myStr == "R")
111 return matrixType;
112 if (myStr == "Nullspace")
113 return multivectorType;
114 if (myStr == "Aggregates")
115 return AGGREGATES;
116 if (myStr == "Graph")
117 return GRAPH;
118 if (myStr == "Coordinates")
119 return XPETRA_MULTIVECTOR_DOUBLE;
120 #ifdef HAVE_MUELU_INTREPID2
121 if (myStr == "pcoarsen: element to node map")
122 return FIELDCONTAINER_ORDINAL;
123 #endif
124 // Check for custom variable
125 size_t firstWordStart = myStr.find_first_not_of(' ');
126 size_t firstWordEnd = myStr.find(' ', firstWordStart);
127 std::string firstWord = myStr.substr(firstWordStart, firstWordEnd - firstWordStart);
128 if (firstWord.length() > 0) {
129 temp = myStr.substr(firstWordEnd, myStr.length() - firstWordEnd);
130 std::string secondWord = trim(temp);
131 if (secondWord.length() > 0) {
132 // make first word lowercase
133 std::transform(firstWord.begin(), firstWord.end(), firstWord.begin(), ::tolower);
134 // compare first word with possible values
135 if (firstWord.find("matrix") != npos)
136 return matrixType;
137 if (firstWord.find("multivector") != npos)
138 return multivectorType;
139 if (firstWord.find("map") != npos)
140 return XPETRA_MAP;
141 if (firstWord.find("ordinalvector") != npos)
142 return XPETRA_ORDINAL_VECTOR;
143 if (firstWord.find("int") != npos)
144 return INT;
145 if (firstWord.find("scalar") != npos)
146 return scalarType;
147 if (firstWord.find("double") != npos)
148 return DOUBLE;
149 if (firstWord.find("complex") != npos)
150 return COMPLEX;
151 }
152 }
153 if (typeName) {
154 std::string typeString(typeName);
155 std::transform(typeString.begin(), typeString.end(), typeString.begin(), ::tolower);
156 if (typeString.find("matrix") != npos)
157 return matrixType;
158 if (typeString.find("multivector") != npos)
159 return multivectorType;
160 if (typeString.find("map") != npos)
161 return XPETRA_MAP;
162 if (typeString.find("ordinalvector") != npos)
163 return XPETRA_ORDINAL_VECTOR;
164 if (typeString.find("int") != npos)
165 return INT;
166 if (typeString.find("scalar") != npos)
167 return scalarType;
168 if (typeString.find("double") != npos)
169 return DOUBLE;
170 if (typeString.find("complex") != npos)
171 return COMPLEX;
172 string errMsg = typeString + " is not a valid type.";
173 throw runtime_error(errMsg);
174 }
175 throw runtime_error("Could not determine type of data.");
176 }
177
178 // Parse a string to get Belos output style (Brief is default)
179 int strToOutputStyle(const char* str) {
180 if (strstr("General", str) != NULL)
181 return Belos::General;
182 else
183 return Belos::Brief;
184 }
185
186 // Get Belos "Verbosity" setting for its ParameterList
187 int getBelosVerbosity(const char* input) {
188 int result = 0;
189 char* str = (char*)input;
190 char* pch;
191 pch = strtok(str, " +,");
192 if (pch == NULL)
193 return result;
194 result |= strToMsgType(pch);
195 while (pch != NULL) {
196 pch = strtok(NULL, " +,");
197 if (pch == NULL)
198 return result;
199 result |= strToMsgType(pch);
200 }
201 return result;
202 }
203
204 template <typename Scalar>
205 mxArray* TpetraSystem<Scalar>::solve(RCP<ParameterList> params, RCP<Tpetra::CrsMatrix<Scalar, mm_LocalOrd, mm_GlobalOrd, mm_node_t>> matrix, const mxArray* b, int& iters) {
206 mxArray* output;
207 try {
208 int matSize = A->getGlobalNumRows();
209 // Define Tpetra vector/multivector types for convenience
210 typedef Tpetra::Vector<Scalar, mm_LocalOrd, mm_GlobalOrd, mm_node_t> Tpetra_Vector;
211 typedef Tpetra::MultiVector<Scalar, mm_LocalOrd, mm_GlobalOrd, mm_node_t> Tpetra_MultiVector;
212 typedef Tpetra::Operator<Scalar, mm_LocalOrd, mm_GlobalOrd, mm_node_t> Tpetra_Operator;
213 RCP<const Teuchos::Comm<int>> comm = Tpetra::getDefaultComm();
214 // numGlobalIndices for map constructor is the number of rows in matrix/vectors, right?
215 RCP<const muemex_map_type> map = rcp(new muemex_map_type(matSize, (mm_GlobalOrd)0, comm));
216 RCP<Tpetra_MultiVector> rhs = loadDataFromMatlab<RCP<Tpetra::MultiVector<Scalar, mm_LocalOrd, mm_GlobalOrd, mm_node_t>>>(b);
217 RCP<Tpetra_MultiVector> lhs = rcp(new Tpetra_MultiVector(map, rhs->getNumVectors()));
218 // rhs is initialized, lhs is not
219 // Default params
220 params->get("Output Frequency", 1);
221 params->get("Output Style", Belos::Brief);
222
223 #ifdef VERBOSE_OUTPUT
224 params->get("Verbosity", Belos::Errors | Belos::Warnings | Belos::Debug | Belos::FinalSummary | Belos::IterationDetails | Belos::OrthoDetails | Belos::TimingDetails | Belos::StatusTestDetails);
225 #else
226 params->get("Verbosity", Belos::Errors | Belos::Warnings | Belos::IterationDetails | Belos::Warnings | Belos::StatusTestDetails);
227 #endif
228 // register all possible solvers
229 auto problem = rcp(new Belos::LinearProblem<Scalar, Tpetra_MultiVector, Tpetra_Operator>(matrix, lhs, rhs));
230 problem->setRightPrec(prec);
231 if (!problem->setProblem()) {
232 throw std::runtime_error("ERROR: failed to set up Belos problem.");
233 }
234 std::string solverName = "GMRES";
235 if (params->isParameter("solver")) {
236 solverName = params->template get<std::string>("solver");
237 }
238 // Convert from basic MueMex solver names to the official Belos names.
239 // At the same time, check that solverName is in the valid set.
240 std::string belosSolverName;
241 if (solverName == "GMRES") {
242 belosSolverName = "PseudoBlock GMRES";
243 } else if (solverName == "CG") {
244 belosSolverName = "PseudoBlock CG";
245 } else {
246 std::string msg = std::string("ERROR: requested solver \"") + solverName + "\" not supported. Currently supported solvers: CG, GMRES";
247 mexPrintf("%s\n", msg.c_str());
248 output = mxCreateDoubleScalar(0);
249 return output;
250 }
251 Teuchos::RCP<Belos::SolverManager<Scalar, Tpetra_MultiVector, Tpetra_Operator>> solver;
252 Belos::SolverFactory<Scalar, Tpetra_MultiVector, Tpetra_Operator> factory;
253 try {
254 // Just use the default parameters for the solver
255 solver = factory.create(belosSolverName, params);
256 } catch (std::exception& e) {
257 mexPrintf("%s\n", e.what());
258 output = mxCreateDoubleScalar(0);
259 return output;
260 }
261 solver->setProblem(problem);
262 Belos::ReturnType ret = solver->solve();
263 iters = solver->getNumIters();
264 if (ret == Belos::Converged) {
265 mexPrintf("Success, Belos converged!\n");
266 output = saveDataToMatlab(lhs);
267 } else {
268 mexPrintf("Belos failed to converge.\n");
269 iters = 0;
270 output = mxCreateDoubleScalar(0);
271 }
272 } catch (exception& e) {
273 mexPrintf("Error occurred while running Belos solver:\n");
274 cout << e.what() << endl;
275 output = mxCreateDoubleScalar(0);
276 }
277 return output;
278 }
279
280 template <typename Scalar>
281 mxArray* TpetraSystem<Scalar>::apply(const mxArray* r) {
282 typedef Tpetra::MultiVector<Scalar, mm_LocalOrd, mm_GlobalOrd, mm_node_t> Tpetra_MultiVector;
283 RCP<Tpetra_MultiVector> rhs = loadDataFromMatlab<RCP<Tpetra::MultiVector<Scalar, mm_LocalOrd, mm_GlobalOrd, mm_node_t>>>(r);
284 RCP<Tpetra_MultiVector> lhs = rcp(new Tpetra_MultiVector(rhs->getMap(), rhs->getNumVectors()));
285 try {
286 this->prec->apply(*rhs, *lhs);
287 } catch (exception& e) {
288 mexPrintf("Error occurred while applying MueLu-Tpetra preconditioner:\n");
289 cout << e.what() << endl;
290 }
291 return saveDataToMatlab(lhs);
292 }
293
294 template <>
295 RCP<Hierarchy_double> getDatapackHierarchy<double>(MuemexSystem* dp) {
296 RCP<MueLu::Hierarchy<double, mm_LocalOrd, mm_GlobalOrd, mm_node_t>> hier;
297 switch (dp->type) {
298 #ifdef HAVE_MUELU_EPETRA
299 case EPETRA: {
300 EpetraSystem* pack = (EpetraSystem*)dp;
301 hier = pack->getHierarchy();
302 break;
303 }
304 #endif
305 case TPETRA: {
306 TpetraSystem<double>* pack = (TpetraSystem<double>*)dp;
307 hier = pack->getHierarchy();
308 break;
309 }
310 default: {
311 throw runtime_error("Got unexpected linear system type for real-valued functions.");
312 }
313 }
314 return hier;
315 }
316
317 #ifdef HAVE_COMPLEX_SCALARS
318 template <>
319 RCP<Hierarchy_complex> getDatapackHierarchy<complex_t>(MuemexSystem* dp) {
320 return ((TpetraSystem<complex_t>*)dp)->getHierarchy();
321 }
322 #endif
323
324 template <typename Scalar, typename T>
325 void setHierarchyData(MuemexSystem* problem, int levelID, T& data, string& dataName) {
326 RCP<Level> level;
327 #ifdef HAVE_MUELU_EPETRA
328 if (problem->type == EPETRA) {
329 RCP<Hierarchy<double, mm_LocalOrd, mm_GlobalOrd, mm_node_t>> hier = ((EpetraSystem*)problem)->getHierarchy();
330 level = hier->GetLevel(levelID);
331 } else
332 #endif
333 if (problem->type == TPETRA) {
334 RCP<Hierarchy<double, mm_LocalOrd, mm_GlobalOrd, mm_node_t>> hier = ((TpetraSystem<double>*)problem)->getHierarchy();
335 level = hier->GetLevel(levelID);
336 } else if (problem->type == TPETRA_COMPLEX) {
337 #ifdef HAVE_COMPLEX_SCALARS
338 RCP<Hierarchy<complex_t, mm_LocalOrd, mm_GlobalOrd, mm_node_t>> hier = ((TpetraSystem<complex_t>*)problem)->getHierarchy();
339 level = hier->GetLevel(levelID);
340 #else
341 throw std::runtime_error("setHierarchyData(): complex scalars not supported.");
342 #endif
343 }
344 if (level.is_null())
345 throw runtime_error("Error getting level when setting custom level data.");
346 level->Set(dataName, data);
347 level->AddKeepFlag(dataName, NoFactory::get(), UserData);
348 }
349
350 // data pack base class implementation
351
352 MuemexSystem::MuemexSystem(DataPackType probType)
353 : id(MUEMEX_ERROR)
354 , type(probType) {}
355 MuemexSystem::~MuemexSystem() {}
356
357 mxArray* MuemexSystem::getHierarchyData(string dataName, MuemexType dataType, int levelID) {
358 mxArray* output = NULL;
359 try {
360 // First, get Level, which doesn't depend on Epetra vs. Tpetra
361 RCP<MueLu::Level> level;
362 RCP<const FactoryManagerBase> fmb;
363 if (this->type == TPETRA) {
364 TpetraSystem<double>* tsys = (TpetraSystem<double>*)this;
365 if (tsys->keepAll)
366 fmb = tsys->systemManagers[levelID];
367 } else if (this->type == TPETRA_COMPLEX) {
368 #ifdef HAVE_COMPLEX_SCALARS
369 TpetraSystem<complex_t>* tsys = (TpetraSystem<complex_t>*)this;
370 if (tsys->keepAll)
371 fmb = tsys->systemManagers[levelID];
372 #else
373 throw std::runtime_error("getHierarchyData(): complex scalars not supported.");
374 #endif
375 }
376 const FactoryBase* factory = NoFactory::get(); //(ptr to constant)
377 bool needFMB = true;
378 if (dataName == "A" || dataName == "P") // these are kept by default, don't use actual factory pointer
379 // Otherwise would break getting A and P when 'keep' is off
380 needFMB = false;
381 switch (this->type) {
382 #ifdef HAVE_MUELU_EPETRA
383 case EPETRA:
384 #endif
385 case TPETRA: {
386 RCP<OpenHierarchy<double, mm_LocalOrd, mm_GlobalOrd, mm_node_t>> hier = rcp_static_cast<OpenHierarchy<double, mm_LocalOrd, mm_GlobalOrd, mm_node_t>>(getDatapackHierarchy<double>(this));
387 level = hier->GetLevel(levelID);
388 if (needFMB) {
389 if (fmb.is_null())
390 fmb = (RCP<const FactoryManagerBase>)hier->GetFactoryManager(levelID);
391 if (!fmb.is_null()) {
392 try {
393 factory = fmb->GetFactory(dataName).get();
394 } catch (exception& e) {
395 } // forced to try using NoFactory (which will work with default keeps A, P)
396 }
397 }
398 break;
399 }
400 case TPETRA_COMPLEX: {
401 #ifdef HAVE_COMPLEX_SCALARS
402 RCP<OpenHierarchy<complex_t, mm_LocalOrd, mm_GlobalOrd, mm_node_t>> hier = rcp_static_cast<OpenHierarchy<complex_t, mm_LocalOrd, mm_GlobalOrd, mm_node_t>>(getDatapackHierarchy<complex_t>(this));
403 level = hier->GetLevel(levelID);
404 if (needFMB) {
405 if (fmb.is_null())
406 fmb = (RCP<const FactoryManagerBase>)hier->GetFactoryManager(levelID);
407 if (!fmb.is_null()) {
408 try {
409 factory = fmb->GetFactory(dataName).get();
410 } catch (exception& e) {
411 } // attempt to use NoFactory
412 }
413 }
414 break;
415 #else
416 throw std::runtime_error("Complex scalars not supported");
417 #endif
418 }
419 }
420 if (level.is_null())
421 throw runtime_error("Can't get level data because level is null.");
422 bool dataIsAvailable = level->IsAvailable(dataName, factory);
423 if (!dataIsAvailable) {
424 // Give the level the FactoryManager again so it can provide the data (by re-creating it, if necessary)
425 level->SetFactoryManager(fmb);
426 }
427 // Given the dataName and factory pointer, all data in the level should now be accessible
428 switch (dataType) {
429 case XPETRA_MATRIX_DOUBLE:
430 return saveDataToMatlab(level->Get<RCP<Xpetra_Matrix_double>>(dataName, factory));
431 case XPETRA_MATRIX_COMPLEX: {
432 #ifdef HAVE_COMPLEX_SCALARS
433 return saveDataToMatlab(level->Get<RCP<Xpetra_Matrix_complex>>(dataName, factory));
434 #endif
435 }
436 case XPETRA_MULTIVECTOR_DOUBLE:
437 if (dataName == "Coordinates") {
438 // Coordinates is special because it's always user-provided on level 0, not always provided at all, not always kept in the level (only kept if doing agg viz, etc), and is always MV<double> regardless of problem scalar type
439 double errReturn = -1;
440 if (level->GetLevelID() == 0) {
441 // Try to get coordinates as if it's user data, but don't be surprised if it's not there at all.
442 try {
443 RCP<Xpetra_MultiVector_double> coords = level->Get<RCP<Xpetra_MultiVector_double>>(dataName, NoFactory::get());
444 if (coords.is_null())
445 throw runtime_error("Coordinates were not available (Level 0)."); // just print the message below and return -1
446 return saveDataToMatlab(coords);
447 } catch (exception& e) {
448 cout << endl
449 << "Coordinates were not available on Level 0." << endl;
450 cout << "They must be provided by the user and aren't generated or kept by default (even in MueMex 'keep' mode)." << endl;
451 cout << "User-provided coordinates for Level 0 will be kept and passed to other levels if something requires them:" << endl;
452 cout << "aggregate visualization, brick aggregation, repartitioning or distance laplacian filtering." << endl
453 << endl;
454 return saveDataToMatlab(errReturn);
455 }
456 } else {
457 // If coords are provided & kept, they are produced by CoordinatesTransferFactory in levels > 0.
458 try {
459 RCP<Xpetra_MultiVector_double> coords = level->Get<RCP<Xpetra_MultiVector_double>>(dataName, factory);
460 if (coords.is_null())
461 throw runtime_error("Coordinates were not available (Level > 0).");
462 return saveDataToMatlab(coords);
463 } catch (exception& e) {
464 cout << "Coordinates must be provided by the user and aren't generated or kept by default (even in MueMex 'keep' mode)." << endl;
465 cout << "User-provided coordinates for Level 0 will be kept and passed to other levels if something requires them:" << endl;
466 cout << "aggregate visualization, brick aggregation, repartitioning or distance laplacian filtering." << endl
467 << endl;
468 return saveDataToMatlab(errReturn);
469 }
470 }
471 } else {
472 return saveDataToMatlab(level->Get<RCP<Xpetra_MultiVector_double>>(dataName, factory));
473 }
474 case XPETRA_MULTIVECTOR_COMPLEX:
475 return saveDataToMatlab(level->Get<RCP<Xpetra_MultiVector_complex>>(dataName, factory));
476 case XPETRA_MAP:
477 return saveDataToMatlab(level->Get<RCP<Xpetra_map>>(dataName, factory));
478 case XPETRA_ORDINAL_VECTOR:
479 return saveDataToMatlab(level->Get<RCP<Xpetra_ordinal_vector>>(dataName, factory));
480 case DOUBLE:
481 return saveDataToMatlab(level->Get<double>(dataName, factory));
482 case COMPLEX:
483 return saveDataToMatlab(level->Get<complex_t>(dataName, factory));
484 case INT:
485 return saveDataToMatlab(level->Get<int>(dataName, factory));
486 case AGGREGATES:
487 return saveDataToMatlab(level->Get<RCP<MAggregates>>(dataName, factory));
488 case GRAPH:
489 return saveDataToMatlab(level->Get<RCP<MGraph>>(dataName, factory));
490 #ifdef HAVE_MUELU_INTREPID2
491 return saveDataToMatlab(level->Get<RCP<FieldContainer_ordinal>>(dataName, factory));
492 #endif
493 default:
494 throw runtime_error("Invalid MuemexType for getting hierarchy data.");
495 }
496 } catch (exception& e) {
497 mexPrintf("Error occurred while getting hierarchy data.\n");
498 cout << e.what() << endl;
499 }
500 return output;
501 }
502
503 #ifdef HAVE_MUELU_EPETRA
504 // EpetraSystem impl
505
506 EpetraSystem::EpetraSystem()
507 : MuemexSystem(EPETRA) {}
508 EpetraSystem::~EpetraSystem() {}
509
510 int EpetraSystem::status() {
511 mexPrintf("**** Problem ID %d [MueLu_Epetra] ****\n", id);
512 if (!A.is_null())
513 mexPrintf("Matrix: %dx%d w/ %d nnz\n", A->NumGlobalRows(), A->NumGlobalCols(), A->NumMyNonzeros());
514 mexPrintf("Operator Complexity: %f\n", operatorComplexity);
515 if (!List.is_null()) {
516 mexPrintf("Parameter List:\n");
517 List->print();
518 }
519 mexPrintf("\n");
520 return IS_TRUE;
521 } /*end status*/
522
523 int EpetraSystem::setup(const mxArray* matlabA, bool haveCoords, const mxArray* matlabCoords) {
524 bool success = false;
525 try {
526 /* Matrix Fill */
527 A = loadDataFromMatlab<RCP<Epetra_CrsMatrix>>(matlabA);
528 if (haveCoords) {
529 // Create 'user data' sublist if it doesn't already exist
530 auto userData = Teuchos::sublist(List, "user data");
531 userData->set("Coordinates", loadDataFromMatlab<RCP<Epetra_MultiVector>>(matlabCoords));
532 }
533 prec = MueLu::CreateEpetraPreconditioner(A, *List);
534 // underlying the Epetra_Operator prec is a MueLu::EpetraOperator
535 RCP<MueLu::EpetraOperator> meo = rcp_static_cast<MueLu::EpetraOperator, Epetra_Operator>(prec);
536 operatorComplexity = meo->GetHierarchy()->GetOperatorComplexity();
537 success = true;
538 } catch (exception& e) {
539 mexPrintf("Error occurred while setting up epetra problem:\n");
540 cout << e.what() << endl;
541 }
542 return success ? IS_TRUE : IS_FALSE;
543 } /*end setup*/
544
545 /* EpetraSystem::solve - Given two Teuchos lists, one in the EpetraSystem, and one of
546 solve-time options, this routine calls the relevant solver and returns the solution.
547 TPL - Teuchos list of solve-time options [I]
548 A - The matrix to solve with (may not be the one the preconditioned was used for)
549 b - RHS vector [I]
550 x - solution vector [O]
551 iters - number of iterations taken [O]
552 Returns: IS_TRUE if solve was succesful, IS_FALSE otherwise
553 */
554 mxArray* EpetraSystem::solve(RCP<ParameterList> TPL, RCP<Epetra_CrsMatrix> matrix, const mxArray* b, int& iters) {
555 mxArray* output;
556 try {
557 // Set up X and B
558 Epetra_Map map = matrix->DomainMap();
559 RCP<Epetra_MultiVector> rhs = loadDataFromMatlab<RCP<Epetra_MultiVector>>(b);
560 RCP<Epetra_MultiVector> lhs = rcp(new Epetra_MultiVector(map, rhs->NumVectors(), true));
561 // Default params
562 TPL->get("Output Frequency", 1);
563 TPL->get("Output Style", Belos::Brief);
564 #ifdef VERBOSE_OUTPUT
565 TPL->get("Verbosity", Belos::Errors | Belos::Warnings | Belos::Debug | Belos::FinalSummary | Belos::IterationDetails | Belos::OrthoDetails | Belos::TimingDetails | Belos::StatusTestDetails);
566 #else
567 TPL->get("Verbosity", Belos::Errors + Belos::Warnings + Belos::IterationDetails + Belos::Warnings + Belos::StatusTestDetails);
568 #endif
569 RCP<Belos::LinearProblem<double, Epetra_MultiVector, Epetra_Operator>> problem = rcp(new Belos::LinearProblem<double, Epetra_MultiVector, Epetra_Operator>(matrix, lhs, rhs));
570 RCP<Belos::EpetraPrecOp> epo = rcp(new Belos::EpetraPrecOp(prec));
571 problem->setRightPrec(epo);
572 bool set = problem->setProblem();
573 TEUCHOS_TEST_FOR_EXCEPTION(!set, runtime_error, "Linear Problem failed to set up correctly!");
574 Belos::SolverFactory<double, Epetra_MultiVector, Epetra_Operator> factory;
575 // Get the solver name from the parameter list, default to PseudoBlockGmres if none specified by user
576 string solverName = TPL->get("solver", "GMRES");
577 RCP<Belos::SolverManager<double, Epetra_MultiVector, Epetra_Operator>> solver = factory.create(solverName, TPL);
578 solver->setProblem(problem);
579 Belos::ReturnType ret = solver->solve();
580 if (ret == Belos::Converged) {
581 mexPrintf("Success, Belos converged!\n");
582 iters = solver->getNumIters();
583 output = saveDataToMatlab(lhs);
584 } else {
585 mexPrintf("Belos failed to converge.\n");
586 iters = 0;
587 output = mxCreateDoubleScalar(0);
588 }
589 output = saveDataToMatlab(lhs);
590 } catch (exception& e) {
591 mexPrintf("Error occurred during Belos solve:\n");
592 cout << e.what() << endl;
593 output = mxCreateDoubleScalar(0);
594 }
595 return output;
596 }
597
598 mxArray* EpetraSystem::apply(const mxArray* r) {
599 RCP<Epetra_MultiVector> rhs = loadDataFromMatlab<RCP<Epetra_MultiVector>>(r);
600 Epetra_SerialComm Comm;
601 Epetra_Map map(rhs->GlobalLength(), 0, Comm);
602 RCP<Epetra_MultiVector> lhs = rcp(new Epetra_MultiVector(map, rhs->NumVectors(), true));
603 try {
604 this->prec->Apply(*rhs, *lhs);
605 } catch (exception& e) {
606 mexPrintf("Error occurred while applying MueLu-Epetra preconditioner:\n");
607 cout << e.what() << endl;
608 }
609 return saveDataToMatlab(lhs);
610 }
611
612 RCP<Hierarchy_double> EpetraSystem::getHierarchy() {
613 RCP<MueLu::EpetraOperator> meo = rcp_static_cast<MueLu::EpetraOperator, Epetra_Operator>(prec);
614 RCP<MueLu::Hierarchy<double, mm_LocalOrd, mm_GlobalOrd, mm_node_t>> hier = meo->GetHierarchy();
615 if (hier.is_null())
616 throw runtime_error("Hierarchy from Epetra problem was null.");
617 return hier;
618 }
619 #endif
620
621 // tpetra_double_data_pack implementation
622
623 template <>
624 TpetraSystem<double>::TpetraSystem()
625 : MuemexSystem(TPETRA) {}
626 template <>
627 TpetraSystem<double>::~TpetraSystem() {}
628
629 template <typename Scalar>
630 int TpetraSystem<Scalar>::setup(const mxArray* matlabA, bool haveCoords, const mxArray* matlabCoords) {
631 // decide whether do do default or custom setup
632 bool doCustomSetup = List->isParameter("keep") && List->isType<bool>("keep") && List->get<bool>("keep");
633 List->remove("keep", false); //"keep" would cause Plist validation to fail if left in
634 if (doCustomSetup) {
635 try {
636 customSetup(matlabA, haveCoords, matlabCoords);
637 } catch (exception& e) {
638 cout << "An error occurred during Tpetra custom problem setup:" << endl;
639 cout << e.what() << endl;
640 return IS_FALSE;
641 }
642 } else {
643 try {
644 normalSetup(matlabA, haveCoords, matlabCoords);
645 } catch (exception& e) {
646 cout << "An error occurred during Tpetra preconditioner setup:" << endl;
647 cout << e.what();
648 return IS_FALSE;
649 }
650 }
651 return IS_TRUE;
652 }
653
654 template <typename Scalar>
655 void TpetraSystem<Scalar>::normalSetup(const mxArray* matlabA, bool haveCoords, const mxArray* matlabCoords) {
656 keepAll = false;
657 A = loadDataFromMatlab<RCP<Tpetra::CrsMatrix<Scalar, mm_LocalOrd, mm_GlobalOrd, mm_node_t>>>(matlabA);
658 RCP<Tpetra::Operator<Scalar, mm_LocalOrd, mm_GlobalOrd, mm_node_t>> opA(A);
659 RCP<MueLu::TpetraOperator<Scalar, mm_LocalOrd, mm_GlobalOrd, mm_node_t>> mop;
660 if (haveCoords) {
661 auto userData = Teuchos::sublist(List, "user data");
662 userData->set("Coordinates", loadDataFromMatlab<RCP<Tpetra_MultiVector_double>>(matlabCoords));
663 }
664 // Create the nullspace if not already set by user through XML
665 if (!(List->isSublist("level 0") && List->sublist("level 0", true).isParameter("Nullspace")) && !(List->isSublist("user data") && List->sublist("user data", true).isParameter("Nullspace"))) {
666 int nPDE = MasterList::getDefault<int>("number of equations");
667 if (List->isSublist("Matrix")) {
668 // Factory style parameter list
669 const Teuchos::ParameterList& operatorList = List->sublist("Matrix");
670 if (operatorList.isParameter("PDE equations"))
671 nPDE = operatorList.get<int>("PDE equations");
672 } else if (List->isParameter("number of equations")) {
673 // Easy style parameter list
674 nPDE = List->get<int>("number of equations");
675 }
676 mexPrintf("** Constructing nullspace for %d PDEs\n", nPDE);
677 auto domainMap = A->getDomainMap();
678 auto nullspace = rcp(new Tpetra::MultiVector<Scalar, mm_LocalOrd, mm_GlobalOrd, mm_node_t>(domainMap, nPDE));
679 if (nPDE == 1) {
680 nullspace->putScalar(Teuchos::ScalarTraits<Scalar>::one());
681 } else {
682 typedef typename Teuchos::ArrayRCP<Scalar>::size_type arrayRCPSizeType;
683 for (int i = 0; i < nPDE; i++) {
684 Teuchos::ArrayRCP<Scalar> nsData = nullspace->getDataNonConst(i);
685 for (arrayRCPSizeType j = 0; j < nsData.size(); j++) {
686 mm_GlobalOrd GID = domainMap->getGlobalElement(j) - domainMap->getIndexBase();
687 if ((GID - i) % nPDE == 0)
688 nsData[j] = Teuchos::ScalarTraits<Scalar>::one();
689 }
690 }
691 }
692 auto userData = Teuchos::sublist(List, "user data");
693 userData->set("Nullspace", nullspace);
694 }
695 mop = MueLu::CreateTpetraPreconditioner<Scalar, mm_LocalOrd, mm_GlobalOrd, mm_node_t>(opA, *List);
696 prec = rcp_implicit_cast<Tpetra::Operator<Scalar, mm_LocalOrd, mm_GlobalOrd, mm_node_t>>(mop);
697
698 // print data??
699 // mop->GetHierarchy()->GetLevel(0)->print(std::cout, MueLu::Debug);
700
701 operatorComplexity = mop->GetHierarchy()->GetOperatorComplexity();
702 }
703
704 template <typename Scalar>
705 void TpetraSystem<Scalar>::customSetup(const mxArray* matlabA, bool haveCoords, const mxArray* matlabCoords) {
706 keepAll = true;
707 A = loadDataFromMatlab<RCP<Tpetra::CrsMatrix<Scalar, mm_LocalOrd, mm_GlobalOrd, mm_node_t>>>(matlabA);
708 RCP<MueLu::TpetraOperator<Scalar, mm_LocalOrd, mm_GlobalOrd, mm_node_t>> mop;
709 // Now modify CreateTpetraPreconditioner to set keep flags on all factories
710 typedef Xpetra::MultiVector<Scalar, mm_LocalOrd, mm_GlobalOrd, mm_node_t> MultiVector;
711 typedef Xpetra::Matrix<Scalar, mm_LocalOrd, mm_GlobalOrd, mm_node_t> Matrix;
712 typedef Hierarchy<Scalar, mm_LocalOrd, mm_GlobalOrd, mm_node_t> Hierarchy;
713 typedef HierarchyManager<Scalar, mm_LocalOrd, mm_GlobalOrd, mm_node_t> HierarchyManager;
714 RCP<HierarchyManager> mueluFactory = rcp(new ParameterListInterpreter<Scalar, mm_LocalOrd, mm_GlobalOrd, mm_node_t>(*List, A->getComm()));
715 RCP<Hierarchy> H = mueluFactory->CreateHierarchy();
716 H->setlib(Xpetra::UseTpetra);
717 RCP<Matrix> xA = TpetraCrs_To_XpetraMatrix<Scalar, mm_LocalOrd, mm_GlobalOrd, mm_node_t>(A);
718 H->GetLevel(0)->Set("A", xA);
719 if (haveCoords) {
720 RCP<Xpetra::MultiVector<double, mm_LocalOrd, mm_GlobalOrd, mm_node_t>> coords = loadDataFromMatlab<RCP<Xpetra_MultiVector_double>>(matlabCoords);
721 H->GetLevel(0)->Set("Coordinates", coords);
722 }
723 // Decide whether user passed level 0 Nullspace in parameter list. If not, make it here.
724 if (!List->isSublist("level 0") || !List->sublist("level 0", true).isParameter("Nullspace")) {
725 int nPDE = MasterList::getDefault<int>("number of equations");
726 if (List->isSublist("Matrix")) {
727 // Factory style parameter list
728 const Teuchos::ParameterList& operatorList = List->sublist("Matrix");
729 if (operatorList.isParameter("PDE equations"))
730 nPDE = operatorList.get<int>("PDE equations");
731 } else if (List->isParameter("number of equations")) {
732 // Easy style parameter list
733 nPDE = List->get<int>("number of equations");
734 }
735 RCP<MultiVector> nullspace = Xpetra::MultiVectorFactory<Scalar, mm_LocalOrd, mm_GlobalOrd, mm_node_t>::Build(xA->getDomainMap(), nPDE);
736 if (nPDE == 1) {
737 nullspace->putScalar(Teuchos::ScalarTraits<Scalar>::one());
738 } else {
739 typedef typename Teuchos::ArrayRCP<Scalar>::size_type arrayRCPSizeType;
740 for (int i = 0; i < nPDE; i++) {
741 Teuchos::ArrayRCP<Scalar> nsData = nullspace->getDataNonConst(i);
742 for (arrayRCPSizeType j = 0; j < nsData.size(); j++) {
743 // TODO optimizations:
744 // TODO This can be optimized by getting the domain map and index base outside the loop.
745 // TODO Also, the whole local-to-global lookup table can be fetched one time, instead of repeatedly
746 // TODO calling getGlobalElement.
747 mm_GlobalOrd GID = A->getDomainMap()->getGlobalElement(j) - A->getDomainMap()->getIndexBase();
748 if ((GID - i) % nPDE == 0)
749 nsData[j] = Teuchos::ScalarTraits<Scalar>::one();
750 }
751 }
752 }
753 H->GetLevel(0)->Set("Nullspace", nullspace);
754 }
755 Teuchos::ParameterList nonSerialList, dummyList;
756 ExtractNonSerializableData(*List, dummyList, nonSerialList);
757 HierarchyUtils<Scalar, mm_LocalOrd, mm_GlobalOrd, mm_node_t>::AddNonSerializableDataToHierarchy(*mueluFactory, *H, nonSerialList);
758 // Set up dummy levels in hierarchy
759 for (int i = 0; i < 5; i++) {
760 RCP<Level> l = rcp(new Level());
761 H->AddLevel(l);
762 }
763 // Set keep flags on ALL factories in ALL levels
764 // We have access to H's list of FactoryManagers so we know how to get factory pointer given the name of the factory.
765 // We don't know which names are in the FactoryManagers though so a brute force approach is needed...
766 vector<string> keepItems = {"A", "P", "R", "Ptent", "Aggregates", "Coordinates", "UnAmalgamationInfo", "Smoother", "PreSmoother", "PostSmoother", "CoarseSolver", "Graph", "CoarseMap", "Nullspace", "Ppattern", "Constraint", "CoarseNumZLayers", "LineDetection_Layers", "LineDetection_VertLineIds", "Partition", "Importer", "DofsPerNode",
767 "Filtering"
768 "pcoarsen: element to node map"};
769 RCP<OpenHierarchy<Scalar, mm_LocalOrd, mm_GlobalOrd, mm_node_t>> openH = rcp_static_cast<OpenHierarchy<Scalar, mm_LocalOrd, mm_GlobalOrd, mm_node_t>, Hierarchy>(H);
770 if (openH.is_null())
771 throw runtime_error("Could not cast RCP<Hierarchy> to subclass.");
772 for (int lvl = 0; lvl < H->GetNumLevels(); lvl++) {
773 RCP<const FactoryManagerBase> fman = (RCP<const FactoryManagerBase>)mueluFactory->GetFactoryManager(lvl);
774 systemManagers.push_back(fman);
775 for (auto s : keepItems) {
776 try {
777 const RCP<const FactoryBase> fact = fman->GetFactory(s); // will throw if factory doesn't exist, ignore in that case
778 if (!fact.is_null()) {
779 FactoryBase* factPtr = (FactoryBase*)fact.get();
780 // Add keep flag to level
781 H->GetLevel(lvl)->Keep(s, factPtr);
782 }
783 } catch (exception& e) {
784 }
785 }
786 }
787 mueluFactory->SetupHierarchy(*H);
788 operatorComplexity = H->GetOperatorComplexity();
789 prec = rcp(new TpetraOperator<Scalar, mm_LocalOrd, mm_GlobalOrd, mm_node_t>(H));
790 }
791
792 template <>
793 int TpetraSystem<double>::status() {
794 mexPrintf("**** Problem ID %d [MueLu_Tpetra] ****\n", id);
795 if (!A.is_null())
796 mexPrintf("Matrix: %dx%d w/ %d nnz\n", A->getGlobalNumRows(), A->getGlobalNumCols(), A->getGlobalNumEntries());
797 mexPrintf("Operator Complexity: %f\n", operatorComplexity);
798 if (!List.is_null()) {
799 mexPrintf("Parameter List:\n");
800 List->print();
801 }
802 mexPrintf("\n");
803 return IS_TRUE;
804 }
805
806 template <typename Scalar>
807 RCP<MueLu::Hierarchy<Scalar, mm_GlobalOrd, mm_LocalOrd, mm_node_t>> TpetraSystem<Scalar>::getHierarchy() {
808 RCP<MueLu::TpetraOperator<Scalar, mm_LocalOrd, mm_GlobalOrd, mm_node_t>> mueluOp = rcp_static_cast<MueLu::TpetraOperator<Scalar, mm_LocalOrd, mm_GlobalOrd, mm_node_t>, Tpetra::Operator<Scalar, mm_LocalOrd, mm_GlobalOrd, mm_node_t>>(prec);
809 if (mueluOp.is_null())
810 throw runtime_error("Tpetra precondition operator was null.");
811 RCP<MueLu::Hierarchy<Scalar, mm_GlobalOrd, mm_LocalOrd, mm_node_t>> hier = mueluOp->GetHierarchy();
812 if (hier.is_null())
813 throw runtime_error("Hierarchy from Tpetra problem was null.");
814 return hier;
815 }
816
817 // tpetra_complex_data_pack implementation
818
819 #ifdef HAVE_COMPLEX_SCALARS
820 template <>
821 TpetraSystem<complex_t>::TpetraSystem()
822 : MuemexSystem(TPETRA_COMPLEX) {}
823 template <>
824 TpetraSystem<complex_t>::~TpetraSystem() {}
825
826 template <>
827 int TpetraSystem<complex_t>::status() {
828 mexPrintf("**** Problem ID %d [MueLu_Tpetra (Complex Scalars)] ****\n", id);
829 if (!A.is_null())
830 mexPrintf("Matrix: %dx%d w/ %d nnz\n", A->getGlobalNumRows(), A->getGlobalNumCols(), A->getGlobalNumEntries());
831 mexPrintf("Operator Complexity: %f\n", operatorComplexity);
832 if (!List.is_null()) {
833 mexPrintf("Parameter List:\n");
834 List->print();
835 }
836 mexPrintf("\n");
837 return IS_TRUE;
838 }
839 #endif // HAVE_COMPLEX_SCALARS
840
841 // MuemexSystemList namespace implementation
842
843 void MuemexSystemList::clearAll() {
844 // When items are cleared, RCPs will auto-delete the datapacks
845 list.clear();
846 }
847
848 /* add - Adds an MuemexSystem to the list.
849 Parameters:
850 D - The MuemexSystem. [I]
851 Returns: problem id number of D
852 */
853 int MuemexSystemList::add(RCP<MuemexSystem> D) {
854 TEUCHOS_ASSERT(!D.is_null());
855 D->id = nextID;
856 nextID++;
857 list.push_back(D);
858 return D->id;
859 } /*end add*/
860
861 /* find - Finds problem by id
862 Parameters:
863 id - ID number [I]
864 Returns: pointer to MuemexSystem matching 'id', if found, NULL if not
865 found.
866 */
867 RCP<MuemexSystem> MuemexSystemList::find(int id) {
868 if (isInList(id)) {
869 for (auto problem : list) {
870 if (problem->id == id)
871 return problem;
872 }
873 }
874 RCP<MuemexSystem> notFound;
875 return notFound;
876 } /*end find*/
877
878 /* remove - Removes problem by id
879 Parameters:
880 id - ID number [I]
881 Returns: IS_TRUE if remove was succesful, IS_FALSE otherwise
882 */
883 int MuemexSystemList::remove(int id) {
884 int index = -1;
885 for (int i = 0; i < int(list.size()); i++) {
886 if (list[i]->id == id) {
887 index = i;
888 break;
889 }
890 }
891 if (index == -1) {
892 mexErrMsgTxt("Error: Tried to clean up a problem that doesn't exist.");
893 return IS_FALSE;
894 }
895 list.erase(list.begin() + index);
896 return IS_TRUE;
897 } /*end remove*/
898
899 /* size - Number of stored problems */
900 int MuemexSystemList::size() {
901 return list.size();
902 }
903
904 /* Returns the status of all members of the list
905 Returns IS_TRUE
906 */
907 int MuemexSystemList::status_all() {
908 // This prints all the existing problems in ascending order by ID
909 for (int i = 0; i < nextID; i++) {
910 for (auto problem : list) {
911 if (problem->id == i) {
912 problem->status();
913 break;
914 }
915 }
916 }
917 return IS_TRUE;
918 } /*end status_all */
919
920 bool MuemexSystemList::isInList(int id) {
921 bool rv = false;
922 for (auto problem : list) {
923 if (problem->id == id) {
924 rv = true;
925 break;
926 }
927 }
928 return rv;
929 }
930
931 /* sanity_check - sanity checks the first couple of arguements and returns the
932 program mode.
933 Parameters:
934 nrhs - Number of program inputs [I]
935 prhs - The problem inputs [I]
936 Return value: Which mode to run the program in.
937 */
938
939 MODE_TYPE sanity_check(int nrhs, const mxArray* prhs[]) {
940 MODE_TYPE rv = MODE_ERROR;
941 /* Check for mode */
942 if (nrhs == 0)
943 mexErrMsgTxt("Error: muelu() expects at least one argument\n");
944 /* Pull mode data from 1st Input */
945 MODE_TYPE mode = (MODE_TYPE)loadDataFromMatlab<int>(prhs[0]);
946 switch (mode) {
947 case MODE_SETUP:
948 if (nrhs > 1 && mxIsSparse(prhs[1])) {
949 if (nrhs > 3 && mxIsSparse(prhs[2]) && mxIsSparse(prhs[3]))
950 rv = MODE_ERROR;
951 else
952 rv = MODE_SETUP;
953 } else {
954 mexErrMsgTxt("Error: Invalid input for setup\n");
955 }
956 break;
957 case MODE_SOLVE:
958 // problem ID and matrix or rhs must be numeric
959 if (nrhs >= 2 && mxIsNumeric(prhs[1]) && mxIsNumeric(prhs[2]))
960 rv = MODE_SOLVE;
961 else
962 mexErrMsgTxt("Error: Invalid input for solve\n");
963 break;
964 case MODE_APPLY:
965 // problem ID and RHS must be numeric
966 if (nrhs == 3 && mxIsNumeric(prhs[1]) && mxIsNumeric(prhs[2]))
967 rv = MODE_APPLY;
968 else
969 mexErrMsgTxt("Error: Invalid input for apply\n");
970 break;
971 case MODE_CLEANUP:
972 if (nrhs == 1 || nrhs == 2)
973 rv = MODE_CLEANUP;
974 else
975 mexErrMsgTxt("Error: Extraneous args for cleanup\n");
976 break;
977 case MODE_STATUS:
978 if (nrhs == 1 || nrhs == 2)
979 rv = MODE_STATUS;
980 else
981 mexErrMsgTxt("Error: Extraneous args for status\n");
982 break;
983 case MODE_AGGREGATE:
984 if (nrhs > 1 && mxIsSparse(prhs[1]))
985 // Uncomment the next line and remove one after when implementing aggregate mode
986 // rv = MODE_AGGREGATE;
987 rv = MODE_ERROR;
988 else
989 mexErrMsgTxt("Error: Invalid input for aggregate\n");
990 break;
991 case MODE_GET:
992 if (nrhs < 4 || nrhs > 5)
993 mexErrMsgTxt("Error: Wrong number of args for get\n");
994 else
995 rv = MODE_GET;
996 break;
997 default:
998 printf("Mode number = %d\n", (int)mode);
999 mexErrMsgTxt("Error: Invalid input mode\n");
1000 };
1001 return rv;
1002 }
1003 /*end sanity_check*/
1004
1005 void csc_print(int n, int* rowind, int* colptr, double* vals) {
1006 int i, j;
1007 for (i = 0; i < n; i++) {
1008 for (j = colptr[i]; j < colptr[i + 1]; j++) {
1009 mexPrintf("%d %d %20.16e\n", rowind[j], i, vals[j]);
1010 }
1011 }
1012 }
1013
1014 void parse_list_item(RCP<ParameterList> List, char* option_name, const mxArray* prhs) {
1015 // List shouldn't be NULL but if it is, initialize here
1016 if (List.is_null()) {
1017 List = rcp(new ParameterList);
1018 }
1019 mxClassID cid;
1020 int i, M, N, *opt_int;
1021 char* opt_char;
1022 double* opt_float;
1023 string opt_str;
1024 RCP<ParameterList> sublist = rcp(new ParameterList);
1025 mxArray *cell1, *cell2;
1026 /* Pull relevant info the the option value */
1027 cid = mxGetClassID(prhs);
1028 M = mxGetM(prhs);
1029 N = mxGetN(prhs);
1030 /* Add to the Teuchos list */
1031
1032 // extract potential typeStr. The code is based on the assumption that
1033 // the typedefinition is the first word. It is necessary to distinguish
1034 // between "map" type (representing a Xpetra::Map) and multivector (default)
1035 vector<string> typestring = tokenizeList(option_name);
1036 std::transform(typestring[0].begin(), typestring[0].end(), typestring[0].begin(), ::tolower);
1037 size_t WordStart = typestring[0].find_first_not_of(' ');
1038 size_t WordEnd = typestring[0].find(' ', WordStart);
1039 std::string typeStr = typestring[0].substr(WordStart, WordEnd - WordStart);
1040
1041 /////
1042
1043 switch (cid) {
1044 case mxCHAR_CLASS:
1045 // String
1046 opt_char = mxArrayToString(prhs);
1047 opt_str = opt_char;
1048 List->set(option_name, opt_str);
1049 if (strcmp(option_name, MUEMEX_INTERFACE) == 0) {
1050 #ifdef HAVE_MUELU_EPETRA
1051 if (strcmp(opt_str.c_str(), "epetra") == 0)
1052 useEpetra = true;
1053 else
1054 #endif
1055 if (strcmp(opt_str.c_str(), "tpetra") == 0)
1056 useEpetra = false;
1057 }
1058 mxFree(opt_char);
1059 break;
1060 case mxDOUBLE_CLASS:
1061 case mxSINGLE_CLASS:
1062 // Single or double, real or complex
1063 if (mxIsComplex(prhs)) {
1064 #ifndef HAVE_COMPLEX_SCALARS
1065 opt_float = mxGetPr(prhs);
1066 double* opt_float_imag = mxGetPi(prhs);
1067 // assuming user wants std::complex<double> here...
1068 if (M == 1 && N == 1) {
1069 List->set(option_name, complex_t(*opt_float, *opt_float_imag));
1070 } else if (M == 0 || N == 0) {
1071 List->set(option_name, (complex_t*)NULL);
1072 } else {
1073 if (mxIsSparse(prhs))
1074 List->set(option_name, loadDataFromMatlab<RCP<Xpetra_Matrix_complex>>(prhs));
1075 else
1076 List->set(option_name, loadDataFromMatlab<RCP<Xpetra_MultiVector_complex>>(prhs));
1077 }
1078 #else
1079 std::cerr << "Error: cannot load argument \"" << option_name << "\" because complex is not instantiated in this build.\n";
1080 throw std::invalid_argument("Complex not supported");
1081 #endif
1082 } else {
1083 opt_float = mxGetPr(prhs);
1084 if (M == 1 && N == 1 && MMISINT(opt_float[0])) {
1085 List->set(option_name, (int)opt_float[0]);
1086 } else if (M == 1 && N == 1) {
1087 List->set(option_name, opt_float[0]);
1088 } else if (M == 0 || N == 0) {
1089 List->set(option_name, (double*)NULL);
1090 } else {
1091 if (mxIsSparse(prhs))
1092 List->set(option_name, loadDataFromMatlab<RCP<Xpetra_Matrix_double>>(prhs));
1093 else {
1094 if (typeStr == "map") // data stored as Xpetra::Map type
1095 List->set(option_name, loadDataFromMatlab<RCP<Xpetra_map>>(prhs));
1096 else // data stored as MultiVector
1097 List->set(option_name, loadDataFromMatlab<RCP<Xpetra_MultiVector_double>>(prhs));
1098 }
1099 }
1100 }
1101 break;
1102 case mxLOGICAL_CLASS:
1103 // Bool
1104 if (M == 1 && N == 1)
1105 List->set(option_name, mxIsLogicalScalarTrue(prhs));
1106 else
1107 List->set(option_name, mxGetLogicals(prhs));
1108 // NTS: The else probably doesn't work.
1109 break;
1110 case mxINT8_CLASS:
1111 case mxUINT8_CLASS:
1112 case mxINT16_CLASS:
1113 case mxUINT16_CLASS:
1114 case mxINT32_CLASS:
1115 case mxUINT32_CLASS:
1116 // Integer
1117 opt_int = (int*)mxGetData(prhs);
1118 if (M == 1 && N == 1)
1119 List->set(option_name, *opt_int);
1120 #ifdef HAVE_MUELU_INTREPID2
1121 else if (strcmp(option_name, "pcoarsen: element to node map") == 0)
1122 List->set(option_name, loadDataFromMatlab<RCP<FieldContainer_ordinal>>(prhs));
1123 #endif
1124 else
1125 List->set(option_name, loadDataFromMatlab<RCP<Xpetra_ordinal_vector>>(prhs));
1126 break;
1127 // NTS: 64-bit ints will break on a 32-bit machine. We
1128 // should probably detect machine type, or somthing, but that would
1129 // involve a non-trivial quantity of autoconf kung fu.
1130 case mxCELL_CLASS:
1131 // Interpret a cell list as a nested teuchos list.
1132 // NTS: Assuming that it's a 1D row ordered array
1133 for (i = 0; i < N; i += 2) {
1134 cell1 = mxGetCell(prhs, i);
1135 cell2 = mxGetCell(prhs, i + 1);
1136 if (!mxIsChar(cell1))
1137 mexErrMsgTxt("Error: Input options are not in ['parameter',value] format!\n");
1138 opt_char = mxArrayToString(cell1);
1139 parse_list_item(sublist, opt_char, cell2);
1140 List->set(option_name, *sublist);
1141 mxFree(opt_char);
1142 }
1143 break;
1144 case mxINT64_CLASS:
1145 case mxUINT64_CLASS:
1146 case mxFUNCTION_CLASS:
1147 case mxUNKNOWN_CLASS:
1148 case mxSTRUCT_CLASS:
1149 // Currently Graph and Aggregates are stored as structures
1150 if (isValidMatlabAggregates(prhs)) {
1151 try {
1152 List->set(option_name, loadDataFromMatlab<RCP<MAggregates>>(prhs));
1153 break;
1154 } catch (exception& e) {
1155 cout << e.what();
1156 throw runtime_error("Parsing aggregates in parameter list failed.");
1157 }
1158 } else if (isValidMatlabGraph(prhs)) {
1159 try {
1160 List->set(option_name, loadDataFromMatlab<RCP<MGraph>>(prhs));
1161 break;
1162 } catch (exception& e) {
1163 cout << e.what();
1164 throw runtime_error("Parsing graph in parameter list failed.");
1165 }
1166 }
1167 default:
1168 mexPrintf("Error parsing input option: %s [type=%d]\n", option_name, cid);
1169 mexErrMsgTxt("Error: An input option is invalid!\n");
1170 };
1171 }
1172
1173 /**************************************************************/
1174 /**************************************************************/
1175 /**************************************************************/
1176 /* build_teuchos_list - takes the inputs (barring the solver mode and
1177 matrix/rhs) and turns them into a Teuchos list.
1178 Parameters:
1179 nrhs - Number of program inputs [I]
1180 prhs - The problem inputs [I]
1181 Return value: Teuchos list containing all parameters passed in by the user.
1182 */
1183 RCP<ParameterList> build_teuchos_list(int nrhs, const mxArray* prhs[]) {
1184 RCP<ParameterList> TPL = rcp(new ParameterList);
1185 char* option_name;
1186 for (int i = 0; i < nrhs; i += 2) {
1187 if (i == nrhs - 1 || !mxIsChar(prhs[i]))
1188 mexErrMsgTxt("Error: Input options are not in ['parameter',value] format!\n");
1189 /* What option are we setting? */
1190 option_name = mxArrayToString(prhs[i]);
1191 /* Parse */
1192 parse_list_item(TPL, option_name, prhs[i + 1]);
1193 /* Free memory */
1194 mxFree(option_name);
1195 }
1196 TPL->print(cout);
1197 return TPL;
1198 }
1199 /*end build_teuchos_list*/
1200
1201 } // namespace MueLu
1202
1203 using namespace MueLu; //...but give mexFunction access to all MueLu members defined above
1204
1205 void mexFunction(int nlhs, mxArray* plhs[], int nrhs, const mxArray* prhs[]) {
1206 // Lazily initialize Tpetra
1207 if (!Tpetra::isInitialized()) {
1208 int argc = 0;
1209 char** argv = NULL;
1210 Tpetra::initialize(&argc, &argv);
1211 }
1212 double* id;
1213 int rv;
1214 // Arrays representing vectors
1215 string intf;
1216 RCP<ParameterList> List;
1217 MODE_TYPE mode;
1218 RCP<MuemexSystem> D;
1219 /* Sanity Check Input */
1220 mode = sanity_check(nrhs, prhs);
1221
1222 #define FORCE_FACTORIES_TO_COMPILE
1223 #ifdef FORCE_FACTORIES_TO_COMPILE
1224 {
1225 // debug
1226 MueLu::TwoLevelMatlabFactory<double, int, int> f1;
1227 MueLu::SingleLevelMatlabFactory<double, int, int> f2;
1228 }
1229 #endif
1230
1231 switch (mode) {
1232 case MODE_SETUP: {
1233 try {
1234 double oc = 0;
1235 bool haveCoords = false;
1236 if (nrhs == 2)
1237 List = rcp(new ParameterList);
1238 else if (nrhs == 3) {
1239 if (!mxIsNumeric(prhs[2]) || mxIsSparse(prhs[2]) || mxIsComplex(prhs[2]))
1240 throw runtime_error("Expected real-valued, dense Coordinates array as the third muelu argument");
1241 else {
1242 haveCoords = true;
1243 List = rcp(new ParameterList);
1244 }
1245 } else {
1246 if (mxIsNumeric(prhs[2]) && !mxIsSparse(prhs[2]) && !mxIsComplex(prhs[2])) {
1247 List = build_teuchos_list(nrhs - 3, &(prhs[3]));
1248 haveCoords = true;
1249 } else {
1250 // assume that the parameters start at third argument if it doesn't seem like coords are there
1251 List = build_teuchos_list(nrhs - 2, &(prhs[2]));
1252 }
1253 }
1254 // Combine xml and easy parameter lists
1255 if (List->isParameter("xml parameter file")) {
1256 RCP<ParameterList> xmlParams = Teuchos::getParametersFromXmlFile(List->get<string>("xml parameter file"));
1257 List->remove("xml parameter file");
1258 List->setParametersNotAlreadySet(*xmlParams);
1259 }
1260 if (mxIsComplex(prhs[1])) {
1261 // Abort if input is complex but complex isn't supported
1262 #ifndef HAVE_COMPLEX_SCALARS
1263 mexPrintf("Error: Complex scalars unsupported by this build of Trilinos.\n");
1264 throw runtime_error("Complex scalars not supported.");
1265 #endif
1266 }
1267 intf = List->get(MUEMEX_INTERFACE, "tpetra");
1268 List->remove(MUEMEX_INTERFACE); // no longer need this parameter
1269 #ifdef HAVE_MUELU_EPETRA
1270 if (intf == "epetra") {
1271 if (mxIsComplex(prhs[1])) {
1272 mexPrintf("Error: Attempting to use complex-valued matrix with Epetra, which is unsupported.\n");
1273 mexPrintf("Use Tpetra with complex matrices instead.\n");
1274 throw runtime_error("Tried to use complex matrix with Epetra");
1275 }
1276 RCP<EpetraSystem> dp = rcp(new EpetraSystem());
1277 dp->List = List;
1278 dp->setup(prhs[1], haveCoords, haveCoords ? prhs[2] : (mxArray*)NULL);
1279 oc = dp->operatorComplexity;
1280 D = rcp_implicit_cast<MuemexSystem>(dp);
1281 } else
1282 #endif
1283 if (intf == "tpetra") {
1284 // infer scalar type from prhs (can be double or complex<double>)
1285 if (mxIsComplex(prhs[1])) {
1286 #ifdef HAVE_COMPLEX_SCALARS
1287 RCP<TpetraSystem<complex_t>> dp = rcp(new TpetraSystem<complex_t>());
1288 dp->List = List;
1289 dp->setup(prhs[1], haveCoords, haveCoords ? prhs[2] : (mxArray*)NULL);
1290 oc = dp->operatorComplexity;
1291 D = rcp_implicit_cast<MuemexSystem>(dp);
1292 #else
1293 throw runtime_error("Complex scalars not supported.");
1294 #endif
1295 } else {
1296 RCP<TpetraSystem<double>> dp = rcp(new TpetraSystem<double>());
1297 dp->List = List;
1298 dp->setup(prhs[1], haveCoords, haveCoords ? prhs[2] : (mxArray*)NULL);
1299 oc = dp->operatorComplexity;
1300 D = rcp_implicit_cast<MuemexSystem>(dp);
1301 }
1302 }
1303 rv = MuemexSystemList::add(D);
1304 mexPrintf("Set up problem #%d\n", rv);
1305 if (nlhs > 0) {
1306 plhs[0] = mxCreateNumericMatrix(1, 1, mxINT32_CLASS, mxREAL);
1307 *((int*)mxGetData(plhs[0])) = rv;
1308 // output OC also
1309 if (nlhs > 1)
1310 plhs[1] = mxCreateDoubleScalar(oc);
1311 }
1312 mexLock();
1313 } catch (exception& e) {
1314 mexPrintf("An error occurred during setup routine:\n");
1315 cout << e.what() << endl;
1316 if (nlhs > 0) {
1317 plhs[0] = mxCreateNumericMatrix(1, 1, mxINT32_CLASS, mxREAL);
1318 *((int*)mxGetData(plhs[0])) = -1;
1319 }
1320 if (nlhs > 1)
1321 plhs[1] = mxCreateDoubleScalar(0);
1322 }
1323 break;
1324 }
1325 case MODE_SOLVE: {
1326 int iters;
1327 try {
1328 bool reuse;
1329 // MODE_SOLVE, probID, matrix, bVec, params OR
1330 // MODE_SOLVE, probID, bVec, params
1331 // prhs[0] holds the MODE_SOLVE enum value
1332 // if reusing, either nrhs == 3 or prhs[3] is not a string
1333 //(because bVec can't be a string)
1334 if (MuemexSystemList::size() == 0)
1335 throw runtime_error("No linear systems are set up.");
1336 if (nrhs == 3 || (nrhs > 3 && mxGetClassID(prhs[3]) == mxCHAR_CLASS)) {
1337 // No matrix supplied as argument, use one from setup
1338 if (nrhs > 3)
1339 List = build_teuchos_list(nrhs - 3, &prhs[3]);
1340 else
1341 List = rcp(new ParameterList);
1342 reuse = true;
1343 } else {
1344 if (nrhs > 4)
1345 List = build_teuchos_list(nrhs - 4, &prhs[4]);
1346 else
1347 List = rcp(new ParameterList);
1348 reuse = false;
1349 }
1350 if (List->isType<string>("Output Style")) {
1351 int type = strToOutputStyle(List->get("Output Style", "Belos::Brief").c_str());
1352 List->remove("Output Style");
1353 // Reset the ParameterList entry to be of type int instead of string
1354 List->set("Output Style", type);
1355 }
1356 // Convert Belos msg type string to int in List
1357 // Note: if the parameter value is already an int, don't touch it.
1358 if (List->isType<string>("Verbosity")) {
1359 // Errors + Warnings is already the default Belos verbosity setting
1360 int verb = getBelosVerbosity(List->get("Verbosity", "Belos::Errors + Belos::Warnings").c_str());
1361 List->remove("Verbosity");
1362 List->set("Verbosity", verb);
1363 }
1364 int probID = loadDataFromMatlab<int>(prhs[1]);
1365 RCP<MuemexSystem> dp = MuemexSystemList::find(probID);
1366 if (dp.is_null())
1367 throw runtime_error("Problem handle not allocated.");
1368 // get pointer to MATLAB array that will be "B" or "rhs" multivector
1369 const mxArray* rhs = reuse ? prhs[2] : prhs[3];
1370 switch (dp->type) {
1371 #ifdef HAVE_MUELU_EPETRA
1372 case EPETRA: {
1373 RCP<EpetraSystem> esys = rcp_static_cast<EpetraSystem, MuemexSystem>(dp);
1374 RCP<Epetra_CrsMatrix> matrix;
1375 if (reuse)
1376 matrix = esys->GetMatrix();
1377 else
1378 matrix = loadDataFromMatlab<RCP<Epetra_CrsMatrix>>(prhs[2]);
1379 plhs[0] = esys->solve(List, matrix, rhs, iters);
1380 break;
1381 }
1382 #endif
1383 case TPETRA: {
1384 RCP<TpetraSystem<double>> tsys = rcp_static_cast<TpetraSystem<double>, MuemexSystem>(dp);
1385 RCP<Tpetra_CrsMatrix_double> matrix;
1386 if (reuse)
1387 matrix = tsys->GetMatrix();
1388 else
1389 matrix = loadDataFromMatlab<RCP<Tpetra_CrsMatrix_double>>(prhs[2]);
1390 plhs[0] = tsys->solve(List, matrix, rhs, iters);
1391 break;
1392 }
1393 case TPETRA_COMPLEX: {
1394 #ifdef HAVE_COMPLEX_SCALARS
1395 RCP<TpetraSystem<complex_t>> tsys = rcp_static_cast<TpetraSystem<complex_t>, MuemexSystem>(dp);
1396 RCP<Tpetra_CrsMatrix_complex> matrix;
1397 if (reuse)
1398 matrix = tsys->GetMatrix();
1399 else
1400 matrix = loadDataFromMatlab<RCP<Tpetra_CrsMatrix_complex>>(prhs[2]);
1401 plhs[0] = tsys->solve(List, matrix, rhs, iters);
1402 break;
1403 #else
1404 std::cerr << "Cannot solve complex-valued system because complex is not enabled in this build.\n";
1405 throw std::invalid_argument("Complex not supported");
1406 #endif
1407 }
1408 }
1409 if (nlhs > 1)
1410 plhs[1] = saveDataToMatlab<int>(iters);
1411 } catch (exception& e) {
1412 mexPrintf("An error occurred during the solve routine:\n");
1413 cout << e.what() << endl;
1414 }
1415 break;
1416 }
1417 case MODE_APPLY: {
1418 try {
1419 // MODE_APPLY, probID, rhsVec
1420 // prhs[0] holds the MODE_APPLY enum value
1421 if (MuemexSystemList::size() == 0)
1422 throw runtime_error("No linear systems are set up.");
1423 int probID = loadDataFromMatlab<int>(prhs[1]);
1424 RCP<MuemexSystem> dp = MuemexSystemList::find(probID);
1425 if (dp.is_null())
1426 throw runtime_error("Problem handle not allocated.");
1427 // get pointer to MATLAB array that will be "B" or "rhs" multivector
1428 const mxArray* rhs = prhs[2];
1429 switch (dp->type) {
1430 #ifdef HAVE_MUELU_EPETRA
1431 case EPETRA: {
1432 RCP<EpetraSystem> esys = rcp_static_cast<EpetraSystem, MuemexSystem>(dp);
1433 plhs[0] = esys->apply(rhs);
1434 break;
1435 }
1436 #endif
1437 case TPETRA: {
1438 RCP<TpetraSystem<double>> tsys = rcp_static_cast<TpetraSystem<double>, MuemexSystem>(dp);
1439 plhs[0] = tsys->apply(rhs);
1440 break;
1441 }
1442 case TPETRA_COMPLEX: {
1443 #ifdef HAVE_COMPLEX_SCALARS
1444 RCP<TpetraSystem<complex_t>> tsys = rcp_static_cast<TpetraSystem<complex_t>, MuemexSystem>(dp);
1445 plhs[0] = tsys->apply(rhs);
1446 break;
1447 #else
1448 std::cerr << "Cannot solve complex-valued system because complex is not enabled in this build.\n";
1449 throw std::invalid_argument("Complex not supported");
1450 #endif
1451 }
1452 }
1453 } catch (exception& e) {
1454 mexPrintf("An error occurred during the apply routine:\n");
1455 cout << e.what() << endl;
1456 }
1457 break;
1458 }
1459 case MODE_CLEANUP: {
1460 try {
1461 mexPrintf("MueMex in cleanup mode.\n");
1462 if (MuemexSystemList::size() > 0 && nrhs == 1) {
1463 /* Cleanup all problems */
1464 for (int i = 0; i < MuemexSystemList::size(); i++)
1465 mexUnlock();
1466 MuemexSystemList::clearAll();
1467 rv = 1;
1468 } else if (MuemexSystemList::size() > 0 && nrhs == 2) {
1469 /* Cleanup one problem */
1470 int probID = loadDataFromMatlab<int>(prhs[1]);
1471 mexPrintf("Cleaning up problem #%d\n", probID);
1472 rv = MuemexSystemList::remove(probID);
1473 if (rv)
1474 mexUnlock();
1475 } /*end elseif*/
1476 else {
1477 rv = 0;
1478 }
1479 /* Set return value */
1480 plhs[0] = mxCreateNumericMatrix(1, 1, mxINT32_CLASS, mxREAL);
1481 id = (double*)mxGetData(plhs[0]);
1482 *id = double(rv);
1483 } catch (exception& e) {
1484 mexPrintf("An error occurred during the cleanup routine:\n");
1485 cout << e.what() << endl;
1486 if (nlhs > 0) {
1487 plhs[0] = mxCreateNumericMatrix(1, 1, mxINT32_CLASS, mxREAL);
1488 *(int*)mxGetData(plhs[0]) = 0;
1489 }
1490 }
1491 break;
1492 }
1493 case MODE_STATUS: {
1494 try {
1495 // mexPrintf("MueMex in status checking mode.\n");
1496 if (MuemexSystemList::size() > 0 && nrhs == 1) {
1497 /* Status check on all problems */
1498 rv = MuemexSystemList::status_all();
1499 } /*end if*/
1500 else if (MuemexSystemList::size() > 0 && nrhs == 2) {
1501 /* Status check one problem */
1502 int probID = loadDataFromMatlab<int>(prhs[1]);
1503 D = MuemexSystemList::find(probID);
1504 if (D.is_null())
1505 throw runtime_error("Error: Problem handle not allocated.\n");
1506 rv = D->status();
1507 } /*end elseif*/
1508 else
1509 mexPrintf("No problems set up.\n");
1510 if (nlhs > 0) {
1511 int outVal = 0;
1512 plhs[0] = saveDataToMatlab<int>(outVal);
1513 }
1514 } catch (exception& e) {
1515 mexPrintf("An error occurred during the status routine:\n");
1516 cout << e.what() << endl;
1517 int outVal = -1;
1518 if (nlhs > 0)
1519 plhs[0] = saveDataToMatlab<int>(outVal);
1520 }
1521 break;
1522 }
1523 case MODE_GET: {
1524 try {
1525 int probID = loadDataFromMatlab<int>(prhs[1]);
1526 int levelID = loadDataFromMatlab<int>(prhs[2]);
1527 char* dataName = mxArrayToString(prhs[3]);
1528 MuemexType outputType = INT;
1529 RCP<MuemexSystem> dp = MuemexSystemList::find(probID);
1530 if (dp.is_null()) {
1531 throw runtime_error("Problem handle not allocated.");
1532 }
1533
1534 // See if typeHint was given
1535 char* paramTypeName = NULL;
1536 if (nrhs > 4) {
1537 paramTypeName = mxArrayToString(prhs[4]);
1538 mexPrintf("paramTypeName %s", paramTypeName);
1539 }
1540 bool complexFlag = dp->type == TPETRA_COMPLEX;
1541 // std::cout << "before strToDataType dataName=" << dataName << " paramTypeName " << paramTypeName << std::endl;
1542
1543 outputType = strToDataType(dataName, paramTypeName, complexFlag);
1544 plhs[0] = dp->getHierarchyData(string(dataName), outputType, levelID);
1545 } catch (exception& e) {
1546 mexPrintf("An error occurred during the get routine:\n");
1547 cout << e.what() << endl;
1548 plhs[0] = mxCreateDoubleScalar(0);
1549 }
1550 break;
1551 }
1552 case MODE_ERROR:
1553 mexPrintf("MueMex error.");
1554 break;
1555 case MODE_AGGREGATE:
1556 default:
1557 mexPrintf("Mode not supported yet.");
1558 }
1559 }