File: Ifpack2_UnitTestSGSMT.cpp

package info (click to toggle)
trilinos 12.18.1-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 825,604 kB
  • sloc: cpp: 3,352,065; ansic: 432,926; fortran: 169,495; xml: 61,903; python: 40,836; sh: 32,697; makefile: 26,612; javascript: 8,535; perl: 7,136; f90: 6,372; csh: 4,183; objc: 2,620; lex: 1,469; lisp: 810; yacc: 497; awk: 364; ml: 281; php: 145
file content (164 lines) | stat: -rw-r--r-- 5,883 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*
HEADER
// ***********************************************************************
//
//       Ifpack2: Templated Object-Oriented Algebraic Preconditioner Package
//                 Copyright (2009) Sandia Corporation
//
// Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
// license for use of this work by or on behalf of the U.S. Government.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// 1. Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// 3. Neither the name of the Corporation nor the names of the
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// Questions? Contact Michael A. Heroux (maherou@sandia.gov)
//
// ***********************************************************************
//@HEADER
*/

/// \file Ifpack2_UnitTestSGSMT.cpp
/// \brief Unit test for multithreaded symmetric Gauss-Seidel

#include "Teuchos_UnitTestHarness.hpp"
#include "Ifpack2_UnitTestHelpers.hpp"
#include "Ifpack2_Relaxation.hpp"
#include "MatrixMarket_Tpetra.hpp"
#include <KokkosSparse_gauss_seidel.hpp>

namespace { // (anonymous)

using Teuchos::RCP;
typedef tif_utest::Node Node;
using std::endl;

TEUCHOS_UNIT_TEST_TEMPLATE_3_DECL(SGS_MT, JacobiComparison, Scalar, LO, GO)
{
  using Teuchos::ArrayView;
  using Teuchos::Array;
  using Teuchos::Comm;
  using Teuchos::ParameterList;
  using std::endl;
  typedef Tpetra::CrsMatrix<Scalar, LO, GO, Node> crs_matrix_type;
  typedef Tpetra::Map<LO, GO, Node> map_type;
  typedef Tpetra::Vector<Scalar, LO, GO, Node> vec_type;
  typedef Tpetra::RowMatrix<Scalar, LO, GO, Node> row_matrix_type;
  typedef Teuchos::ScalarTraits<Scalar> STS;

  Teuchos::OSTab tab0 (out);
  out << "Test multiple threaded SGS sweeps compared w.r.t. Jacobi" << endl;
  Teuchos::OSTab tab1 (out);

  RCP<const Comm<int> > comm = Tpetra::getDefaultComm ();

  Tpetra::MatrixMarket::Reader<crs_matrix_type> crs_reader;
  std::string file_name = "sherman1.mtx";

  RCP<const crs_matrix_type> A = crs_reader.readSparseFile(file_name, comm);

  RCP<const map_type> rowMap = A->getRowMap();
  RCP<const map_type> domainMap = rowMap;
  RCP<const map_type> rangeMap = rowMap;


  vec_type X_wanted (domainMap);
  vec_type Y_result (rangeMap);

  X_wanted.randomize ();
  A->apply(X_wanted, Y_result);


  // Test Symmetric Gauss-Seidel (SGS) with three sweeps.
  // Start by letting SGS set the starting solution to zero.
  ParameterList params_mt_sgs;

  params_mt_sgs.set ("relaxation: type", "MT Gauss-Seidel");
  params_mt_sgs.set ("relaxation: sweeps", 10);

  params_mt_sgs.set ("relaxation: zero starting solution", true);
  Ifpack2::Relaxation<row_matrix_type> prec_mt_sgs (A);

  prec_mt_sgs.setParameters (params_mt_sgs);
  TEST_NOTHROW( prec_mt_sgs.initialize () );
  TEST_NOTHROW( prec_mt_sgs.compute () );


  vec_type X_seek (domainMap);
  X_seek.putScalar (STS::zero ());
  vec_type initial_X_diff(X_seek, Teuchos::Copy);
  initial_X_diff.update (1, X_wanted, -1);
  double initial_normInf = initial_X_diff.normInf ();


  prec_mt_sgs.apply (Y_result, X_seek);

  vec_type X_diff_mtgs(X_seek, Teuchos::Copy);
  X_diff_mtgs.update (1, X_wanted, -1);
  double normInf_mt_gs = X_diff_mtgs.normInf ();

  ParameterList params_jacobi;
  params_jacobi.set ("relaxation: type", "Jacobi");
  params_jacobi.set ("relaxation: sweeps", 10);
  params_jacobi.set ("relaxation: zero starting solution", true);
  Ifpack2::Relaxation<row_matrix_type> prec_jacobi(A);

  prec_jacobi.setParameters (params_jacobi);
  TEST_NOTHROW( prec_jacobi.initialize () );
  TEST_NOTHROW( prec_jacobi.compute () );

  X_seek.putScalar (STS::zero ());
  prec_jacobi.apply (Y_result, X_seek);

  vec_type X_diff_jacobi(X_seek, Teuchos::Copy);
  X_diff_jacobi.update (1, X_wanted, -1);
  double normInf_jacobi = X_diff_jacobi.normInf ();

  int sgs_improve = initial_normInf > normInf_mt_gs;
  int sgs_better_than_jacobi = normInf_mt_gs < normInf_jacobi;

  out << "Initial Norm:" << initial_normInf
      << " MT GS Norm:" << normInf_mt_gs
      << " Jacobi Norm:" << normInf_jacobi << std::endl;

  TEST_EQUALITY( sgs_improve, 1 );
  TEST_EQUALITY( sgs_better_than_jacobi, 1 );
}


#define UNIT_TEST_GROUP_SC_LO_GO( Scalar, LO, GO ) \
    TEUCHOS_UNIT_TEST_TEMPLATE_3_INSTANT( SGS_MT, JacobiComparison, Scalar, LO, GO )
#include "Ifpack2_ETIHelperMacros.h"

IFPACK2_ETI_MANGLING_TYPEDEFS()

// TODO (mfh 24 Aug 2016) Test complex Scalar types

IFPACK2_INSTANTIATE_SLG_REAL( UNIT_TEST_GROUP_SC_LO_GO )

} // namespace (anonymous)