| 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 |
#include <algorithm> // shuffle |
| 12 |
#include <vector> // vector |
| 13 |
#include <random> |
| 14 |
|
| 15 |
#include <Xpetra_MultiVectorFactory.hpp> |
| 16 |
#include <Xpetra_IO.hpp> |
| 17 |
|
| 18 |
// Teuchos |
| 19 |
#include <Teuchos_StandardCatchMacros.hpp> |
| 20 |
|
| 21 |
// Galeri |
| 22 |
#include <Galeri_XpetraParameters.hpp> |
| 23 |
#include <Galeri_XpetraProblemFactory.hpp> |
| 24 |
#include <Galeri_XpetraUtils.hpp> |
| 25 |
#include <Galeri_XpetraMaps.hpp> |
| 26 |
// MueLu |
| 27 |
#include "MueLu.hpp" |
| 28 |
#include "MueLu_TestHelpers.hpp" |
| 29 |
|
| 30 |
#if defined(HAVE_TPETRA_INST_OPENMP) |
| 31 |
#include "Xpetra_TpetraVector.hpp" |
| 32 |
#include "TpetraExt_MatrixMatrix_def.hpp" |
| 33 |
#endif |
| 34 |
|
| 35 |
// ========================================================================= |
| 36 |
// Support Routines |
| 37 |
// ========================================================================= |
| 38 |
template <class View1, class View2> |
| 39 |
inline void copy_view_n(const int n, const View1 x1, View2 x2) { |
| 40 |
Kokkos::parallel_for( |
| 41 |
n, KOKKOS_LAMBDA(const size_t i) { |
| 42 |
x2[i] = x1[i]; |
| 43 |
}); |
| 44 |
} |
| 45 |
|
| 46 |
template <class View1, class View2> |
| 47 |
inline void copy_view(const View1 x1, View2 x2) { |
| 48 |
copy_view_n(x1.extent(0), x1, x2); |
| 49 |
} |
| 50 |
|
| 51 |
// ========================================================================= |
| 52 |
// MKL Testing |
| 53 |
// ========================================================================= |
| 54 |
#ifdef HAVE_MUELU_MKL |
| 55 |
#include "mkl.h" |
| 56 |
|
| 57 |
// mkl_sparse_spmm |
| 58 |
template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node> |
| 59 |
void Jacobi_MKL_SPMM(const Xpetra::Matrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> &A, const Xpetra::Matrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> &B, Xpetra::Matrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> &C, Xpetra::Vector<Scalar, LocalOrdinal, GlobalOrdinal, Node> &D, Scalar omega) { |
| 60 |
#include <MueLu_UseShortNames.hpp> |
| 61 |
using Teuchos::RCP; |
| 62 |
using Teuchos::rcp; |
| 63 |
using Teuchos::TimeMonitor; |
| 64 |
// NOTE: MKL uses int as its columnn index type and the either double or float for its Scalar type |
| 65 |
|
| 66 |
// Jacobi Does ( I - omega D A) B |
| 67 |
|
| 68 |
Xpetra::UnderlyingLib lib = A.getRowMap()->lib(); |
| 69 |
RCP<TimeMonitor> tm; |
| 70 |
typedef Tpetra::CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> crs_matrix_type; |
| 71 |
typedef Tpetra::Vector<Scalar, LocalOrdinal, GlobalOrdinal, Node> vector_type; |
| 72 |
typedef typename crs_matrix_type::local_matrix_device_type KCRS; |
| 73 |
typedef typename KCRS::StaticCrsGraphType graph_t; |
| 74 |
typedef typename graph_t::row_map_type::non_const_type lno_view_t; |
| 75 |
typedef typename graph_t::row_map_type::const_type c_lno_view_t; |
| 76 |
typedef typename graph_t::entries_type::non_const_type lno_nnz_view_t; |
| 77 |
typedef typename graph_t::entries_type::const_type c_lno_nnz_view_t; |
| 78 |
typedef typename KCRS::values_type::non_const_type scalar_view_t; |
| 79 |
|
| 80 |
typedef typename vector_type::device_type device_type; |
| 81 |
typedef typename Kokkos::View<MKL_INT *, typename lno_nnz_view_t::array_layout, typename lno_nnz_view_t::device_type> mkl_int_type; |
| 82 |
|
| 83 |
RCP<const crs_matrix_type> Au = toTpetra(rcp(&A, false)); |
| 84 |
RCP<const crs_matrix_type> Bu = toTpetra(rcp(&B, false)); |
| 85 |
RCP<const crs_matrix_type> Cu = toTpetra(rcp(&C, false)); |
| 86 |
RCP<crs_matrix_type> Cnc = Teuchos::rcp_const_cast<crs_matrix_type>(Cu); |
| 87 |
RCP<const vector_type> Du = Xpetra::toTpetra(D); |
| 88 |
|
| 89 |
const KCRS &Amat = Au->getLocalMatrixDevice(); |
| 90 |
const KCRS &Bmat = Bu->getLocalMatrixDevice(); |
| 91 |
|
| 92 |
if (A.getLocalNumRows() != C.getLocalNumRows()) throw std::runtime_error("C is not sized correctly"); |
| 93 |
|
| 94 |
c_lno_view_t Arowptr = Amat.graph.row_map, Browptr = Bmat.graph.row_map; |
| 95 |
lno_view_t Crowptr("Crowptr", C.getLocalNumRows() + 1); |
| 96 |
c_lno_nnz_view_t Acolind = Amat.graph.entries, Bcolind = Bmat.graph.entries; |
| 97 |
lno_nnz_view_t Ccolind; |
| 98 |
const scalar_view_t Avals = Amat.values, Bvals = Bmat.values; |
| 99 |
scalar_view_t Cvals; |
| 100 |
|
| 101 |
auto Dvals = Du->getLocalViewDevice(Tpetra::Access::ReadOnly); |
| 102 |
|
| 103 |
sparse_matrix_t AMKL; |
| 104 |
sparse_matrix_t BMKL; |
| 105 |
sparse_matrix_t CMKL; |
| 106 |
sparse_matrix_t DMKL; |
| 107 |
|
| 108 |
sparse_matrix_t XTempMKL, YTempMKL; |
| 109 |
|
| 110 |
sparse_status_t result; |
| 111 |
// ********************************** |
| 112 |
// Copy in the matrix data for MKL |
| 113 |
tm = rcp(new TimeMonitor(*TimeMonitor::getNewTimer("Jacobi MKL: Matrix CopyIn"))); |
| 114 |
|
| 115 |
mkl_int_type ArowptrMKL("Arowptr", Arowptr.extent(0)); |
| 116 |
mkl_int_type BrowptrMKL("Browptr", Browptr.extent(0)); |
| 117 |
|
| 118 |
mkl_int_type AcolindMKL("Acolind", Acolind.extent(0)); |
| 119 |
mkl_int_type BcolindMKL("Bcolind", Bcolind.extent(0)); |
| 120 |
|
| 121 |
copy_view(Arowptr, ArowptrMKL); |
| 122 |
copy_view(Browptr, BrowptrMKL); |
| 123 |
copy_view(Acolind, AcolindMKL); |
| 124 |
copy_view(Bcolind, BcolindMKL); |
| 125 |
|
| 126 |
if (std::is_same<Scalar, double>::value) { |
| 127 |
mkl_sparse_d_create_csr(&AMKL, SPARSE_INDEX_BASE_ZERO, Au->getLocalNumRows(), Au->getLocalNumCols(), ArowptrMKL.data(), ArowptrMKL.data() + 1, AcolindMKL.data(), (double *)Avals.data()); |
| 128 |
mkl_sparse_d_create_csr(&BMKL, SPARSE_INDEX_BASE_ZERO, Bu->getLocalNumRows(), Bu->getLocalNumCols(), BrowptrMKL.data(), BrowptrMKL.data() + 1, BcolindMKL.data(), (double *)Bvals.data()); |
| 129 |
} else |
| 130 |
throw std::runtime_error("MKL Type Mismatch"); |
| 131 |
tm = Teuchos::null; |
| 132 |
Au->getComm()->barrier(); |
| 133 |
|
| 134 |
// ********************************** |
| 135 |
// Copy in the vector-as-matrix data for MKL |
| 136 |
tm = rcp(new TimeMonitor(*TimeMonitor::getNewTimer("Jacobi MKL: Scaled D-Vector CopyIn"))); |
| 137 |
size_t n = Dvals.extent(0); |
| 138 |
mkl_int_type DrowptrMKL("Drowptr", n + 1); |
| 139 |
mkl_int_type DcolindMKL("Dcolind", n); |
| 140 |
scalar_view_t DvalsMKL("Dvals", n); |
| 141 |
Kokkos::parallel_for( |
| 142 |
n + 1, KOKKOS_LAMBDA(const size_t i) { |
| 143 |
DrowptrMKL[i] = i; |
| 144 |
if (i < n) { |
| 145 |
DcolindMKL[i] = i; |
| 146 |
DvalsMKL[i] = -omega * Dvals(i, 0); |
| 147 |
} |
| 148 |
}); |
| 149 |
|
| 150 |
// NOTE: No sanity checks here. We did that above |
| 151 |
mkl_sparse_d_create_csr(&DMKL, SPARSE_INDEX_BASE_ZERO, Dvals.extent(0), Dvals.extent(0), DrowptrMKL.data(), DrowptrMKL.data() + 1, DcolindMKL.data(), DvalsMKL.data()); |
| 152 |
|
| 153 |
tm = Teuchos::null; |
| 154 |
Au->getComm()->barrier(); |
| 155 |
|
| 156 |
// ********************************** |
| 157 |
// Multiply (A*B) |
| 158 |
tm = rcp(new TimeMonitor(*TimeMonitor::getNewTimer("Jacobi MKL: Multiply"))); |
| 159 |
result = mkl_sparse_spmm(SPARSE_OPERATION_NON_TRANSPOSE, AMKL, BMKL, &XTempMKL); |
| 160 |
typename KCRS::execution_space().fence(); |
| 161 |
if (result != SPARSE_STATUS_SUCCESS) throw std::runtime_error("MKL Multiply failed"); |
| 162 |
|
| 163 |
// ********************************** |
| 164 |
// Scale (-omegaD) * AB) |
| 165 |
tm = rcp(new TimeMonitor(*TimeMonitor::getNewTimer("Jacobi MKL: Scale-Via-Multiply"))); |
| 166 |
result = mkl_sparse_spmm(SPARSE_OPERATION_NON_TRANSPOSE, DMKL, XTempMKL, &YTempMKL); |
| 167 |
typename KCRS::execution_space().fence(); |
| 168 |
if (result != SPARSE_STATUS_SUCCESS) throw std::runtime_error("MKL Scale failed"); |
| 169 |
|
| 170 |
// ********************************** |
| 171 |
// Add B - ((-omegaD) * AB)) |
| 172 |
tm = rcp(new TimeMonitor(*TimeMonitor::getNewTimer("Jacobi MKL: Add"))); |
| 173 |
result = mkl_sparse_d_add(SPARSE_OPERATION_NON_TRANSPOSE, BMKL, 1.0, YTempMKL, &CMKL); |
| 174 |
typename KCRS::execution_space().fence(); |
| 175 |
if (result != SPARSE_STATUS_SUCCESS) throw std::runtime_error("MKL Add failed"); |
| 176 |
|
| 177 |
// ********************************** |
| 178 |
// Copy out the data for MKL |
| 179 |
tm = rcp(new TimeMonitor(*TimeMonitor::getNewTimer("Jacobi MKL: Copy Out"))); |
| 180 |
sparse_index_base_t c_indexing; |
| 181 |
MKL_INT c_rows, c_cols, *rows_start, *rows_end, *columns; |
| 182 |
double *values; |
| 183 |
mkl_sparse_d_export_csr(CMKL, &c_indexing, &c_rows, &c_cols, &rows_start, &rows_end, &columns, &values); |
| 184 |
size_t cnnz = rows_end[c_rows - 1]; |
| 185 |
Kokkos::resize(Ccolind, cnnz); |
| 186 |
Kokkos::resize(Cvals, cnnz); |
| 187 |
if (c_rows != A.getLocalNumRows() || c_rows + 1 != Crowptr.extent(0)) throw std::runtime_error("C row size mismatch"); |
| 188 |
copy_view_n(c_rows, rows_start, Crowptr); |
| 189 |
Crowptr(c_rows) = rows_end[c_rows - 1]; |
| 190 |
copy_view_n(cnnz, columns, Ccolind); |
| 191 |
copy_view_n(cnnz, values, Cvals); |
| 192 |
|
| 193 |
Cnc->replaceColMap(Bu->getColMap()); |
| 194 |
Cnc->setAllValues(Crowptr, |
| 195 |
Ccolind, |
| 196 |
Cvals); |
| 197 |
|
| 198 |
mkl_sparse_destroy(AMKL); |
| 199 |
mkl_sparse_destroy(BMKL); |
| 200 |
mkl_sparse_destroy(CMKL); |
| 201 |
mkl_sparse_destroy(XTempMKL); |
| 202 |
mkl_sparse_destroy(YTempMKL); |
| 203 |
|
| 204 |
tm = Teuchos::null; |
| 205 |
Au->getComm()->barrier(); |
| 206 |
} |
| 207 |
|
| 208 |
#endif |
| 209 |
|
| 210 |
// ========================================================================= |
| 211 |
// LTG Testing |
| 212 |
// ========================================================================= |
| 213 |
#include "Tpetra_Import_Util2.hpp" |
| 214 |
#include "TpetraExt_MatrixMatrix.hpp" |
| 215 |
#include "TpetraExt_MatrixMatrix_ExtraKernels_decl.hpp" |
| 216 |
#include "TpetraExt_MatrixMatrix_ExtraKernels_def.hpp" |
| 217 |
|
| 218 |
template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node> |
| 219 |
void Jacobi_Wrapper(const Xpetra::Matrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> &A, const Xpetra::Matrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> &B, Xpetra::Matrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> &C, const Xpetra::Vector<Scalar, LocalOrdinal, GlobalOrdinal, Node> &D, Scalar omega, std::string jacobi_algorithm_name, std::string spgemm_algorithm_name, int team_work_size) { |
| 220 |
#include <MueLu_UseShortNames.hpp> |
| 221 |
using Teuchos::RCP; |
| 222 |
using Teuchos::rcp; |
| 223 |
using Teuchos::TimeMonitor; |
| 224 |
|
| 225 |
Xpetra::UnderlyingLib lib = A.getRowMap()->lib(); |
| 226 |
RCP<TimeMonitor> tm; |
| 227 |
|
| 228 |
std::string name = jacobi_algorithm_name + "/" + spgemm_algorithm_name; |
| 229 |
|
| 230 |
if (lib == Xpetra::UseTpetra) { |
| 231 |
#if defined(HAVE_TPETRA_INST_OPENMP) |
| 232 |
typedef Tpetra::CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> crs_matrix_type; |
| 233 |
typedef Tpetra::Vector<Scalar, LocalOrdinal, GlobalOrdinal, Node> vector_type; |
| 234 |
typedef Tpetra::Import<LocalOrdinal, GlobalOrdinal, Node> import_type; |
| 235 |
typedef typename crs_matrix_type::local_matrix_device_type KCRS; |
| 236 |
typedef typename KCRS::device_type device_t; |
| 237 |
typedef typename KCRS::StaticCrsGraphType graph_t; |
| 238 |
typedef typename graph_t::row_map_type::non_const_type lno_view_t; |
| 239 |
typedef typename graph_t::entries_type::non_const_type lno_nnz_view_t; |
| 240 |
typedef typename KCRS::values_type::non_const_type scalar_view_t; |
| 241 |
typedef Kokkos::View<LO *, typename lno_view_t::array_layout, typename lno_view_t::device_type> lo_view_t; |
| 242 |
typedef Tpetra::Map<LO, GO, NO> map_type; |
| 243 |
typedef typename map_type::local_map_type local_map_type; |
| 244 |
typedef typename Node::execution_space execution_space; |
| 245 |
typedef Kokkos::RangePolicy<execution_space, size_t> range_type; |
| 246 |
LocalOrdinal LO_INVALID = Teuchos::OrdinalTraits<LO>::invalid(); |
| 247 |
RCP<const import_type> Cimport; |
| 248 |
RCP<const crs_matrix_type> Au = toTpetra(rcp(&A, false)); |
| 249 |
RCP<const crs_matrix_type> Bu = toTpetra(rcp(&B, false)); |
| 250 |
RCP<const crs_matrix_type> Cu = toTpetra(rcp(&C, false)); |
| 251 |
RCP<crs_matrix_type> Cnc = Teuchos::rcp_const_cast<crs_matrix_type>(Cu); |
| 252 |
|
| 253 |
RCP<const vector_type> Du = Xpetra::toTpetra(D); |
| 254 |
|
| 255 |
// ********************************** |
| 256 |
// Copy in the data for Jacobi Kernel Wrapper |
| 257 |
tm = rcp(new TimeMonitor(*TimeMonitor::getNewTimer("Jacobi " + name + ": CopyIn"))); |
| 258 |
|
| 259 |
Tpetra::CrsMatrixStruct<Scalar, LocalOrdinal, GlobalOrdinal, Node> Aview, Bview; |
| 260 |
Aview.origMatrix = Au; |
| 261 |
Aview.origRowMap = Au->getRowMap(); |
| 262 |
Aview.rowMap = Au->getRowMap(); |
| 263 |
Aview.colMap = Au->getColMap(); |
| 264 |
Aview.domainMap = Au->getDomainMap(); |
| 265 |
Aview.importColMap = Teuchos::null; |
| 266 |
|
| 267 |
Bview.origMatrix = Bu; |
| 268 |
Bview.origRowMap = Bu->getRowMap(); |
| 269 |
Bview.rowMap = Bu->getRowMap(); |
| 270 |
Bview.colMap = Bu->getColMap(); |
| 271 |
Bview.domainMap = Bu->getDomainMap(); |
| 272 |
Bview.importColMap = Teuchos::null; |
| 273 |
|
| 274 |
// Because we're in serial... |
| 275 |
Cnc->replaceColMap(Bu->getColMap()); |
| 276 |
|
| 277 |
// Bcol2Ccol is trivial |
| 278 |
lo_view_t Bcol2Ccol(Kokkos::ViewAllocateWithoutInitializing("Bcol2Ccol"), Bview.colMap->getLocalNumElements()), Icol2Ccol; |
| 279 |
const LO colMapSize = static_cast<LO>(Bview.colMap->getLocalNumElements()); |
| 280 |
Kokkos::parallel_for( |
| 281 |
"Tpetra::mult_A_B_newmatrix::Bcol2Ccol_fill", |
| 282 |
Kokkos::RangePolicy<execution_space, LO>(0, colMapSize), |
| 283 |
KOKKOS_LAMBDA(const LO i) { |
| 284 |
Bcol2Ccol(i) = i; |
| 285 |
}); |
| 286 |
|
| 287 |
// Acol2Brow |
| 288 |
local_map_type Acolmap_local = Aview.colMap->getLocalMap(); |
| 289 |
local_map_type Browmap_local = Bview.origMatrix->getRowMap()->getLocalMap(); |
| 290 |
lo_view_t targetMapToOrigRow(Kokkos::ViewAllocateWithoutInitializing("targetMapToOrigRow"), Aview.colMap->getLocalNumElements()); |
| 291 |
lo_view_t targetMapToImportRow; |
| 292 |
Kokkos::parallel_for( |
| 293 |
"Tpetra::mult_A_B_newmatrix::construct_tables", range_type(Aview.colMap->getMinLocalIndex(), Aview.colMap->getMaxLocalIndex() + 1), KOKKOS_LAMBDA(const LO i) { |
| 294 |
GO aidx = Acolmap_local.getGlobalElement(i); |
| 295 |
LO B_LID = Browmap_local.getLocalElement(aidx); |
| 296 |
if (B_LID != LO_INVALID) { |
| 297 |
targetMapToOrigRow(i) = B_LID; |
| 298 |
// targetMapToImportRow(i) = LO_INVALID; |
| 299 |
} else { |
| 300 |
// This shouldn't happen here |
| 301 |
} |
| 302 |
}); |
| 303 |
tm = Teuchos::null; |
| 304 |
Au->getComm()->barrier(); |
| 305 |
|
| 306 |
// ********************************** |
| 307 |
// Multiply |
| 308 |
Teuchos::RCP<Teuchos::ParameterList> params = Teuchos::rcp(new Teuchos::ParameterList); |
| 309 |
params->set("openmp: jacobi algorithm", jacobi_algorithm_name); |
| 310 |
params->set("openmp: algorithm", spgemm_algorithm_name); |
| 311 |
params->set("openmp: team work size", team_work_size); |
| 312 |
|
| 313 |
tm = rcp(new TimeMonitor(*TimeMonitor::getNewTimer(std::string("Jacobi ") + name + ": Kernel"))); |
| 314 |
Tpetra::MMdetails::KernelWrappers2<SC, LO, GO, Node, lno_nnz_view_t>::jacobi_A_B_newmatrix_kernel_wrapper(omega, *Du, Aview, Bview, targetMapToOrigRow, targetMapToImportRow, Bcol2Ccol, Icol2Ccol, *Cnc, Cimport, name, params); |
| 315 |
|
| 316 |
tm = Teuchos::null; |
| 317 |
Au->getComm()->barrier(); |
| 318 |
|
| 319 |
#endif |
| 320 |
} |
| 321 |
} |
| 322 |
|
| 323 |
// ========================================================================= |
| 324 |
// ========================================================================= |
| 325 |
// ========================================================================= |
| 326 |
template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node> |
| 327 |
int main_(Teuchos::CommandLineProcessor &clp, Xpetra::UnderlyingLib &lib, int argc, char *argv[]) { |
| 328 |
#include <MueLu_UseShortNames.hpp> |
| 329 |
|
| 330 |
using std::endl; |
| 331 |
using Teuchos::RCP; // reference count pointers |
| 332 |
using Teuchos::rcp; |
| 333 |
using Teuchos::TimeMonitor; |
| 334 |
|
| 335 |
bool success = false; |
| 336 |
bool verbose = true; |
| 337 |
try { |
| 338 |
RCP<const Teuchos::Comm<int> > comm = Teuchos::DefaultComm<int>::getComm(); |
| 339 |
|
| 340 |
// ========================================================================= |
| 341 |
// Convenient definitions |
| 342 |
// ========================================================================= |
| 343 |
// SC zero = Teuchos::ScalarTraits<SC>::zero(), one = Teuchos::ScalarTraits<SC>::one(); |
| 344 |
|
| 345 |
// Instead of checking each time for rank, create a rank 0 stream |
| 346 |
RCP<Teuchos::FancyOStream> pOut = Teuchos::fancyOStream(Teuchos::rcpFromRef(std::cout)); |
| 347 |
Teuchos::FancyOStream &out = *pOut; |
| 348 |
out.setOutputToRootOnly(0); |
| 349 |
|
| 350 |
// ========================================================================= |
| 351 |
// Parameters initialization |
| 352 |
// ========================================================================= |
| 353 |
// GO nx = 50, ny = 50, nz = 50; |
| 354 |
// Galeri::Xpetra::Parameters<GO> matrixParameters(clp, nx, ny, nz, "Laplace3D"); // manage parameters of the test case |
| 355 |
Xpetra::Parameters xpetraParameters(clp); // manage parameters of Xpetra |
| 356 |
|
| 357 |
bool printTimings = true; |
| 358 |
clp.setOption("timings", "notimings", &printTimings, "print timings to screen"); |
| 359 |
int nrepeat = 100; |
| 360 |
clp.setOption("nrepeat", &nrepeat, "repeat the experiment N times"); |
| 361 |
// the kernels |
| 362 |
bool do_mkl = true; |
| 363 |
bool do_kk_mem = true; |
| 364 |
bool do_kk_dense = true; |
| 365 |
bool do_kk_default = true; |
| 366 |
bool do_ltg_msak = true; |
| 367 |
bool do_ltg_jac = true; |
| 368 |
|
| 369 |
#ifndef HAVE_MUELU_MKL |
| 370 |
do_mkl = false; |
| 371 |
#endif |
| 372 |
clp.setOption("mkl", "nomkl", &do_mkl, "Evaluate MKL SpMM"); |
| 373 |
clp.setOption("kk_mem", "nokk_mem", &do_kk_mem, "Evaluate KK Mem"); |
| 374 |
clp.setOption("kk_dense", "nokk_dense", &do_kk_dense, "Evaluate KK Dense"); |
| 375 |
clp.setOption("kk_default", "nokk_default", &do_kk_default, "Evaluate KK Default"); |
| 376 |
clp.setOption("ltg_msak", "noltg_msak", &do_ltg_msak, "Evaluate LTG (MSAK)"); |
| 377 |
clp.setOption("ltg_jac", "noltg_jac", &do_ltg_jac, "Evaluate LTG (Jacobi)"); |
| 378 |
|
| 379 |
int kk_team_work_size = 16; |
| 380 |
|
| 381 |
std::string matrixFileNameA = "A.mm"; |
| 382 |
clp.setOption("matrixfileA", &matrixFileNameA, "matrix market file containing matrix"); |
| 383 |
std::string matrixFileNameB = "B.mm"; |
| 384 |
clp.setOption("matrixfileB", &matrixFileNameB, "matrix market file containing matrix"); |
| 385 |
|
| 386 |
switch (clp.parse(argc, argv)) { |
| 387 |
case Teuchos::CommandLineProcessor::PARSE_HELP_PRINTED: return EXIT_SUCCESS; |
| 388 |
case Teuchos::CommandLineProcessor::PARSE_ERROR: |
| 389 |
case Teuchos::CommandLineProcessor::PARSE_UNRECOGNIZED_OPTION: return EXIT_FAILURE; |
| 390 |
case Teuchos::CommandLineProcessor::PARSE_SUCCESSFUL: break; |
| 391 |
} |
| 392 |
|
| 393 |
#ifndef HAVE_MUELU_MKL |
| 394 |
if (do_mkl == true) { |
| 395 |
out << "MKL was requested, but this kernel is not available. Disabling..." << endl; |
| 396 |
do_mkl = false; |
| 397 |
} |
| 398 |
#endif |
| 399 |
|
| 400 |
// simple hack to randomize order of experiments |
| 401 |
enum class Experiments { MKL_SPMM = 0, |
| 402 |
KK_MEM, |
| 403 |
KK_DENSE, |
| 404 |
KK_DEFAULT, |
| 405 |
LTG_MSAK, |
| 406 |
LTG_JAC }; |
| 407 |
std::vector<Experiments> my_experiments; |
| 408 |
// add the experiments we will run |
| 409 |
|
| 410 |
#ifdef HAVE_MUELU_MKL |
| 411 |
if (do_mkl) my_experiments.push_back(Experiments::MKL_SPMM); // MKL SPMM |
| 412 |
#endif |
| 413 |
|
| 414 |
// assume these are available |
| 415 |
if (do_kk_mem) my_experiments.push_back(Experiments::KK_MEM); // KK Mem |
| 416 |
if (do_kk_dense) my_experiments.push_back(Experiments::KK_DENSE); // KK Dense |
| 417 |
if (do_kk_default) my_experiments.push_back(Experiments::KK_DEFAULT); // KK Default |
| 418 |
if (do_ltg_msak) my_experiments.push_back(Experiments::LTG_MSAK); // LTG MSAK |
| 419 |
if (do_ltg_jac) my_experiments.push_back(Experiments::LTG_JAC); // LTG Jacobi |
| 420 |
|
| 421 |
out << "========================================================" << endl |
| 422 |
<< xpetraParameters |
| 423 |
// << matrixParameters |
| 424 |
<< "========================================================" << endl |
| 425 |
<< "Template Types:" << endl |
| 426 |
<< " Scalar: " << Teuchos::demangleName(typeid(SC).name()) << endl |
| 427 |
<< " LocalOrdinal: " << Teuchos::demangleName(typeid(LO).name()) << endl |
| 428 |
<< " GlobalOrdinal: " << Teuchos::demangleName(typeid(GO).name()) << endl |
| 429 |
<< " Node: " << Teuchos::demangleName(typeid(NO).name()) << endl |
| 430 |
<< "Sizes:" << endl |
| 431 |
<< " Scalar: " << sizeof(SC) << endl |
| 432 |
<< " LocalOrdinal: " << sizeof(LO) << endl |
| 433 |
<< " GlobalOrdinal: " << sizeof(GO) << endl |
| 434 |
<< "========================================================" << endl |
| 435 |
<< "Matrix: " << Teuchos::demangleName(typeid(Matrix).name()) << endl |
| 436 |
<< "Vector: " << Teuchos::demangleName(typeid(Vector).name()) << endl |
| 437 |
<< "Hierarchy: " << Teuchos::demangleName(typeid(Hierarchy).name()) << endl |
| 438 |
<< "========================================================" << endl; |
| 439 |
|
| 440 |
#if defined(HAVE_TPETRA_INST_OPENMP) |
| 441 |
out << "Tpetra::KokkosCompat::KokkosOpenMPWrapperNode::execution_space().concurrency() = " << Tpetra::KokkosCompat::KokkosOpenMPWrapperNode::execution_space().concurrency() << endl |
| 442 |
<< "========================================================" << endl; |
| 443 |
#endif |
| 444 |
|
| 445 |
// At the moment, this test only runs on one MPI rank |
| 446 |
if (comm->getSize() != 1) exit(1); |
| 447 |
|
| 448 |
// ========================================================================= |
| 449 |
// Problem construction |
| 450 |
// ========================================================================= |
| 451 |
RCP<TimeMonitor> globalTimeMonitor = rcp(new TimeMonitor(*TimeMonitor::getNewTimer("MatrixRead: S - Global Time"))); |
| 452 |
|
| 453 |
comm->barrier(); |
| 454 |
|
| 455 |
RCP<Matrix> A = Xpetra::IO<SC, LO, GO, Node>::Read(std::string(matrixFileNameA), lib, comm); |
| 456 |
RCP<Matrix> B = Xpetra::IO<SC, LO, GO, Node>::Read(std::string(matrixFileNameB), lib, comm); |
| 457 |
RCP<Matrix> C; |
| 458 |
|
| 459 |
// Jacobi-specific |
| 460 |
RCP<Vector> Diagonal = Xpetra::VectorFactory<SC, LO, GO, Node>::Build(A->getRowMap(), true); |
| 461 |
A->getLocalDiagCopy(*Diagonal); |
| 462 |
RCP<Vector> D = Xpetra::VectorFactory<SC, LO, GO, Node>::Build(A->getRowMap(), true); |
| 463 |
D->reciprocal(*Diagonal); |
| 464 |
SC omega = (4.0 / 3.0) * Teuchos::ScalarTraits<Scalar>::one(); |
| 465 |
|
| 466 |
globalTimeMonitor = Teuchos::null; |
| 467 |
comm->barrier(); |
| 468 |
|
| 469 |
out << "Matrix Read complete." << endl |
| 470 |
<< "Matrix A:" << endl |
| 471 |
<< *A |
| 472 |
<< "========================================================" << endl |
| 473 |
<< "Matrix B:" << endl |
| 474 |
<< *B |
| 475 |
<< "========================================================" << endl; |
| 476 |
|
| 477 |
// random source |
| 478 |
std::random_device rd; |
| 479 |
std::mt19937 random_source(rd()); |
| 480 |
|
| 481 |
// no need for a barrier, because the randomization process uses a collective. |
| 482 |
if (!my_experiments.empty()) { |
| 483 |
for (int i = 0; i < nrepeat; i++) { |
| 484 |
// randomize the experiments |
| 485 |
if (comm->getRank() == 0) { |
| 486 |
std::shuffle(my_experiments.begin(), my_experiments.end(), random_source); |
| 487 |
} |
| 488 |
// Broadcast this ordering to the other processes |
| 489 |
comm->broadcast(0, |
| 490 |
static_cast<int>(sizeof(Experiments::LTG_MSAK) * my_experiments.size()), |
| 491 |
reinterpret_cast<char *>(my_experiments.data())); |
| 492 |
|
| 493 |
// loop over the randomized experiments |
| 494 |
for (const auto &experiment_id : my_experiments) { |
| 495 |
switch (experiment_id) { |
| 496 |
// MKL_SPMM |
| 497 |
case Experiments::MKL_SPMM: |
| 498 |
#ifdef HAVE_MUELU_MKL |
| 499 |
C = Xpetra::MatrixFactory<SC, LO, GO, Node>::Build(A->getRowMap(), 0); |
| 500 |
{ |
| 501 |
TimeMonitor t(*TimeMonitor::getNewTimer("JAC MKL: Total")); |
| 502 |
Jacobi_MKL_SPMM(*A, *B, *C, *D, omega); |
| 503 |
} |
| 504 |
#endif |
| 505 |
break; |
| 506 |
// KK Algorithms (KK Memory) |
| 507 |
case Experiments::KK_MEM: |
| 508 |
C = Xpetra::MatrixFactory<SC, LO, GO, Node>::Build(A->getRowMap(), 0); |
| 509 |
{ |
| 510 |
TimeMonitor t(*TimeMonitor::getNewTimer("JAC MSAK/SPGEMM_KK_MEMORY: Total")); |
| 511 |
Jacobi_Wrapper(*A, *B, *C, *D, omega, std::string("MSAK"), std::string("SPGEMM_KK_MEMORY"), kk_team_work_size); |
| 512 |
} |
| 513 |
break; |
| 514 |
// KK Algorithms (KK Dense) |
| 515 |
case Experiments::KK_DENSE: |
| 516 |
C = Xpetra::MatrixFactory<SC, LO, GO, Node>::Build(A->getRowMap(), 0); |
| 517 |
{ |
| 518 |
TimeMonitor t(*TimeMonitor::getNewTimer("JAC MSAK/SPGEMM_KK_DENSE: Total")); |
| 519 |
Jacobi_Wrapper(*A, *B, *C, *D, omega, std::string("MSAK"), std::string("SPGEMM_KK_DENSE"), kk_team_work_size); |
| 520 |
} |
| 521 |
break; |
| 522 |
// KK Algorithms (KK Default) |
| 523 |
case Experiments::KK_DEFAULT: |
| 524 |
C = Xpetra::MatrixFactory<SC, LO, GO, Node>::Build(A->getRowMap(), 0); |
| 525 |
{ |
| 526 |
TimeMonitor t(*TimeMonitor::getNewTimer("JAC SPGEMM_KK: Total")); |
| 527 |
Jacobi_Wrapper(*A, *B, *C, *D, omega, std::string("MSAK"), std::string("SPGEMM_KK"), kk_team_work_size); |
| 528 |
} |
| 529 |
break; |
| 530 |
// LTG (MSAK) |
| 531 |
case Experiments::LTG_MSAK: |
| 532 |
C = Xpetra::MatrixFactory<SC, LO, GO, Node>::Build(A->getRowMap(), 0); |
| 533 |
{ |
| 534 |
TimeMonitor t(*TimeMonitor::getNewTimer("JAC MSAK/LTG: Total")); |
| 535 |
Jacobi_Wrapper(*A, *B, *C, *D, omega, std::string("MSAK"), std::string("LTG"), 0); |
| 536 |
} |
| 537 |
break; |
| 538 |
// LTG (Jacobi) |
| 539 |
case Experiments::LTG_JAC: |
| 540 |
C = Xpetra::MatrixFactory<SC, LO, GO, Node>::Build(A->getRowMap(), 0); |
| 541 |
{ |
| 542 |
TimeMonitor t(*TimeMonitor::getNewTimer("JAC JLTG: Total")); |
| 543 |
Jacobi_Wrapper(*A, *B, *C, *D, omega, std::string("LTG"), std::string("--"), 0); |
| 544 |
} |
| 545 |
break; |
| 546 |
|
| 547 |
default: |
| 548 |
std::cerr << "Unknown experiment ID encountered: " << (int)experiment_id << std::endl; |
| 549 |
} |
| 550 |
comm->barrier(); |
| 551 |
} // end random exp loop |
| 552 |
} // end repeat |
| 553 |
} // end ! my_experiments.empty() |
| 554 |
|
| 555 |
if (printTimings) { |
| 556 |
TimeMonitor::summarize(A->getRowMap()->getComm().ptr(), std::cout, false, true, false, Teuchos::Union, "", true); |
| 557 |
} |
| 558 |
|
| 559 |
success = true; |
| 560 |
} |
| 561 |
TEUCHOS_STANDARD_CATCH_STATEMENTS(verbose, std::cerr, success); |
| 562 |
|
| 563 |
return (success ? EXIT_SUCCESS : EXIT_FAILURE); |
| 564 |
} // main |
| 565 |
|
| 566 |
//- -- -------------------------------------------------------- |
| 567 |
#define MUELU_AUTOMATIC_TEST_ETI_NAME main_ |
| 568 |
#include "MueLu_Test_ETI.hpp" |
| 569 |
|
| 570 |
int main(int argc, char *argv[]) { |
| 571 |
return Automatic_Test_ETI(argc, argv); |
| 572 |
} |