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 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428
|
#include "Teuchos_UnitTestHarness.hpp"
#include "Ifpack2_LocalSparseTriangularSolver.hpp"
#include "Tpetra_Core.hpp"
#include "Tpetra_CrsMatrix.hpp"
#include "Tpetra_MultiVector.hpp"
#include "Tpetra_Details_determineLocalTriangularStructure.hpp"
#include "Teuchos_CommHelpers.hpp"
#include <type_traits>
namespace {
template<class LO, class GO, class NT>
Tpetra::Details::LocalTriangularStructureResult<LO>
getLocalTriangularStructure (const Tpetra::RowGraph<LO, GO, NT>& G)
{
using Tpetra::Details::determineLocalTriangularStructure;
using crs_graph_type = Tpetra::CrsGraph<LO, GO, NT>;
const crs_graph_type& G_crs = dynamic_cast<const crs_graph_type&> (G);
auto G_lcl = G_crs.getLocalGraph ();
auto lclRowMap = G.getRowMap ()->getLocalMap ();
auto lclColMap = G.getColMap ()->getLocalMap ();
return determineLocalTriangularStructure (G_lcl, lclRowMap, lclColMap, true);
}
template<class SC, class LO, class GO, class NT>
Tpetra::Details::LocalTriangularStructureResult<LO>
getLocalTriangularStructure (const Tpetra::CrsMatrix<SC, LO, GO, NT>& A)
{
return getLocalTriangularStructure (* (A.getGraph ()));
}
using Teuchos::Array;
using Teuchos::ArrayView;
using Teuchos::as;
using Teuchos::Comm;
using Teuchos::outArg;
using Teuchos::ParameterList;
using Teuchos::parameterList;
using Teuchos::RCP;
using Teuchos::rcp;
using Teuchos::REDUCE_SUM;
using Teuchos::reduceAll;
using Teuchos::tuple;
using Teuchos::ETransp;
using Teuchos::CONJ_TRANS;
using Teuchos::NO_TRANS;
using Teuchos::TRANS;
using Teuchos::EDiag;
using Teuchos::UNIT_DIAG;
using Teuchos::NON_UNIT_DIAG;
using Teuchos::EUplo;
using Teuchos::LOWER_TRI;
using Teuchos::UNDEF_TRI;
using Teuchos::UPPER_TRI;
using std::endl;
using GST = Tpetra::global_size_t;
TEUCHOS_STATIC_SETUP()
{
Teuchos::CommandLineProcessor &clp = Teuchos::UnitTestRepository::getCLP();
clp.addOutputSetupOptions(true);
}
//
// UNIT TESTS
//
////
TEUCHOS_UNIT_TEST_TEMPLATE_4_DECL( CrsMatrix, EmptyTriSolve, Scalar, LO, GO, Node )
{
using crs_matrix_type = Tpetra::CrsMatrix<Scalar, LO, GO, Node>;
using row_matrix_type = Tpetra::RowMatrix<Scalar, LO, GO, Node>;
using solver_type = Ifpack2::LocalSparseTriangularSolver<row_matrix_type>;
using STS = Teuchos::ScalarTraits<Scalar>;
using MV = Tpetra::MultiVector<Scalar,LO,GO,Node>;
using mag_type = typename STS::magnitudeType;
using STM = Teuchos::ScalarTraits<mag_type>;
using map_type = Tpetra::Map<LO, GO, Node>;
const size_t numLocal = 13, numVecs = 7;
const GST INVALID = Teuchos::OrdinalTraits<GST>::invalid ();
// get a comm
RCP<const Comm<int> > comm = Tpetra::getDefaultComm();
// create a Map
RCP<const map_type> map =
Tpetra::createContigMapWithNode<LO, GO, Node> (INVALID, numLocal, comm);
/* Create a triangular matrix with no entries, for testing implicit diagonals.
We test with Transpose and Non-Transpose application solve (these should be equivalent for the identity matrix)
*/
MV X(map,numVecs), B(map,numVecs), Xhat(map,numVecs);
X.setObjectLabel("X");
B.setObjectLabel("B");
Xhat.setObjectLabel("Xhat");
X.randomize();
for (size_t tnum=0; tnum < 2; ++tnum) {
ETransp trans = ((tnum & 1) == 1 ? CONJ_TRANS : NO_TRANS);
RCP<solver_type> ZeroIOp;
{
RCP<crs_matrix_type> ZeroMat;
// must explicitly provide the column map for implicit diagonals
ZeroMat = rcp(new crs_matrix_type(map,map,0));
RCP<ParameterList> params = parameterList();
RCP<ParameterList> fillparams = sublist(params,"Local Sparse Ops");
fillparams->set("Prepare Solve", true);
fillparams->set("Prepare Transpose Solve", true);
fillparams->set("Prepare Conjugate Transpose Solve", true);
ZeroMat->fillComplete(params);
auto lclTri = getLocalTriangularStructure (*ZeroMat);
TEST_ASSERT( lclTri.couldBeLowerTriangular );
TEST_ASSERT( lclTri.couldBeUpperTriangular );
GO gblDiagCount = 0;
reduceAll<int, GO> (*comm, REDUCE_SUM,
static_cast<GO> (lclTri.diagCount),
outArg (gblDiagCount));
TEST_EQUALITY( gblDiagCount, static_cast<GO> (0) );
ZeroIOp = rcp<solver_type> (new solver_type (ZeroMat.getConst ()));
ZeroIOp->initialize ();
ZeroIOp->compute ();
}
X = B;
Xhat.randomize();
ZeroIOp->apply(B,Xhat,trans);
//
Xhat.update(-STS::one(),X,STS::one());
Array<mag_type> errnrms(numVecs), normsB(numVecs), zeros(numVecs, STM::zero());
Xhat.norm1(errnrms());
B.norm1(normsB());
mag_type maxBnrm = *std::max_element( normsB.begin(), normsB.end() );
if (std::is_integral<Scalar>::value) {
TEST_COMPARE_ARRAYS(errnrms, zeros);
}
else {
TEST_COMPARE_FLOATING_ARRAYS( errnrms, zeros, maxBnrm );
}
}
}
////
TEUCHOS_UNIT_TEST_TEMPLATE_4_DECL( CrsMatrix, TriSolve, Scalar, LO, GO, Node )
{
out << "Testing Tpetra::CrsMatrix triangular solve with nonempty matrices"
<< endl;
Teuchos::OSTab tab0 (out);
// mfh 26 Feb 2014: Organizing the if-else in this way avoids a
// build warning for "dynamic initialization in unreachable code."
if (std::is_integral<Scalar>::value) {
out << "Skipping testing for the integral type Scalar="
<< Teuchos::TypeNameTraits<Scalar>::name () << "." << endl;
return;
}
else {
using crs_matrix_type = Tpetra::CrsMatrix<Scalar,LO,GO,Node>;
using row_matrix_type = Tpetra::RowMatrix<Scalar,LO,GO,Node>;
using solver_type = Ifpack2::LocalSparseTriangularSolver<row_matrix_type>;
using map_type = Tpetra::Map<LO, GO, Node>;
using STS = Teuchos::ScalarTraits<Scalar>;
using MV = Tpetra::MultiVector<Scalar,LO,GO,Node>;
using mag_type = typename STS::magnitudeType;
using STM = Teuchos::ScalarTraits<mag_type>;
const size_t numLocal = 13, numVecs = 7;
const GST INVALID = Teuchos::OrdinalTraits<GST>::invalid ();
RCP<const Comm<int> > comm = Tpetra::getDefaultComm ();
// Create a row Map for the matrix.
// This will be the same as the domain and range Maps.
RCP<const map_type> map =
Tpetra::createContigMapWithNode<LO, GO, Node> (INVALID, numLocal, comm);
Scalar SONE = STS::one ();
/* Create one of the following locally triangular matries:
0 [1 2 ]
1 [ 1 3 ]
. [ . . ] = U
n-2 [ 1 n]
n-1 [ 1]
0 [1 ]
1 [2 1 ]
. [ . . ] = L
n-2 [ n-1 1 ]
n-1 [ n 1]
The resulting global matrices are diag(U,U,...,U) resp. diag(L,L,...,L).
For each of these (upper or lower triangular), we test all
16 combinations of the following options:
- Explicit or implicit unit diagonal
- With and without the (conjugate) transpose
- Optimized or nonoptimized storage
*/
MV X (map, numVecs), B (map, numVecs), Xhat (map, numVecs);
X.setObjectLabel("X");
B.setObjectLabel("B");
Xhat.setObjectLabel("Xhat");
X.randomize();
// Sanity check for X.
Array<mag_type> normsX (numVecs);
{
X.norm1 (normsX ());
Array<size_t> badColumns;
for (size_t j = 0; j < numVecs; ++j) {
if (STS::isnaninf (normsX[j])) {
badColumns.push_back (j);
}
}
TEUCHOS_TEST_FOR_EXCEPTION(
badColumns.size () > 0,
std::runtime_error,
"Columns " << Teuchos::toString (badColumns) << " of the input X "
"have a 1-norm either Inf or NaN. That suggests that randomize() "
"is broken. Here are the 1-norms of each column: "
<< Teuchos::toString (normsX));
}
RCP<ParameterList> params = parameterList();
// Test all 16 combinations of options.
for (size_t tnum = 0; tnum < 16; ++tnum) {
const EUplo uplo = ((tnum & 1) == 1 ? UPPER_TRI : LOWER_TRI);
const EDiag diag = ((tnum & 2) == 2 ? UNIT_DIAG : NON_UNIT_DIAG);
const ETransp trans = ((tnum & 8) == 8 ? CONJ_TRANS : NO_TRANS);
const bool optimizeStorage = (tnum & 4) == 4;
std::string diagStr;
if (diag == UNIT_DIAG) {
diagStr = "UNIT_DIAG";
} else if (diag == NON_UNIT_DIAG) {
diagStr = "NON_UNIT_DIAG";
} else {
diagStr = "UNDEFINED";
}
std::string uploStr;
if (uplo == LOWER_TRI) {
uploStr = "LOWER_TRI";
} else if (uplo == UPPER_TRI) {
uploStr = "UPPER_TRI";
} else if (uplo == UNDEF_TRI) {
uploStr = "UNDEF_TRI";
} else {
uploStr = "UNDEFINED";
}
std::string transStr;
if (trans == CONJ_TRANS) {
transStr = "CONJ_TRANS";
} else if (trans == TRANS) {
transStr = "TRANS";
} else if (trans == NO_TRANS) {
transStr = "NO_TRANS";
} else {
transStr = "UNDEFINED";
}
out << "Test " << (tnum+1) << " of " << 16 << ":" << endl;
Teuchos::OSTab tab1 (out);
{
out << "Parameters:" << endl;
Teuchos::OSTab tab2 (out);
out << "uplo: " << uploStr << endl
<< "diag: " << diagStr << endl
<< "trans: " << transStr << endl
<< "optimizeStorage: " << (optimizeStorage ? "true" : "false") << endl
<< endl;
}
params->set ("Optimize Storage", optimizeStorage);
RCP<ParameterList> fillparams = sublist (params, "Local Sparse Ops");
fillparams->set ("Prepare Solve", true);
fillparams->set ("Prepare Transpose Solve", true);
fillparams->set ("Prepare Conjugate Transpose Solve", true);
RCP<solver_type> AIOp;
RCP<crs_matrix_type> AMat;
{
if (diag == UNIT_DIAG) {
// must explicitly specify the column map
AMat = rcp(new crs_matrix_type(map,map,2));
}
else {
// can let the matrix compute a column map
AMat = rcp(new crs_matrix_type(map,2));
}
// fill the matrix
if (uplo == UPPER_TRI) {
if (diag == UNIT_DIAG) {
for (GO gid=map->getMinGlobalIndex(); gid <= map->getMaxGlobalIndex(); ++gid) {
if (gid == map->getMaxGlobalIndex()) {
// do nothing
}
else {
AMat->insertGlobalValues (gid, tuple<GO> (gid+1), tuple<Scalar> (as<Scalar> (gid+2)));
}
}
}
else {
for (GO gid=map->getMinGlobalIndex(); gid <= map->getMaxGlobalIndex(); ++gid) {
if (gid == map->getMaxGlobalIndex()) {
AMat->insertGlobalValues (gid, tuple<GO> (gid), tuple<Scalar> (SONE));
}
else {
AMat->insertGlobalValues (gid, tuple<GO> (gid,gid+1), tuple<Scalar> (SONE, as<Scalar>(gid+2)));
}
}
}
}
else { // uplo == LOWER_TRI
if (diag == UNIT_DIAG) {
for (GO gid=map->getMinGlobalIndex(); gid <= map->getMaxGlobalIndex(); ++gid) {
if (gid == map->getMinGlobalIndex()) {
// do nothing
}
else {
AMat->insertGlobalValues (gid, tuple<GO> (gid-1), tuple<Scalar> (as<Scalar> (gid+1)));
}
}
}
else {
for (GO gid=map->getMinGlobalIndex(); gid <= map->getMaxGlobalIndex(); ++gid) {
if (gid == map->getMinGlobalIndex()) {
AMat->insertGlobalValues (gid, tuple<GO> (gid), tuple<Scalar> (SONE));
}
else {
AMat->insertGlobalValues (gid, tuple<GO> (gid-1, gid), tuple<Scalar> (as<Scalar> (gid+1), SONE));
}
}
}
}
AMat->fillComplete(params);
auto lclTri = getLocalTriangularStructure (*AMat);
TEST_EQUALITY( lclTri.couldBeLowerTriangular, uplo == LOWER_TRI );
TEST_EQUALITY( lclTri.couldBeUpperTriangular, uplo == UPPER_TRI );
GO gblDiagCount = 0;
reduceAll<int, GO> (*comm, REDUCE_SUM,
static_cast<GO> (lclTri.diagCount),
outArg (gblDiagCount));
TEST_EQUALITY( gblDiagCount == static_cast<GO> (0), diag == UNIT_DIAG );
// AIOp.apply (X,B,trans) solves op(A) X=B for X locally,
// using a triangular solve. op(A) is just A if
// trans==NO_TRANS, else A^H (Hermitian transpose) if
// trans==CONJ_TRANS.
AIOp = rcp<solver_type> (new solver_type (AMat.getConst ()));
AIOp->initialize ();
AIOp->compute ();
}
B.randomize ();
AIOp->apply (X, B, trans);
if (diag == UNIT_DIAG) {
// we want (I+A)*X -> B
// A*X -> B needs to be augmented with X
B.update (STS::one (), X, STS::one());
}
Array<mag_type> normsB (numVecs);
B.norm1 (normsB ());
{
Array<size_t> badColumns;
for (size_t j = 0; j < numVecs; ++j) {
if (STS::isnaninf (normsB[j])) {
badColumns.push_back (j);
}
}
if (badColumns.size () > 0) {
out << "Result of triangular solve contains Inf or NaN:" << endl;
Teuchos::OSTab tab2 (out);
B.normInf (normsB ());
out << "Inf-norms of each column of B = A \\ X: "
<< Teuchos::toString (normsB) << endl;
B.norm2 (normsB ());
out << "2-norms of each column of B = A \\ X: "
<< Teuchos::toString (normsB) << endl;
B.norm1 (normsB ());
out << "1-norms of each column of B = A \\ X: "
<< Teuchos::toString (normsB) << endl;
out << "Input MV X:" << endl;
X.describe (out, Teuchos::VERB_EXTREME);
out << "Output MV B, on output:" << endl;
B.describe (out, Teuchos::VERB_EXTREME);
}
}
Xhat.randomize ();
AIOp->apply (B, Xhat, trans);
Xhat.update (-STS::one (), X, STS::one ());
Array<mag_type> errnrms (numVecs), zeros (numVecs, STM::zero ());
Xhat.norm1 (errnrms ());
B.norm1 (normsB ());
mag_type maxBnrm = *std::max_element (normsB.begin (), normsB.end ());
if (std::is_integral<Scalar>::value) {
TEST_COMPARE_ARRAYS( errnrms, zeros );
} else {
TEST_COMPARE_FLOATING_ARRAYS( errnrms, zeros, maxBnrm );
}
}
}
}
//
// INSTANTIATIONS
//
#define UNIT_TEST_GROUP( SCALAR, LO, GO, NODE ) \
TEUCHOS_UNIT_TEST_TEMPLATE_4_INSTANT( CrsMatrix, EmptyTriSolve, SCALAR, LO, GO, NODE ) \
TEUCHOS_UNIT_TEST_TEMPLATE_4_INSTANT( CrsMatrix, TriSolve, SCALAR, LO, GO, NODE )
#include "Ifpack2_ETIHelperMacros.h"
IFPACK2_ETI_MANGLING_TYPEDEFS()
IFPACK2_INSTANTIATE_SLGN( UNIT_TEST_GROUP )
}
|