Anasazi  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
TsqrTpetraTest.hpp
1 // @HEADER
2 // ***********************************************************************
3 //
4 // Anasazi: Block Eigensolvers Package
5 // Copyright (2010) 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 // This library is free software; you can redistribute it and/or modify
11 // it under the terms of the GNU Lesser General Public License as
12 // published by the Free Software Foundation; either version 2.1 of the
13 // License, or (at your option) any later version.
14 //
15 // This library is distributed in the hope that it will be useful, but
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 // Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public
21 // License along with this library; if not, write to the Free Software
22 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
23 // USA
24 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
25 //
26 // ***********************************************************************
27 // @HEADER
28 
29 #ifndef __TSQR_Trilinos_TsqrTpetraTest_hpp
30 #define __TSQR_Trilinos_TsqrTpetraTest_hpp
31 
32 #include "Teuchos_Tuple.hpp"
34 
35 #include "Kokkos_ConfigDefs.hpp"
36 #include "Kokkos_NodeHelpers.hpp"
37 #include "Kokkos_SerialNode.hpp"
38 #ifdef HAVE_KOKKOSCLASSIC_TBB
39 #include "Kokkos_TBBNode.hpp"
40 #endif // HAVE_KOKKOSCLASSIC_TBB
41 
42 #include "Teuchos_Time.hpp"
43 #include "AnasaziTpetraAdapter.hpp" // sic (not "-or")
44 #include "Tsqr_Random_NormalGenerator.hpp"
45 
46 #include <sstream>
47 #include <stdexcept>
48 #include <vector>
49 
52 
53 namespace TSQR {
54  namespace Trilinos {
55  namespace Test {
56  using Teuchos::RCP;
57  using Teuchos::Tuple;
58 
59  template< class S, class LO, class GO, class Node >
60  class TpetraTsqrTest {
61  public:
62  typedef S scalar_type;
63  typedef LO local_ordinal_type;
64  typedef GO global_ordinal_type;
65  typedef Node node_type;
66 
67  typedef typename TSQR::ScalarTraits< S >::magnitude_type magnitude_type;
68  typedef TSQR::Trilinos::TsqrTpetraAdaptor< S, LO, GO, Node > adaptor_type;
69 
70  TpetraTsqrTest (const Tpetra::global_size_t nrowsGlobal,
71  const size_t ncols,
72  const Teuchos::RCP< const Teuchos::Comm<int> >& comm,
73  const Teuchos::RCP< Node >& node,
74  const Teuchos::ParameterList& params) :
75  results_ (magnitude_type(0), magnitude_type(0))
76  {
77  using Teuchos::Tuple;
79  typedef typename adaptor_type::factor_output_type factor_output_type;
80  typedef Teuchos::SerialDenseMatrix< LO, S > matrix_type;
81 
82  bool contiguousCacheBlocks = false;
83  try {
84  contiguousCacheBlocks = params.get<bool>("contiguousCacheBlocks");
85  } catch (InvalidParameter&) {
86  contiguousCacheBlocks = false;
87  }
88 
89  triple_type testProblem =
90  makeTestProblem (nrowsGlobal, ncols, comm, node, params);
91  // A is already filled in with the test problem. A_copy and
92  // Q are just multivectors with the same layout as A.
93  // A_copy will be used for temporary storage, and Q will
94  // store the (explicitly represented) Q factor output. R
95  // will store the R factor output.
96  RCP< MV > A = testProblem[0];
97  RCP< MV > A_copy = testProblem[1];
98  RCP< MV > Q = testProblem[2];
99  matrix_type R (ncols, ncols);
100 
101  // Adaptor uses one of the multivectors only to reference
102  // the underlying communicator object.
103  adaptor_type adaptor (*A, params);
104  if (contiguousCacheBlocks)
105  adaptor.cacheBlock (*A, *A_copy);
106 
107  factor_output_type factorOutput =
108  adaptor.factor (*A_copy, R, contiguousCacheBlocks);
109  adaptor.explicitQ (*A_copy, factorOutput, *Q, contiguousCacheBlocks);
110  if (contiguousCacheBlocks)
111  {
112  // Use A_copy as temporary storage for un-cache-blocking
113  // Q. Tpetra::MultiVector objects copy deeply.
114  *A_copy = *Q;
115  adaptor.unCacheBlock (*A_copy, *Q);
116  }
117  results_ = adaptor.verify (*A, *Q, R);
118  }
119 
122  std::pair< magnitude_type, magnitude_type >
123  getResults() const
124  {
125  return results_;
126  }
127 
128  private:
129  typedef Tpetra::MultiVector< S, LO, GO, Node > MV;
130  typedef Teuchos::Tuple< RCP< MV >, 3 > triple_type;
131  typedef Teuchos::RCP< const Teuchos::Comm<int> > comm_ptr;
132  typedef Tpetra::Map< LO, GO, Node > map_type;
133  typedef Teuchos::RCP< const map_type > map_ptr;
134  typedef TSQR::Random::NormalGenerator< LO, S > normalgen_type;
135  typedef Teuchos::RCP< Node > node_ptr;
136 
141  std::pair< magnitude_type, magnitude_type > results_;
142 
151  static map_ptr
152  makeMap (const Tpetra::global_size_t nrowsGlobal,
153  const comm_ptr& comm,
154  const node_ptr& node)
155  {
156  using Tpetra::createUniformContigMapWithNode;
157  return createUniformContigMapWithNode< LO, GO, Node > (nrowsGlobal,
158  comm, node);
159  }
160 
163  static RCP< MV >
164  makeMultiVector (const map_ptr& map,
165  const size_t ncols)
166  {
167  // "false" means "don't fill with zeros"; we'll fill it in
168  // fillTpetraMultiVector().
169  return Teuchos::rcp (new MV (map, ncols, false));
170  }
171 
174  static void
175  fillMultiVector (const RCP< MV >& mv,
176  const RCP< normalgen_type >& pGen)
177  {
178  using TSQR::Trilinos::TpetraRandomizer;
179  typedef TpetraRandomizer< S, LO, GO, Node, normalgen_type > randomizer_type;
180 
181  const LO ncols = mv->getNumVectors();
182  std::vector< S > singular_values (ncols);
183  if (ncols > 0)
184  {
185  singular_values[0] = S(1);
186  for (LO k = 1; k < ncols; ++k)
187  singular_values[k] = singular_values[k-1] / S(2);
188  }
189  randomizer_type randomizer (*mv, pGen);
190  randomizer.randomMultiVector (*mv, &singular_values[0]);
191  }
192 
193  static triple_type
194  makeTestProblem (const Tpetra::global_size_t nrowsGlobal,
195  const size_t ncols,
196  const comm_ptr& comm,
197  const node_ptr& node,
198  const Teuchos::ParameterList& params)
199  {
200  using TSQR::Trilinos::TpetraMessenger;
201  using TSQR::MessengerBase;
202 
203  map_ptr map = makeMap (nrowsGlobal, comm, node);
204  RCP< MV > A = makeMultiVector (map, ncols);
205  RCP< MV > A_copy = makeMultiVector (map, ncols);
206  RCP< MV > Q = makeMultiVector (map, ncols);
207 
208  // Fill A with the random test problem
209  RCP< normalgen_type > pGen (new normalgen_type);
210  fillMultiVector (A, pGen);
211 
212  return Teuchos::tuple (A, A_copy, Q);
213  }
214  };
215 
216  } // namespace Test
217  } // namespace Trilinos
218 } // namespace TSQR
219 
220 #endif // __TSQR_Trilinos_TsqrTpetraTest_hpp
T & get(const std::string &name, T def_value)
Adaptor between Anasazi and Tpetra::{MultiVector,Operator}.
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)