| 1 |
// @HEADER |
| 2 |
// ***************************************************************************** |
| 3 |
// MueLu: A package for multigrid based preconditioning |
| 4 |
// |
| 5 |
// Copyright 2012 NTESS and the MueLu contributors. |
| 6 |
// SPDX-License-Identifier: BSD-3-Clause |
| 7 |
// ***************************************************************************** |
| 8 |
// @HEADER |
| 9 |
|
| 10 |
#include <iostream> |
| 11 |
#ifndef _MSC_VER |
| 12 |
#include <sys/time.h> |
| 13 |
#endif |
| 14 |
|
| 15 |
#include <Teuchos_StandardCatchMacros.hpp> |
| 16 |
#include <Teuchos_TimeMonitor.hpp> |
| 17 |
#include <Teuchos_StackedTimer.hpp> |
| 18 |
|
| 19 |
#include <Xpetra_MultiVectorFactory.hpp> |
| 20 |
#include <Xpetra_MatrixMatrix.hpp> |
| 21 |
#include <Xpetra_IO.hpp> |
| 22 |
|
| 23 |
// Galeri |
| 24 |
#include <Galeri_XpetraParameters.hpp> |
| 25 |
#include <Galeri_XpetraUtils.hpp> |
| 26 |
#include <Galeri_XpetraMaps.hpp> |
| 27 |
// |
| 28 |
|
| 29 |
#include "MueLu.hpp" |
| 30 |
#include "MueLu_TestHelpers.hpp" |
| 31 |
|
| 32 |
namespace MueLuTests { |
| 33 |
|
| 34 |
// generate random matrix |
| 35 |
template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node> |
| 36 |
Teuchos::RCP<Xpetra::Matrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> > generateRandomMatrix(LocalOrdinal const &minEntriesPerRow, LocalOrdinal const &maxEntriesPerRow, |
| 37 |
Teuchos::RCP<const Xpetra::Map<LocalOrdinal, GlobalOrdinal, Node> > &rowMap, |
| 38 |
Teuchos::RCP<const Xpetra::Map<LocalOrdinal, GlobalOrdinal, Node> > &domainMap); |
| 39 |
|
| 40 |
// generate random map |
| 41 |
template <class LocalOrdinal, class GlobalOrdinal, class Node> |
| 42 |
Teuchos::RCP<const Xpetra::Map<LocalOrdinal, GlobalOrdinal, Node> > generateRandomContiguousMap(size_t const minRowsPerProc, size_t const maxRowsPerProc, |
| 43 |
Teuchos::RCP<const Teuchos::Comm<int> > const &comm, Xpetra::UnderlyingLib const lib); |
| 44 |
|
| 45 |
// generate "random" whole number in interval [a,b] |
| 46 |
template <class T> |
| 47 |
size_t generateRandomNumber(T a, T b); |
| 48 |
|
| 49 |
// generate distinct seeds for each process. The seeds are different each run, but print to screen in case the run needs reproducibility. |
| 50 |
unsigned int generateSeed(Teuchos::Comm<int> const &comm, const double initSeed = -1); |
| 51 |
|
| 52 |
} // namespace MueLuTests |
| 53 |
|
| 54 |
//- -- -------------------------------------------------------- |
| 55 |
template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node> |
| 56 |
int main_(Teuchos::CommandLineProcessor &clp, Xpetra::UnderlyingLib &lib, int argc, char *argv[]) { |
| 57 |
using Teuchos::RCP; |
| 58 |
using Teuchos::rcp; |
| 59 |
using Teuchos::StackedTimer; |
| 60 |
using Teuchos::Time; |
| 61 |
using Teuchos::TimeMonitor; |
| 62 |
using namespace MueLuTests; |
| 63 |
|
| 64 |
RCP<const Teuchos::Comm<int> > comm = Teuchos::DefaultComm<int>::getComm(); |
| 65 |
|
| 66 |
#include <MueLu_UseShortNames.hpp> |
| 67 |
Xpetra::Parameters xpetraParameters(clp); |
| 68 |
int optNmults = 1; |
| 69 |
clp.setOption("nmults", &optNmults, "number of matrix matrix multiplies to perform"); |
| 70 |
|
| 71 |
GO optMaxRowsPerProc = 1000; |
| 72 |
clp.setOption("maxrows", &optMaxRowsPerProc, "maximum number of rows per processor"); |
| 73 |
GO optMinRowsPerProc = 500; |
| 74 |
clp.setOption("minrows", &optMinRowsPerProc, "minimum number of rows per processor"); |
| 75 |
LO optMaxEntriesPerRow = 50; |
| 76 |
clp.setOption("maxnnz", &optMaxEntriesPerRow, "maximum number of nonzeros per row"); |
| 77 |
LO optMinEntriesPerRow = 25; |
| 78 |
clp.setOption("minnnz", &optMinEntriesPerRow, "minimum number of nonzeros per row"); |
| 79 |
|
| 80 |
bool optDumpMatrices = false; |
| 81 |
clp.setOption("dump", "nodump", &optDumpMatrices, "write matrix to file"); |
| 82 |
bool optTimings = false; |
| 83 |
clp.setOption("timings", "notimings", &optTimings, "print timings to screen"); |
| 84 |
double optSeed = -1; |
| 85 |
clp.setOption("seed", &optSeed, "random number seed (cast to unsigned int)"); |
| 86 |
|
| 87 |
std::ostringstream description; |
| 88 |
description << "This is a parallel-only test of the sparse matrix matrix multiply. Two\n" |
| 89 |
<< "\"random\" matrices are generated. They are most likely rectangular. Each\n" |
| 90 |
<< "has random contiguous domain and range maps. Each row has a random number of\n" |
| 91 |
<< "entries with random global column IDs. The matrix values are identically 1,\n" |
| 92 |
<< "and success is declared if the multiply finishes."; |
| 93 |
clp.setDocString(description.str().c_str()); |
| 94 |
|
| 95 |
switch (clp.parse(argc, argv)) { |
| 96 |
case Teuchos::CommandLineProcessor::PARSE_HELP_PRINTED: return EXIT_SUCCESS; |
| 97 |
case Teuchos::CommandLineProcessor::PARSE_ERROR: |
| 98 |
case Teuchos::CommandLineProcessor::PARSE_UNRECOGNIZED_OPTION: return EXIT_FAILURE; |
| 99 |
case Teuchos::CommandLineProcessor::PARSE_SUCCESSFUL: break; |
| 100 |
} |
| 101 |
|
| 102 |
unsigned int seed = generateSeed(*comm, optSeed); |
| 103 |
Teuchos::ScalarTraits<SC>::seedrandom(seed); |
| 104 |
|
| 105 |
RCP<StackedTimer> timer = rcp(new StackedTimer("MatrixMatrix Multiply: Total")); |
| 106 |
TimeMonitor::setStackedTimer(timer); |
| 107 |
|
| 108 |
for (int jj = 0; jj < optNmults; ++jj) { |
| 109 |
RCP<Matrix> A; |
| 110 |
RCP<Matrix> B; |
| 111 |
{ |
| 112 |
TimeMonitor tm(*TimeMonitor::getNewTimer("MatrixMatrixMultiplyTest: 1 - Matrix creation")); |
| 113 |
|
| 114 |
size_t maxRowsPerProc = optMaxRowsPerProc; |
| 115 |
size_t minRowsPerProc = optMinRowsPerProc; |
| 116 |
if (minRowsPerProc > maxRowsPerProc) minRowsPerProc = maxRowsPerProc; |
| 117 |
|
| 118 |
// Create row map. This will also be used as the range map. |
| 119 |
RCP<const Map> rowMapForA = generateRandomContiguousMap<LO, GO, NO>(minRowsPerProc, maxRowsPerProc, comm, xpetraParameters.GetLib()); |
| 120 |
// Create domain map for A. This will also be the row map for B. |
| 121 |
RCP<const Map> domainMapForA = generateRandomContiguousMap<LO, GO, NO>(minRowsPerProc, maxRowsPerProc, comm, xpetraParameters.GetLib()); |
| 122 |
// Create domain map for B. |
| 123 |
RCP<const Map> domainMapForB = generateRandomContiguousMap<LO, GO, NO>(minRowsPerProc, maxRowsPerProc, comm, xpetraParameters.GetLib()); |
| 124 |
|
| 125 |
A = generateRandomMatrix<SC, LO, GO, NO>(optMinEntriesPerRow, optMaxEntriesPerRow, rowMapForA, domainMapForA); |
| 126 |
B = generateRandomMatrix<SC, LO, GO, NO>(optMinEntriesPerRow, optMaxEntriesPerRow, domainMapForA, domainMapForB); |
| 127 |
|
| 128 |
if (comm->getRank() == 0) { |
| 129 |
std::cout << "case " << jj << " of " << optNmults - 1 << " : " |
| 130 |
<< "seed = " << seed |
| 131 |
<< ", minEntriesPerRow = " << optMinEntriesPerRow |
| 132 |
<< ", maxEntriesPerRow = " << optMaxEntriesPerRow |
| 133 |
<< std::endl |
| 134 |
<< " A stats:" |
| 135 |
<< " #rows = " << rowMapForA->getGlobalNumElements() |
| 136 |
<< " #cols = " << domainMapForA->getGlobalNumElements() |
| 137 |
<< ", nnz = " << A->getGlobalNumEntries() |
| 138 |
<< std::endl |
| 139 |
<< " B stats:" |
| 140 |
<< " #rows = " << domainMapForA->getGlobalNumElements() |
| 141 |
<< " #cols = " << domainMapForB->getGlobalNumElements() |
| 142 |
<< ", nnz = " << B->getGlobalNumEntries() |
| 143 |
<< std::endl; |
| 144 |
} |
| 145 |
|
| 146 |
if (optDumpMatrices) { |
| 147 |
std::string fileName = "checkA.mm"; |
| 148 |
Xpetra::IO<SC, LO, GO, Node>::Write(fileName, *A); |
| 149 |
fileName = "checkB.mm"; |
| 150 |
Xpetra::IO<SC, LO, GO, Node>::Write(fileName, *B); |
| 151 |
} |
| 152 |
|
| 153 |
} // scope for timing matrix creation |
| 154 |
|
| 155 |
{ |
| 156 |
TimeMonitor tm(*TimeMonitor::getNewTimer("MatrixMatrixMultiplyTest: 2 - Multiply")); |
| 157 |
|
| 158 |
RCP<Matrix> AB; |
| 159 |
RCP<Teuchos::FancyOStream> fos = Teuchos::getFancyOStream(rcp(new Teuchos::oblackholestream())); |
| 160 |
AB = Xpetra::MatrixMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>::Multiply(*A, false, *B, false, *fos); |
| 161 |
// if (optDumpMatrices) { |
| 162 |
// std::string fileName="checkAB.mm"; |
| 163 |
// Utils::Write( fileName,*AB); |
| 164 |
// } |
| 165 |
|
| 166 |
} // scope for multiply |
| 167 |
|
| 168 |
seed = generateSeed(*comm); |
| 169 |
|
| 170 |
} // for (int jj=0; jj<optNmults; ++jj) |
| 171 |
|
| 172 |
timer->stopBaseTimer(); |
| 173 |
StackedTimer::OutputOptions options; |
| 174 |
options.print_warnings = false; |
| 175 |
timer->report(std::cout, comm, options); |
| 176 |
|
| 177 |
auto xmlOut = timer->reportWatchrXML(std::string("MueLu MatrixMatrix Multiply ") + std::to_string(comm->getSize()) + " ranks", comm); |
| 178 |
if (xmlOut.length()) |
| 179 |
std::cout << "\nAlso created Watchr performance report " << xmlOut << '\n'; |
| 180 |
|
| 181 |
if (comm->getRank() == 0) |
| 182 |
std::cout << "End Result: TEST PASSED"; |
| 183 |
|
| 184 |
return 0; |
| 185 |
} |
| 186 |
// main_ |
| 187 |
|
| 188 |
namespace MueLuTests { |
| 189 |
|
| 190 |
template <class T> |
| 191 |
size_t generateRandomNumber(T a, T b) { |
| 192 |
double rv = (Teuchos::ScalarTraits<double>::random() + 1) * 0.5; // shift "random" value from interval [-1,1] to [0,1] |
| 193 |
size_t numMyElements = Teuchos::as<size_t>(a + rv * (b - a)); |
| 194 |
return numMyElements; |
| 195 |
} |
| 196 |
|
| 197 |
//- -- -------------------------------------------------------- |
| 198 |
|
| 199 |
unsigned int generateSeed(const Teuchos::Comm<int> &comm, const double initSeed) { |
| 200 |
timeval t1; |
| 201 |
gettimeofday(&t1, NULL); |
| 202 |
|
| 203 |
unsigned int seed; |
| 204 |
if (initSeed > -1) |
| 205 |
seed = Teuchos::as<unsigned int>(initSeed); |
| 206 |
else |
| 207 |
seed = t1.tv_usec * t1.tv_sec; |
| 208 |
|
| 209 |
const Teuchos::MpiComm<int> *mpiComm = dynamic_cast<const Teuchos::MpiComm<int> *>(&comm); |
| 210 |
TEUCHOS_TEST_FOR_EXCEPTION(mpiComm == 0, MueLu::Exceptions::RuntimeError, "Cast to MpiComm failed"); |
| 211 |
|
| 212 |
// use variant of proc 0's seed so we can always reproduce the results |
| 213 |
MPI_Bcast((void *)&seed, 1, MPI_UNSIGNED, 0, *(mpiComm->getRawMpiComm())); |
| 214 |
seed = seed * (1 + comm.getRank()); |
| 215 |
|
| 216 |
return seed; |
| 217 |
} |
| 218 |
|
| 219 |
//- -- -------------------------------------------------------- |
| 220 |
template <class LocalOrdinal, class GlobalOrdinal, class Node> |
| 221 |
Teuchos::RCP<const Xpetra::Map<LocalOrdinal, GlobalOrdinal, Node> > generateRandomContiguousMap(size_t const minRowsPerProc, size_t const maxRowsPerProc, Teuchos::RCP<const Teuchos::Comm<int> > const &comm, Xpetra::UnderlyingLib const lib) { |
| 222 |
size_t numMyElements = generateRandomNumber(minRowsPerProc, maxRowsPerProc); |
| 223 |
Teuchos::RCP<const Xpetra::Map<LocalOrdinal, GlobalOrdinal, Node> > map = Xpetra::MapFactory<LocalOrdinal, GlobalOrdinal, Node>::createContigMap(lib, |
| 224 |
Teuchos::OrdinalTraits<size_t>::invalid(), |
| 225 |
numMyElements, |
| 226 |
comm); |
| 227 |
return map; |
| 228 |
} |
| 229 |
|
| 230 |
//- -- -------------------------------------------------------- |
| 231 |
template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node> |
| 232 |
Teuchos::RCP<Xpetra::Matrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> > generateRandomMatrix(LocalOrdinal const &minEntriesPerRow, LocalOrdinal const &maxEntriesPerRow, |
| 233 |
Teuchos::RCP<const Xpetra::Map<LocalOrdinal, GlobalOrdinal, Node> > &rowMap, |
| 234 |
Teuchos::RCP<const Xpetra::Map<LocalOrdinal, GlobalOrdinal, Node> > &domainMap) { |
| 235 |
using Teuchos::Array; |
| 236 |
using Teuchos::ArrayView; |
| 237 |
using Teuchos::RCP; |
| 238 |
#include <MueLu_UseShortNames.hpp> |
| 239 |
|
| 240 |
size_t numLocalRowsInA = rowMap->getLocalNumElements(); |
| 241 |
// Now allocate random number of entries per row for each processor, between minEntriesPerRow and maxEntriesPerRow |
| 242 |
Teuchos::ArrayRCP<size_t> eprData(numLocalRowsInA); |
| 243 |
for (Teuchos::ArrayRCP<size_t>::iterator i = eprData.begin(); i != eprData.end(); ++i) { |
| 244 |
*i = generateRandomNumber(minEntriesPerRow, maxEntriesPerRow); |
| 245 |
} |
| 246 |
|
| 247 |
// Populate CrsMatrix with random nnz per row and with random column locations. |
| 248 |
ArrayView<const GlobalOrdinal> myGlobalElements = rowMap->getLocalElementList(); |
| 249 |
GO numGlobalRows = rowMap->getGlobalNumElements(); |
| 250 |
|
| 251 |
LO realMaxEntriesPerRow = maxEntriesPerRow; |
| 252 |
if (maxEntriesPerRow > numGlobalRows) |
| 253 |
realMaxEntriesPerRow = numGlobalRows; |
| 254 |
RCP<Matrix> A = Teuchos::rcp(new Xpetra::CrsMatrixWrap<Scalar, LocalOrdinal, GlobalOrdinal, Node>(rowMap, eprData)); |
| 255 |
|
| 256 |
Array<Scalar> vals(realMaxEntriesPerRow); |
| 257 |
// stick in ones for values |
| 258 |
for (LO j = 0; j < realMaxEntriesPerRow; ++j) vals[j] = Teuchos::ScalarTraits<SC>::one(); |
| 259 |
Array<GO> cols(realMaxEntriesPerRow); |
| 260 |
for (size_t i = 0; i < numLocalRowsInA; ++i) { |
| 261 |
ArrayView<SC> av(&vals[0], eprData[i]); |
| 262 |
ArrayView<GO> iv(&cols[0], eprData[i]); |
| 263 |
for (size_t j = 0; j < eprData[i]; ++j) { |
| 264 |
// we assume there are no gaps in the indices |
| 265 |
cols[j] = Teuchos::as<GO>(generateRandomNumber(Teuchos::as<GO>(0), domainMap->getMaxAllGlobalIndex())); |
| 266 |
} |
| 267 |
A->insertGlobalValues(myGlobalElements[i], iv, av); |
| 268 |
} |
| 269 |
|
| 270 |
A->fillComplete(domainMap, rowMap); |
| 271 |
|
| 272 |
return A; |
| 273 |
} |
| 274 |
|
| 275 |
} // namespace MueLuTests |
| 276 |
|
| 277 |
//- -- -------------------------------------------------------- |
| 278 |
#define MUELU_AUTOMATIC_TEST_ETI_NAME main_ |
| 279 |
#include "MueLu_Test_ETI.hpp" |
| 280 |
|
| 281 |
int main(int argc, char *argv[]) { |
| 282 |
return Automatic_Test_ETI(argc, argv); |
| 283 |
} |