// @HEADER // ***************************************************************************** // MueLu: A package for multigrid based preconditioning // // Copyright 2012 NTESS and the MueLu contributors. // SPDX-License-Identifier: BSD-3-Clause // ***************************************************************************** // @HEADER #ifndef MUELU_CONSTRAINT_DEF_HPP #define MUELU_CONSTRAINT_DEF_HPP #include #include #include #include #include #include #include #include #include #include #include #include "MueLu_Exceptions.hpp" #include "MueLu_Constraint_decl.hpp" namespace MueLu { template void Constraint::Setup(const MultiVector& /* B */, const MultiVector& Bc, RCP Ppattern) { const size_t NSDim = Bc.getNumVectors(); Ppattern_ = Ppattern; size_t numRows = Ppattern_->getLocalNumRows(); XXtInv_.resize(numRows); RCP importer = Ppattern_->getImporter(); X_ = MultiVectorFactory::Build(Ppattern_->getColMap(), NSDim); if (!importer.is_null()) X_->doImport(Bc, *importer, Xpetra::INSERT); else *X_ = Bc; std::vector Xval(NSDim); for (size_t j = 0; j < NSDim; j++) Xval[j] = X_->getData(j).get(); SC zero = Teuchos::ScalarTraits::zero(); SC one = Teuchos::ScalarTraits::one(); Teuchos::BLAS blas; Teuchos::LAPACK lapack; LO lwork = 3 * NSDim; ArrayRCP IPIV(NSDim); ArrayRCP WORK(lwork); for (size_t i = 0; i < numRows; i++) { Teuchos::ArrayView indices; Ppattern_->getLocalRowView(i, indices); size_t nnz = indices.size(); XXtInv_[i] = Teuchos::SerialDenseMatrix(NSDim, NSDim, false /*zeroOut*/); Teuchos::SerialDenseMatrix& XXtInv = XXtInv_[i]; if (NSDim == 1) { SC d = zero; for (size_t j = 0; j < nnz; j++) d += Xval[0][indices[j]] * Xval[0][indices[j]]; XXtInv(0, 0) = one / d; } else { Teuchos::SerialDenseMatrix locX(NSDim, nnz, false /*zeroOut*/); for (size_t j = 0; j < nnz; j++) for (size_t k = 0; k < NSDim; k++) locX(k, j) = Xval[k][indices[j]]; // XXtInv_ = (locX*locX^T)^{-1} blas.GEMM(Teuchos::NO_TRANS, Teuchos::CONJ_TRANS, NSDim, NSDim, nnz, one, locX.values(), locX.stride(), locX.values(), locX.stride(), zero, XXtInv.values(), XXtInv.stride()); LO info; // Compute LU factorization using partial pivoting with row exchanges lapack.GETRF(NSDim, NSDim, XXtInv.values(), XXtInv.stride(), IPIV.get(), &info); // Use the computed factorization to compute the inverse lapack.GETRI(NSDim, XXtInv.values(), XXtInv.stride(), IPIV.get(), WORK.get(), lwork, &info); } } } //! \note We assume that the graph of Projected is the same as Ppattern_ template void Constraint::Apply(const Matrix& P, Matrix& Projected) const { // We check only row maps. Column may be different. TEUCHOS_TEST_FOR_EXCEPTION(!P.getRowMap()->isSameAs(*Projected.getRowMap()), Exceptions::Incompatible, "Row maps are incompatible"); const size_t NSDim = X_->getNumVectors(); const size_t numRows = P.getLocalNumRows(); const Map& colMap = *P.getColMap(); const Map& PColMap = *Projected.getColMap(); Projected.resumeFill(); Teuchos::ArrayView indices, pindices; Teuchos::ArrayView values, pvalues; Teuchos::Array valuesAll(colMap.getLocalNumElements()), newValues; LO invalid = Teuchos::OrdinalTraits::invalid(); LO oneLO = Teuchos::OrdinalTraits::one(); SC zero = Teuchos::ScalarTraits::zero(); SC one = Teuchos::ScalarTraits::one(); std::vector Xval(NSDim); for (size_t j = 0; j < NSDim; j++) Xval[j] = X_->getData(j).get(); for (size_t i = 0; i < numRows; i++) { P.getLocalRowView(i, indices, values); Projected.getLocalRowView(i, pindices, pvalues); size_t nnz = indices.size(); // number of nonzeros in the supplied matrix size_t pnnz = pindices.size(); // number of nonzeros in the constrained matrix newValues.resize(pnnz); // Step 1: fix stencil // Projected *must* already have the correct stencil // Step 2: copy correct stencil values // The algorithm is very similar to the one used in the calculation of // Frobenius dot product, see src/Transfers/Energy-Minimization/Solvers/MueLu_CGSolver_def.hpp // NOTE: using extra array allows us to skip the search among indices for (size_t j = 0; j < nnz; j++) valuesAll[indices[j]] = values[j]; for (size_t j = 0; j < pnnz; j++) { LO ind = colMap.getLocalElement(PColMap.getGlobalElement(pindices[j])); // FIXME: we could do that before the full loop just once if (ind != invalid) // index indices[j] is part of template, copy corresponding value newValues[j] = valuesAll[ind]; else newValues[j] = zero; } for (size_t j = 0; j < nnz; j++) valuesAll[indices[j]] = zero; // Step 3: project to the space Teuchos::SerialDenseMatrix& XXtInv = XXtInv_[i]; Teuchos::SerialDenseMatrix locX(NSDim, pnnz, false); for (size_t j = 0; j < pnnz; j++) for (size_t k = 0; k < NSDim; k++) locX(k, j) = Xval[k][pindices[j]]; Teuchos::SerialDenseVector val(pnnz, false), val1(NSDim, false), val2(NSDim, false); for (size_t j = 0; j < pnnz; j++) val[j] = newValues[j]; Teuchos::BLAS blas; // val1 = locX * val; blas.GEMV(Teuchos::NO_TRANS, NSDim, pnnz, one, locX.values(), locX.stride(), val.values(), oneLO, zero, val1.values(), oneLO); // val2 = XXtInv * val1 blas.GEMV(Teuchos::NO_TRANS, NSDim, NSDim, one, XXtInv.values(), XXtInv.stride(), val1.values(), oneLO, zero, val2.values(), oneLO); // val = X^T * val2 blas.GEMV(Teuchos::CONJ_TRANS, NSDim, pnnz, one, locX.values(), locX.stride(), val2.values(), oneLO, zero, val.values(), oneLO); for (size_t j = 0; j < pnnz; j++) newValues[j] -= val[j]; Projected.replaceLocalValues(i, pindices, newValues); } Projected.fillComplete(Projected.getDomainMap(), Projected.getRangeMap()); // FIXME: maps needed? } } // namespace MueLu #endif // ifndef MUELU_CONSTRAINT_DEF_HPP