// @HEADER // ***************************************************************************** // ShyLU: Scalable Hybrid LU Preconditioner and Solver // // Copyright 2011 NTESS and the ShyLU contributors. // SPDX-License-Identifier: BSD-3-Clause // ***************************************************************************** // @HEADER #ifndef _FROSCH_EXTRACTSUBMATRICES_DEF_HPP #define _FROSCH_EXTRACTSUBMATRICES_DEF_HPP #include namespace FROSch { using namespace std; using namespace Teuchos; using namespace Xpetra; template RCP > ExtractLocalSubdomainMatrix(RCP > globalMatrix, RCP > map) { FROSCH_DETAILTIMER_START(extractLocalSubdomainMatrixTime,"ExtractLocalSubdomainMatrix"); RCP > subdomainMatrix = MatrixFactory::Build(map,globalMatrix->getGlobalMaxNumRowEntries()); RCP > scatter = ImportFactory::Build(globalMatrix->getRowMap(),map); subdomainMatrix->doImport(*globalMatrix,*scatter,ADD); //cout << *subdomainMatrix << endl; RCP > SerialComm = rcp(new MpiComm(MPI_COMM_SELF)); RCP > localSubdomainMap = MapFactory::Build(map->lib(),map->getLocalNumElements(),0,SerialComm); RCP > localSubdomainMatrix = MatrixFactory::Build(localSubdomainMap,globalMatrix->getGlobalMaxNumRowEntries()); for (unsigned i=0; igetLocalNumElements(); i++) { ArrayView indices; ArrayView values; subdomainMatrix->getGlobalRowView(map->getGlobalElement(i),indices,values); LO size = indices.size(); if (size>0) { Array indicesLocal; Array valuesLocal; for (LO j=0; jgetLocalElement(indices[j]); if (localIndex>=0) { indicesLocal.push_back(localIndex); valuesLocal.push_back(values[j]); } } localSubdomainMatrix->insertGlobalValues(i,indicesLocal(),valuesLocal()); } } localSubdomainMatrix->fillComplete(); return localSubdomainMatrix.getConst(); } // this version just read indices without building submatrices, which is done in extractLocalSubdomainMatrix_Symbolic template void ExtractLocalSubdomainMatrix_Symbolic(RCP > subdomainMatrix, // input : globalMatrix, re-distributed with map RCP > localSubdomainMatrix) // output : local submatrix { FROSCH_DETAILTIMER_START(extractLocalSubdomainMatrixTime_symbolic, "ExtractLocalSubdomainMatrix_Symbolic"); auto subdomainMap = subdomainMatrix->getRowMap(); const SC zero = ScalarTraits::zero(); for (unsigned i=0; igetLocalNumElements(); i++) { ArrayView indices; ArrayView values; subdomainMatrix->getGlobalRowView(subdomainMap->getGlobalElement(i),indices,values); LO size = indices.size(); if (size>0) { Array indicesLocal; Array valuesLocal; for (LO j=0; jgetLocalElement(indices[j]); if (localIndex>=0) { indicesLocal.push_back(localIndex); valuesLocal.push_back(zero); } } localSubdomainMatrix->insertGlobalValues(i,indicesLocal(),valuesLocal()); } } localSubdomainMatrix->fillComplete(); return; } template void ExtractLocalSubdomainMatrix_Compute(RCP > globalMatrix, RCP< Matrix > subdomainMatrix, RCP< Matrix > localSubdomainMatrix) { RCP > scatter = ImportFactory::Build(globalMatrix->getRowMap(), subdomainMatrix->getRowMap()); ExtractLocalSubdomainMatrix_Compute(scatter, globalMatrix, subdomainMatrix, localSubdomainMatrix); } template void ExtractLocalSubdomainMatrix_Compute(RCP< Import > scatter, RCP > globalMatrix, RCP< Matrix > subdomainMatrix, RCP< Matrix > localSubdomainMatrix) { FROSCH_DETAILTIMER_START(extractLocalSubdomainMatrixTime_compute, "ExtractLocalSubdomainMatrix_Compute"); const SC zero = ScalarTraits::zero(); auto subdomainRowMap = subdomainMatrix->getRowMap(); subdomainMatrix->setAllToScalar(zero); subdomainMatrix->resumeFill(); subdomainMatrix->doImport(*globalMatrix, *scatter, ADD); // "fillComplete" is quite expensive, and it seem to be cheaper to replace values each row at a time #if 0 //defined(HAVE_XPETRA_TPETRA) if (globalMatrix->getRowMap()->lib() == UseTpetra) { // NOTE: this fillComplete is expensive on GPUs subdomainMatrix->fillComplete(); auto devSubdomainMap = subdomainRowMap->getLocalMap(); auto devSubdomainMatrix = subdomainMatrix->getLocalMatrixDevice(); auto devLocalSubdomainMatrix = localSubdomainMatrix->getLocalMatrixDevice(); using UN = unsigned; using execution_space = typename Map::local_map_type::execution_space; UN numRows = subdomainRowMap->getLocalNumElements(); Kokkos::RangePolicy policy_row (0, numRows); Kokkos::parallel_for( "FROSch :: ExtractLocalSubdomainMatrix_Compute", policy_row, KOKKOS_LAMBDA(const UN i) { for (UN j=devSubdomainMatrix.graph.row_map(i); j=0) { // look for the same column index in localSubdomainMatrix for (UN k=devLocalSubdomainMatrix.graph.row_map(i); kresumeFill(); size_t max_nnz = localSubdomainMatrix->getLocalMaxNumRowEntries(); std::vector local_cols_vector (max_nnz); std::vector local_vals_vector (max_nnz); for (unsigned i=0; igetLocalNumElements(); i++) { ArrayView global_indices; ArrayView global_values; subdomainMatrix->getGlobalRowView(subdomainRowMap->getGlobalElement(i),global_indices,global_values); LO size = global_indices.size(); if (size>0) { // using "workspace" not to overwrite what's in localSubdomainMatrix size_t new_nnz = 0; ArrayView local_cols (local_cols_vector); ArrayView local_vals (local_vals_vector); for (LO j=0; jgetLocalElement(global_indices[j]); if (localIndex>=0) { local_cols[new_nnz] = localIndex; local_vals[new_nnz] = global_values[j]; new_nnz ++; } } localSubdomainMatrix->replaceLocalValues(i, local_cols(0, new_nnz), local_vals(0, new_nnz)); } } RCP fillCompleteParams(new ParameterList); fillCompleteParams->set("No Nonlocal Changes", true); localSubdomainMatrix->fillComplete(fillCompleteParams); } return; } template RCP > ExtractLocalSubdomainMatrix(RCP > globalMatrix, RCP > map, SC value) { FROSCH_DETAILTIMER_START(extractLocalSubdomainMatrixTime,"ExtractLocalSubdomainMatrix"); RCP > subdomainMatrix = MatrixFactory::Build(map,2*globalMatrix->getGlobalMaxNumRowEntries()); RCP > scatter = ImportFactory::Build(globalMatrix->getRowMap(),map); subdomainMatrix->doImport(*globalMatrix,*scatter,ADD); //cout << *subdomainMatrix << endl; RCP > SerialComm = rcp(new MpiComm(MPI_COMM_SELF)); RCP > localSubdomainMap = MapFactory::Build(map->lib(),map->getLocalNumElements(),0,SerialComm); RCP > localSubdomainMatrix = MatrixFactory::Build(localSubdomainMap,globalMatrix->getGlobalMaxNumRowEntries()); for (unsigned i=0; igetLocalNumElements(); i++) { ArrayView indices; ArrayView values; subdomainMatrix->getGlobalRowView(map->getGlobalElement(i),indices,values); LO size = indices.size(); if (size>0) { Array indicesGlobal; Array valuesLocal; for (LO j=0; jgetLocalElement(indices[j]); if (localIndex>=0) { indicesGlobal.push_back(localIndex); valuesLocal.push_back(value); } } localSubdomainMatrix->insertGlobalValues(i,indicesGlobal(),valuesLocal()); } } localSubdomainMatrix->fillComplete(); return localSubdomainMatrix.getConst(); } template int UpdateLocalSubdomainMatrix(RCP > globalMatrix, RCP > &map, RCP > &localSubdomainMatrix) { FROSCH_DETAILTIMER_START(updateLocalSubdomainMatrixTime,"UpdateLocalSubdomainMatrix"); RCP > subdomainMatrix = MatrixFactory::Build(map,2*globalMatrix->getGlobalMaxNumRowEntries()); RCP > scatter = ImportFactory::Build(globalMatrix->getRowMap(),map); subdomainMatrix->doImport(*globalMatrix,*scatter,ADD); localSubdomainMatrix->resumeFill(); for (unsigned i=0; igetLocalNumElements(); i++) { ArrayView indices; ArrayView values; subdomainMatrix->getGlobalRowView(map->getGlobalElement(i),indices,values); LO size = indices.size(); if (size>0) { Array indicesLocal; Array valuesLocal; for (LO j=0; jgetLocalElement(indices[j]); if (localIndex>=0) { indicesLocal.push_back(localIndex); valuesLocal.push_back(values[j]); } } localSubdomainMatrix->replaceLocalValues(i,indicesLocal(),valuesLocal()); } } localSubdomainMatrix->fillComplete(); return 0; } template int BuildSubmatrices(RCP > k, ArrayView indI, RCP > &kII, RCP > &kIJ, RCP > &kJI, RCP > &kJJ) { FROSCH_DETAILTIMER_START(buildSubmatricesTime,"BuildSubmatrices"); // We need four Maps const GO INVALID = Teuchos::OrdinalTraits::invalid(); RCP > mapI = MapFactory::Build(k->getRowMap()->lib(),INVALID,indI(),0,k->getRowMap()->getComm()); RCP > mapILocal = MapFactory::Build(k->getRowMap()->lib(),INVALID,indI.size(),0,k->getRowMap()->getComm()); Array indJ; for (unsigned i=0; igetLocalNumRows(); i++) { if (mapI->getLocalElement(i)<0) { indJ.push_back(i); } } RCP > mapJ = MapFactory::Build(k->getRowMap()->lib(),INVALID,indJ(),0,k->getRowMap()->getComm()); RCP > mapJLocal = MapFactory::Build(k->getRowMap()->lib(),INVALID,indJ.size(),0,k->getRowMap()->getComm()); RCP > colMap = k->getColMap(); #if defined(HAVE_XPETRA_TPETRA) if (k->getRowMap()->lib() == UseTpetra) { using crsmat_type = typename Matrix::local_matrix_type; using graph_type = typename crsmat_type::StaticCrsGraphType; using rowptr_type = typename graph_type::row_map_type::non_const_type; using indices_type = typename graph_type::entries_type::non_const_type; using values_type = typename crsmat_type::values_type::non_const_type; using execution_space = typename Map::local_map_type::execution_space; using UN = unsigned; UN numRows = k->getLocalNumRows(); UN numRowsI = indI.size(); UN numRowsJ = indJ.size(); rowptr_type RowptrII (Kokkos::ViewAllocateWithoutInitializing("RowptrII"), numRowsI+1); rowptr_type RowptrIJ (Kokkos::ViewAllocateWithoutInitializing("RowptrIJ"), numRowsI+1); rowptr_type RowptrJI (Kokkos::ViewAllocateWithoutInitializing("RowptrJI"), numRowsJ+1); rowptr_type RowptrJJ (Kokkos::ViewAllocateWithoutInitializing("RowptrJJ"), numRowsJ+1); // count nnz on each blocks Kokkos::deep_copy(RowptrII, 0); Kokkos::deep_copy(RowptrIJ, 0); Kokkos::deep_copy(RowptrJI, 0); Kokkos::deep_copy(RowptrJJ, 0); auto localMapI = mapI->getLocalMap(); auto localMapJ = mapJ->getLocalMap(); auto localColMap = colMap->getLocalMap(); auto localK = k->getLocalMatrixDevice(); Kokkos::RangePolicy policy_row (0, numRows); Kokkos::parallel_for( "FROSch_BuildSubmatrices::countNnz", policy_row, KOKKOS_LAMBDA(const UN i) { LO tmp1=localMapI.getLocalElement(i); LO tmp2=0; if (tmp1>=0) { for (UN j=localK.graph.row_map(i); j=0) { RowptrII(tmp1+1) ++; } else { RowptrIJ(tmp1+1) ++; } } } else { tmp1=localMapJ.getLocalElement((GO) i); for (UN j=localK.graph.row_map(i); j=0) { RowptrJI(tmp1+1) ++; } else { RowptrJJ(tmp1+1) ++; } } } } ); Kokkos::fence(); // cout nnz UN nnzII = 0; UN nnzIJ = 0; UN nnzJI = 0; UN nnzJJ = 0; Kokkos::parallel_reduce("FROSch_CoarseSpace::BuildSubmatrices:nnzII", 1+numRowsI, KOKKOS_LAMBDA(const int &i, UN &lsum) { lsum += RowptrII[i]; }, nnzII); Kokkos::parallel_reduce("FROSch_CoarseSpace::BuildSubmatrices:nnzIJ", 1+numRowsI, KOKKOS_LAMBDA(const int &i, UN &lsum) { lsum += RowptrIJ[i]; }, nnzIJ); Kokkos::parallel_reduce("FROSch_CoarseSpace::BuildSubmatrices:nnzJI", 1+numRowsJ, KOKKOS_LAMBDA(const int &i, UN &lsum) { lsum += RowptrJI[i]; }, nnzJI); Kokkos::parallel_reduce("FROSch_CoarseSpace::BuildSubmatrices:nnzJJ", 1+numRowsJ, KOKKOS_LAMBDA(const int &i, UN &lsum) { lsum += RowptrJJ[i]; }, nnzJJ); // make it into offsets #if KOKKOSKERNELS_VERSION >= 40199 KokkosKernels::Impl::kk_inclusive_parallel_prefix_sum (1+numRowsI, RowptrII); KokkosKernels::Impl::kk_inclusive_parallel_prefix_sum (1+numRowsI, RowptrIJ); KokkosKernels::Impl::kk_inclusive_parallel_prefix_sum (1+numRowsJ, RowptrJI); KokkosKernels::Impl::kk_inclusive_parallel_prefix_sum (1+numRowsJ, RowptrJJ); #else KokkosKernels::Impl::kk_inclusive_parallel_prefix_sum (1+numRowsI, RowptrII); KokkosKernels::Impl::kk_inclusive_parallel_prefix_sum (1+numRowsI, RowptrIJ); KokkosKernels::Impl::kk_inclusive_parallel_prefix_sum (1+numRowsJ, RowptrJI); KokkosKernels::Impl::kk_inclusive_parallel_prefix_sum (1+numRowsJ, RowptrJJ); #endif // allocate kII block indices_type IndicesII (Kokkos::ViewAllocateWithoutInitializing("IndicesII"), nnzII); values_type ValuesII (Kokkos::ViewAllocateWithoutInitializing("ValuesII"), nnzII); // allocate kIJ block indices_type IndicesIJ (Kokkos::ViewAllocateWithoutInitializing("IndicesIJ"), nnzIJ); values_type ValuesIJ (Kokkos::ViewAllocateWithoutInitializing("ValuesIJ"), nnzIJ); // allocate kJI block indices_type IndicesJI (Kokkos::ViewAllocateWithoutInitializing("IndicesJI"), nnzJI); values_type ValuesJI (Kokkos::ViewAllocateWithoutInitializing("ValuesJI"), nnzJI); // allocate kJJ block indices_type IndicesJJ (Kokkos::ViewAllocateWithoutInitializing("IndicesJJ"), nnzJJ); values_type ValuesJJ (Kokkos::ViewAllocateWithoutInitializing("ValuesJJ"), nnzJJ); // fill in all the blocks Kokkos::parallel_for( "BuildSubmatrices::countNnz", policy_row, KOKKOS_LAMBDA(const UN i) { LO tmp1=localMapI.getLocalElement((GO) i); LO tmp2=0; if (tmp1>=0) { UN nnz_i = RowptrII[tmp1]; UN nnz_j = RowptrIJ[tmp1]; for (UN j=localK.graph.row_map(i); j=0) { IndicesII(nnz_i) = tmp2; ValuesII(nnz_i) = localK.values[j]; nnz_i++; } else { tmp2 = localMapJ.getLocalElement(colid); IndicesIJ(nnz_j) = tmp2; ValuesIJ(nnz_j) = localK.values[j]; nnz_j++; } } } else { tmp1=localMapJ.getLocalElement((GO) i); UN nnz_i = RowptrJI[tmp1]; UN nnz_j = RowptrJJ[tmp1]; for (UN j=localK.graph.row_map(i); j=0) { IndicesJI(nnz_i) = tmp2; ValuesJI(nnz_i) = localK.values[j]; nnz_i++; } else { tmp2 = localMapJ.getLocalElement(colid); IndicesJJ(nnz_j) = tmp2; ValuesJJ(nnz_j) = localK.values[j]; nnz_j++; } } } } ); Kokkos::fence(); Teuchos::RCP params = Teuchos::rcp (new Teuchos::ParameterList()); params->set("sorted", false); // create kII graph_type crsgraphII (IndicesII, RowptrII); crsmat_type LocalII = crsmat_type ("CrsMatrix", numRowsI, ValuesII, crsgraphII); kII = MatrixFactory::Build(LocalII, mapILocal, mapILocal, mapILocal, mapILocal, params); // create kIJ graph_type crsgraphIJ (IndicesIJ, RowptrIJ); crsmat_type LocalIJ = crsmat_type ("CrsMatrix", numRowsI, ValuesIJ, crsgraphIJ); kIJ = MatrixFactory::Build(LocalIJ, mapILocal, mapJLocal, mapJLocal, mapILocal, params); // create kJI graph_type crsgraphJI (IndicesJI, RowptrJI); crsmat_type LocalJI = crsmat_type ("CrsMatrix", numRowsJ, ValuesJI, crsgraphJI); kJI = MatrixFactory::Build(LocalJI, mapJLocal, mapILocal, mapILocal, mapJLocal, params); // create kJJ graph_type crsgraphJJ (IndicesJJ, RowptrJJ); crsmat_type LocalJJ = crsmat_type ("CrsMatrix", numRowsJ, ValuesJJ, crsgraphJJ); kJJ = MatrixFactory::Build(LocalJJ, mapJLocal, mapJLocal, mapJLocal, mapJLocal, params); } else #endif { kII = MatrixFactory::Build(mapILocal,min((LO) k->getGlobalMaxNumRowEntries(),(LO) indI.size())); kIJ = MatrixFactory::Build(mapILocal,min((LO) k->getGlobalMaxNumRowEntries(),(LO) indJ.size())); kJI = MatrixFactory::Build(mapJLocal,min((LO) k->getGlobalMaxNumRowEntries(),(LO) indI.size())); kJJ = MatrixFactory::Build(mapJLocal,min((LO) k->getGlobalMaxNumRowEntries(),(LO) indJ.size())); for (unsigned i=0; igetLocalNumRows(); i++) { ArrayView indices; ArrayView values; k->getLocalRowView(i,indices,values); //cout << numEntries << endl; Array indicesI; Array valuesI; Array indicesJ; Array valuesJ; LO tmp1=mapI->getLocalElement(i); LO tmp2=0; if (tmp1>=0) { for (LO j=0; jgetLocalElement(colMap->getGlobalElement(indices[j])); if (tmp2>=0) { indicesI.push_back(tmp2); valuesI.push_back(values[j]); } else { indicesJ.push_back(mapJ->getLocalElement(colMap->getGlobalElement(indices[j]))); valuesJ.push_back(values[j]); } } kII->insertGlobalValues(tmp1,indicesI(),valuesI()); kIJ->insertGlobalValues(tmp1,indicesJ(),valuesJ()); } else { tmp1=mapJ->getLocalElement((GO) i); for (LO j=0; jgetLocalElement(colMap->getGlobalElement(indices[j])); if (tmp2>=0) { indicesI.push_back(tmp2); valuesI.push_back(values[j]); } else { indicesJ.push_back(mapJ->getLocalElement(colMap->getGlobalElement(indices[j]))); valuesJ.push_back(values[j]); } } kJI->insertGlobalValues(tmp1,indicesI(),valuesI()); kJJ->insertGlobalValues(tmp1,indicesJ(),valuesJ()); } } kII->fillComplete(mapILocal,mapILocal); kIJ->fillComplete(mapJLocal,mapILocal); kJI->fillComplete(mapILocal,mapJLocal); kJJ->fillComplete(mapJLocal,mapJLocal); } return 0; } template int BuildSubmatrix(RCP > k, ArrayView indI, RCP > &kII) { FROSCH_DETAILTIMER_START(buildSubmatrixTime,"BuildSubmatrix"); const GO INVALID = Teuchos::OrdinalTraits::invalid(); //RCP fancy = fancyOStream(rcpFromRef(cout)); RCP > mapI = MapFactory::Build(k->getRowMap()->lib(),INVALID,indI(),0,k->getRowMap()->getComm()); kII = MatrixFactory::Build(mapI,min((LO) k->getGlobalMaxNumRowEntries(),(LO) indI.size())); GO maxGID = mapI->getMaxAllGlobalIndex(); GO minGID = mapI->getMinAllGlobalIndex(); for (unsigned i=0; igetLocalNumRows(); i++) { ArrayView indices; ArrayView values; k->getLocalRowView(i,indices,values); Array indicesI; Array valuesI; LO tmp1=mapI->getLocalElement(k->getRowMap()->getGlobalElement(i)); GO tmp2=0; if (tmp1>=0) { for (LO j=0; jgetColMap()->getGlobalElement(indices[j]); if (minGID<=tmp2 && tmp2<=maxGID) { indicesI.push_back(tmp2); valuesI.push_back(values[j]); } } kII->insertGlobalValues(mapI->getGlobalElement(tmp1),indicesI(),valuesI()); } } kII->fillComplete(mapI,mapI); return 0; } template int BuildSubgraph(RCP > k, ArrayView indI, RCP > &kII) { FROSCH_DETAILTIMER_START(buildSubgraphTime,"BuildSubgraph"); const GO INVALID = Teuchos::OrdinalTraits::invalid(); //RCP fancy = fancyOStream(rcpFromRef(cout)); RCP > mapI = MapFactory::Build(k->getRowMap()->lib(),INVALID,indI(),0,k->getRowMap()->getComm()); kII = CrsGraphFactory::Build(mapI,min((LO) k->getGlobalMaxNumRowEntries(),(LO) indI.size())); GO maxGID = mapI->getMaxAllGlobalIndex(); GO minGID = mapI->getMinAllGlobalIndex(); for (unsigned i=0; igetLocalNumRows(); i++) { ArrayView indices; k->getLocalRowView(i,indices); Array indicesI; LO tmp1=mapI->getLocalElement(k->getRowMap()->getGlobalElement(i)); GO tmp2=0; if (tmp1>=0) { for (LO j=0; jgetColMap()->getGlobalElement(indices[j]); if (minGID<=tmp2 && tmp2<=maxGID) { indicesI.push_back(tmp2); } } kII->insertGlobalValues(mapI->getGlobalElement(tmp1),indicesI()); } } kII->fillComplete(mapI,mapI); return 0; } } #endif