11 EigenLDLTWrapper<S> solver;
13 Eigen::SparseMatrix<S, Eigen::ColMajor> matrix;
15 using Index =
typename Eigen::SparseMatrix<S, Eigen::ColMajor>::StorageIndex;
18 CSCMatrix<S, Index> d_matrix;
20 thrust::host_vector<S> h_x;
21 thrust::host_vector<S> h_b;
23 void fill_matrix_structure() {
24 const auto dim = d_matrix.d_pointers.size() - 1;
25 matrix.resize(dim, dim);
26 matrix.resizeNonZeros(d_matrix.d_values.size());
28 auto h_ptrs = matrix.outerIndexPtr();
29 auto h_indices = matrix.innerIndexPtr();
31 thrust::copy(d_matrix.d_pointers.begin(), d_matrix.d_pointers.end(),
33 thrust::copy(d_matrix.d_indices.begin(), d_matrix.d_indices.end(),
40 void fill_matrix_values() {
41 auto h_values = matrix.valuePtr();
42 thrust::copy(d_matrix.d_values.begin(), d_matrix.d_values.end(), h_values);
48 virtual void update_structure(Graph<T, S> *graph,
50 H.build_structure(graph, streams);
51 H.build_csc_structure(graph, d_matrix);
52 fill_matrix_structure();
53 solver.analyze_pattern(matrix);
56 virtual void update_values(Graph<T, S> *graph,
StreamPool &streams)
override {
57 H.update_values(graph, streams);
58 H.update_csc_values(graph, d_matrix);
62 virtual void set_damping_factor(Graph<T, S> *graph, T damping_factor,
64 H.apply_damping(graph, damping_factor, streams);
65 H.update_csc_values(graph, d_matrix);
70 virtual bool solve(Graph<T, S> *graph, T *x,
StreamPool &streams)
override {
72 auto dim = graph->get_hessian_dimension();
74 if (!solver.factorize(matrix)) {
75 std::cerr <<
"LDLT matrix decomposition failed!";
79 thrust::fill(thrust::device, x, x + dim,
static_cast<T
>(0.0));
81 thrust::copy(graph->get_b().begin(), graph->get_b().end(), h_b.data());
82 thrust::device_ptr<T> d_x(
84 thrust::copy(d_x, d_x + dim, h_x.data());
86 auto map_b = VecMap<S>(h_b.data(), dim, 1);
87 auto map_x = VecMap<S>(h_x.data(), dim, 1);
88 if (!solver.solve(map_b, map_x)) {
89 std::cerr <<
"LDLT solve failed!";
93 thrust::copy(h_x.begin(), h_x.end(), d_x);