qdldl_interface.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. #ifndef QDLDL_INTERFACE_H
  2. #define QDLDL_INTERFACE_H
  3. #ifdef __cplusplus
  4. extern "C" {
  5. #endif
  6. #include "types.h"
  7. #include "qdldl_types.h"
  8. /**
  9. * QDLDL solver structure
  10. */
  11. typedef struct qdldl qdldl_solver;
  12. struct qdldl {
  13. enum linsys_solver_type type;
  14. /**
  15. * @name Functions
  16. * @{
  17. */
  18. c_int (*solve)(struct qdldl * self, c_float * b);
  19. #ifndef EMBEDDED
  20. void (*free)(struct qdldl * self); ///< Free workspace (only if desktop)
  21. #endif
  22. // This used only in non embedded or embedded 2 version
  23. #if EMBEDDED != 1
  24. c_int (*update_matrices)(struct qdldl * self, const csc *P, const csc *A); ///< Update solver matrices
  25. c_int (*update_rho_vec)(struct qdldl * self, const c_float * rho_vec); ///< Update rho_vec parameter
  26. #endif
  27. #ifndef EMBEDDED
  28. c_int nthreads;
  29. #endif
  30. /** @} */
  31. /**
  32. * @name Attributes
  33. * @{
  34. */
  35. csc *L; ///< lower triangular matrix in LDL factorization
  36. c_float *Dinv; ///< inverse of diag matrix in LDL (as a vector)
  37. c_int *P; ///< permutation of KKT matrix for factorization
  38. c_float *bp; ///< workspace memory for solves
  39. c_float *sol; ///< solution to the KKT system
  40. c_float *rho_inv_vec; ///< parameter vector
  41. c_float sigma; ///< scalar parameter
  42. #ifndef EMBEDDED
  43. c_int polish; ///< polishing flag
  44. #endif
  45. c_int n; ///< number of QP variables
  46. c_int m; ///< number of QP constraints
  47. #if EMBEDDED != 1
  48. // These are required for matrix updates
  49. c_int * Pdiag_idx, Pdiag_n; ///< index and number of diagonal elements in P
  50. csc * KKT; ///< Permuted KKT matrix in sparse form (used to update P and A matrices)
  51. c_int * PtoKKT, * AtoKKT; ///< Index of elements from P and A to KKT matrix
  52. c_int * rhotoKKT; ///< Index of rho places in KKT matrix
  53. // QDLDL Numeric workspace
  54. QDLDL_float *D;
  55. QDLDL_int *etree;
  56. QDLDL_int *Lnz;
  57. QDLDL_int *iwork;
  58. QDLDL_bool *bwork;
  59. QDLDL_float *fwork;
  60. #endif
  61. /** @} */
  62. };
  63. /**
  64. * Initialize QDLDL Solver
  65. *
  66. * @param s Pointer to a private structure
  67. * @param P Cost function matrix (upper triangular form)
  68. * @param A Constraints matrix
  69. * @param sigma Algorithm parameter. If polish, then sigma = delta.
  70. * @param rho_vec Algorithm parameter. If polish, then rho_vec = OSQP_NULL.
  71. * @param polish Flag whether we are initializing for polish or not
  72. * @return Exitflag for error (0 if no errors)
  73. */
  74. c_int init_linsys_solver_qdldl(qdldl_solver ** sp, const csc * P, const csc * A, c_float sigma, const c_float * rho_vec, c_int polish);
  75. /**
  76. * Solve linear system and store result in b
  77. * @param s Linear system solver structure
  78. * @param b Right-hand side
  79. * @return Exitflag
  80. */
  81. c_int solve_linsys_qdldl(qdldl_solver * s, c_float * b);
  82. #if EMBEDDED != 1
  83. /**
  84. * Update linear system solver matrices
  85. * @param s Linear system solver structure
  86. * @param P Matrix P
  87. * @param A Matrix A
  88. * @return Exitflag
  89. */
  90. c_int update_linsys_solver_matrices_qdldl(qdldl_solver * s, const csc *P, const csc *A);
  91. /**
  92. * Update rho_vec parameter in linear system solver structure
  93. * @param s Linear system solver structure
  94. * @param rho_vec new rho_vec value
  95. * @return exitflag
  96. */
  97. c_int update_linsys_solver_rho_vec_qdldl(qdldl_solver * s, const c_float * rho_vec);
  98. #endif
  99. #ifndef EMBEDDED
  100. /**
  101. * Free linear system solver
  102. * @param s linear system solver object
  103. */
  104. void free_linsys_solver_qdldl(qdldl_solver * s);
  105. #endif
  106. #ifdef __cplusplus
  107. }
  108. #endif
  109. #endif