-
Notifications
You must be signed in to change notification settings - Fork 25
/
emosqp_mex.c
492 lines (368 loc) · 11.6 KB
/
emosqp_mex.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
#include <string.h>
#include <mex.h>
#include "osqp.h"
#include "workspace.h"
/*********************************
* Timer Structs and Functions * *
*********************************/
// Windows
#ifdef IS_WINDOWS
#include <windows.h>
typedef struct {
LARGE_INTEGER tic;
LARGE_INTEGER toc;
LARGE_INTEGER freq;
} PyTimer;
// Mac
#elif defined IS_MAC
#include <mach/mach_time.h>
/* Use MAC OSX mach_time for timing */
typedef struct {
uint64_t tic;
uint64_t toc;
mach_timebase_info_data_t tinfo;
} PyTimer;
// Linux
#else
/* Use POSIX clocl_gettime() for timing on non-Windows machines */
#include <time.h>
#include <sys/time.h>
typedef struct {
struct timespec tic;
struct timespec toc;
} PyTimer;
#endif
/**
* Timer Methods
*/
// Windows
#ifdef IS_WINDOWS
void tic(PyTimer* t)
{
QueryPerformanceFrequency(&t->freq);
QueryPerformanceCounter(&t->tic);
}
c_float toc(PyTimer* t)
{
QueryPerformanceCounter(&t->toc);
return ((t->toc.QuadPart - t->tic.QuadPart) / (c_float)t->freq.QuadPart);
}
// Mac
#elif defined IS_MAC
void tic(PyTimer* t)
{
/* read current clock cycles */
t->tic = mach_absolute_time();
}
c_float toc(PyTimer* t)
{
uint64_t duration; /* elapsed time in clock cycles*/
t->toc = mach_absolute_time();
duration = t->toc - t->tic;
/*conversion from clock cycles to nanoseconds*/
mach_timebase_info(&(t->tinfo));
duration *= t->tinfo.numer;
duration /= t->tinfo.denom;
return (c_float)duration / 1e9;
}
// Linux
#else
/* read current time */
void tic(PyTimer* t)
{
clock_gettime(CLOCK_MONOTONIC, &t->tic);
}
/* return time passed since last call to tic on this timer */
c_float toc(PyTimer* t)
{
struct timespec temp;
clock_gettime(CLOCK_MONOTONIC, &t->toc);
if ((t->toc.tv_nsec - t->tic.tv_nsec)<0) {
temp.tv_sec = t->toc.tv_sec - t->tic.tv_sec-1;
temp.tv_nsec = 1e9+t->toc.tv_nsec - t->tic.tv_nsec;
} else {
temp.tv_sec = t->toc.tv_sec - t->tic.tv_sec;
temp.tv_nsec = t->toc.tv_nsec - t->tic.tv_nsec;
}
return (c_float)temp.tv_sec + (c_float)temp.tv_nsec / 1e9;
}
#endif
/****************************************
* END( Timer Structs and Functions ) * *
****************************************/
// Internal utility functions
c_float* copyToCfloatVector(double * vecData, c_int numel);
void castToDoubleArr(c_float *arr, double* arr_out, c_int len);
void setToNaN(double* arr_out, c_int len);
#if EMBEDDED != 1
c_int* copyDoubleToCintVector(double* vecData, c_int numel);
void change1To0Indexing(c_int *vecData, c_int numel);
#endif
// Function handler
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
// Get the command string
char cmd[64];
if (nrhs < 1 || mxGetString(prhs[0], cmd, sizeof(cmd)))
mexErrMsgTxt("First input should be a command string less than 64 characters long.");
// SOLVE
if (!strcmp("solve", cmd)) {
// Allocate timer
double solve_time;
PyTimer * timer;
timer = mxMalloc(sizeof(PyTimer));
if (nlhs != 5 || nrhs != 1){
mexErrMsgTxt("Solve : wrong number of inputs / outputs");
}
// solve the problem
tic(timer); // start timer
osqp_solve(&workspace);
solve_time = toc(timer); // stop timer
// Allocate space for solution
// primal variables
plhs[0] = mxCreateDoubleMatrix((&workspace)->data->n, 1, mxREAL);
// dual variables
plhs[1] = mxCreateDoubleMatrix((&workspace)->data->m, 1, mxREAL);
// status value
plhs[2] = mxCreateDoubleScalar((&workspace)->info->status_val);
// number of iterations
plhs[3] = mxCreateDoubleScalar((&workspace)->info->iter);
// solve time
plhs[4] = mxCreateDoubleScalar(solve_time);
//copy results to mxArray outputs
//assume that three outputs will always
//be returned to matlab-side class wrapper
if (((&workspace)->info->status_val != OSQP_PRIMAL_INFEASIBLE) &&
((&workspace)->info->status_val != OSQP_PRIMAL_INFEASIBLE_INACCURATE) &&
((&workspace)->info->status_val != OSQP_DUAL_INFEASIBLE) &&
((&workspace)->info->status_val != OSQP_DUAL_INFEASIBLE_INACCURATE)){
//primal variables
castToDoubleArr((&workspace)->solution->x, mxGetPr(plhs[0]), (&workspace)->data->n);
//dual variables
castToDoubleArr((&workspace)->solution->y, mxGetPr(plhs[1]), (&workspace)->data->m);
} else { // Problem is primal or dual infeasible -> NaN values
// Set primal and dual variables to NaN
setToNaN(mxGetPr(plhs[0]), (&workspace)->data->n);
setToNaN(mxGetPr(plhs[1]), (&workspace)->data->m);
}
return;
}
// update linear cost
if (!strcmp("update_lin_cost", cmd)) {
// Fill q
const mxArray *q = prhs[1];
// Copy vector to ensure it is cast as c_float
c_float *q_vec;
if(!mxIsEmpty(q)){
q_vec = copyToCfloatVector(mxGetPr(q), (&workspace)->data->n);
}
if(!mxIsEmpty(q)){
osqp_update_lin_cost(&workspace, q_vec);
}
// Free
if(!mxIsEmpty(q)) mxFree(q_vec);
return;
}
// update lower bound
if (!strcmp("update_lower_bound", cmd)) {
// Fill l
const mxArray *l = prhs[1];
// Copy vector to ensure it is cast as c_float
c_float *l_vec;
if(!mxIsEmpty(l)){
l_vec = copyToCfloatVector(mxGetPr(l), (&workspace)->data->m);
}
if(!mxIsEmpty(l)){
osqp_update_lower_bound(&workspace, l_vec);
}
// Free
if(!mxIsEmpty(l)) mxFree(l_vec);
return;
}
// update upper bound
if (!strcmp("update_upper_bound", cmd)) {
// Fill l
const mxArray *u = prhs[1];
// Copy vector to ensure it is cast as c_float
c_float *u_vec;
if(!mxIsEmpty(u)){
u_vec = copyToCfloatVector(mxGetPr(u), (&workspace)->data->m);
}
if(!mxIsEmpty(u)){
osqp_update_upper_bound(&workspace, u_vec);
}
// Free
if(!mxIsEmpty(u)) mxFree(u_vec);
return;
}
// update bounds
if (!strcmp("update_bounds", cmd)) {
// Fill l, u
const mxArray *l = prhs[1];
const mxArray *u = prhs[2];
// Copy vectors to ensure they are cast as c_float
c_float *l_vec;
c_float *u_vec;
if(!mxIsEmpty(l)){
l_vec = copyToCfloatVector(mxGetPr(l), (&workspace)->data->m);
}
if(!mxIsEmpty(u)){
u_vec = copyToCfloatVector(mxGetPr(u), (&workspace)->data->m);
}
if(!mxIsEmpty(u)){
osqp_update_bounds(&workspace, l_vec, u_vec);
}
// Free
if(!mxIsEmpty(l)) mxFree(l_vec);
if(!mxIsEmpty(u)) mxFree(u_vec);
return;
}
#if EMBEDDED != 1
// update matrix P
if (!strcmp("update_P", cmd)) {
// Fill Px and Px_idx
const mxArray *Px = prhs[1];
const mxArray *Px_idx = prhs[2];
int Px_n = mxGetScalar(prhs[3]);
if(Px_n == 0){
Px_n = (&workspace)->data->P->nzmax;
}
// Copy vectors to ensure they are cast as c_float and c_int
c_float *Px_vec;
c_int *Px_idx_vec = NULL;
if(!mxIsEmpty(Px)){
Px_vec = copyToCfloatVector(mxGetPr(Px), Px_n);
}
if(!mxIsEmpty(Px_idx)){
Px_idx_vec = copyDoubleToCintVector(mxGetPr(Px_idx), Px_n);
// Change indexing to match C 0-based one
change1To0Indexing(Px_idx_vec, Px_n);
}
if(!mxIsEmpty(Px)){
osqp_update_P((&workspace), Px_vec, Px_idx_vec, Px_n);
}
// Free
if(!mxIsEmpty(Px)) mxFree(Px_vec);
if(!mxIsEmpty(Px_idx)) mxFree(Px_idx_vec);
return;
}
// update matrix A
if (!strcmp("update_A", cmd)) {
// Fill Ax and Ax_idx
const mxArray *Ax = prhs[1];
const mxArray *Ax_idx = prhs[2];
int Ax_n = mxGetScalar(prhs[3]);
if(Ax_n == 0){
Ax_n = (&workspace)->data->A->nzmax;
}
// Copy vectors to ensure they are cast as c_float and c_int
c_float *Ax_vec;
c_int *Ax_idx_vec = NULL;
if(!mxIsEmpty(Ax)){
Ax_vec = copyToCfloatVector(mxGetPr(Ax), Ax_n);
}
if(!mxIsEmpty(Ax_idx)){
Ax_idx_vec = copyDoubleToCintVector(mxGetPr(Ax_idx), Ax_n);
// Change indexing to match C 0-based one
change1To0Indexing(Ax_idx_vec, Ax_n);
}
if(!mxIsEmpty(Ax)){
osqp_update_A((&workspace), Ax_vec, Ax_idx_vec, Ax_n);
}
// Free
if(!mxIsEmpty(Ax)) mxFree(Ax_vec);
if(!mxIsEmpty(Ax_idx)) mxFree(Ax_idx_vec);
return;
}
// update matrices P and A
if (!strcmp("update_P_A", cmd)) {
// Fill vectors
const mxArray *Px = prhs[1];
const mxArray *Px_idx = prhs[2];
const mxArray *Ax = prhs[4];
const mxArray *Ax_idx = prhs[5];
int Px_n = mxGetScalar(prhs[3]);
int Ax_n = mxGetScalar(prhs[6]);
if(Px_n == 0){
Px_n = (&workspace)->data->P->nzmax;
}
if(Ax_n == 0){
Ax_n = (&workspace)->data->A->nzmax;
}
// Copy vectors to ensure they are cast as c_float and c_int
c_float *Px_vec, *Ax_vec;
c_int *Px_idx_vec = NULL;
c_int *Ax_idx_vec = NULL;
if(!mxIsEmpty(Px)){
Px_vec = copyToCfloatVector(mxGetPr(Px), Px_n);
}
if(!mxIsEmpty(Px_idx)){
Px_idx_vec = copyDoubleToCintVector(mxGetPr(Px_idx), Px_n);
// Change indexing to match C 0-based one
change1To0Indexing(Px_idx_vec, Px_n);
}
if(!mxIsEmpty(Ax)){
Ax_vec = copyToCfloatVector(mxGetPr(Ax), Ax_n);
}
if(!mxIsEmpty(Ax_idx)){
Ax_idx_vec = copyDoubleToCintVector(mxGetPr(Ax_idx), Ax_n);
// Change indexing to match C 0-based one
change1To0Indexing(Ax_idx_vec, Ax_n);
}
if(!mxIsEmpty(Ax) && !mxIsEmpty(Px)){
osqp_update_P_A((&workspace), Px_vec, Px_idx_vec, Px_n,
Ax_vec, Ax_idx_vec, Ax_n);
}
// Free
if(!mxIsEmpty(Px)) mxFree(Px_vec);
if(!mxIsEmpty(Px_idx)) mxFree(Px_idx_vec);
if(!mxIsEmpty(Ax)) mxFree(Ax_vec);
if(!mxIsEmpty(Ax_idx)) mxFree(Ax_idx_vec);
return;
}
#endif // end EMBEDDED
// Got here, so command not recognized
mexErrMsgTxt("Command not recognized.");
}
c_float* copyToCfloatVector(double* vecData, c_int numel){
// This memory needs to be freed!
c_float* out;
c_int i;
out = mxMalloc(numel * sizeof(c_float));
//copy data
for(i=0; i < numel; i++){
out[i] = (c_float)vecData[i];
}
return out;
}
void castToDoubleArr(c_float *arr, double* arr_out, c_int len){
c_int i;
for (i = 0; i < len; i++) {
arr_out[i] = (double)arr[i];
}
}
void setToNaN(double* arr_out, c_int len){
c_int i;
for (i = 0; i < len; i++) {
arr_out[i] = mxGetNaN();
}
}
#if EMBEDDED != 1
c_int* copyDoubleToCintVector(double* vecData, c_int numel){
// This memory needs to be freed!
c_int* out;
c_int i;
out = mxMalloc(numel * sizeof(c_int));
//copy data
for(i=0; i < numel; i++){
out[i] = (c_int)vecData[i];
}
return out;
}
void change1To0Indexing(c_int *vecData, c_int numel){
c_int i; // Indexing
for(i=0; i < numel; i++){
vecData[i] -= 1; // Decrease index by 1
}
}
#endif // end EMBEDDED