| 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 MUEMEX_H |
| 11 |
#define MUEMEX_H |
| 12 |
|
| 13 |
#include <cstdio> |
| 14 |
#include <cstring> |
| 15 |
#include <string> |
| 16 |
#include <vector> |
| 17 |
#include <complex> |
| 18 |
#include <stdexcept> |
| 19 |
|
| 20 |
#include "Teuchos_ParameterList.hpp" |
| 21 |
#include "Teuchos_RCP.hpp" |
| 22 |
#include "MueLu_config.hpp" |
| 23 |
#include "MueLu.hpp" |
| 24 |
#ifdef HAVE_MUELU_EPETRA |
| 25 |
#include "MueLu_EpetraOperator.hpp" |
| 26 |
#endif |
| 27 |
#include "MueLu_TpetraOperator.hpp" |
| 28 |
#include "MueLu_Hierarchy.hpp" |
| 29 |
#include "MueLu_MatlabUtils.hpp" |
| 30 |
#ifdef HAVE_MUELU_EPETRA |
| 31 |
#include "MueLu_CreateEpetraPreconditioner.hpp" |
| 32 |
#endif |
| 33 |
#include "MueLu_CreateTpetraPreconditioner.hpp" |
| 34 |
#ifdef HAVE_MUELU_EPETRA |
| 35 |
#include "Epetra_SerialComm.h" |
| 36 |
#include "Epetra_Map.h" |
| 37 |
#include "Epetra_MultiVector.h" |
| 38 |
#include "Epetra_CrsMatrix.h" |
| 39 |
#include "Epetra_LinearProblem.h" |
| 40 |
#endif |
| 41 |
#include "Tpetra_CrsMatrix.hpp" |
| 42 |
#ifdef HAVE_MUELU_EPETRA |
| 43 |
#include "Xpetra_EpetraCrsMatrix.hpp" |
| 44 |
#endif |
| 45 |
#include "BelosSolverFactory.hpp" |
| 46 |
#ifdef HAVE_MUELU_EPETRA |
| 47 |
#include "BelosEpetraAdapter.hpp" |
| 48 |
#endif |
| 49 |
#include "BelosTpetraAdapter.hpp" |
| 50 |
#include "BelosPseudoBlockGmresSolMgr.hpp" |
| 51 |
#include "BelosBlockGmresSolMgr.hpp" |
| 52 |
#include "BelosMueLuAdapter.hpp" |
| 53 |
#include "MueLu_MatlabUtils.hpp" |
| 54 |
|
| 55 |
#include "mex.h" |
| 56 |
|
| 57 |
#ifdef HAVE_TPETRA_INST_COMPLEX_DOUBLE |
| 58 |
#define HAVE_COMPLEX_SCALARS |
| 59 |
#endif |
| 60 |
|
| 61 |
namespace MueLu |
| 62 |
{ |
| 63 |
|
| 64 |
typedef enum |
| 65 |
{ |
| 66 |
#ifdef HAVE_MUELU_EPETRA |
| 67 |
EPETRA, |
| 68 |
#endif |
| 69 |
TPETRA, |
| 70 |
TPETRA_COMPLEX |
| 71 |
} DataPackType; |
| 72 |
|
| 73 |
//Mode value passed to MATLAB muemex function as 1st arg (int) |
| 74 |
typedef enum |
| 75 |
{ |
| 76 |
MODE_SETUP, //0 |
| 77 |
MODE_SOLVE, //1 |
| 78 |
MODE_CLEANUP, //2 |
| 79 |
MODE_STATUS, //3 |
| 80 |
MODE_AGGREGATE, //4 |
| 81 |
MODE_GET, //5 |
| 82 |
MODE_SET, //6 |
| 83 |
MODE_ERROR, //7 |
| 84 |
MODE_APPLY //8 |
| 85 |
} MODE_TYPE; |
| 86 |
|
| 87 |
/* Note: MuemexSystem is declared friend in MueLu::Hierarchy and MueLu::FactoryManager. |
| 88 |
This gives access to the private method Hierarchy::GetFactoryManager, which allows |
| 89 |
muelu('get', ...) to retrieve nonstandard "kept" items like Nullspace and Aggregates. |
| 90 |
*/ |
| 91 |
class MuemexSystem |
| 92 |
{ |
| 93 |
public: |
| 94 |
MuemexSystem(DataPackType type); //type is one of EPETRA, TPETRA or TPETRA_COMPLEX |
| 95 |
virtual ~MuemexSystem() = 0; |
| 96 |
virtual int status() = 0; |
| 97 |
virtual int setup(const mxArray* matlabA, bool haveCoords , const mxArray* matlabCoords) = 0; |
| 98 |
int id; |
| 99 |
Teuchos::RCP<Teuchos::ParameterList> List; |
| 100 |
DataPackType type; |
| 101 |
mxArray* getHierarchyData(std::string dataName, MuemexType dataType, int levelID); //Works for all dp types |
| 102 |
}; |
| 103 |
|
| 104 |
#ifdef HAVE_MUELU_EPETRA |
| 105 |
class EpetraSystem : public MuemexSystem |
| 106 |
{ |
| 107 |
public: |
| 108 |
EpetraSystem(); |
| 109 |
~EpetraSystem(); |
| 110 |
int setup(const mxArray* matlabA, bool haveCoords = false, const mxArray* matlabCoords = NULL); |
| 111 |
int status(); |
| 112 |
mxArray* solve(Teuchos::RCP<Teuchos::ParameterList> params, Teuchos::RCP<Epetra_CrsMatrix> matrix, const mxArray* rhs, int &iters); |
| 113 |
mxArray* apply(const mxArray* rhs); |
| 114 |
Teuchos::RCP<Epetra_CrsMatrix> GetMatrix() |
| 115 |
{ |
| 116 |
return A; |
| 117 |
} |
| 118 |
Teuchos::RCP<Epetra_Operator> GetPrec() |
| 119 |
{ |
| 120 |
return prec; |
| 121 |
} |
| 122 |
int NumGlobalRows() |
| 123 |
{ |
| 124 |
return A->NumGlobalRows(); |
| 125 |
} |
| 126 |
int NumMyCols() |
| 127 |
{ |
| 128 |
return A->NumGlobalCols(); |
| 129 |
} |
| 130 |
double operatorComplexity; |
| 131 |
Teuchos::RCP<Hierarchy_double> getHierarchy(); |
| 132 |
private: |
| 133 |
Teuchos::RCP<Epetra_CrsMatrix> A; |
| 134 |
Teuchos::RCP<Epetra_Operator> prec; |
| 135 |
}; |
| 136 |
#endif |
| 137 |
|
| 138 |
//Scalar can be double or std::complex<double> (complex_t) |
| 139 |
//Note: DataPackType is either TPETRA or TPETRA_COMPLEX |
| 140 |
|
| 141 |
template<typename Scalar> |
| 142 |
class TpetraSystem : public MuemexSystem |
| 143 |
{ |
| 144 |
public: |
| 145 |
TpetraSystem(); |
| 146 |
~TpetraSystem(); |
| 147 |
typedef Tpetra::CrsMatrix<Scalar, mm_LocalOrd, mm_GlobalOrd, mm_node_t> TMatrix; |
| 148 |
typedef Tpetra::Operator<Scalar, mm_LocalOrd, mm_GlobalOrd, mm_node_t> TOperator; |
| 149 |
int setup(const mxArray* matlabA, bool haveCoords = false, const mxArray* matlabCoords = NULL); |
| 150 |
void normalSetup(const mxArray* matlabA, bool haveCoords = false, const mxArray* matlabCoords = NULL); |
| 151 |
void customSetup(const mxArray* matlabA, bool haveCoords = false, const mxArray* matlabCoords = NULL); |
| 152 |
int status(); |
| 153 |
mxArray* solve(Teuchos::RCP<Teuchos::ParameterList> params, Teuchos::RCP<TMatrix> matrix, const mxArray* rhs, int &iters); |
| 154 |
mxArray* apply(const mxArray* rhs); |
| 155 |
//note: I typedef'd mm_node_t at the top of this file as the Kokkos default type |
| 156 |
Teuchos::RCP<TMatrix> GetMatrix() |
| 157 |
{ |
| 158 |
return A; |
| 159 |
} |
| 160 |
Teuchos::RCP<TOperator> GetPrec() |
| 161 |
{ |
| 162 |
return prec; |
| 163 |
} |
| 164 |
int NumMyRows() |
| 165 |
{ |
| 166 |
if(A.is_null()) |
| 167 |
return 0; |
| 168 |
else |
| 169 |
return A->getGlobalNumRows(); |
| 170 |
} |
| 171 |
int NumMyCols() |
| 172 |
{ |
| 173 |
if(A.is_null()) |
| 174 |
return 0; |
| 175 |
else |
| 176 |
return A->getGlobalNumCols(); |
| 177 |
} |
| 178 |
double operatorComplexity; |
| 179 |
Teuchos::RCP<MueLu::Hierarchy<Scalar, mm_LocalOrd, mm_GlobalOrd, mm_node_t>> getHierarchy(); |
| 180 |
private: |
| 181 |
Teuchos::RCP<TMatrix> A; |
| 182 |
Teuchos::RCP<TOperator> prec; |
| 183 |
public: |
| 184 |
bool keepAll; |
| 185 |
std::vector<Teuchos::RCP<const FactoryManagerBase>> systemManagers; |
| 186 |
}; |
| 187 |
|
| 188 |
namespace MuemexSystemList |
| 189 |
{ |
| 190 |
extern std::vector<Teuchos::RCP<MuemexSystem>> list; |
| 191 |
extern int nextID; |
| 192 |
int add(Teuchos::RCP<MuemexSystem> D); |
| 193 |
Teuchos::RCP<MuemexSystem> find(int id); |
| 194 |
int remove(int id); |
| 195 |
int size(); |
| 196 |
int status_all(); |
| 197 |
bool isInList(int id); |
| 198 |
void clearAll(); |
| 199 |
} |
| 200 |
|
| 201 |
// Get a hierarchy from a MuemexSystem |
| 202 |
template<typename Scalar> |
| 203 |
Teuchos::RCP<MueLu::Hierarchy<Scalar, mm_LocalOrd, mm_GlobalOrd, mm_node_t>> getDatapackHierarchy(MuemexSystem* dp); |
| 204 |
|
| 205 |
}// end namespace |
| 206 |
|
| 207 |
#endif //MUEMEX_H |