14 EigenLDLTWrapper<S, Index> solver;
16 Eigen::SparseMatrix<S, Eigen::ColMajor, Index> matrix;
19 CSCMatrix<S, Index> d_matrix;
21 thrust::host_vector<S> h_x;
22 thrust::host_vector<S> h_b;
24 void fill_matrix_structure() {
25 const auto dim = d_matrix.d_pointers.size() - 1;
26 matrix.resize(dim, dim);
27 matrix.resizeNonZeros(d_matrix.d_values.size());
29 auto h_ptrs = matrix.outerIndexPtr();
30 auto h_indices = matrix.innerIndexPtr();
32 thrust::copy(d_matrix.d_pointers.begin(), d_matrix.d_pointers.end(),
34 thrust::copy(d_matrix.d_indices.begin(), d_matrix.d_indices.end(),
41 void fill_matrix_values() {
42 auto h_values = matrix.valuePtr();
43 thrust::copy(d_matrix.d_values.begin(), d_matrix.d_values.end(), h_values);
49 virtual void update_structure(Graph<T, S> *graph,
51 H.build_structure(graph, streams);
52 H.build_csc_structure(graph, d_matrix);
53 fill_matrix_structure();
54 solver.analyze_pattern(matrix);
57 virtual void update_values(Graph<T, S> *graph,
StreamPool &streams)
override {
58 H.update_values(graph, streams);
59 H.update_csc_values(graph, d_matrix);
63 virtual void set_damping_factor(Graph<T, S> *graph, T damping_factor,
64 const bool use_identity,
66 H.apply_damping(graph, damping_factor, use_identity, streams);
67 H.update_csc_values(graph, d_matrix);
72 virtual bool solve(Graph<T, S> *graph, T *x,
StreamPool &streams)
override {
74 auto dim = graph->get_hessian_dimension();
76 if (!solver.factorize(matrix)) {
77 std::cerr <<
"LDLT matrix decomposition failed!";
81 thrust::fill(thrust::device, x, x + dim,
static_cast<T
>(0.0));
83 thrust::copy(graph->get_b().begin(), graph->get_b().end(), h_b.data());
84 thrust::device_ptr<T> d_x(
86 thrust::copy(d_x, d_x + dim, h_x.data());
88 auto map_b = VecMap<S>(h_b.data(), dim, 1);
89 auto map_x = VecMap<S>(h_x.data(), dim, 1);
90 if (!solver.solve(map_b, map_x)) {
91 std::cerr <<
"LDLT solve failed!";
95 thrust::copy(h_x.begin(), h_x.end(), d_x);