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 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
|
/*
//@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_UnitTestAmesos2solver.cpp
/// \brief Ifpack2 unit test for the Amesos2 wrapper.
///
/// This test only builds nontrivially and executes if Amesos2 is enabled.
#include <Teuchos_ConfigDefs.hpp>
#include <Ifpack2_ConfigDefs.hpp>
#include <Teuchos_UnitTestHarness.hpp>
//#if defined(HAVE_IFPACK2_AMESOS2) && defined(HAVE_AMESOS2_SUPERLU)
#if defined(HAVE_IFPACK2_AMESOS2)
#include <Amesos2_config.h>
#include <Ifpack2_Details_Amesos2Wrapper.hpp>
#include <iostream>
#include <Ifpack2_UnitTestHelpers.hpp>
namespace {
using Teuchos::RCP;
using std::endl;
typedef Tpetra::global_size_t GST;
typedef tif_utest::Node Node;
//this macro declares the unit-test-class:
TEUCHOS_UNIT_TEST_TEMPLATE_3_DECL(Ifpack2Amesos2Wrapper, Test0, Scalar, LO, GO)
{
//we are now in a class method declared by the above macro, and
//that method has these input arguments:
//Teuchos::FancyOStream& out, bool& success
using Kokkos::Details::ArithTraits;
typedef Tpetra::Map<LO,GO,Node> map_type;
typedef Tpetra::CrsMatrix<Scalar,LO,GO,Node> crs_matrix_type;
typedef Tpetra::RowMatrix<Scalar,LO,GO,Node> row_matrix_type;
#ifdef HAVE_AMESOS2_SUPERLU
typedef Tpetra::MultiVector<Scalar,LO,GO,Node> mv_type;
typedef typename mv_type::impl_scalar_type val_type;
typedef typename Kokkos::Details::ArithTraits<val_type>::mag_type mag_type;
typedef typename map_type::device_type device_type;
//const mag_type oneMag = ArithTraits<mag_type>::one ();
#endif
out << "Ifpack2 Amesos2 wrapper: Test0" << endl;
Teuchos::OSTab tab1 (out);
GST num_rows_per_proc = 5;
RCP<const map_type> rowmap =
tif_utest::create_tpetra_map<LO,GO,Node> (num_rows_per_proc);
RCP<const crs_matrix_type> crsmatrix = tif_utest::create_test_matrix<Scalar,LO,GO,Node> (rowmap);
out << "Create Ifpack2 Amesos2 wrapper instance" << endl;
Ifpack2::Details::Amesos2Wrapper<row_matrix_type> prec (crsmatrix);
out << "Set solver parameters" << endl;
Teuchos::ParameterList params;
params.set("Amesos2 solver name","superlu");
Teuchos::ParameterList &sublist = params.sublist("Amesos2");
(sublist.sublist("SuperLU")).set("ILU_Flag",false); //create SuperLU sublist to get rid of unused variable warnings
TEST_NOTHROW(prec.setParameters(params));
//trivial tests to insist that the preconditioner's domain/range maps are
//identically those of the matrix:
const map_type* mtx_dom_map_ptr = &*crsmatrix->getDomainMap();
const map_type* mtx_rng_map_ptr = &*crsmatrix->getRangeMap();
const map_type* prec_dom_map_ptr = &*prec.getDomainMap();
const map_type* prec_rng_map_ptr = &*prec.getRangeMap();
TEST_EQUALITY( prec_dom_map_ptr, mtx_dom_map_ptr );
TEST_EQUALITY( prec_rng_map_ptr, mtx_rng_map_ptr );
# if !defined(HAVE_AMESOS2_SUPERLU)
out << "NOT TESTING SUPERLU!!! SUPERLU is NOT enabled!" << endl;
TEST_THROW(prec.initialize(),std::invalid_argument);
# else
out << "SuperLU IS enabled." << endl;
out << "Test prec.initialize()" << endl;
TEST_NOTHROW(prec.initialize());
out << "Test prec.compute()" << endl;
prec.compute();
mv_type x (rowmap, 2);
mv_type y (rowmap, 2);
x.putScalar (Teuchos::ScalarTraits<Scalar>::one ());
out << "Test prec.apply(x,y)" << endl;
prec.apply (x, y);
out << "y should be full of 0.5's now" << endl;
const Scalar one = Teuchos::ScalarTraits<Scalar>::one ();
const Scalar two = one + one;
const Scalar oneHalf = one / two;
mv_type diff (y.getMap (), y.getNumVectors ());
diff.putScalar (oneHalf);
diff.update (one, y, -one);
Kokkos::View<mag_type*, device_type> norms ("norms", diff.getNumVectors ());
diff.normInf (norms);
auto norms_h = Kokkos::create_mirror_view (norms);
Kokkos::deep_copy (norms_h, norms);
{
typedef Teuchos::ScalarTraits<Scalar> STS;
const mag_type bound = ArithTraits<mag_type>::zero ();
diff.putScalar (STS::one ());
diff.update (STS::one (), x, -STS::one ());
diff.normInf (norms);
Kokkos::deep_copy (norms_h, norms);
for (LO j = 0; j < static_cast<LO> (norms_h.extent (0)); ++j) {
const mag_type absVal = ArithTraits<mag_type>::abs (norms_h(j));
TEST_ASSERT( absVal <= bound );
if (absVal > bound) {
out << "\\| x(:," << j << ") - 1 \\|_{\\infty} = "
<< absVal << " > " << bound
<< "; the norm equals " << norms_h(j)
<< endl;
}
}
}
# endif
}
TEUCHOS_UNIT_TEST_TEMPLATE_3_DECL(Ifpack2Amesos2Wrapper, Test1, Scalar, LocalOrdinal, GlobalOrdinal)
{
//we are now in a class method declared by the above macro, and
//that method has these input arguments:
//Teuchos::FancyOStream& out, bool& success
using Teuchos::RCP;
typedef Tpetra::Map<LocalOrdinal,GlobalOrdinal,Node> map_type;
typedef Tpetra::CrsMatrix<Scalar,LocalOrdinal,GlobalOrdinal,Node> crs_matrix_type;
typedef Tpetra::RowMatrix<Scalar,LocalOrdinal,GlobalOrdinal,Node> row_matrix_type;
out << "Ifpack2 Amesos2 wrapper: Test1" << endl;
Teuchos::OSTab tab1 (out);
GST num_rows_per_proc = 5;
RCP<const map_type> rowmap = tif_utest::create_tpetra_map<LocalOrdinal,GlobalOrdinal,Node> (num_rows_per_proc);
RCP<const crs_matrix_type> crsmatrix = tif_utest::create_test_matrix3<Scalar,LocalOrdinal,GlobalOrdinal,Node>(rowmap);
Ifpack2::Details::Amesos2Wrapper<row_matrix_type> prec (crsmatrix);
Teuchos::ParameterList params;
params.set("Amesos2 solver name","superlu");
Teuchos::ParameterList &sublist = params.sublist("Amesos2");
(sublist.sublist("SuperLU")).set("ILU_Flag",false); //create SuperLU sublist to get rid of unused variable warnings
TEST_NOTHROW(prec.setParameters(params));
# if !defined(HAVE_AMESOS2_SUPERLU)
TEST_THROW(prec.initialize(),std::invalid_argument);
# else
TEST_NOTHROW(prec.initialize());
prec.compute();
typedef Tpetra::MultiVector<Scalar,LocalOrdinal,GlobalOrdinal,Node> mv_type;
mv_type x(rowmap,2), y(rowmap,2);
x.putScalar (Teuchos::ScalarTraits<Scalar>::one ());
crsmatrix->apply (x, y);
prec.apply (y, x);
Teuchos::ArrayRCP<const Scalar> xview = x.get1dView();
//x should be full of 1's now.
Teuchos::ArrayRCP<Scalar> ones(num_rows_per_proc*2, Teuchos::ScalarTraits<Scalar>::one ());
TEST_COMPARE_FLOATING_ARRAYS(xview, ones(), 2*Teuchos::ScalarTraits<Scalar>::eps());
# endif
}
#define UNIT_TEST_GROUP_SCALAR_ORDINAL(Scalar,LocalOrdinal,GlobalOrdinal) \
TEUCHOS_UNIT_TEST_TEMPLATE_3_INSTANT( Ifpack2Amesos2Wrapper, Test0, Scalar, LocalOrdinal, GlobalOrdinal) \
TEUCHOS_UNIT_TEST_TEMPLATE_3_INSTANT( Ifpack2Amesos2Wrapper, Test1, Scalar, LocalOrdinal, GlobalOrdinal)
// FIXME (mfh 11 Apr 2018) We should test this at least for all
// enabled real Scalar types, if not complex Scalar types as well.
typedef Tpetra::MultiVector<>::scalar_type default_scalar_type;
typedef Tpetra::MultiVector<>::local_ordinal_type default_local_ordinal_type;
typedef Tpetra::MultiVector<>::global_ordinal_type default_global_ordinal_type;
UNIT_TEST_GROUP_SCALAR_ORDINAL( default_scalar_type, default_local_ordinal_type, default_global_ordinal_type )
} // namespace (anonymous)
#endif // HAVE_IFPACK2_AMESOS2
|