osqp / osqp-python

Python interface for OSQP

Home Page:https://osqp.org/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

FLOAT and LONG flags in code_generator.py/ codegen()

vifr9883 opened this issue · comments

Original post: OSQP Forum

It was recommended by @goulart-paul that I open an issue in GitHub.


Hello,

I am trying to generate C source code with the Python3 tool m.codegen(dir_name, **opts). I would like the resulting code to use float instead of double and to use int instead of long long. However, I believe I am not achieving these settings. The following is a description of the behavior I am experiencing.

Following the documentation, I tried running the following Python3 script:

import osqp

# Allocate problem data and src variable
... 

m = osqp.OSQP();
m.setup(P, q, A, l, u)
m.codegen(src, force_rewrite=True, parameters='matrices', FLOAT=True, LONG=False)

The result is a directory with the correct source code. In fact, I can compile the source code with the rest of my project and correctly solve the QP. However, I believe the type of c_int is long long and the type of c_float is double. I have tested this by printing, for example, as follows
printf( "Test c_int workspace->data->A->i[4] = %d, workspace->data->A->i[4]);
>> Test c_int workspace->data->A->i[4] = 467912
printf( "Test c_int workspace->data->A->i[4] = %lld, workspace->data->A->i[4]);
>> Test c_int workspace->data->A->i[4] = 1

The expected value of workspace->data->A->i[4] is 1. I can cast (int)workspace->data->A->i[4] and then I also get 1 as expected. But I believe this shows me the type of c_int is long long.

Furthermore, here are some lines in the file src/include/osqp_configure.h that seem to have auto-generated with unexpected content. I would expect #define DFLOAT for example.

/* EMBEDDED */
#define EMBEDDED (2)

/* PRINTING */
/* #undef PRINTING */

/* PROFILING */
/* #undef PROFILING */

/* CTRLC */
/* #undef CTRLC */

/* DFLOAT */
/* #undef DFLOAT */

/* DLONG */
#define DLONG

If I try to manually change this osqp_configure.h file, I get some compilation errors from the qdldl library with type mismatches.

I apologize for the long explanation. Any advice on how to correctly use the m.codegen(dir_name, **opts) Python3 method to generate code with the correct float and int typing would be appreciated.

Thank you for your time,

-Victor

As a temporary solution, the following changes to the autogenerated header files achieves the desired behavior:

// osqp_configure.h
...
/* DFLOAT */
#define DFLOAT

/* DLONG */
#undef DLONG
...
// qdldl_types.h
...
//typedef long long    QDLDL_int;   /* for indices */
typedef int    QDLDL_int;   /* for indices */
//typedef double  QDLDL_float; /* for numerical values  */
typedef float  QDLDL_float; /* for numerical values  */ 
...