Skip to content

Commit

Permalink
Removed pyamgcl.params class
Browse files Browse the repository at this point in the history
make_solver and make_preconditioner now take python dicts for params
  • Loading branch information
ddemidov committed Sep 25, 2014
1 parent e64acdd commit 5cd8d83
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 49 deletions.
66 changes: 19 additions & 47 deletions python/pyamgcl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,57 +31,36 @@ struct is_builtin_vector< numpy_boost<double,1> > : boost::true_type {};
}

//---------------------------------------------------------------------------
struct params {
params() {}

params(const boost::python::dict &args) {
using namespace boost::python;

for(stl_input_iterator<tuple> arg(args.items()), end; arg != end; ++arg) {
const char *name = extract<const char*>((*arg)[0]);
const char *type = extract<const char*>((*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<int>((*arg)[1]));
else
setf(name, extract<float>((*arg)[1]));
}
}
for(stl_input_iterator<tuple> arg(args.items()), end; arg != end; ++arg) {
const char *name = extract<const char*>((*arg)[0]);
const char *type = extract<const char*>((*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<int>((*arg)[1]));
else
p.put(name, extract<float>((*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 {
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<int, 1> &ptr,
const numpy_boost<int, 1> &col,
const numpy_boost<double, 1> &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<double, 1> &rhs) const {
Expand Down Expand Up @@ -118,20 +97,21 @@ struct make_solver {
amgcl::runtime::make_solver< amgcl::backend::builtin<double> > S;

mutable boost::tuple<int, double> cnv;

};

//---------------------------------------------------------------------------
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<int, 1> &ptr,
const numpy_boost<int, 1> &col,
const numpy_boost<double, 1> &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<double, 1> &rhs) const {
Expand Down Expand Up @@ -163,14 +143,6 @@ BOOST_PYTHON_MODULE(pyamgcl)
{
using namespace boost::python;

class_<params>("params", init<>())
.def(init<const boost::python::dict &>())
.def("__setitem__", &params::seti)
.def("__setitem__", &params::setf)
.def("__str__", &params::str)
.def("__repr__", &params::repr)
;

enum_<amgcl::runtime::coarsening::type>("coarsening")
.value("ruge_stuben", amgcl::runtime::coarsening::ruge_stuben)
.value("aggregation", amgcl::runtime::coarsening::aggregation)
Expand Down Expand Up @@ -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<int, 1>&,
const numpy_boost<int, 1>&,
const numpy_boost<double, 1>&
Expand All @@ -218,7 +190,7 @@ BOOST_PYTHON_MODULE(pyamgcl)
init<
amgcl::runtime::coarsening::type,
amgcl::runtime::relaxation::type,
const params&,
const dict&,
const numpy_boost<int, 1>&,
const numpy_boost<int, 1>&,
const numpy_boost<double, 1>&
Expand Down
4 changes: 2 additions & 2 deletions python/test_pyamgcl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit 5cd8d83

Please sign in to comment.