-
Notifications
You must be signed in to change notification settings - Fork 16
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
Add AMGCL_cuda #70
base: main
Are you sure you want to change the base?
Add AMGCL_cuda #70
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,7 +15,8 @@ | |
"Eigen::MINRES", | ||
"Pardiso", | ||
"Hypre", | ||
"AMGCL" | ||
"AMGCL", | ||
"AMGCL_cuda" | ||
], | ||
"doc": "Settings for the linear solver." | ||
}, | ||
|
@@ -42,6 +43,7 @@ | |
"Pardiso", | ||
"Hypre", | ||
"AMGCL", | ||
"AMGCL_cuda", | ||
"Eigen::LeastSquaresConjugateGradient", | ||
"Eigen::DGMRES", | ||
"Eigen::ConjugateGradient", | ||
|
@@ -153,6 +155,16 @@ | |
], | ||
"doc": "Settings for the AMGCL solver." | ||
}, | ||
{ | ||
"pointer": "/AMGCL_cuda", | ||
"default": null, | ||
"type": "object", | ||
"optional": [ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. does it have opts? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added. |
||
"solver", | ||
"precond" | ||
], | ||
"doc": "Settings for the AMGCL_cuda solver." | ||
}, | ||
{ | ||
"pointer": "/Eigen::LeastSquaresConjugateGradient/max_iter", | ||
"default": 1000, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,173 @@ | ||
#ifdef POLYSOLVE_WITH_AMGCL | ||
// #ifdef POLYSOLVE_WITH_CUDA | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i think we need this There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I commented it out because polysolve CMakeLists.txt does not have POLYSOLVE_WITH_CUDA flag, but polyfem has. So I deleted the flag here. |
||
|
||
//////////////////////////////////////////////////////////////////////////////// | ||
#include "AMGCL_cuda.hpp" | ||
|
||
#include <boost/property_tree/ptree.hpp> | ||
#include <boost/property_tree/json_parser.hpp> | ||
//////////////////////////////////////////////////////////////////////////////// | ||
|
||
namespace polysolve::linear | ||
{ | ||
|
||
namespace | ||
{ | ||
/* https://stackoverflow.com/questions/15904896/range-based-for-loop-on-a-dynamic-array */ | ||
template <typename T> | ||
struct WrappedArray | ||
{ | ||
WrappedArray(const T *first, const T *last) | ||
: begin_{first}, end_{last} {} | ||
WrappedArray(const T *first, std::ptrdiff_t size) | ||
: WrappedArray{first, first + size} {} | ||
|
||
const T *begin() const noexcept { return begin_; } | ||
const T *end() const noexcept { return end_; } | ||
const T &operator[](const size_t i) const { return begin_[i]; } | ||
|
||
const T *begin_; | ||
const T *end_; | ||
}; | ||
|
||
json default_params() | ||
{ | ||
json params = R"({ | ||
"precond": { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. thse should go in the spec There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Moved. |
||
"relax": { | ||
"type": "spai0" | ||
}, | ||
"class": "amg", | ||
"max_levels": 6, | ||
"direct_coarse": false, | ||
"ncycle": 2, | ||
"coarsening": { | ||
"type": "smoothed_aggregation", | ||
"estimate_spectral_radius": true, | ||
"relax": 1, | ||
"aggr": { | ||
"eps_strong": 0 | ||
} | ||
} | ||
}, | ||
"solver": { | ||
"tol": 1e-10, | ||
"maxiter": 1000, | ||
"type": "cg" | ||
} | ||
})"_json; | ||
|
||
return params; | ||
} | ||
|
||
void set_params(const json ¶ms, json &out) | ||
{ | ||
if (params.contains("AMGCL_cuda")) | ||
{ | ||
// Patch the stored params with input ones | ||
if (params["AMGCL_cuda"].contains("precond")) | ||
out["precond"].merge_patch(params["AMGCL_cuda"]["precond"]); | ||
if (params["AMGCL_cuda"].contains("solver")) | ||
out["solver"].merge_patch(params["AMGCL_cuda"]["solver"]); | ||
|
||
if (out["precond"]["class"] == "schur_pressure_correction") | ||
{ | ||
// Initialize the u and p solvers with a tolerance that is comparable to the main solver's | ||
if (!out["precond"].contains("usolver")) | ||
{ | ||
out["precond"]["usolver"] = R"({"solver": {"maxiter": 100}})"_json; | ||
out["precond"]["usolver"]["solver"]["tol"] = 10 * out["solver"]["tol"].get<double>(); | ||
} | ||
if (!out["precond"].contains("usolver")) | ||
{ | ||
out["precond"]["psolver"] = R"({"solver": {"maxiter": 100}})"_json; | ||
out["precond"]["psolver"]["solver"]["tol"] = 10 * out["solver"]["tol"].get<double>(); | ||
} | ||
} | ||
} | ||
} | ||
} // namespace | ||
|
||
//////////////////////////////////////////////////////////////////////////////// | ||
|
||
AMGCL_cuda::AMGCL_cuda() | ||
{ | ||
params_ = default_params(); | ||
// NOTE: usolver and psolver parameters are only used if the | ||
// preconditioner class is "schur_pressure_correction" | ||
precond_num_ = 0; | ||
cusparseCreate(&backend_params_.cusparse_handle); | ||
} | ||
|
||
// Set solver parameters | ||
void AMGCL_cuda::set_parameters(const json ¶ms) | ||
{ | ||
if (params.contains("AMGCL_cuda")) | ||
{ | ||
set_params(params, params_); | ||
} | ||
} | ||
|
||
void AMGCL_cuda::get_info(json ¶ms) const | ||
{ | ||
params["num_iterations"] = iterations_; | ||
params["final_res_norm"] = residual_error_; | ||
} | ||
|
||
//////////////////////////////////////////////////////////////////////////////// | ||
|
||
void AMGCL_cuda::factorize(const StiffnessMatrix &Ain) | ||
{ | ||
assert(precond_num_ > 0); | ||
|
||
int numRows = Ain.rows(); | ||
|
||
WrappedArray<StiffnessMatrix::StorageIndex> ia(Ain.outerIndexPtr(), numRows + 1); | ||
WrappedArray<StiffnessMatrix::StorageIndex> ja(Ain.innerIndexPtr(), Ain.nonZeros()); | ||
WrappedArray<StiffnessMatrix::Scalar> a(Ain.valuePtr(), Ain.nonZeros()); | ||
if (params_["precond"]["class"] == "schur_pressure_correction") | ||
{ | ||
std::vector<char> pmask(numRows, 0); | ||
for (size_t i = precond_num_; i < numRows; ++i) | ||
pmask[i] = 1; | ||
params_["precond"]["pmask"] = pmask; | ||
} | ||
|
||
// AMGCL takes the parameters as a Boost property_tree (i.e., another JSON data structure) | ||
std::stringstream ss_params; | ||
ss_params << params_; | ||
boost::property_tree::ptree pt_params; | ||
boost::property_tree::read_json(ss_params, pt_params); | ||
auto A = std::tie(numRows, ia, ja, a); | ||
solver_ = std::make_unique<Solver>(A, pt_params, backend_params_); | ||
// std::cout << *solver_.get() << std::endl; | ||
iterations_ = 0; | ||
residual_error_ = 0; | ||
} | ||
|
||
//////////////////////////////////////////////////////////////////////////////// | ||
|
||
//////////////////////////////////////////////////////////////////////////////// | ||
|
||
void AMGCL_cuda::solve(const Eigen::Ref<const VectorXd> rhs, Eigen::Ref<VectorXd> result) | ||
{ | ||
assert(result.size() == rhs.size()); | ||
std::vector<double> _rhs(rhs.data(), rhs.data() + rhs.size()); | ||
std::vector<double> x(result.data(), result.data() + result.size()); | ||
auto rhs_b = Backend::copy_vector(_rhs, backend_params_); | ||
auto x_b = Backend::copy_vector(x, backend_params_); | ||
|
||
std::tie(iterations_, residual_error_) = (*solver_)(*rhs_b, *x_b); | ||
thrust::copy(x_b->begin(), x_b->end(), result.data()); | ||
} | ||
|
||
//////////////////////////////////////////////////////////////////////////////// | ||
|
||
AMGCL_cuda::~AMGCL_cuda() | ||
{ | ||
} | ||
|
||
} // namespace polysolve | ||
|
||
// #endif | ||
#endif |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
leave pardiso off
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed.