Commit 57c8d057 authored by Dmitry Bagaev's avatar Dmitry Bagaev

SolverParameters class init

Basic functionality
parent 6467caf5
......@@ -263,8 +263,11 @@ set(INMOST_INSTALL_HEADERS Source/Headers/inmost.h
Source/Headers/inmost_nonlinear.h
Source/Headers/inmost_partitioner.h
Source/Headers/inmost_solver.h
Source/Headers/inmost_solver_interface.h
Source/Headers/inmost_solver_parameters.h
Source/Headers/inmost_sparse.h
Source/Headers/inmost_xml.h
Source/Headers/inmost_utils.h
Source/Headers/inmost_variable.h
Source/Headers/container.hpp)
......@@ -293,9 +296,12 @@ set_property(TARGET inmost PROPERTY PUBLIC_HEADER
"${PROJECT_SOURCE_DIR}/Source/Headers/inmost_nonlinear.h"
"${PROJECT_SOURCE_DIR}/Source/Headers/inmost_partitioner.h"
"${PROJECT_SOURCE_DIR}/Source/Headers/inmost_solver.h"
"${PROJECT_SOURCE_DIR}/Source/Headers/inmost_solver_interface.h"
"${PROJECT_SOURCE_DIR}/Source/Headers/inmost_solver_parameters.h"
"${PROJECT_SOURCE_DIR}/Source/Headers/inmost_sparse.h"
"${PROJECT_SOURCE_DIR}/Source/Headers/inmost_variable.h"
"${PROJECT_SOURCE_DIR}/Source/Headers/inmost_xml.h"
"${PROJECT_SOURCE_DIR}/Source/Headers/inmost_utils.h"
"${PROJECT_SOURCE_DIR}/Source/Headers/container.hpp")
install(FILES
......
......@@ -132,7 +132,7 @@ int main(int argc,char ** argv)
ttt = Timer();
Solver S("inner_ilu2"); // Specify the linear solver to ASM+ILU2+BiCGStab one
S.SetParameter("absolute_tolerance", "1e-8");
S.GetParameters().set("absolute_tolerance", "1e-8");
Residual R; // Residual vector
Sparse::LockService Locks;
Sparse::Vector Update; // Declare the solution and the right-hand side vectors
......
......@@ -718,10 +718,12 @@ int main(int argc,char ** argv)
//Solver S(Solver::INNER_ILU2);
//Solver S(Solver::INNER_MPTILUC);
Solver S("superlu");
S.SetParameter("relative_tolerance", "1.0e-14");
S.SetParameter("absolute_tolerance", "1.0e-12");
S.SetParameter("drop_tolerance", "1.0e-1");
S.SetParameter("reuse_tolerance", "1.0e-2");
SolverParameters &p = S.GetParameters();
p.set("relative_tolerance", "1.0e-14");
p.set("absolute_tolerance", "1.0e-12");
p.set("drop_tolerance", "1.0e-1");
p.set("reuse_tolerance", "1.0e-2");
S.SetMatrix(R.GetJacobian());
//std::fill(Update.Begin(),Update.End(),0.0);
if( S.Solve(R.GetResidual(),Update) )
......
......@@ -129,7 +129,7 @@ int main(int argc,char ** argv)
ttt = Timer();
Solver S("inner_ilu2"); // Specify the linear solver to ASM+ILU2+BiCGStab one
S.SetParameter("absolute_tolerance", "1e-8");
S.GetParameters().set("absolute_tolerance", "1e-8");
Sparse::LockService L;
Sparse::Matrix A; // Declare the matrix of the linear system to be solved
Sparse::Vector x,b; // Declare the solution and the right-hand side vectors
......
......@@ -7,8 +7,10 @@ add_subdirectory(NonlinearSolver)
add_subdirectory(Autodiff)
add_subdirectory(Partitioner)
add_subdirectory(Misc)
add_subdirectory(Utils)
add_subdirectory(Headers)
set(SOLVER_DEFINITIONS ${SOLVER_DEFINITIONS} PARENT_SCOPE)
set(SOURCE ${SOURCE} PARENT_SCOPE)
set(HEADER ${HEADER} PARENT_SCOPE)
......@@ -13,6 +13,7 @@ set(HEADER
${CMAKE_CURRENT_SOURCE_DIR}/inmost_mesh.h
${CMAKE_CURRENT_SOURCE_DIR}/inmost_solver.h
${CMAKE_CURRENT_SOURCE_DIR}/inmost_solver_interface.h
${CMAKE_CURRENT_SOURCE_DIR}/inmost_solver_parameters.h
${CMAKE_CURRENT_SOURCE_DIR}/inmost_partitioner.h
${CMAKE_CURRENT_SOURCE_DIR}/inmost_autodiff.h
${CMAKE_CURRENT_SOURCE_DIR}/inmost_expression.h
......@@ -20,6 +21,7 @@ set(HEADER
${CMAKE_CURRENT_SOURCE_DIR}/inmost_sparse.h
${CMAKE_CURRENT_SOURCE_DIR}/inmost_nonlinear.h
${CMAKE_CURRENT_SOURCE_DIR}/inmost_xml.h
${CMAKE_CURRENT_SOURCE_DIR}/inmost_utils.h
${CMAKE_CURRENT_SOURCE_DIR}/container.hpp
PARENT_SCOPE
)
\ No newline at end of file
......@@ -11,5 +11,6 @@
#include "inmost_variable.h"
#include "inmost_nonlinear.h"
#include "inmost_xml.h"
#include "inmost_utils.h"
#endif // INMOST_H_INCLUDED
......@@ -224,6 +224,7 @@ namespace INMOST
SolverNotFound,
SolverUnsupportedOperation,
SolverUnknownParameter,
SolverNonExistentParameters,
/// The list of errors may occur in the Partitioner.
ErrorInPartitioner = 500,
......
......@@ -21,12 +21,14 @@
#define DEFAULT_PRECONDITIONER_ADAPT_DDPQ_TOLERANCE 1
#if defined(USE_SOLVER)
namespace INMOST {
namespace INMOST
{
#define GUARD_MPI(x) {ierr = x; if( ierr != MPI_SUCCESS ) {char str[4096]; int len; MPI_Error_string(ierr,str,&len); std::cout << #x << " not successfull: " << str << std::endl; MPI_Abort(comm,-1000);}}
#define HASH_TABLE_SIZE 2048
class Solver {
class Solver
{
private:
static int *argc;
static char ***argv;
......@@ -40,7 +42,8 @@ namespace INMOST {
public:
/// Base class for low level operations with objects of Solver class.
class OrderInfo {
class OrderInfo
{
private:
typedef std::vector<INMOST_DATA_ENUM_TYPE> storage_type;
storage_type global_to_proc; //stores ends of all non-overlapping intervals of elements, owned by this processor
......@@ -98,7 +101,8 @@ namespace INMOST {
GetLocalRegion(INMOST_DATA_ENUM_TYPE proc, INMOST_DATA_ENUM_TYPE &mbeg, INMOST_DATA_ENUM_TYPE &mend) const;
/// Get the local index region for the current process.
void GetVectorRegion(INMOST_DATA_ENUM_TYPE &mbeg, INMOST_DATA_ENUM_TYPE &mend) const {
void GetVectorRegion(INMOST_DATA_ENUM_TYPE &mbeg, INMOST_DATA_ENUM_TYPE &mend) const
{
mbeg = local_vector_begin;
mend = local_vector_end;
}
......@@ -116,12 +120,14 @@ namespace INMOST {
/// Get the communicator which the solver is associated with.
INMOST_MPI_Comm GetComm() const { return comm; }
// Access to arrays below allows to organize manual exchange
INMOST_MPI_Request *GetSendRequests() {
INMOST_MPI_Request *GetSendRequests()
{
assert(!send_requests.empty());
return &send_requests[0];
}
INMOST_MPI_Request *GetRecvRequests() {
INMOST_MPI_Request *GetRecvRequests()
{
assert(!recv_requests.empty());
return &recv_requests[0];
}
......@@ -130,14 +136,16 @@ namespace INMOST {
INMOST_DATA_ENUM_TYPE GetRecvRequestsSize() { return static_cast<INMOST_DATA_ENUM_TYPE>(recv_requests.size()); }
INMOST_DATA_ENUM_TYPE *GetSendExchangeArray() {
INMOST_DATA_ENUM_TYPE *GetSendExchangeArray()
{
assert(!vector_exchange_send.empty());
return &vector_exchange_send[0];
}
INMOST_DATA_ENUM_TYPE GetSendExchangeSize() { return static_cast<INMOST_DATA_ENUM_TYPE>(send_storage.size()); }
INMOST_DATA_ENUM_TYPE *GetRecvExchangeArray() {
INMOST_DATA_ENUM_TYPE *GetRecvExchangeArray()
{
assert(!vector_exchange_recv.empty());
return &vector_exchange_recv[0];
}
......@@ -241,11 +249,7 @@ namespace INMOST {
/// Clear all internal data of the current solver including matrix, preconditioner etc.
bool Clear();
void SetDefaultParameters();
SolverParameter GetParameter(std::string property) const;
void SetParameter(std::string property, std::string value);
SolverParameters &GetParameters() const;
/// Return the number of iterations performed by the last solution.
/// @see Sparse::Solve
......
#ifndef INMOST_SOLVERINTERFACE_H
#define INMOST_SOLVERINTERFACE_H
#ifndef INMOST_SOLVER_INTERFACE_H
#define INMOST_SOLVER_INTERFACE_H
#include <string>
#include <sstream>
#include "inmost_solver_parameters.h"
#include "inmost_sparse.h"
namespace INMOST {
#if defined(USE_SOLVER)
class SolverParameter {
private:
std::string value;
public:
SolverParameter() {};
SolverParameter(std::string value) : value(value) {};
template<typename T>
const T get() const {
std::istringstream stream(value);
T parameter;
stream >> parameter;
return parameter;
}
};
class SolverParameters {
private:
std::map<std::string, SolverParameter> parameters;
public:
SolverParameters();
void SetParameter(std::string name, std::string value);
const SolverParameter GetParameter(std::string name) const;
template<typename T>
const T get(std::string name) const {
return GetParameter(name).get<T>();
}
};
class SolverInterface {
protected:
INMOST_MPI_Comm communicator;
SolverParameters parameters;
SolverParameters &parameters;
public:
SolverInterface() {};
SolverInterface(SolverParameters &parameters): parameters(parameters) {};
SolverInterface(const SolverInterface *other) {};
SolverInterface(const SolverInterface *other): parameters(other->parameters) {
parameters = SolverParameters(other->parameters);
};
virtual void Assign(const SolverInterface *other) = 0;
......@@ -63,15 +33,13 @@ namespace INMOST {
virtual bool isMatrixSet() = 0;
virtual void SetDefaultParameters() = 0;
virtual SolverParameter GetParameter(std::string name) const {
return parameters.GetParameter(name);
virtual SolverParameters &GetParameters() const {
return parameters;
};
virtual void SetParameter(std::string name, std::string value) {
parameters.SetParameter(name, value);
};
virtual void SetParameters(SolverParameters &_parameters) {
parameters = _parameters;
}
virtual const INMOST_DATA_ENUM_TYPE Iterations() const = 0;
......@@ -89,7 +57,6 @@ namespace INMOST {
virtual ~SolverInterface() {};
void SetCommunicator(INMOST_MPI_Comm _communicator) {
communicator = _communicator;
}
......@@ -97,10 +64,11 @@ namespace INMOST {
INMOST_MPI_Comm GetCommunicator() {
return communicator;
}
};
}
#endif
#endif //INMOST_SOLVERINTERFACE_H
#endif //INMOST_SOLVER_INTERFACE_H
#ifndef INMOST_SOLVER_PARAMETERS_H
#define INMOST_SOLVER_PARAMETERS_H
#include <string>
#include "inmost_utils.h"
#include "inmost_common.h"
namespace INMOST {
#if defined(USE_SOLVER)
class SolverParameters {
private:
std::map<std::string, std::string> parameters;
std::string prefix;
std::string solverName;
public:
SolverParameters();
SolverParameters(std::string solverName, std::string prefix);
SolverParameters(const SolverParameters &other);
std::string getSolverName() const;
std::string getPrefix() const;
void require(std::string name, std::string value);
void set(std::string name, std::string value);
template<typename T>
T get(std::string name) {
return from_string<T>(parameters[name]);
}
};
class GlobalSolversParameters {
private:
static std::vector<SolverParameters> parameters;
GlobalSolversParameters() {};
static void Initialize(std::string databasePath);
public:
static SolverParameters& getSolverParameters(std::string solverName, std::string prefix);
};
#endif
}
#endif //INMOST_SOLVER_PARAMETERS_H
#ifndef INMOST_UTILS_H
#define INMOST_UTILS_H
#include <sstream>
#include <string>
namespace INMOST {
template<typename T>
T from_string(std::string str) {
T v;
std::istringstream ss(str);
ss >> v;
return v;
}
}
#endif //INMOST_UTILS_H
This diff is collapsed.
......@@ -4,10 +4,10 @@ namespace INMOST {
std::map<std::string, SolverBaseFactory *> SolverMaster::solvers = std::map<std::string, SolverBaseFactory *>();
SolverInterface *SolverMaster::getSolver(std::string name) {
SolverInterface *SolverMaster::getSolver(std::string name, SolverParameters &parameters) {
solvers_map_iterator_t iterator = SolverMaster::solvers.find(name);
if (iterator != SolverMaster::solvers.end()) {
return iterator->second->create();
return iterator->second->create(parameters);
} else {
throw INMOST::SolverNotFound;
}
......
......@@ -7,7 +7,7 @@
namespace INMOST {
struct SolverBaseFactory {
virtual SolverInterface *create() = 0;
virtual SolverInterface *create(SolverParameters &parameters) = 0;
virtual SolverInterface *copy(const SolverInterface *other) = 0;
......@@ -16,8 +16,8 @@ namespace INMOST {
template<class C>
struct SolverCreateFactory : SolverBaseFactory {
SolverInterface *create() {
return new C;
SolverInterface *create(SolverParameters &parameters) {
return new C(parameters);
};
SolverInterface *copy(const SolverInterface *other) {
......@@ -34,7 +34,7 @@ namespace INMOST {
solvers.insert(std::make_pair(name, new SolverCreateFactory<T>));
};
static SolverInterface *getSolver(std::string name);
static SolverInterface *getSolver(std::string name, SolverParameters &parameters);
static SolverInterface *copySolver(const SolverInterface *other);
......
#include "inmost_solver_parameters.h"
#include "inmost_solver.h"
#include "inmost_solver_interface.h"
namespace INMOST {
typedef std::map<std::string, SolverParameter>::const_iterator parameters_map_iterator_t;
SolverParameters::SolverParameters() {}
void SolverParameters::SetParameter(std::string name, std::string value) {
parameters[name] = SolverParameter(value);
SolverParameters::SolverParameters(std::string solverName, std::string prefix) : solverName(solverName), prefix(prefix) {}
SolverParameters::SolverParameters(const SolverParameters &other) : prefix(other.prefix), parameters(other.parameters) {}
void SolverParameters::set(std::string name, std::string value) {
parameters[name] = value;
}
const SolverParameter SolverParameters::GetParameter(std::string name) const {
parameters_map_iterator_t value = parameters.find(name);
if (value != parameters.cend()) {
return value->second;
} else {
throw INMOST::SolverUnknownParameter;
void SolverParameters::require(std::string name, std::string value) {
if (parameters.find(name) == parameters.end()) {
parameters[name] = value;
}
}
std::string SolverParameters::getSolverName() const {
return solverName;
}
std::string SolverParameters::getPrefix() const {
return prefix;
}
typedef std::vector<SolverParameters>::iterator parameters_iterator_t;
std::vector<SolverParameters> GlobalSolversParameters::parameters = std::vector<SolverParameters>();
SolverParameters &GlobalSolversParameters::getSolverParameters(std::string solverName, std::string prefix) {
for (parameters_iterator_t i = parameters.begin(); i < parameters.end(); i++) {
SolverParameters& p = *i;
if (p.getSolverName() == solverName && p.getPrefix() == prefix) return p;
}
SolverParameters new_p = SolverParameters(solverName, prefix);
parameters.push_back(new_p);
return (parameters[parameters.size() - 1]);
}
void GlobalSolversParameters::Initialize(std::string databasePath) {
std::vector<std::string> solvers = Solver::getAvailableSolvers();
for (std::vector<std::string>::iterator name = solvers.begin(); name < solvers.end(); name++) {
//global[*name] = std::map<std::string, SolverParameters>();
}
}
}
......@@ -49,11 +49,10 @@ namespace INMOST {
bool Solver::is_finalized = false;
Solver::Solver(std::string solverName, std::string prefix, INMOST_MPI_Comm _comm) {
this->solver = SolverMaster::getSolver(solverName);
SolverParameters &p = GlobalSolversParameters::getSolverParameters(solverName, prefix);
this->solver = SolverMaster::getSolver(solverName, p);
this->prefix = prefix;
solver->SetCommunicator(_comm);
solver->SetDefaultParameters();
std::string solverDatabasePath = Solver::parseDatabase(solverName);
solver->Initialize(argc, argv, solverDatabasePath.c_str(), prefix);
}
......@@ -61,19 +60,17 @@ namespace INMOST {
Solver::Solver(const Solver &other) {
this->solver = SolverMaster::copySolver(other.solver);
this->prefix = other.prefix;
solver->SetCommunicator(other.solver->GetCommunicator());
solver->SetDefaultParameters();
std::string solverDatabasePath = Solver::parseDatabase(solver->SolverName());
solver->Initialize(argc, argv, solverDatabasePath.c_str(), prefix);
solver->Initialize(argc, argv, solverDatabasePath.c_str(), this->prefix);
}
Solver &Solver::operator=(const Solver &other) {
if (this != &other) {
this->solver->SetCommunicator(other.solver->GetCommunicator());
this->solver->SetDefaultParameters();
this->prefix = other.prefix;
this->solver->Assign(other.solver);
this->solver->SetParameters(other.solver->GetParameters());
}
return *this;
}
......@@ -181,16 +178,8 @@ namespace INMOST {
return solver->Clear();
}
void Solver::SetDefaultParameters() {
return solver->SetDefaultParameters();
}
SolverParameter Solver::GetParameter(std::string property) const {
return solver->GetParameter(property);
}
void Solver::SetParameter(std::string property, std::string value) {
solver->SetParameter(property, value);
SolverParameters &Solver::GetParameters() const {
return solver->GetParameters();
}
const INMOST_DATA_ENUM_TYPE Solver::Iterations() const {
......
......@@ -2,11 +2,11 @@
namespace INMOST {
SolverANI::SolverANI() {
SolverANI::SolverANI(SolverParameters &parameters): SolverInterface(parameters) {
};
SolverANI::SolverANI(const SolverInterface *other) {
SolverANI::SolverANI(const SolverInterface *other): SolverInterface(other) {
throw INMOST::SolverUnsupportedOperation; //later
};
......@@ -109,18 +109,6 @@ namespace INMOST {
return m.n != 0;
}
void SolverANI::SetDefaultParameters() {
}
SolverParameter SolverANI::GetParameter(std::string name) const {
throw INMOST::SolverUnsupportedOperation;
}
void SolverANI::SetParameter(std::string name, std::string value) {
//throw INMOST::SolverUnsupportedOperation;
}
const INMOST_DATA_ENUM_TYPE SolverANI::Iterations() const {
return static_cast<INMOST_DATA_ENUM_TYPE>(solver.ITER);
}
......
......@@ -13,7 +13,7 @@ namespace INMOST {
INMOST_DATA_ENUM_TYPE local_size;
public:
SolverANI();
SolverANI(SolverParameters &parameters);
SolverANI(const SolverInterface *other);
......@@ -29,12 +29,6 @@ namespace INMOST {
virtual bool isMatrixSet();
virtual void SetDefaultParameters();
virtual SolverParameter GetParameter(std::string name) const;
virtual void SetParameter(std::string name, std::string value);
virtual const INMOST_DATA_ENUM_TYPE Iterations() const;
virtual const INMOST_DATA_REAL_TYPE Residual() const;
......
......@@ -2,11 +2,11 @@
namespace INMOST {
SolverFCBIILU2::SolverFCBIILU2() {
SolverFCBIILU2::SolverFCBIILU2(SolverParameters &parameters): SolverInterface(parameters) {
}
SolverFCBIILU2::SolverFCBIILU2(const SolverInterface *other) {
SolverFCBIILU2::SolverFCBIILU2(const SolverInterface *other): SolverInterface(other) {
const SolverFCBIILU2 *fcother = static_cast<const SolverFCBIILU2 *>(other);
SolverCopyDataFcbiilu2(&solver_data, fcother->solver_data, communicator);
if (fcother->matrix_data != NULL) {
......@@ -129,18 +129,6 @@ namespace INMOST {
return matrix_data != NULL;
}
void SolverFCBIILU2::SetDefaultParameters() {
}
SolverParameter SolverFCBIILU2::GetParameter(std::string name) const {
throw INMOST::SolverUnsupportedOperation;
}
void SolverFCBIILU2::SetParameter(std::string name, std::string value) {
throw INMOST::SolverUnsupportedOperation;
}
const INMOST_DATA_ENUM_TYPE SolverFCBIILU2::Iterations() const {
return static_cast<INMOST_DATA_ENUM_TYPE>(SolverIterationNumberFcbiilu2(solver_data));
}
......
......@@ -12,7 +12,7 @@ namespace INMOST {
void *matrix_data;
INMOST_DATA_ENUM_TYPE local_size, global_size;
public:
SolverFCBIILU2();
SolverFCBIILU2(SolverParameters &parameters);
SolverFCBIILU2(const SolverInterface *other);
......@@ -28,12 +28,6 @@ namespace INMOST {
virtual bool isMatrixSet();
virtual void SetDefaultParameters();
virtual SolverParameter GetParameter(std::string name) const;
virtual void SetParameter(std::string name, std::string value);
virtual const INMOST_DATA_ENUM_TYPE Iterations() const;
virtual const INMOST_DATA_REAL_TYPE Residual() const;
......
......@@ -2,11 +2,25 @@
namespace INMOST {
SolverInner::SolverInner() {
SolverInner::SolverInner(SolverParameters &parameters): SolverInterface(parameters) {
parameters.require("additive_schwartz_overlap", "1");
parameters.require("maximum_iterations", "2500");
parameters.require("reorder_nonzero", "1");
parameters.require("rescale_iterations", "6");
parameters.require("condition_estimation", "1");
parameters.require("adapt_ddpq_tolerance", "1");
parameters.require("gmres_substeps", "2");
parameters.require("absolute_tolerance", "1.0e-5");
parameters.require("relative_tolerance", "1.0e-12");
parameters.require("divergence_tolerance", "1.0e+100");
parameters.require("drop_tolerance", "0.005");
parameters.require("reuse_tolerance", "0.00005");
parameters.require("ddpq_tolerance", "0.75");
parameters.require("fill_level", "3");
}
SolverInner::SolverInner(const SolverInterface *other) {
SolverInner::SolverInner(const SolverInterface *other): SolverInterface(other) {
//You should not really want to copy solver's information
throw INMOST::SolverUnsupportedOperation;
}
......@@ -17,23 +31,23 @@ namespace INMOST {
}
void SolverInner::Initialize(int *argc, char ***argv, const char *parameters_file, std::string prefix) {
FILE *databaseFile = fopen(parameters_file, "r");
if (!databaseFile) {
return;
}
char *tmp = (char *) calloc(256, sizeof(char));
char *parameterName = (char *) calloc(128, sizeof(char));
char *parameterValue = (char *) calloc(128, sizeof(char));
while (!feof(databaseFile) && fgets(tmp, 256, databaseFile)) {
char *line = tmp;
//Comment str
if (line[0] == '#') continue;
sscanf(line, "%s %s", parameterName, parameterValue);
parameters.SetParameter(parameterName, parameterValue);
}
free(parameterValue);
free(parameterName);
free(tmp);
// FILE *databaseFile = fopen(parameters_file, "r");
// if (!databaseFile) {
// return;