From 5cd8d83b9dc4e616deae59e20dfeb3c9359dff5d Mon Sep 17 00:00:00 2001 From: Denis Demidov Date: Thu, 25 Sep 2014 22:16:44 +0400 Subject: [PATCH] Removed pyamgcl.params class make_solver and make_preconditioner now take python dicts for params --- python/pyamgcl.cpp | 66 +++++++++++++-------------------------------- python/test_pyamgcl | 4 +-- 2 files changed, 21 insertions(+), 49 deletions(-) diff --git a/python/pyamgcl.cpp b/python/pyamgcl.cpp index 381b51cd..66d9d750 100644 --- a/python/pyamgcl.cpp +++ b/python/pyamgcl.cpp @@ -31,43 +31,22 @@ struct is_builtin_vector< numpy_boost > : boost::true_type {}; } //--------------------------------------------------------------------------- -struct params { - params() {} - - params(const boost::python::dict &args) { - using namespace boost::python; - - for(stl_input_iterator arg(args.items()), end; arg != end; ++arg) { - const char *name = extract((*arg)[0]); - const char *type = extract((*arg)[1].attr("__class__").attr("__name__")); +boost::property_tree::ptree make_ptree(const boost::python::dict &args) { + using namespace boost::python; + boost::property_tree::ptree p; - if (strcmp(type, "int") == 0) - seti(name, extract((*arg)[1])); - else - setf(name, extract((*arg)[1])); - } - } + for(stl_input_iterator arg(args.items()), end; arg != end; ++arg) { + const char *name = extract((*arg)[0]); + const char *type = extract((*arg)[1].attr("__class__").attr("__name__")); - void seti(const char *name, int value) { - p.put(name, value); + if (strcmp(type, "int") == 0) + p.put(name, extract((*arg)[1])); + else + p.put(name, extract((*arg)[1])); } - void setf(const char *name, float value) { - p.put(name, value); - } - - std::string str() const { - std::ostringstream buf; - write_json(buf, p); - return buf.str(); - } - - std::string repr() const { - return "amgcl params: " + str(); - } - - boost::property_tree::ptree p; -}; + return p; +} //--------------------------------------------------------------------------- struct make_solver { @@ -75,13 +54,13 @@ struct make_solver { amgcl::runtime::coarsening::type coarsening, amgcl::runtime::relaxation::type relaxation, amgcl::runtime::solver::type solver, - const params &prm, + const boost::python::dict &prm, const numpy_boost &ptr, const numpy_boost &col, const numpy_boost &val ) : n(ptr.num_elements() - 1), - S(coarsening, relaxation, solver, boost::tie(n, ptr, col, val), prm.p) + S(coarsening, relaxation, solver, boost::tie(n, ptr, col, val), make_ptree(prm)) { } PyObject* solve(const numpy_boost &rhs) const { @@ -118,6 +97,7 @@ struct make_solver { amgcl::runtime::make_solver< amgcl::backend::builtin > S; mutable boost::tuple cnv; + }; //--------------------------------------------------------------------------- @@ -125,13 +105,13 @@ struct make_preconditioner { make_preconditioner( amgcl::runtime::coarsening::type coarsening, amgcl::runtime::relaxation::type relaxation, - const params &prm, + const boost::python::dict &prm, const numpy_boost &ptr, const numpy_boost &col, const numpy_boost &val ) : n(ptr.num_elements() - 1), - P(coarsening, relaxation, boost::tie(n, ptr, col, val), prm.p) + P(coarsening, relaxation, boost::tie(n, ptr, col, val), make_ptree(prm)) { } PyObject* apply(const numpy_boost &rhs) const { @@ -163,14 +143,6 @@ BOOST_PYTHON_MODULE(pyamgcl) { using namespace boost::python; - class_("params", init<>()) - .def(init()) - .def("__setitem__", ¶ms::seti) - .def("__setitem__", ¶ms::setf) - .def("__str__", ¶ms::str) - .def("__repr__", ¶ms::repr) - ; - enum_("coarsening") .value("ruge_stuben", amgcl::runtime::coarsening::ruge_stuben) .value("aggregation", amgcl::runtime::coarsening::aggregation) @@ -202,7 +174,7 @@ BOOST_PYTHON_MODULE(pyamgcl) amgcl::runtime::coarsening::type, amgcl::runtime::relaxation::type, amgcl::runtime::solver::type, - const params&, + const dict&, const numpy_boost&, const numpy_boost&, const numpy_boost& @@ -218,7 +190,7 @@ BOOST_PYTHON_MODULE(pyamgcl) init< amgcl::runtime::coarsening::type, amgcl::runtime::relaxation::type, - const params&, + const dict&, const numpy_boost&, const numpy_boost&, const numpy_boost& diff --git a/python/test_pyamgcl b/python/test_pyamgcl index 19c3c77f..246afab9 100755 --- a/python/test_pyamgcl +++ b/python/test_pyamgcl @@ -50,7 +50,7 @@ class TestPyAMGCL(unittest.TestCase): amg.coarsening.smoothed_aggregation, amg.relaxation.spai0, amg.solver_type.bicgstab, - amg.params({"tol" : 1e-8}), + {"tol" : 1e-8}, A.indptr.astype(np.int32), A.indices.astype(np.int32), A.data.astype(np.float64) @@ -69,7 +69,7 @@ class TestPyAMGCL(unittest.TestCase): P = amg.make_preconditioner( amg.coarsening.smoothed_aggregation, amg.relaxation.spai0, - amg.params(), + {}, A.indptr.astype(np.int32), A.indices.astype(np.int32), A.data.astype(np.float64)