From 3e9edeff98136b909ea3c1eb880d5ce12acfe0fe Mon Sep 17 00:00:00 2001 From: Louis Goyard Date: Mon, 13 Jun 2022 16:59:29 +0200 Subject: [PATCH 01/13] WIP: remove plugin tags Remove LinearAlgebra for now --- plugins/hypre/CMakeLists.txt | 1 - .../hypre/examples/hypre_example_solve.cpp | 50 ++--- plugins/hypre/include/alien/hypre/backend.h | 58 +++--- plugins/hypre/include/alien/hypre/options.h | 43 +---- .../converters/hypre_to_simplecsr_vector.cpp | 6 +- .../converters/simplecsr_to_hypre_matrix.cpp | 16 +- .../converters/simplecsr_to_hypre_vector.cpp | 6 +- plugins/hypre/src/hypre_linear_algebra.cpp | 173 ------------------ plugins/hypre/src/hypre_linear_solver.cpp | 45 +++-- plugins/hypre/src/hypre_linear_solver.h | 24 ++- plugins/hypre/src/hypre_matrix.cpp | 6 +- plugins/hypre/src/hypre_matrix.h | 4 +- plugins/hypre/src/hypre_vector.cpp | 6 +- plugins/hypre/src/hypre_vector.h | 2 +- src/core/alien/core/backend/BackEnd.h | 75 +++++++- src/core/alien/core/backend/LinearSolver.h | 24 ++- src/core/alien/core/backend/LinearSolverT.h | 44 ++--- src/core/alien/core/impl/MultiMatrixImpl.h | 50 ++--- src/core/alien/data/CompositeMatrix.cc | 2 +- src/core/alien/kernels/dok/DoKBackEnd.h | 5 +- .../redistributor/RedistributorBackEnd.h | 5 +- 21 files changed, 244 insertions(+), 401 deletions(-) delete mode 100644 plugins/hypre/src/hypre_linear_algebra.cpp diff --git a/plugins/hypre/CMakeLists.txt b/plugins/hypre/CMakeLists.txt index 72ee72afc..f2184d5d3 100644 --- a/plugins/hypre/CMakeLists.txt +++ b/plugins/hypre/CMakeLists.txt @@ -14,7 +14,6 @@ add_library(hypre_wrapper src/hypre_vector.h src/hypre_matrix.cpp src/hypre_matrix.h - src/hypre_linear_algebra.cpp src/hypre_linear_solver.cpp src/converters/hypre_to_simplecsr_vector.cpp src/converters/simplecsr_to_hypre_vector.cpp diff --git a/plugins/hypre/examples/hypre_example_solve.cpp b/plugins/hypre/examples/hypre_example_solve.cpp index ae5d51dba..8f3e5ad0b 100644 --- a/plugins/hypre/examples/hypre_example_solve.cpp +++ b/plugins/hypre/examples/hypre_example_solve.cpp @@ -16,12 +16,14 @@ * SPDX-License-Identifier: Apache-2.0 */ -#include - #include #include +#include +#include +#include + int test() { auto* pm = Arccore::MessagePassing::Mpi::StandaloneMpiMessagePassingMng::create(MPI_COMM_WORLD); @@ -73,9 +75,10 @@ int test() Alien::Vector b(size, pm); - Alien::Hypre::LinearAlgebra algebra; + Alien::BackEnd::BackendHandle* backendHandle = Alien::BackEnd::loadBackend("hypre"); + //Alien::ILinearAlgebra algebra = backendHandle->getLinearAlgebra(); - algebra.mult(A, xe, b); + //algebra.mult(A, xe, b); Alien::Vector x(size, pm); @@ -89,7 +92,8 @@ int test() // // auto solver = Alien::Hypre::LinearSolver (options); - auto solver = Alien::Hypre::LinearSolver(); + //auto solver = Alien::Hypre::LinearSolver(); + auto solver = Alien::LinearSolver("hypre"); solver.solve(A, b, x); @@ -97,28 +101,28 @@ int test() Alien::Vector r(size, pm); - { - Alien::Vector tmp(size, pm); - tm->info() << "t = Ax"; - algebra.mult(A, x, tmp); - tm->info() << "r = t"; - algebra.copy(tmp, r); - tm->info() << "r -= b"; - algebra.axpy(-1., b, r); - } + //{ + // Alien::Vector tmp(size, pm); + // tm->info() << "t = Ax"; + // algebra.mult(A, x, tmp); + // tm->info() << "r = t"; + // algebra.copy(tmp, r); + // tm->info() << "r -= b"; + // algebra.axpy(-1., b, r); + //} - auto norm = algebra.norm2(r); + //auto norm = algebra.norm2(r); - tm->info() << " => ||r|| = " << norm; + //tm->info() << " => ||r|| = " << norm; - tm->info() << "* r = || x - xe ||"; + //tm->info() << "* r = || x - xe ||"; - { - tm->info() << "r = x"; - algebra.copy(x, r); - tm->info() << "r -= xe"; - algebra.axpy(-1., xe, r); - } + //{ + // tm->info() << "r = x"; + // algebra.copy(x, r); + // tm->info() << "r -= xe"; + // algebra.axpy(-1., xe, r); + //} tm->info() << " => ||r|| = " << norm; diff --git a/plugins/hypre/include/alien/hypre/backend.h b/plugins/hypre/include/alien/hypre/backend.h index 1dffd41dd..30d6a99e2 100644 --- a/plugins/hypre/include/alien/hypre/backend.h +++ b/plugins/hypre/include/alien/hypre/backend.h @@ -20,22 +20,22 @@ #include #include -#include -namespace Alien::Hypre -{ +namespace Alien { + class Matrix; class Vector; class Options; -extern IInternalLinearSolver* InternalLinearSolverFactory(const Options& options); +class ILinearSolver; + +extern IInternalLinearSolver* LinearSolverFactory(const BackEnd::Options& options); -extern IInternalLinearSolver* InternalLinearSolverFactory(); +extern IInternalLinearSolver* LinearSolverFactory(); +} // namespace Alien -extern IInternalLinearAlgebra* InternalLinearAlgebraFactory(); -} // namespace Alien::Hypre namespace Alien { @@ -49,42 +49,34 @@ namespace BackEnd } // namespace tag } // namespace BackEnd -template <> -struct AlgebraTraits + +class Plugin : public BackEnd::IPlugin { - // types - using matrix_type = Hypre::Matrix; - using vector_type = Hypre::Vector; - using options_type = Hypre::Options; - using algebra_type = IInternalLinearAlgebra; - using solver_type = IInternalLinearSolver; - - // factory to build algebra - static auto* algebra_factory() + // factories to default solver + IInternalLinearSolver* solver_factory() { - return Hypre::InternalLinearAlgebraFactory(); + return LinearSolverFactory(); } // factories to build solver - static auto* solver_factory(const options_type& options) + IInternalLinearSolver* solver_factory(const Alien::BackEnd::Options& options) { - return Hypre::InternalLinearSolverFactory(options); + return LinearSolverFactory(options); } - // factories to build default solver - static auto* solver_factory() + BackEndId name() { return "hypre"; } +}; + +extern "C" { + inline Plugin* create() { - return Hypre::InternalLinearSolverFactory(); + return new Plugin; } - static BackEndId name() { return "hypre"; } -}; + inline void destroy(Plugin* p) + { + delete p; + } +} } // namespace Alien - -// user interface -namespace Alien::Hypre -{ -using LinearSolver = Alien::LinearSolver; -using LinearAlgebra = Alien::LinearAlgebra; -} // namespace Alien::Hypre diff --git a/plugins/hypre/include/alien/hypre/options.h b/plugins/hypre/include/alien/hypre/options.h index 156eb477d..d10ffa012 100644 --- a/plugins/hypre/include/alien/hypre/options.h +++ b/plugins/hypre/include/alien/hypre/options.h @@ -22,7 +22,7 @@ #include #include -namespace Alien::Hypre +namespace Alien { struct OptionTypes { @@ -48,44 +48,9 @@ struct OptionTypes struct Options { Arccore::Integer numIterationsMax_ = 100; - Arccore::Integer numIterationsMax() const { return numIterationsMax_; } - Options& numIterationsMax(Arccore::Integer n) - { - numIterationsMax_ = n; - return *this; - } - Arccore::Real stopCriteriaValue_ = 1.e-10; - Arccore::Real stopCriteriaValue() const { return stopCriteriaValue_; } - Options& stopCriteriaValue(Arccore::Real n) - { - stopCriteriaValue_ = n; - return *this; - } - - bool verbose_ = false; - bool verbose() const { return verbose_; } - Options& verbose(bool n) - { - verbose_ = n; - return *this; - } - - Alien::Hypre::OptionTypes::eSolver solver_ = Alien::Hypre::OptionTypes::GMRES; - Alien::Hypre::OptionTypes::eSolver solver() const { return solver_; } - Options& solver(Alien::Hypre::OptionTypes::eSolver n) - { - solver_ = n; - return *this; - } - - Alien::Hypre::OptionTypes::ePreconditioner preconditioner_ = Alien::Hypre::OptionTypes::AMGPC; - Alien::Hypre::OptionTypes::ePreconditioner preconditioner() const { return preconditioner_; } - Options& preconditioner(Alien::Hypre::OptionTypes::ePreconditioner n) - { - preconditioner_ = n; - return *this; - } + Alien::OptionTypes::eSolver solver_ = Alien::OptionTypes::GMRES; + Alien::OptionTypes::ePreconditioner preconditioner_ = Alien::OptionTypes::AMGPC; }; class OptionsUtils @@ -160,4 +125,4 @@ class OptionsUtils } }; -} // namespace Alien::Hypre \ No newline at end of file +} // namespace Alien::Hypre diff --git a/plugins/hypre/src/converters/hypre_to_simplecsr_vector.cpp b/plugins/hypre/src/converters/hypre_to_simplecsr_vector.cpp index 6e5f9a39a..77503c5ad 100644 --- a/plugins/hypre/src/converters/hypre_to_simplecsr_vector.cpp +++ b/plugins/hypre/src/converters/hypre_to_simplecsr_vector.cpp @@ -23,7 +23,7 @@ #include #include -#include +#include "../../include/alien/hypre/backend.h" class Hypre_to_SimpleCSR_VectorConverter : public Alien::IVectorConverter { @@ -33,7 +33,7 @@ class Hypre_to_SimpleCSR_VectorConverter : public Alien::IVectorConverter virtual ~Hypre_to_SimpleCSR_VectorConverter() {} public: - Alien::BackEndId sourceBackend() const { return Alien::AlgebraTraits::name(); } + Alien::BackEndId sourceBackend() const { return "hypre"; } Alien::BackEndId targetBackend() const { return Alien::AlgebraTraits::name(); } @@ -43,7 +43,7 @@ class Hypre_to_SimpleCSR_VectorConverter : public Alien::IVectorConverter void Hypre_to_SimpleCSR_VectorConverter::convert( const Alien::IVectorImpl* sourceImpl, Alien::IVectorImpl* targetImpl) const { - const auto& v = cast(sourceImpl, sourceBackend()); + const auto& v = cast(sourceImpl, sourceBackend()); auto& v2 = cast>(targetImpl, targetBackend()); alien_debug([&] { diff --git a/plugins/hypre/src/converters/simplecsr_to_hypre_matrix.cpp b/plugins/hypre/src/converters/simplecsr_to_hypre_matrix.cpp index dc2140ff2..da644b64c 100644 --- a/plugins/hypre/src/converters/simplecsr_to_hypre_matrix.cpp +++ b/plugins/hypre/src/converters/simplecsr_to_hypre_matrix.cpp @@ -25,7 +25,7 @@ #include #include -#include +#include "../../include/alien/hypre/backend.h" class SimpleCSR_to_Hypre_MatrixConverter : public Alien::IMatrixConverter { @@ -40,19 +40,19 @@ class SimpleCSR_to_Hypre_MatrixConverter : public Alien::IMatrixConverter return Alien::AlgebraTraits::name(); } - BackEndId targetBackend() const { return Alien::AlgebraTraits::name(); } + BackEndId targetBackend() const { return "hypre"; } void convert(const Alien::IMatrixImpl* sourceImpl, Alien::IMatrixImpl* targetImpl) const; - void _build(const Alien::SimpleCSRMatrix& sourceImpl, Alien::Hypre::Matrix& targetImpl) const; + void _build(const Alien::SimpleCSRMatrix& sourceImpl, Alien::Matrix& targetImpl) const; - void _buildBlock(const Alien::SimpleCSRMatrix& sourceImpl, Alien::Hypre::Matrix& targetImpl) const; + void _buildBlock(const Alien::SimpleCSRMatrix& sourceImpl, Alien::Matrix& targetImpl) const; }; void SimpleCSR_to_Hypre_MatrixConverter::convert(const IMatrixImpl* sourceImpl, IMatrixImpl* targetImpl) const { const auto& v = cast>(sourceImpl, sourceBackend()); - auto& v2 = cast(targetImpl, targetBackend()); + auto& v2 = cast(targetImpl, targetBackend()); alien_debug([&] { cout() << "Converting Alien::SimpleCSRMatrix: " << &v << " to Hypre::Matrix " << &v2; @@ -67,7 +67,7 @@ void SimpleCSR_to_Hypre_MatrixConverter::convert(const IMatrixImpl* sourceImpl, } void SimpleCSR_to_Hypre_MatrixConverter::_build(const Alien::SimpleCSRMatrix& sourceImpl, - Alien::Hypre::Matrix& targetImpl) const + Alien::Matrix& targetImpl) const { const auto& dist = sourceImpl.distribution(); const auto& profile = sourceImpl.getCSRProfile(); @@ -106,7 +106,7 @@ void SimpleCSR_to_Hypre_MatrixConverter::_build(const Alien::SimpleCSRMatrix& sourceImpl, - Alien::Hypre::Matrix& targetImpl) const + Alien::Matrix& targetImpl) const { const auto& dist = sourceImpl.distribution(); const auto& profile = sourceImpl.getCSRProfile(); @@ -176,5 +176,3 @@ void SimpleCSR_to_Hypre_MatrixConverter::_buildBlock(const Alien::SimpleCSRMatri targetImpl.assemble(); } - -REGISTER_MATRIX_CONVERTER(SimpleCSR_to_Hypre_MatrixConverter); diff --git a/plugins/hypre/src/converters/simplecsr_to_hypre_vector.cpp b/plugins/hypre/src/converters/simplecsr_to_hypre_vector.cpp index 35b3dab9d..8a4f0e4e8 100644 --- a/plugins/hypre/src/converters/simplecsr_to_hypre_vector.cpp +++ b/plugins/hypre/src/converters/simplecsr_to_hypre_vector.cpp @@ -35,7 +35,7 @@ class SimpleCSR_to_Hypre_VectorConverter : public Alien::IVectorConverter public: Alien::BackEndId sourceBackend() const { return Alien::AlgebraTraits::name(); } - Alien::BackEndId targetBackend() const { return Alien::AlgebraTraits::name(); } + Alien::BackEndId targetBackend() const { return "hypre"; } void convert(const Alien::IVectorImpl* sourceImpl, Alien::IVectorImpl* targetImpl) const; }; @@ -44,7 +44,7 @@ void SimpleCSR_to_Hypre_VectorConverter::convert(const Alien::IVectorImpl* sourc Alien::IVectorImpl* targetImpl) const { const auto& v = cast>(sourceImpl, sourceBackend()); - auto& v2 = cast(targetImpl, targetBackend()); + auto& v2 = cast(targetImpl, targetBackend()); alien_debug([&] { cout() << "Converting Alien::SimpleCSRVector: " << &v << " to Hypre::Vector " << &v2; @@ -73,5 +73,3 @@ void SimpleCSR_to_Hypre_VectorConverter::convert(const Alien::IVectorImpl* sourc v2.assemble(); } - -REGISTER_VECTOR_CONVERTER(SimpleCSR_to_Hypre_VectorConverter); diff --git a/plugins/hypre/src/hypre_linear_algebra.cpp b/plugins/hypre/src/hypre_linear_algebra.cpp deleted file mode 100644 index 51343b979..000000000 --- a/plugins/hypre/src/hypre_linear_algebra.cpp +++ /dev/null @@ -1,173 +0,0 @@ -/* - * Copyright 2020 IFPEN-CEA - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * SPDX-License-Identifier: Apache-2.0 - */ - -#include "hypre_matrix.h" -#include "hypre_vector.h" - -#include - -// FIXME: use public API for Hypre ! -#include <_hypre_parcsr_mv.h> - -#include -#include -#include - -#include - -#include -#include - -namespace Alien -{ -namespace -{ - HYPRE_ParVector hypre_implem(const Hypre::Vector& v) - { - HYPRE_ParVector res; - HYPRE_IJVectorGetObject(v.internal(), reinterpret_cast(&res)); - return res; - } - - HYPRE_ParCSRMatrix hypre_implem(const Hypre::Matrix& m) - { - HYPRE_ParCSRMatrix res; - HYPRE_IJMatrixGetObject(m.internal(), reinterpret_cast(&res)); - return res; - } -} // namespace - -template class ALIEN_HYPRE_EXPORT LinearAlgebra; -} // namespace Alien - -namespace Alien::Hypre -{ -class ALIEN_HYPRE_EXPORT InternalLinearAlgebra -: public IInternalLinearAlgebra -{ - public: - InternalLinearAlgebra() {} - - virtual ~InternalLinearAlgebra() {} - - public: - // IInternalLinearAlgebra interface. - Arccore::Real norm0(const Vector& x) const override; - - Arccore::Real norm1(const Vector& x) const override; - - Arccore::Real norm2(const Vector& x) const override; - - void mult(const Matrix& a, const Vector& x, Vector& r) const override; - - void axpy(Arccore::Real alpha, const Vector& x, Vector& r) const override; - - void aypx(Arccore::Real alpha, Vector& y, const Vector& x) const override; - - void copy(const Vector& x, Vector& r) const override; - - Arccore::Real dot(const Vector& x, const Vector& y) const override; - - void scal(Arccore::Real alpha, Vector& x) const override; - - void diagonal(const Matrix& a, Vector& x) const override; - - void reciprocal(Vector& x) const override; - - void pointwiseMult(const Vector& x, const Vector& y, Vector& w) const override; -}; - -Arccore::Real -InternalLinearAlgebra::norm0(const Vector& vx ALIEN_UNUSED_PARAM) const -{ - throw Arccore::NotImplementedException(A_FUNCINFO, "HypreLinearAlgebra::norm0 not implemented"); -} - -Arccore::Real -InternalLinearAlgebra::norm1(const Vector& vx ALIEN_UNUSED_PARAM) const -{ - throw Arccore::NotImplementedException(A_FUNCINFO, "HypreLinearAlgebra::norm1 not implemented"); -} - -Arccore::Real -InternalLinearAlgebra::norm2(const Vector& vx) const -{ - return std::sqrt(dot(vx, vx)); -} - -void InternalLinearAlgebra::mult(const Matrix& ma, const Vector& vx, Vector& vr) const -{ - HYPRE_ParCSRMatrixMatvec(1.0, hypre_implem(ma), hypre_implem(vx), 0.0, hypre_implem(vr)); -} - -void InternalLinearAlgebra::axpy( -Arccore::Real alpha, const Vector& vx, Vector& vr) const -{ - HYPRE_ParVectorAxpy(alpha, hypre_implem(vx), hypre_implem(vr)); -} - -void InternalLinearAlgebra::copy(const Vector& vx, Vector& vr) const -{ - HYPRE_ParVectorCopy(hypre_implem(vx), hypre_implem(vr)); -} - -Arccore::Real -InternalLinearAlgebra::dot(const Vector& vx, const Vector& vy) const -{ - double dot_prod = 0; - HYPRE_ParVectorInnerProd(hypre_implem(vx), hypre_implem(vy), &dot_prod); - return static_cast(dot_prod); -} - -void InternalLinearAlgebra::diagonal(Matrix const& m ALIEN_UNUSED_PARAM, Vector& v ALIEN_UNUSED_PARAM) const -{ - throw Arccore::NotImplementedException( - A_FUNCINFO, "HypreLinearAlgebra::diagonal not implemented"); -} - -void InternalLinearAlgebra::reciprocal(Vector& v ALIEN_UNUSED_PARAM) const -{ - throw Arccore::NotImplementedException( - A_FUNCINFO, "HypreLinearAlgebra::reciprocal not implemented"); -} - -void InternalLinearAlgebra::aypx( -Arccore::Real alpha ALIEN_UNUSED_PARAM, Vector& y ALIEN_UNUSED_PARAM, const Vector& x ALIEN_UNUSED_PARAM) const -{ - throw Arccore::NotImplementedException(A_FUNCINFO, "HypreLinearAlgebra::aypx not implemented"); -} - -void InternalLinearAlgebra::pointwiseMult( -const Vector& x ALIEN_UNUSED_PARAM, const Vector& y ALIEN_UNUSED_PARAM, Vector& w ALIEN_UNUSED_PARAM) const -{ - throw Arccore::NotImplementedException( - A_FUNCINFO, "HypreLinearAlgebra::pointwiseMult not implemented"); -} - -void InternalLinearAlgebra::scal(Arccore::Real alpha, Vector& x) const -{ - HYPRE_ParVectorScale(static_cast(alpha), hypre_implem(x)); -} - -ALIEN_HYPRE_EXPORT -IInternalLinearAlgebra* -InternalLinearAlgebraFactory() -{ - return new Hypre::InternalLinearAlgebra(); -} -} // namespace Alien::Hypre diff --git a/plugins/hypre/src/hypre_linear_solver.cpp b/plugins/hypre/src/hypre_linear_solver.cpp index 9b0bf263e..9c6a680ad 100644 --- a/plugins/hypre/src/hypre_linear_solver.cpp +++ b/plugins/hypre/src/hypre_linear_solver.cpp @@ -23,14 +23,13 @@ namespace Alien { -// Compile HypreLinearSolver. -template class ALIEN_HYPRE_EXPORT LinearSolver; - -} // namespace Alien - -namespace Alien::Hypre +Arccore::String +PluginLinearSolver::getBackEndName() const { -void InternalLinearSolver::checkError( + return "hypre"; +} + +void PluginLinearSolver::checkError( const Arccore::String& msg, int ierr, int skipError) const { if (ierr != 0 and (ierr & ~skipError) != 0) { @@ -42,7 +41,14 @@ const Arccore::String& msg, int ierr, int skipError) const } } -bool InternalLinearSolver::solve(const Matrix& A, const Vector& b, Vector& x) + +// FIXME: check type and conversion +bool PluginLinearSolver::solve(const IMatrixImpl& A, const IVectorImpl& b, IVectorImpl& x) +{ + return _solve(dynamic_cast(A), dynamic_cast(b), dynamic_cast(x)); +} + +bool PluginLinearSolver::_solve(const Matrix& A, const Vector& b, Vector& x) { auto ij_matrix = A.internal(); auto bij_vector = b.internal(); @@ -292,29 +298,22 @@ bool InternalLinearSolver::solve(const Matrix& A, const Vector& b, Vector& x) } const Alien::SolverStatus& -InternalLinearSolver::getStatus() const +PluginLinearSolver::getStatus() const { return m_status; } ALIEN_HYPRE_EXPORT -std::shared_ptr -InternalLinearSolver::algebra() const +IInternalLinearSolver* +LinearSolverFactory(const BackEnd::Options& options) { - return std::make_shared(); + return new PluginLinearSolver(options); } ALIEN_HYPRE_EXPORT -IInternalLinearSolver* -InternalLinearSolverFactory(const Options& options) +IInternalLinearSolver* +LinearSolverFactory() { - return new InternalLinearSolver(options); + return new PluginLinearSolver(); } - -ALIEN_HYPRE_EXPORT -IInternalLinearSolver* -InternalLinearSolverFactory() -{ - return new InternalLinearSolver(); -} -} // namespace Alien::Hypre +} // namespace Alien diff --git a/plugins/hypre/src/hypre_linear_solver.h b/plugins/hypre/src/hypre_linear_solver.h index 612cd0e3f..b2f4ad666 100644 --- a/plugins/hypre/src/hypre_linear_solver.h +++ b/plugins/hypre/src/hypre_linear_solver.h @@ -21,6 +21,9 @@ #include #include +#include +#include + #include #include #include @@ -28,17 +31,17 @@ #include "hypre_matrix.h" #include "hypre_vector.h" -namespace Alien::Hypre +namespace Alien { -class InternalLinearSolver : public IInternalLinearSolver +class PluginLinearSolver : public IInternalLinearSolver , public ObjectWithTrace { public: typedef SolverStatus Status; - InternalLinearSolver() = default; + PluginLinearSolver() = default; - explicit InternalLinearSolver(const Options& options) + explicit PluginLinearSolver(const BackEnd::Options& options) : m_status() , m_init_time(0.0) , m_total_solve_time(0.0) @@ -48,13 +51,15 @@ class InternalLinearSolver : public IInternalLinearSolver , m_options(options) {} - virtual ~InternalLinearSolver() = default; + virtual ~PluginLinearSolver() = default; public: + Arccore::String getBackEndName() const; + // Nothing to do void updateParallelMng(ALIEN_UNUSED_PARAM Arccore::MessagePassing::IMessagePassingMng* pm) {} - bool solve(const Matrix& A, const Vector& b, Vector& x); + bool solve(const IMatrixImpl& A, const IVectorImpl& b, IVectorImpl& x); bool hasParallelSupport() const { return true; } @@ -63,7 +68,8 @@ class InternalLinearSolver : public IInternalLinearSolver const SolverStat& getSolverStat() const { return m_stat; } - std::shared_ptr algebra() const; + private: + bool _solve(const Matrix& A, const Vector& b, Vector& x); private: Status m_status; @@ -74,9 +80,9 @@ class InternalLinearSolver : public IInternalLinearSolver Arccore::Integer m_total_iter_num; SolverStat m_stat; - Options m_options; + BackEnd::Options m_options; private: void checkError(const Arccore::String& msg, int ierr, int skipError = 0) const; }; -} // namespace Alien::Hypre +} // namespace Alien diff --git a/plugins/hypre/src/hypre_matrix.cpp b/plugins/hypre/src/hypre_matrix.cpp index 39847f739..ab9222dc2 100644 --- a/plugins/hypre/src/hypre_matrix.cpp +++ b/plugins/hypre/src/hypre_matrix.cpp @@ -26,10 +26,10 @@ #include -namespace Alien::Hypre +namespace Alien { Matrix::Matrix(const MultiMatrixImpl* multi_impl) -: IMatrixImpl(multi_impl, AlgebraTraits::name()) +: IMatrixImpl(multi_impl, "hypre") , m_hypre(nullptr) { const auto& row_space = multi_impl->rowSpace(); @@ -89,4 +89,4 @@ void Matrix::setRowValues(int rows, Arccore::ConstArrayView cols, Arccore:: } } -} // namespace Alien::Hypre \ No newline at end of file +} // namespace Alien diff --git a/plugins/hypre/src/hypre_matrix.h b/plugins/hypre/src/hypre_matrix.h index ba73eff9e..95e7fc010 100644 --- a/plugins/hypre/src/hypre_matrix.h +++ b/plugins/hypre/src/hypre_matrix.h @@ -22,7 +22,7 @@ #include -namespace Alien::Hypre +namespace Alien { class Matrix : public IMatrixImpl { @@ -49,4 +49,4 @@ class Matrix : public IMatrixImpl MPI_Comm m_comm; }; -} // namespace Alien::Hypre +} // namespace Alien diff --git a/plugins/hypre/src/hypre_vector.cpp b/plugins/hypre/src/hypre_vector.cpp index 444069e79..117d5cfd9 100644 --- a/plugins/hypre/src/hypre_vector.cpp +++ b/plugins/hypre/src/hypre_vector.cpp @@ -24,10 +24,10 @@ #include -namespace Alien::Hypre +namespace Alien { Vector::Vector(const MultiVectorImpl* multi_impl) -: IVectorImpl(multi_impl, AlgebraTraits::name()) +: IVectorImpl(multi_impl, "hypre") , m_hypre(nullptr) { auto block_size = 1; @@ -99,4 +99,4 @@ void Vector::assemble() throw Arccore::FatalErrorException(A_FUNCINFO, "Hypre assembling failed"); } } -} // namespace Alien::Hypre \ No newline at end of file +} // namespace Alien diff --git a/plugins/hypre/src/hypre_vector.h b/plugins/hypre/src/hypre_vector.h index 9bbb4dea0..bc91248c1 100644 --- a/plugins/hypre/src/hypre_vector.h +++ b/plugins/hypre/src/hypre_vector.h @@ -22,7 +22,7 @@ #include -namespace Alien::Hypre +namespace Alien { class VectorInternal; diff --git a/src/core/alien/core/backend/BackEnd.h b/src/core/alien/core/backend/BackEnd.h index 17e5f7e23..fb7370a13 100644 --- a/src/core/alien/core/backend/BackEnd.h +++ b/src/core/alien/core/backend/BackEnd.h @@ -20,6 +20,7 @@ #include #include +#include /*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/ @@ -38,13 +39,83 @@ struct AlgebraTraits; template class LUSendRecvTraits; +class IInternalLinearSolver; + +class ILinearSolver; +class ILinearAlgebra; + + namespace BackEnd { - namespace tag + struct OptionTypes { - } // namespace tag + enum eSolver + { + AMG, + CG, + GMRES, + BiCGStab, + Hybrid + }; + + enum ePreconditioner + { + NoPC, + DiagPC, + AMGPC, + ParaSailsPC, + EuclidPC + }; + }; + + struct Options + { + Arccore::Integer numIterationsMax_ = 100; + Arccore::Integer numIterationsMax() const { return numIterationsMax_; } + Options& numIterationsMax(Arccore::Integer n) + { + numIterationsMax_ = n; + return *this; + } + + Arccore::Real stopCriteriaValue_ = 1.e-10; + Arccore::Real stopCriteriaValue() const { return stopCriteriaValue_; } + Options& stopCriteriaValue(Arccore::Real n) + { + stopCriteriaValue_ = n; + return *this; + } + + bool verbose_ = false; + bool verbose() const { return verbose_; } + Options& verbose(bool n) + { + verbose_ = n; + return *this; + } + + OptionTypes::eSolver solver_; + OptionTypes::eSolver solver() const { return solver_; } + Options& solver(OptionTypes::eSolver n) + { + solver_ = n; + return *this; + } + + OptionTypes::ePreconditioner preconditioner_; + OptionTypes::ePreconditioner preconditioner() const { return preconditioner_; } + Options& preconditioner(OptionTypes::ePreconditioner n) + { + preconditioner_ = n; + return *this; + } + }; } // namespace BackEnd + + + + /*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/ diff --git a/src/core/alien/core/backend/LinearSolver.h b/src/core/alien/core/backend/LinearSolver.h index 518cf8d92..bda895049 100644 --- a/src/core/alien/core/backend/LinearSolver.h +++ b/src/core/alien/core/backend/LinearSolver.h @@ -30,7 +30,7 @@ #include #include -#include +#include #include @@ -58,12 +58,11 @@ class IMatrix; * * \tparam Tag The tag of the type of solvers used */ -template class LinearSolver : public ILinearSolver { public: //! The type of the solver - typedef typename AlgebraTraits::solver_type KernelSolver; + //typedef typename AlgebraTraits::solver_type KernelSolver; public: /*! @@ -74,10 +73,16 @@ class LinearSolver : public ILinearSolver * \tparam T Variadics type of linear solver * \param[in] args Linear solvers */ - template - LinearSolver(T... args) - : m_solver(AlgebraTraits::solver_factory(args...)) - {} + // FIXME + //LinearSolver(...) + //: m_solver(AlgebraTraits::solver_factory(...)) + //{} + + LinearSolver(BackEndId backEndId, Args... args) + : m_backEndId(backEndId) + { + // FIXME: init m_solver here + } //! Free resources virtual ~LinearSolver(); @@ -143,7 +148,7 @@ class LinearSolver : public ILinearSolver * \brief Get kernel solver implementation * \return Linear solver actual implementation */ - KernelSolver* implem(); + IInternalLinearSolver* implem(); /*! * \brief Option to add an extra-equation @@ -161,7 +166,8 @@ class LinearSolver : public ILinearSolver private: //! The linear solver kernel - std::unique_ptr m_solver; + std::unique_ptr m_solver; + BackEndId m_backEndId; }; /*---------------------------------------------------------------------------*/ diff --git a/src/core/alien/core/backend/LinearSolverT.h b/src/core/alien/core/backend/LinearSolverT.h index 8a7c65650..2dc5f3e4d 100644 --- a/src/core/alien/core/backend/LinearSolverT.h +++ b/src/core/alien/core/backend/LinearSolverT.h @@ -39,17 +39,15 @@ namespace Alien /*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/ -template -LinearSolver::~LinearSolver() {} +LinearSolver::~LinearSolver() {} /*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/ -template String -LinearSolver::getBackEndName() const +LinearSolver::getBackEndName() const { - return AlgebraTraits::name(); + return m_solver->getBackEndName(); } /*---------------------------------------------------------------------------*/ @@ -58,8 +56,7 @@ LinearSolver::getBackEndName() const /*! * \todo Check the comment below and fix if necessary */ -template -bool LinearSolver::solve(const IMatrix& A, const IVector& b, IVector& x) +bool LinearSolver::solve(const IMatrix& A, const IVector& b, IVector& x) { auto dist = A.impl()->distribution(); // solve is a global call @@ -76,9 +73,10 @@ bool LinearSolver::solve(const IMatrix& A, const IVector& b, IVector& x) m_solver->updateParallelMng(A.impl()->distribution().parallelMng()); } // m_solver->getSolverStat().startPrepareMeasure(); - const auto& matrix = A.impl()->get(); - const auto& rhs = b.impl()->get(); - auto& sol = x.impl()->get(true); + // FIXME + const auto& matrix = A.impl()->get(m_backEndId); + const auto& rhs = b.impl()->get(m_backEndId); + auto& sol = x.impl()->get(m_backEndId, true); // m_solver->getSolverStat().stopPrepareMeasure(); return m_solver->solve(matrix, rhs, sol); } @@ -86,8 +84,7 @@ bool LinearSolver::solve(const IMatrix& A, const IVector& b, IVector& x) /*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/ -template -void LinearSolver::init() +void LinearSolver::init() { m_solver->init(); } @@ -95,23 +92,20 @@ void LinearSolver::init() /*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/ -template -void LinearSolver::end() +void LinearSolver::end() { m_solver->end(); } -template -void LinearSolver::updateParallelMng(Arccore::MessagePassing::IMessagePassingMng* pm) +void LinearSolver::updateParallelMng(Arccore::MessagePassing::IMessagePassingMng* pm) { m_solver->updateParallelMng(pm); } /*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/ -template const SolverStat& -LinearSolver::getSolverStat() const +LinearSolver::getSolverStat() const { return m_solver->getSolverStat(); } @@ -119,8 +113,7 @@ LinearSolver::getSolverStat() const /*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/ -template -bool LinearSolver::hasParallelSupport() const +bool LinearSolver::hasParallelSupport() const { return m_solver->hasParallelSupport(); } @@ -128,9 +121,8 @@ bool LinearSolver::hasParallelSupport() const /*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/ -template const SolverStatus& -LinearSolver::getStatus() const +LinearSolver::getStatus() const { return m_solver->getStatus(); } @@ -138,9 +130,8 @@ LinearSolver::getStatus() const /*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/ -template -typename LinearSolver::KernelSolver* -LinearSolver::implem() +IInternalLinearSolver* +LinearSolver::implem() { return m_solver.get(); } @@ -148,9 +139,8 @@ LinearSolver::implem() /*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/ -template std::shared_ptr -LinearSolver::algebra() const +LinearSolver::algebra() const { return m_solver->algebra(); } diff --git a/src/core/alien/core/impl/MultiMatrixImpl.h b/src/core/alien/core/impl/MultiMatrixImpl.h index 2eb6222a0..fb1383b8d 100644 --- a/src/core/alien/core/impl/MultiMatrixImpl.h +++ b/src/core/alien/core/impl/MultiMatrixImpl.h @@ -181,8 +181,7 @@ class ALIEN_EXPORT MultiMatrixImpl : public TimestampMng * * \returns The up to date matrix in the requested implementation */ - template - const typename AlgebraTraits::matrix_type& get() const; + const IMatrixImpl& get(BackEndId) const; /*! * \brief Get a specific matrix implementation @@ -193,12 +192,10 @@ class ALIEN_EXPORT MultiMatrixImpl : public TimestampMng * \param[in] update_stamp Whether or not the timestamp should be increased or not * \returns The up to date matrix in the requested implementation */ - template - typename AlgebraTraits::matrix_type& get(const bool update_stamp); + IMatrixImpl& get(BackEndId, const bool update_stamp); //! Release a matrix implementation - template - void release() const; + void release(BackEndId) const; /*! * \brief Clone this object @@ -218,7 +215,6 @@ class ALIEN_EXPORT MultiMatrixImpl : public TimestampMng * \param[in] backend The id of the specific implementation * \returns The matrix in the requested format */ - template IMatrixImpl*& getImpl(BackEndId backend) const; /*! @@ -233,8 +229,7 @@ class ALIEN_EXPORT MultiMatrixImpl : public TimestampMng * \param[in] backend The implementation backend id * \param[in] m The matrix to insert */ - template - void insert(BackEndId backend, matrix_type* m); + void insert(BackEndId backend, IMatrixImpl* m); private: //! The matrix row space @@ -258,48 +253,45 @@ class ALIEN_EXPORT MultiMatrixImpl : public TimestampMng /*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/ -template -const typename AlgebraTraits::matrix_type& -MultiMatrixImpl::get() const +const IMatrixImpl& +MultiMatrixImpl::get(BackEndId backEndId) const { // TOCHECK : to be removed or not ? // ALIEN_ASSERT(!m_row_space.isNull(), ("Null row space matrix access")); // ALIEN_ASSERT(!m_col_space.isNull(), ("Null col space matrix access")); - typedef typename AlgebraTraits::matrix_type matrix_type; - IMatrixImpl*& impl2 = getImpl(AlgebraTraits::name()); + // FIXME: type + IMatrixImpl*& impl2 = getImpl(backEndId); ALIEN_ASSERT( - (impl2->backend() == AlgebraTraits::name()), ("Inconsistent backend")); + (impl2->backend() == backEndId), ("Inconsistent backend")); updateImpl(impl2); - return *dynamic_cast(impl2); + return *impl2; } /*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/ -template -typename AlgebraTraits::matrix_type& -MultiMatrixImpl::get(const bool update_stamp) +IMatrixImpl& +MultiMatrixImpl::get(BackEndId backEndId, const bool update_stamp) { // TOCHECK : to be removed or not ? // ALIEN_ASSERT(!m_row_space.isNull(), ("Null row space matrix access")); // ALIEN_ASSERT(!m_col_space.isNull(), ("Null col space matrix access")); - typedef typename AlgebraTraits::matrix_type matrix_type; - IMatrixImpl*& impl2 = getImpl(AlgebraTraits::name()); + // FIXME: type + IMatrixImpl*& impl2 = getImpl(backEndId); ALIEN_ASSERT( - (impl2->backend() == AlgebraTraits::name()), ("Inconsistent backend")); + (impl2->backend() == backEndId), ("Inconsistent backend")); updateImpl(impl2); if (update_stamp) impl2->updateTimestamp(); - return *dynamic_cast(impl2); + return *impl2; } /*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/ -template -void MultiMatrixImpl::release() const +void MultiMatrixImpl::release(BackEndId backEndId) const { - auto finder = m_impls2.find(AlgebraTraits::name()); + auto finder = m_impls2.find(backEndId); if (finder == m_impls2.end()) return; // already freed delete finder->second, finder->second = NULL; @@ -308,14 +300,13 @@ void MultiMatrixImpl::release() const /*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/ -template IMatrixImpl*& MultiMatrixImpl::getImpl(BackEndId backend) const { auto inserter = m_impls2.insert(MultiMatrixImplMap::value_type(backend, NULL)); IMatrixImpl*& impl2 = inserter.first->second; if (impl2 == NULL) { - matrix_type* new_impl = new matrix_type(this); // constructeur associé à un multi-impl + IMatrixImpl* new_impl = new IMatrixImpl(this); // constructeur associé à un multi-impl // new_impl->init(*m_row_space.get(), // *m_col_space.get(), // m_distribution); @@ -327,8 +318,7 @@ MultiMatrixImpl::getImpl(BackEndId backend) const /*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/ -template -void MultiMatrixImpl::insert(BackEndId backend, matrix_type* m) +void MultiMatrixImpl::insert(BackEndId backend, IMatrixImpl* m) { if (m_impls2.find(backend) != m_impls2.end()) { alien_fatal([&] { cout() << "try to insert already inserted value"; }); diff --git a/src/core/alien/data/CompositeMatrix.cc b/src/core/alien/data/CompositeMatrix.cc index b690148de..23d34dac7 100644 --- a/src/core/alien/data/CompositeMatrix.cc +++ b/src/core/alien/data/CompositeMatrix.cc @@ -59,7 +59,7 @@ CompositeMatrix::CompositeMatrix() CompositeMatrix::CompositeMatrix(Integer nc) : m_impl(new CompositeKernel::MultiMatrixImpl()) -, m_composite_matrix(m_impl->get(false)) +, m_composite_matrix(m_impl->get("composite", false)) { m_impl->setFeature("composite"); diff --git a/src/core/alien/kernels/dok/DoKBackEnd.h b/src/core/alien/kernels/dok/DoKBackEnd.h index dae8413cc..55719b34c 100644 --- a/src/core/alien/kernels/dok/DoKBackEnd.h +++ b/src/core/alien/kernels/dok/DoKBackEnd.h @@ -33,12 +33,11 @@ class DoKVector; class Space; template class IInternalLinearAlgebra; -template class IInternalLinearSolver; extern IInternalLinearAlgebra* DoKLinearAlgebraFactory(); -extern IInternalLinearSolver* DoKLinearSolverFactory( +extern IInternalLinearSolver* DoKLinearSolverFactory( IMessagePassingMng* p_mng); /*---------------------------------------------------------------------------*/ @@ -55,7 +54,7 @@ struct AlgebraTraits typedef DoKMatrix matrix_type; typedef DoKVector vector_type; typedef IInternalLinearAlgebra algebra_type; - typedef IInternalLinearSolver solver_type; + typedef IInternalLinearSolver solver_type; static algebra_type* algebra_factory() { return DoKLinearAlgebraFactory(); } static solver_type* solver_factory(IMessagePassingMng* p_mng) { diff --git a/src/core/alien/kernels/redistributor/RedistributorBackEnd.h b/src/core/alien/kernels/redistributor/RedistributorBackEnd.h index 6f3836ba6..f58aa0bb9 100644 --- a/src/core/alien/kernels/redistributor/RedistributorBackEnd.h +++ b/src/core/alien/kernels/redistributor/RedistributorBackEnd.h @@ -38,13 +38,12 @@ class RedistributorVector; class Space; template class IInternalLinearAlgebra; -template class IInternalLinearSolver; extern IInternalLinearAlgebra* redistributorLinearAlgebraFactory(); -extern IInternalLinearSolver* +extern IInternalLinearSolver* redistributorLinearSolverFactory(IMessagePassingMng* p_mng); /*---------------------------------------------------------------------------*/ @@ -64,7 +63,7 @@ struct AlgebraTraits typedef RedistributorMatrix matrix_type; typedef RedistributorVector vector_type; typedef IInternalLinearAlgebra algebra_type; - typedef IInternalLinearSolver solver_type; + typedef IInternalLinearSolver solver_type; static algebra_type* algebra_factory() { return redistributorLinearAlgebraFactory(); } static solver_type* solver_factory(IMessagePassingMng* p_mng) { From 8f28fe854d3f61bc7e421727e3d8ff2591b2b131 Mon Sep 17 00:00:00 2001 From: Louis Goyard Date: Tue, 21 Jun 2022 11:36:04 +0200 Subject: [PATCH 02/13] WIP: more wip --- src/core/alien/data/CompositeMatrix.cc | 2 +- src/core/alien/kernels/dok/BaseDoKDirectMatrixBuilder.cpp | 2 +- src/core/alien/kernels/redistributor/Redistributor.cc | 2 +- src/core/alien/kernels/redistributor/RedistributorMatrix.cc | 4 ++-- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/core/alien/data/CompositeMatrix.cc b/src/core/alien/data/CompositeMatrix.cc index 23d34dac7..9c6642b9d 100644 --- a/src/core/alien/data/CompositeMatrix.cc +++ b/src/core/alien/data/CompositeMatrix.cc @@ -59,7 +59,7 @@ CompositeMatrix::CompositeMatrix() CompositeMatrix::CompositeMatrix(Integer nc) : m_impl(new CompositeKernel::MultiMatrixImpl()) -, m_composite_matrix(m_impl->get("composite", false)) +, m_composite_matrix(dynamic_cast(m_impl->get("composite", false))) { m_impl->setFeature("composite"); diff --git a/src/core/alien/kernels/dok/BaseDoKDirectMatrixBuilder.cpp b/src/core/alien/kernels/dok/BaseDoKDirectMatrixBuilder.cpp index e875bf3de..4430dc78e 100644 --- a/src/core/alien/kernels/dok/BaseDoKDirectMatrixBuilder.cpp +++ b/src/core/alien/kernels/dok/BaseDoKDirectMatrixBuilder.cpp @@ -31,7 +31,7 @@ Common::BaseDoKDirectMatrixBuilder::BaseDoKDirectMatrixBuilder(Alien::IMatrix& s : m_matrix(self) { m_matrix.impl()->lock(); - m_impl = &m_matrix.impl()->get(true); + m_impl = dynamic_cast(&m_matrix.impl()->get("DoK", true)); } Common::BaseDoKDirectMatrixBuilder::~BaseDoKDirectMatrixBuilder() diff --git a/src/core/alien/kernels/redistributor/Redistributor.cc b/src/core/alien/kernels/redistributor/Redistributor.cc index f2f17bb02..7a9c87821 100644 --- a/src/core/alien/kernels/redistributor/Redistributor.cc +++ b/src/core/alien/kernels/redistributor/Redistributor.cc @@ -52,7 +52,7 @@ int globalSize, Arccore::MessagePassing::IMessagePassingMng* super, Arccore::Mes std::shared_ptr Redistributor::redistribute(MultiMatrixImpl* mat) { - auto& red_mat = mat->get(true); + auto& red_mat = dynamic_cast(mat->get("redistributor", true)); return red_mat.updateTargetPM(m_distributor.get()); } diff --git a/src/core/alien/kernels/redistributor/RedistributorMatrix.cc b/src/core/alien/kernels/redistributor/RedistributorMatrix.cc index f9e63f75f..757ae7954 100644 --- a/src/core/alien/kernels/redistributor/RedistributorMatrix.cc +++ b/src/core/alien/kernels/redistributor/RedistributorMatrix.cc @@ -81,8 +81,8 @@ RedistributorMatrix::updateTargetPM(const RedistributorCommPlan* commPlan) std::shared_ptr RedistributorMatrix::redistribute() { - auto& mat_src = m_multi_impl->get(); - auto& mat_tgt = m_tgt_impl->get(true); + auto& mat_src = dynamic_cast(m_multi_impl->get("DoK")); + auto& mat_tgt = dynamic_cast(m_tgt_impl->get("DoK", true)); m_distributor->distribute(mat_src, mat_tgt); return m_tgt_impl; } From 11257ffed7f13baa620f74bdb44f3e390b2a5b0c Mon Sep 17 00:00:00 2001 From: Louis Goyard Date: Tue, 21 Jun 2022 15:48:07 +0200 Subject: [PATCH 03/13] WIP: compiling but not linking --- src/core/CMakeLists.txt | 15 +- src/core/alien/core/backend/LinearAlgebra.h | 266 --------- .../alien/core/backend/LinearAlgebraExpr.h | 266 --------- .../alien/core/backend/LinearAlgebraExprT.h | 268 --------- src/core/alien/core/backend/LinearAlgebraT.h | 213 -------- src/core/alien/core/impl/MultiMatrixImpl.cc | 4 +- src/core/alien/core/impl/MultiVectorImpl.cc | 7 +- src/core/alien/core/impl/MultiVectorImpl.h | 46 +- src/core/alien/data/CompositeVector.cc | 2 +- .../expression/normalization/NormalizeOpt.cc | 16 +- .../alien/expression/solver/ILinearAlgebra.h | 113 ---- .../alien/handlers/block/BlockVectorReaderT.h | 2 +- .../alien/handlers/block/BlockVectorWriterT.h | 2 +- .../block/ProfiledFixedBlockMatrixBuilder.cc | 2 +- .../alien/handlers/profiler/MatrixProfilerT.h | 2 +- .../scalar/BaseDirectMatrixBuilder.cc | 2 +- .../scalar/BaseProfiledMatrixBuilder.cc | 2 +- .../alien/handlers/scalar/VectorReaderT.h | 2 +- .../alien/handlers/scalar/VectorWriterT.h | 2 +- .../kernels/redistributor/Redistributor.cc | 2 +- .../redistributor/RedistributorVector.cc | 6 +- .../alien/kernels/simple_csr/CMakeLists.txt | 15 +- .../simple_csr/algebra/CBLASMPIKernel.h | 115 ---- .../algebra/SimpleCSRInternalLinearAlgebra.cc | 394 -------------- .../algebra/SimpleCSRInternalLinearAlgebra.h | 214 -------- .../algebra/SimpleCSRLinearAlgebra.h | 34 -- .../simple_csr/algebra/SimpleCSRMatrixMult.h | 88 --- .../simple_csr/algebra/SimpleCSRMatrixMultT.h | 515 ------------------ .../kernels/simple_csr/algebra/alien_cblas.h | 76 --- 29 files changed, 52 insertions(+), 2639 deletions(-) delete mode 100644 src/core/alien/core/backend/LinearAlgebra.h delete mode 100644 src/core/alien/core/backend/LinearAlgebraExpr.h delete mode 100644 src/core/alien/core/backend/LinearAlgebraExprT.h delete mode 100644 src/core/alien/core/backend/LinearAlgebraT.h delete mode 100644 src/core/alien/expression/solver/ILinearAlgebra.h delete mode 100644 src/core/alien/kernels/simple_csr/algebra/CBLASMPIKernel.h delete mode 100644 src/core/alien/kernels/simple_csr/algebra/SimpleCSRInternalLinearAlgebra.cc delete mode 100644 src/core/alien/kernels/simple_csr/algebra/SimpleCSRInternalLinearAlgebra.h delete mode 100644 src/core/alien/kernels/simple_csr/algebra/SimpleCSRLinearAlgebra.h delete mode 100644 src/core/alien/kernels/simple_csr/algebra/SimpleCSRMatrixMult.h delete mode 100644 src/core/alien/kernels/simple_csr/algebra/SimpleCSRMatrixMultT.h delete mode 100644 src/core/alien/kernels/simple_csr/algebra/alien_cblas.h diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt index 3380cbe8d..2500f29a5 100644 --- a/src/core/CMakeLists.txt +++ b/src/core/CMakeLists.txt @@ -1,17 +1,17 @@ # Copyright 2020 IFPEN-CEA -# +# # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at -# +# # http://www.apache.org/licenses/LICENSE-2.0 -# +# # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. -# +# # SPDX-License-Identifier: Apache-2.0 add_subdirectory(alien/utils) @@ -40,10 +40,6 @@ add_library(alien_core alien/core/backend/IMatrixConverter.h alien/core/backend/IVectorConverter.h alien/core/backend/ISolverFabric.h - alien/core/backend/LinearAlgebra.h - alien/core/backend/LinearAlgebraT.h - alien/core/backend/LinearAlgebraExpr.h - alien/core/backend/LinearAlgebraExprT.h alien/core/backend/LinearSolver.h alien/core/backend/LinearSolverT.h alien/core/backend/MatrixConverterRegisterer.cc @@ -82,7 +78,6 @@ add_library(alien_core alien/expression/normalization/NormalizeOpt.cc alien/expression/normalization/NormalizeOpt.h alien/expression/solver/IEigenSolver.h - alien/expression/solver/ILinearAlgebra.h alien/expression/solver/ILinearSolver.h alien/expression/solver/SolverStat.cc alien/expression/solver/SolverStater.cc @@ -198,4 +193,4 @@ enable_sanitizers(alien_core) if (ALIEN_UNIT_TESTS) add_subdirectory(tests) -endif (ALIEN_UNIT_TESTS) \ No newline at end of file +endif (ALIEN_UNIT_TESTS) diff --git a/src/core/alien/core/backend/LinearAlgebra.h b/src/core/alien/core/backend/LinearAlgebra.h deleted file mode 100644 index 9b6324b76..000000000 --- a/src/core/alien/core/backend/LinearAlgebra.h +++ /dev/null @@ -1,266 +0,0 @@ -/* - * Copyright 2020 IFPEN-CEA - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * SPDX-License-Identifier: Apache-2.0 - */ - -/*! - * \file LinearAlgebra.h - * \brief LinearAlgebra.h - */ -#pragma once - -#include - -#include -#include - -#include -#include - -#include - -/*---------------------------------------------------------------------------*/ -/*---------------------------------------------------------------------------*/ - -namespace Alien -{ - -/*---------------------------------------------------------------------------*/ -/*---------------------------------------------------------------------------*/ - -class Space; -class IVector; -class IMatrix; - -/*---------------------------------------------------------------------------*/ -/*---------------------------------------------------------------------------*/ - -/*! - * \ingroup core - * \brief Linear algebra interface - * - * Interface for all linear algebra package - * - * \tparam Tag The tag of the type of matrix used - * \tparam TagV The tag of the type of vector used - */ -template -class LinearAlgebra : public ILinearAlgebra -{ - public: - /*! - * \brief Creates a linear algebra - * - * Creates a linear algebra using traits and the linear algebra factory - * - * \tparam T Variadics type of linear algebra - * \param[in] args Linear algebras - */ - template - LinearAlgebra(T... args) - : m_algebra(AlgebraTraits::algebra_factory(args...)) - {} - - //! Free resources - virtual ~LinearAlgebra(); - - /*! - * \brief Compute L0 norm of a vector - * \param[in] x The vector on which norm0 is computed - * \returns The norm0 of the vector - */ - Real norm0(const IVector& x) const; - - /*! - * \brief Compute L1 norm of a vector - * \param[in] x The vector on which norm0 is computed - * \returns The norm1 of the vector - */ - Real norm1(const IVector& x) const; - - /*! - * \brief Compute L2 norm of a vector - * \param[in] x The vector on which norm0 is computed - * \returns The norm2 of the vector - */ - Real norm2(const IVector& x) const; - - /*! - * \brief Compute a matrix vector product - * - * Compute the matrix-vector product a by x and store it in r : r = a * x - * - * \param[in] a The matrix to be multiplied - * \param[in] x The vector to be multipled - * \param[in,out] r The resulting vector - */ - void mult(const IMatrix& a, const IVector& x, IVector& r) const; - - /*! - * \brief Scale a vector by a factor and adds the result to another vector - * - * Scale the vector x by the real value alpha and add the result to the vector y : y += - * alpha * x - * - * \param[in] alpha The real value to scale with - * \param[in] x The vector to be scaled - * \param[in,out] y The resulting vector - */ - void axpy(Real alpha, const IVector& x, IVector& y) const; - - /*! - * \brief Scale a vector by a factor and adds the result to another vector - * - * Scale the vector y by the real value alpha and add the values of x : alpha * y += x - * - * \param[in] alpha The real value to scale with - * \param[in,out] y The vector to be scaled - * \param[in] x The vector to add - */ - void aypx(Real alpha, IVector& y, const IVector& x) const; - - /*! - * \brief Copy a vector in another one - * - * \param[in] x The vector to copy - * \param[in,out] r The copied vector - */ - void copy(const IVector& x, IVector& r) const; - - /*! - * \brief Compute the dot product of two vectors - * \param[in] x The first vector - * \param[in] y The second vector - * \returns The dot product of x * y - */ - Real dot(const IVector& x, const IVector& y) const; - - /*! - * \brief Scale a vector by a factor - * \param[in] alpha The real value to scale with - * \param[in,out] x The vector to be scaled - */ - void scal(Real alpha, IVector& x) const; - - /*! - * \brief Extract the diagonal of a matrix in a vector - * \param[in] a The matrix to extract the diagonal - * \param[in,out] x The diagonal elements of the matrix stored in a vector - */ - void diagonal(const IMatrix& a, IVector& x) const; - - /*! - * \brief Compute the reciprocal of a vector - * \param[in,out] x The vector to be processed - */ - void reciprocal(IVector& x) const; - - /*! - * \brief Compute the point wise multiplication of two vectors and store the result in - * another one - * \param[in] x The first vector - * \param[in] y The second vector - * \param[in,out] w The resulting vector - */ - void pointwiseMult(const IVector& x, const IVector& y, IVector& w) const; - - /*! - * \brief Compute a matrix vector product - * - * Compute the matrix-vector product a by x and store it in r : r = a * x - * - * \param[in] a The matrix to be multiplied - * \param[in] x The vector to be multipled - * \param[in,out] r The resulting vector - */ - void mult(const IMatrix& a, const UniqueArray& x, UniqueArray& r) const; - - /*! - * \brief Scale a vector by a factor and adds the result to another vector - * - * Scale the vector x by the real value alpha and add the result to the vector y : y += - * alpha * x - * - * \param[in] alpha The real value to scale with - * \param[in] x The vector to be scaled - * \param[in,out] y The resulting vector - */ - void axpy(const Real& alpha, const UniqueArray& x, UniqueArray& r) const; - - /*! - * \brief Scale a vector by a factor and adds the result to another vector - * - * Scale the vector y by the real value alpha and add the values of x : alpha * y += x - * - * \param[in] alpha The real value to scale with - * \param[in,out] y The vector to be scaled - * \param[in] x The vector to add - */ - void aypx(Real alpha, UniqueArray& y, const UniqueArray& x) const; - - /*! - * \brief Copy a vector in another one - * - * \param[in] x The vector to copy - * \param[in,out] r The copied vector - */ - void copy(const UniqueArray& x, UniqueArray& r) const; - - /*! - * \brief Compute the dot product of two vectors - * \param[in] local_size The size of the vectors - * \param[in] x The first vector - * \param[in] y The second vector - * \returns The dot product of x * y - */ - Real dot( - Integer local_size, const UniqueArray& x, const UniqueArray& y) const; - - /*! - * \brief Scale a vector by a factor - * \param[in] alpha The real value to scale with - * \param[in,out] x The vector to be scaled - */ - void scal(Real alpha, UniqueArray& x) const; - - /*! - * \brief Dumps a matrix to a file - * \param[in] a The matrix to dump - * \param[in] filename The name of the file - */ - void dump(IMatrix const& a, std::string const& filename) const; - - /*! - * \brief Dumps a vector to a file - * \param[in] x The vector to dump - * \param[in] filename The name of the file - */ - void dump(IVector const& x, std::string const& filename) const; - - private: - //! The type of the linear algebra - typedef typename AlgebraTraits::algebra_type KernelAlgebra; - //! The linear algebra kernel - std::unique_ptr m_algebra; -}; - -/*---------------------------------------------------------------------------*/ -/*---------------------------------------------------------------------------*/ - -} // namespace Alien - -/*---------------------------------------------------------------------------*/ -/*---------------------------------------------------------------------------*/ diff --git a/src/core/alien/core/backend/LinearAlgebraExpr.h b/src/core/alien/core/backend/LinearAlgebraExpr.h deleted file mode 100644 index d01e2e816..000000000 --- a/src/core/alien/core/backend/LinearAlgebraExpr.h +++ /dev/null @@ -1,266 +0,0 @@ -/* - -Copyright 2020 IFPEN-CEA - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - -http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. - -*/ - -/*! - * \file LinearAlgebraExpr.h - * \brief LinearAlgebraExpr.h - */ -#pragma once - -#include - -#include - -#include -#include - -#include - -/*---------------------------------------------------------------------------*/ -/*---------------------------------------------------------------------------*/ - -namespace Alien -{ - -/*---------------------------------------------------------------------------*/ -/*---------------------------------------------------------------------------*/ - -class Space; -class IVector; -class IMatrix; - -/*---------------------------------------------------------------------------*/ -/*---------------------------------------------------------------------------*/ - -/*! - * \ingroup core - * \brief Linear algebra interface - * - * Interface for all linear algebra package - * - * \tparam Tag The tag of the type of matrix used - * \tparam TagV The tag of the type of vector used - */ -template -class LinearAlgebraExpr -{ - public: - /*! - * \brief Creates a linear algebra - * - * Creates a linear algebra using traits and the linear algebra factory - * - * \tparam T Variadics type of linear algebra - * \param[in] args Linear algebras - */ - template - LinearAlgebraExpr(T... args) - : m_algebra(AlgebraTraits::algebra_expr_factory(args...)) - {} - - //! Free resources - virtual ~LinearAlgebraExpr(); - - /*! - * \brief Compute L0 norm of a vector - * \param[in] x The vector on which norm0 is computed - * \returns The norm0 of the vector - */ - Real norm0(const IVector& x) const; - - /*! - * \brief Compute L1 norm of a vector - * \param[in] x The vector on which norm0 is computed - * \returns The norm1 of the vector - */ - Real norm1(const IVector& x) const; - - /*! - * \brief Compute L2 norm of a vector - * \param[in] x The vector on which norm0 is computed - * \returns The norm2 of the vector - */ - Real norm2(const IVector& x) const; - - /*! - * \brief Compute a matrix vector product - * - * Compute the matrix-vector product a by x and store it in r : r = a * x - * - * \param[in] a The matrix to be multiplied - * \param[in] x The vector to be multipled - * \param[in,out] r The resulting vector - */ - void mult(const IMatrix& a, const IVector& x, IVector& r) const; - - /*! - * \brief Scale a vector by a factor and adds the result to another vector - * - * Scale the vector x by the real value alpha and add the result to the vector y : y += - * alpha * x - * - * \param[in] alpha The real value to scale with - * \param[in] x The vector to be scaled - * \param[in,out] y The resulting vector - */ - void axpy(Real alpha, const IVector& x, IVector& y) const; - - /*! - * \brief Scale a vector by a factor and adds the result to another vector - * - * Scale the vector y by the real value alpha and add the values of x : alpha * y += x - * - * \param[in] alpha The real value to scale with - * \param[in,out] y The vector to be scaled - * \param[in] x The vector to add - */ - void aypx(Real alpha, IVector& y, const IVector& x) const; - - /*! - * \brief Copy a vector in another one - * - * \param[in] x The vector to copy - * \param[in,out] r The copied vector - */ - void copy(const IVector& x, IVector& r) const; - - /*! - * \brief Compute the dot product of two vectors - * \param[in] x The first vector - * \param[in] y The second vector - * \returns The dot product of x * y - */ - Real dot(const IVector& x, const IVector& y) const; - - /*! - * \brief Scale a vector by a factor - * \param[in] alpha The real value to scale with - * \param[in,out] x The vector to be scaled - */ - void scal(Real alpha, IVector& x) const; - - /*! - * \brief Extract the diagonal of a matrix in a vector - * \param[in] a The matrix to extract the diagonal - * \param[in,out] x The diagonal elements of the matrix stored in a vector - */ - void diagonal(const IMatrix& a, IVector& x) const; - - /*! - * \brief Compute the reciprocal of a vector - * \param[in,out] x The vector to be processed - */ - void reciprocal(IVector& x) const; - - /*! - * \brief Compute the point wise multiplication of two vectors and store the result in - * another one - * \param[in] x The first vector - * \param[in] y The second vector - * \param[in,out] w The resulting vector - */ - void pointwiseMult(const IVector& x, const IVector& y, IVector& w) const; - - /*! - * \brief Compute a matrix vector product - * - * Compute the matrix-vector product a by x and store it in r : r = a * x - * - * \param[in] a The matrix to be multiplied - * \param[in] x The vector to be multipled - * \param[in,out] r The resulting vector - */ - void mult(const IMatrix& a, const UniqueArray& x, UniqueArray& r) const; - - /*! - * \brief Scale a vector by a factor and adds the result to another vector - * - * Scale the vector x by the real value alpha and add the result to the vector y : y += - * alpha * x - * - * \param[in] alpha The real value to scale with - * \param[in] x The vector to be scaled - * \param[in,out] y The resulting vector - */ - void axpy(Real alpha, const UniqueArray& x, UniqueArray& r) const; - - /*! - * \brief Scale a vector by a factor and adds the result to another vector - * - * Scale the vector y by the real value alpha and add the values of x : alpha * y += x - * - * \param[in] alpha The real value to scale with - * \param[in,out] y The vector to be scaled - * \param[in] x The vector to add - */ - void aypx(Real alpha, UniqueArray& y, const UniqueArray& x) const; - - /*! - * \brief Copy a vector in another one - * - * \param[in] x The vector to copy - * \param[in,out] r The copied vector - */ - void copy(const UniqueArray& x, UniqueArray& r) const; - - /*! - * \brief Compute the dot product of two vectors - * \param[in] local_size The size of the vectors - * \param[in] x The first vector - * \param[in] y The second vector - * \returns The dot product of x * y - */ - Real dot( - Integer local_size, const UniqueArray& x, const UniqueArray& y) const; - - /*! - * \brief Scale a vector by a factor - * \param[in] alpha The real value to scale with - * \param[in,out] x The vector to be scaled - */ - void scal(Real alpha, UniqueArray& x) const; - - /*! - * \brief Dumps a matrix to a file - * \param[in] a The matrix to dump - * \param[in] filename The name of the file - */ - void dump(IMatrix const& a, std::string const& filename) const; - - /*! - * \brief Dumps a vector to a file - * \param[in] x The vector to dump - * \param[in] filename The name of the file - */ - void dump(IVector const& x, std::string const& filename) const; - - private: - //! The type of the linear algebra - typedef typename AlgebraTraits::algebra_type KernelAlgebra; - typedef typename AlgebraTraits::algebra_expr_type KernelAlgebraExpr; - //! The linear algebra kernel - std::unique_ptr m_algebra; -}; - -/*---------------------------------------------------------------------------*/ -/*---------------------------------------------------------------------------*/ - -} // namespace Alien - -/*---------------------------------------------------------------------------*/ -/*---------------------------------------------------------------------------*/ diff --git a/src/core/alien/core/backend/LinearAlgebraExprT.h b/src/core/alien/core/backend/LinearAlgebraExprT.h deleted file mode 100644 index 5cc1b71ec..000000000 --- a/src/core/alien/core/backend/LinearAlgebraExprT.h +++ /dev/null @@ -1,268 +0,0 @@ -/* - -Copyright 2020 IFPEN-CEA - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - -http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. - -*/ - -/*! - * \file LinearAlgebraExprT.h - * \brief LinearAlgebraExprT.h - */ - -#pragma once - -#include - -#include -#include -#include -#include -#include - -/*---------------------------------------------------------------------------*/ -/*---------------------------------------------------------------------------*/ - -namespace Alien -{ - -/*---------------------------------------------------------------------------*/ -/*---------------------------------------------------------------------------*/ - -template -LinearAlgebraExpr::~LinearAlgebraExpr() {} - -/*---------------------------------------------------------------------------*/ - -template -Arccore::Real -LinearAlgebraExpr::norm0(const IVector& x) const -{ - const auto& vx = x.impl()->get(); - return m_algebra->norm0(vx); -} - -/*---------------------------------------------------------------------------*/ - -template -Arccore::Real -LinearAlgebraExpr::norm1(const IVector& x) const -{ - const auto& vx = x.impl()->get(); - return m_algebra->norm1(vx); -} - -/*---------------------------------------------------------------------------*/ -/*---------------------------------------------------------------------------*/ - -template -Arccore::Real -LinearAlgebraExpr::norm2(const IVector& x) const -{ - const auto& vx = x.impl()->get(); - return m_algebra->norm2(vx); -} - -/*---------------------------------------------------------------------------*/ -/*---------------------------------------------------------------------------*/ - -template -void LinearAlgebraExpr::mult(const IMatrix& a, const IVector& x, IVector& r) const -{ - const auto& ma = a.impl()->get(); - const auto& vx = x.impl()->get(); - auto& vr = r.impl()->get(true); - ALIEN_ASSERT((ma.colSpace() == vx.space() && ma.rowSpace() == vr.space()), - ("Incompatible spaces")); - m_algebra->mult(ma, vx, vr); -} - -/*---------------------------------------------------------------------------*/ -/*---------------------------------------------------------------------------*/ - -template -void LinearAlgebraExpr::axpy(Real alpha, const IVector& x, IVector& r) const -{ - const auto& vx = x.impl()->get(); - auto& vr = r.impl()->get(true); - ALIEN_ASSERT((vx.space() == vr.space()), ("Incompatible spaces")); - m_algebra->axpy(alpha, vx, vr); -} - -/*---------------------------------------------------------------------------*/ -/*---------------------------------------------------------------------------*/ - -template -void LinearAlgebraExpr::aypx(Real alpha, IVector& y, const IVector& x) const -{ - const auto& vx = x.impl()->get(); - auto& vy = y.impl()->get(true); - ALIEN_ASSERT((vx.space() == vy.space()), ("Incompatible spaces")); - m_algebra->aypx(alpha, vy, vx); -} - -/*---------------------------------------------------------------------------*/ - -template -void LinearAlgebraExpr::copy(const IVector& x, IVector& r) const -{ - const auto& vx = x.impl()->get(); - auto& vr = r.impl()->get(true); - ALIEN_ASSERT((vx.space() == vr.space()), ("Incompatible spaces")); - m_algebra->copy(vx, vr); -} - -/*---------------------------------------------------------------------------*/ - -template -Real LinearAlgebraExpr::dot(const IVector& x, const IVector& y) const -{ - const auto& vx = x.impl()->get(); - const auto& vy = y.impl()->get(); - ALIEN_ASSERT((vx.space() == vy.space()), ("Incompatible space")); - return m_algebra->dot(vx, vy); -} - -/*---------------------------------------------------------------------------*/ - -template -void LinearAlgebraExpr::diagonal(const IMatrix& a, IVector& x) const -{ - auto& vx = x.impl()->get(true); - const auto& ma = a.impl()->get(); - ALIEN_ASSERT((ma.rowSpace() == ma.colSpace()), ("Matrix not square")); - ALIEN_ASSERT((ma.rowSpace() == vx.space()), ("Incompatible space")); - m_algebra->diagonal(ma, vx); -} - -/*---------------------------------------------------------------------------*/ -/*---------------------------------------------------------------------------*/ - -template -void LinearAlgebraExpr::reciprocal(IVector& x) const -{ - auto& vx = x.impl()->get(true); - m_algebra->reciprocal(vx); -} - -/*---------------------------------------------------------------------------*/ -/*---------------------------------------------------------------------------*/ - -template -void LinearAlgebraExpr::scal(Real alpha, IVector& x) const -{ - auto& vx = x.impl()->get(true); - m_algebra->scal(alpha, vx); -} - -/*---------------------------------------------------------------------------*/ -/*---------------------------------------------------------------------------*/ - -template -void LinearAlgebraExpr::pointwiseMult( -const IVector& x, const IVector& y, IVector& w) const -{ - auto& vw = w.impl()->get(true); - const auto& vx = x.impl()->get(); - const auto& vy = y.impl()->get(); - ALIEN_ASSERT((vy.space() == vx.space()), ("Incompatible space")); - ALIEN_ASSERT((vw.space() == vx.space()), ("Incompatible space")); - m_algebra->pointwiseMult(vx, vy, vw); -} - -/*---------------------------------------------------------------------------*/ -/*---------------------------------------------------------------------------*/ - -template -void LinearAlgebraExpr::mult( -const IMatrix& a, const UniqueArray& x, UniqueArray& r) const -{ - auto const& ma = a.impl()->get(); - m_algebra->mult(ma, x, r); -} - -/*---------------------------------------------------------------------------*/ -/*---------------------------------------------------------------------------*/ - -template -void LinearAlgebraExpr::axpy(Real alpha, const UniqueArray& x, UniqueArray& r) const -{ - m_algebra->axpy(alpha, x, r); -} - -/*---------------------------------------------------------------------------*/ -/*---------------------------------------------------------------------------*/ - -template -void LinearAlgebraExpr::aypx(Real alpha, UniqueArray& y, UniqueArray const& x) const -{ - m_algebra->aypx(alpha, y, x); -} - -/*---------------------------------------------------------------------------*/ -/*---------------------------------------------------------------------------*/ - -template -void LinearAlgebraExpr::copy( -const Alien::UniqueArray& x, Alien::UniqueArray& r) const -{ - m_algebra->copy(x, r); -} - -/*---------------------------------------------------------------------------*/ -/*---------------------------------------------------------------------------*/ - -template -Real LinearAlgebraExpr::dot( -Integer local_size, const UniqueArray& x, const UniqueArray& y) const -{ - return m_algebra->dot(local_size, x, y); -} - -/*---------------------------------------------------------------------------*/ -/*---------------------------------------------------------------------------*/ - -template -void LinearAlgebraExpr::scal(Real alpha, Alien::UniqueArray& x) const -{ - m_algebra->scal(alpha, x); -} - -/*---------------------------------------------------------------------------*/ -/*---------------------------------------------------------------------------*/ - -template -void LinearAlgebraExpr::dump(const IMatrix& a, std::string const& filename) const -{ - auto const& ma = a.impl()->get(); - m_algebra->dump(ma, filename); -} - -/*---------------------------------------------------------------------------*/ -/*---------------------------------------------------------------------------*/ - -template -void LinearAlgebraExpr::dump(const IVector& x, std::string const& filename) const -{ - auto const& vx = x.impl()->get(); - m_algebra->dump(vx, filename); -} - -/*---------------------------------------------------------------------------*/ -/*---------------------------------------------------------------------------*/ - -} // namespace Alien - -/*---------------------------------------------------------------------------*/ -/*---------------------------------------------------------------------------*/ diff --git a/src/core/alien/core/backend/LinearAlgebraT.h b/src/core/alien/core/backend/LinearAlgebraT.h deleted file mode 100644 index 3b72a7303..000000000 --- a/src/core/alien/core/backend/LinearAlgebraT.h +++ /dev/null @@ -1,213 +0,0 @@ -/* - * Copyright 2020 IFPEN-CEA - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * SPDX-License-Identifier: Apache-2.0 - */ - -/*! - * \file LinearAlgebraT.h - * \brief LinearAlgebraT.h - */ - -#pragma once - -#include - -#include - -#include -#include -#include -#include - -/*---------------------------------------------------------------------------*/ -/*---------------------------------------------------------------------------*/ - -namespace Alien -{ - -/*---------------------------------------------------------------------------*/ -/*---------------------------------------------------------------------------*/ - -template -LinearAlgebra::~LinearAlgebra() {} - -/*---------------------------------------------------------------------------*/ - -template -Arccore::Real -LinearAlgebra::norm0(const IVector& x) const -{ - const auto& vx = x.impl()->get(); - return m_algebra->norm0(vx); -} - -/*---------------------------------------------------------------------------*/ - -template -Arccore::Real -LinearAlgebra::norm1(const IVector& x) const -{ - const auto& vx = x.impl()->get(); - return m_algebra->norm1(vx); -} - -/*---------------------------------------------------------------------------*/ -/*---------------------------------------------------------------------------*/ - -template -Arccore::Real -LinearAlgebra::norm2(const IVector& x) const -{ - const auto& vx = x.impl()->get(); - return m_algebra->norm2(vx); -} - -/*---------------------------------------------------------------------------*/ -/*---------------------------------------------------------------------------*/ - -template -void LinearAlgebra::mult(const IMatrix& a, const IVector& x, IVector& r) const -{ - const auto& ma = a.impl()->get(); - const auto& vx = x.impl()->get(); - auto& vr = r.impl()->get(true); - ALIEN_ASSERT((ma.colSpace() == vx.space() && ma.rowSpace() == vr.space()), - ("Incompatible spaces")); - m_algebra->mult(ma, vx, vr); -} - -/*---------------------------------------------------------------------------*/ -/*---------------------------------------------------------------------------*/ - -template -void LinearAlgebra::axpy(Real alpha, const IVector& x, IVector& r) const -{ - const auto& vx = x.impl()->get(); - auto& vr = r.impl()->get(true); - ALIEN_ASSERT((vx.space() == vr.space()), ("Incompatible spaces")); - m_algebra->axpy(alpha, vx, vr); -} - -/*---------------------------------------------------------------------------*/ -/*---------------------------------------------------------------------------*/ - -template -void LinearAlgebra::aypx(Real alpha, IVector& y, const IVector& x) const -{ - const auto& vx = x.impl()->get(); - auto& vy = y.impl()->get(true); - ALIEN_ASSERT((vx.space() == vy.space()), ("Incompatible spaces")); - m_algebra->aypx(alpha, vy, vx); -} - -/*---------------------------------------------------------------------------*/ - -template -void LinearAlgebra::copy(const IVector& x, IVector& r) const -{ - const auto& vx = x.impl()->get(); - auto& vr = r.impl()->get(true); - ALIEN_ASSERT((vx.space() == vr.space()), ("Incompatible spaces")); - m_algebra->copy(vx, vr); -} - -/*---------------------------------------------------------------------------*/ - -template -Real LinearAlgebra::dot(const IVector& x, const IVector& y) const -{ - const auto& vx = x.impl()->get(); - const auto& vy = y.impl()->get(); - ALIEN_ASSERT((vx.space() == vy.space()), ("Incompatible space")); - return m_algebra->dot(vx, vy); -} - -/*---------------------------------------------------------------------------*/ - -template -void LinearAlgebra::diagonal(const IMatrix& a, IVector& x) const -{ - auto& vx = x.impl()->get(true); - const auto& ma = a.impl()->get(); - ALIEN_ASSERT((ma.rowSpace() == ma.colSpace()), ("Matrix not square")); - ALIEN_ASSERT((ma.rowSpace() == vx.space()), ("Incompatible space")); - m_algebra->diagonal(ma, vx); -} - -/*---------------------------------------------------------------------------*/ -/*---------------------------------------------------------------------------*/ - -template -void LinearAlgebra::reciprocal(IVector& x) const -{ - auto& vx = x.impl()->get(true); - m_algebra->reciprocal(vx); -} - -/*---------------------------------------------------------------------------*/ -/*---------------------------------------------------------------------------*/ - -template -void LinearAlgebra::scal(Real alpha, IVector& x) const -{ - auto& vx = x.impl()->get(true); - m_algebra->scal(alpha, vx); -} - -/*---------------------------------------------------------------------------*/ -/*---------------------------------------------------------------------------*/ - -template -void LinearAlgebra::pointwiseMult( -const IVector& x, const IVector& y, IVector& w) const -{ - auto& vw = w.impl()->get(true); - const auto& vx = x.impl()->get(); - const auto& vy = y.impl()->get(); - ALIEN_ASSERT((vy.space() == vx.space()), ("Incompatible space")); - ALIEN_ASSERT((vw.space() == vx.space()), ("Incompatible space")); - m_algebra->pointwiseMult(vx, vy, vw); -} - -/*---------------------------------------------------------------------------*/ -/*---------------------------------------------------------------------------*/ -/*---------------------------------------------------------------------------*/ -/*---------------------------------------------------------------------------*/ - -template -void LinearAlgebra::dump(const IMatrix& a, std::string const& filename) const -{ - auto const& ma = a.impl()->get(); - m_algebra->dump(ma, filename); -} - -/*---------------------------------------------------------------------------*/ -/*---------------------------------------------------------------------------*/ - -template -void LinearAlgebra::dump(const IVector& x, std::string const& filename) const -{ - auto const& vx = x.impl()->get(); - m_algebra->dump(vx, filename); -} - -/*---------------------------------------------------------------------------*/ -/*---------------------------------------------------------------------------*/ - -} // namespace Alien - -/*---------------------------------------------------------------------------*/ -/*---------------------------------------------------------------------------*/ diff --git a/src/core/alien/core/impl/MultiMatrixImpl.cc b/src/core/alien/core/impl/MultiMatrixImpl.cc index 690c5d5f6..56062912d 100644 --- a/src/core/alien/core/impl/MultiMatrixImpl.cc +++ b/src/core/alien/core/impl/MultiMatrixImpl.cc @@ -198,7 +198,7 @@ MultiMatrixImpl::clone() const typedef BackEnd::tag::simplecsr tag; // We get the last up to date implementation - const SimpleCSRMatrix& matrixToClone = this->get(); + const SimpleCSRMatrix& matrixToClone = dynamic_cast&>(this->get("simplecsr")); // And clone it SimpleCSRMatrix* matrixCloned = matrixToClone.cloneTo(impl); matrixCloned->setTimestamp(impl, matrixToClone.timestamp()); @@ -278,7 +278,7 @@ void MultiMatrixImpl::updateImpl(IMatrixImpl* target) const }; // Request simplecsr implementation - auto* simplecsr = getImpl>("simplecsr"); + auto* simplecsr = getImpl("simplecsr"); // Checking that we have a converter from simplecsr to the requested implementation auto* simplecsr_target = diff --git a/src/core/alien/core/impl/MultiVectorImpl.cc b/src/core/alien/core/impl/MultiVectorImpl.cc index 9bc07a426..5db321396 100644 --- a/src/core/alien/core/impl/MultiVectorImpl.cc +++ b/src/core/alien/core/impl/MultiVectorImpl.cc @@ -150,14 +150,13 @@ MultiVectorImpl::clone() const auto impl = new MultiVectorImpl(*this); // We get the last up to date implementation - typedef BackEnd::tag::simplecsr tag; - const SimpleCSRVector& vectorToClone = this->get(); + const SimpleCSRVector& vectorToClone = dynamic_cast&>(this->get("simplecsr")); // And clone it SimpleCSRVector* vectorCloned = vectorToClone.cloneTo(impl); vectorCloned->setTimestamp(impl, vectorToClone.timestamp()); vectorCloned->updateTimestamp(); impl->m_impls2.insert( - MultiVectorImplMap::value_type(AlgebraTraits::name(), vectorCloned)); + MultiVectorImplMap::value_type("simplecsr", vectorCloned)); // TOCHECK: to be removed or not ? /* WARNING: this implementation is temporary. Later it should be implemented through a @@ -231,7 +230,7 @@ void MultiVectorImpl::updateImpl(IVectorImpl* target) const }; // Request simplecsr implementation - auto* simplecsr = getImpl>("simplecsr"); + auto* simplecsr = dynamic_cast*>(getImpl("simplecsr")); // Checking that we have a converter from simplecsr to the requested implementation auto* simplecsr_target = diff --git a/src/core/alien/core/impl/MultiVectorImpl.h b/src/core/alien/core/impl/MultiVectorImpl.h index 5cea668bd..29ff02a77 100644 --- a/src/core/alien/core/impl/MultiVectorImpl.h +++ b/src/core/alien/core/impl/MultiVectorImpl.h @@ -158,8 +158,7 @@ class ALIEN_EXPORT MultiVectorImpl : public TimestampMng * * \returns The up to date vector in the requested implementation */ - template - const typename AlgebraTraits::vector_type& get() const; + const IVectorImpl& get(BackEndId backEndId) const; /*! * \brief Get a specific vector implementation @@ -170,12 +169,10 @@ class ALIEN_EXPORT MultiVectorImpl : public TimestampMng * \param[in] update_stamp Whether or not the timestamp should be increased or not * \returns The up to date vector in the requested implementation */ - template - typename AlgebraTraits::vector_type& get(bool update_stamp); + IVectorImpl& get(BackEndId backEndId, bool update_stamp); //! Release a vector implementation - template - void release() const; + void release(BackEndId backEndId) const; private: /*! @@ -183,7 +180,6 @@ class ALIEN_EXPORT MultiVectorImpl : public TimestampMng * \param[in] backend The id of the specific implementation * \returns The vector in the requested format */ - template IVectorImpl*& getImpl(BackEndId backend) const; /*! @@ -216,46 +212,39 @@ class ALIEN_EXPORT MultiVectorImpl : public TimestampMng std::shared_ptr m_variable_block; }; -/*---------------------------------------------------------------------------*/ -/*---------------------------------------------------------------------------*/ - -template -const typename AlgebraTraits::vector_type& -MultiVectorImpl::get() const +const IVectorImpl& +MultiVectorImpl::get(BackEndId backEndId) const { - typedef typename AlgebraTraits::vector_type vector_type; - IVectorImpl*& impl2 = getImpl(AlgebraTraits::name()); + IVectorImpl*& impl2 = getImpl(backEndId); ALIEN_ASSERT( - (impl2->backend() == AlgebraTraits::name()), ("Inconsistent backend")); + (impl2->backend() == backEndId), ("Inconsistent backend")); updateImpl(impl2); - return *dynamic_cast(impl2); + return *impl2; } /*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/ -template -typename AlgebraTraits::vector_type& -MultiVectorImpl::get(const bool update_stamp) +IVectorImpl& +MultiVectorImpl::get(BackEndId backEndId, const bool update_stamp) { - typedef typename AlgebraTraits::vector_type vector_type; - IVectorImpl*& impl2 = getImpl(AlgebraTraits::name()); + IVectorImpl*& impl2 = getImpl(backEndId); ALIEN_ASSERT( - (impl2->backend() == AlgebraTraits::name()), ("Inconsistent backend")); + (impl2->backend() == backEndId), ("Inconsistent backend")); updateImpl(impl2); if (update_stamp) { impl2->updateTimestamp(); } - return *dynamic_cast(impl2); + return *impl2; } /*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/ -template -void MultiVectorImpl::release() const + +void MultiVectorImpl::release(BackEndId backEndId) const { - auto finder = m_impls2.find(AlgebraTraits::name()); + auto finder = m_impls2.find(backEndId); if (finder == m_impls2.end()) return; // already freed delete finder->second, finder->second = NULL; @@ -264,14 +253,13 @@ void MultiVectorImpl::release() const /*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/ -template IVectorImpl*& MultiVectorImpl::getImpl(BackEndId backend) const { auto inserter = m_impls2.insert(MultiVectorImplMap::value_type(backend, NULL)); IVectorImpl*& impl2 = inserter.first->second; if (impl2 == NULL) { - auto new_impl = new vector_type(this); // constructeur associ� � un multi-impl + auto new_impl = new IVectorImpl(this); // constructeur associ� � un multi-impl new_impl->init(*m_distribution.get(), true); impl2 = new_impl; } diff --git a/src/core/alien/data/CompositeVector.cc b/src/core/alien/data/CompositeVector.cc index 121e8614c..5aa16fc9e 100644 --- a/src/core/alien/data/CompositeVector.cc +++ b/src/core/alien/data/CompositeVector.cc @@ -59,7 +59,7 @@ CompositeVector::CompositeVector() CompositeVector::CompositeVector(Integer nc) : m_impl(new CompositeKernel::MultiVectorImpl()) -, m_composite_vector(m_impl->get(false)) +, m_composite_vector(dynamic_cast(m_impl->get("composite", false))) { m_impl->setFeature("composite"); diff --git a/src/core/alien/expression/normalization/NormalizeOpt.cc b/src/core/alien/expression/normalization/NormalizeOpt.cc index 3f4904aa3..638eda003 100644 --- a/src/core/alien/expression/normalization/NormalizeOpt.cc +++ b/src/core/alien/expression/normalization/NormalizeOpt.cc @@ -79,8 +79,8 @@ void NormalizeOpt::setOpt(eOptType opt, bool flag) NormalizeOpt::eErrorType NormalizeOpt::normalize(IMatrix& m, IVector& x) const { - MatrixImpl& A = m.impl()->get(true); - VectorImpl& b = x.impl()->get(true); + MatrixImpl& A = dynamic_cast&>(m.impl()->get("simplecsr", true)); + VectorImpl& b = dynamic_cast&>(x.impl()->get("simplecsr", true)); return _normalize(A, b); } @@ -92,12 +92,12 @@ NormalizeOpt::normalize( CompositeMatrix& m, CompositeVector& x, ConstArrayView eq_ids) const { // need to update timestamp of all submatrices - MatrixImpl& A00 = m(0, 0).impl()->get(true); - MatrixImpl& A01 = m(0, 1).impl()->get(true); - MatrixImpl& A10 = m(1, 0).impl()->get(true); - MatrixImpl& A11 = m(1, 1).impl()->get(true); - VectorImpl& b0 = x[0].impl()->get(true); - VectorImpl& b1 = x[1].impl()->get(true); + MatrixImpl& A00 = dynamic_cast&>(m(0, 0).impl()->get("simplecsr", true)); + MatrixImpl& A01 = dynamic_cast&>(m(0, 1).impl()->get("simplecsr", true)); + MatrixImpl& A10 = dynamic_cast&>(m(1, 0).impl()->get("simplecsr", true)); + MatrixImpl& A11 = dynamic_cast&>(m(1, 1).impl()->get("simplecsr", true)); + VectorImpl& b0 = dynamic_cast&>(x[0].impl()->get("simplecsr", true)); + VectorImpl& b1 = dynamic_cast&>(x[1].impl()->get("simplecsr", true)); { eErrorType error = _normalize(A00, A01, m(0, 1).impl()->hasFeature("transposed"), eq_ids, b0); diff --git a/src/core/alien/expression/solver/ILinearAlgebra.h b/src/core/alien/expression/solver/ILinearAlgebra.h deleted file mode 100644 index d1e1586d5..000000000 --- a/src/core/alien/expression/solver/ILinearAlgebra.h +++ /dev/null @@ -1,113 +0,0 @@ -/* - * Copyright 2020 IFPEN-CEA - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * SPDX-License-Identifier: Apache-2.0 - */ - -/*! - * \file ILinearAlgebra.h - * \brief ILinearAlgebra.h - */ - -#pragma once - -#include - -/*---------------------------------------------------------------------------*/ -/*---------------------------------------------------------------------------*/ - -namespace Alien -{ - -/*---------------------------------------------------------------------------*/ -/*---------------------------------------------------------------------------*/ - -class IVector; -class IMatrix; - -/*---------------------------------------------------------------------------*/ -/*---------------------------------------------------------------------------*/ - -/*! - * \ingroup expression - * \brief Interface for linear algebra - */ -class ILinearAlgebra -{ - public: - //! Free resources - virtual ~ILinearAlgebra() {} - - public: - /*! - * \brief Computes norm 0 of a vector - * \param[in] x The vector - * \returns The norm0 of the vector - */ - virtual Arccore::Real norm0(const IVector& x) const = 0; - - /*! - * \brief Computes norm 1 of a vector - * \param[in] x The vector - * \returns The norm1 of the vector - */ - virtual Arccore::Real norm1(const IVector& x) const = 0; - - /*! - * \brief Computes norm 2 of a vector - * \param[in] x The vector - * \returns The norm2 of the vector - */ - virtual Arccore::Real norm2(const IVector& x) const = 0; - - /*! - * \brief Computes a matrix vector product - * \param[in] a The matrix to multiply - * \param[in] x The vector to multiply - * \param[in,out] r The vector to store the result - */ - virtual void mult(const IMatrix& a, const IVector& x, IVector& r) const = 0; - - /*! - * \brief Computes y += alpa * x - * \param[in] alpha The real value to scale the vector - * \param[in] x The vector to scale - * \param[in, out] y The vector to store the result - */ - virtual void axpy(Real alpha, const IVector& x, IVector& y) const = 0; - - /*! - * \brief Copy a vector - * \param[in] x The vector to copy - * \param[in,out] r The copied vector - */ - virtual void copy(const IVector& x, IVector& r) const = 0; - - /*! - * \brief Computes the dot product of two vectors - * \param[in] x The first vector - * \param[in] y The second vector - * \returns The dot product - */ - virtual Arccore::Real dot(const IVector& x, const IVector& y) const = 0; -}; - -/*---------------------------------------------------------------------------*/ -/*---------------------------------------------------------------------------*/ - -} // namespace Alien - -/*---------------------------------------------------------------------------*/ -/*---------------------------------------------------------------------------*/ diff --git a/src/core/alien/handlers/block/BlockVectorReaderT.h b/src/core/alien/handlers/block/BlockVectorReaderT.h index 2d63e0b85..40b29d8fe 100644 --- a/src/core/alien/handlers/block/BlockVectorReaderT.h +++ b/src/core/alien/handlers/block/BlockVectorReaderT.h @@ -47,7 +47,7 @@ namespace Common , m_local_offset(0) { using namespace Alien; - const SimpleCSRVector& v = vector.impl()->get(); + const SimpleCSRVector& v = dynamic_cast&>(vector.impl()->get("simplecsr")); m_values = v.fullValues(); m_vector_impl = &v; m_local_offset = v.distribution().offset(); diff --git a/src/core/alien/handlers/block/BlockVectorWriterT.h b/src/core/alien/handlers/block/BlockVectorWriterT.h index 92a23c78e..f36d75092 100644 --- a/src/core/alien/handlers/block/BlockVectorWriterT.h +++ b/src/core/alien/handlers/block/BlockVectorWriterT.h @@ -52,7 +52,7 @@ namespace Common { using namespace Alien; SimpleCSRVector& v = - m_vector.impl()->template get(false); + dynamic_cast&>(m_vector.impl()->get("simplecsr", false)); m_values = v.fullValues(); m_vector_impl = &v; } diff --git a/src/core/alien/handlers/block/ProfiledFixedBlockMatrixBuilder.cc b/src/core/alien/handlers/block/ProfiledFixedBlockMatrixBuilder.cc index 53d7ec606..bba92cd2a 100644 --- a/src/core/alien/handlers/block/ProfiledFixedBlockMatrixBuilder.cc +++ b/src/core/alien/handlers/block/ProfiledFixedBlockMatrixBuilder.cc @@ -48,7 +48,7 @@ namespace Common , m_finalized(false) { m_matrix.impl()->lock(); - m_matrix_impl = &m_matrix.impl()->get(true); + m_matrix_impl = &(dynamic_cast&>(m_matrix.impl()->get("simplecsr", true))); const ISpace& space = m_matrix.rowSpace(); // if (space != m_matrix.colSpace()) diff --git a/src/core/alien/handlers/profiler/MatrixProfilerT.h b/src/core/alien/handlers/profiler/MatrixProfilerT.h index 659026190..94329cd7f 100644 --- a/src/core/alien/handlers/profiler/MatrixProfilerT.h +++ b/src/core/alien/handlers/profiler/MatrixProfilerT.h @@ -50,7 +50,7 @@ namespace Common MatrixProfilerT::MatrixProfilerT(IMatrix& matrix) : m_matrix(matrix) { - m_matrix_impl = &m_matrix.impl()->template get(false); + m_matrix_impl = &(dynamic_cast&>(m_matrix.impl()->get("simplecsr", false))); const ISpace& space = m_matrix_impl->rowSpace(); diff --git a/src/core/alien/handlers/scalar/BaseDirectMatrixBuilder.cc b/src/core/alien/handlers/scalar/BaseDirectMatrixBuilder.cc index 47679f8b3..36a1fe1cc 100644 --- a/src/core/alien/handlers/scalar/BaseDirectMatrixBuilder.cc +++ b/src/core/alien/handlers/scalar/BaseDirectMatrixBuilder.cc @@ -80,7 +80,7 @@ namespace Common , m_trace(nullptr) { m_matrix.impl()->lock(); - m_matrix_impl = &m_matrix.impl()->get(true); + m_matrix_impl = &dynamic_cast&>(m_matrix.impl()->get("simplecsr", true)); const MatrixDistribution& dist = m_matrix_impl->distribution(); diff --git a/src/core/alien/handlers/scalar/BaseProfiledMatrixBuilder.cc b/src/core/alien/handlers/scalar/BaseProfiledMatrixBuilder.cc index f558a6afd..01011f5d6 100644 --- a/src/core/alien/handlers/scalar/BaseProfiledMatrixBuilder.cc +++ b/src/core/alien/handlers/scalar/BaseProfiledMatrixBuilder.cc @@ -49,7 +49,7 @@ namespace Common , m_finalized(false) { m_matrix.impl()->lock(); - m_matrix_impl = &m_matrix.impl()->get(true); + m_matrix_impl = &dynamic_cast&>(m_matrix.impl()->get("simplecsr", true)); const MatrixDistribution& dist = m_matrix_impl->distribution(); diff --git a/src/core/alien/handlers/scalar/VectorReaderT.h b/src/core/alien/handlers/scalar/VectorReaderT.h index c4fecc637..9a695219b 100644 --- a/src/core/alien/handlers/scalar/VectorReaderT.h +++ b/src/core/alien/handlers/scalar/VectorReaderT.h @@ -42,7 +42,7 @@ namespace Common template VectorReaderT::VectorReaderT(const IVector& vector) { - const auto& v = vector.impl()->get(); + const auto& v = dynamic_cast&>(vector.impl()->get("simple_csr")); m_values = v.fullValues(); m_local_offset = v.distribution().offset(); } diff --git a/src/core/alien/handlers/scalar/VectorWriterT.h b/src/core/alien/handlers/scalar/VectorWriterT.h index 2fa9d4e99..12d1ad6fe 100644 --- a/src/core/alien/handlers/scalar/VectorWriterT.h +++ b/src/core/alien/handlers/scalar/VectorWriterT.h @@ -45,7 +45,7 @@ namespace Common , m_local_offset(0) , m_finalized(false) { - auto& v = vector.impl()->get(update); + auto& v = dynamic_cast&>(vector.impl()->get("simplecsr", update)); m_local_offset = v.distribution().offset(); m_values = v.fullValues(); m_time_stamp = &v; diff --git a/src/core/alien/kernels/redistributor/Redistributor.cc b/src/core/alien/kernels/redistributor/Redistributor.cc index 7a9c87821..e292667f4 100644 --- a/src/core/alien/kernels/redistributor/Redistributor.cc +++ b/src/core/alien/kernels/redistributor/Redistributor.cc @@ -59,7 +59,7 @@ Redistributor::redistribute(MultiMatrixImpl* mat) std::shared_ptr Redistributor::redistribute(MultiVectorImpl* vect) { - auto& red_vect = vect->get(false); + auto& red_vect = dynamic_cast(vect->get("redistributor", false)); return red_vect.updateTargetPM(m_distributor.get()); } diff --git a/src/core/alien/kernels/redistributor/RedistributorVector.cc b/src/core/alien/kernels/redistributor/RedistributorVector.cc index ca44a99de..3d4f814de 100644 --- a/src/core/alien/kernels/redistributor/RedistributorVector.cc +++ b/src/core/alien/kernels/redistributor/RedistributorVector.cc @@ -85,15 +85,15 @@ RedistributorVector::updateTargetPM(const RedistributorCommPlan* commPlan) std::shared_ptr RedistributorVector::redistribute() { - auto& vec_src = m_multi_impl->get(); - auto& vec_tgt = m_tgt_impl->get(true); + auto& vec_src = dynamic_cast&>(m_multi_impl->get("simplecsr")); + auto& vec_tgt = dynamic_cast&>(m_tgt_impl->get("simplecsr", true)); m_distributor->distribute(vec_src, vec_tgt); return m_tgt_impl; } void RedistributorVector::redistributeBack(SimpleCSRVector& vec_tgt) const { - auto& vec_src = m_tgt_impl->get(); + auto& vec_src = dynamic_cast&>(m_tgt_impl->get("simplecsr")); m_distributor->distributeBack(vec_src, vec_tgt); } diff --git a/src/core/alien/kernels/simple_csr/CMakeLists.txt b/src/core/alien/kernels/simple_csr/CMakeLists.txt index f2e20eee5..cad2113cd 100644 --- a/src/core/alien/kernels/simple_csr/CMakeLists.txt +++ b/src/core/alien/kernels/simple_csr/CMakeLists.txt @@ -1,27 +1,20 @@ # Copyright 2020 IFPEN-CEA -# +# # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at -# +# # http://www.apache.org/licenses/LICENSE-2.0 -# +# # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. -# +# # SPDX-License-Identifier: Apache-2.0 add_library(alien_kernel_simplecsr OBJECT - algebra/CBLASMPIKernel.h - algebra/alien_cblas.h - algebra/SimpleCSRInternalLinearAlgebra.cc - algebra/SimpleCSRInternalLinearAlgebra.h - algebra/SimpleCSRLinearAlgebra.h - algebra/SimpleCSRMatrixMult.h - algebra/SimpleCSRMatrixMultT.h CSRStructInfo.h DistStructInfo.cc DistStructInfo.h diff --git a/src/core/alien/kernels/simple_csr/algebra/CBLASMPIKernel.h b/src/core/alien/kernels/simple_csr/algebra/CBLASMPIKernel.h deleted file mode 100644 index 600f68f76..000000000 --- a/src/core/alien/kernels/simple_csr/algebra/CBLASMPIKernel.h +++ /dev/null @@ -1,115 +0,0 @@ -/* - * Copyright 2020 IFPEN-CEA - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * SPDX-License-Identifier: Apache-2.0 - */ - -#pragma once - -#include -#include - -#include -#include - -namespace Alien -{ - -class CBLASMPIKernel -{ - public: - // static const tag::eType type = tag::CPU; - static const bool is_hybrid = false; - static const bool is_mpi = true; - - template - static void copy( - Distribution const& dist ALIEN_UNUSED_PARAM, const VectorT& x, VectorT& y) - { - typedef typename VectorT::ValueType ValueType; - cblas::copy( - x.scalarizedLocalSize(), (ValueType*)x.getDataPtr(), 1, y.getDataPtr(), 1); - } - - template - static void axpy(Distribution const& dist ALIEN_UNUSED_PARAM, - typename VectorT::ValueType alpha, const VectorT& x, VectorT& y) - { - cblas::axpy(x.scalarizedLocalSize(), alpha, x.getDataPtr(), 1, y.getDataPtr(), 1); - } - - template - static void scal(Distribution const& dist ALIEN_UNUSED_PARAM, - typename VectorT::ValueType alpha, VectorT& x) - { - cblas::scal(x.scalarizedLocalSize(), alpha, x.getDataPtr(), 1); - } - - template - static void pointwiseMult(Distribution const& dist, - VectorT const& x, - VectorT const& y, - VectorT& z) - { - auto local_size = x.scalarizedLocalSize(); - auto x_ptr = x.getDataPtr(); - auto y_ptr = y.getDataPtr(); - auto z_ptr = z.getDataPtr(); - for (std::size_t i = 0; i < local_size; ++i) { - z_ptr[i] = x_ptr[i] * y_ptr[i]; - } - } - - template - static void assign(Distribution const& dist, - typename VectorT::ValueType alpha, - VectorT& y) - { - auto local_size = y.scalarizedLocalSize(); - auto y_ptr = y.getDataPtr(); - for (std::size_t i = 0; i < local_size; ++i) { - y_ptr[i] = alpha; - } - } - - template - static typename VectorT::ValueType dot( - Distribution const& dist, const VectorT& x, const VectorT& y) - { - typedef typename VectorT::ValueType ValueType; - ValueType value = cblas::dot(x.scalarizedLocalSize(), (ValueType*)x.getDataPtr(), 1, - (ValueType*)y.getDataPtr(), 1); - if (dist.isParallel()) { - return Arccore::MessagePassing::mpAllReduce( - dist.parallelMng(), Arccore::MessagePassing::ReduceSum, value); - } - return value; - } - - template - static typename VectorT::ValueType nrm2(Distribution const& dist, const VectorT& x) - { - typedef typename VectorT::ValueType ValueType; - typename VectorT::ValueType value = cblas::dot(x.scalarizedLocalSize(), - (ValueType*)x.getDataPtr(), 1, (ValueType*)x.getDataPtr(), 1); - if (dist.isParallel()) { - value = Arccore::MessagePassing::mpAllReduce( - dist.parallelMng(), Arccore::MessagePassing::ReduceSum, value); - } - return std::sqrt(value); - } -}; - -} // namespace Alien diff --git a/src/core/alien/kernels/simple_csr/algebra/SimpleCSRInternalLinearAlgebra.cc b/src/core/alien/kernels/simple_csr/algebra/SimpleCSRInternalLinearAlgebra.cc deleted file mode 100644 index 7763112eb..000000000 --- a/src/core/alien/kernels/simple_csr/algebra/SimpleCSRInternalLinearAlgebra.cc +++ /dev/null @@ -1,394 +0,0 @@ -/* - * Copyright 2020 IFPEN-CEA - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * SPDX-License-Identifier: Apache-2.0 - */ - -#include "SimpleCSRInternalLinearAlgebra.h" -#include - -#include -#include - -#include -#include - -#include "CBLASMPIKernel.h" -#include "SimpleCSRMatrixMult.h" - -/*---------------------------------------------------------------------------*/ - -namespace Alien -{ - -using namespace Arccore; - -template class ALIEN_EXPORT LinearAlgebra; -template class ALIEN_EXPORT LinearAlgebraExpr; - -ALIEN_EXPORT IInternalLinearAlgebra, SimpleCSRVector>* -SimpleCSRInternalLinearAlgebraFactory() -{ - return new SimpleCSRInternalLinearAlgebra(); -} - -ALIEN_EXPORT IInternalLinearAlgebraExpr, SimpleCSRVector>* -SimpleCSRInternalLinearAlgebraExprFactory() -{ - return new SimpleCSRInternalLinearAlgebraExpr(); -} - -/*---------------------------------------------------------------------------*/ - -namespace Internal = SimpleCSRInternal; - -/*---------------------------------------------------------------------------*/ - -SimpleCSRInternalLinearAlgebra::SimpleCSRInternalLinearAlgebra() -: IInternalLinearAlgebra() -{} - -/*---------------------------------------------------------------------------*/ - -SimpleCSRInternalLinearAlgebra::~SimpleCSRInternalLinearAlgebra() -{ -#ifdef ALIEN_USE_PERF_TIMER - m_timer.printInfo("SIMPLECSR-ALGEBRA"); -#endif -} - -/*---------------------------------------------------------------------------*/ -/*---------------------------------------------------------------------------*/ -SimpleCSRInternalLinearAlgebra::ResourceType const& -SimpleCSRInternalLinearAlgebra::resource(Matrix const& A) -{ - return A.distribution().rowDistribution(); -} - -void SimpleCSRInternalLinearAlgebra::allocate(ResourceType const& resource, Vector& v) -{ - v.init(resource, true); -} - -void SimpleCSRInternalLinearAlgebra::free(Vector& v) -{ - v.clear(); -} - -Real SimpleCSRInternalLinearAlgebra::norm0(const CSRVector& vx ALIEN_UNUSED_PARAM) const -{ - // return CBLASMPIKernel::nrm0(x.space().structInfo(),vx); - throw NotImplementedException( - A_FUNCINFO, "SimpleCSRLinearAlgebra::norm0 not implemented"); -} - -/*---------------------------------------------------------------------------*/ - -Real SimpleCSRInternalLinearAlgebra::norm1(const CSRVector& vx ALIEN_UNUSED_PARAM) const -{ - // return CBLASMPIKernel::nrm1(x.space().structInfo(),vx); - throw NotImplementedException( - A_FUNCINFO, "SimpleCSRLinearAlgebra::norm1 not implemented"); -} - -/*---------------------------------------------------------------------------*/ - -Real SimpleCSRInternalLinearAlgebra::norm2(const CSRVector& vx) const -{ -#ifdef ALIEN_USE_PERF_TIMER - SentryType s(m_timer, "CSR-NORM2"); -#endif - return CBLASMPIKernel::nrm2(vx.distribution(), vx); -} - -/*---------------------------------------------------------------------------*/ - -void SimpleCSRInternalLinearAlgebra::mult(const CSRMatrix& ma, - const CSRVector& vx, - CSRVector& vr) const -{ -#ifdef ALIEN_USE_PERF_TIMER - SentryType s(m_timer, "CSR-SPMV"); -#endif - Internal::SimpleCSRMatrixMultT(ma).mult(vx, vr); -} - -void SimpleCSRInternalLinearAlgebra::addLMult(Real alpha, - const CSRMatrix& ma, - const CSRVector& vx, - CSRVector& vr) const -{ -#ifdef ALIEN_USE_PERF_TIMER - SentryType s(m_timer, "CSR-AddLMult"); -#endif - Internal::SimpleCSRMatrixMultT(ma).addLMult(alpha, vx, vr); -} - -void SimpleCSRInternalLinearAlgebra::addUMult(Real alpha, - const CSRMatrix& ma, - const CSRVector& vx, - CSRVector& vr) const -{ -#ifdef ALIEN_USE_PERF_TIMER - SentryType s(m_timer, "CSR-AddUMult"); -#endif - Internal::SimpleCSRMatrixMultT(ma).addUMult(alpha, vx, vr); -} - -void SimpleCSRInternalLinearAlgebra::multInvDiag(const CSRMatrix& ma, CSRVector& vr) const -{ -#ifdef ALIEN_USE_PERF_TIMER - SentryType s(m_timer, "CSR-MULTINVDIAG"); -#endif - Internal::SimpleCSRMatrixMultT(ma).multInvDiag(vr); -} - -void SimpleCSRInternalLinearAlgebra::computeInvDiag(const CSRMatrix& ma, CSRVector& vr) const -{ -#ifdef ALIEN_USE_PERF_TIMER - SentryType s(m_timer, "CSR-INVDIAG"); -#endif - Internal::SimpleCSRMatrixMultT(ma).computeInvDiag(vr); -} - -/*---------------------------------------------------------------------------*/ - -void SimpleCSRInternalLinearAlgebra::axpy(Real alpha, const CSRVector& vx, CSRVector& vr) const -{ -#ifdef ALIEN_USE_PERF_TIMER - SentryType s(m_timer, "CSR-AXPY"); -#endif - CBLASMPIKernel::axpy(vx.distribution(), alpha, vx, vr); -} - -/*---------------------------------------------------------------------------*/ - -void SimpleCSRInternalLinearAlgebra::aypx(Real alpha ALIEN_UNUSED_PARAM, - CSRVector& y ALIEN_UNUSED_PARAM, - const CSRVector& x ALIEN_UNUSED_PARAM) const -{ - throw NotImplementedException( - A_FUNCINFO, "SimpleCSRLinearAlgebra::aypx not implemented"); -} - -/*---------------------------------------------------------------------------*/ - -void SimpleCSRInternalLinearAlgebra::copy(const CSRVector& vx, CSRVector& vr) const -{ -#ifdef ALIEN_USE_PERF_TIMER - SentryType s(m_timer, "CSR-COPY"); -#endif - CBLASMPIKernel::copy(vx.distribution(), vx, vr); -} - -/*---------------------------------------------------------------------------*/ - -Real SimpleCSRInternalLinearAlgebra::dot(const CSRVector& vx, const CSRVector& vy) const -{ -#ifdef ALIEN_USE_PERF_TIMER - SentryType s(m_timer, "CSR-DOT"); -#endif - return CBLASMPIKernel::dot(vx.distribution(), vx, vy); -} - -void SimpleCSRInternalLinearAlgebra::dot(const CSRVector& vx, - const CSRVector& vy, - SimpleCSRInternalLinearAlgebra::FutureType& res) const -{ -#ifdef ALIEN_USE_PERF_TIMER - SentryType s(m_timer, "CSR-DOT-F"); -#endif - res() = CBLASMPIKernel::dot(vx.distribution(), vx, vy); -} - -/*---------------------------------------------------------------------------*/ - -void SimpleCSRInternalLinearAlgebra::scal(Real alpha, CSRVector& vx) const -{ -#ifdef ALIEN_USE_PERF_TIMER - SentryType s(m_timer, "CSR-SCAL"); -#endif - CBLASMPIKernel::scal(vx.distribution(), alpha, vx); -} - -void SimpleCSRInternalLinearAlgebra::diagonal( -const CSRMatrix& a ALIEN_UNUSED_PARAM, CSRVector& x ALIEN_UNUSED_PARAM) const -{ - throw NotImplementedException( - A_FUNCINFO, "SimpleCSRLinearAlgebra::aypx not implemented"); -} - -void SimpleCSRInternalLinearAlgebra::reciprocal(CSRVector& x ALIEN_UNUSED_PARAM) const -{ - throw NotImplementedException( - A_FUNCINFO, "SimpleCSRLinearAlgebra::aypx not implemented"); -} - -void SimpleCSRInternalLinearAlgebra::pointwiseMult(const CSRVector& vx, - const CSRVector& vy, - CSRVector& vz) const -{ -#ifdef ALIEN_USE_PERF_TIMER - SentryType s(m_timer, "CSR-XYZ"); -#endif - CBLASMPIKernel::pointwiseMult(vx.distribution(), vx, vy, vz); -} - -void SimpleCSRInternalLinearAlgebra::assign(CSRVector& vx, Real alpha) const -{ -#ifdef ALIEN_USE_PERF_TIMER - SentryType s(m_timer, "CSR-ASSIGN"); -#endif - CBLASMPIKernel::assign(vx.distribution(), alpha, vx); -} - -SimpleCSRInternalLinearAlgebraExpr::SimpleCSRInternalLinearAlgebraExpr() -: IInternalLinearAlgebraExpr() -{} - -/*---------------------------------------------------------------------------*/ - -SimpleCSRInternalLinearAlgebraExpr::~SimpleCSRInternalLinearAlgebraExpr() {} - -/*---------------------------------------------------------------------------*/ -/*---------------------------------------------------------------------------*/ - -Real SimpleCSRInternalLinearAlgebraExpr::norm0(const CSRVector& vx ALIEN_UNUSED_PARAM) const -{ - // return CBLASMPIKernel::nrm0(x.space().structInfo(),vx); - throw NotImplementedException( - A_FUNCINFO, "SimpleCSRLinearAlgebra::norm0 not implemented"); -} - -/*---------------------------------------------------------------------------*/ - -Real SimpleCSRInternalLinearAlgebraExpr::norm1(const CSRVector& vx ALIEN_UNUSED_PARAM) const -{ - // return CBLASMPIKernel::nrm1(x.space().structInfo(),vx); - throw NotImplementedException( - A_FUNCINFO, "SimpleCSRLinearAlgebra::norm1 not implemented"); -} - -/*---------------------------------------------------------------------------*/ - -Real SimpleCSRInternalLinearAlgebraExpr::norm2(const CSRVector& vx) const -{ - return CBLASMPIKernel::nrm2(vx.distribution(), vx); -} - -/*---------------------------------------------------------------------------*/ - -void SimpleCSRInternalLinearAlgebraExpr::mult( -const CSRMatrix& ma, const CSRVector& vx, CSRVector& vr) const -{ - Internal::SimpleCSRMatrixMultT(ma).mult(vx, vr); -} - -void SimpleCSRInternalLinearAlgebraExpr::mult( -const CSRMatrix& ma, const UniqueArray& vx, UniqueArray& vr) const -{ - Internal::SimpleCSRMatrixMultT(ma).mult(vx, vr); -} - -/*---------------------------------------------------------------------------*/ - -void SimpleCSRInternalLinearAlgebraExpr::axpy(Real alpha, const UniqueArray& vx, UniqueArray& vr) const -{ - cblas::axpy(vx.size(), alpha, dataPtr(vx), 1, dataPtr(vr), 1); -} - -void SimpleCSRInternalLinearAlgebraExpr::axpy(Real alpha, const CSRVector& vx, CSRVector& vr) const -{ - CBLASMPIKernel::axpy(vx.distribution(), alpha, vx, vr); -} - -/*---------------------------------------------------------------------------*/ - -void SimpleCSRInternalLinearAlgebraExpr::aypx(Real alpha, UniqueArray& vy, UniqueArray const& vx) const -{ - throw NotImplementedException( - A_FUNCINFO, "SimpleCSRLinearAlgebra::aypx not implemented"); -} - -void SimpleCSRInternalLinearAlgebraExpr::aypx(Real alpha ALIEN_UNUSED_PARAM, - CSRVector& y ALIEN_UNUSED_PARAM, const CSRVector& x ALIEN_UNUSED_PARAM) const -{ - throw NotImplementedException( - A_FUNCINFO, "SimpleCSRLinearAlgebra::aypx not implemented"); -} - -/*---------------------------------------------------------------------------*/ - -void SimpleCSRInternalLinearAlgebraExpr::copy( -const UniqueArray& vx, UniqueArray& vr) const -{ - cblas::copy(vx.size(), dataPtr(vx), 1, dataPtr(vr), 1); -} - -void SimpleCSRInternalLinearAlgebraExpr::copy(const CSRVector& vx, CSRVector& vr) const -{ - CBLASMPIKernel::copy(vx.distribution(), vx, vr); -} - -/*---------------------------------------------------------------------------*/ - -Real SimpleCSRInternalLinearAlgebraExpr::dot( -Integer local_size, const UniqueArray& vx, const UniqueArray& vy) const -{ - return cblas::dot(local_size, dataPtr(vx), 1, dataPtr(vy), 1); -} - -Real SimpleCSRInternalLinearAlgebraExpr::dot(const CSRVector& vx, const CSRVector& vy) const -{ - return CBLASMPIKernel::dot(vx.distribution(), vx, vy); -} - -/*---------------------------------------------------------------------------*/ -void SimpleCSRInternalLinearAlgebraExpr::scal(Real alpha ALIEN_UNUSED_PARAM, UniqueArray& x ALIEN_UNUSED_PARAM) const -{ - throw NotImplementedException( - A_FUNCINFO, "SimpleCSRLinearAlgebra::scal not implemented"); -} - -void SimpleCSRInternalLinearAlgebraExpr::scal(Real alpha ALIEN_UNUSED_PARAM, CSRVector& x ALIEN_UNUSED_PARAM) const -{ - throw NotImplementedException( - A_FUNCINFO, "SimpleCSRLinearAlgebra::aypx not implemented"); -} - -void SimpleCSRInternalLinearAlgebraExpr::diagonal( -const CSRMatrix& a ALIEN_UNUSED_PARAM, CSRVector& x ALIEN_UNUSED_PARAM) const -{ - throw NotImplementedException( - A_FUNCINFO, "SimpleCSRLinearAlgebra::aypx not implemented"); -} - -void SimpleCSRInternalLinearAlgebraExpr::reciprocal(CSRVector& x ALIEN_UNUSED_PARAM) const -{ - throw NotImplementedException( - A_FUNCINFO, "SimpleCSRLinearAlgebra::aypx not implemented"); -} - -void SimpleCSRInternalLinearAlgebraExpr::pointwiseMult(const CSRVector& x ALIEN_UNUSED_PARAM, - const CSRVector& y ALIEN_UNUSED_PARAM, CSRVector& w ALIEN_UNUSED_PARAM) const -{ - throw NotImplementedException( - A_FUNCINFO, "SimpleCSRLinearAlgebra::aypx not implemented"); -} - -} // namespace Alien - -/*---------------------------------------------------------------------------*/ -/*---------------------------------------------------------------------------*/ diff --git a/src/core/alien/kernels/simple_csr/algebra/SimpleCSRInternalLinearAlgebra.h b/src/core/alien/kernels/simple_csr/algebra/SimpleCSRInternalLinearAlgebra.h deleted file mode 100644 index a82adc8d4..000000000 --- a/src/core/alien/kernels/simple_csr/algebra/SimpleCSRInternalLinearAlgebra.h +++ /dev/null @@ -1,214 +0,0 @@ -/* - * Copyright 2020 IFPEN-CEA - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * SPDX-License-Identifier: Apache-2.0 - */ - -#pragma once - -#include - -#include -#include -#include -#include -#include - -#include -#include - -#include - -#include -/*---------------------------------------------------------------------------*/ - -namespace Alien -{ - -typedef AlgebraTraits::matrix_type CSRMatrix; -typedef AlgebraTraits::vector_type CSRVector; - -template <> -struct LUSendRecvTraits -{ - // clang-format off - typedef CSRMatrix matrix_type ; - typedef CSRVector vector_type ; - typedef AlgebraTraits::value_type value_type ; - typedef SimpleCSRInternal::LUSendRecvOp matrix_op_type ; - typedef SimpleCSRInternal::SendRecvOp vector_op_type ; - // clang-format on -}; - -class ALIEN_EXPORT SimpleCSRInternalLinearAlgebra -: public IInternalLinearAlgebra -{ - public: - typedef BackEnd::tag::simplecsr BackEndType; - - typedef VectorDistribution ResourceType; - - class NullValueException - : public Exception::NumericException - { - public: - typedef Exception::NumericException BaseType; - NullValueException(std::string const& type) - : BaseType(type, __LINE__) - {} - }; - - template - class Future - { - public: - Future(T& value) - : m_value(value) - {} - - T& operator()() - { - return m_value; - } - - T operator()() const - { - return m_value; - } - - T get() - { - return m_value; - } - - private: - T& m_value; - }; - - typedef Future FutureType; - - typedef Alien::StdTimer TimerType; - typedef TimerType::Sentry SentryType; - - SimpleCSRInternalLinearAlgebra(); - virtual ~SimpleCSRInternalLinearAlgebra(); - - public: - // IInternalLinearAlgebra interface. - Real norm0(const Vector& x) const; - Real norm1(const Vector& x) const; - Real norm2(const Vector& x) const; - - void mult(const Matrix& A, const Vector& x, Vector& r) const; - void addLMult(Real alpha, const Matrix& A, const Vector& x, Vector& y) const; - void addUMult(Real alpha, const Matrix& A, const Vector& x, Vector& y) const; - - void multInvDiag(const Matrix& A, Vector& y) const; - void computeInvDiag(const Matrix& a, Vector& inv_diag) const; - - void axpy(Real alpha, const Vector& x, Vector& r) const; - void aypx(Real alpha, Vector& y, const Vector& x) const; - void copy(const Vector& x, Vector& r) const; - - Real dot(const Vector& x, const Vector& y) const; - void dot(const Vector& x, const Vector& y, FutureType& res) const; - - void scal(Real alpha, Vector& x) const; - void diagonal(const Matrix& a, Vector& x) const; - void reciprocal(Vector& x) const; - void pointwiseMult(const Vector& x, const Vector& y, Vector& w) const; - void assign(Vector& x, Real alpha) const; - - template - void assign(Vector& x, LambdaT const& lambda) const - { - auto x_ptr = x.getDataPtr(); - for (Integer i = 0; i < x.getAllocSize(); ++i) { - x_ptr[i] = lambda(i); - } - } - - template - void exec(PrecondT& precond, Vector const& x, Vector& y) - { - return precond.solve(*this, x, y); - } - - static ResourceType const& resource(Matrix const& A); - - void allocate(ResourceType const& resource, Vector& v); - - template - void allocate(ResourceType const& resource, T0& v0, T&... args) - { - allocate(resource, v0); - allocate(resource, args...); - } - - void free(Vector& v); - - template - void free(T0& v0, T&... args) - { - free(v0); - free(args...); - } - -#ifdef ALIEN_USE_PERF_TIMER - private: - mutable TimerType m_timer; -#endif -}; - -class SimpleCSRInternalLinearAlgebraExpr -: public IInternalLinearAlgebraExpr -{ - public: - SimpleCSRInternalLinearAlgebraExpr(); - virtual ~SimpleCSRInternalLinearAlgebraExpr(); - - public: - // IInternalLinearAlgebra interface. - Real norm0(const Vector& x) const; - Real norm1(const Vector& x) const; - Real norm2(const Vector& x) const; - void mult(const Matrix& a, const Vector& x, Vector& r) const; - void axpy(Real alpha, const Vector& x, Vector& r) const; - void aypx(Real alpha, Vector& y, const Vector& x) const; - void copy(const Vector& x, Vector& r) const; - Real dot(const Vector& x, const Vector& y) const; - void scal(Real alpha, Vector& x) const; - void diagonal(const Matrix& a, Vector& x) const; - void reciprocal(Vector& x) const; - void pointwiseMult(const Vector& x, const Vector& y, Vector& w) const; - - // IInternalLinearAlgebra interface. - - void mult(const Matrix& a, const UniqueArray& x, UniqueArray& r) const; - void axpy(Real alpha, UniqueArray const& x, UniqueArray& r) const; - void aypx(Real alpha, UniqueArray& y, UniqueArray const& x) const; - void copy(const UniqueArray& x, UniqueArray& r) const; - Real dot( - Integer local_size, const UniqueArray& x, const UniqueArray& y) const; - - void scal(Real alpha, UniqueArray& x) const; - - private: - // No member. -}; - -} // namespace Alien - -/*---------------------------------------------------------------------------*/ diff --git a/src/core/alien/kernels/simple_csr/algebra/SimpleCSRLinearAlgebra.h b/src/core/alien/kernels/simple_csr/algebra/SimpleCSRLinearAlgebra.h deleted file mode 100644 index d6829963c..000000000 --- a/src/core/alien/kernels/simple_csr/algebra/SimpleCSRLinearAlgebra.h +++ /dev/null @@ -1,34 +0,0 @@ -/* - * Copyright 2020 IFPEN-CEA - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * SPDX-License-Identifier: Apache-2.0 - */ - -#pragma once - -#include - -#include -#include - -#include - -namespace Alien -{ - -typedef LinearAlgebra SimpleCSRLinearAlgebra; -typedef LinearAlgebraExpr SimpleCSRLinearAlgebraExpr; - -} // namespace Alien diff --git a/src/core/alien/kernels/simple_csr/algebra/SimpleCSRMatrixMult.h b/src/core/alien/kernels/simple_csr/algebra/SimpleCSRMatrixMult.h deleted file mode 100644 index a50ea17b4..000000000 --- a/src/core/alien/kernels/simple_csr/algebra/SimpleCSRMatrixMult.h +++ /dev/null @@ -1,88 +0,0 @@ -/* - * Copyright 2020 IFPEN-CEA - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * SPDX-License-Identifier: Apache-2.0 - */ - -/* - - - -*/ - -#pragma once - -#include -#include -#include - -namespace Alien::SimpleCSRInternal -{ - -/*@! Classe amie de SimpleCSRMatrix pour externaliser plus rapidement (mais moins - * proprement) - * le produit matrice vecteur */ -template -class SimpleCSRMatrixMultT -{ - public: - //! Template parameter - typedef ValueT ValueType; - typedef SimpleCSRMatrix MatrixType; - typedef SimpleCSRVector VectorType; - - public: - //! Constructeur de la classe - SimpleCSRMatrixMultT(const MatrixType& matrix); - - //! Destructeur de la classe - virtual ~SimpleCSRMatrixMultT() {} - - public: - //! Matrix vector product - void mult(const VectorType& x, VectorType& y) const; - void mult(const UniqueArray& x, UniqueArray& y) const; - - void addLMult(Real alpha, const VectorType& x, VectorType& y) const; - void addUMult(Real alpha, const VectorType& x, VectorType& y) const; - - void computeInvDiag(VectorType& y) const; - void multInvDiag(VectorType& y) const; - - private: - void _parallelMult(const VectorType& x, VectorType& y) const; - void _parallelMult(const UniqueArray& x, UniqueArray& y) const; - - void _seqMult(const VectorType& x, VectorType& y) const; - void _seqMult(const UniqueArray& x, UniqueArray& y) const; - - void _seqAddLMult(Real alpha, const VectorType& x, VectorType& y) const; - void _seqAddUMult(Real alpha, const VectorType& x, VectorType& y) const; - - void _parallelMultBlock(const VectorType& x, VectorType& y) const; - - void _seqMultBlock(const VectorType& x, VectorType& y) const; - - void _parallelMultVariableBlock(const VectorType& x, VectorType& y) const; - - void _seqMultVariableBlock(const VectorType& x, VectorType& y) const; - - private: - const MatrixType& m_matrix_impl; -}; - -} // namespace Alien::SimpleCSRInternal - -#include "SimpleCSRMatrixMultT.h" diff --git a/src/core/alien/kernels/simple_csr/algebra/SimpleCSRMatrixMultT.h b/src/core/alien/kernels/simple_csr/algebra/SimpleCSRMatrixMultT.h deleted file mode 100644 index cafa9c527..000000000 --- a/src/core/alien/kernels/simple_csr/algebra/SimpleCSRMatrixMultT.h +++ /dev/null @@ -1,515 +0,0 @@ -/* - * Copyright 2020 IFPEN-CEA - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * SPDX-License-Identifier: Apache-2.0 - */ - -#pragma once - -#include - -#include - -/*---------------------------------------------------------------------------*/ - -namespace Alien::SimpleCSRInternal -{ - -/*---------------------------------------------------------------------------*/ - -template -SimpleCSRMatrixMultT::SimpleCSRMatrixMultT(const MatrixType& matrix) -: m_matrix_impl(matrix) -{} - -/*---------------------------------------------------------------------------*/ - -template -void SimpleCSRMatrixMultT::mult(const VectorType& x, VectorType& y) const -{ - if (m_matrix_impl.block()) { - if (m_matrix_impl.m_is_parallel) - _parallelMultBlock(x, y); - else - _seqMultBlock(x, y); - } - else if (m_matrix_impl.vblock()) { - if (m_matrix_impl.m_is_parallel) - _parallelMultVariableBlock(x, y); - else - _seqMultVariableBlock(x, y); - } - else { - if (m_matrix_impl.m_is_parallel) - _parallelMult(x, y); - else - _seqMult(x, y); - } -} - -template -void SimpleCSRMatrixMultT::addLMult(Real alpha, const VectorType& x, VectorType& y) const -{ - _seqAddLMult(alpha, x, y); -} - -template -void SimpleCSRMatrixMultT::addUMult(Real alpha, const VectorType& x, VectorType& y) const -{ - _seqAddUMult(alpha, x, y); -} - -template -void SimpleCSRMatrixMultT::mult(const UniqueArray& x, UniqueArray& y) const -{ - if (m_matrix_impl.m_is_parallel) - _parallelMult(x, y); - else - _seqMult(x, y); -} - -/*---------------------------------------------------------------------------*/ - -template -void SimpleCSRMatrixMultT::_parallelMult( -const VectorType& x_impl, VectorType& y_impl) const -{ - Integer alloc_size = m_matrix_impl.m_local_size + m_matrix_impl.m_ghost_size; - x_impl.resize(alloc_size); - Real* y_ptr = y_impl.getDataPtr(); - Real* x_ptr = (Real*)x_impl.getDataPtr(); - ConstArrayView matrix = m_matrix_impl.m_matrix.getValues(); - // ConstArrayView cols2 = - // m_matrix_impl.m_matrix.getCSRProfile().getCols(); - ConstArrayView cols = m_matrix_impl.getDistStructInfo().m_cols; - ConstArrayView row_offset = - m_matrix_impl.m_matrix.getCSRProfile().getRowOffset(); - SendRecvOp op(x_ptr, m_matrix_impl.m_matrix_dist_info.m_send_info, - m_matrix_impl.m_send_policy, x_ptr, m_matrix_impl.m_matrix_dist_info.m_recv_info, - m_matrix_impl.m_recv_policy, m_matrix_impl.m_parallel_mng, m_matrix_impl.m_trace); - op.start(); - ConstArrayView local_row_size = - m_matrix_impl.m_matrix_dist_info.m_local_row_size; - for (Integer irow = 0; irow < m_matrix_impl.m_local_size; ++irow) { - Integer off = row_offset[irow]; - Integer off2 = off + local_row_size[irow]; - Real tmpy = 0.; - for (Integer j = off; j < off2; ++j) { - tmpy += matrix[j] * x_ptr[cols[j]]; - } - y_ptr[irow] = tmpy; - } - op.end(); - - Integer interface_nrow = m_matrix_impl.m_matrix_dist_info.m_interface_nrow; - ConstArrayView row_ids = m_matrix_impl.m_matrix_dist_info.m_interface_rows; - for (Integer i = 0; i < interface_nrow; ++i) { - Integer irow = row_ids[i]; - Integer off = row_offset[irow] + local_row_size[irow]; - Integer off2 = row_offset[irow + 1]; - Real tmpy = 0.; - for (Integer j = off; j < off2; ++j) { - tmpy += matrix[j] * x_ptr[cols[j]]; - } - y_ptr[irow] += tmpy; - } -} - -template -void SimpleCSRMatrixMultT::_parallelMult( -const UniqueArray& x_impl, UniqueArray& y_impl) const -{ - Real* y_ptr = dataPtr(y_impl); - Real* x_ptr = (Real*)dataPtr(x_impl); - ConstArrayView matrix = m_matrix_impl.m_matrix.getValues(); - ConstArrayView cols = m_matrix_impl.getDistStructInfo().m_cols; - ConstArrayView row_offset = - m_matrix_impl.m_matrix.getCSRProfile().getRowOffset(); - SendRecvOp op(x_ptr, m_matrix_impl.m_matrix_dist_info.m_send_info, - m_matrix_impl.m_send_policy, x_ptr, m_matrix_impl.m_matrix_dist_info.m_recv_info, - m_matrix_impl.m_recv_policy, m_matrix_impl.m_parallel_mng, m_matrix_impl.m_trace); - op.start(); - ConstArrayView local_row_size = - m_matrix_impl.m_matrix_dist_info.m_local_row_size; - for (Integer irow = 0; irow < m_matrix_impl.m_local_size; ++irow) { - Integer off = row_offset[irow]; - Integer off2 = off + local_row_size[irow]; - Real tmpy = 0.; - for (Integer j = off; j < off2; ++j) { - tmpy += matrix[j] * x_ptr[cols[j]]; - } - y_ptr[irow] = tmpy; - } - op.end(); - - Integer interface_nrow = m_matrix_impl.m_matrix_dist_info.m_interface_nrow; - ConstArrayView row_ids = m_matrix_impl.m_matrix_dist_info.m_interface_rows; - for (Integer i = 0; i < interface_nrow; ++i) { - Integer irow = row_ids[i]; - Integer off = row_offset[irow] + local_row_size[irow]; - Integer off2 = row_offset[irow + 1]; - Real tmpy = 0.; - for (Integer j = off; j < off2; ++j) { - tmpy += matrix[j] * x_ptr[cols[j]]; - } - y_ptr[irow] += tmpy; - } -} -/*---------------------------------------------------------------------------*/ - -template -void SimpleCSRMatrixMultT::_seqMult(const VectorType& x_impl, VectorType& y_impl) const -{ -#ifdef ALIEN_USE_PERF_TIMER - typename MatrixType::SentryType sentry(m_matrix_impl.timer(), "CSR-SPMV"); -#endif - Real* y_ptr = y_impl.getDataPtr(); - Real* x_ptr = (Real*)x_impl.getDataPtr(); - ConstArrayView matrix = m_matrix_impl.m_matrix.getValues(); - ConstArrayView cols = m_matrix_impl.m_matrix.getCSRProfile().getCols(); - ConstArrayView row_offset = - m_matrix_impl.m_matrix.getCSRProfile().getRowOffset(); - for (Integer irow = 0; irow < m_matrix_impl.m_local_size; ++irow) { - Real tmpy = 0.; - for (Integer j = row_offset[irow]; j < row_offset[irow + 1]; ++j) { - tmpy += matrix[j] * x_ptr[cols[j]]; - } - y_ptr[irow] = tmpy; - } -} - -template -void SimpleCSRMatrixMultT::_seqAddLMult(Real alpha, const VectorType& x_impl, VectorType& y_impl) const -{ -#ifdef ALIEN_USE_PERF_TIMER - typename MatrixType::SentryType sentry(m_matrix_impl.timer(), "CSR-AddLMult"); -#endif - Real* y_ptr = y_impl.getDataPtr(); - Real* x_ptr = (Real*)x_impl.getDataPtr(); - ConstArrayView matrix = m_matrix_impl.m_matrix.getValues(); - ConstArrayView cols = m_matrix_impl.m_matrix.getCSRProfile().getCols(); - ConstArrayView row_offset = m_matrix_impl.m_matrix.getCSRProfile().getRowOffset(); - auto diag_offset = m_matrix_impl.m_matrix.getCSRProfile().getUpperDiagOffset(); - for (Integer irow = 0; irow < m_matrix_impl.m_local_size; ++irow) { - Real tmpy = y_ptr[irow]; - for (Integer j = row_offset[irow]; j < diag_offset[irow]; ++j) { - tmpy += alpha * matrix[j] * x_ptr[cols[j]]; - } - y_ptr[irow] = tmpy; - } -} - -template -void SimpleCSRMatrixMultT::_seqAddUMult(Real alpha, const VectorType& x_impl, VectorType& y_impl) const -{ -#ifdef ALIEN_USE_PERF_TIMER - typename MatrixType::SentryType sentry(m_matrix_impl.timer(), "CSR-AddUMult"); -#endif - Real* y_ptr = y_impl.getDataPtr(); - Real* x_ptr = (Real*)x_impl.getDataPtr(); - ConstArrayView matrix = m_matrix_impl.m_matrix.getValues(); - ConstArrayView cols = m_matrix_impl.m_matrix.getCSRProfile().getCols(); - ConstArrayView row_offset = m_matrix_impl.m_matrix.getCSRProfile().getRowOffset(); - auto diag_offset = m_matrix_impl.m_matrix.getCSRProfile().getUpperDiagOffset(); - for (Integer irow = 0; irow < m_matrix_impl.m_local_size; ++irow) { - Real tmpy = y_ptr[irow]; - for (Integer j = diag_offset[irow] + 1; j < row_offset[irow + 1]; ++j) { - tmpy += alpha * matrix[j] * x_ptr[cols[j]]; - } - y_ptr[irow] = tmpy; - } -} - -template -void SimpleCSRMatrixMultT::_seqMult( -const UniqueArray& x_impl, UniqueArray& y_impl) const -{ - Real* y_ptr = dataPtr(y_impl); - Real* x_ptr = (Real*)dataPtr(x_impl); - ConstArrayView matrix = m_matrix_impl.m_matrix.getValues(); - ConstArrayView cols = m_matrix_impl.m_matrix.getCSRProfile().getCols(); - ConstArrayView row_offset = - m_matrix_impl.m_matrix.getCSRProfile().getRowOffset(); - for (Integer irow = 0; irow < m_matrix_impl.m_local_size; ++irow) { - Real tmpy = 0.; - for (Integer j = row_offset[irow]; j < row_offset[irow + 1]; ++j) { - tmpy += matrix[j] * x_ptr[cols[j]]; - } - y_ptr[irow] = tmpy; - } -} - -/*---------------------------------------------------------------------------*/ - -template -void SimpleCSRMatrixMultT::_parallelMultBlock(const VectorType& x, VectorType& y) const -{ - Integer alloc_size = m_matrix_impl.m_local_size + m_matrix_impl.m_ghost_size; - const Integer block_size = m_matrix_impl.block()->size(); - x.resize(alloc_size * block_size); - ArrayView _y = y.fullValues(); - ConstArrayView x_ptr = x.fullValues(); - Real const* matrix = m_matrix_impl.m_matrix.getDataPtr(); - ConstArrayView cols = m_matrix_impl.getDistStructInfo().m_cols; - // ConstArrayView cols2 = - // m_matrix_impl.m_matrix.getCSRProfile().getCols(); - ConstArrayView row_offset = - m_matrix_impl.m_matrix.getCSRProfile().getRowOffset(); - SimpleCSRInternal::SendRecvOp op(x.getDataPtr(), - m_matrix_impl.m_matrix_dist_info.m_send_info, m_matrix_impl.m_send_policy, - (Real*)x.getDataPtr(), m_matrix_impl.m_matrix_dist_info.m_recv_info, - m_matrix_impl.m_recv_policy, m_matrix_impl.m_parallel_mng, m_matrix_impl.m_trace, - block_size); - op.start(); - ConstArrayView local_row_size = - m_matrix_impl.m_matrix_dist_info.m_local_row_size; - for (Integer irow = 0; irow < m_matrix_impl.m_local_size; ++irow) { - ArrayView y_ptr = _y.subView(irow * block_size, block_size); - Integer off = row_offset[irow]; - Integer off2 = off + local_row_size[irow]; - Real const* m = matrix + off * block_size * block_size; - for (Integer ieq = 0; ieq < block_size; ++ieq) - y_ptr[ieq] = 0.; - for (Integer jcol = off; jcol < off2; ++jcol) { - ConstArrayView ptr = x_ptr.subView(cols[jcol] * block_size, block_size); - for (Integer ieq = 0; ieq < block_size; ++ieq) - for (Integer iu = 0; iu < block_size; ++iu) - y_ptr[ieq] += m[ieq + block_size * iu] * ptr[iu]; - m += block_size * block_size; - } - // y_ptr += block_size; - } - op.end(); - - Integer interface_nrow = m_matrix_impl.m_matrix_dist_info.m_interface_nrow; - ConstArrayView row_ids = m_matrix_impl.m_matrix_dist_info.m_interface_rows; - ArrayView y_ptr = _y; - for (Integer i = 0; i < interface_nrow; ++i) { - Integer irow = row_ids[i]; - ArrayView yptr = y_ptr.subView(irow * block_size, block_size); - Integer off = row_offset[irow] + local_row_size[irow]; - Integer off2 = row_offset[irow + 1]; - Real const* m = matrix + off * block_size * block_size; - for (Integer jcol = off; jcol < off2; ++jcol) { - ConstArrayView ptr = x_ptr.subView(cols[jcol] * block_size, block_size); - for (Integer ieq = 0; ieq < block_size; ++ieq) - for (Integer iu = 0; iu < block_size; ++iu) - yptr[ieq] += m[ieq + block_size * iu] * ptr[iu]; - m += block_size * block_size; - } - } -} - -/*---------------------------------------------------------------------------*/ - -template -void SimpleCSRMatrixMultT::_seqMultBlock(const VectorType& x, VectorType& y) const -{ - Real* y_ptr = y.getDataPtr(); - Real const* x_ptr = x.getDataPtr(); - Real const* matrix = m_matrix_impl.m_matrix.getDataPtr(); - const Integer block_size = m_matrix_impl.block()->size(); - ConstArrayView cols = m_matrix_impl.m_matrix.getCSRProfile().getCols(); - ConstArrayView row_offset = - m_matrix_impl.m_matrix.getCSRProfile().getRowOffset(); - for (Integer irow = 0; irow < m_matrix_impl.m_local_size; ++irow) { - Integer off = row_offset[irow]; - Integer off2 = row_offset[irow + 1]; - Real const* m = matrix + off * block_size * block_size; - for (Integer ieq = 0; ieq < block_size; ++ieq) - y_ptr[ieq] = 0.; - for (Integer jcol = off; jcol < off2; ++jcol) { - Real const* ptr = x_ptr + cols[jcol] * block_size; - for (Integer ieq = 0; ieq < block_size; ++ieq) - for (Integer iu = 0; iu < block_size; ++iu) - y_ptr[ieq] += m[ieq + block_size * iu] * ptr[iu]; - m += block_size * block_size; - } - y_ptr += block_size; - } -} - -/*---------------------------------------------------------------------------*/ - -template -void SimpleCSRMatrixMultT::_parallelMultVariableBlock( -const VectorType& x_impl, VectorType& y_impl) const -{ - // alien_info([&] { cout()<<"_parallelMultVariableBlock";}) ; - - ArrayView y = y_impl.fullValues(); - - ConstArrayView block_sizes = m_matrix_impl.getDistStructInfo().m_block_sizes; - ConstArrayView block_offsets = - m_matrix_impl.getDistStructInfo().m_block_offsets; - { - const Integer last = block_offsets.size() - 1; - x_impl.resize(block_offsets[last] + block_sizes[last]); - } - - const ValueT* x_ptr = x_impl.getDataPtr(); - const ValueT* matrix_ptr = m_matrix_impl.m_matrix.getDataPtr(); - - ConstArrayView row_offset = - m_matrix_impl.m_matrix.getCSRProfile().getRowOffset(); - ConstArrayView cols = m_matrix_impl.getDistStructInfo().m_cols; - ConstArrayView block_cols = - m_matrix_impl.m_matrix.getCSRProfile().getBlockCols(); - - SendRecvOp op(x_ptr, m_matrix_impl.m_matrix_dist_info.m_send_info, - m_matrix_impl.m_send_policy, (ValueT*)x_ptr, - m_matrix_impl.m_matrix_dist_info.m_recv_info, m_matrix_impl.m_recv_policy, - m_matrix_impl.m_parallel_mng, m_matrix_impl.m_trace, block_sizes, block_offsets); - - op.start(); - - UniqueArray tmpy; - tmpy.reserve(m_matrix_impl.vblock()->maxBlockSize()); - - ConstArrayView local_row_size = - m_matrix_impl.m_matrix_dist_info.m_local_row_size; - for (Integer irow = 0; irow < m_matrix_impl.m_local_size; ++irow) { - Integer off = row_offset[irow]; - Integer off2 = off + local_row_size[irow]; - const Integer block_size_row = block_sizes[irow]; - tmpy.resize(block_size_row); - tmpy.fill(ValueT()); - for (Integer j = off; j < off2; ++j) { - const Integer col = cols[j]; - const Integer block_size_col = block_sizes[col]; - ConstArrayView x(block_size_col, x_ptr + block_offsets[col]); - ConstArray2View matrix( - matrix_ptr + block_cols[j], block_size_row, block_size_col); - for (Integer krow = 0; krow < block_size_row; ++krow) { - for (Integer kcol = 0; kcol < block_size_col; ++kcol) { - tmpy[krow] += matrix[krow][kcol] * x[kcol]; - } - } - } - y.subView(block_offsets[irow], block_size_row).copy(tmpy); - } - - op.end(); - - Integer interface_nrow = m_matrix_impl.m_matrix_dist_info.m_interface_nrow; - ConstArrayView row_ids = m_matrix_impl.m_matrix_dist_info.m_interface_rows; - for (Integer i = 0; i < interface_nrow; ++i) { - Integer irow = row_ids[i]; - Integer off = row_offset[irow] + local_row_size[irow]; - Integer off2 = row_offset[irow + 1]; - const Integer block_size_row = block_sizes[irow]; - tmpy.resize(block_size_row); - tmpy.fill(ValueT()); - for (Integer j = off; j < off2; ++j) { - const Integer col = cols[j]; - const Integer block_size_col = block_sizes[col]; - ConstArrayView x(block_size_col, x_ptr + block_offsets[col]); - ConstArray2View matrix( - matrix_ptr + block_cols[j], block_size_row, block_size_col); - for (Integer krow = 0; krow < block_size_row; ++krow) { - for (Integer kcol = 0; kcol < block_size_col; ++kcol) { - tmpy[krow] += matrix[krow][kcol] * x[kcol]; - } - } - } - ArrayView y_view = y.subView(block_offsets[irow], block_size_row); - for (Integer k = 0; k < block_size_row; ++k) - y_view[k] += tmpy[k]; - } -} - -/*---------------------------------------------------------------------------*/ - -template -void SimpleCSRMatrixMultT::_seqMultVariableBlock( -const VectorType& x_impl, VectorType& y_impl) const -{ - // alien_info([&] { cout()<<"_seqMultVariableBlock";}) ; - - ArrayView y = y_impl.fullValues(); - - const ValueT* x_ptr = x_impl.getDataPtr(); - const ValueT* matrix_ptr = m_matrix_impl.m_matrix.getDataPtr(); - - ConstArrayView row_offset = - m_matrix_impl.m_matrix.getCSRProfile().getRowOffset(); - ConstArrayView cols = m_matrix_impl.m_matrix.getCSRProfile().getCols(); - ConstArrayView block_cols = - m_matrix_impl.m_matrix.getCSRProfile().getBlockCols(); - const VBlock* block = m_matrix_impl.vblock(); - const VBlockImpl& block_infos = x_impl.vblockImpl(); - - UniqueArray tmpy; - tmpy.reserve(block->maxBlockSize()); - for (Integer irow = 0; irow < m_matrix_impl.m_local_size; - ++irow) // Attention, c'est local !!!! - { - const Integer block_size_row = block->size(irow); - tmpy.resize(block_size_row); - tmpy.fill(ValueT()); - for (Integer j = row_offset[irow]; j < row_offset[irow + 1]; ++j) { - const Integer col = cols[j]; - const Integer block_size_col = block->size(col); - ConstArrayView x(block_size_col, x_ptr + block_infos.offset(col)); - ConstArray2View matrix( - matrix_ptr + block_cols[j], block_size_row, block_size_col); - for (Integer krow = 0; krow < block_size_row; ++krow) { - for (Integer kcol = 0; kcol < block_size_col; ++kcol) { - tmpy[krow] += matrix[krow][kcol] * x[kcol]; - } - } - } - y.subView(block_infos.offset(irow), block_size_row).copy(tmpy); - } -} - -template -void SimpleCSRMatrixMultT::multInvDiag(VectorType& y) const -{ - Real* y_ptr = y.getDataPtr(); - CSRConstViewT view(m_matrix_impl); - // clang-format off - auto nrows = view.nrows() ; - auto kcol = view.kcol() ; - auto dcol = view.dcol() ; - auto cols = view.cols() ; - auto values = view.data() ; - // clang-format on - for (Integer irow = 0; irow < nrows; ++irow) - y_ptr[irow] = y_ptr[irow] / values[dcol[irow]]; -} - -template -void SimpleCSRMatrixMultT::computeInvDiag(VectorType& y) const -{ - Real* y_ptr = y.getDataPtr(); - - CSRConstViewT view(m_matrix_impl); - // clang-format off - auto nrows = view.nrows() ; - auto kcol = view.kcol() ; - auto dcol = view.dcol() ; - auto cols = view.cols() ; - auto values = view.data() ; - // clang-format on - for (Integer irow = 0; irow < nrows; ++irow) - y_ptr[irow] = 1. / values[dcol[irow]]; -} - -/*---------------------------------------------------------------------------*/ - -} // namespace Alien::SimpleCSRInternal - -/*---------------------------------------------------------------------------*/ diff --git a/src/core/alien/kernels/simple_csr/algebra/alien_cblas.h b/src/core/alien/kernels/simple_csr/algebra/alien_cblas.h deleted file mode 100644 index af42379a9..000000000 --- a/src/core/alien/kernels/simple_csr/algebra/alien_cblas.h +++ /dev/null @@ -1,76 +0,0 @@ -#pragma once - -#ifdef __cplusplus -extern "C" { -#endif // __cplusplus - -float cblas_sdot(const int n, const float* x, const int incx, const float* y, - const int incy); -double cblas_ddot(const int n, const double* x, const int incx, const double* y, - const int incy); -void cblas_saxpy(const int n, const float alpha, const float* x, const int incx, - float* y, const int incy); -void cblas_daxpy(const int n, const double alpha, const double* x, - const int incx, double* y, const int incy); -void cblas_scopy(const int n, const float* x, const int incx, float* y, - const int incy); -void cblas_dcopy(const int n, const double* x, const int incx, double* y, - const int incy); -void cblas_sscal(const int n, const float alpha, float* x, const int incx); -void cblas_dscal(const int n, const double alpha, double* x, const int incx); -double cblas_dnrm2(const int n, const double* x, const int incx); - -#ifdef __cplusplus -} -#endif // __cplusplus - -namespace cblas -{ - -float dot(const int n, const float* x, const int incx, const float* y, - const int incy) -{ - return cblas_sdot(n, x, incx, y, incy); -} - -double dot(const int n, const double* x, const int incx, const double* y, const int incy) -{ - return cblas_ddot(n, x, incx, y, incy); -} - -void axpy(const int n, const float alpha, const float* x, const int incx, float* y, const int incy) -{ - return cblas_saxpy(n, alpha, x, incx, y, incy); -} - -void axpy(const int n, const double alpha, const double* x, const int incx, double* y, const int incy) -{ - return cblas_daxpy(n, alpha, x, incx, y, incy); -} - -void copy(const int n, const float* x, const int incx, float* y, const int incy) -{ - return cblas_scopy(n, x, incx, y, incy); -} - -void copy(const int n, const double* x, const int incx, double* y, const int incy) -{ - return cblas_dcopy(n, x, incx, y, incy); -} - -void scal(const int n, const float alpha, float* x, const int incx) -{ - return cblas_sscal(n, alpha, x, incx); -} - -void scal(const int n, const double alpha, double* x, const int incx) -{ - return cblas_dscal(n, alpha, x, incx); -} - -double nrm2(const int n, const double* x, const int incx) -{ - return cblas_dnrm2(n, x, incx); -} - -} // namespace cblas From 73ae488a1cfb9913e2ac8d132caa4e3b52137579 Mon Sep 17 00:00:00 2001 From: Louis Goyard Date: Tue, 21 Jun 2022 16:27:25 +0200 Subject: [PATCH 04/13] WIP: avoid definitions in .h --- src/core/alien/core/impl/MultiMatrixImpl.cc | 74 +++++++++++++++++++++ src/core/alien/core/impl/MultiMatrixImpl.h | 73 -------------------- src/core/alien/core/impl/MultiVectorImpl.cc | 54 +++++++++++++++ src/core/alien/core/impl/MultiVectorImpl.h | 53 --------------- 4 files changed, 128 insertions(+), 126 deletions(-) diff --git a/src/core/alien/core/impl/MultiMatrixImpl.cc b/src/core/alien/core/impl/MultiMatrixImpl.cc index 56062912d..a12e31d73 100644 --- a/src/core/alien/core/impl/MultiMatrixImpl.cc +++ b/src/core/alien/core/impl/MultiMatrixImpl.cc @@ -311,6 +311,80 @@ void MultiMatrixImpl::updateImpl(IMatrixImpl* target) const /*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/ + +const IMatrixImpl& +MultiMatrixImpl::get(BackEndId backEndId) const +{ + // TOCHECK : to be removed or not ? + // ALIEN_ASSERT(!m_row_space.isNull(), ("Null row space matrix access")); + // ALIEN_ASSERT(!m_col_space.isNull(), ("Null col space matrix access")); + // FIXME: type + IMatrixImpl*& impl2 = getImpl(backEndId); + ALIEN_ASSERT( + (impl2->backend() == backEndId), ("Inconsistent backend")); + updateImpl(impl2); + return *impl2; +} + +/*---------------------------------------------------------------------------*/ +/*---------------------------------------------------------------------------*/ + +IMatrixImpl& +MultiMatrixImpl::get(BackEndId backEndId, const bool update_stamp) +{ + // TOCHECK : to be removed or not ? + // ALIEN_ASSERT(!m_row_space.isNull(), ("Null row space matrix access")); + // ALIEN_ASSERT(!m_col_space.isNull(), ("Null col space matrix access")); + // FIXME: type + IMatrixImpl*& impl2 = getImpl(backEndId); + ALIEN_ASSERT( + (impl2->backend() == backEndId), ("Inconsistent backend")); + updateImpl(impl2); + if (update_stamp) + impl2->updateTimestamp(); + return *impl2; +} + +/*---------------------------------------------------------------------------*/ +/*---------------------------------------------------------------------------*/ + +void MultiMatrixImpl::release(BackEndId backEndId) const +{ + auto finder = m_impls2.find(backEndId); + if (finder == m_impls2.end()) + return; // already freed + delete finder->second, finder->second = NULL; +} + +/*---------------------------------------------------------------------------*/ +/*---------------------------------------------------------------------------*/ + +IMatrixImpl*& +MultiMatrixImpl::getImpl(BackEndId backend) const +{ + auto inserter = m_impls2.insert(MultiMatrixImplMap::value_type(backend, NULL)); + IMatrixImpl*& impl2 = inserter.first->second; + if (impl2 == NULL) { + IMatrixImpl* new_impl = new IMatrixImpl(this); // constructeur associé à un multi-impl + // new_impl->init(*m_row_space.get(), + // *m_col_space.get(), + // m_distribution); + impl2 = new_impl; + } + return impl2; +} + +/*---------------------------------------------------------------------------*/ +/*---------------------------------------------------------------------------*/ + +void MultiMatrixImpl::insert(BackEndId backend, IMatrixImpl* m) +{ + if (m_impls2.find(backend) != m_impls2.end()) { + alien_fatal([&] { cout() << "try to insert already inserted value"; }); + } + m_impls2[backend] = m; +} + } // namespace Alien /*---------------------------------------------------------------------------*/ diff --git a/src/core/alien/core/impl/MultiMatrixImpl.h b/src/core/alien/core/impl/MultiMatrixImpl.h index fb1383b8d..23988e0ca 100644 --- a/src/core/alien/core/impl/MultiMatrixImpl.h +++ b/src/core/alien/core/impl/MultiMatrixImpl.h @@ -253,79 +253,6 @@ class ALIEN_EXPORT MultiMatrixImpl : public TimestampMng /*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/ -const IMatrixImpl& -MultiMatrixImpl::get(BackEndId backEndId) const -{ - // TOCHECK : to be removed or not ? - // ALIEN_ASSERT(!m_row_space.isNull(), ("Null row space matrix access")); - // ALIEN_ASSERT(!m_col_space.isNull(), ("Null col space matrix access")); - // FIXME: type - IMatrixImpl*& impl2 = getImpl(backEndId); - ALIEN_ASSERT( - (impl2->backend() == backEndId), ("Inconsistent backend")); - updateImpl(impl2); - return *impl2; -} - -/*---------------------------------------------------------------------------*/ -/*---------------------------------------------------------------------------*/ - -IMatrixImpl& -MultiMatrixImpl::get(BackEndId backEndId, const bool update_stamp) -{ - // TOCHECK : to be removed or not ? - // ALIEN_ASSERT(!m_row_space.isNull(), ("Null row space matrix access")); - // ALIEN_ASSERT(!m_col_space.isNull(), ("Null col space matrix access")); - // FIXME: type - IMatrixImpl*& impl2 = getImpl(backEndId); - ALIEN_ASSERT( - (impl2->backend() == backEndId), ("Inconsistent backend")); - updateImpl(impl2); - if (update_stamp) - impl2->updateTimestamp(); - return *impl2; -} - -/*---------------------------------------------------------------------------*/ -/*---------------------------------------------------------------------------*/ - -void MultiMatrixImpl::release(BackEndId backEndId) const -{ - auto finder = m_impls2.find(backEndId); - if (finder == m_impls2.end()) - return; // already freed - delete finder->second, finder->second = NULL; -} - -/*---------------------------------------------------------------------------*/ -/*---------------------------------------------------------------------------*/ - -IMatrixImpl*& -MultiMatrixImpl::getImpl(BackEndId backend) const -{ - auto inserter = m_impls2.insert(MultiMatrixImplMap::value_type(backend, NULL)); - IMatrixImpl*& impl2 = inserter.first->second; - if (impl2 == NULL) { - IMatrixImpl* new_impl = new IMatrixImpl(this); // constructeur associé à un multi-impl - // new_impl->init(*m_row_space.get(), - // *m_col_space.get(), - // m_distribution); - impl2 = new_impl; - } - return impl2; -} - -/*---------------------------------------------------------------------------*/ -/*---------------------------------------------------------------------------*/ - -void MultiMatrixImpl::insert(BackEndId backend, IMatrixImpl* m) -{ - if (m_impls2.find(backend) != m_impls2.end()) { - alien_fatal([&] { cout() << "try to insert already inserted value"; }); - } - m_impls2[backend] = m; -} - /*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/ diff --git a/src/core/alien/core/impl/MultiVectorImpl.cc b/src/core/alien/core/impl/MultiVectorImpl.cc index 5db321396..ecbff296f 100644 --- a/src/core/alien/core/impl/MultiVectorImpl.cc +++ b/src/core/alien/core/impl/MultiVectorImpl.cc @@ -263,6 +263,60 @@ void MultiVectorImpl::updateImpl(IVectorImpl* target) const /*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/ +const IVectorImpl& +MultiVectorImpl::get(BackEndId backEndId) const +{ + IVectorImpl*& impl2 = getImpl(backEndId); + ALIEN_ASSERT( + (impl2->backend() == backEndId), ("Inconsistent backend")); + updateImpl(impl2); + return *impl2; +} + +/*---------------------------------------------------------------------------*/ +/*---------------------------------------------------------------------------*/ + +IVectorImpl& +MultiVectorImpl::get(BackEndId backEndId, const bool update_stamp) +{ + IVectorImpl*& impl2 = getImpl(backEndId); + ALIEN_ASSERT( + (impl2->backend() == backEndId), ("Inconsistent backend")); + updateImpl(impl2); + if (update_stamp) { + impl2->updateTimestamp(); + } + return *impl2; +} + +/*---------------------------------------------------------------------------*/ +/*---------------------------------------------------------------------------*/ + + +void MultiVectorImpl::release(BackEndId backEndId) const +{ + auto finder = m_impls2.find(backEndId); + if (finder == m_impls2.end()) + return; // already freed + delete finder->second, finder->second = NULL; +} + +/*---------------------------------------------------------------------------*/ +/*---------------------------------------------------------------------------*/ + +IVectorImpl*& +MultiVectorImpl::getImpl(BackEndId backend) const +{ + auto inserter = m_impls2.insert(MultiVectorImplMap::value_type(backend, NULL)); + IVectorImpl*& impl2 = inserter.first->second; + if (impl2 == NULL) { + auto new_impl = new IVectorImpl(this); // constructeur associ� � un multi-impl + new_impl->init(*m_distribution.get(), true); + impl2 = new_impl; + } + return impl2; +} + } // namespace Alien /*---------------------------------------------------------------------------*/ diff --git a/src/core/alien/core/impl/MultiVectorImpl.h b/src/core/alien/core/impl/MultiVectorImpl.h index 29ff02a77..e5cf737fb 100644 --- a/src/core/alien/core/impl/MultiVectorImpl.h +++ b/src/core/alien/core/impl/MultiVectorImpl.h @@ -212,59 +212,6 @@ class ALIEN_EXPORT MultiVectorImpl : public TimestampMng std::shared_ptr m_variable_block; }; -const IVectorImpl& -MultiVectorImpl::get(BackEndId backEndId) const -{ - IVectorImpl*& impl2 = getImpl(backEndId); - ALIEN_ASSERT( - (impl2->backend() == backEndId), ("Inconsistent backend")); - updateImpl(impl2); - return *impl2; -} - -/*---------------------------------------------------------------------------*/ -/*---------------------------------------------------------------------------*/ - -IVectorImpl& -MultiVectorImpl::get(BackEndId backEndId, const bool update_stamp) -{ - IVectorImpl*& impl2 = getImpl(backEndId); - ALIEN_ASSERT( - (impl2->backend() == backEndId), ("Inconsistent backend")); - updateImpl(impl2); - if (update_stamp) { - impl2->updateTimestamp(); - } - return *impl2; -} - -/*---------------------------------------------------------------------------*/ -/*---------------------------------------------------------------------------*/ - - -void MultiVectorImpl::release(BackEndId backEndId) const -{ - auto finder = m_impls2.find(backEndId); - if (finder == m_impls2.end()) - return; // already freed - delete finder->second, finder->second = NULL; -} - -/*---------------------------------------------------------------------------*/ -/*---------------------------------------------------------------------------*/ - -IVectorImpl*& -MultiVectorImpl::getImpl(BackEndId backend) const -{ - auto inserter = m_impls2.insert(MultiVectorImplMap::value_type(backend, NULL)); - IVectorImpl*& impl2 = inserter.first->second; - if (impl2 == NULL) { - auto new_impl = new IVectorImpl(this); // constructeur associ� � un multi-impl - new_impl->init(*m_distribution.get(), true); - impl2 = new_impl; - } - return impl2; -} /*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/ From 6a80ee2227776188e78a2851ad0f589de4b92677 Mon Sep 17 00:00:00 2001 From: Louis Goyard Date: Tue, 21 Jun 2022 16:47:33 +0200 Subject: [PATCH 05/13] WIP: Compile more tests --- src/core/alien/Alien.h | 1 - 1 file changed, 1 deletion(-) diff --git a/src/core/alien/Alien.h b/src/core/alien/Alien.h index 320d035c7..7f6c3f287 100644 --- a/src/core/alien/Alien.h +++ b/src/core/alien/Alien.h @@ -66,7 +66,6 @@ #include "kernels/simple_csr/SimpleCSRMatrix.h" #include "kernels/simple_csr/SimpleCSRVector.h" -#include "kernels/simple_csr/algebra/SimpleCSRLinearAlgebra.h" #include "kernels/composite/CompositeMultiVectorImpl.h" #include "kernels/composite/CompositeSpace.h" From 45ed9759dae46ed8b2eb72bf553c89b8dfbc620d Mon Sep 17 00:00:00 2001 From: Louis Goyard Date: Wed, 22 Jun 2022 14:35:00 +0200 Subject: [PATCH 06/13] WIP: more compile + shared_ptr correction --- benchmark/src/alien/benchmark/LinearBench.cpp | 8 +- .../hypre/examples/hypre_example_solve.cpp | 9 +- plugins/hypre/include/alien/hypre/backend.h | 8 +- .../converters/hypre_to_simplecsr_vector.cpp | 2 +- .../converters/simplecsr_to_hypre_matrix.cpp | 2 +- plugins/hypre/src/hypre_linear_solver.cpp | 9 +- plugins/hypre/src/hypre_linear_solver.h | 6 +- src/core/CMakeLists.txt | 2 +- src/core/alien/core/backend/BackEnd.h | 9 + .../core/backend/IInternalLinearSolverT.h | 7 +- .../{LinearSolverT.h => LinearSolver.cc} | 4 +- src/core/alien/core/backend/LinearSolver.h | 8 +- src/core/alien/kernels/dok/DoKBackEnd.h | 5 +- .../redistributor/RedistributorBackEnd.h | 5 +- src/core/tests/TestDoK.cc | 4 +- .../move/handlers/sub_matrix/Extraction.cc | 2 +- .../alien/move/impl/MatrixMarketReader.cc | 2 +- src/movesemantic/tests/CMakeLists.txt | 9 +- .../tests/TestDoKDirectMatrixBuilder.cc | 1 - src/movesemantic/tests/TestDoKMatrix.cc | 2 +- .../tests/TestMatrixDirectBuilder.cc | 80 --------- .../handlers/stream/StreamMatrixBuilderT.h | 2 +- .../stream/StreamVBlockMatrixBuilderT.h | 2 +- .../import_export/MatrixMarketSystemWriter.cc | 4 +- .../alien/ref/import_export/SystemWriter.cc | 22 +-- src/refsemantic/tests/CMakeLists.txt | 14 +- src/refsemantic/tests/TestCompositeMatrix.cc | 4 +- src/refsemantic/tests/TestCompositeVector.cc | 4 +- src/refsemantic/tests/TestMVExpr.cc | 164 ------------------ .../tests/TestMatrixDirectBuilder.cc | 70 -------- src/refsemantic/tests/TestRedistributor.cc | 8 +- 31 files changed, 87 insertions(+), 391 deletions(-) rename src/core/alien/core/backend/{LinearSolverT.h => LinearSolver.cc} (98%) delete mode 100644 src/movesemantic/tests/TestMatrixDirectBuilder.cc delete mode 100644 src/refsemantic/tests/TestMVExpr.cc delete mode 100644 src/refsemantic/tests/TestMatrixDirectBuilder.cc diff --git a/benchmark/src/alien/benchmark/LinearBench.cpp b/benchmark/src/alien/benchmark/LinearBench.cpp index 81dcdacd2..53eeb8b89 100644 --- a/benchmark/src/alien/benchmark/LinearBench.cpp +++ b/benchmark/src/alien/benchmark/LinearBench.cpp @@ -25,7 +25,7 @@ #include #include -#include +//#include #include namespace Alien::Benchmark @@ -89,7 +89,7 @@ Alien::Move::VectorData LinearBench::_solve(Alien::Move::MatrixData&& linop, Ali LinearBenchResults::LinearBenchResults(const LinearBench& bench, Alien::Move::VectorData&& solution) { - SimpleCSRLinearAlgebra algebra; + //SimpleCSRLinearAlgebra algebra; auto linop = bench.m_lp->matrix(); @@ -97,8 +97,8 @@ LinearBenchResults::LinearBenchResults(const LinearBench& bench, Alien::Move::Ve auto rhs = bench.m_lp->vector(); - algebra.mult(linop, solution, residual); - algebra.axpy(-1., rhs, residual); + //algebra.mult(linop, solution, residual); + //algebra.axpy(-1., rhs, residual); m_solution = computeAnalytics(solution); m_rhs = computeAnalytics(rhs); diff --git a/plugins/hypre/examples/hypre_example_solve.cpp b/plugins/hypre/examples/hypre_example_solve.cpp index 8f3e5ad0b..8f50b3c91 100644 --- a/plugins/hypre/examples/hypre_example_solve.cpp +++ b/plugins/hypre/examples/hypre_example_solve.cpp @@ -21,8 +21,8 @@ #include #include -#include -#include +#include +//#include int test() { @@ -75,9 +75,6 @@ int test() Alien::Vector b(size, pm); - Alien::BackEnd::BackendHandle* backendHandle = Alien::BackEnd::loadBackend("hypre"); - //Alien::ILinearAlgebra algebra = backendHandle->getLinearAlgebra(); - //algebra.mult(A, xe, b); Alien::Vector x(size, pm); @@ -124,7 +121,7 @@ int test() // algebra.axpy(-1., xe, r); //} - tm->info() << " => ||r|| = " << norm; + tm->info() << " => ||r|| = "; // << norm; tm->info() << " "; tm->info() << "... example finished !!!"; diff --git a/plugins/hypre/include/alien/hypre/backend.h b/plugins/hypre/include/alien/hypre/backend.h index 30d6a99e2..41e0f2eaf 100644 --- a/plugins/hypre/include/alien/hypre/backend.h +++ b/plugins/hypre/include/alien/hypre/backend.h @@ -31,9 +31,9 @@ class Options; class ILinearSolver; -extern IInternalLinearSolver* LinearSolverFactory(const BackEnd::Options& options); +extern std::shared_ptr> LinearSolverFactory(const BackEnd::Options& options); -extern IInternalLinearSolver* LinearSolverFactory(); +extern std::shared_ptr> LinearSolverFactory(); } // namespace Alien @@ -53,13 +53,13 @@ namespace BackEnd class Plugin : public BackEnd::IPlugin { // factories to default solver - IInternalLinearSolver* solver_factory() + std::shared_ptr> solver_factory() { return LinearSolverFactory(); } // factories to build solver - IInternalLinearSolver* solver_factory(const Alien::BackEnd::Options& options) + std::shared_ptr> solver_factory(const Alien::BackEnd::Options& options) { return LinearSolverFactory(options); } diff --git a/plugins/hypre/src/converters/hypre_to_simplecsr_vector.cpp b/plugins/hypre/src/converters/hypre_to_simplecsr_vector.cpp index 77503c5ad..cab5fe0ea 100644 --- a/plugins/hypre/src/converters/hypre_to_simplecsr_vector.cpp +++ b/plugins/hypre/src/converters/hypre_to_simplecsr_vector.cpp @@ -23,7 +23,7 @@ #include #include -#include "../../include/alien/hypre/backend.h" +#include class Hypre_to_SimpleCSR_VectorConverter : public Alien::IVectorConverter { diff --git a/plugins/hypre/src/converters/simplecsr_to_hypre_matrix.cpp b/plugins/hypre/src/converters/simplecsr_to_hypre_matrix.cpp index da644b64c..7859d3139 100644 --- a/plugins/hypre/src/converters/simplecsr_to_hypre_matrix.cpp +++ b/plugins/hypre/src/converters/simplecsr_to_hypre_matrix.cpp @@ -25,7 +25,7 @@ #include #include -#include "../../include/alien/hypre/backend.h" +#include class SimpleCSR_to_Hypre_MatrixConverter : public Alien::IMatrixConverter { diff --git a/plugins/hypre/src/hypre_linear_solver.cpp b/plugins/hypre/src/hypre_linear_solver.cpp index 9c6a680ad..d680fab90 100644 --- a/plugins/hypre/src/hypre_linear_solver.cpp +++ b/plugins/hypre/src/hypre_linear_solver.cpp @@ -41,7 +41,6 @@ const Arccore::String& msg, int ierr, int skipError) const } } - // FIXME: check type and conversion bool PluginLinearSolver::solve(const IMatrixImpl& A, const IVectorImpl& b, IVectorImpl& x) { @@ -304,16 +303,16 @@ PluginLinearSolver::getStatus() const } ALIEN_HYPRE_EXPORT -IInternalLinearSolver* +std::shared_ptr> LinearSolverFactory(const BackEnd::Options& options) { - return new PluginLinearSolver(options); + return std::make_shared(options); } ALIEN_HYPRE_EXPORT -IInternalLinearSolver* +std::shared_ptr> LinearSolverFactory() { - return new PluginLinearSolver(); + return std::make_shared(); } } // namespace Alien diff --git a/plugins/hypre/src/hypre_linear_solver.h b/plugins/hypre/src/hypre_linear_solver.h index b2f4ad666..e4b28b021 100644 --- a/plugins/hypre/src/hypre_linear_solver.h +++ b/plugins/hypre/src/hypre_linear_solver.h @@ -18,7 +18,7 @@ #include -#include +#include #include #include @@ -33,7 +33,7 @@ namespace Alien { -class PluginLinearSolver : public IInternalLinearSolver +class PluginLinearSolver : public IInternalLinearSolver , public ObjectWithTrace { public: @@ -68,6 +68,8 @@ class PluginLinearSolver : public IInternalLinearSolver const SolverStat& getSolverStat() const { return m_stat; } + const BackEndId backEndName() const { return "hypre"; } + private: bool _solve(const Matrix& A, const Vector& b, Vector& x); diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt index 2500f29a5..49c5b6c08 100644 --- a/src/core/CMakeLists.txt +++ b/src/core/CMakeLists.txt @@ -41,7 +41,7 @@ add_library(alien_core alien/core/backend/IVectorConverter.h alien/core/backend/ISolverFabric.h alien/core/backend/LinearSolver.h - alien/core/backend/LinearSolverT.h + alien/core/backend/LinearSolver.cc alien/core/backend/MatrixConverterRegisterer.cc alien/core/backend/MatrixConverterRegisterer.h alien/core/backend/VectorConverterRegisterer.cc diff --git a/src/core/alien/core/backend/BackEnd.h b/src/core/alien/core/backend/BackEnd.h index fb7370a13..e74dd80c5 100644 --- a/src/core/alien/core/backend/BackEnd.h +++ b/src/core/alien/core/backend/BackEnd.h @@ -39,6 +39,7 @@ struct AlgebraTraits; template class LUSendRecvTraits; +template class IInternalLinearSolver; class ILinearSolver; @@ -110,12 +111,20 @@ namespace BackEnd return *this; } }; + + class IPlugin + { + IInternalLinearSolver* solver_factory(); + IInternalLinearSolver* solver_factory(const Alien::BackEnd::Options& options); + BackEndId name(); + }; } // namespace BackEnd + /*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/ diff --git a/src/core/alien/core/backend/IInternalLinearSolverT.h b/src/core/alien/core/backend/IInternalLinearSolverT.h index eb64dfbaf..42502336e 100644 --- a/src/core/alien/core/backend/IInternalLinearSolverT.h +++ b/src/core/alien/core/backend/IInternalLinearSolverT.h @@ -46,6 +46,9 @@ class ILinearAlgebra; class SolverStat; class SolverStatus; +class IMatrixImpl; +class IVectorImpl; + /*! * \ingroup core * \brief Internal linear solver interface @@ -85,7 +88,7 @@ class IInternalLinearSolver * \param[in,out] x The solution * \returns Solver success or failure */ - virtual bool solve(const Matrix& A, const Vector& b, Vector& x) = 0; + virtual bool solve(const IMatrixImpl& A, const IVectorImpl& b, IVectorImpl& x) = 0; /*! * \brief Get statistics on the solve phase @@ -117,6 +120,8 @@ class IInternalLinearSolver { return std::shared_ptr(); } + + virtual const BackEndId backEndName() const = 0; }; /*---------------------------------------------------------------------------*/ diff --git a/src/core/alien/core/backend/LinearSolverT.h b/src/core/alien/core/backend/LinearSolver.cc similarity index 98% rename from src/core/alien/core/backend/LinearSolverT.h rename to src/core/alien/core/backend/LinearSolver.cc index 2dc5f3e4d..aa3f398c8 100644 --- a/src/core/alien/core/backend/LinearSolverT.h +++ b/src/core/alien/core/backend/LinearSolver.cc @@ -47,7 +47,7 @@ LinearSolver::~LinearSolver() {} String LinearSolver::getBackEndName() const { - return m_solver->getBackEndName(); + return m_solver->backEndName(); } /*---------------------------------------------------------------------------*/ @@ -130,7 +130,7 @@ LinearSolver::getStatus() const /*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/ -IInternalLinearSolver* +IInternalLinearSolver* LinearSolver::implem() { return m_solver.get(); diff --git a/src/core/alien/core/backend/LinearSolver.h b/src/core/alien/core/backend/LinearSolver.h index bda895049..a433a466e 100644 --- a/src/core/alien/core/backend/LinearSolver.h +++ b/src/core/alien/core/backend/LinearSolver.h @@ -30,7 +30,7 @@ #include #include -#include +#include #include @@ -78,7 +78,7 @@ class LinearSolver : public ILinearSolver //: m_solver(AlgebraTraits::solver_factory(...)) //{} - LinearSolver(BackEndId backEndId, Args... args) + LinearSolver(BackEndId backEndId) : m_backEndId(backEndId) { // FIXME: init m_solver here @@ -148,7 +148,7 @@ class LinearSolver : public ILinearSolver * \brief Get kernel solver implementation * \return Linear solver actual implementation */ - IInternalLinearSolver* implem(); + IInternalLinearSolver* implem(); /*! * \brief Option to add an extra-equation @@ -166,7 +166,7 @@ class LinearSolver : public ILinearSolver private: //! The linear solver kernel - std::unique_ptr m_solver; + std::unique_ptr> m_solver; BackEndId m_backEndId; }; diff --git a/src/core/alien/kernels/dok/DoKBackEnd.h b/src/core/alien/kernels/dok/DoKBackEnd.h index 55719b34c..dae8413cc 100644 --- a/src/core/alien/kernels/dok/DoKBackEnd.h +++ b/src/core/alien/kernels/dok/DoKBackEnd.h @@ -33,11 +33,12 @@ class DoKVector; class Space; template class IInternalLinearAlgebra; +template class IInternalLinearSolver; extern IInternalLinearAlgebra* DoKLinearAlgebraFactory(); -extern IInternalLinearSolver* DoKLinearSolverFactory( +extern IInternalLinearSolver* DoKLinearSolverFactory( IMessagePassingMng* p_mng); /*---------------------------------------------------------------------------*/ @@ -54,7 +55,7 @@ struct AlgebraTraits typedef DoKMatrix matrix_type; typedef DoKVector vector_type; typedef IInternalLinearAlgebra algebra_type; - typedef IInternalLinearSolver solver_type; + typedef IInternalLinearSolver solver_type; static algebra_type* algebra_factory() { return DoKLinearAlgebraFactory(); } static solver_type* solver_factory(IMessagePassingMng* p_mng) { diff --git a/src/core/alien/kernels/redistributor/RedistributorBackEnd.h b/src/core/alien/kernels/redistributor/RedistributorBackEnd.h index f58aa0bb9..6f3836ba6 100644 --- a/src/core/alien/kernels/redistributor/RedistributorBackEnd.h +++ b/src/core/alien/kernels/redistributor/RedistributorBackEnd.h @@ -38,12 +38,13 @@ class RedistributorVector; class Space; template class IInternalLinearAlgebra; +template class IInternalLinearSolver; extern IInternalLinearAlgebra* redistributorLinearAlgebraFactory(); -extern IInternalLinearSolver* +extern IInternalLinearSolver* redistributorLinearSolverFactory(IMessagePassingMng* p_mng); /*---------------------------------------------------------------------------*/ @@ -63,7 +64,7 @@ struct AlgebraTraits typedef RedistributorMatrix matrix_type; typedef RedistributorVector vector_type; typedef IInternalLinearAlgebra algebra_type; - typedef IInternalLinearSolver solver_type; + typedef IInternalLinearSolver solver_type; static algebra_type* algebra_factory() { return redistributorLinearAlgebraFactory(); } static solver_type* solver_factory(IMessagePassingMng* p_mng) { diff --git a/src/core/tests/TestDoK.cc b/src/core/tests/TestDoK.cc index 95ede44be..9615bf53b 100644 --- a/src/core/tests/TestDoK.cc +++ b/src/core/tests/TestDoK.cc @@ -154,8 +154,8 @@ TEST(TestDoKMatrix, ConvertFromCSR) std::unique_ptr multimat( new Alien::MultiMatrixImpl(row_space.clone(), col_space.clone(), mdist.clone())); - auto& csr_mat(multimat->get()); - auto& dok_mat(multimat->get(true)); + auto& csr_mat(dynamic_cast&>(multimat->get("simplecsr"))); + auto& dok_mat(dynamic_cast(multimat->get("DoK", true))); Alien::SimpleCSRtoDoKMatrixConverter converter; converter.convert(&csr_mat, &dok_mat); diff --git a/src/movesemantic/alien/move/handlers/sub_matrix/Extraction.cc b/src/movesemantic/alien/move/handlers/sub_matrix/Extraction.cc index 2da0b2395..8b7d24493 100644 --- a/src/movesemantic/alien/move/handlers/sub_matrix/Extraction.cc +++ b/src/movesemantic/alien/move/handlers/sub_matrix/Extraction.cc @@ -51,7 +51,7 @@ SubMatrix::extractRange(const IMatrix& matrix, const ExtractionIndices& indices) { const SimpleCSRMatrix* matrix_impl = - &matrix.impl()->get(); + &dynamic_cast&>(matrix.impl()->get("simple_csr")); const auto& dist = matrix_impl->distribution(); diff --git a/src/movesemantic/alien/move/impl/MatrixMarketReader.cc b/src/movesemantic/alien/move/impl/MatrixMarketReader.cc index 457dfc7f7..0255fb54c 100644 --- a/src/movesemantic/alien/move/impl/MatrixMarketReader.cc +++ b/src/movesemantic/alien/move/impl/MatrixMarketReader.cc @@ -215,7 +215,7 @@ VectorData ALIEN_MOVESEMANTIC_EXPORT readFromMatrixMarket(const VectorDistribution& distribution, const std::string& filename) { VectorData out(distribution); - auto& v = out.impl()->template get(true); + auto& v = dynamic_cast(out.impl()->get("DoK", true)); if (distribution.parallelMng()->commRank() == 0) { // Only rank 0 read the file std::ifstream stream; diff --git a/src/movesemantic/tests/CMakeLists.txt b/src/movesemantic/tests/CMakeLists.txt index 773696ece..a29cc54f9 100644 --- a/src/movesemantic/tests/CMakeLists.txt +++ b/src/movesemantic/tests/CMakeLists.txt @@ -1,17 +1,17 @@ # Copyright 2020 IFPEN-CEA -# +# # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at -# +# # http://www.apache.org/licenses/LICENSE-2.0 -# +# # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. -# +# # SPDX-License-Identifier: Apache-2.0 find_package(MPI REQUIRED) @@ -32,7 +32,6 @@ add_executable(move.gtest.mpi add_executable(move.gtest.seq main.cpp TestVectorBuilder.cc - TestMatrixDirectBuilder.cc ) target_link_libraries(move.gtest.mpi PRIVATE diff --git a/src/movesemantic/tests/TestDoKDirectMatrixBuilder.cc b/src/movesemantic/tests/TestDoKDirectMatrixBuilder.cc index bec0b4828..98bdbb611 100644 --- a/src/movesemantic/tests/TestDoKDirectMatrixBuilder.cc +++ b/src/movesemantic/tests/TestDoKDirectMatrixBuilder.cc @@ -29,7 +29,6 @@ #include #include -#include TEST(TestDoKDirectMatrixBuilder, Fill) { diff --git a/src/movesemantic/tests/TestDoKMatrix.cc b/src/movesemantic/tests/TestDoKMatrix.cc index f0a406555..43910a38a 100644 --- a/src/movesemantic/tests/TestDoKMatrix.cc +++ b/src/movesemantic/tests/TestDoKMatrix.cc @@ -67,6 +67,6 @@ TEST(TestDoKMatrix, MultiImplConverter) A = builder.release(); Alien::MultiMatrixImpl* multiA = A.impl(); - const Alien::DoKMatrix& dok_a = multiA->get(); + const Alien::DoKMatrix& dok_a = dynamic_cast(multiA->get("DoK")); dok_a.backend(); } diff --git a/src/movesemantic/tests/TestMatrixDirectBuilder.cc b/src/movesemantic/tests/TestMatrixDirectBuilder.cc deleted file mode 100644 index c84339ae6..000000000 --- a/src/movesemantic/tests/TestMatrixDirectBuilder.cc +++ /dev/null @@ -1,80 +0,0 @@ -/* - * Copyright 2020 IFPEN-CEA - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * SPDX-License-Identifier: Apache-2.0 - */ - -#include - -#include -#include -#include - -#include -#include -#include -#include -#include - -#include -#include - -TEST(TestMatrixDirectBuilder, Functional) -{ - Alien::Space row_space(3, "RowSpace"); - Alien::Space col_space(4, "ColSpace"); - Alien::MatrixDistribution mdist( - row_space, col_space, AlienTest::Environment::parallelMng()); - Alien::VectorDistribution vdist(col_space, AlienTest::Environment::parallelMng()); - Alien::Move::MatrixData A(mdist); - ASSERT_EQ(A.rowSpace(), row_space); - ASSERT_EQ(A.colSpace(), col_space); - auto tag = Alien::DirectMatrixOptions::eResetValues; - { - Alien::Move::DirectMatrixBuilder builder( - std::move(A), tag, Alien::DirectMatrixOptions::SymmetricFlag::eUnSymmetric); - builder.reserve(5); - builder.allocate(); - builder(0, 0) = 1.; - builder(1, 1) = 1.; - builder(2, 2) = 1.; - builder(2, 3) = 1.; - A = builder.release(); - } - // check with spmv - Alien::LinearAlgebra Alg(vdist.parallelMng()); - Alien::Move::VectorData X(vdist); - { - Alien::Move::LocalVectorWriter writer(std::move(X)); - writer[0] = 1.; - writer[1] = 2.; - writer[2] = 3.; - writer[3] = 4.; - X = writer.release(); - } - Alien::VectorDistribution vdist2(row_space, AlienTest::Environment::parallelMng()); - Alien::Move::VectorData R(vdist2); - Alg.mult(A, X, R); - { - Alien::Move::LocalVectorReader reader(std::move(R)); - std::cout << reader[0] << std::endl; - std::cout << reader[1] << std::endl; - std::cout << reader[2] << std::endl; - - ASSERT_EQ(reader[0], 1.); - ASSERT_EQ(reader[1], 2.); - ASSERT_EQ(reader[2], 7.); - } -} diff --git a/src/refsemantic/alien/ref/handlers/stream/StreamMatrixBuilderT.h b/src/refsemantic/alien/ref/handlers/stream/StreamMatrixBuilderT.h index 3564565ec..3acf35b56 100644 --- a/src/refsemantic/alien/ref/handlers/stream/StreamMatrixBuilderT.h +++ b/src/refsemantic/alien/ref/handlers/stream/StreamMatrixBuilderT.h @@ -189,7 +189,7 @@ void StreamMatrixBuilderT::init() m_matrix.impl()->lock(); - m_matrix_impl = &m_matrix.impl()->template get(true); + m_matrix_impl = &dynamic_cast&>(m_matrix.impl()->get("simplecsr", true)); // const ISpace& space = m_matrix_impl->rowSpace(); // if (space != m_matrix_impl->colSpace()) diff --git a/src/refsemantic/alien/ref/handlers/stream/StreamVBlockMatrixBuilderT.h b/src/refsemantic/alien/ref/handlers/stream/StreamVBlockMatrixBuilderT.h index 455a3a862..f1509cc07 100644 --- a/src/refsemantic/alien/ref/handlers/stream/StreamVBlockMatrixBuilderT.h +++ b/src/refsemantic/alien/ref/handlers/stream/StreamVBlockMatrixBuilderT.h @@ -176,7 +176,7 @@ void StreamVBlockMatrixBuilderT::init() m_parallel_mng = dist.parallelMng(); m_matrix.impl()->lock(); - m_matrix_impl = &m_matrix.impl()->template get(true); + m_matrix_impl = &dynamic_cast&>(m_matrix.impl()->get("simplecsr", true)); m_matrix_impl->free(); m_local_size = dist.localRowSize(); diff --git a/src/refsemantic/alien/ref/import_export/MatrixMarketSystemWriter.cc b/src/refsemantic/alien/ref/import_export/MatrixMarketSystemWriter.cc index 8834ed778..a87ce8084 100644 --- a/src/refsemantic/alien/ref/import_export/MatrixMarketSystemWriter.cc +++ b/src/refsemantic/alien/ref/import_export/MatrixMarketSystemWriter.cc @@ -59,7 +59,7 @@ MatrixMarketSystemWriter::~MatrixMarketSystemWriter() {} void MatrixMarketSystemWriter::dump(Matrix const& A) { - const SimpleCSRMatrix& csr = A.impl()->get(); + const SimpleCSRMatrix& csr = dynamic_cast&>(A.impl()->get("simplecsr")); const SimpleCSRMatrix::ProfileType& profile = csr.getProfile(); int nrows = profile.getNRows(); int nnz = profile.getNnz(); @@ -138,7 +138,7 @@ void MatrixMarketSystemWriter::dump(Matrix const& A) void MatrixMarketSystemWriter::dump(Vector const& rhs) { - const SimpleCSRVector& v = rhs.impl()->get(); + const SimpleCSRVector& v = dynamic_cast&>(rhs.impl()->get("simplecsr")); auto local_size = v.distribution().localSize(); const double* values = v.getAddressData(); if (m_nproc == 1) { diff --git a/src/refsemantic/alien/ref/import_export/SystemWriter.cc b/src/refsemantic/alien/ref/import_export/SystemWriter.cc index ef983506f..9a139ebce 100644 --- a/src/refsemantic/alien/ref/import_export/SystemWriter.cc +++ b/src/refsemantic/alien/ref/import_export/SystemWriter.cc @@ -214,7 +214,7 @@ void SystemWriter::_endDump(Exporter* exporter, FileNodeT& base_node) void SystemWriter::dump(Matrix const& A) { - const SimpleCSRMatrix& csr = A.impl()->get(); + const SimpleCSRMatrix& csr = dynamic_cast&>(A.impl()->get("simplecsr")); const SimpleCSRMatrix::ProfileType& profile = csr.getProfile(); int nrows = profile.getNRows(); int blk_size = 1; @@ -245,7 +245,7 @@ void SystemWriter::dump(Matrix const& A, Vector const& rhs) { // A is supposed to be square - const SimpleCSRMatrix& csr = A.impl()->get(); + const SimpleCSRMatrix& csr = dynamic_cast&>(A.impl()->get("simplecsr")); const SimpleCSRMatrix::ProfileType& profile = csr.getProfile(); int nrows = profile.getNRows(); int blk_size = 1; @@ -271,7 +271,7 @@ void SystemWriter::dump(Matrix const& A, Vector const& rhs) FileNode vector_node = exporter->createFileNode(root_node, "rhs-0"); { - const SimpleCSRVector& v = rhs.impl()->get(); + const SimpleCSRVector& v = dynamic_cast&>(rhs.impl()->get("simplecsr")); const double* values = v.getAddressData(); _writeVector(*exporter, vector_node, nrows, blk_size, values); } @@ -283,7 +283,7 @@ void SystemWriter::dump(Matrix const& A, Vector const& rhs) void SystemWriter::dump( Matrix const& A, Vector const& rhs, Vector const& sol, SolutionInfo const& sol_info) { - const SimpleCSRMatrix& csr = A.impl()->get(); + const SimpleCSRMatrix& csr = dynamic_cast&>(A.impl()->get("simplecsr")); const SimpleCSRMatrix::ProfileType& profile = csr.getProfile(); int nrows = profile.getNRows(); int blk_size = 1; @@ -309,7 +309,7 @@ Matrix const& A, Vector const& rhs, Vector const& sol, SolutionInfo const& sol_i FileNode rhs_node = exporter->createFileNode(root_node, "rhs-0"); { - const SimpleCSRVector& v = rhs.impl()->get(); + const SimpleCSRVector& v = dynamic_cast&>(rhs.impl()->get("simplecsr")); const double* values = v.getAddressData(); _writeVector(*exporter, rhs_node, nrows, blk_size, values); } @@ -324,7 +324,7 @@ Matrix const& A, Vector const& rhs, Vector const& sol, SolutionInfo const& sol_i FileNode solution_vector_node = exporter->createFileNode(solution_node, "solution-vector"); - const SimpleCSRVector& v = sol.impl()->get(); + const SimpleCSRVector& v = dynamic_cast&>(sol.impl()->get("simplecsr")); const double* values = v.getAddressData(); _writeVector(*exporter, solution_vector_node, nrows, blk_size, values); exporter->closeFileNode(solution_vector_node); @@ -336,7 +336,7 @@ Matrix const& A, Vector const& rhs, Vector const& sol, SolutionInfo const& sol_i void SystemWriter::dump(BlockMatrix const& A, BlockVector const& rhs) { - const SimpleCSRMatrix& csr = A.impl()->get(); + const SimpleCSRMatrix& csr = dynamic_cast&>(A.impl()->get("simplecsr")); const SimpleCSRMatrix::ProfileType& profile = csr.getProfile(); int nrows = profile.getNRows(); int blk_size = A.block().size(); @@ -362,7 +362,7 @@ void SystemWriter::dump(BlockMatrix const& A, BlockVector const& rhs) FileNode vector_node = exporter->createFileNode(root_node, "rhs-0"); { - const SimpleCSRVector& v = rhs.impl()->get(); + const SimpleCSRVector& v = dynamic_cast&>(rhs.impl()->get("simplecsr")); const double* values = v.getAddressData(); _writeVector(*exporter, vector_node, nrows, blk_size, values); } @@ -373,7 +373,7 @@ void SystemWriter::dump(BlockMatrix const& A, BlockVector const& rhs) void SystemWriter::dump(BlockMatrix const& A, BlockVector const& rhs, BlockVector const& sol, SolutionInfo const& sol_info) { - const SimpleCSRMatrix& csr = A.impl()->get(); + const SimpleCSRMatrix& csr = dynamic_cast&>(A.impl()->get("simplecsr")); const SimpleCSRMatrix::ProfileType& profile = csr.getProfile(); int nrows = profile.getNRows(); int blk_size = A.block().size(); @@ -399,7 +399,7 @@ void SystemWriter::dump(BlockMatrix const& A, BlockVector const& rhs, BlockVecto FileNode rhs_node = exporter->createFileNode(root_node, "rhs-0"); { - const SimpleCSRVector& v = rhs.impl()->get(); + const SimpleCSRVector& v = dynamic_cast&>(rhs.impl()->get("simplecsr")); const double* values = v.getAddressData(); _writeVector(*exporter, rhs_node, nrows, blk_size, values); } @@ -408,7 +408,7 @@ void SystemWriter::dump(BlockMatrix const& A, BlockVector const& rhs, BlockVecto FileNode sol_node = exporter->createFileNode(root_node, "sol"); { - const SimpleCSRVector& v = sol.impl()->get(); + const SimpleCSRVector& v = dynamic_cast&>(sol.impl()->get("simplecsr")); const double* values = v.getAddressData(); _writeVector(*exporter, sol_node, nrows, blk_size, values); } diff --git a/src/refsemantic/tests/CMakeLists.txt b/src/refsemantic/tests/CMakeLists.txt index 78c1d306d..d13b6e32c 100644 --- a/src/refsemantic/tests/CMakeLists.txt +++ b/src/refsemantic/tests/CMakeLists.txt @@ -1,17 +1,17 @@ # Copyright 2020 IFPEN-CEA -# +# # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at -# +# # http://www.apache.org/licenses/LICENSE-2.0 -# +# # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. -# +# # SPDX-License-Identifier: Apache-2.0 find_package(MPI REQUIRED) @@ -29,14 +29,12 @@ endif () add_executable(ref.gtest.seq main.cpp TestVector.cc TestVectorBuilder.cc - TestMatrixDirectBuilder.cc TestCompositeVector.cc TestCompositeMatrix.cc TestBlockMatrix.cc TestVBlockMatrix.cc - TestRedistributor.cc - TestMVExpr.cc) - + TestRedistributor.cc) + IF(ALIEN_USE_SYCL) target_sources(ref.gtest.seq PRIVATE TestMVSYCL.cc) ENDIF() diff --git a/src/refsemantic/tests/TestCompositeMatrix.cc b/src/refsemantic/tests/TestCompositeMatrix.cc index 66b0e75f1..7b454bc48 100644 --- a/src/refsemantic/tests/TestCompositeMatrix.cc +++ b/src/refsemantic/tests/TestCompositeMatrix.cc @@ -214,8 +214,8 @@ TEST(TestCompositeMatrix, TimeStampTest) auto* impl0 = c0.impl(); ASSERT_EQ(0, impl0->timestamp()); std::cout << "sub0 ts = " << impl0->timestamp() << std::endl; - impl0->get(true); - impl0->get(true); + impl0->get("simple_csr", true); + impl0->get("simple_csr", true); ASSERT_EQ(2, impl0->timestamp()); std::cout << "sub0 ts = " << impl0->timestamp() << std::endl; ASSERT_EQ(2, impl->timestamp()); diff --git a/src/refsemantic/tests/TestCompositeVector.cc b/src/refsemantic/tests/TestCompositeVector.cc index 7ea76e439..84df99fa8 100644 --- a/src/refsemantic/tests/TestCompositeVector.cc +++ b/src/refsemantic/tests/TestCompositeVector.cc @@ -193,8 +193,8 @@ TEST(TestCompositeVector, TimeStampTest) auto* impl0 = c0.impl(); ASSERT_EQ(0, impl0->timestamp()); std::cout << "sub0 ts = " << impl0->timestamp() << std::endl; - impl0->get(true); - impl0->get(true); + impl0->get("simple_csr", true); + impl0->get("simple_csr", true); ASSERT_EQ(2, impl0->timestamp()); std::cout << "sub0 ts = " << impl0->timestamp() << std::endl; ASSERT_EQ(2, impl->timestamp()); diff --git a/src/refsemantic/tests/TestMVExpr.cc b/src/refsemantic/tests/TestMVExpr.cc deleted file mode 100644 index 6db46aea6..000000000 --- a/src/refsemantic/tests/TestMVExpr.cc +++ /dev/null @@ -1,164 +0,0 @@ -/* - * Copyright 2020 IFPEN-CEA - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * SPDX-License-Identifier: Apache-2.0 - */ - -#include -#include -#include -#include - -#include -#include - -#include - -// Tests the default c'tor. -TEST(TestMVExpr, ExprEngine) -{ - using namespace Alien; - Alien::ITraceMng* trace_mng = AlienTest::Environment::traceMng(); - Integer global_size = 10; - const Alien::Space s(10, "MySpace"); - Alien::MatrixDistribution mdist(s, s, AlienTest::Environment::parallelMng()); - Alien::VectorDistribution vdist(s, AlienTest::Environment::parallelMng()); - Alien::Matrix A(mdist); // A.setName("A") ; - Alien::Vector x(vdist); // x.setName("x") ; - Alien::Vector y(vdist); // y.setName("y") ; - Alien::Vector r(vdist); // r.setName("r") ; - Alien::Real lambda = 0.5; - - auto local_size = vdist.localSize(); - auto offset = vdist.offset(); - { - Alien::MatrixProfiler profiler(A); - for (Integer i = 0; i < local_size; ++i) { - Integer row = offset + i; - profiler.addMatrixEntry(row, row); - if (row + 1 < global_size) - profiler.addMatrixEntry(row, row + 1); - if (row - 1 >= 0) - profiler.addMatrixEntry(row, row - 1); - } - } - { - Alien::ProfiledMatrixBuilder builder(A, Alien::ProfiledMatrixOptions::eResetValues); - for (Integer i = 0; i < local_size; ++i) { - Integer row = offset + i; - builder(row, row) = 2.; - if (row + 1 < global_size) - builder(row, row + 1) = -1.; - if (row - 1 >= 0) - builder(row, row - 1) = -1.; - } - } - { - SystemWriter writer("MatrixA.txt"); - writer.dump(A); - } - { - Alien::LocalVectorWriter writer(x); - for (Integer i = 0; i < local_size; ++i) - writer[i] = 1.; - } - - using namespace Alien::MVExpr; - - { - auto expr = mul(ref(A), ref(x)); - auto evaluated = eval(expr); - } - - { - auto expr = add(ref(x), ref(y)); - auto evaluated = eval(expr); - } - - { - auto expr = mul(ref(A), add(ref(x), ref(y))); - auto evaluated = eval(expr); - } - - { - // auto expr = mul(ref(A), add(ref(x),mul(cst(lambda),ref(y)))) ; - // auto evaluated = eval(expr); - } - - Alien::SimpleCSRLinearAlgebra alg; - // trace_mng->info()<<" NORME : "<info() << "y = lambda*x"; - assign(y, lambda * x); - y = lambda * x; - // trace_mng->info()<<" NORME : "<info() << "y=A*x"; - assign(y, mul(ref(A), ref(x))); - y = A * x; - { - Alien::LocalVectorReader view(y); - for (Integer i = 0; i < local_size; ++i) - trace_mng->info() << "Y[" << i << "]=" << view[i]; - } - - trace_mng->info() << " NORME : " << alg.norm2(y); - ASSERT_EQ(alg.norm2(y), std::sqrt(2)); - - trace_mng->info() << "r=y-A*x"; - r = y - A * x; - // trace_mng->info()<<" NORME : "<info() << "x=x+lambda*r"; - y = y + (lambda * x); - // trace_mng->info()<<" NORME : "<info() << "y=A*lambda*r"; - y = A * (lambda * x); - // trace_mng->info()<<" NORME : "<info() << " DOT(x,y) " << value1; - ASSERT_EQ(value1, 45); - - Real value2 = eval(dot(lambda * x, y)); - trace_mng->info() << " DOT(lambda*x,y) " << value2; - ASSERT_EQ(value2, 22.5); - - Real value3 = eval(dot(x, r + y)); - trace_mng->info() << " DOT(x,r+y) " << value3; - ASSERT_EQ(value3, 45); - - Real value4 = eval(dot(x, A * y)); - trace_mng->info() << " DOT(x,A*y) " << value4; - ASSERT_EQ(value4, 9); - } - //////////////////////////////////////////////// - // - // TESTS PIPELINE ENABLING ASYNCHRONISM - { - pipeline(vassign(y, A * x), vassign(r, y - A * x)); - } -} diff --git a/src/refsemantic/tests/TestMatrixDirectBuilder.cc b/src/refsemantic/tests/TestMatrixDirectBuilder.cc deleted file mode 100644 index e1ac8e13d..000000000 --- a/src/refsemantic/tests/TestMatrixDirectBuilder.cc +++ /dev/null @@ -1,70 +0,0 @@ -/* - * Copyright 2020 IFPEN-CEA - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * SPDX-License-Identifier: Apache-2.0 - */ - -#include - -#include - -#include -#include - -TEST(TestMatrixDirectBuilder, ConstructorWithSpaces) -{ - Alien::Space row_space(3, "RowSpace"); - Alien::Space col_space(4, "ColSpace"); - Alien::MatrixDistribution mdist( - row_space, col_space, AlienTest::Environment::parallelMng()); - Alien::VectorDistribution vdist(col_space, AlienTest::Environment::parallelMng()); - Alien::Matrix A(mdist); - ASSERT_EQ(A.rowSpace(), row_space); - ASSERT_EQ(A.colSpace(), col_space); - auto tag = Alien::DirectMatrixOptions::eResetValues; - { - Alien::DirectMatrixBuilder builder( - A, tag, Alien::DirectMatrixOptions::SymmetricFlag::eUnSymmetric); - builder.reserve(5); - builder.allocate(); - builder(0, 0) = 1.; - builder(1, 1) = 1.; - builder(2, 2) = 1.; - builder(2, 3) = 1.; - } - // check with spmv - Alien::LinearAlgebra Alg(vdist.parallelMng()); - Alien::Vector X(vdist); - { - Alien::LocalVectorWriter writer(X); - writer[0] = 1.; - writer[1] = 2.; - writer[2] = 3.; - writer[3] = 4.; - } - Alien::VectorDistribution vdist2(row_space, AlienTest::Environment::parallelMng()); - Alien::Vector R(vdist2); - Alg.mult(A, X, R); - { - Alien::LocalVectorReader reader(R); - std::cout << reader[0] << std::endl; - std::cout << reader[1] << std::endl; - std::cout << reader[2] << std::endl; - - ASSERT_EQ(reader[0], 1.); - ASSERT_EQ(reader[1], 2.); - ASSERT_EQ(reader[2], 7.); - } -} diff --git a/src/refsemantic/tests/TestRedistributor.cc b/src/refsemantic/tests/TestRedistributor.cc index a2dd32b7d..41508b4a3 100644 --- a/src/refsemantic/tests/TestRedistributor.cc +++ b/src/refsemantic/tests/TestRedistributor.cc @@ -34,7 +34,7 @@ #include -#include +//#include using namespace Arccore; @@ -87,7 +87,7 @@ TEST(TestRedistributorRef, Redistributor) Alien::RedistributedVector bb(b, redist); Alien::RedistributedVector xx(x, redist); - Alien::SimpleCSRLinearAlgebra algebra; - algebra.axpy(2., bb, xx); - algebra.mult(Aa, xx, bb); + //Alien::SimpleCSRLinearAlgebra algebra; + //algebra.axpy(2., bb, xx); + //algebra.mult(Aa, xx, bb); } From 764e55c0b93636f7849ba516439bdb759efc2587 Mon Sep 17 00:00:00 2001 From: Louis Goyard Date: Thu, 23 Jun 2022 16:40:42 +0200 Subject: [PATCH 07/13] WIP: more compil --- plugins/hypre/examples/CMakeLists.txt | 11 +++----- .../examples/hypre_example_solve_move.cpp | 4 +-- plugins/hypre/include/alien/hypre/backend.h | 9 ++++--- plugins/hypre/src/hypre_linear_solver.cpp | 8 +++--- plugins/hypre/src/hypre_linear_solver.h | 2 +- src/core/CMakeLists.txt | 2 +- src/core/alien/core/backend/BackEnd.h | 6 ++--- ...inearSolverT.h => IInternalLinearSolver.h} | 5 ++-- src/core/alien/core/backend/LinearSolver.cc | 27 +++++++++++++++++-- src/core/alien/core/backend/LinearSolver.h | 26 +++++++++++------- src/core/alien/kernels/dok/DoKBackEnd.h | 5 ++-- .../redistributor/RedistributorBackEnd.h | 5 ++-- 12 files changed, 68 insertions(+), 42 deletions(-) rename src/core/alien/core/backend/{IInternalLinearSolverT.h => IInternalLinearSolver.h} (97%) diff --git a/plugins/hypre/examples/CMakeLists.txt b/plugins/hypre/examples/CMakeLists.txt index 33ceea485..753b38940 100644 --- a/plugins/hypre/examples/CMakeLists.txt +++ b/plugins/hypre/examples/CMakeLists.txt @@ -1,17 +1,17 @@ # Copyright 2020 IFPEN-CEA -# +# # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at -# +# # http://www.apache.org/licenses/LICENSE-2.0 -# +# # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. -# +# # SPDX-License-Identifier: Apache-2.0 cmake_minimum_required(VERSION 3.13) @@ -25,19 +25,16 @@ find_package(Alien REQUIRED) add_executable(example_hypre hypre_example_solve.cpp) target_link_libraries(example_hypre PUBLIC - Alien::hypre_wrapper Alien::alien_semantic_ref) add_executable(example_hypre_move hypre_example_solve_move.cpp) target_link_libraries(example_hypre_move PUBLIC - Alien::hypre_wrapper Alien::alien_semantic_move) add_executable(mm_driver_hypre driver_mm_hypre.cc) target_link_libraries(mm_driver_hypre PUBLIC - Alien::hypre_wrapper Alien::alien_semantic_move) configure_file(msc00726.mtx msc00726.mtx COPYONLY) diff --git a/plugins/hypre/examples/hypre_example_solve_move.cpp b/plugins/hypre/examples/hypre_example_solve_move.cpp index c8e4350e8..f6d2e09d4 100644 --- a/plugins/hypre/examples/hypre_example_solve_move.cpp +++ b/plugins/hypre/examples/hypre_example_solve_move.cpp @@ -21,8 +21,8 @@ #include #include -#include -#include +#include +#include int test() { diff --git a/plugins/hypre/include/alien/hypre/backend.h b/plugins/hypre/include/alien/hypre/backend.h index 41e0f2eaf..9692bf2ea 100644 --- a/plugins/hypre/include/alien/hypre/backend.h +++ b/plugins/hypre/include/alien/hypre/backend.h @@ -31,9 +31,9 @@ class Options; class ILinearSolver; -extern std::shared_ptr> LinearSolverFactory(const BackEnd::Options& options); +extern std::unique_ptr LinearSolverFactory(const BackEnd::Options& options); -extern std::shared_ptr> LinearSolverFactory(); +extern std::unique_ptr LinearSolverFactory(); } // namespace Alien @@ -52,14 +52,15 @@ namespace BackEnd class Plugin : public BackEnd::IPlugin { +public: // factories to default solver - std::shared_ptr> solver_factory() + std::unique_ptr solver_factory() { return LinearSolverFactory(); } // factories to build solver - std::shared_ptr> solver_factory(const Alien::BackEnd::Options& options) + std::unique_ptr solver_factory(const Alien::BackEnd::Options& options) { return LinearSolverFactory(options); } diff --git a/plugins/hypre/src/hypre_linear_solver.cpp b/plugins/hypre/src/hypre_linear_solver.cpp index d680fab90..a86a79e40 100644 --- a/plugins/hypre/src/hypre_linear_solver.cpp +++ b/plugins/hypre/src/hypre_linear_solver.cpp @@ -303,16 +303,16 @@ PluginLinearSolver::getStatus() const } ALIEN_HYPRE_EXPORT -std::shared_ptr> +std::unique_ptr LinearSolverFactory(const BackEnd::Options& options) { - return std::make_shared(options); + return std::make_unique(options); } ALIEN_HYPRE_EXPORT -std::shared_ptr> +std::unique_ptr LinearSolverFactory() { - return std::make_shared(); + return std::make_unique(); } } // namespace Alien diff --git a/plugins/hypre/src/hypre_linear_solver.h b/plugins/hypre/src/hypre_linear_solver.h index e4b28b021..519f44902 100644 --- a/plugins/hypre/src/hypre_linear_solver.h +++ b/plugins/hypre/src/hypre_linear_solver.h @@ -33,7 +33,7 @@ namespace Alien { -class PluginLinearSolver : public IInternalLinearSolver +class PluginLinearSolver : public IInternalLinearSolver , public ObjectWithTrace { public: diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt index 49c5b6c08..52012d38f 100644 --- a/src/core/CMakeLists.txt +++ b/src/core/CMakeLists.txt @@ -36,7 +36,7 @@ add_library(alien_core alien/core/backend/IInternalEigenSolverT.h alien/core/backend/IInternalLinearAlgebraT.h alien/core/backend/IInternalLinearAlgebraExprT.h - alien/core/backend/IInternalLinearSolverT.h + alien/core/backend/IInternalLinearSolver.h alien/core/backend/IMatrixConverter.h alien/core/backend/IVectorConverter.h alien/core/backend/ISolverFabric.h diff --git a/src/core/alien/core/backend/BackEnd.h b/src/core/alien/core/backend/BackEnd.h index e74dd80c5..ad8a36db0 100644 --- a/src/core/alien/core/backend/BackEnd.h +++ b/src/core/alien/core/backend/BackEnd.h @@ -39,7 +39,6 @@ struct AlgebraTraits; template class LUSendRecvTraits; -template class IInternalLinearSolver; class ILinearSolver; @@ -114,8 +113,9 @@ namespace BackEnd class IPlugin { - IInternalLinearSolver* solver_factory(); - IInternalLinearSolver* solver_factory(const Alien::BackEnd::Options& options); + public: + virtual std::unique_ptr solver_factory() = 0; + virtual std::unique_ptr solver_factory(const Alien::BackEnd::Options& options) = 0; BackEndId name(); }; } // namespace BackEnd diff --git a/src/core/alien/core/backend/IInternalLinearSolverT.h b/src/core/alien/core/backend/IInternalLinearSolver.h similarity index 97% rename from src/core/alien/core/backend/IInternalLinearSolverT.h rename to src/core/alien/core/backend/IInternalLinearSolver.h index 42502336e..b1c6f43ce 100644 --- a/src/core/alien/core/backend/IInternalLinearSolverT.h +++ b/src/core/alien/core/backend/IInternalLinearSolver.h @@ -17,8 +17,8 @@ */ /*! - * \file IInternalLinearSolverT.h - * \brief IInternalLinearSolverT.h + * \file IInternalLinearSolver.h + * \brief IInternalLinearSolver.h */ #pragma once @@ -58,7 +58,6 @@ class IVectorImpl; * \tparam Matrix The type of matrix used * \tparam Vector The type of vector used */ -template class IInternalLinearSolver { public: diff --git a/src/core/alien/core/backend/LinearSolver.cc b/src/core/alien/core/backend/LinearSolver.cc index aa3f398c8..4c0abda7a 100644 --- a/src/core/alien/core/backend/LinearSolver.cc +++ b/src/core/alien/core/backend/LinearSolver.cc @@ -39,7 +39,30 @@ namespace Alien /*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/ -LinearSolver::~LinearSolver() {} +LinearSolver::LinearSolver(BackEndId backEndId) +{ + // FIXME: add error checking + + m_handle = dlopen(backEndId.localstr(), RTLD_NOW | RTLD_LOCAL); + m_plugin_create = (BackEnd::IPlugin*(*)()) dlsym(m_handle, "create"); + m_plugin_destroy = (void (*)(BackEnd::IPlugin*)) dlsym(m_handle, "destroy"); + + m_plugin.reset(m_plugin_create()); + m_solver = m_plugin->solver_factory(); + + m_backEndId = m_solver->backEndName(); + + std::cout << "BACKENDNAMEIS " << m_backEndId << std::endl; +} + +/*---------------------------------------------------------------------------*/ +/*---------------------------------------------------------------------------*/ + +LinearSolver::~LinearSolver() +{ + // FIXME: m_handle + plugin.destroy()? + m_plugin_destroy(m_plugin.release()); +} /*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/ @@ -130,7 +153,7 @@ LinearSolver::getStatus() const /*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/ -IInternalLinearSolver* +IInternalLinearSolver* LinearSolver::implem() { return m_solver.get(); diff --git a/src/core/alien/core/backend/LinearSolver.h b/src/core/alien/core/backend/LinearSolver.h index a433a466e..6d1a6efd1 100644 --- a/src/core/alien/core/backend/LinearSolver.h +++ b/src/core/alien/core/backend/LinearSolver.h @@ -22,6 +22,11 @@ */ #pragma once +#ifndef _GNU_SOURCE +#define _GNU_SOURCE +#endif +#include + #include #include @@ -30,7 +35,7 @@ #include #include -#include +#include #include @@ -58,7 +63,9 @@ class IMatrix; * * \tparam Tag The tag of the type of solvers used */ -class LinearSolver : public ILinearSolver + +// FIXME: force access through factory? +class ALIEN_EXPORT LinearSolver : public ILinearSolver { public: //! The type of the solver @@ -78,11 +85,7 @@ class LinearSolver : public ILinearSolver //: m_solver(AlgebraTraits::solver_factory(...)) //{} - LinearSolver(BackEndId backEndId) - : m_backEndId(backEndId) - { - // FIXME: init m_solver here - } + LinearSolver(BackEndId backEndId); //! Free resources virtual ~LinearSolver(); @@ -148,7 +151,7 @@ class LinearSolver : public ILinearSolver * \brief Get kernel solver implementation * \return Linear solver actual implementation */ - IInternalLinearSolver* implem(); + IInternalLinearSolver* implem(); /*! * \brief Option to add an extra-equation @@ -166,8 +169,13 @@ class LinearSolver : public ILinearSolver private: //! The linear solver kernel - std::unique_ptr> m_solver; + std::unique_ptr m_solver; + void* m_handle; BackEndId m_backEndId; + + std::unique_ptr m_plugin; + BackEnd::IPlugin* (*m_plugin_create)(); + void (*m_plugin_destroy)(BackEnd::IPlugin*); }; /*---------------------------------------------------------------------------*/ diff --git a/src/core/alien/kernels/dok/DoKBackEnd.h b/src/core/alien/kernels/dok/DoKBackEnd.h index dae8413cc..55719b34c 100644 --- a/src/core/alien/kernels/dok/DoKBackEnd.h +++ b/src/core/alien/kernels/dok/DoKBackEnd.h @@ -33,12 +33,11 @@ class DoKVector; class Space; template class IInternalLinearAlgebra; -template class IInternalLinearSolver; extern IInternalLinearAlgebra* DoKLinearAlgebraFactory(); -extern IInternalLinearSolver* DoKLinearSolverFactory( +extern IInternalLinearSolver* DoKLinearSolverFactory( IMessagePassingMng* p_mng); /*---------------------------------------------------------------------------*/ @@ -55,7 +54,7 @@ struct AlgebraTraits typedef DoKMatrix matrix_type; typedef DoKVector vector_type; typedef IInternalLinearAlgebra algebra_type; - typedef IInternalLinearSolver solver_type; + typedef IInternalLinearSolver solver_type; static algebra_type* algebra_factory() { return DoKLinearAlgebraFactory(); } static solver_type* solver_factory(IMessagePassingMng* p_mng) { diff --git a/src/core/alien/kernels/redistributor/RedistributorBackEnd.h b/src/core/alien/kernels/redistributor/RedistributorBackEnd.h index 6f3836ba6..f58aa0bb9 100644 --- a/src/core/alien/kernels/redistributor/RedistributorBackEnd.h +++ b/src/core/alien/kernels/redistributor/RedistributorBackEnd.h @@ -38,13 +38,12 @@ class RedistributorVector; class Space; template class IInternalLinearAlgebra; -template class IInternalLinearSolver; extern IInternalLinearAlgebra* redistributorLinearAlgebraFactory(); -extern IInternalLinearSolver* +extern IInternalLinearSolver* redistributorLinearSolverFactory(IMessagePassingMng* p_mng); /*---------------------------------------------------------------------------*/ @@ -64,7 +63,7 @@ struct AlgebraTraits typedef RedistributorMatrix matrix_type; typedef RedistributorVector vector_type; typedef IInternalLinearAlgebra algebra_type; - typedef IInternalLinearSolver solver_type; + typedef IInternalLinearSolver solver_type; static algebra_type* algebra_factory() { return redistributorLinearAlgebraFactory(); } static solver_type* solver_factory(IMessagePassingMng* p_mng) { From cfc4a3b6f4cb32b81e563baf0a603486387b4470 Mon Sep 17 00:00:00 2001 From: Louis Goyard Date: Fri, 24 Jun 2022 16:50:59 +0200 Subject: [PATCH 08/13] Add plugin init to interface --- plugins/hypre/bench/bench.cpp | 26 +++++----- plugins/hypre/examples/driver_mm_hypre.cc | 38 +++++++------- .../hypre/examples/hypre_example_solve.cpp | 2 +- .../examples/hypre_example_solve_move.cpp | 52 +++++++++---------- plugins/hypre/tests/move_use.cpp | 32 ++++++------ plugins/hypre/tests/ref_use.cpp | 32 ++++++------ src/core/alien/core/backend/BackEnd.h | 18 ++++++- src/core/alien/core/backend/LinearSolver.cc | 4 +- src/core/alien/core/backend/LinearSolver.h | 2 +- 9 files changed, 111 insertions(+), 95 deletions(-) diff --git a/plugins/hypre/bench/bench.cpp b/plugins/hypre/bench/bench.cpp index 10a77bceb..987f70f1b 100644 --- a/plugins/hypre/bench/bench.cpp +++ b/plugins/hypre/bench/bench.cpp @@ -24,13 +24,13 @@ #include #include -#include -#include +#include +#include #include "alien/expression/solver/SolverStat.h" constexpr int NB_RUNS = 5; -int test(const Alien::Hypre::OptionTypes::eSolver& solv, const Alien::Hypre::OptionTypes::ePreconditioner& prec, const std::string& mat_filename, const std::string& vec_filename) +int test(const Alien::BackEnd::OptionTypes::eSolver& solv, const Alien::BackEnd::OptionTypes::ePreconditioner& prec, const std::string& mat_filename, const std::string& vec_filename) { auto* pm = Arccore::MessagePassing::Mpi::StandaloneMpiMessagePassingMng::create(MPI_COMM_WORLD); auto* tm = Arccore::arccoreCreateDefaultTraceMng(); @@ -41,12 +41,12 @@ int test(const Alien::Hypre::OptionTypes::eSolver& solv, const Alien::Hypre::Opt auto bench = Alien::Benchmark::LinearBench(std::move(problem)); for (int i = 0; i < NB_RUNS; i++) { - Alien::Hypre::Options options; + Alien::BackEnd::Options options; options.numIterationsMax(500); options.stopCriteriaValue(1e-8); options.preconditioner(prec); // Jacobi, NoPC options.solver(solv); //CG, GMRES, BICG, BICGSTAB - auto solver = Alien::Hypre::LinearSolver(options); + auto solver = Alien::LinearSolver("hypre"); tm->info() << "Running Hypre"; auto solution = bench.solve(&solver); @@ -81,8 +81,8 @@ int main(int argc, char** argv) } // Default options, to run as test - auto solver = Alien::Hypre::OptionTypes::GMRES; - auto prec = Alien::Hypre::OptionTypes::DiagPC; + auto solver = Alien::BackEnd::OptionTypes::GMRES; + auto prec = Alien::BackEnd::OptionTypes::DiagPC; std::string matrix_file = "matrix.mtx"; std::string vec_file = "rhs.mtx"; @@ -92,10 +92,10 @@ int main(int argc, char** argv) else { // Read the solver if (std::string(argv[1]) == "CG") { - solver = Alien::Hypre::OptionTypes::CG; + solver = Alien::BackEnd::OptionTypes::CG; } else if (std::string(argv[1]) == "GMRES") { - solver = Alien::Hypre::OptionTypes::GMRES; + solver = Alien::BackEnd::OptionTypes::GMRES; } else { std::cerr << "Unrecognized solver : " << argv[1] << "\n" @@ -104,13 +104,13 @@ int main(int argc, char** argv) } if (std::string(argv[2]) == "Jacobi") { - prec = Alien::Hypre::OptionTypes::DiagPC; + prec = Alien::BackEnd::OptionTypes::DiagPC; } else if (std::string(argv[2]) == "NoPC") { - prec = Alien::Hypre::OptionTypes::NoPC; + prec = Alien::BackEnd::OptionTypes::NoPC; } else if (std::string(argv[2]) == "AMG") { - prec = Alien::Hypre::OptionTypes::AMGPC; + prec = Alien::BackEnd::OptionTypes::AMGPC; } else { std::cerr << "Unrecognized preconditioner : " << argv[2] << "\n" @@ -141,4 +141,4 @@ int main(int argc, char** argv) MPI_Finalize(); return ret; -} \ No newline at end of file +} diff --git a/plugins/hypre/examples/driver_mm_hypre.cc b/plugins/hypre/examples/driver_mm_hypre.cc index 446d32327..a7c7d082c 100644 --- a/plugins/hypre/examples/driver_mm_hypre.cc +++ b/plugins/hypre/examples/driver_mm_hypre.cc @@ -21,8 +21,8 @@ #include #include -#include -#include +#include +#include int test(const std::string& filename) { @@ -51,21 +51,21 @@ int test(const std::string& filename) Alien::Move::VectorData b(A.rowSpace(), A.distribution().rowDistribution()); - Alien::Hypre::LinearAlgebra algebra; + //Alien::Hypre::LinearAlgebra algebra; - algebra.mult(A, xe, b); + //algebra.mult(A, xe, b); Alien::Move::VectorData x(A.colSpace(), A.distribution().rowDistribution()); tm->info() << "* x = A^-1 b"; - auto options = Alien::Hypre::Options() - .numIterationsMax(100) - .stopCriteriaValue(1e-10) - .preconditioner(Alien::Hypre::OptionTypes::AMGPC) - .solver(Alien::Hypre::OptionTypes::GMRES); + //auto options = Alien::Hypre::Options() + // .numIterationsMax(100) + // .stopCriteriaValue(1e-10) + // .preconditioner(Alien::Hypre::OptionTypes::AMGPC) + // .solver(Alien::Hypre::OptionTypes::GMRES); - auto solver = Alien::Hypre::LinearSolver(options); + auto solver = Alien::LinearSolver("hypre"); solver.solve(A, b, x); @@ -73,24 +73,24 @@ int test(const std::string& filename) tm->info() << "* r = Ax - b"; Alien::Move::VectorData r(A.rowSpace(), A.distribution().rowDistribution()); - algebra.mult(A, x, r); + //algebra.mult(A, x, r); tm->info() << "r -= b"; - algebra.axpy(-1., b, r); + //algebra.axpy(-1., b, r); - auto norm = algebra.norm2(r); - auto norm_b = algebra.norm2(b); + //auto norm = algebra.norm2(r); + //auto norm_b = algebra.norm2(b); - tm->info() << " => ||r|| = " << norm << " ; ||r||/||b|| = " << norm / norm_b; + //tm->info() << " => ||r|| = " << norm << " ; ||r||/||b|| = " << norm / norm_b; } { tm->info() << "|| x - xe ||"; - algebra.axpy(-1., xe, x); + //algebra.axpy(-1., xe, x); - auto norm = algebra.norm2(x); - auto norm_xe = algebra.norm2(xe); + //auto norm = algebra.norm2(x); + //auto norm_xe = algebra.norm2(xe); - tm->info() << " => ||x-xe|| = " << norm << " ; ||r||/||b|| = " << norm / norm_xe; + //tm->info() << " => ||x-xe|| = " << norm << " ; ||r||/||b|| = " << norm / norm_xe; } tm->info() << " "; tm->info() << "... example finished !!!"; diff --git a/plugins/hypre/examples/hypre_example_solve.cpp b/plugins/hypre/examples/hypre_example_solve.cpp index 8f50b3c91..9766d6228 100644 --- a/plugins/hypre/examples/hypre_example_solve.cpp +++ b/plugins/hypre/examples/hypre_example_solve.cpp @@ -90,7 +90,7 @@ int test() // auto solver = Alien::Hypre::LinearSolver (options); //auto solver = Alien::Hypre::LinearSolver(); - auto solver = Alien::LinearSolver("hypre"); + auto solver = Alien::LinearSolver("../libhypre_wrapper.so"); solver.solve(A, b, x); diff --git a/plugins/hypre/examples/hypre_example_solve_move.cpp b/plugins/hypre/examples/hypre_example_solve_move.cpp index f6d2e09d4..9f52e0381 100644 --- a/plugins/hypre/examples/hypre_example_solve_move.cpp +++ b/plugins/hypre/examples/hypre_example_solve_move.cpp @@ -85,21 +85,21 @@ int test() Alien::Move::VectorData b(distribution.rowDistribution()); - Alien::Hypre::LinearAlgebra algebra; + //Alien::Hypre::LinearAlgebra algebra; - algebra.mult(A, xe, b); + //algebra.mult(A, xe, b); Alien::Move::VectorData x(distribution.colDistribution()); tm->info() << "* x = A^-1 b"; - auto options = Alien::Hypre::Options() - .numIterationsMax(100) - .stopCriteriaValue(1e-10) - .preconditioner(Alien::Hypre::OptionTypes::AMGPC) - .solver(Alien::Hypre::OptionTypes::GMRES); + //auto options = Alien::Hypre::Options() + // .numIterationsMax(100) + // .stopCriteriaValue(1e-10) + // .preconditioner(Alien::Hypre::OptionTypes::AMGPC) + // .solver(Alien::Hypre::OptionTypes::GMRES); - auto solver = Alien::Hypre::LinearSolver(options); + auto solver = Alien::LinearSolver("hypre"); solver.solve(A, b, x); @@ -107,30 +107,30 @@ int test() Alien::Move::VectorData r(distribution.colDistribution()); - { - Alien::Move::VectorData tmp(distribution.colDistribution()); - tm->info() << "t = Ax"; - algebra.mult(A, x, tmp); - tm->info() << "r = t"; - algebra.copy(tmp, r); - tm->info() << "r -= b"; - algebra.axpy(-1., b, r); - } + //{ + // Alien::Move::VectorData tmp(distribution.colDistribution()); + // tm->info() << "t = Ax"; + // algebra.mult(A, x, tmp); + // tm->info() << "r = t"; + // algebra.copy(tmp, r); + // tm->info() << "r -= b"; + // algebra.axpy(-1., b, r); + //} - auto norm = algebra.norm2(r); + //auto norm = algebra.norm2(r); - tm->info() << " => ||r|| = " << norm; + //tm->info() << " => ||r|| = " << norm; tm->info() << "* r = || x - xe ||"; - { - tm->info() << "r = x"; - algebra.copy(x, r); - tm->info() << "r -= xe"; - algebra.axpy(-1., xe, r); - } + //{ + // tm->info() << "r = x"; + // algebra.copy(x, r); + // tm->info() << "r -= xe"; + // algebra.axpy(-1., xe, r); + //} - tm->info() << " => ||r|| = " << norm; + //tm->info() << " => ||r|| = " << norm; tm->info() << " "; tm->info() << "... example finished !!!"; diff --git a/plugins/hypre/tests/move_use.cpp b/plugins/hypre/tests/move_use.cpp index 58aec500f..db1a52960 100644 --- a/plugins/hypre/tests/move_use.cpp +++ b/plugins/hypre/tests/move_use.cpp @@ -23,8 +23,8 @@ #include #include -#include -#include +#include +#include class SimpleLinearProblemFixtureMove : public ::testing::Test { @@ -88,7 +88,7 @@ TEST_F(SimpleLinearProblemFixtureMove, SimpleSolve) { Alien::Move::VectorData x(m_matrix.distribution().rowDistribution()); - auto solver = Alien::Hypre::LinearSolver(); + auto solver = Alien::LinearSolver("hypre"); ASSERT_TRUE(solver.solve(m_matrix, m_rhs, x)); } @@ -97,13 +97,13 @@ TEST_F(SimpleLinearProblemFixtureMove, ParametrizedSolve) { Alien::Move::VectorData x(m_matrix.distribution().rowDistribution()); - auto options = Alien::Hypre::Options() - .numIterationsMax(10) - .stopCriteriaValue(1e-10) - .preconditioner(Alien::Hypre::OptionTypes::AMGPC) - .solver(Alien::Hypre::OptionTypes::GMRES); + //auto options = Alien::Hypre::Options() + // .numIterationsMax(10) + // .stopCriteriaValue(1e-10) + // .preconditioner(Alien::Hypre::OptionTypes::AMGPC) + // .solver(Alien::Hypre::OptionTypes::GMRES); - auto solver = Alien::Hypre::LinearSolver(options); + auto solver = Alien::LinearSolver("hypre"); ASSERT_TRUE(solver.solve(m_matrix, m_rhs, x)); } @@ -120,12 +120,12 @@ TEST_F(SimpleLinearProblemFixtureMove, MultipleSolve) } x = w.release(); - auto options = Alien::Hypre::Options() - .numIterationsMax(1) - .stopCriteriaValue(1e-20) - .preconditioner(Alien::Hypre::OptionTypes::NoPC) - .solver(Alien::Hypre::OptionTypes::CG); - auto solver = Alien::Hypre::LinearSolver(options); + //auto options = Alien::Hypre::Options() + // .numIterationsMax(1) + // .stopCriteriaValue(1e-20) + // .preconditioner(Alien::Hypre::OptionTypes::NoPC) + // .solver(Alien::Hypre::OptionTypes::CG); + auto solver = Alien::LinearSolver("hypre"); EXPECT_FALSE(solver.solve(m_matrix, m_rhs, x)); std::cerr << "Residual " << solver.getStatus().residual << " after " << solver.getStatus().iteration_count << std::endl; @@ -133,7 +133,7 @@ TEST_F(SimpleLinearProblemFixtureMove, MultipleSolve) { // Second call, should succeed - auto solver = Alien::Hypre::LinearSolver(); + auto solver = Alien::LinearSolver("hypre"); ASSERT_TRUE(solver.solve(m_matrix, m_rhs, x)); } } diff --git a/plugins/hypre/tests/ref_use.cpp b/plugins/hypre/tests/ref_use.cpp index 6552194ae..6cce4a9e8 100644 --- a/plugins/hypre/tests/ref_use.cpp +++ b/plugins/hypre/tests/ref_use.cpp @@ -23,8 +23,8 @@ #include #include -#include -#include +#include +#include class SimpleLinearProblemFixtureRef : public ::testing::Test { @@ -76,7 +76,7 @@ TEST_F(SimpleLinearProblemFixtureRef, SimpleSolve) { Alien::Vector x(m_matrix.distribution().rowDistribution()); - auto solver = Alien::Hypre::LinearSolver(); + auto solver = Alien::LinearSolver("hypre"); ASSERT_TRUE(solver.solve(m_matrix, m_rhs, x)); } @@ -85,13 +85,13 @@ TEST_F(SimpleLinearProblemFixtureRef, ParametrizedSolve) { Alien::Vector x(m_matrix.distribution().rowDistribution()); - auto options = Alien::Hypre::Options() - .numIterationsMax(10) - .stopCriteriaValue(1e-10) - .preconditioner(Alien::Hypre::OptionTypes::AMGPC) - .solver(Alien::Hypre::OptionTypes::GMRES); + //auto options = Alien::Hypre::Options() + // .numIterationsMax(10) + // .stopCriteriaValue(1e-10) + // .preconditioner(Alien::Hypre::OptionTypes::AMGPC) + // .solver(Alien::Hypre::OptionTypes::GMRES); - auto solver = Alien::Hypre::LinearSolver(options); + auto solver = Alien::LinearSolver("hypre"); ASSERT_TRUE(solver.solve(m_matrix, m_rhs, x)); } @@ -108,13 +108,13 @@ TEST_F(SimpleLinearProblemFixtureRef, MultipleSolve) } { // First call, should fail - auto options = Alien::Hypre::Options() - .numIterationsMax(1) - .stopCriteriaValue(1e-20) - .preconditioner(Alien::Hypre::OptionTypes::NoPC) - .solver(Alien::Hypre::OptionTypes::CG); + //auto options = Alien::Hypre::Options() + // .numIterationsMax(1) + // .stopCriteriaValue(1e-20) + // .preconditioner(Alien::Hypre::OptionTypes::NoPC) + // .solver(Alien::Hypre::OptionTypes::CG); - auto solver = Alien::Hypre::LinearSolver(options); + auto solver = Alien::LinearSolver("hypre"); EXPECT_FALSE(solver.solve(m_matrix, m_rhs, x)); } @@ -128,7 +128,7 @@ TEST_F(SimpleLinearProblemFixtureRef, MultipleSolve) { // Second call, should succeed - auto solver = Alien::Hypre::LinearSolver(); + auto solver = Alien::LinearSolver("hypre"); ASSERT_TRUE(solver.solve(m_matrix, m_rhs, x)); } } diff --git a/src/core/alien/core/backend/BackEnd.h b/src/core/alien/core/backend/BackEnd.h index ad8a36db0..e32450c7d 100644 --- a/src/core/alien/core/backend/BackEnd.h +++ b/src/core/alien/core/backend/BackEnd.h @@ -22,6 +22,8 @@ #include #include +#include + /*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/ @@ -44,6 +46,11 @@ class IInternalLinearSolver; class ILinearSolver; class ILinearAlgebra; +class IMatrixConverter; +class IVectorConverter; +class IMatrixImpl; +class IVectorImpl; + namespace BackEnd { @@ -113,10 +120,19 @@ namespace BackEnd class IPlugin { + typedef IMatrixImpl* (*MatrixFactory) (void); + typedef IVectorImpl* (*VectorFactory) (void); + public: virtual std::unique_ptr solver_factory() = 0; virtual std::unique_ptr solver_factory(const Alien::BackEnd::Options& options) = 0; - BackEndId name(); + + virtual void registerMatrixConverters(std::map, IMatrixConverter>& converters) = 0; + virtual void registerVectorConverters(std::map, IVectorConverter>& converters) = 0; + virtual void registerMatrixFactory(std::map& matrixFactories) = 0; + virtual void registerVectorFactory(std::map& vectorFactories) = 0; + + virtual BackEndId name() = 0; }; } // namespace BackEnd diff --git a/src/core/alien/core/backend/LinearSolver.cc b/src/core/alien/core/backend/LinearSolver.cc index 4c0abda7a..e8161cae3 100644 --- a/src/core/alien/core/backend/LinearSolver.cc +++ b/src/core/alien/core/backend/LinearSolver.cc @@ -39,11 +39,11 @@ namespace Alien /*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/ -LinearSolver::LinearSolver(BackEndId backEndId) +LinearSolver::LinearSolver(std::string soFile) { // FIXME: add error checking - m_handle = dlopen(backEndId.localstr(), RTLD_NOW | RTLD_LOCAL); + m_handle = dlopen(soFile.c_str(), RTLD_NOW | RTLD_LOCAL); m_plugin_create = (BackEnd::IPlugin*(*)()) dlsym(m_handle, "create"); m_plugin_destroy = (void (*)(BackEnd::IPlugin*)) dlsym(m_handle, "destroy"); diff --git a/src/core/alien/core/backend/LinearSolver.h b/src/core/alien/core/backend/LinearSolver.h index 6d1a6efd1..c2a02db0b 100644 --- a/src/core/alien/core/backend/LinearSolver.h +++ b/src/core/alien/core/backend/LinearSolver.h @@ -85,7 +85,7 @@ class ALIEN_EXPORT LinearSolver : public ILinearSolver //: m_solver(AlgebraTraits::solver_factory(...)) //{} - LinearSolver(BackEndId backEndId); + LinearSolver(std::string soFile); //! Free resources virtual ~LinearSolver(); From 8265573fbae0d4798931208cc4e89aa5f9474ec5 Mon Sep 17 00:00:00 2001 From: Louis Goyard Date: Mon, 27 Jun 2022 15:10:56 +0200 Subject: [PATCH 09/13] WIP: register converters and factories at plugin load --- plugins/hypre/CMakeLists.txt | 4 ++ plugins/hypre/include/alien/hypre/backend.h | 6 ++ plugins/hypre/src/backend.cpp | 34 ++++++++++ .../converters/hypre_to_simplecsr_vector.cpp | 44 +----------- .../converters/hypre_to_simplecsr_vector.h | 43 ++++++++++++ .../converters/simplecsr_to_hypre_matrix.cpp | 67 +++++-------------- .../converters/simplecsr_to_hypre_matrix.h | 52 ++++++++++++++ .../converters/simplecsr_to_hypre_vector.cpp | 42 +----------- .../converters/simplecsr_to_hypre_vector.h | 43 ++++++++++++ plugins/hypre/src/hypre_matrix.h | 2 + plugins/hypre/src/hypre_vector.h | 2 + src/core/alien/core/backend/BackEnd.h | 22 ++++-- src/core/alien/core/backend/LinearSolver.cc | 5 +- src/core/alien/core/impl/MultiMatrixImpl.cc | 5 +- src/core/alien/core/impl/MultiMatrixImpl.h | 5 ++ src/core/alien/core/impl/MultiVectorImpl.cc | 5 +- src/core/alien/core/impl/MultiVectorImpl.h | 5 ++ 17 files changed, 245 insertions(+), 141 deletions(-) create mode 100644 plugins/hypre/src/backend.cpp create mode 100644 plugins/hypre/src/converters/hypre_to_simplecsr_vector.h create mode 100644 plugins/hypre/src/converters/simplecsr_to_hypre_matrix.h create mode 100644 plugins/hypre/src/converters/simplecsr_to_hypre_vector.h diff --git a/plugins/hypre/CMakeLists.txt b/plugins/hypre/CMakeLists.txt index f2184d5d3..3ba3fd352 100644 --- a/plugins/hypre/CMakeLists.txt +++ b/plugins/hypre/CMakeLists.txt @@ -15,9 +15,13 @@ add_library(hypre_wrapper src/hypre_matrix.cpp src/hypre_matrix.h src/hypre_linear_solver.cpp + src/backend.cpp src/converters/hypre_to_simplecsr_vector.cpp + src/converters/hypre_to_simplecsr_vector.h src/converters/simplecsr_to_hypre_vector.cpp + src/converters/simplecsr_to_hypre_vector.h src/converters/simplecsr_to_hypre_matrix.cpp + src/converters/simplecsr_to_hypre_matrix.h src/hypre_linear_solver.h) target_link_libraries(hypre_wrapper PUBLIC diff --git a/plugins/hypre/include/alien/hypre/backend.h b/plugins/hypre/include/alien/hypre/backend.h index 9692bf2ea..ed0f61e20 100644 --- a/plugins/hypre/include/alien/hypre/backend.h +++ b/plugins/hypre/include/alien/hypre/backend.h @@ -65,6 +65,12 @@ class Plugin : public BackEnd::IPlugin return LinearSolverFactory(options); } + void registerMatrixConverters(std::map, IMatrixConverter*>& converters); + void registerVectorConverters(std::map, IVectorConverter*>& converters); + void registerMatrixFactory(std::map& matrixFactories); + void registerVectorFactory(std::map& vectorFactories); + + BackEndId name() { return "hypre"; } }; diff --git a/plugins/hypre/src/backend.cpp b/plugins/hypre/src/backend.cpp new file mode 100644 index 000000000..ce3a61df0 --- /dev/null +++ b/plugins/hypre/src/backend.cpp @@ -0,0 +1,34 @@ +#include "../include/alien/hypre/backend.h" + +#include "converters/simplecsr_to_hypre_matrix.h" +#include "converters/simplecsr_to_hypre_vector.h" +#include "converters/hypre_to_simplecsr_vector.h" + +#include "hypre_matrix.h" +#include "hypre_vector.h" + +namespace Alien +{ + +void Plugin::registerMatrixConverters(std::map, IMatrixConverter*>& converters) +{ + REGISTER_PLUGIN_MATRIX_CONVERTER("hypre", "simplecsr", SimpleCSR_to_Hypre_MatrixConverter); +} + +void Plugin::registerVectorConverters(std::map, IVectorConverter*>& converters) +{ + REGISTER_PLUGIN_VECTOR_CONVERTER("hypre", "simplecsr", SimpleCSR_to_Hypre_VectorConverter); + REGISTER_PLUGIN_VECTOR_CONVERTER("simplecsr", "hypre", Hypre_to_SimpleCSR_VectorConverter); +} + +void Plugin::registerMatrixFactory(std::map& matrixFactories) +{ + REGISTER_PLUGIN_MATRIX_FACTORY("hypre", matrixFactories, &matrix_factory); +} + +void Plugin::registerVectorFactory(std::map& vectorFactories) +{ + REGISTER_PLUGIN_VECTOR_FACTORY("hypre", vectorFactories, &vector_factory); +} + +} diff --git a/plugins/hypre/src/converters/hypre_to_simplecsr_vector.cpp b/plugins/hypre/src/converters/hypre_to_simplecsr_vector.cpp index cab5fe0ea..675b4eb53 100644 --- a/plugins/hypre/src/converters/hypre_to_simplecsr_vector.cpp +++ b/plugins/hypre/src/converters/hypre_to_simplecsr_vector.cpp @@ -1,44 +1,4 @@ -/* - * Copyright 2020 IFPEN-CEA - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * SPDX-License-Identifier: Apache-2.0 - */ - -#include "../hypre_vector.h" - -#include -#include -#include -#include - -#include - -class Hypre_to_SimpleCSR_VectorConverter : public Alien::IVectorConverter -{ - public: - Hypre_to_SimpleCSR_VectorConverter() {} - - virtual ~Hypre_to_SimpleCSR_VectorConverter() {} - - public: - Alien::BackEndId sourceBackend() const { return "hypre"; } - - Alien::BackEndId targetBackend() const { return Alien::AlgebraTraits::name(); } - - void convert(const Alien::IVectorImpl* sourceImpl, Alien::IVectorImpl* targetImpl) const; -}; +#include "hypre_to_simplecsr_vector.h" void Hypre_to_SimpleCSR_VectorConverter::convert( const Alien::IVectorImpl* sourceImpl, Alien::IVectorImpl* targetImpl) const @@ -53,5 +13,3 @@ const Alien::IVectorImpl* sourceImpl, Alien::IVectorImpl* targetImpl) const v.getValues(values); } - -REGISTER_VECTOR_CONVERTER(Hypre_to_SimpleCSR_VectorConverter); diff --git a/plugins/hypre/src/converters/hypre_to_simplecsr_vector.h b/plugins/hypre/src/converters/hypre_to_simplecsr_vector.h new file mode 100644 index 000000000..4f42d6e8f --- /dev/null +++ b/plugins/hypre/src/converters/hypre_to_simplecsr_vector.h @@ -0,0 +1,43 @@ +/* + * Copyright 2020 IFPEN-CEA + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#pragma once + +#include "../hypre_vector.h" + +#include +#include +#include +#include + +#include + +class Hypre_to_SimpleCSR_VectorConverter : public Alien::IVectorConverter +{ + public: + Hypre_to_SimpleCSR_VectorConverter() {} + + virtual ~Hypre_to_SimpleCSR_VectorConverter() {} + + public: + Alien::BackEndId sourceBackend() const { return "hypre"; } + + Alien::BackEndId targetBackend() const { return Alien::AlgebraTraits::name(); } + + void convert(const Alien::IVectorImpl* sourceImpl, Alien::IVectorImpl* targetImpl) const; +}; diff --git a/plugins/hypre/src/converters/simplecsr_to_hypre_matrix.cpp b/plugins/hypre/src/converters/simplecsr_to_hypre_matrix.cpp index 7859d3139..ac29ba0c4 100644 --- a/plugins/hypre/src/converters/simplecsr_to_hypre_matrix.cpp +++ b/plugins/hypre/src/converters/simplecsr_to_hypre_matrix.cpp @@ -16,55 +16,7 @@ * SPDX-License-Identifier: Apache-2.0 */ -#include "../hypre_matrix.h" - -#include - -#include -#include -#include -#include - -#include - -class SimpleCSR_to_Hypre_MatrixConverter : public Alien::IMatrixConverter -{ - public: - SimpleCSR_to_Hypre_MatrixConverter() {} - - virtual ~SimpleCSR_to_Hypre_MatrixConverter() {} - - public: - BackEndId sourceBackend() const - { - return Alien::AlgebraTraits::name(); - } - - BackEndId targetBackend() const { return "hypre"; } - - void convert(const Alien::IMatrixImpl* sourceImpl, Alien::IMatrixImpl* targetImpl) const; - - void _build(const Alien::SimpleCSRMatrix& sourceImpl, Alien::Matrix& targetImpl) const; - - void _buildBlock(const Alien::SimpleCSRMatrix& sourceImpl, Alien::Matrix& targetImpl) const; -}; - -void SimpleCSR_to_Hypre_MatrixConverter::convert(const IMatrixImpl* sourceImpl, IMatrixImpl* targetImpl) const -{ - const auto& v = cast>(sourceImpl, sourceBackend()); - auto& v2 = cast(targetImpl, targetBackend()); - - alien_debug([&] { - cout() << "Converting Alien::SimpleCSRMatrix: " << &v << " to Hypre::Matrix " << &v2; - }); - - if (targetImpl->block()) - _buildBlock(v, v2); - else if (targetImpl->vblock()) - throw Arccore::FatalErrorException(A_FUNCINFO, "Block sizes are variable - builds not yet implemented"); - else - _build(v, v2); -} +#include "simplecsr_to_hypre_matrix.h" void SimpleCSR_to_Hypre_MatrixConverter::_build(const Alien::SimpleCSRMatrix& sourceImpl, Alien::Matrix& targetImpl) const @@ -176,3 +128,20 @@ void SimpleCSR_to_Hypre_MatrixConverter::_buildBlock(const Alien::SimpleCSRMatri targetImpl.assemble(); } + +void SimpleCSR_to_Hypre_MatrixConverter::convert(const IMatrixImpl* sourceImpl, IMatrixImpl* targetImpl) const +{ + const auto& v = cast>(sourceImpl, sourceBackend()); + auto& v2 = cast(targetImpl, targetBackend()); + + alien_debug([&] { + cout() << "Converting Alien::SimpleCSRMatrix: " << &v << " to Hypre::Matrix " << &v2; + }); + + if (targetImpl->block()) + _buildBlock(v, v2); + else if (targetImpl->vblock()) + throw Arccore::FatalErrorException(A_FUNCINFO, "Block sizes are variable - builds not yet implemented"); + else + _build(v, v2); +} diff --git a/plugins/hypre/src/converters/simplecsr_to_hypre_matrix.h b/plugins/hypre/src/converters/simplecsr_to_hypre_matrix.h new file mode 100644 index 000000000..15757917c --- /dev/null +++ b/plugins/hypre/src/converters/simplecsr_to_hypre_matrix.h @@ -0,0 +1,52 @@ +/* + * Copyright 2020 IFPEN-CEA + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#pragma once + +#include "../hypre_matrix.h" + +#include + +#include +#include +#include +#include + +#include + +class SimpleCSR_to_Hypre_MatrixConverter : public Alien::IMatrixConverter +{ + public: + SimpleCSR_to_Hypre_MatrixConverter() {} + + virtual ~SimpleCSR_to_Hypre_MatrixConverter() {} + + public: + BackEndId sourceBackend() const + { + return Alien::AlgebraTraits::name(); + } + + BackEndId targetBackend() const { return "hypre"; } + + void convert(const Alien::IMatrixImpl* sourceImpl, Alien::IMatrixImpl* targetImpl) const; + + void _build(const Alien::SimpleCSRMatrix& sourceImpl, Alien::Matrix& targetImpl) const; + + void _buildBlock(const Alien::SimpleCSRMatrix& sourceImpl, Alien::Matrix& targetImpl) const; +}; diff --git a/plugins/hypre/src/converters/simplecsr_to_hypre_vector.cpp b/plugins/hypre/src/converters/simplecsr_to_hypre_vector.cpp index 8a4f0e4e8..2b4d3e053 100644 --- a/plugins/hypre/src/converters/simplecsr_to_hypre_vector.cpp +++ b/plugins/hypre/src/converters/simplecsr_to_hypre_vector.cpp @@ -1,44 +1,4 @@ -/* - * Copyright 2020 IFPEN-CEA - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * SPDX-License-Identifier: Apache-2.0 - */ - -#include "../hypre_vector.h" - -#include -#include -#include -#include - -#include - -class SimpleCSR_to_Hypre_VectorConverter : public Alien::IVectorConverter -{ - public: - SimpleCSR_to_Hypre_VectorConverter() {} - - virtual ~SimpleCSR_to_Hypre_VectorConverter() {} - - public: - Alien::BackEndId sourceBackend() const { return Alien::AlgebraTraits::name(); } - - Alien::BackEndId targetBackend() const { return "hypre"; } - - void convert(const Alien::IVectorImpl* sourceImpl, Alien::IVectorImpl* targetImpl) const; -}; +#include "simplecsr_to_hypre_vector.h" void SimpleCSR_to_Hypre_VectorConverter::convert(const Alien::IVectorImpl* sourceImpl, Alien::IVectorImpl* targetImpl) const diff --git a/plugins/hypre/src/converters/simplecsr_to_hypre_vector.h b/plugins/hypre/src/converters/simplecsr_to_hypre_vector.h new file mode 100644 index 000000000..869adf9e0 --- /dev/null +++ b/plugins/hypre/src/converters/simplecsr_to_hypre_vector.h @@ -0,0 +1,43 @@ +/* + * Copyright 2020 IFPEN-CEA + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#pragma once + +#include "../hypre_vector.h" + +#include +#include +#include +#include + +#include + +class SimpleCSR_to_Hypre_VectorConverter : public Alien::IVectorConverter +{ + public: + SimpleCSR_to_Hypre_VectorConverter() {} + + virtual ~SimpleCSR_to_Hypre_VectorConverter() {} + + public: + Alien::BackEndId sourceBackend() const { return Alien::AlgebraTraits::name(); } + + Alien::BackEndId targetBackend() const { return "hypre"; } + + void convert(const Alien::IVectorImpl* sourceImpl, Alien::IVectorImpl* targetImpl) const; +}; diff --git a/plugins/hypre/src/hypre_matrix.h b/plugins/hypre/src/hypre_matrix.h index 95e7fc010..4f2fdb289 100644 --- a/plugins/hypre/src/hypre_matrix.h +++ b/plugins/hypre/src/hypre_matrix.h @@ -49,4 +49,6 @@ class Matrix : public IMatrixImpl MPI_Comm m_comm; }; +inline IMatrixImpl* matrix_factory(const MultiMatrixImpl* multiImpl) { return new Matrix(multiImpl); }; + } // namespace Alien diff --git a/plugins/hypre/src/hypre_vector.h b/plugins/hypre/src/hypre_vector.h index bc91248c1..cfbf98364 100644 --- a/plugins/hypre/src/hypre_vector.h +++ b/plugins/hypre/src/hypre_vector.h @@ -53,4 +53,6 @@ class Vector : public IVectorImpl Arccore::UniqueArray m_rows; }; +inline IVectorImpl* vector_factory(const MultiVectorImpl* multiImpl) { return new Vector(multiImpl); }; + } // namespace Alien::Hypre diff --git a/src/core/alien/core/backend/BackEnd.h b/src/core/alien/core/backend/BackEnd.h index e32450c7d..975b09a82 100644 --- a/src/core/alien/core/backend/BackEnd.h +++ b/src/core/alien/core/backend/BackEnd.h @@ -51,6 +51,9 @@ class IVectorConverter; class IMatrixImpl; class IVectorImpl; +class MultiMatrixImpl; +class MultiVectorImpl; + namespace BackEnd { @@ -120,15 +123,15 @@ namespace BackEnd class IPlugin { - typedef IMatrixImpl* (*MatrixFactory) (void); - typedef IVectorImpl* (*VectorFactory) (void); - public: + typedef IMatrixImpl* (*MatrixFactory) (const MultiMatrixImpl*); + typedef IVectorImpl* (*VectorFactory) (const MultiVectorImpl*); + virtual std::unique_ptr solver_factory() = 0; virtual std::unique_ptr solver_factory(const Alien::BackEnd::Options& options) = 0; - virtual void registerMatrixConverters(std::map, IMatrixConverter>& converters) = 0; - virtual void registerVectorConverters(std::map, IVectorConverter>& converters) = 0; + virtual void registerMatrixConverters(std::map, IMatrixConverter*>& converters) = 0; + virtual void registerVectorConverters(std::map, IVectorConverter*>& converters) = 0; virtual void registerMatrixFactory(std::map& matrixFactories) = 0; virtual void registerVectorFactory(std::map& vectorFactories) = 0; @@ -136,9 +139,18 @@ namespace BackEnd }; } // namespace BackEnd +#define REGISTER_PLUGIN_MATRIX_CONVERTER(a, b, converter) \ + converters.insert(std::map, IMatrixConverter*>::value_type(std::pair{a, b}, new converter())); + +#define REGISTER_PLUGIN_VECTOR_CONVERTER(a, b, converter) \ + converters.insert(std::map, IVectorConverter*>::value_type(std::pair{a, b}, new converter())); +#define REGISTER_PLUGIN_MATRIX_FACTORY(a, factories, factory) \ + factories.insert(std::map::value_type(a, factory)); +#define REGISTER_PLUGIN_VECTOR_FACTORY(a, factories, factory) \ + factories.insert(std::map::value_type(a, factory)); /*---------------------------------------------------------------------------*/ diff --git a/src/core/alien/core/backend/LinearSolver.cc b/src/core/alien/core/backend/LinearSolver.cc index e8161cae3..899a03bb2 100644 --- a/src/core/alien/core/backend/LinearSolver.cc +++ b/src/core/alien/core/backend/LinearSolver.cc @@ -52,7 +52,10 @@ LinearSolver::LinearSolver(std::string soFile) m_backEndId = m_solver->backEndName(); - std::cout << "BACKENDNAMEIS " << m_backEndId << std::endl; + m_plugin->registerMatrixConverters(MultiMatrixImpl::m_matrixConverters); + m_plugin->registerVectorConverters(MultiVectorImpl::m_vectorConverters); + m_plugin->registerMatrixFactory(MultiMatrixImpl::m_matrixFactory); + m_plugin->registerVectorFactory(MultiVectorImpl::m_vectorFactory); } /*---------------------------------------------------------------------------*/ diff --git a/src/core/alien/core/impl/MultiMatrixImpl.cc b/src/core/alien/core/impl/MultiMatrixImpl.cc index a12e31d73..2d61173b5 100644 --- a/src/core/alien/core/impl/MultiMatrixImpl.cc +++ b/src/core/alien/core/impl/MultiMatrixImpl.cc @@ -36,6 +36,9 @@ namespace Alien using namespace Arccore; +std::map, IMatrixConverter*> MultiMatrixImpl::m_matrixConverters; +std::map MultiMatrixImpl::m_matrixFactory; + /*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/ @@ -365,7 +368,7 @@ MultiMatrixImpl::getImpl(BackEndId backend) const auto inserter = m_impls2.insert(MultiMatrixImplMap::value_type(backend, NULL)); IMatrixImpl*& impl2 = inserter.first->second; if (impl2 == NULL) { - IMatrixImpl* new_impl = new IMatrixImpl(this); // constructeur associé à un multi-impl + IMatrixImpl* new_impl = m_matrixFactory[backend](this); // constructeur associé à un multi-impl // new_impl->init(*m_row_space.get(), // *m_col_space.get(), // m_distribution); diff --git a/src/core/alien/core/impl/MultiMatrixImpl.h b/src/core/alien/core/impl/MultiMatrixImpl.h index 23988e0ca..d4ec144b9 100644 --- a/src/core/alien/core/impl/MultiMatrixImpl.h +++ b/src/core/alien/core/impl/MultiMatrixImpl.h @@ -248,6 +248,11 @@ class ALIEN_EXPORT MultiMatrixImpl : public TimestampMng std::shared_ptr m_rows_block; //! The variable col block datas std::shared_ptr m_cols_block; + +// FIXME: private, access through functions + public: + static std::map, IMatrixConverter*> m_matrixConverters; + static std::map m_matrixFactory; }; /*---------------------------------------------------------------------------*/ diff --git a/src/core/alien/core/impl/MultiVectorImpl.cc b/src/core/alien/core/impl/MultiVectorImpl.cc index ecbff296f..25ecfccaa 100644 --- a/src/core/alien/core/impl/MultiVectorImpl.cc +++ b/src/core/alien/core/impl/MultiVectorImpl.cc @@ -33,6 +33,9 @@ namespace Alien using namespace Arccore; +std::map, IVectorConverter*> MultiVectorImpl::m_vectorConverters; +std::map MultiVectorImpl::m_vectorFactory; + /*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/ @@ -310,7 +313,7 @@ MultiVectorImpl::getImpl(BackEndId backend) const auto inserter = m_impls2.insert(MultiVectorImplMap::value_type(backend, NULL)); IVectorImpl*& impl2 = inserter.first->second; if (impl2 == NULL) { - auto new_impl = new IVectorImpl(this); // constructeur associ� � un multi-impl + auto new_impl = m_vectorFactory[backend](this); // constructeur associ� � un multi-impl new_impl->init(*m_distribution.get(), true); impl2 = new_impl; } diff --git a/src/core/alien/core/impl/MultiVectorImpl.h b/src/core/alien/core/impl/MultiVectorImpl.h index e5cf737fb..488c807ba 100644 --- a/src/core/alien/core/impl/MultiVectorImpl.h +++ b/src/core/alien/core/impl/MultiVectorImpl.h @@ -210,6 +210,11 @@ class ALIEN_EXPORT MultiVectorImpl : public TimestampMng std::shared_ptr m_block; //! The variable block datas std::shared_ptr m_variable_block; + +// FIXME: private, access throuh functions + public: + static std::map, IVectorConverter*> m_vectorConverters; + static std::map m_vectorFactory; }; From 8eb6d5fddc162d4a36e78381a459ab8edadba998 Mon Sep 17 00:00:00 2001 From: Louis Goyard Date: Mon, 27 Jun 2022 16:55:29 +0200 Subject: [PATCH 10/13] WIP: Properly export hypre plugin's create and destroy functions Also throw exception on Multi[Matrix,Vector]Impl::getImpl if backend not found --- plugins/hypre/examples/hypre_example_solve.cpp | 3 ++- plugins/hypre/include/alien/hypre/backend.h | 11 ++--------- plugins/hypre/src/backend.cpp | 10 ++++++++++ src/core/alien/core/impl/MultiMatrixImpl.cc | 6 +++++- src/core/alien/core/impl/MultiVectorImpl.cc | 6 +++++- 5 files changed, 24 insertions(+), 12 deletions(-) diff --git a/plugins/hypre/examples/hypre_example_solve.cpp b/plugins/hypre/examples/hypre_example_solve.cpp index 9766d6228..dae8ac09c 100644 --- a/plugins/hypre/examples/hypre_example_solve.cpp +++ b/plugins/hypre/examples/hypre_example_solve.cpp @@ -50,6 +50,8 @@ int test() int lsize = dist.localRowSize(); int gsize = dist.globalRowSize(); + auto solver = Alien::LinearSolver("../libhypre_wrapper.so"); + tm->info() << "build matrix with direct matrix builder"; { Alien::DirectMatrixBuilder builder(A, Alien::DirectMatrixOptions::eResetValues); @@ -90,7 +92,6 @@ int test() // auto solver = Alien::Hypre::LinearSolver (options); //auto solver = Alien::Hypre::LinearSolver(); - auto solver = Alien::LinearSolver("../libhypre_wrapper.so"); solver.solve(A, b, x); diff --git a/plugins/hypre/include/alien/hypre/backend.h b/plugins/hypre/include/alien/hypre/backend.h index ed0f61e20..f453384a3 100644 --- a/plugins/hypre/include/alien/hypre/backend.h +++ b/plugins/hypre/include/alien/hypre/backend.h @@ -75,15 +75,8 @@ class Plugin : public BackEnd::IPlugin }; extern "C" { - inline Plugin* create() - { - return new Plugin; - } - - inline void destroy(Plugin* p) - { - delete p; - } + ALIEN_EXPORT Plugin* create(); + ALIEN_EXPORT void destroy(Plugin* p); } } // namespace Alien diff --git a/plugins/hypre/src/backend.cpp b/plugins/hypre/src/backend.cpp index ce3a61df0..9a607261d 100644 --- a/plugins/hypre/src/backend.cpp +++ b/plugins/hypre/src/backend.cpp @@ -31,4 +31,14 @@ void Plugin::registerVectorFactory(std::map& vectorFac REGISTER_PLUGIN_VECTOR_FACTORY("hypre", vectorFactories, &vector_factory); } +Plugin* create() +{ + return new Plugin; +} + +void destroy(Plugin* p) +{ + delete p; +} + } diff --git a/src/core/alien/core/impl/MultiMatrixImpl.cc b/src/core/alien/core/impl/MultiMatrixImpl.cc index 2d61173b5..a6a449c0a 100644 --- a/src/core/alien/core/impl/MultiMatrixImpl.cc +++ b/src/core/alien/core/impl/MultiMatrixImpl.cc @@ -368,7 +368,11 @@ MultiMatrixImpl::getImpl(BackEndId backend) const auto inserter = m_impls2.insert(MultiMatrixImplMap::value_type(backend, NULL)); IMatrixImpl*& impl2 = inserter.first->second; if (impl2 == NULL) { - IMatrixImpl* new_impl = m_matrixFactory[backend](this); // constructeur associé à un multi-impl + auto factory = m_matrixFactory[backend]; + if (!factory) + throw FatalErrorException("MultiMatrixImpl::getImpl(): Can't find matrix factory for backend: " + backend); + + auto new_impl = factory(this); // constructeur associé à un multi-impl // new_impl->init(*m_row_space.get(), // *m_col_space.get(), // m_distribution); diff --git a/src/core/alien/core/impl/MultiVectorImpl.cc b/src/core/alien/core/impl/MultiVectorImpl.cc index 25ecfccaa..3efcb8a4c 100644 --- a/src/core/alien/core/impl/MultiVectorImpl.cc +++ b/src/core/alien/core/impl/MultiVectorImpl.cc @@ -313,7 +313,11 @@ MultiVectorImpl::getImpl(BackEndId backend) const auto inserter = m_impls2.insert(MultiVectorImplMap::value_type(backend, NULL)); IVectorImpl*& impl2 = inserter.first->second; if (impl2 == NULL) { - auto new_impl = m_vectorFactory[backend](this); // constructeur associ� � un multi-impl + auto factory = m_vectorFactory[backend]; + if (!factory) + throw FatalErrorException("MultiVectorImpl::getImpl(): Can't find vector factory for backend: " + backend); + + auto new_impl = factory(this); // constructeur associ� � un multi-impl new_impl->init(*m_distribution.get(), true); impl2 = new_impl; } From a1f2f97bf44b6078dd02ea4a668ec6b772c83c40 Mon Sep 17 00:00:00 2001 From: Louis Goyard Date: Tue, 28 Jun 2022 11:13:12 +0200 Subject: [PATCH 11/13] WIP: make hypre example works --- plugins/hypre/examples/hypre_example_solve_move.cpp | 4 ++-- plugins/hypre/include/alien/hypre/backend.h | 4 ++-- plugins/hypre/src/backend.cpp | 10 +++++----- src/core/alien/core/backend/BackEnd.h | 10 +++++----- src/core/alien/core/backend/LinearSolver.cc | 12 ++++++++++++ src/core/alien/core/backend/LinearSolver.h | 3 +++ src/core/alien/core/impl/MultiMatrixImpl.cc | 8 +++++--- src/core/alien/core/impl/MultiMatrixImpl.h | 2 +- src/core/alien/core/impl/MultiVectorImpl.cc | 2 +- src/core/alien/core/impl/MultiVectorImpl.h | 2 +- src/core/alien/handlers/scalar/VectorReaderT.h | 2 +- src/core/alien/kernels/simple_csr/SimpleCSRMatrix.h | 5 +++++ src/core/alien/kernels/simple_csr/SimpleCSRVector.h | 5 +++++ .../alien/move/handlers/sub_matrix/Extraction.cc | 2 +- src/refsemantic/tests/TestCompositeMatrix.cc | 4 ++-- src/refsemantic/tests/TestCompositeVector.cc | 4 ++-- 16 files changed, 53 insertions(+), 26 deletions(-) diff --git a/plugins/hypre/examples/hypre_example_solve_move.cpp b/plugins/hypre/examples/hypre_example_solve_move.cpp index 9f52e0381..532b9263a 100644 --- a/plugins/hypre/examples/hypre_example_solve_move.cpp +++ b/plugins/hypre/examples/hypre_example_solve_move.cpp @@ -52,6 +52,8 @@ int test() int lsize = dist.localRowSize(); int gsize = dist.globalRowSize(); + auto solver = Alien::LinearSolver("../libhypre_wrapper.so"); + tm->info() << "build matrix with direct matrix builder"; { Alien::Move::DirectMatrixBuilder builder(std::move(A), Alien::DirectMatrixOptions::eResetValues); @@ -99,8 +101,6 @@ int test() // .preconditioner(Alien::Hypre::OptionTypes::AMGPC) // .solver(Alien::Hypre::OptionTypes::GMRES); - auto solver = Alien::LinearSolver("hypre"); - solver.solve(A, b, x); tm->info() << "* r = Ax - b"; diff --git a/plugins/hypre/include/alien/hypre/backend.h b/plugins/hypre/include/alien/hypre/backend.h index f453384a3..4fa10e89f 100644 --- a/plugins/hypre/include/alien/hypre/backend.h +++ b/plugins/hypre/include/alien/hypre/backend.h @@ -67,8 +67,8 @@ class Plugin : public BackEnd::IPlugin void registerMatrixConverters(std::map, IMatrixConverter*>& converters); void registerVectorConverters(std::map, IVectorConverter*>& converters); - void registerMatrixFactory(std::map& matrixFactories); - void registerVectorFactory(std::map& vectorFactories); + void registerMatrixFactory(std::map& matrixFactories); + void registerVectorFactory(std::map& vectorFactories); BackEndId name() { return "hypre"; } diff --git a/plugins/hypre/src/backend.cpp b/plugins/hypre/src/backend.cpp index 9a607261d..db263c2a9 100644 --- a/plugins/hypre/src/backend.cpp +++ b/plugins/hypre/src/backend.cpp @@ -12,21 +12,21 @@ namespace Alien void Plugin::registerMatrixConverters(std::map, IMatrixConverter*>& converters) { - REGISTER_PLUGIN_MATRIX_CONVERTER("hypre", "simplecsr", SimpleCSR_to_Hypre_MatrixConverter); + REGISTER_PLUGIN_MATRIX_CONVERTER("simplecsr", "hypre", SimpleCSR_to_Hypre_MatrixConverter); } void Plugin::registerVectorConverters(std::map, IVectorConverter*>& converters) { - REGISTER_PLUGIN_VECTOR_CONVERTER("hypre", "simplecsr", SimpleCSR_to_Hypre_VectorConverter); - REGISTER_PLUGIN_VECTOR_CONVERTER("simplecsr", "hypre", Hypre_to_SimpleCSR_VectorConverter); + REGISTER_PLUGIN_VECTOR_CONVERTER("simplecsr", "hypre", SimpleCSR_to_Hypre_VectorConverter); + REGISTER_PLUGIN_VECTOR_CONVERTER("hypre", "simplecsr", Hypre_to_SimpleCSR_VectorConverter); } -void Plugin::registerMatrixFactory(std::map& matrixFactories) +void Plugin::registerMatrixFactory(std::map& matrixFactories) { REGISTER_PLUGIN_MATRIX_FACTORY("hypre", matrixFactories, &matrix_factory); } -void Plugin::registerVectorFactory(std::map& vectorFactories) +void Plugin::registerVectorFactory(std::map& vectorFactories) { REGISTER_PLUGIN_VECTOR_FACTORY("hypre", vectorFactories, &vector_factory); } diff --git a/src/core/alien/core/backend/BackEnd.h b/src/core/alien/core/backend/BackEnd.h index 975b09a82..026046271 100644 --- a/src/core/alien/core/backend/BackEnd.h +++ b/src/core/alien/core/backend/BackEnd.h @@ -121,12 +121,12 @@ namespace BackEnd } }; + typedef IMatrixImpl* (*MatrixFactory) (const MultiMatrixImpl*); + typedef IVectorImpl* (*VectorFactory) (const MultiVectorImpl*); + class IPlugin { public: - typedef IMatrixImpl* (*MatrixFactory) (const MultiMatrixImpl*); - typedef IVectorImpl* (*VectorFactory) (const MultiVectorImpl*); - virtual std::unique_ptr solver_factory() = 0; virtual std::unique_ptr solver_factory(const Alien::BackEnd::Options& options) = 0; @@ -147,10 +147,10 @@ namespace BackEnd #define REGISTER_PLUGIN_MATRIX_FACTORY(a, factories, factory) \ - factories.insert(std::map::value_type(a, factory)); + factories.insert(std::map::value_type(a, factory)); #define REGISTER_PLUGIN_VECTOR_FACTORY(a, factories, factory) \ - factories.insert(std::map::value_type(a, factory)); + factories.insert(std::map::value_type(a, factory)); /*---------------------------------------------------------------------------*/ diff --git a/src/core/alien/core/backend/LinearSolver.cc b/src/core/alien/core/backend/LinearSolver.cc index 899a03bb2..8d4cee5d9 100644 --- a/src/core/alien/core/backend/LinearSolver.cc +++ b/src/core/alien/core/backend/LinearSolver.cc @@ -30,6 +30,9 @@ #include #include +#include +#include + /*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/ @@ -43,6 +46,15 @@ LinearSolver::LinearSolver(std::string soFile) { // FIXME: add error checking + if (!m_initialized) + { + // FIXME: add others kernels + REGISTER_PLUGIN_MATRIX_FACTORY("simplecsr", MultiMatrixImpl::m_matrixFactory, &SimpleCSRInternal::matrix_factory); + REGISTER_PLUGIN_VECTOR_FACTORY("simplecsr", MultiVectorImpl::m_vectorFactory, &SimpleCSRInternal::vector_factory); + + m_initialized = true; + } + m_handle = dlopen(soFile.c_str(), RTLD_NOW | RTLD_LOCAL); m_plugin_create = (BackEnd::IPlugin*(*)()) dlsym(m_handle, "create"); m_plugin_destroy = (void (*)(BackEnd::IPlugin*)) dlsym(m_handle, "destroy"); diff --git a/src/core/alien/core/backend/LinearSolver.h b/src/core/alien/core/backend/LinearSolver.h index c2a02db0b..90c7cf784 100644 --- a/src/core/alien/core/backend/LinearSolver.h +++ b/src/core/alien/core/backend/LinearSolver.h @@ -176,6 +176,9 @@ class ALIEN_EXPORT LinearSolver : public ILinearSolver std::unique_ptr m_plugin; BackEnd::IPlugin* (*m_plugin_create)(); void (*m_plugin_destroy)(BackEnd::IPlugin*); + + //FIXME: do it differently + bool m_initialized{false}; }; /*---------------------------------------------------------------------------*/ diff --git a/src/core/alien/core/impl/MultiMatrixImpl.cc b/src/core/alien/core/impl/MultiMatrixImpl.cc index a6a449c0a..1ed2ca95e 100644 --- a/src/core/alien/core/impl/MultiMatrixImpl.cc +++ b/src/core/alien/core/impl/MultiMatrixImpl.cc @@ -37,7 +37,7 @@ namespace Alien using namespace Arccore; std::map, IMatrixConverter*> MultiMatrixImpl::m_matrixConverters; -std::map MultiMatrixImpl::m_matrixFactory; +std::map MultiMatrixImpl::m_matrixFactory; /*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/ @@ -240,7 +240,8 @@ void MultiMatrixImpl::updateImpl(IMatrixImpl* target) const if (candidate->timestamp() != timestamp()) continue; auto* converter = - MatrixConverterRegisterer::getConverter(candidate->backend(), target->backend()); + //MatrixConverterRegisterer::getConverter(candidate->backend(), target->backend()); + MultiMatrixImpl::m_matrixConverters[std::make_pair(candidate->backend(), target->backend())]; // If no converter is found, we continue if (converter == nullptr) continue; @@ -285,7 +286,8 @@ void MultiMatrixImpl::updateImpl(IMatrixImpl* target) const // Checking that we have a converter from simplecsr to the requested implementation auto* simplecsr_target = - MatrixConverterRegisterer::getConverter(simplecsr->backend(), target->backend()); + //MatrixConverterRegisterer::getConverter(simplecsr->backend(), target->backend()); + MultiMatrixImpl::m_matrixConverters[std::pair(simplecsr->backend(), target->backend())]; // If not, throw error if (simplecsr_target == nullptr) diff --git a/src/core/alien/core/impl/MultiMatrixImpl.h b/src/core/alien/core/impl/MultiMatrixImpl.h index d4ec144b9..b700e7f54 100644 --- a/src/core/alien/core/impl/MultiMatrixImpl.h +++ b/src/core/alien/core/impl/MultiMatrixImpl.h @@ -252,7 +252,7 @@ class ALIEN_EXPORT MultiMatrixImpl : public TimestampMng // FIXME: private, access through functions public: static std::map, IMatrixConverter*> m_matrixConverters; - static std::map m_matrixFactory; + static std::map m_matrixFactory; }; /*---------------------------------------------------------------------------*/ diff --git a/src/core/alien/core/impl/MultiVectorImpl.cc b/src/core/alien/core/impl/MultiVectorImpl.cc index 3efcb8a4c..99e8dc020 100644 --- a/src/core/alien/core/impl/MultiVectorImpl.cc +++ b/src/core/alien/core/impl/MultiVectorImpl.cc @@ -34,7 +34,7 @@ namespace Alien using namespace Arccore; std::map, IVectorConverter*> MultiVectorImpl::m_vectorConverters; -std::map MultiVectorImpl::m_vectorFactory; +std::map MultiVectorImpl::m_vectorFactory; /*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/ diff --git a/src/core/alien/core/impl/MultiVectorImpl.h b/src/core/alien/core/impl/MultiVectorImpl.h index 488c807ba..e6ca7c86e 100644 --- a/src/core/alien/core/impl/MultiVectorImpl.h +++ b/src/core/alien/core/impl/MultiVectorImpl.h @@ -214,7 +214,7 @@ class ALIEN_EXPORT MultiVectorImpl : public TimestampMng // FIXME: private, access throuh functions public: static std::map, IVectorConverter*> m_vectorConverters; - static std::map m_vectorFactory; + static std::map m_vectorFactory; }; diff --git a/src/core/alien/handlers/scalar/VectorReaderT.h b/src/core/alien/handlers/scalar/VectorReaderT.h index 9a695219b..7b0528fc0 100644 --- a/src/core/alien/handlers/scalar/VectorReaderT.h +++ b/src/core/alien/handlers/scalar/VectorReaderT.h @@ -42,7 +42,7 @@ namespace Common template VectorReaderT::VectorReaderT(const IVector& vector) { - const auto& v = dynamic_cast&>(vector.impl()->get("simple_csr")); + const auto& v = dynamic_cast&>(vector.impl()->get("simplecsr")); m_values = v.fullValues(); m_local_offset = v.distribution().offset(); } diff --git a/src/core/alien/kernels/simple_csr/SimpleCSRMatrix.h b/src/core/alien/kernels/simple_csr/SimpleCSRMatrix.h index de335a2b2..18590d4fb 100644 --- a/src/core/alien/kernels/simple_csr/SimpleCSRMatrix.h +++ b/src/core/alien/kernels/simple_csr/SimpleCSRMatrix.h @@ -339,6 +339,11 @@ class SimpleCSRMatrix : public IMatrixImpl } }; +namespace SimpleCSRInternal +{ + inline IMatrixImpl* matrix_factory(const MultiMatrixImpl* multiImpl) { return new SimpleCSRMatrix(multiImpl); }; +} + /*---------------------------------------------------------------------------*/ } // namespace Alien diff --git a/src/core/alien/kernels/simple_csr/SimpleCSRVector.h b/src/core/alien/kernels/simple_csr/SimpleCSRVector.h index 7c35fba04..21541037d 100644 --- a/src/core/alien/kernels/simple_csr/SimpleCSRVector.h +++ b/src/core/alien/kernels/simple_csr/SimpleCSRVector.h @@ -189,6 +189,11 @@ class SimpleCSRVector : public IVectorImpl VectorDistribution m_own_distribution; }; +namespace SimpleCSRInternal +{ + inline IVectorImpl* vector_factory(const MultiVectorImpl* multiImpl) { return new SimpleCSRVector(multiImpl); }; +} + /*---------------------------------------------------------------------------*/ } // namespace Alien diff --git a/src/movesemantic/alien/move/handlers/sub_matrix/Extraction.cc b/src/movesemantic/alien/move/handlers/sub_matrix/Extraction.cc index 8b7d24493..49afc24ff 100644 --- a/src/movesemantic/alien/move/handlers/sub_matrix/Extraction.cc +++ b/src/movesemantic/alien/move/handlers/sub_matrix/Extraction.cc @@ -51,7 +51,7 @@ SubMatrix::extractRange(const IMatrix& matrix, const ExtractionIndices& indices) { const SimpleCSRMatrix* matrix_impl = - &dynamic_cast&>(matrix.impl()->get("simple_csr")); + &dynamic_cast&>(matrix.impl()->get("simplecsr")); const auto& dist = matrix_impl->distribution(); diff --git a/src/refsemantic/tests/TestCompositeMatrix.cc b/src/refsemantic/tests/TestCompositeMatrix.cc index 7b454bc48..eaaef5384 100644 --- a/src/refsemantic/tests/TestCompositeMatrix.cc +++ b/src/refsemantic/tests/TestCompositeMatrix.cc @@ -214,8 +214,8 @@ TEST(TestCompositeMatrix, TimeStampTest) auto* impl0 = c0.impl(); ASSERT_EQ(0, impl0->timestamp()); std::cout << "sub0 ts = " << impl0->timestamp() << std::endl; - impl0->get("simple_csr", true); - impl0->get("simple_csr", true); + impl0->get("simplecsr", true); + impl0->get("simplecsr", true); ASSERT_EQ(2, impl0->timestamp()); std::cout << "sub0 ts = " << impl0->timestamp() << std::endl; ASSERT_EQ(2, impl->timestamp()); diff --git a/src/refsemantic/tests/TestCompositeVector.cc b/src/refsemantic/tests/TestCompositeVector.cc index 84df99fa8..1a01bb9b5 100644 --- a/src/refsemantic/tests/TestCompositeVector.cc +++ b/src/refsemantic/tests/TestCompositeVector.cc @@ -193,8 +193,8 @@ TEST(TestCompositeVector, TimeStampTest) auto* impl0 = c0.impl(); ASSERT_EQ(0, impl0->timestamp()); std::cout << "sub0 ts = " << impl0->timestamp() << std::endl; - impl0->get("simple_csr", true); - impl0->get("simple_csr", true); + impl0->get("simplecsr", true); + impl0->get("simplecsr", true); ASSERT_EQ(2, impl0->timestamp()); std::cout << "sub0 ts = " << impl0->timestamp() << std::endl; ASSERT_EQ(2, impl->timestamp()); From 0f319f6f1272d92d76b57b328c178a42147d53d0 Mon Sep 17 00:00:00 2001 From: Louis Goyard Date: Tue, 28 Jun 2022 15:00:47 +0200 Subject: [PATCH 12/13] WIP: Register integrated matrix factories and converters at static member definition --- .../hypre/examples/hypre_example_solve.cpp | 4 ++-- .../examples/hypre_example_solve_move.cpp | 4 ++-- src/core/alien/core/backend/LinearSolver.cc | 17 ++-------------- src/core/alien/core/backend/LinearSolver.h | 4 +--- src/core/alien/core/impl/MultiMatrixImpl.cc | 17 +++++++++++++--- src/core/alien/core/impl/MultiMatrixImpl.h | 20 +++++++++++++++++-- src/core/alien/core/impl/MultiVectorImpl.cc | 17 +++++++++++++--- src/core/alien/core/impl/MultiVectorImpl.h | 20 +++++++++++++++++-- 8 files changed, 71 insertions(+), 32 deletions(-) diff --git a/plugins/hypre/examples/hypre_example_solve.cpp b/plugins/hypre/examples/hypre_example_solve.cpp index dae8ac09c..b4b63cdf2 100644 --- a/plugins/hypre/examples/hypre_example_solve.cpp +++ b/plugins/hypre/examples/hypre_example_solve.cpp @@ -50,8 +50,6 @@ int test() int lsize = dist.localRowSize(); int gsize = dist.globalRowSize(); - auto solver = Alien::LinearSolver("../libhypre_wrapper.so"); - tm->info() << "build matrix with direct matrix builder"; { Alien::DirectMatrixBuilder builder(A, Alien::DirectMatrixOptions::eResetValues); @@ -93,6 +91,8 @@ int test() //auto solver = Alien::Hypre::LinearSolver(); + auto solver = Alien::LinearSolver("../libhypre_wrapper.so"); + solver.solve(A, b, x); tm->info() << "* r = Ax - b"; diff --git a/plugins/hypre/examples/hypre_example_solve_move.cpp b/plugins/hypre/examples/hypre_example_solve_move.cpp index 532b9263a..e88522f62 100644 --- a/plugins/hypre/examples/hypre_example_solve_move.cpp +++ b/plugins/hypre/examples/hypre_example_solve_move.cpp @@ -52,8 +52,6 @@ int test() int lsize = dist.localRowSize(); int gsize = dist.globalRowSize(); - auto solver = Alien::LinearSolver("../libhypre_wrapper.so"); - tm->info() << "build matrix with direct matrix builder"; { Alien::Move::DirectMatrixBuilder builder(std::move(A), Alien::DirectMatrixOptions::eResetValues); @@ -101,6 +99,8 @@ int test() // .preconditioner(Alien::Hypre::OptionTypes::AMGPC) // .solver(Alien::Hypre::OptionTypes::GMRES); + auto solver = Alien::LinearSolver("../libhypre_wrapper.so"); + solver.solve(A, b, x); tm->info() << "* r = Ax - b"; diff --git a/src/core/alien/core/backend/LinearSolver.cc b/src/core/alien/core/backend/LinearSolver.cc index 8d4cee5d9..4747f76d6 100644 --- a/src/core/alien/core/backend/LinearSolver.cc +++ b/src/core/alien/core/backend/LinearSolver.cc @@ -30,9 +30,6 @@ #include #include -#include -#include - /*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/ @@ -45,16 +42,6 @@ namespace Alien LinearSolver::LinearSolver(std::string soFile) { // FIXME: add error checking - - if (!m_initialized) - { - // FIXME: add others kernels - REGISTER_PLUGIN_MATRIX_FACTORY("simplecsr", MultiMatrixImpl::m_matrixFactory, &SimpleCSRInternal::matrix_factory); - REGISTER_PLUGIN_VECTOR_FACTORY("simplecsr", MultiVectorImpl::m_vectorFactory, &SimpleCSRInternal::vector_factory); - - m_initialized = true; - } - m_handle = dlopen(soFile.c_str(), RTLD_NOW | RTLD_LOCAL); m_plugin_create = (BackEnd::IPlugin*(*)()) dlsym(m_handle, "create"); m_plugin_destroy = (void (*)(BackEnd::IPlugin*)) dlsym(m_handle, "destroy"); @@ -66,8 +53,8 @@ LinearSolver::LinearSolver(std::string soFile) m_plugin->registerMatrixConverters(MultiMatrixImpl::m_matrixConverters); m_plugin->registerVectorConverters(MultiVectorImpl::m_vectorConverters); - m_plugin->registerMatrixFactory(MultiMatrixImpl::m_matrixFactory); - m_plugin->registerVectorFactory(MultiVectorImpl::m_vectorFactory); + m_plugin->registerMatrixFactory(MultiMatrixImpl::m_matrixFactories); + m_plugin->registerVectorFactory(MultiVectorImpl::m_vectorFactories); } /*---------------------------------------------------------------------------*/ diff --git a/src/core/alien/core/backend/LinearSolver.h b/src/core/alien/core/backend/LinearSolver.h index 90c7cf784..6b853f6cc 100644 --- a/src/core/alien/core/backend/LinearSolver.h +++ b/src/core/alien/core/backend/LinearSolver.h @@ -55,6 +55,7 @@ class IMatrix; /*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/ + /*! * \ingroup core * \brief Linear solver interface @@ -176,9 +177,6 @@ class ALIEN_EXPORT LinearSolver : public ILinearSolver std::unique_ptr m_plugin; BackEnd::IPlugin* (*m_plugin_create)(); void (*m_plugin_destroy)(BackEnd::IPlugin*); - - //FIXME: do it differently - bool m_initialized{false}; }; /*---------------------------------------------------------------------------*/ diff --git a/src/core/alien/core/impl/MultiMatrixImpl.cc b/src/core/alien/core/impl/MultiMatrixImpl.cc index 1ed2ca95e..c5afce5a6 100644 --- a/src/core/alien/core/impl/MultiMatrixImpl.cc +++ b/src/core/alien/core/impl/MultiMatrixImpl.cc @@ -36,8 +36,19 @@ namespace Alien using namespace Arccore; -std::map, IMatrixConverter*> MultiMatrixImpl::m_matrixConverters; -std::map MultiMatrixImpl::m_matrixFactory; +MultiMatrixImpl::MatrixConverters::MatrixConverters() + : std::map, IMatrixConverter*>() +{ +} + +MultiMatrixImpl::MatrixFactories::MatrixFactories() + : std::map() +{ + REGISTER_PLUGIN_MATRIX_FACTORY("simplecsr", MultiMatrixImpl::m_matrixFactories, &Alien::SimpleCSRInternal::matrix_factory); +} + +MultiMatrixImpl::MatrixConverters MultiMatrixImpl::m_matrixConverters; +MultiMatrixImpl::MatrixFactories MultiMatrixImpl::m_matrixFactories; /*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/ @@ -370,7 +381,7 @@ MultiMatrixImpl::getImpl(BackEndId backend) const auto inserter = m_impls2.insert(MultiMatrixImplMap::value_type(backend, NULL)); IMatrixImpl*& impl2 = inserter.first->second; if (impl2 == NULL) { - auto factory = m_matrixFactory[backend]; + auto factory = m_matrixFactories[backend]; if (!factory) throw FatalErrorException("MultiMatrixImpl::getImpl(): Can't find matrix factory for backend: " + backend); diff --git a/src/core/alien/core/impl/MultiMatrixImpl.h b/src/core/alien/core/impl/MultiMatrixImpl.h index b700e7f54..a925e0b17 100644 --- a/src/core/alien/core/impl/MultiMatrixImpl.h +++ b/src/core/alien/core/impl/MultiMatrixImpl.h @@ -36,6 +36,7 @@ #include #include #include +#include #include #include @@ -249,10 +250,25 @@ class ALIEN_EXPORT MultiMatrixImpl : public TimestampMng //! The variable col block datas std::shared_ptr m_cols_block; + + + + class MatrixConverters : public std::map, IMatrixConverter*> + { + public: + MatrixConverters(); + }; + + class MatrixFactories : public std::map + { + public: + MatrixFactories(); + }; + // FIXME: private, access through functions public: - static std::map, IMatrixConverter*> m_matrixConverters; - static std::map m_matrixFactory; + static MatrixConverters m_matrixConverters; + static MatrixFactories m_matrixFactories; }; /*---------------------------------------------------------------------------*/ diff --git a/src/core/alien/core/impl/MultiVectorImpl.cc b/src/core/alien/core/impl/MultiVectorImpl.cc index 99e8dc020..fe94369ab 100644 --- a/src/core/alien/core/impl/MultiVectorImpl.cc +++ b/src/core/alien/core/impl/MultiVectorImpl.cc @@ -33,8 +33,19 @@ namespace Alien using namespace Arccore; -std::map, IVectorConverter*> MultiVectorImpl::m_vectorConverters; -std::map MultiVectorImpl::m_vectorFactory; +MultiVectorImpl::VectorConverters::VectorConverters() + : std::map, IVectorConverter*>() +{ +} + +MultiVectorImpl::VectorFactories::VectorFactories() + : std::map() +{ + REGISTER_PLUGIN_VECTOR_FACTORY("simplecsr", MultiVectorImpl::m_vectorFactories, &Alien::SimpleCSRInternal::vector_factory); +} + +MultiVectorImpl::VectorConverters MultiVectorImpl::m_vectorConverters; +MultiVectorImpl::VectorFactories MultiVectorImpl::m_vectorFactories; /*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/ @@ -313,7 +324,7 @@ MultiVectorImpl::getImpl(BackEndId backend) const auto inserter = m_impls2.insert(MultiVectorImplMap::value_type(backend, NULL)); IVectorImpl*& impl2 = inserter.first->second; if (impl2 == NULL) { - auto factory = m_vectorFactory[backend]; + auto factory = m_vectorFactories[backend]; if (!factory) throw FatalErrorException("MultiVectorImpl::getImpl(): Can't find vector factory for backend: " + backend); diff --git a/src/core/alien/core/impl/MultiVectorImpl.h b/src/core/alien/core/impl/MultiVectorImpl.h index e6ca7c86e..d59eccf59 100644 --- a/src/core/alien/core/impl/MultiVectorImpl.h +++ b/src/core/alien/core/impl/MultiVectorImpl.h @@ -36,6 +36,7 @@ #include #include #include +#include /*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/ @@ -46,6 +47,7 @@ namespace Alien /*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/ + class ALIEN_EXPORT MultiVectorImpl : public TimestampMng , public UserFeatureMng , public ObjectWithTrace @@ -211,10 +213,24 @@ class ALIEN_EXPORT MultiVectorImpl : public TimestampMng //! The variable block datas std::shared_ptr m_variable_block; + + + class VectorConverters : public std::map, IVectorConverter*> + { + public: + VectorConverters(); + }; + + class VectorFactories : public std::map + { + public: + VectorFactories(); + }; + // FIXME: private, access throuh functions public: - static std::map, IVectorConverter*> m_vectorConverters; - static std::map m_vectorFactory; + static VectorConverters m_vectorConverters; + static VectorFactories m_vectorFactories; }; From a314c60f2e0c1ee45b026493e81892a324e7dbb2 Mon Sep 17 00:00:00 2001 From: Louis Goyard Date: Wed, 3 Aug 2022 09:25:43 +0200 Subject: [PATCH 13/13] WIP: debugging with OpenMPI-dependant plugin --- .../arccore/message_passing_mpi/MpiAdapter.cc | 9 +----- .../hypre/examples/hypre_example_solve.cpp | 5 ++++ plugins/hypre/include/alien/hypre/backend.h | 1 + plugins/hypre/src/backend.cpp | 12 ++++++++ plugins/hypre/src/hypre_linear_solver.cpp | 9 ++++++ src/core/alien/core/backend/BackEnd.h | 4 +++ src/core/alien/core/backend/LinearSolver.cc | 28 ++++++++++++++++++- 7 files changed, 59 insertions(+), 9 deletions(-) diff --git a/framework/arccore/src/message_passing_mpi/arccore/message_passing_mpi/MpiAdapter.cc b/framework/arccore/src/message_passing_mpi/arccore/message_passing_mpi/MpiAdapter.cc index fba543a76..cc9f36018 100644 --- a/framework/arccore/src/message_passing_mpi/arccore/message_passing_mpi/MpiAdapter.cc +++ b/framework/arccore/src/message_passing_mpi/arccore/message_passing_mpi/MpiAdapter.cc @@ -954,14 +954,7 @@ directSendPack(const void* send_buffer,Int64 send_buffer_size, MpiMessagePassingMng* MpiAdapter:: commSplit(bool keep) { - MPI_Comm new_comm; - - MPI_Comm_split(m_communicator, (keep) ? 1 : MPI_UNDEFINED, commRank(), &new_comm); - if (keep) { - // Failed if new_comm is MPI_COMM_NULL - return StandaloneMpiMessagePassingMng::create(new_comm, true); - } - return nullptr; + return StandaloneMpiMessagePassingMng::create(m_communicator, true); } /*---------------------------------------------------------------------------*/ diff --git a/plugins/hypre/examples/hypre_example_solve.cpp b/plugins/hypre/examples/hypre_example_solve.cpp index b4b63cdf2..87a3ed0a6 100644 --- a/plugins/hypre/examples/hypre_example_solve.cpp +++ b/plugins/hypre/examples/hypre_example_solve.cpp @@ -92,6 +92,11 @@ int test() //auto solver = Alien::Hypre::LinearSolver(); auto solver = Alien::LinearSolver("../libhypre_wrapper.so"); + //auto solver2 = Alien::LinearSolver("../libhypre_wrapper.so"); + + std::cout << "foufou1" << std::endl; + MPI_Barrier(MPI_COMM_WORLD); + std::cout << "foufou2" << std::endl; solver.solve(A, b, x); diff --git a/plugins/hypre/include/alien/hypre/backend.h b/plugins/hypre/include/alien/hypre/backend.h index 4fa10e89f..6128ee597 100644 --- a/plugins/hypre/include/alien/hypre/backend.h +++ b/plugins/hypre/include/alien/hypre/backend.h @@ -70,6 +70,7 @@ class Plugin : public BackEnd::IPlugin void registerMatrixFactory(std::map& matrixFactories); void registerVectorFactory(std::map& vectorFactories); + void init(); BackEndId name() { return "hypre"; } }; diff --git a/plugins/hypre/src/backend.cpp b/plugins/hypre/src/backend.cpp index db263c2a9..4b0b35b0a 100644 --- a/plugins/hypre/src/backend.cpp +++ b/plugins/hypre/src/backend.cpp @@ -31,6 +31,18 @@ void Plugin::registerVectorFactory(std::map& REGISTER_PLUGIN_VECTOR_FACTORY("hypre", vectorFactories, &vector_factory); } +void Plugin::init() +{ + int argc = 0; + char** argv = nullptr; + std::cout << "coucou" << std::endl; + MPI_Init(&argc, &argv); + MPI_Barrier(MPI_COMM_WORLD); + int foo = 0; + MPI_Comm_size(MPI_COMM_WORLD, &foo); + std::cout << "foufou" << std::endl; +}; + Plugin* create() { return new Plugin; diff --git a/plugins/hypre/src/hypre_linear_solver.cpp b/plugins/hypre/src/hypre_linear_solver.cpp index a86a79e40..071ecc0c9 100644 --- a/plugins/hypre/src/hypre_linear_solver.cpp +++ b/plugins/hypre/src/hypre_linear_solver.cpp @@ -53,6 +53,12 @@ bool PluginLinearSolver::_solve(const Matrix& A, const Vector& b, Vector& x) auto bij_vector = b.internal(); auto xij_vector = x.internal(); + std::cout << "boubou1" << std::endl; + int foo = 0; + MPI_Comm_size(MPI_COMM_WORLD, &foo); + MPI_Barrier(MPI_COMM_WORLD); + std::cout << "boubou2" << std::endl; + // Clear all Hypre error for this session HYPRE_ClearAllErrors(); @@ -70,6 +76,7 @@ bool PluginLinearSolver::_solve(const Matrix& A, const Vector& b, Vector& x) int (*precond_destroy_function)(HYPRE_Solver) = nullptr; MPI_Comm comm = MPI_COMM_WORLD; + printf("Foofoo\n"); auto* mpi_comm_mng = dynamic_cast( A.distribution().parallelMng()); if (mpi_comm_mng) @@ -255,6 +262,7 @@ bool PluginLinearSolver::_solve(const Matrix& A, const Vector& b, Vector& x) HYPRE_ParCSRMatrix par_a; HYPRE_ParVector par_rhs, par_x; + printf("Barbar\n"); checkError( "Hypre Matrix GetObject", HYPRE_IJMatrixGetObject(ij_matrix, (void**)&par_a)); checkError("Hypre RHS Vector GetObject", @@ -265,6 +273,7 @@ bool PluginLinearSolver::_solve(const Matrix& A, const Vector& b, Vector& x) checkError("Hypre " + solver_name + " solver Setup", (*solver_setup_function)(solver, par_a, par_rhs, par_x)); m_status.succeeded = ((*solver_solve_function)(solver, par_a, par_rhs, par_x) == 0); + printf("Farfar\n"); if (m_status.succeeded) { checkError("Hypre " + solver_name + " solver GetNumIterations", diff --git a/src/core/alien/core/backend/BackEnd.h b/src/core/alien/core/backend/BackEnd.h index 026046271..468019611 100644 --- a/src/core/alien/core/backend/BackEnd.h +++ b/src/core/alien/core/backend/BackEnd.h @@ -22,6 +22,8 @@ #include #include +extern "C" int MPI_Init(int *argc, char ***argv); + #include /*---------------------------------------------------------------------------*/ @@ -130,6 +132,8 @@ namespace BackEnd virtual std::unique_ptr solver_factory() = 0; virtual std::unique_ptr solver_factory(const Alien::BackEnd::Options& options) = 0; + virtual void init() = 0; + virtual void registerMatrixConverters(std::map, IMatrixConverter*>& converters) = 0; virtual void registerVectorConverters(std::map, IVectorConverter*>& converters) = 0; virtual void registerMatrixFactory(std::map& matrixFactories) = 0; diff --git a/src/core/alien/core/backend/LinearSolver.cc b/src/core/alien/core/backend/LinearSolver.cc index 4747f76d6..6d7db216e 100644 --- a/src/core/alien/core/backend/LinearSolver.cc +++ b/src/core/alien/core/backend/LinearSolver.cc @@ -39,10 +39,34 @@ namespace Alien /*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/ +#define MANAGE_DL_ERROR(name) \ + error = dlerror(); \ + if (error != NULL) { \ + fprintf(stderr, "%s\n", error); \ + printf("%s failed\n", name); \ + } + LinearSolver::LinearSolver(std::string soFile) { // FIXME: add error checking - m_handle = dlopen(soFile.c_str(), RTLD_NOW | RTLD_LOCAL); + printf("LM_ID_NEWLM is: %d\n", LM_ID_NEWLM); + printf("LM_ID_BASE is: %d\n", LM_ID_BASE); + char* error; + void* fooHandle = dlmopen(LM_ID_NEWLM, "libc.so.6", RTLD_NOW); + if (fooHandle == NULL){ + MANAGE_DL_ERROR("first dlopen"); + } + Lmid_t givenNs; + dlinfo(fooHandle, RTLD_DI_LMID, &givenNs); + + void* mpiHandle = dlmopen(givenNs, "libmpi.so.40", RTLD_NOW); + if (fooHandle == NULL){ + MANAGE_DL_ERROR("mpi dlopen"); + } + m_handle = dlmopen(givenNs, soFile.c_str(), RTLD_NOW); + if (m_handle == NULL){ + MANAGE_DL_ERROR("plugin dlopen"); + } m_plugin_create = (BackEnd::IPlugin*(*)()) dlsym(m_handle, "create"); m_plugin_destroy = (void (*)(BackEnd::IPlugin*)) dlsym(m_handle, "destroy"); @@ -55,6 +79,8 @@ LinearSolver::LinearSolver(std::string soFile) m_plugin->registerVectorConverters(MultiVectorImpl::m_vectorConverters); m_plugin->registerMatrixFactory(MultiMatrixImpl::m_matrixFactories); m_plugin->registerVectorFactory(MultiVectorImpl::m_vectorFactories); + + m_plugin->init(); } /*---------------------------------------------------------------------------*/