Galeri Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
cxx_main_tpetra.cpp
Go to the documentation of this file.
1 // @HEADER
2 // *****************************************************************************
3 // Galeri: Finite Element and Matrix Generation Package
4 //
5 // Copyright 2006 ETHZ/NTESS and the Galeri contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
10 #include "Galeri_XpetraMaps.hpp"
11 #include "Galeri_MatrixTraits.hpp"
12 #include "Galeri_XpetraMatrixTypes.hpp"
13 #include "Galeri_XpetraProblemFactory.hpp"
14 
15 #include "Teuchos_DefaultComm.hpp"
17 
18 #ifndef GALERI_TEST_USE_LONGLONG_GO
19 #define GO int
20 #else
21 #define GO long long
22 #endif
23 #define Scalar int
24 #define LO int
25 #define Node Tpetra::KokkosClassic::DefaultNode::DefaultNodeType
26 
27 using namespace Galeri;
28 
29 int main(int argc, char* argv[]) {
30  using Teuchos::RCP;
31  using Teuchos::rcp;
32 
33  typedef Tpetra::Map<LO, GO, Node> Tpetra_Map;
34  typedef Tpetra::CrsMatrix<Scalar, LO, GO, Node> Tpetra_CrsMatrix;
35  typedef Tpetra::MultiVector<Scalar, LO, GO, Node> Tpetra_MultiVector;
36  typedef Teuchos::ScalarTraits<Scalar> ScalarTraits;
37 
38 #ifdef HAVE_MPI
39  MPI_Init(&argc, &argv);
40 #endif
41 
42  // Create comm
43  RCP<const Teuchos::Comm<int>> comm = Teuchos::DefaultComm<int>::getComm();
44 
45  // Here we create the linear problem
46  //
47  // Matrix * LHS = RHS
48  //
49  // with Matrix arising from a 5-point formula discretization.
50 
51  std::string mapType = "Cartesian2D";
52  auto mapParameters = Teuchos::ParameterList("Tpetra::Map");
53  // dimension of the problem is nx x ny
54  mapParameters.set("nx", 10 * comm->getSize());
55  mapParameters.set("ny", 10);
56  // total number of processors is mx x my
57  mapParameters.set("mx", comm->getSize());
58  mapParameters.set("my", 1);
59 
60  auto out = Teuchos::getFancyOStream(Teuchos::rcpFromRef(std::cout));
61 
62  try {
63  // Creation of the map
64  auto map = RCP{Galeri::Xpetra::CreateMap<Scalar, GO, Tpetra_Map>(mapType, comm, mapParameters)};
65 
66  // Creation of linear problem
67  auto problem = Galeri::Xpetra::BuildProblem<Scalar, LO, GO, Tpetra_Map, Tpetra_CrsMatrix, Tpetra_MultiVector>("Laplace2D", map, mapParameters);
68 
69  // Build Matrix and MultiVectors
70  auto matrix = problem->BuildMatrix();
71  auto LHS = rcp(new Tpetra_MultiVector(matrix->getDomainMap(), 1));
72  auto RHS = rcp(new Tpetra_MultiVector(matrix->getRangeMap(), 1));
73  auto ExactSolution = rcp(new Tpetra_MultiVector(matrix->getDomainMap(), 1));
74 
75  ExactSolution->randomize(0, 100);
76  LHS->putScalar(ScalarTraits::zero());
77 
78  matrix->apply(*ExactSolution, *RHS);
79 
80  matrix->describe(*out, Teuchos::EVerbosityLevel::VERB_EXTREME);
81  LHS->describe(*out, Teuchos::EVerbosityLevel::VERB_EXTREME);
82  RHS->describe(*out, Teuchos::EVerbosityLevel::VERB_EXTREME);
83  ExactSolution->describe(*out, Teuchos::EVerbosityLevel::VERB_EXTREME);
84 
85  // at this point any LinearSolver can be used which understands the Tpetra objects. For example: Amesos2 or Ifpack2
86  } catch (Galeri::Exception& rhs) {
87  if (comm->getRank() == 0) {
88  cerr << "Caught exception: ";
89  rhs.Print();
90 
91 #ifdef HAVE_MPI
92  MPI_Finalize();
93 #endif
94  return (EXIT_FAILURE);
95  }
96  }
97 
98 #ifdef HAVE_MPI
99  MPI_Finalize();
100 #endif
101 
102  return (EXIT_SUCCESS);
103 }
static Teuchos::RCP< const Comm< OrdinalType > > getComm()
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
int main(int argc, char *argv[])
Definition: CrsMatrix.cpp:29