osqp-python icon indicating copy to clipboard operation
osqp-python copied to clipboard

Python solve is faster than C code-generated one

Open xkhainguyen opened this issue 2 years ago • 4 comments

Hi,

I ran the quadrotor example in Python and generated C code in the standard way. I noticed that it intentionally generated code with an EMBEDDED flag then I have to use time.h to measure the solve time of the osqp_solve(&workspace) function.

Both Python and C gave me the same results but the solve times are unexpected. Python one is like 1ms and C one is like 3ms. I don't really get why this happened. Did anyone see this problem before?

Thanks.

xkhainguyen avatar Apr 26 '23 18:04 xkhainguyen

Do you have the same compiler optimization flags enabled in both cases?

goulart-paul avatar Apr 26 '23 19:04 goulart-paul

I just ran the Python code normally from .py file. My function looks like this prob.codegen('code', project_type='Makefile', force_rewrite=True, FLOAT=False)

I did not modify anything in the generated CMakeLists.txt.

if (NOT DEFINED EMBEDDED)  # enable embedded anyway
    set (EMBEDDED 1)
endif()
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O3")
set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -O0 -g")

But I do see this in osqp_configure.h.in

/* DEBUG */
#cmakedefine DEBUG

xkhainguyen avatar Apr 26 '23 19:04 xkhainguyen

By Python do you mean just calling solve on the problem, not using the codegen wrapper? If so, then there is an expected difference in the solve times because the code generation/embedded mode will disable certain features of the solver - such as the adaptive rho computation. Disabling those can possibly increase the time needed to solve the problem. One thing to try is to take the problem and take the optimal rho values from Python and put it into the code generation version (this should hopefully give you closer timings).

imciner2 avatar May 02 '23 17:05 imciner2

@imciner2 Thank you for your response. Intuitively, I thought for embedded mode, the features should encourage faster solve time. My naive expectation is that the generated C code should be faster given the same problem setup. If not, what is the goal and workflow of the embedded mode?

xkhainguyen avatar May 02 '23 18:05 xkhainguyen