| 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 |
#include <Teuchos_StackedTimer.hpp> |
| 21 |
|
| 22 |
// Galeri |
| 23 |
#include <Galeri_XpetraParameters.hpp> |
| 24 |
#include <Galeri_XpetraProblemFactory.hpp> |
| 25 |
#include <Galeri_XpetraUtils.hpp> |
| 26 |
#include <Galeri_XpetraMaps.hpp> |
| 27 |
// MueLu |
| 28 |
#include "MueLu.hpp" |
| 29 |
#include "MueLu_TestHelpers.hpp" |
| 30 |
#include "MueLu_PerfModels.hpp" |
| 31 |
#include "MueLu_PerfModelReporter.hpp" |
| 32 |
#include <MatrixLoad.hpp> |
| 33 |
|
| 34 |
#include "Xpetra_TpetraMultiVector.hpp" |
| 35 |
#include "Xpetra_TpetraImport.hpp" |
| 36 |
#include "Tpetra_CrsMatrix.hpp" |
| 37 |
#include "Tpetra_MultiVector.hpp" |
| 38 |
#include "KokkosSparse_spmv.hpp" |
| 39 |
#include "Kokkos_Core.hpp" |
| 40 |
|
| 41 |
#if defined(HAVE_MUELU_CUSPARSE) |
| 42 |
#include "cublas_v2.h" |
| 43 |
#include "cusparse.h" |
| 44 |
#endif |
| 45 |
|
| 46 |
#if defined(HAVE_MUELU_HYPRE) |
| 47 |
#include "HYPRE_IJ_mv.h" |
| 48 |
#include "HYPRE_parcsr_ls.h" |
| 49 |
#include "HYPRE_parcsr_mv.h" |
| 50 |
#include "HYPRE.h" |
| 51 |
#endif |
| 52 |
|
| 53 |
#if defined(HAVE_MUELU_PETSC) |
| 54 |
#include "petscksp.h" |
| 55 |
#endif |
| 56 |
|
| 57 |
Teuchos::RCP<Teuchos::StackedTimer> stacked_timer; |
| 58 |
Teuchos::RCP<Teuchos::TimeMonitor> globalTimeMonitor; |
| 59 |
|
| 60 |
// ========================================================================= |
| 61 |
// Support Routines |
| 62 |
// ========================================================================= |
| 63 |
|
| 64 |
template <class View1, class View2> |
| 65 |
inline void copy_view_n(const int n, const View1 x1, View2 x2) { |
| 66 |
Kokkos::parallel_for( |
| 67 |
n, KOKKOS_LAMBDA(const size_t i) { |
| 68 |
x2[i] = x1[i]; |
| 69 |
}); |
| 70 |
} |
| 71 |
|
| 72 |
template <class View1, class View2> |
| 73 |
inline void copy_view(const View1 x1, View2 x2) { |
| 74 |
copy_view_n(x1.extent(0), x1, x2); |
| 75 |
} |
| 76 |
|
| 77 |
template <class V1, class V2> |
| 78 |
void print_crs_graph(std::string name, const V1 rowptr, const V2 colind) { |
| 79 |
printf("%s rowptr[%d] = ", name.c_str(), rowptr.extent(0)); |
| 80 |
for (size_t i = 0; i < rowptr.extent(0); i++) |
| 81 |
printf(" %d", (int)rowptr[i]); |
| 82 |
printf("\n%s colind[%d] = ", name.c_str(), colind.extent(0)); |
| 83 |
for (size_t i = 0; i < colind.extent(0); i++) |
| 84 |
printf(" %d", (int)colind[i]); |
| 85 |
printf("\n"); |
| 86 |
} |
| 87 |
|
| 88 |
//============================================================================== |
| 89 |
template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node> |
| 90 |
void MakeContiguousMaps(const Tpetra::CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>& Matrix, |
| 91 |
Teuchos::RCP<const Tpetra::Map<LocalOrdinal, GlobalOrdinal, Node> >& ContiguousRowMap, |
| 92 |
Teuchos::RCP<const Tpetra::Map<LocalOrdinal, GlobalOrdinal, Node> >& ContiguousColumnMap, |
| 93 |
Teuchos::RCP<const Tpetra::Map<LocalOrdinal, GlobalOrdinal, Node> >& ContiguousDomainMap) { |
| 94 |
using Teuchos::rcp; |
| 95 |
using Teuchos::RCP; |
| 96 |
using LO = LocalOrdinal; |
| 97 |
using GO = GlobalOrdinal; |
| 98 |
using NO = Node; |
| 99 |
using map_type = Tpetra::Map<LO, GO, NO>; |
| 100 |
using import_type = Tpetra::Import<LO, GO, NO>; |
| 101 |
using go_vector_type = Tpetra::Vector<GO, LO, GO, NO>; |
| 102 |
|
| 103 |
// Must create GloballyContiguous DomainMap (which is a permutation of Matrix_'s |
| 104 |
// DomainMap) and the corresponding permuted ColumnMap. |
| 105 |
// Epetra_GID ---------> LID ----------> HYPRE_GID |
| 106 |
// via DomainMap.LID() via GloballyContiguousDomainMap.GID() |
| 107 |
RCP<const map_type> RowMap = Matrix.getRowMap(); |
| 108 |
RCP<const map_type> DomainMap = Matrix.getDomainMap(); |
| 109 |
RCP<const map_type> ColumnMap = Matrix.getColMap(); |
| 110 |
RCP<const import_type> importer = Matrix.getGraph()->getImporter(); |
| 111 |
|
| 112 |
if (RowMap->isContiguous()) { |
| 113 |
// If the row map is linear, then we can just use the row map as is. |
| 114 |
ContiguousRowMap = RowMap; |
| 115 |
} else { |
| 116 |
// The row map isn't linear, so we need a new row map |
| 117 |
ContiguousRowMap = rcp(new map_type(Matrix.getRowMap()->getGlobalNumElements(), |
| 118 |
Matrix.getRowMap()->getLocalNumElements(), 0, Matrix.getRowMap()->getComm())); |
| 119 |
} |
| 120 |
|
| 121 |
if (DomainMap->isContiguous()) { |
| 122 |
// If the domain map is linear, then we can just use the column map as is. |
| 123 |
ContiguousDomainMap = DomainMap; |
| 124 |
ContiguousColumnMap = ColumnMap; |
| 125 |
} else { |
| 126 |
// The domain map isn't linear, so we need a new domain map |
| 127 |
ContiguousDomainMap = rcp(new map_type(DomainMap->getGlobalNumElements(), |
| 128 |
DomainMap->getLocalNumElements(), 0, DomainMap->getComm())); |
| 129 |
if (importer) { |
| 130 |
// If there's an importer then we can use it to get a new column map |
| 131 |
go_vector_type MyGIDsHYPRE(DomainMap, ContiguousDomainMap->getLocalElementList()); |
| 132 |
|
| 133 |
// import the HYPRE GIDs |
| 134 |
go_vector_type ColGIDsHYPRE(ColumnMap); |
| 135 |
ColGIDsHYPRE.doImport(MyGIDsHYPRE, *importer, Tpetra::INSERT); |
| 136 |
|
| 137 |
// Make a HYPRE numbering-based column map. |
| 138 |
ContiguousColumnMap = rcp(new map_type(ColumnMap->getGlobalNumElements(), ColGIDsHYPRE.getDataNonConst()(), 0, ColumnMap->getComm())); |
| 139 |
} else { |
| 140 |
// The problem has matching domain/column maps, and somehow the domain map isn't linear, so just use the new domain map |
| 141 |
ContiguousColumnMap = rcp(new map_type(ColumnMap->getGlobalNumElements(), ContiguousDomainMap->getLocalElementList(), 0, ColumnMap->getComm())); |
| 142 |
} |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
// ========================================================================= |
| 147 |
// PETSC Testing |
| 148 |
// ========================================================================= |
| 149 |
#if defined(HAVE_MUELU_PETSC) && defined(HAVE_MPI) |
| 150 |
|
| 151 |
/*#define PETSC_CHK_ERR(x) { \ |
| 152 |
PetscCall(x); \ |
| 153 |
}*/ |
| 154 |
|
| 155 |
template <typename Scalar, typename LocalOrdinal, typename GlobalOrdinal, typename Node> |
| 156 |
class Petsc_SpmV_Pack { |
| 157 |
typedef Tpetra::CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> crs_matrix_type; |
| 158 |
typedef Tpetra::MultiVector<Scalar, LocalOrdinal, GlobalOrdinal, Node> vector_type; |
| 159 |
typedef Tpetra::Map<LocalOrdinal, GlobalOrdinal, Node> map_type; |
| 160 |
|
| 161 |
public: |
| 162 |
Petsc_SpmV_Pack(const crs_matrix_type& A, |
| 163 |
const vector_type& X, |
| 164 |
vector_type& Y) { |
| 165 |
using LO = LocalOrdinal; |
| 166 |
const MPI_Comm& comm = *Teuchos::rcp_dynamic_cast<const Teuchos::MpiComm<int> >(A.getRowMap()->getComm())->getRawMpiComm(); |
| 167 |
LO Nx = (LO)X.getMap()->getLocalNumElements(); |
| 168 |
LO Ny = (LO)Y.getMap()->getLocalNumElements(); |
| 169 |
|
| 170 |
// NOTE: Petsc requires contiguous GIDs for the row map |
| 171 |
Teuchos::RCP<const map_type> c_row_map, c_col_map, c_domain_map; |
| 172 |
MakeContiguousMaps(A, c_row_map, c_col_map, c_domain_map); |
| 173 |
|
| 174 |
// Note: PETSC appears to favor local indices for vector insertion |
| 175 |
std::vector<PetscInt> l_indices(std::max(Nx, Ny)); |
| 176 |
for (int i = 0; i < std::max(Nx, Ny); i++) |
| 177 |
l_indices[i] = i; |
| 178 |
|
| 179 |
// x vector |
| 180 |
VecCreate(comm, &x_p); |
| 181 |
VecSetType(x_p, VECMPI); |
| 182 |
VecSetSizes(x_p, Nx, X.getMap()->getGlobalNumElements()); |
| 183 |
VecSetValues(x_p, Nx, l_indices.data(), X.getData(0).getRawPtr(), INSERT_VALUES); |
| 184 |
VecAssemblyBegin(x_p); |
| 185 |
VecAssemblyEnd(x_p); |
| 186 |
|
| 187 |
// y vector |
| 188 |
VecCreate(comm, &y_p); |
| 189 |
VecSetType(y_p, VECMPI); |
| 190 |
VecSetSizes(y_p, Ny, Y.getMap()->getGlobalNumElements()); |
| 191 |
VecSetValues(y_p, Ny, l_indices.data(), Y.getData(0).getRawPtr(), INSERT_VALUES); |
| 192 |
VecAssemblyBegin(y_p); |
| 193 |
VecAssemblyEnd(y_p); |
| 194 |
|
| 195 |
// A matrix |
| 196 |
// NOTE: This is an overallocation, but that's OK |
| 197 |
PetscInt max_nnz = A.getLocalMaxNumRowEntries(); |
| 198 |
MatCreateAIJ(comm, c_row_map->getLocalNumElements(), c_domain_map->getLocalNumElements(), PETSC_DECIDE, PETSC_DECIDE, |
| 199 |
max_nnz, NULL, max_nnz, NULL, &A_p); |
| 200 |
|
| 201 |
std::vector<PetscInt> new_indices(max_nnz); |
| 202 |
for (LO i = 0; i < (LO)A.getLocalNumRows(); i++) { |
| 203 |
typename crs_matrix_type::values_host_view_type values; |
| 204 |
typename crs_matrix_type::local_inds_host_view_type indices; |
| 205 |
A.getLocalRowView(i, indices, values); |
| 206 |
for (LO j = 0; j < (LO)indices.extent(0); j++) { |
| 207 |
new_indices[j] = c_col_map->getGlobalElement(indices(j)); |
| 208 |
} |
| 209 |
PetscInt GlobalRow[1]; |
| 210 |
PetscInt numEntries = (PetscInt)indices.extent(0); |
| 211 |
GlobalRow[0] = c_row_map->getGlobalElement(i); |
| 212 |
|
| 213 |
MatSetValues(A_p, 1, GlobalRow, numEntries, new_indices.data(), values.data(), INSERT_VALUES); |
| 214 |
} |
| 215 |
MatAssemblyBegin(A_p, MAT_FINAL_ASSEMBLY); |
| 216 |
MatAssemblyEnd(A_p, MAT_FINAL_ASSEMBLY); |
| 217 |
} |
| 218 |
|
| 219 |
~Petsc_SpmV_Pack() { |
| 220 |
VecDestroy(&x_p); |
| 221 |
VecDestroy(&y_p); |
| 222 |
MatDestroy(&A_p); |
| 223 |
} |
| 224 |
|
| 225 |
bool spmv(const Scalar alpha, const Scalar beta) { |
| 226 |
int rv = MatMult(A_p, x_p, y_p); |
| 227 |
Kokkos::fence(); |
| 228 |
return (rv != 0); |
| 229 |
} |
| 230 |
|
| 231 |
private: |
| 232 |
Mat A_p; |
| 233 |
Vec x_p, y_p; |
| 234 |
}; |
| 235 |
|
| 236 |
#endif |
| 237 |
|
| 238 |
// ========================================================================= |
| 239 |
// HYPRE Testing |
| 240 |
// ========================================================================= |
| 241 |
#if defined(HAVE_MUELU_HYPRE) && defined(HAVE_MPI) |
| 242 |
|
| 243 |
template <typename Scalar, typename LocalOrdinal, typename GlobalOrdinal, typename Node> |
| 244 |
class HYPRE_SpmV_Pack { |
| 245 |
typedef Tpetra::CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> crs_matrix_type; |
| 246 |
typedef Tpetra::MultiVector<Scalar, LocalOrdinal, GlobalOrdinal, Node> vector_type; |
| 247 |
typedef Tpetra::Map<LocalOrdinal, GlobalOrdinal, Node> map_type; |
| 248 |
|
| 249 |
public: |
| 250 |
HYPRE_SpmV_Pack(const crs_matrix_type& A, |
| 251 |
const vector_type& X, |
| 252 |
vector_type& Y) { |
| 253 |
using LO = LocalOrdinal; |
| 254 |
using GO = GlobalOrdinal; |
| 255 |
|
| 256 |
// Time to copy the matrix / vector over to HYPRE datastructures |
| 257 |
const MPI_Comm& comm = *Teuchos::rcp_dynamic_cast<const Teuchos::MpiComm<int> >(A.getRowMap()->getComm())->getRawMpiComm(); |
| 258 |
|
| 259 |
// NOTE: Hypre requires contiguous GIDs for the row map |
| 260 |
Teuchos::RCP<const map_type> c_row_map, c_col_map, c_domain_map; |
| 261 |
MakeContiguousMaps(A, c_row_map, c_col_map, c_domain_map); |
| 262 |
|
| 263 |
// Create matrix |
| 264 |
int row_lo = c_row_map->getMinGlobalIndex(); |
| 265 |
int row_hi = c_row_map->getMaxGlobalIndex(); |
| 266 |
int dom_lo = c_domain_map->getMinGlobalIndex(); |
| 267 |
int dom_hi = c_domain_map->getMaxGlobalIndex(); |
| 268 |
HYPRE_IJMatrixCreate(comm, row_lo, row_hi, dom_lo, dom_hi, &ij_matrix); |
| 269 |
HYPRE_IJMatrixSetObjectType(ij_matrix, HYPRE_PARCSR); |
| 270 |
HYPRE_IJMatrixInitialize(ij_matrix); |
| 271 |
|
| 272 |
// Fill matrix |
| 273 |
std::vector<GO> new_indices(A.getLocalMaxNumRowEntries()); |
| 274 |
for (LO i = 0; i < (LO)A.getLocalNumRows(); i++) { |
| 275 |
typename crs_matrix_type::values_host_view_type values; |
| 276 |
typename crs_matrix_type::local_inds_host_view_type indices; |
| 277 |
A.getLocalRowView(i, indices, values); |
| 278 |
for (LO j = 0; j < (LO)indices.extent(0); j++) { |
| 279 |
new_indices[j] = c_col_map->getGlobalElement(indices(j)); |
| 280 |
} |
| 281 |
GO GlobalRow[1]; |
| 282 |
GO numEntries = (GO)indices.extent(0); |
| 283 |
GlobalRow[0] = c_row_map->getGlobalElement(i); |
| 284 |
HYPRE_IJMatrixSetValues(ij_matrix, 1, &numEntries, GlobalRow, new_indices.data(), values.data()); |
| 285 |
} |
| 286 |
HYPRE_IJMatrixAssemble(ij_matrix); |
| 287 |
HYPRE_IJMatrixGetObject(ij_matrix, (void**)&parcsr_matrix); |
| 288 |
|
| 289 |
// Now the x vector |
| 290 |
GO* dom_indices = const_cast<GO*>(c_domain_map->getLocalElementList().getRawPtr()); |
| 291 |
HYPRE_IJVectorCreate(comm, dom_lo, dom_hi, &x_ij); |
| 292 |
HYPRE_IJVectorSetObjectType(x_ij, HYPRE_PARCSR); |
| 293 |
HYPRE_IJVectorInitialize(x_ij); |
| 294 |
HYPRE_IJVectorSetValues(x_ij, X.getLocalLength(), dom_indices, const_cast<vector_type*>(&X)->getDataNonConst(0).getRawPtr()); |
| 295 |
HYPRE_IJVectorAssemble(x_ij); |
| 296 |
HYPRE_IJVectorGetObject(x_ij, (void**)&x_par); |
| 297 |
|
| 298 |
// Now the y vector |
| 299 |
GO* row_indices = const_cast<GO*>(c_row_map->getLocalElementList().getRawPtr()); |
| 300 |
HYPRE_IJVectorCreate(comm, row_lo, row_hi, &y_ij); |
| 301 |
HYPRE_IJVectorSetObjectType(y_ij, HYPRE_PARCSR); |
| 302 |
HYPRE_IJVectorInitialize(y_ij); |
| 303 |
HYPRE_IJVectorSetValues(y_ij, Y.getLocalLength(), row_indices, Y.getDataNonConst(0).getRawPtr()); |
| 304 |
HYPRE_IJVectorAssemble(y_ij); |
| 305 |
HYPRE_IJVectorGetObject(y_ij, (void**)&y_par); |
| 306 |
} |
| 307 |
|
| 308 |
~HYPRE_SpmV_Pack() { |
| 309 |
HYPRE_IJMatrixDestroy(ij_matrix); |
| 310 |
HYPRE_IJVectorDestroy(x_ij); |
| 311 |
HYPRE_IJVectorDestroy(y_ij); |
| 312 |
} |
| 313 |
|
| 314 |
bool spmv(const Scalar alpha, const Scalar beta) { |
| 315 |
int rv = HYPRE_ParCSRMatrixMatvec(alpha, parcsr_matrix, x_par, beta, y_par); |
| 316 |
Kokkos::fence(); |
| 317 |
return (rv != 0); |
| 318 |
} |
| 319 |
|
| 320 |
private: |
| 321 |
// NOTE: The par matrix/vectors are just pointers to the internal data of the ij guys. They do not need to |
| 322 |
// be deallocated on their own. |
| 323 |
HYPRE_IJMatrix ij_matrix; |
| 324 |
HYPRE_ParCSRMatrix parcsr_matrix; |
| 325 |
|
| 326 |
HYPRE_IJVector x_ij, y_ij; |
| 327 |
HYPRE_ParVector x_par, y_par; |
| 328 |
}; |
| 329 |
|
| 330 |
#endif |
| 331 |
|
| 332 |
// ========================================================================= |
| 333 |
// MAGMASparse Testing |
| 334 |
// ========================================================================= |
| 335 |
#if defined(HAVE_MUELU_MAGMASPARSE) |
| 336 |
#include <magma_v2.h> |
| 337 |
#include <magmasparse.h> |
| 338 |
template <typename Scalar, typename LocalOrdinal, typename GlobalOrdinal, typename Node> |
| 339 |
class MagmaSparse_SpmV_Pack { |
| 340 |
typedef Tpetra::CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> crs_matrix_type; |
| 341 |
typedef Tpetra::MultiVector<Scalar, LocalOrdinal, GlobalOrdinal, Node> vector_type; |
| 342 |
|
| 343 |
public: |
| 344 |
MagmaSparse_SpmV_Pack(const crs_matrix_type& A, |
| 345 |
const vector_type& X, |
| 346 |
vector_type& Y) {} |
| 347 |
|
| 348 |
~MagmaSparse_SpmV_Pack(){}; |
| 349 |
|
| 350 |
bool spmv(const Scalar alpha, const Scalar beta) { return (true); } |
| 351 |
}; |
| 352 |
|
| 353 |
template <typename LocalOrdinal, typename GlobalOrdinal> |
| 354 |
class MagmaSparse_SpmV_Pack<double, LocalOrdinal, GlobalOrdinal, Tpetra::KokkosCompat::KokkosCudaWrapperNode> { |
| 355 |
// typedefs shared among other TPLs |
| 356 |
typedef double Scalar; |
| 357 |
typedef typename Tpetra::KokkosCompat::KokkosCudaWrapperNode Node; |
| 358 |
typedef Tpetra::CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> crs_matrix_type; |
| 359 |
typedef Tpetra::MultiVector<Scalar, LocalOrdinal, GlobalOrdinal, Node> vector_type; |
| 360 |
typedef typename crs_matrix_type::local_matrix_host_type KCRS; |
| 361 |
typedef typename KCRS::StaticCrsGraphType graph_t; |
| 362 |
typedef typename graph_t::row_map_type::non_const_type lno_view_t; |
| 363 |
typedef typename graph_t::row_map_type::const_type c_lno_view_t; |
| 364 |
typedef typename graph_t::entries_type::non_const_type lno_nnz_view_t; |
| 365 |
typedef typename graph_t::entries_type::const_type c_lno_nnz_view_t; |
| 366 |
typedef typename KCRS::values_type::non_const_type scalar_view_t; |
| 367 |
typedef typename Node::device_type device_type; |
| 368 |
|
| 369 |
typedef typename Kokkos::View<int*, |
| 370 |
typename lno_nnz_view_t::array_layout, |
| 371 |
typename lno_nnz_view_t::device_type> |
| 372 |
int_array_type; |
| 373 |
|
| 374 |
public: |
| 375 |
MagmaSparse_SpmV_Pack(const crs_matrix_type& A, |
| 376 |
const vector_type& X, |
| 377 |
vector_type& Y) { |
| 378 |
// data access common to other TPLs |
| 379 |
const KCRS& Amat = A.getLocalMatrixHost(); |
| 380 |
c_lno_view_t Arowptr = Amat.graph.row_map; |
| 381 |
c_lno_nnz_view_t Acolind = Amat.graph.entries; |
| 382 |
const scalar_view_t Avals = Amat.values; |
| 383 |
|
| 384 |
// type conversion |
| 385 |
Arowptr_int = int_array_type("Arowptr", Arowptr.extent(0)); |
| 386 |
// copy the ordinals into the local view (type conversion) |
| 387 |
copy_view(Arowptr, Arowptr_int); |
| 388 |
|
| 389 |
m = static_cast<int>(A.getLocalNumRows()); |
| 390 |
n = static_cast<int>(A.getLocalNumCols()); |
| 391 |
nnz = static_cast<int>(Acolind.extent(0)); |
| 392 |
vals = reinterpret_cast<Scalar*>(Avals.data()); |
| 393 |
cols = const_cast<int*>(reinterpret_cast<const int*>(Acolind.data())); |
| 394 |
rowptr = reinterpret_cast<int*>(Arowptr_int.data()); |
| 395 |
|
| 396 |
auto X_lcl = X.template getLocalView<device_type>(); |
| 397 |
auto Y_lcl = Y.template getLocalView<device_type>(); |
| 398 |
x = reinterpret_cast<Scalar*>(X_lcl.data()); |
| 399 |
y = reinterpret_cast<Scalar*>(Y_lcl.data()); |
| 400 |
|
| 401 |
magma_init(); |
| 402 |
int device; |
| 403 |
magma_getdevice(&device); |
| 404 |
magma_queue_create(device, &queue); |
| 405 |
|
| 406 |
// create the magma data container |
| 407 |
magma_dev_Acrs = {Magma_CSR}; |
| 408 |
magma_Acrs = {Magma_CSR}; |
| 409 |
magma_dev_x = {Magma_DENSE}; |
| 410 |
magma_dev_y = {Magma_DENSE}; |
| 411 |
|
| 412 |
// |
| 413 |
magma_dvset_dev(m, 1, // assume mx1 size |
| 414 |
x, // ptr to data on the device |
| 415 |
&magma_dev_x, // magma vector to populate |
| 416 |
queue); |
| 417 |
|
| 418 |
magma_dvset_dev(m, 1, // assume mx1 size |
| 419 |
y, // ptr to data on the device |
| 420 |
&magma_dev_y, // magma vector to populate |
| 421 |
queue); |
| 422 |
magma_dcsrset(m, n, rowptr, cols, vals, &magma_Acrs, queue); |
| 423 |
magma_dmtransfer(magma_Acrs, &magma_dev_Acrs, Magma_DEV, Magma_DEV, queue); |
| 424 |
} |
| 425 |
|
| 426 |
~MagmaSparse_SpmV_Pack() { |
| 427 |
// we dont' own the data... not sure what magma does here... |
| 428 |
magma_dmfree(&magma_dev_x, queue); |
| 429 |
magma_dmfree(&magma_dev_y, queue); |
| 430 |
magma_dmfree(&magma_dev_Acrs, queue); |
| 431 |
|
| 432 |
magma_finalize(); |
| 433 |
}; |
| 434 |
|
| 435 |
bool spmv(const Scalar alpha, const Scalar beta) { |
| 436 |
magma_d_spmv(1.0, magma_dev_Acrs, magma_dev_x, 0.0, magma_dev_y, queue); |
| 437 |
Kokkos::fence(); |
| 438 |
} |
| 439 |
|
| 440 |
private: |
| 441 |
magma_d_matrix magma_dev_Acrs; |
| 442 |
magma_d_matrix magma_Acrs; |
| 443 |
magma_d_matrix magma_dev_x; |
| 444 |
magma_d_matrix magma_dev_y; |
| 445 |
magma_queue_t queue; |
| 446 |
|
| 447 |
int m = -1; |
| 448 |
int n = -1; |
| 449 |
int nnz = -1; |
| 450 |
Scalar* vals = nullptr; // aliased |
| 451 |
int* cols = nullptr; // aliased |
| 452 |
int* rowptr = nullptr; // copied |
| 453 |
Scalar* x = nullptr; // aliased |
| 454 |
Scalar* y = nullptr; // aliased |
| 455 |
|
| 456 |
// handles to the copied data |
| 457 |
int_array_type Arowptr_int; |
| 458 |
}; |
| 459 |
#endif |
| 460 |
|
| 461 |
// ========================================================================= |
| 462 |
// CuSparse Testing |
| 463 |
// ========================================================================= |
| 464 |
#if defined(HAVE_MUELU_CUSPARSE) |
| 465 |
|
| 466 |
#define CHECK_CUDA(func) \ |
| 467 |
{ \ |
| 468 |
cudaError_t status = (func); \ |
| 469 |
if (status != cudaSuccess) { \ |
| 470 |
printf("CUDA API failed at line %d with error: %s (%d)\n", \ |
| 471 |
__LINE__, cudaGetErrorString(status), status); \ |
| 472 |
} \ |
| 473 |
} |
| 474 |
|
| 475 |
#define CHECK_CUSPARSE(func) \ |
| 476 |
{ \ |
| 477 |
cusparseStatus_t status = (func); \ |
| 478 |
if (status != CUSPARSE_STATUS_SUCCESS) { \ |
| 479 |
printf("CUSPARSE API failed at line %d with error: %s (%d)\n", \ |
| 480 |
__LINE__, cusparseGetErrorString(status), status); \ |
| 481 |
} \ |
| 482 |
} |
| 483 |
|
| 484 |
template <typename Scalar, typename LocalOrdinal, typename GlobalOrdinal, typename Node> |
| 485 |
class CuSparse_SpmV_Pack { |
| 486 |
typedef Tpetra::CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> crs_matrix_type; |
| 487 |
typedef Tpetra::MultiVector<Scalar, LocalOrdinal, GlobalOrdinal, Node> vector_type; |
| 488 |
|
| 489 |
public: |
| 490 |
CuSparse_SpmV_Pack(const crs_matrix_type& A, |
| 491 |
const vector_type& X, |
| 492 |
vector_type& Y) {} |
| 493 |
|
| 494 |
~CuSparse_SpmV_Pack(){}; |
| 495 |
|
| 496 |
cusparseStatus_t spmv(const Scalar alpha, const Scalar beta) { return CUSPARSE_STATUS_SUCCESS; } |
| 497 |
}; |
| 498 |
|
| 499 |
template <typename LocalOrdinal, typename GlobalOrdinal> |
| 500 |
class CuSparse_SpmV_Pack<double, LocalOrdinal, GlobalOrdinal, Tpetra::KokkosCompat::KokkosCudaWrapperNode> { |
| 501 |
// typedefs shared among other TPLs |
| 502 |
typedef double Scalar; |
| 503 |
typedef typename Tpetra::KokkosCompat::KokkosCudaWrapperNode Node; |
| 504 |
typedef Tpetra::CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> crs_matrix_type; |
| 505 |
typedef Tpetra::MultiVector<Scalar, LocalOrdinal, GlobalOrdinal, Node> vector_type; |
| 506 |
typedef typename crs_matrix_type::local_matrix_device_type KCRS; |
| 507 |
typedef typename KCRS::StaticCrsGraphType graph_t; |
| 508 |
typedef typename graph_t::row_map_type::non_const_type lno_view_t; |
| 509 |
typedef typename graph_t::row_map_type::const_type c_lno_view_t; |
| 510 |
typedef typename graph_t::entries_type::non_const_type lno_nnz_view_t; |
| 511 |
typedef typename graph_t::entries_type::const_type c_lno_nnz_view_t; |
| 512 |
typedef typename KCRS::values_type::non_const_type scalar_view_t; |
| 513 |
typedef typename Node::device_type device_type; |
| 514 |
|
| 515 |
typedef typename Kokkos::View<int*, |
| 516 |
typename lno_nnz_view_t::array_layout, |
| 517 |
typename lno_nnz_view_t::device_type> |
| 518 |
cusparse_int_type; |
| 519 |
|
| 520 |
public: |
| 521 |
CuSparse_SpmV_Pack(const crs_matrix_type& A, |
| 522 |
const vector_type& X, |
| 523 |
vector_type& Y) { |
| 524 |
// data access common to other TPLs |
| 525 |
const KCRS& Amat = A.getLocalMatrixDevice(); |
| 526 |
c_lno_view_t Arowptr = Amat.graph.row_map; |
| 527 |
c_lno_nnz_view_t Acolind = Amat.graph.entries; |
| 528 |
const scalar_view_t Avals = Amat.values; |
| 529 |
|
| 530 |
Arowptr_cusparse = cusparse_int_type("Arowptr", Arowptr.extent(0)); |
| 531 |
Acolind_cusparse = cusparse_int_type("Acolind", Acolind.extent(0)); |
| 532 |
// copy the ordinals into the local view (type conversion) |
| 533 |
copy_view(Arowptr, Arowptr_cusparse); |
| 534 |
copy_view(Acolind, Acolind_cusparse); |
| 535 |
|
| 536 |
m = static_cast<int>(A.getLocalNumRows()); |
| 537 |
n = static_cast<int>(A.getLocalNumCols()); |
| 538 |
nnz = static_cast<int>(Acolind_cusparse.extent(0)); |
| 539 |
vals = reinterpret_cast<Scalar*>(Avals.data()); |
| 540 |
cols = reinterpret_cast<int*>(Acolind_cusparse.data()); |
| 541 |
rowptr = reinterpret_cast<int*>(Arowptr_cusparse.data()); |
| 542 |
|
| 543 |
auto X_lcl = X.getLocalViewDevice(Tpetra::Access::ReadOnly); |
| 544 |
auto Y_lcl = Y.getLocalViewDevice(Tpetra::Access::ReadWrite); |
| 545 |
x = const_cast<Scalar*>(reinterpret_cast<const Scalar*>(X_lcl.data())); |
| 546 |
y = reinterpret_cast<Scalar*>(Y_lcl.data()); |
| 547 |
|
| 548 |
/* Get handle to the CUBLAS context */ |
| 549 |
cublasCreate(&cublasHandle); |
| 550 |
/* Get handle to the CUSPARSE context */ |
| 551 |
cusparseCreate(&cusparseHandle); |
| 552 |
|
| 553 |
CHECK_CUSPARSE(cusparseCreateCsr(&descrA, m, n, nnz, |
| 554 |
rowptr, cols, vals, |
| 555 |
CUSPARSE_INDEX_32I, CUSPARSE_INDEX_32I, |
| 556 |
CUSPARSE_INDEX_BASE_ZERO, CUDA_R_64F)); |
| 557 |
|
| 558 |
CHECK_CUSPARSE(cusparseCreateDnVec(&vecY, m, y, CUDA_R_64F)); |
| 559 |
CHECK_CUSPARSE(cusparseCreateDnVec(&vecX, n, x, CUDA_R_64F)); |
| 560 |
} |
| 561 |
|
| 562 |
~CuSparse_SpmV_Pack() { |
| 563 |
CHECK_CUSPARSE(cusparseDestroySpMat(descrA)); |
| 564 |
CHECK_CUSPARSE(cusparseDestroyDnVec(vecX)); |
| 565 |
CHECK_CUSPARSE(cusparseDestroyDnVec(vecY)); |
| 566 |
cusparseDestroy(cusparseHandle); |
| 567 |
cublasDestroy(cublasHandle); |
| 568 |
} |
| 569 |
|
| 570 |
cusparseStatus_t spmv(const Scalar alpha, const Scalar beta) { |
| 571 |
// compute: y = alpha*Ax + beta*y |
| 572 |
|
| 573 |
#if CUSPARSE_VERSION >= 11201 |
| 574 |
cusparseSpMVAlg_t alg = CUSPARSE_SPMV_ALG_DEFAULT; |
| 575 |
#else |
| 576 |
cusparseSpMVAlg_t alg = CUSPARSE_MV_ALG_DEFAULT; |
| 577 |
#endif |
| 578 |
|
| 579 |
size_t bufferSize; |
| 580 |
CHECK_CUSPARSE(cusparseSpMV_bufferSize(cusparseHandle, |
| 581 |
transA, |
| 582 |
&alpha, |
| 583 |
descrA, |
| 584 |
vecX, |
| 585 |
&beta, |
| 586 |
vecY, |
| 587 |
CUDA_R_64F, |
| 588 |
alg, |
| 589 |
&bufferSize)); |
| 590 |
|
| 591 |
void* dBuffer = NULL; |
| 592 |
CHECK_CUDA(cudaMalloc(&dBuffer, bufferSize)); |
| 593 |
|
| 594 |
cusparseStatus_t rc = cusparseSpMV(cusparseHandle, |
| 595 |
transA, |
| 596 |
&alpha, |
| 597 |
descrA, |
| 598 |
vecX, |
| 599 |
&beta, |
| 600 |
vecY, |
| 601 |
CUDA_R_64F, |
| 602 |
alg, |
| 603 |
dBuffer); |
| 604 |
|
| 605 |
CHECK_CUDA(cudaFree(dBuffer)); |
| 606 |
Kokkos::fence(); |
| 607 |
return (rc); |
| 608 |
} |
| 609 |
|
| 610 |
private: |
| 611 |
cublasHandle_t cublasHandle = 0; |
| 612 |
cusparseHandle_t cusparseHandle = 0; |
| 613 |
cusparseSpMatDescr_t descrA = 0; |
| 614 |
cusparseDnVecDescr_t vecX = 0, vecY = 0; |
| 615 |
|
| 616 |
// CUSPARSE_OPERATION_NON_TRANSPOSE |
| 617 |
// CUSPARSE_OPERATION_TRANSPOSE |
| 618 |
// CUSPARSE_OPERATION_CONJUGATE_TRANSPOSE |
| 619 |
cusparseOperation_t transA = CUSPARSE_OPERATION_NON_TRANSPOSE; |
| 620 |
int m = -1; |
| 621 |
int n = -1; |
| 622 |
int nnz = -1; |
| 623 |
Scalar* vals = nullptr; // aliased |
| 624 |
int* cols = nullptr; // copied |
| 625 |
int* rowptr = nullptr; // copied |
| 626 |
Scalar* x = nullptr; // aliased |
| 627 |
Scalar* y = nullptr; // aliased |
| 628 |
|
| 629 |
// handles to the copied data |
| 630 |
cusparse_int_type Arowptr_cusparse; |
| 631 |
cusparse_int_type Acolind_cusparse; |
| 632 |
}; |
| 633 |
#endif |
| 634 |
|
| 635 |
// ========================================================================= |
| 636 |
// MKL Testing |
| 637 |
// ========================================================================= |
| 638 |
#if defined(HAVE_MUELU_MKL) |
| 639 |
#include "mkl.h" |
| 640 |
|
| 641 |
std::string mkl_error(sparse_status_t code) { |
| 642 |
switch (code) { |
| 643 |
case SPARSE_STATUS_SUCCESS: |
| 644 |
return std::string("Success"); |
| 645 |
case SPARSE_STATUS_NOT_INITIALIZED: |
| 646 |
return std::string("Empty handle or matrix array"); |
| 647 |
case SPARSE_STATUS_ALLOC_FAILED: |
| 648 |
return std::string("Memory allocation failed"); |
| 649 |
case SPARSE_STATUS_INVALID_VALUE: |
| 650 |
return std::string("Input contains an invalid value"); |
| 651 |
case SPARSE_STATUS_EXECUTION_FAILED: |
| 652 |
return std::string("Execution failed"); |
| 653 |
case SPARSE_STATUS_INTERNAL_ERROR: |
| 654 |
return std::string("Internal error"); |
| 655 |
case SPARSE_STATUS_NOT_SUPPORTED: |
| 656 |
return std::string("Operation not supported"); |
| 657 |
}; |
| 658 |
} |
| 659 |
|
| 660 |
struct matrix_descr mkl_descr; |
| 661 |
|
| 662 |
void MV_MKL(sparse_matrix_t& AMKL, double* x, double* y) { |
| 663 |
// sparse_status_t mkl_sparse_d_mv (sparse_operation_t operation, double alpha, const sparse_matrix_t A, struct matrix_descr descr, const double *x, double beta, double *y); |
| 664 |
|
| 665 |
mkl_sparse_d_mv(SPARSE_OPERATION_NON_TRANSPOSE, 1.0, AMKL, mkl_descr, x, 0.0, y); |
| 666 |
Kokkos::fence(); |
| 667 |
} |
| 668 |
|
| 669 |
#endif |
| 670 |
|
| 671 |
// ========================================================================= |
| 672 |
// Tpetra Kernel Testing |
| 673 |
// ========================================================================= |
| 674 |
template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node> |
| 675 |
void MV_Tpetra(const Tpetra::CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>& A, const Tpetra::MultiVector<Scalar, LocalOrdinal, GlobalOrdinal, Node>& x, Tpetra::MultiVector<Scalar, LocalOrdinal, GlobalOrdinal, Node>& y) { |
| 676 |
A.apply(x, y); |
| 677 |
Kokkos::fence(); |
| 678 |
} |
| 679 |
|
| 680 |
template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node> |
| 681 |
void MV_KK(const Tpetra::CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>& A, const Tpetra::MultiVector<Scalar, LocalOrdinal, GlobalOrdinal, Node>& x, Tpetra::MultiVector<Scalar, LocalOrdinal, GlobalOrdinal, Node>& y) { |
| 682 |
typedef typename Node::device_type device_type; |
| 683 |
const auto& AK = A.getLocalMatrixDevice(); |
| 684 |
auto X_lcl = x.getLocalViewDevice(Tpetra::Access::ReadOnly); |
| 685 |
auto Y_lcl = y.getLocalViewDevice(Tpetra::Access::OverwriteAll); |
| 686 |
KokkosSparse::spmv(KokkosSparse::NoTranspose, Teuchos::ScalarTraits<Scalar>::one(), AK, X_lcl, Teuchos::ScalarTraits<Scalar>::zero(), Y_lcl); |
| 687 |
Kokkos::fence(); |
| 688 |
} |
| 689 |
|
| 690 |
// ========================================================================= |
| 691 |
// ========================================================================= |
| 692 |
// ========================================================================= |
| 693 |
template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node> |
| 694 |
int main_(Teuchos::CommandLineProcessor& clp, Xpetra::UnderlyingLib& lib, int argc, char* argv[]) { |
| 695 |
#include <MueLu_UseShortNames.hpp> |
| 696 |
|
| 697 |
using std::endl; |
| 698 |
using Teuchos::RCP; // reference count pointers |
| 699 |
using Teuchos::rcp; |
| 700 |
using Teuchos::TimeMonitor; |
| 701 |
|
| 702 |
#if defined(HAVE_MUELU_PETSC) && defined(HAVE_MPI) |
| 703 |
PetscInitialize(0, NULL, NULL, NULL); |
| 704 |
#endif |
| 705 |
|
| 706 |
bool success = false; |
| 707 |
bool verbose = true; |
| 708 |
try { |
| 709 |
RCP<const Teuchos::Comm<int> > comm = Teuchos::DefaultComm<int>::getComm(); |
| 710 |
int numProc = comm->getSize(); |
| 711 |
|
| 712 |
// ========================================================================= |
| 713 |
// Convenient definitions |
| 714 |
// ========================================================================= |
| 715 |
// SC zero = Teuchos::ScalarTraits<SC>::zero(), one = Teuchos::ScalarTraits<SC>::one(); |
| 716 |
|
| 717 |
// Instead of checking each time for rank, create a rank 0 stream |
| 718 |
RCP<Teuchos::FancyOStream> pOut = Teuchos::fancyOStream(Teuchos::rcpFromRef(std::cout)); |
| 719 |
Teuchos::FancyOStream& out = *pOut; |
| 720 |
out.setOutputToRootOnly(0); |
| 721 |
|
| 722 |
// ========================================================================= |
| 723 |
// Parameters initialization |
| 724 |
// ========================================================================= |
| 725 |
GO nx = 100, ny = 100, nz = 50; |
| 726 |
Galeri::Xpetra::Parameters<GO> galeriParameters(clp, nx, ny, nz, "Laplace2D"); // manage parameters of the test case |
| 727 |
Xpetra::Parameters xpetraParameters(clp); // manage parameters of Xpetra |
| 728 |
|
| 729 |
bool binaryFormat = false; |
| 730 |
clp.setOption("binary", "ascii", &binaryFormat, "print timings to screen"); |
| 731 |
std::string matrixFile; |
| 732 |
clp.setOption("matrixfile", &matrixFile, "matrix market file containing matrix"); |
| 733 |
std::string rowMapFile; |
| 734 |
clp.setOption("rowmap", &rowMapFile, "map data file"); |
| 735 |
std::string colMapFile; |
| 736 |
clp.setOption("colmap", &colMapFile, "colmap data file"); |
| 737 |
std::string domainMapFile; |
| 738 |
clp.setOption("domainmap", &domainMapFile, "domainmap data file"); |
| 739 |
std::string rangeMapFile; |
| 740 |
clp.setOption("rangemap", &rangeMapFile, "rangemap data file"); |
| 741 |
|
| 742 |
bool printTimings = true; |
| 743 |
clp.setOption("timings", "notimings", &printTimings, "print timings to screen"); |
| 744 |
int nrepeat = 1000; |
| 745 |
clp.setOption("nrepeat", &nrepeat, "repeat the experiment N times"); |
| 746 |
|
| 747 |
bool describeMatrix = true; |
| 748 |
clp.setOption("showmatrix", "noshowmatrix", &describeMatrix, "describe matrix"); |
| 749 |
bool useStackedTimer = false; |
| 750 |
clp.setOption("stackedtimer", "nostackedtimer", &useStackedTimer, "use stacked timer"); |
| 751 |
std::string watchrProblemName = std::string("MueLu Matvec ") + std::to_string(comm->getSize()) + " ranks"; |
| 752 |
clp.setOption("watchr-problem-name", &watchrProblemName, "Problem name for Watchr plot headers"); |
| 753 |
bool verboseModel = false; |
| 754 |
clp.setOption("verbosemodel", "noverbosemodel", &verboseModel, "use stacked verbose performance model"); |
| 755 |
|
| 756 |
// the kernels |
| 757 |
bool do_mkl = true; |
| 758 |
bool do_tpetra = true; |
| 759 |
bool do_kk = true; |
| 760 |
bool do_cusparse = true; |
| 761 |
bool do_magmasparse = true; |
| 762 |
bool do_hypre = true; |
| 763 |
bool do_petsc = true; |
| 764 |
|
| 765 |
bool report_error_norms = false; |
| 766 |
// handle the TPLs |
| 767 |
#if !defined(HAVE_MUELU_MKL) |
| 768 |
do_mkl = false; |
| 769 |
#endif |
| 770 |
#if !defined(HAVE_MUELU_CUSPARSE) |
| 771 |
do_cusparse = false; |
| 772 |
#endif |
| 773 |
#if !defined(HAVE_MUELU_MAGMASPARSE) |
| 774 |
do_magmasparse = false; |
| 775 |
#endif |
| 776 |
#if !defined(HAVE_MUELU_HYPRE) || !defined(HAVE_MPI) |
| 777 |
do_hypre = false; |
| 778 |
#endif |
| 779 |
#if !defined(HAVE_MUELU_PETSC) || !defined(HAVE_MPI) |
| 780 |
do_petsc = false; |
| 781 |
#endif |
| 782 |
|
| 783 |
clp.setOption("mkl", "nomkl", &do_mkl, "Evaluate MKL"); |
| 784 |
clp.setOption("tpetra", "notpetra", &do_tpetra, "Evaluate Tpetra"); |
| 785 |
clp.setOption("kk", "nokk", &do_kk, "Evaluate KokkosKernels"); |
| 786 |
clp.setOption("cusparse", "nocusparse", &do_cusparse, "Evaluate CuSparse"); |
| 787 |
clp.setOption("magamasparse", "nomagmasparse", &do_magmasparse, "Evaluate MagmaSparse"); |
| 788 |
clp.setOption("hypre", "nohypre", &do_hypre, "Evaluate Hypre"); |
| 789 |
clp.setOption("petsc", "nopetsc", &do_petsc, "Evaluate Petsc"); |
| 790 |
|
| 791 |
clp.setOption("report_error_norms", "noreport_error_norms", &report_error_norms, "Report L2 norms for the solution"); |
| 792 |
|
| 793 |
std::ostringstream galeriStream; |
| 794 |
std::string rhsFile, coordFile, coordMapFile, nullFile, materialFile; // unused |
| 795 |
typedef typename Teuchos::ScalarTraits<SC>::magnitudeType real_type; |
| 796 |
typedef Xpetra::MultiVector<real_type, LO, GO, NO> RealValuedMultiVector; |
| 797 |
RCP<RealValuedMultiVector> coordinates; |
| 798 |
RCP<MultiVector> nullspace, material, x, b; |
| 799 |
RCP<Matrix> A; |
| 800 |
RCP<const Map> map; |
| 801 |
|
| 802 |
switch (clp.parse(argc, argv)) { |
| 803 |
case Teuchos::CommandLineProcessor::PARSE_HELP_PRINTED: return EXIT_SUCCESS; |
| 804 |
case Teuchos::CommandLineProcessor::PARSE_ERROR: |
| 805 |
case Teuchos::CommandLineProcessor::PARSE_UNRECOGNIZED_OPTION: return EXIT_FAILURE; |
| 806 |
case Teuchos::CommandLineProcessor::PARSE_SUCCESSFUL: break; |
| 807 |
} |
| 808 |
|
| 809 |
// Load the matrix off disk (or generate it via Galeri), assuming only one right hand side is loaded. |
| 810 |
MatrixLoad<SC, LO, GO, NO>(comm, lib, binaryFormat, matrixFile, rhsFile, rowMapFile, colMapFile, domainMapFile, rangeMapFile, coordFile, coordMapFile, nullFile, materialFile, map, A, coordinates, nullspace, material, x, b, 1, galeriParameters, xpetraParameters, galeriStream); |
| 811 |
|
| 812 |
if (do_kk && comm->getSize() > 1) { |
| 813 |
out << "KK was requested, but this kernel this cannot be run on more than one rank. Disabling..." << endl; |
| 814 |
do_kk = false; |
| 815 |
} |
| 816 |
|
| 817 |
#ifndef HAVE_MUELU_MKL |
| 818 |
if (do_mkl) { |
| 819 |
out << "MKL was requested, but this kernel is not available. Disabling..." << endl; |
| 820 |
do_mkl = false; |
| 821 |
} |
| 822 |
#endif |
| 823 |
|
| 824 |
#if !defined(HAVE_MUELU_CUSPARSE) |
| 825 |
if (do_cusparse) { |
| 826 |
out << "CuSparse was requested, but this kernel is not available. Disabling..." << endl; |
| 827 |
do_cusparse = false; |
| 828 |
} |
| 829 |
#else |
| 830 |
if (!std::is_same<NO, Tpetra::KokkosCompat::KokkosCudaWrapperNode>::value) do_cusparse = false; |
| 831 |
#endif |
| 832 |
|
| 833 |
#if !defined(HAVE_MUELU_MAGMASPARSE) |
| 834 |
if (do_magmasparse) { |
| 835 |
out << "MagmaSparse was requested, but this kernel is not available. Disabling..." << endl; |
| 836 |
do_magmasparse = false; |
| 837 |
} |
| 838 |
#endif |
| 839 |
|
| 840 |
#if !defined(HAVE_MUELU_HYPRE) |
| 841 |
if (do_hypre) { |
| 842 |
out << "Hypre was requested, but this kernel is not available. Disabling..." << endl; |
| 843 |
do_hypre = false; |
| 844 |
} |
| 845 |
#endif |
| 846 |
|
| 847 |
#if !defined(HAVE_MUELU_PETSC) |
| 848 |
if (do_hypre) { |
| 849 |
out << "Petsc was requested, but this kernel is not available. Disabling..." << endl; |
| 850 |
do_petsc = false; |
| 851 |
} |
| 852 |
#endif |
| 853 |
|
| 854 |
// simple hack to randomize order of experiments |
| 855 |
enum class Experiments { MKL = 0, |
| 856 |
TPETRA, |
| 857 |
KK, |
| 858 |
CUSPARSE, |
| 859 |
MAGMASPARSE, |
| 860 |
HYPRE, |
| 861 |
PETSC }; |
| 862 |
const char* const experiment_id_to_string[] = { |
| 863 |
"MKL ", |
| 864 |
"Tpetra ", |
| 865 |
"KK ", |
| 866 |
"CuSparse ", |
| 867 |
"MagmaSparse", |
| 868 |
"HYPRE", |
| 869 |
"PETSC"}; |
| 870 |
|
| 871 |
std::vector<Experiments> my_experiments; |
| 872 |
// add the experiments we will run |
| 873 |
|
| 874 |
#ifdef HAVE_MUELU_MKL |
| 875 |
if (do_mkl) my_experiments.push_back(Experiments::MKL); // MKL |
| 876 |
#endif |
| 877 |
|
| 878 |
#ifdef HAVE_MUELU_CUSPARSE |
| 879 |
if (do_cusparse) my_experiments.push_back(Experiments::CUSPARSE); // CuSparse |
| 880 |
#endif |
| 881 |
|
| 882 |
#ifdef HAVE_MUELU_MAGMASPARSE |
| 883 |
if (do_magmasparse) my_experiments.push_back(Experiments::MAGMASPARSE); // MagmaSparse |
| 884 |
#endif |
| 885 |
|
| 886 |
#if defined(HAVE_MUELU_HYPRE) && defined(HAVE_MPI) |
| 887 |
if (do_hypre) my_experiments.push_back(Experiments::HYPRE); // HYPRE |
| 888 |
#endif |
| 889 |
|
| 890 |
#if defined(HAVE_MUELU_PETSC) && defined(HAVE_MPI) |
| 891 |
if (do_petsc) my_experiments.push_back(Experiments::PETSC); // PETSc |
| 892 |
#endif |
| 893 |
|
| 894 |
// assume these are available |
| 895 |
if (do_tpetra) my_experiments.push_back(Experiments::TPETRA); // Tpetra |
| 896 |
if (do_kk) my_experiments.push_back(Experiments::KK); // KK |
| 897 |
|
| 898 |
out << "========================================================" << endl |
| 899 |
<< xpetraParameters |
| 900 |
// << matrixParameters |
| 901 |
<< "========================================================" << endl |
| 902 |
<< "Template Types:" << endl |
| 903 |
<< " Scalar: " << Teuchos::demangleName(typeid(SC).name()) << endl |
| 904 |
<< " LocalOrdinal: " << Teuchos::demangleName(typeid(LO).name()) << endl |
| 905 |
<< " GlobalOrdinal: " << Teuchos::demangleName(typeid(GO).name()) << endl |
| 906 |
<< " Node: " << Teuchos::demangleName(typeid(NO).name()) << endl |
| 907 |
<< "Sizes:" << endl |
| 908 |
<< " Scalar: " << sizeof(SC) << endl |
| 909 |
<< " LocalOrdinal: " << sizeof(LO) << endl |
| 910 |
<< " GlobalOrdinal: " << sizeof(GO) << endl |
| 911 |
<< "========================================================" << endl |
| 912 |
<< "Matrix: " << Teuchos::demangleName(typeid(Matrix).name()) << endl |
| 913 |
<< "Vector: " << Teuchos::demangleName(typeid(MultiVector).name()) << endl |
| 914 |
<< "Hierarchy: " << Teuchos::demangleName(typeid(Hierarchy).name()) << endl |
| 915 |
<< "========================================================" << endl |
| 916 |
<< " MPI Ranks: " << numProc << endl |
| 917 |
<< "========================================================" << endl; |
| 918 |
|
| 919 |
#if defined(HAVE_TPETRA_INST_OPENMP) |
| 920 |
out << "Tpetra::KokkosCompat::KokkosOpenMPWrapperNode::execution_space().concurrency() = " << Tpetra::KokkosCompat::KokkosOpenMPWrapperNode::execution_space().concurrency() << endl |
| 921 |
<< "========================================================" << endl; |
| 922 |
#endif |
| 923 |
|
| 924 |
// ========================================================================= |
| 925 |
// Problem construction |
| 926 |
// ========================================================================= |
| 927 |
if (useStackedTimer) { |
| 928 |
stacked_timer = rcp(new Teuchos::StackedTimer("MueLu_MatvecKernelDriver")); |
| 929 |
Teuchos::TimeMonitor::setStackedTimer(stacked_timer); |
| 930 |
} else |
| 931 |
globalTimeMonitor = rcp(new TimeMonitor(*TimeMonitor::getNewTimer("MatrixRead: S - Global Time"))); |
| 932 |
|
| 933 |
comm->barrier(); |
| 934 |
|
| 935 |
RCP<MultiVector> y = Xpetra::VectorFactory<SC, LO, GO, Node>::Build(A->getRowMap()); |
| 936 |
RCP<MultiVector> y_baseline = Xpetra::VectorFactory<SC, LO, GO, Node>::Build(A->getRowMap()); |
| 937 |
x->putScalar(Teuchos::ScalarTraits<Scalar>::one()); |
| 938 |
y->putScalar(Teuchos::ScalarTraits<Scalar>::nan()); |
| 939 |
y_baseline->putScalar(Teuchos::ScalarTraits<Scalar>::nan()); |
| 940 |
|
| 941 |
typedef Tpetra::CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> crs_matrix_type; |
| 942 |
typedef Tpetra::MultiVector<Scalar, LocalOrdinal, GlobalOrdinal, Node> vector_type; |
| 943 |
RCP<const crs_matrix_type> At; |
| 944 |
vector_type xt; |
| 945 |
vector_type yt; |
| 946 |
|
| 947 |
At = toTpetra(A); |
| 948 |
const crs_matrix_type& Att = *At; |
| 949 |
xt = Xpetra::toTpetra(*x); |
| 950 |
yt = Xpetra::toTpetra(*y); |
| 951 |
|
| 952 |
size_t l_permutes = 0, g_permutes = 0; |
| 953 |
if (!At->getGraph()->getImporter().is_null()) { |
| 954 |
l_permutes = At->getGraph()->getImporter()->getNumPermuteIDs(); |
| 955 |
Teuchos::reduceAll(*comm, Teuchos::REDUCE_SUM, 1, &l_permutes, &g_permutes); |
| 956 |
} |
| 957 |
|
| 958 |
#if defined(HAVE_MUELU_CUSPARSE) |
| 959 |
typedef CuSparse_SpmV_Pack<SC, LO, GO, Node> CuSparse_thing_t; |
| 960 |
CuSparse_thing_t cusparse_spmv(*At, xt, yt); |
| 961 |
#endif |
| 962 |
|
| 963 |
#if defined(HAVE_MUELU_MAGMASPARSE) |
| 964 |
typedef MagmaSparse_SpmV_Pack<SC, LO, GO, Node> MagmaSparse_thing_t; |
| 965 |
MagmaSparse_thing_t magmasparse_spmv(*At, xt, yt); |
| 966 |
#endif |
| 967 |
#if defined(HAVE_MUELU_HYPRE) && defined(HAVE_MPI) |
| 968 |
typedef HYPRE_SpmV_Pack<SC, LO, GO, Node> HYPRE_thing_t; |
| 969 |
HYPRE_thing_t hypre_spmv(*At, xt, yt); |
| 970 |
#endif |
| 971 |
#if defined(HAVE_MUELU_PETSC) && defined(HAVE_MPI) |
| 972 |
typedef Petsc_SpmV_Pack<SC, LO, GO, Node> Petsc_thing_t; |
| 973 |
Petsc_thing_t petsc_spmv(*At, xt, yt); |
| 974 |
#endif |
| 975 |
|
| 976 |
#if defined(HAVE_MUELU_MKL) |
| 977 |
// typedefs shared among other TPLs |
| 978 |
typedef typename crs_matrix_type::local_matrix_host_type KCRS; |
| 979 |
typedef typename KCRS::StaticCrsGraphType graph_t; |
| 980 |
typedef typename graph_t::row_map_type::non_const_type lno_view_t; |
| 981 |
typedef typename graph_t::row_map_type::const_type c_lno_view_t; |
| 982 |
typedef typename graph_t::entries_type::non_const_type lno_nnz_view_t; |
| 983 |
typedef typename graph_t::entries_type::const_type c_lno_nnz_view_t; |
| 984 |
typedef typename KCRS::values_type::non_const_type scalar_view_t; |
| 985 |
typedef typename Node::device_type device_type; |
| 986 |
|
| 987 |
// data access common to other TPLs |
| 988 |
const KCRS& Amat = At->getLocalMatrixHost(); |
| 989 |
c_lno_view_t Arowptr = Amat.graph.row_map; |
| 990 |
c_lno_nnz_view_t Acolind = Amat.graph.entries; |
| 991 |
const scalar_view_t Avals = Amat.values; |
| 992 |
|
| 993 |
// MKL specific things |
| 994 |
sparse_matrix_t mkl_A; |
| 995 |
typedef typename Kokkos::View<MKL_INT*, typename lno_nnz_view_t::array_layout, typename lno_nnz_view_t::device_type> mkl_int_type; |
| 996 |
|
| 997 |
mkl_int_type ArowptrMKL("Arowptr", Arowptr.extent(0)); |
| 998 |
mkl_int_type AcolindMKL("Acolind", Acolind.extent(0)); |
| 999 |
copy_view(Arowptr, ArowptrMKL); |
| 1000 |
copy_view(Acolind, AcolindMKL); |
| 1001 |
double* mkl_xdouble = nullptr; |
| 1002 |
double* mkl_ydouble = nullptr; |
| 1003 |
mkl_descr.type = SPARSE_MATRIX_TYPE_GENERAL; |
| 1004 |
|
| 1005 |
if (std::is_same<Scalar, double>::value) { |
| 1006 |
mkl_sparse_d_create_csr(&mkl_A, |
| 1007 |
SPARSE_INDEX_BASE_ZERO, |
| 1008 |
At->getLocalNumRows(), |
| 1009 |
At->getLocalNumCols(), |
| 1010 |
ArowptrMKL.data(), |
| 1011 |
ArowptrMKL.data() + 1, |
| 1012 |
AcolindMKL.data(), |
| 1013 |
(double*)Avals.data()); |
| 1014 |
} else |
| 1015 |
throw std::runtime_error("MKL Type Mismatch"); |
| 1016 |
|
| 1017 |
#endif // end MKL |
| 1018 |
|
| 1019 |
// globalTimeMonitor = Teuchos::null; |
| 1020 |
comm->barrier(); |
| 1021 |
|
| 1022 |
out << "Matrix Read complete." << endl; |
| 1023 |
if (describeMatrix) { |
| 1024 |
out << "Matrix A:" << endl |
| 1025 |
<< *A |
| 1026 |
<< "========================================================" << endl; |
| 1027 |
} |
| 1028 |
// save std::cout formating |
| 1029 |
std::ios cout_default_fmt_flags(NULL); |
| 1030 |
cout_default_fmt_flags.copyfmt(std::cout); |
| 1031 |
|
| 1032 |
// random source |
| 1033 |
std::random_device rd; |
| 1034 |
std::mt19937 random_source(rd()); |
| 1035 |
|
| 1036 |
// compute the baseline |
| 1037 |
vector_type yt_baseline = Xpetra::toTpetra(*y_baseline); |
| 1038 |
if (report_error_norms) MV_Tpetra(*At, xt, yt_baseline); |
| 1039 |
const bool error_check_y = true; |
| 1040 |
std::vector<typename Teuchos::ScalarTraits<Scalar>::magnitudeType> dummy; |
| 1041 |
dummy.resize(1); |
| 1042 |
Teuchos::ArrayView<typename Teuchos::ScalarTraits<Scalar>::magnitudeType> y_norms(dummy.data(), 1); |
| 1043 |
|
| 1044 |
// no need for a barrier, because the randomization process uses a collective. |
| 1045 |
if (!my_experiments.empty()) { |
| 1046 |
for (int i = 0; i < nrepeat; i++) { |
| 1047 |
// randomize the experiments |
| 1048 |
if (comm->getRank() == 0) { |
| 1049 |
std::shuffle(my_experiments.begin(), my_experiments.end(), random_source); |
| 1050 |
} |
| 1051 |
|
| 1052 |
// Broadcast this ordering to the other processes |
| 1053 |
comm->broadcast(0, |
| 1054 |
static_cast<int>(sizeof(Experiments::TPETRA) * my_experiments.size()), |
| 1055 |
reinterpret_cast<char*>(my_experiments.data())); |
| 1056 |
|
| 1057 |
// loop over the randomized experiments |
| 1058 |
for (const auto& experiment_id : my_experiments) { |
| 1059 |
switch (experiment_id) { |
| 1060 |
#ifdef HAVE_MUELU_MKL |
| 1061 |
// MKL |
| 1062 |
case Experiments::MKL: { |
| 1063 |
TimeMonitor t(*TimeMonitor::getNewTimer("MV MKL: Total")); |
| 1064 |
auto X_lcl = xt.getLocalViewDevice(Tpetra::Access::ReadOnly); |
| 1065 |
auto Y_lcl = yt.getLocalViewDevice(Tpetra::Access::OverwriteAll); |
| 1066 |
mkl_xdouble = (double*)X_lcl.data(); |
| 1067 |
mkl_ydouble = (double*)Y_lcl.data(); |
| 1068 |
MV_MKL(mkl_A, mkl_xdouble, mkl_ydouble); |
| 1069 |
} break; |
| 1070 |
#endif |
| 1071 |
|
| 1072 |
// KK Algorithms |
| 1073 |
case Experiments::KK: { |
| 1074 |
TimeMonitor t(*TimeMonitor::getNewTimer("MV KK: Total")); |
| 1075 |
MV_KK(Att, xt, yt); |
| 1076 |
} break; |
| 1077 |
// Tpetra |
| 1078 |
case Experiments::TPETRA: { |
| 1079 |
TimeMonitor t(*TimeMonitor::getNewTimer("MV Tpetra: Total")); |
| 1080 |
MV_Tpetra(*At, xt, yt); |
| 1081 |
} break; |
| 1082 |
|
| 1083 |
#ifdef HAVE_MUELU_CUSPARSE |
| 1084 |
// CUSPARSE |
| 1085 |
case Experiments::CUSPARSE: { |
| 1086 |
const Scalar alpha = 1.0; |
| 1087 |
const Scalar beta = 0.0; |
| 1088 |
TimeMonitor t(*TimeMonitor::getNewTimer("MV CuSparse: Total")); |
| 1089 |
cusparse_spmv.spmv(alpha, beta); |
| 1090 |
} break; |
| 1091 |
#endif |
| 1092 |
|
| 1093 |
#ifdef HAVE_MUELU_MAGMASPARSE |
| 1094 |
// Magma CSR |
| 1095 |
case Experiments::MAGMASPARSE: { |
| 1096 |
const Scalar alpha = 1.0; |
| 1097 |
const Scalar beta = 0.0; |
| 1098 |
TimeMonitor t(*TimeMonitor::getNewTimer("MV MagmaSparse: Total")); |
| 1099 |
magmasparse_spmv.spmv(alpha, beta); |
| 1100 |
} break; |
| 1101 |
#endif |
| 1102 |
|
| 1103 |
#ifdef HAVE_MUELU_HYPRE |
| 1104 |
// HYPRE |
| 1105 |
case Experiments::HYPRE: { |
| 1106 |
const Scalar alpha = 1.0; |
| 1107 |
const Scalar beta = 0.0; |
| 1108 |
TimeMonitor t(*TimeMonitor::getNewTimer("MV HYPRE: Total")); |
| 1109 |
hypre_spmv.spmv(alpha, beta); |
| 1110 |
} break; |
| 1111 |
#endif |
| 1112 |
|
| 1113 |
#ifdef HAVE_MUELU_PETSC |
| 1114 |
// PETSc |
| 1115 |
case Experiments::PETSC: { |
| 1116 |
const Scalar alpha = 1.0; |
| 1117 |
const Scalar beta = 0.0; |
| 1118 |
TimeMonitor t(*TimeMonitor::getNewTimer("MV Petsc: Total")); |
| 1119 |
petsc_spmv.spmv(alpha, beta); |
| 1120 |
} break; |
| 1121 |
#endif |
| 1122 |
|
| 1123 |
default: |
| 1124 |
std::cerr << "Unknown experiment ID encountered: " << (int)experiment_id << std::endl; |
| 1125 |
} |
| 1126 |
// TODO: add a correctness check |
| 1127 |
// For now, all things alias x/y, so we can test yt (flakey and scary, but you only live once) |
| 1128 |
if (error_check_y && report_error_norms) { |
| 1129 |
// compute ||y||_2 |
| 1130 |
y_norms[0] = -1; |
| 1131 |
y->norm2(y_norms); |
| 1132 |
const auto y_norm2 = y_norms[0]; |
| 1133 |
|
| 1134 |
y_norms[0] = -1; |
| 1135 |
yt.norm2(y_norms); |
| 1136 |
const auto y_mv_norm2 = y_norms[0]; |
| 1137 |
|
| 1138 |
y->update(-Teuchos::ScalarTraits<Scalar>::one(), *y_baseline, Teuchos::ScalarTraits<Scalar>::one()); |
| 1139 |
|
| 1140 |
y_norms[0] = -1; |
| 1141 |
y->norm2(y_norms); |
| 1142 |
const auto y_err = y_norms[0]; |
| 1143 |
|
| 1144 |
y->putScalar(Teuchos::ScalarTraits<Scalar>::nan()); |
| 1145 |
; |
| 1146 |
|
| 1147 |
y_norms[0] = -1; |
| 1148 |
y_baseline->norm2(y_norms); |
| 1149 |
const auto y_baseline_norm2 = y_norms[0]; |
| 1150 |
|
| 1151 |
y_norms[0] = -1; |
| 1152 |
yt.norm2(y_norms); |
| 1153 |
const auto y_mv_norm2_next_itr = y_norms[0]; |
| 1154 |
|
| 1155 |
std::cout << "ExperimentID: " << experiment_id_to_string[(int)experiment_id] << ", ||y-y_hat||_2 = " |
| 1156 |
<< std::setprecision(std::numeric_limits<Scalar>::digits10 + 1) |
| 1157 |
<< std::scientific << y_err |
| 1158 |
<< ", ||y||_2 = " << y_norm2 |
| 1159 |
<< ", ||y_baseline||_2 = " << y_baseline_norm2 |
| 1160 |
<< ", ||y_ptr|| == ||y_mv||: " << std::boolalpha << (y_mv_norm2 == y_norm2) |
| 1161 |
<< ", setting y to nan ... ||y||_2 for next iter: " << y_mv_norm2_next_itr |
| 1162 |
<< "\n"; |
| 1163 |
} |
| 1164 |
|
| 1165 |
// We need to both fence and barrier to make sure the kernels do not overlap |
| 1166 |
Kokkos::fence(); |
| 1167 |
comm->barrier(); |
| 1168 |
} // end random exp loop |
| 1169 |
} // end repeat |
| 1170 |
} // end ! my_experiments.empty() |
| 1171 |
// restore the IO stream |
| 1172 |
std::cout.copyfmt(cout_default_fmt_flags); |
| 1173 |
|
| 1174 |
if (useStackedTimer) { |
| 1175 |
stacked_timer->stop("MueLu_MatvecKernelDriver"); |
| 1176 |
Teuchos::StackedTimer::OutputOptions options; |
| 1177 |
options.output_fraction = options.output_histogram = options.output_minmax = true; |
| 1178 |
stacked_timer->report(out, comm, options); |
| 1179 |
auto xmlOut = stacked_timer->reportWatchrXML(watchrProblemName, comm); |
| 1180 |
if (xmlOut.length()) |
| 1181 |
std::cout << "\nAlso created Watchr performance report " << xmlOut << '\n'; |
| 1182 |
} else { |
| 1183 |
TimeMonitor::summarize(A->getRowMap()->getComm().ptr(), std::cout, false, true, false, Teuchos::Union, "", true); |
| 1184 |
} |
| 1185 |
|
| 1186 |
#if defined(HAVE_MUELU_MKL) |
| 1187 |
mkl_sparse_destroy(mkl_A); |
| 1188 |
#endif |
| 1189 |
|
| 1190 |
// ========================================== |
| 1191 |
// Performance Models |
| 1192 |
// ========================================== |
| 1193 |
std::vector<const char*> timer_names = {"MV MKL: Total", |
| 1194 |
"MV KK: Total", |
| 1195 |
"MV Tpetra: Total", |
| 1196 |
"MV CuSparse: Total", |
| 1197 |
"MV MagmaSparse: Total", |
| 1198 |
"MV HYPRE: Total", |
| 1199 |
"MV Petsc: Total"}; |
| 1200 |
|
| 1201 |
MueLu::report_spmv_performance_models<Matrix>(A, nrepeat, timer_names, globalTimeMonitor, "", verboseModel); |
| 1202 |
globalTimeMonitor = Teuchos::null; |
| 1203 |
|
| 1204 |
success = true; |
| 1205 |
} |
| 1206 |
TEUCHOS_STANDARD_CATCH_STATEMENTS(verbose, std::cerr, success); |
| 1207 |
|
| 1208 |
#if defined(HAVE_MUELU_PETSC) && defined(HAVE_MPI) |
| 1209 |
PetscFinalize(); |
| 1210 |
#endif |
| 1211 |
|
| 1212 |
return (success ? EXIT_SUCCESS : EXIT_FAILURE); |
| 1213 |
} // main |
| 1214 |
|
| 1215 |
//- -- -------------------------------------------------------- |
| 1216 |
#define MUELU_AUTOMATIC_TEST_ETI_NAME main_ |
| 1217 |
#include "MueLu_Test_ETI.hpp" |
| 1218 |
|
| 1219 |
int main(int argc, char* argv[]) { |
| 1220 |
return Automatic_Test_ETI(argc, argv); |
| 1221 |
} |