util.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. #ifndef UTIL_H
  2. # define UTIL_H
  3. # ifdef __cplusplus
  4. extern "C" {
  5. # endif // ifdef __cplusplus
  6. # include "types.h"
  7. # include "constants.h"
  8. /******************
  9. * Versioning *
  10. ******************/
  11. /**
  12. * Return OSQP version
  13. * @return OSQP version
  14. */
  15. const char* osqp_version(void);
  16. /**********************
  17. * Utility Functions *
  18. **********************/
  19. # ifndef EMBEDDED
  20. /**
  21. * Copy settings creating a new settings structure (uses MALLOC)
  22. * @param settings Settings to be copied
  23. * @return New settings structure
  24. */
  25. OSQPSettings* copy_settings(const OSQPSettings *settings);
  26. # endif // #ifndef EMBEDDED
  27. /**
  28. * Custom string copy to avoid string.h library
  29. * @param dest destination string
  30. * @param source source string
  31. */
  32. void c_strcpy(char dest[],
  33. const char source[]);
  34. # ifdef PRINTING
  35. /**
  36. * Print Header before running the algorithm
  37. * @param work osqp workspace
  38. */
  39. void print_setup_header(const OSQPWorkspace *work);
  40. /**
  41. * Print header with data to be displayed per iteration
  42. */
  43. void print_header(void);
  44. /**
  45. * Print iteration summary
  46. * @param work current workspace
  47. */
  48. void print_summary(OSQPWorkspace *work);
  49. /**
  50. * Print information after polish
  51. * @param work current workspace
  52. */
  53. void print_polish(OSQPWorkspace *work);
  54. /**
  55. * Print footer when algorithm terminates
  56. * @param info info structure
  57. * @param polish is polish enabled?
  58. */
  59. void print_footer(OSQPInfo *info,
  60. c_int polish);
  61. # endif // ifdef PRINTING
  62. /*********************************
  63. * Timer Structs and Functions * *
  64. *********************************/
  65. /*! \cond PRIVATE */
  66. # ifdef PROFILING
  67. // Windows
  68. # ifdef IS_WINDOWS
  69. // Some R packages clash with elements
  70. // of the windows.h header, so use a
  71. // slimmer version for conflict avoidance
  72. # ifdef R_LANG
  73. #define NOGDI
  74. # endif
  75. # include <windows.h>
  76. struct OSQP_TIMER {
  77. LARGE_INTEGER tic;
  78. LARGE_INTEGER toc;
  79. LARGE_INTEGER freq;
  80. };
  81. // Mac
  82. # elif defined IS_MAC
  83. # include <mach/mach_time.h>
  84. /* Use MAC OSX mach_time for timing */
  85. struct OSQP_TIMER {
  86. uint64_t tic;
  87. uint64_t toc;
  88. mach_timebase_info_data_t tinfo;
  89. };
  90. // Linux
  91. # else // ifdef IS_WINDOWS
  92. /* Use POSIX clock_gettime() for timing on non-Windows machines */
  93. # include <time.h>
  94. # include <sys/time.h>
  95. struct OSQP_TIMER {
  96. struct timespec tic;
  97. struct timespec toc;
  98. };
  99. # endif // ifdef IS_WINDOWS
  100. /*! \endcond */
  101. /**
  102. * Timer Methods
  103. */
  104. /**
  105. * Start timer
  106. * @param t Timer object
  107. */
  108. void osqp_tic(OSQPTimer *t);
  109. /**
  110. * Report time
  111. * @param t Timer object
  112. * @return Reported time
  113. */
  114. c_float osqp_toc(OSQPTimer *t);
  115. # endif /* END #ifdef PROFILING */
  116. /* ================================= DEBUG FUNCTIONS ======================= */
  117. /*! \cond PRIVATE */
  118. # ifndef EMBEDDED
  119. /* Compare CSC matrices */
  120. c_int is_eq_csc(csc *A,
  121. csc *B,
  122. c_float tol);
  123. /* Convert sparse CSC to dense */
  124. c_float* csc_to_dns(csc *M);
  125. # endif // #ifndef EMBEDDED
  126. # ifdef PRINTING
  127. # include <stdio.h>
  128. /* Print a csc sparse matrix */
  129. void print_csc_matrix(csc *M,
  130. const char *name);
  131. /* Dump csc sparse matrix to file */
  132. void dump_csc_matrix(csc *M,
  133. const char *file_name);
  134. /* Print a triplet format sparse matrix */
  135. void print_trip_matrix(csc *M,
  136. const char *name);
  137. /* Print a dense matrix */
  138. void print_dns_matrix(c_float *M,
  139. c_int m,
  140. c_int n,
  141. const char *name);
  142. /* Print vector */
  143. void print_vec(c_float *v,
  144. c_int n,
  145. const char *name);
  146. /* Dump vector to file */
  147. void dump_vec(c_float *v,
  148. c_int len,
  149. const char *file_name);
  150. // Print int array
  151. void print_vec_int(c_int *x,
  152. c_int n,
  153. const char *name);
  154. # endif // ifdef PRINTING
  155. /*! \endcond */
  156. # ifdef __cplusplus
  157. }
  158. # endif // ifdef __cplusplus
  159. #endif // ifndef UTIL_H