// @HEADER // ***************************************************************************** // MueLu: A package for multigrid based preconditioning // // Copyright 2012 NTESS and the MueLu contributors. // SPDX-License-Identifier: BSD-3-Clause // ***************************************************************************** // @HEADER #ifndef MUELU_LOWPRECISIONFACTORY_DEF_HPP #define MUELU_LOWPRECISIONFACTORY_DEF_HPP #include #include #include #include #include "MueLu_LowPrecisionFactory_decl.hpp" #include "MueLu_Level.hpp" #include "MueLu_Monitor.hpp" namespace MueLu { template RCP LowPrecisionFactory::GetValidParameterList() const { RCP validParamList = rcp(new ParameterList()); validParamList->set("matrix key", "A", ""); validParamList->set >("R", Teuchos::null, "Generating factory of the matrix A to be converted to lower precision"); validParamList->set >("A", Teuchos::null, "Generating factory of the matrix A to be converted to lower precision"); validParamList->set >("P", Teuchos::null, "Generating factory of the matrix A to be converted to lower precision"); return validParamList; } template void LowPrecisionFactory::DeclareInput(Level& currentLevel) const { const ParameterList& pL = GetParameterList(); std::string matrixKey = pL.get("matrix key"); Input(currentLevel, matrixKey); } template void LowPrecisionFactory::Build(Level& currentLevel) const { using Teuchos::ParameterList; const ParameterList& pL = GetParameterList(); std::string matrixKey = pL.get("matrix key"); FactoryMonitor m(*this, "Converting " + matrixKey + " to half precision", currentLevel); RCP A = Get >(currentLevel, matrixKey); GetOStream(Warnings) << "Matrix not converted to half precision. This only works for Tpetra and when both Scalar and HalfScalar have been instantiated." << std::endl; Set(currentLevel, matrixKey, A); } #if defined(HAVE_TPETRA_INST_DOUBLE) && defined(HAVE_TPETRA_INST_FLOAT) template RCP LowPrecisionFactory::GetValidParameterList() const { RCP validParamList = rcp(new ParameterList()); validParamList->set("matrix key", "A", ""); validParamList->set >("R", Teuchos::null, "Generating factory of the matrix A to be converted to lower precision"); validParamList->set >("A", Teuchos::null, "Generating factory of the matrix A to be converted to lower precision"); validParamList->set >("P", Teuchos::null, "Generating factory of the matrix A to be converted to lower precision"); return validParamList; } template void LowPrecisionFactory::DeclareInput(Level& currentLevel) const { const ParameterList& pL = GetParameterList(); std::string matrixKey = pL.get("matrix key"); Input(currentLevel, matrixKey); } template void LowPrecisionFactory::Build(Level& currentLevel) const { using Teuchos::ParameterList; using HalfScalar = typename Teuchos::ScalarTraits::halfPrecision; const ParameterList& pL = GetParameterList(); std::string matrixKey = pL.get("matrix key"); FactoryMonitor m(*this, "Converting " + matrixKey + " to half precision", currentLevel); RCP A = Get >(currentLevel, matrixKey); if ((A->getRowMap()->lib() == Xpetra::UseTpetra) && std::is_same::value) { auto tpA = toTpetra(A); auto tpLowA = tpA->template convert(); auto tpLowOpA = rcp(new Tpetra::CrsMatrixMultiplyOp(tpLowA)); auto xpTpLowOpA = rcp(new TpetraOperator(tpLowOpA)); auto xpLowOpA = rcp_dynamic_cast(xpTpLowOpA); Set(currentLevel, matrixKey, xpLowOpA); return; } GetOStream(Warnings) << "Matrix not converted to half precision. This only works for Tpetra and when both Scalar and HalfScalar have been instantiated." << std::endl; Set(currentLevel, matrixKey, A); } #endif #if defined(HAVE_TPETRA_INST_COMPLEX_DOUBLE) && defined(HAVE_TPETRA_INST_COMPLEX_FLOAT) template RCP LowPrecisionFactory, LocalOrdinal, GlobalOrdinal, Node>::GetValidParameterList() const { RCP validParamList = rcp(new ParameterList()); validParamList->set("matrix key", "A", ""); validParamList->set >("R", Teuchos::null, "Generating factory of the matrix A to be converted to lower precision"); validParamList->set >("A", Teuchos::null, "Generating factory of the matrix A to be converted to lower precision"); validParamList->set >("P", Teuchos::null, "Generating factory of the matrix A to be converted to lower precision"); return validParamList; } template void LowPrecisionFactory, LocalOrdinal, GlobalOrdinal, Node>::DeclareInput(Level& currentLevel) const { const ParameterList& pL = GetParameterList(); std::string matrixKey = pL.get("matrix key"); Input(currentLevel, matrixKey); } template void LowPrecisionFactory, LocalOrdinal, GlobalOrdinal, Node>::Build(Level& currentLevel) const { using Teuchos::ParameterList; using HalfScalar = typename Teuchos::ScalarTraits::halfPrecision; const ParameterList& pL = GetParameterList(); std::string matrixKey = pL.get("matrix key"); FactoryMonitor m(*this, "Converting " + matrixKey + " to half precision", currentLevel); RCP A = Get >(currentLevel, matrixKey); if ((A->getRowMap()->lib() == Xpetra::UseTpetra) && std::is_same >::value) { auto tpA = toTpetra(A); auto tpLowA = tpA->template convert(); auto tpLowOpA = rcp(new Tpetra::CrsMatrixMultiplyOp(tpLowA)); auto xpTpLowOpA = rcp(new TpetraOperator(tpLowOpA)); auto xpLowOpA = rcp_dynamic_cast(xpTpLowOpA); Set(currentLevel, matrixKey, xpLowOpA); return; } GetOStream(Warnings) << "Matrix not converted to half precision. This only works for Tpetra and when both Scalar and HalfScalar have been instantiated." << std::endl; Set(currentLevel, matrixKey, A); } #endif } // namespace MueLu #endif // MUELU_LOWPRECISIONFACTORY_DEF_HPP