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