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 //
4 // Galeri: Finite Element and Matrix Generation Package
5 // Copyright (2006) ETHZ/Sandia Corporation
6 //
7 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8 // license for use of this work by or on behalf of the U.S. Government.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions about Galeri? Contact Marzio Sala (marzio.sala _AT_ gmail.com)
38 //
39 // ************************************************************************
40 // @HEADER
41 
42 #include "Galeri_XpetraMaps.hpp"
43 #include "Galeri_MatrixTraits.hpp"
44 #include "Galeri_XpetraMatrixTypes.hpp"
45 #include "Galeri_XpetraProblemFactory.hpp"
46 
47 #include "Teuchos_DefaultComm.hpp"
49 
50 #ifndef GALERI_TEST_USE_LONGLONG_GO
51 #define GO int
52 #else
53 #define GO long long
54 #endif
55 #define Scalar int
56 #define LO int
57 #define Node Tpetra::KokkosClassic::DefaultNode::DefaultNodeType
58 
59 using namespace Galeri;
60 
61 int main(int argc, char* argv[])
62 {
63  using Teuchos::RCP;
64  using Teuchos::rcp;
65 
66  typedef Tpetra::Map<LO, GO, Node> Tpetra_Map;
67  typedef Tpetra::CrsMatrix<Scalar, LO, GO, Node> Tpetra_CrsMatrix;
68  typedef Tpetra::MultiVector<Scalar, LO, GO, Node> Tpetra_MultiVector;
69  typedef Teuchos::ScalarTraits<Scalar> ScalarTraits;
70 
71 #ifdef HAVE_MPI
72  MPI_Init(&argc, &argv);
73 #endif
74 
75  // Create comm
76  RCP<const Teuchos::Comm<int>> comm = Teuchos::DefaultComm<int>::getComm();
77 
78  // Here we create the linear problem
79  //
80  // Matrix * LHS = RHS
81  //
82  // with Matrix arising from a 5-point formula discretization.
83 
84  std::string mapType = "Cartesian2D";
85  auto mapParameters = Teuchos::ParameterList("Tpetra::Map");
86  // dimension of the problem is nx x ny
87  mapParameters.set("nx", 10 * comm->getSize());
88  mapParameters.set("ny", 10);
89  // total number of processors is mx x my
90  mapParameters.set("mx", comm->getSize());
91  mapParameters.set("my", 1);
92 
93  auto out = Teuchos::getFancyOStream(Teuchos::rcpFromRef(std::cout));
94 
95  try
96  {
97  // Creation of the map
98  auto map = RCP{Galeri::Xpetra::CreateMap<Scalar, GO, Tpetra_Map>(mapType, comm, mapParameters)};
99 
100  // Creation of linear problem
101  auto problem = Galeri::Xpetra::BuildProblem<Scalar, LO, GO, Tpetra_Map, Tpetra_CrsMatrix, Tpetra_MultiVector>("Laplace2D", map, mapParameters);
102 
103  // Build Matrix and MultiVectors
104  auto matrix = problem->BuildMatrix();
105  auto LHS = rcp(new Tpetra_MultiVector(matrix->getDomainMap(), 1));
106  auto RHS = rcp(new Tpetra_MultiVector(matrix->getRangeMap(), 1));
107  auto ExactSolution = rcp(new Tpetra_MultiVector(matrix->getDomainMap(), 1));
108 
109  ExactSolution->randomize(0, 100);
110  LHS->putScalar(ScalarTraits::zero());
111 
112  matrix->apply(*ExactSolution, *RHS);
113 
114  matrix->describe(*out, Teuchos::EVerbosityLevel::VERB_EXTREME);
115  LHS->describe(*out, Teuchos::EVerbosityLevel::VERB_EXTREME);
116  RHS->describe(*out, Teuchos::EVerbosityLevel::VERB_EXTREME);
117  ExactSolution->describe(*out, Teuchos::EVerbosityLevel::VERB_EXTREME);
118 
119  // at this point any LinearSolver can be used which understands the Tpetra objects. For example: Amesos2 or Ifpack2
120  }
121  catch (Galeri::Exception &rhs)
122  {
123  if (comm->getRank() == 0)
124  {
125  cerr << "Caught exception: ";
126  rhs.Print();
127 
128 #ifdef HAVE_MPI
129  MPI_Finalize();
130 #endif
131  return (EXIT_FAILURE);
132  }
133  }
134 
135 #ifdef HAVE_MPI
136  MPI_Finalize();
137 #endif
138 
139  return (EXIT_SUCCESS);
140 }
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:61