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 2004 Sandia Corporation
6 //
7 // Under terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
8 // the U.S. Government retains certain rights in this software.
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? Contact Michael A. Heroux (maherou@sandia.gov)
38 //
39 // ***********************************************************************
40 // @HEADER
41 
42 #ifndef __TSQR_Trilinos_TsqrTpetraTest_hpp
43 #define __TSQR_Trilinos_TsqrTpetraTest_hpp
44 
45 #include "AnasaziTpetraAdapter.hpp" // sic (not "-or")
46 #include "Tsqr_Random_NormalGenerator.hpp"
48 #include "Teuchos_Tuple.hpp"
49 #include "Teuchos_Time.hpp"
50 #include <sstream>
51 #include <stdexcept>
52 #include <vector>
53 
54 
55 namespace TSQR {
56  namespace Trilinos {
57  namespace Test {
58  using Teuchos::RCP;
59  using Teuchos::Tuple;
60 
61  template< class S, class LO, class GO, class Node >
62  class TpetraTsqrTest {
63  public:
64  typedef S scalar_type;
65  typedef LO local_ordinal_type;
66  typedef GO global_ordinal_type;
67  typedef Node node_type;
68 
69  typedef typename TSQR::ScalarTraits< S >::magnitude_type magnitude_type;
70  typedef TSQR::Trilinos::TsqrTpetraAdaptor< S, LO, GO, Node > adaptor_type;
71 
72  TpetraTsqrTest (const Tpetra::global_size_t nrowsGlobal,
73  const size_t ncols,
74  const Teuchos::RCP< const Teuchos::Comm<int> >& comm,
75  const Teuchos::RCP< Node >& node,
76  const Teuchos::ParameterList& params) :
77  results_ (magnitude_type(0), magnitude_type(0))
78  {
79  using Teuchos::Tuple;
81  typedef typename adaptor_type::factor_output_type factor_output_type;
82  typedef Teuchos::SerialDenseMatrix< LO, S > matrix_type;
83 
84  bool contiguousCacheBlocks = false;
85  try {
86  contiguousCacheBlocks = params.get<bool>("contiguousCacheBlocks");
87  } catch (InvalidParameter&) {
88  contiguousCacheBlocks = false;
89  }
90 
91  triple_type testProblem =
92  makeTestProblem (nrowsGlobal, ncols, comm, node, params);
93  // A is already filled in with the test problem. A_copy and
94  // Q are just multivectors with the same layout as A.
95  // A_copy will be used for temporary storage, and Q will
96  // store the (explicitly represented) Q factor output. R
97  // will store the R factor output.
98  RCP< MV > A = testProblem[0];
99  RCP< MV > A_copy = testProblem[1];
100  RCP< MV > Q = testProblem[2];
101  matrix_type R (ncols, ncols);
102 
103  // Adaptor uses one of the multivectors only to reference
104  // the underlying communicator object.
105  adaptor_type adaptor (*A, params);
106  if (contiguousCacheBlocks)
107  adaptor.cacheBlock (*A, *A_copy);
108 
109  factor_output_type factorOutput =
110  adaptor.factor (*A_copy, R, contiguousCacheBlocks);
111  adaptor.explicitQ (*A_copy, factorOutput, *Q, contiguousCacheBlocks);
112  if (contiguousCacheBlocks)
113  {
114  // Use A_copy as temporary storage for un-cache-blocking
115  // Q. Tpetra::MultiVector objects copy deeply.
116  *A_copy = *Q;
117  adaptor.unCacheBlock (*A_copy, *Q);
118  }
119  results_ = adaptor.verify (*A, *Q, R);
120  }
121 
124  std::pair< magnitude_type, magnitude_type >
125  getResults() const
126  {
127  return results_;
128  }
129 
130  private:
131  typedef Tpetra::MultiVector< S, LO, GO, Node > MV;
132  typedef Teuchos::Tuple< RCP< MV >, 3 > triple_type;
133  typedef Teuchos::RCP< const Teuchos::Comm<int> > comm_ptr;
134  typedef Tpetra::Map< LO, GO, Node > map_type;
135  typedef Teuchos::RCP< const map_type > map_ptr;
136  typedef TSQR::Random::NormalGenerator< LO, S > normalgen_type;
137  typedef Teuchos::RCP< Node > node_ptr;
138 
143  std::pair< magnitude_type, magnitude_type > results_;
144 
153  static map_ptr
154  makeMap (const Tpetra::global_size_t nrowsGlobal,
155  const comm_ptr& comm,
156  const node_ptr& node)
157  {
158  using Tpetra::createUniformContigMapWithNode;
159  return createUniformContigMapWithNode< LO, GO, Node > (nrowsGlobal, comm);
160  }
161 
164  static RCP< MV >
165  makeMultiVector (const map_ptr& map,
166  const size_t ncols)
167  {
168  // "false" means "don't fill with zeros"; we'll fill it in
169  // fillTpetraMultiVector().
170  return Teuchos::rcp (new MV (map, ncols, false));
171  }
172 
175  static void
176  fillMultiVector (const RCP< MV >& mv,
177  const RCP< normalgen_type >& pGen)
178  {
179  using TSQR::Trilinos::TpetraRandomizer;
180  typedef TpetraRandomizer< S, LO, GO, Node, normalgen_type > randomizer_type;
181 
182  const LO ncols = mv->getNumVectors();
183  std::vector< S > singular_values (ncols);
184  if (ncols > 0)
185  {
186  singular_values[0] = S(1);
187  for (LO k = 1; k < ncols; ++k)
188  singular_values[k] = singular_values[k-1] / S(2);
189  }
190  randomizer_type randomizer (*mv, pGen);
191  randomizer.randomMultiVector (*mv, &singular_values[0]);
192  }
193 
194  static triple_type
195  makeTestProblem (const Tpetra::global_size_t nrowsGlobal,
196  const size_t ncols,
197  const comm_ptr& comm,
198  const node_ptr& node,
199  const Teuchos::ParameterList& params)
200  {
201  using TSQR::Trilinos::TpetraMessenger;
202  using TSQR::MessengerBase;
203 
204  map_ptr map = makeMap (nrowsGlobal, comm, node);
205  RCP< MV > A = makeMultiVector (map, ncols);
206  RCP< MV > A_copy = makeMultiVector (map, ncols);
207  RCP< MV > Q = makeMultiVector (map, ncols);
208 
209  // Fill A with the random test problem
210  RCP< normalgen_type > pGen (new normalgen_type);
211  fillMultiVector (A, pGen);
212 
213  return Teuchos::tuple (A, A_copy, Q);
214  }
215  };
216 
217  } // namespace Test
218  } // namespace Trilinos
219 } // namespace TSQR
220 
221 #endif // __TSQR_Trilinos_TsqrTpetraTest_hpp
T & get(const std::string &name, T def_value)
Partial specialization of Anasazi::MultiVecTraits and Anasazi::OperatorTraits for Tpetra objects...
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)