Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Core][TrilinosApplication] Defining list of fastest direct linear solvers. #12350

Merged
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,8 @@ void AddBasicOperations(pybind11::module& m)
.def("SetValue", SetValue)
//.def("GetValue", GetValue) //deliberately commented out. Only works for local Ids
.def("GatherValues", GatherValues)
.def_static("IsDistributed", &TrilinosSparseSpaceType::IsDistributed)
.def_static("FastestDirectSolverList", &TrilinosSparseSpaceType::FastestDirectSolverList)
;

m.def("CreateCommunicator", CreateCommunicator);
Expand Down
22 changes: 22 additions & 0 deletions applications/TrilinosApplication/trilinos_space.h
Original file line number Diff line number Diff line change
Expand Up @@ -862,6 +862,28 @@ class TrilinosSpace
return true;
}

/**
* @brief Returns a list of the fastest direct solvers.
* @details This function returns a vector of strings representing the names of the fastest direct solvers. The order of the solvers in the list may need to be updated and reordered depending on the size of the equation system.
* @return A vector of strings containing the names of the fastest direct solvers.
*/
inline static std::vector<std::string> FastestDirectSolverList()
{
// May need to be updated and reordered. In fact I think it depends of the size of the equation system
std::vector<std::string> faster_direct_solvers({
"mumps2", // Amesos2 (if compiled with MUMPS-support)
"mumps", // Amesos (if compiled with MUMPS-support)
"super_lu_dist2", // Amesos2 SuperLUDist (if compiled with MPI-support)
"super_lu_dist", // Amesos SuperLUDist (if compiled with MPI-support)
"amesos2", // Amesos2
"amesos", // Amesos
"klu2", // Amesos2 KLU
"klu", // Amesos KLU
"basker" // Amesos2 Basker
});
return faster_direct_solvers;
}

/**
* @brief This function returns a value from a given vector according to a given index
* @param rX The vector from which values are to be gathered
Expand Down
50 changes: 50 additions & 0 deletions kratos/python/add_space_to_python.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// | / |
// ' / __| _` | __| _ \ __|
// . \ | ( | | ( |\__ `
// _|\_\_| \__,_|\__|\___/ ____/
// Multi-Physics
//
// License: BSD License
// Kratos default license: kratos/license.txt
//
// Main authors: Vicente Mataix Ferrandiz
//

// System includes

// External includes

// Project includes
#include "includes/define_python.h"
#include "python/add_space_to_python.h"
#include "spaces/ublas_space.h"

namespace Kratos::Python
{
void AddSpaceToPython(pybind11::module& m)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am 99% sure this exists already somewhere

Probably in the addstrategiestopython

Having it separate makes sense but not duplicated

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A priori it is not, at least I couldn't find it. UblasSpace is old...

Copy link
Member Author

@loumalouomega loumalouomega May 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay I found it, name is UblasSparseSpace

{
namespace py = pybind11;

using SparseSpaceType = UblasSpace<double, CompressedMatrix, boost::numeric::ublas::vector<double>>;

py::class_<SparseSpaceType, SparseSpaceType::Pointer>(m,"SparseSpace")
.def(py::init<>())
.def_static("Size", &SparseSpaceType::Size)
.def_static("Size1", &SparseSpaceType::Size1)
.def_static("Size2", &SparseSpaceType::Size2)
.def_static("IsDistributed", &SparseSpaceType::IsDistributed)
.def_static("FastestDirectSolverList", &SparseSpaceType::FastestDirectSolverList)
;

using LocalSpaceType = UblasSpace<double, Matrix, Vector>;

py::class_<LocalSpaceType, LocalSpaceType::Pointer>(m,"LocalSpace")
.def(py::init<>())
.def_static("Size", &LocalSpaceType::Size)
.def_static("Size1", &LocalSpaceType::Size1)
.def_static("Size2", &LocalSpaceType::Size2)
.def_static("IsDistributed", &LocalSpaceType::IsDistributed)
.def_static("FastestDirectSolverList", &LocalSpaceType::FastestDirectSolverList)
;
}
} // namespace Kratos::Python.
27 changes: 27 additions & 0 deletions kratos/python/add_space_to_python.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// | / |
// ' / __| _` | __| _ \ __|
// . \ | ( | | ( |\__ `
// _|\_\_| \__,_|\__|\___/ ____/
// Multi-Physics
//
// License: BSD License
// Kratos default license: kratos/license.txt
//
// Main authors: Vicente Mataix Ferrandiz
//

#pragma once

// System includes

// External includes
#include <pybind11/pybind11.h>

// Project includes

namespace Kratos::Python
{

void AddSpaceToPython(pybind11::module& m);

} // namespace Kratos::Python.
1 change: 0 additions & 1 deletion kratos/python/add_sparse_matrices_to_python.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

// | / |
// ' / __| _` | __| _ \ __|
// . \ | ( | | ( |\__ `
Expand Down
2 changes: 2 additions & 0 deletions kratos/python/kratos_python.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
#include "add_accessors_to_python.h"
#include "add_globals_to_python.h"
#include "add_geometry_data_to_python.h"
#include "add_space_to_python.h"

namespace Kratos::Python
{
Expand Down Expand Up @@ -156,6 +157,7 @@ PYBIND11_MODULE(Kratos, m)
AddRegistryToPython(m);
AddContainerExpressionToPython(m);
AddGlobalsToPython(m);
AddSpaceToPython(m);

m.def("Hello", Hello);
}
Expand Down
11 changes: 2 additions & 9 deletions kratos/python_scripts/python_linear_solver_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
from KratosMultiphysics import kratos_utilities as kratos_utils
from importlib import import_module


def ConstructSolver(configuration):
if(type(configuration) != KM.Parameters):
raise Exception("input is expected to be provided as a Kratos Parameters object")
Expand All @@ -26,11 +25,7 @@ def CreateFastestAvailableDirectLinearSolver():
if kratos_utils.CheckIfApplicationsAvailable("LinearSolversApplication"):
from KratosMultiphysics import LinearSolversApplication

linear_solvers_by_speed = [
"pardiso_lu", # LinearSolversApplication (if compiled with Intel-support)
"sparse_lu", # LinearSolversApplication
"skyline_lu_factorization" # in Core, always available, but slow
]
linear_solvers_by_speed = KM.SparseSpace.FastestDirectSolverList()

for solver_name in linear_solvers_by_speed:
if KM.LinearSolverFactory().Has(solver_name):
Expand All @@ -41,7 +36,6 @@ def CreateFastestAvailableDirectLinearSolver():

raise Exception("Linear-Solver could not be constructed!")


def __GetSolverTypeAndImportApplication(solver_type):
# remove unused "KratosMultiphysics.
if solver_type.startswith("KratosMultiphysics."):
Expand All @@ -57,5 +51,4 @@ def __GetSolverTypeAndImportApplication(solver_type):
solver_type = splitted_name[1]
import_module("KratosMultiphysics." + app_name)

return solver_type

return solver_type
16 changes: 16 additions & 0 deletions kratos/spaces/ublas_space.h
Original file line number Diff line number Diff line change
Expand Up @@ -891,6 +891,22 @@ class UblasSpace
return false;
}

/**
* @brief Returns a list of the fastest direct solvers.
* @details This function returns a vector of strings representing the names of the fastest direct solvers. The order of the solvers in the list may need to be updated and reordered depending on the size of the equation system.
* @return A vector of strings containing the names of the fastest direct solvers.
*/
inline static std::vector<std::string> FastestDirectSolverList()
{
std::vector<std::string> faster_direct_solvers({
"pardiso_lu", // LinearSolversApplication (if compiled with Intel-support)
"pardiso_ldlt", // LinearSolversApplication (if compiled with Intel-support)
"sparse_lu", // LinearSolversApplication
"skyline_lu_factorization" // In Core, always available, but slow
});
return faster_direct_solvers;
}

//***********************************************************************

inline static TDataType GetValue(const VectorType& x, std::size_t I)
Expand Down
Loading