FLOAT and LONG flags in Code Generation with Python3


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.

#define EMBEDDED (2)

/* #undef PRINTING */

/* #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,


It’s entirely possible that this feature is broken - could you open it as an issue in github?

As a temporary workaround, you could modify osqp_configure.h has you have done. There is a very similar header called qdldl_types.h that would require complementary changes and which works in basically the same way.

Thank you, the suggested temporary workaround worked as expected. The changes in qdldl_types.h are

//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  */ 

Here is the GitHub issue: