cvxgrp / cvxpygen

Code generation with CVXPY
Apache License 2.0
129 stars 17 forks source link

Using CVPYGEN generated code for Microcontroller #54

Open karthyyy opened 3 weeks ago

karthyyy commented 3 weeks ago

Hi,

Thanks for sharing CVXPYGEN. I am able to install the same and generate code seamlessly on my Laptop. I am having a doubt regarding its architecture specific compilation and portability in lower end microcontrollers. In the paper "Embedded Code Generation With CVXPY", the generated code is executed on intel Atom processor.

1) Is there any help/tutorial showing how to change the CMake file targeting specific hardware architecture ?

2) Whether the generated code can be run on any Embedded Microcontroller with RISC-V or ARM architecture ?

Thanks in advance.

Regards Karthik

maxschaller commented 3 weeks ago

To execute the generated code on the Intel Atom processor, we did not adapt the generated CMakeLists.txt.

  1. The generated code for canoncalization and retrieval is library- allocation- and division-free. If the solver OSQP is used, then this holds true for all the generated code. This way, you don't have to worry about things like hardware-specific compilation of any external libraries etc. The generated CMakeLists.txt exposes cpg_head and cpg_src to the parent scope, such that the generated solver can be included in any C/C++ project as a subdirectory. We expect that hardware-specific customizations are done in the main directory's CMakeLists.txt (written by the user).

  2. Feel free to share your experience with your microcontroller.

Let me know if you have any further questions.

karthyyy commented 3 weeks ago

Hi @maxschaller ,

Thanks for the prompt reply and explanation. I shall start setting it up for my controller and will update you the progress.

Thanks and Regards Karthik

karthyyy commented 2 weeks ago

Hi @maxschaller,

I am trying to generate code for a simple example using CVXPYGEN. when executing the command "cpg.generate_code(problem, code_dir='cvxpygen_test', solver='OSQP')", following messages are displayed in the vscode terminal.

PS D:\.....\CVXPYGEN>

.....
Generating code with CVXPYgen ...
-----------------------------------------------------------------
           OSQP v0.6.3  -  Operator Splitting QP Solver
              (c) Bartolomeo Stellato,  Goran Banjac
        University of Oxford  -  Stanford University 2021
-----------------------------------------------------------------
problem:  variables n = 3, constraints m = 3
          nnz(P) + nnz(A) = 7
settings: linear system solver = qdldl,
          eps_abs = 1.0e-03, eps_rel = 1.0e-03,
          eps_prim_inf = 1.0e-04, eps_dual_inf = 1.0e-04,
          rho = 1.00e-01 (adaptive),
          sigma = 1.00e-06, alpha = 1.60, max_iter = 4000
          check_termination: on (interval 25),
          scaling: on, scaled_termination: off
          warm start: on, polish: off, time_limit: off

Getting workspace from OSQP object...                           [done]
Creating target directories...                                  [done]
Copying OSQP sources...                                         [done]
Generating customized code...                                   [done]
Creating project...
CMake Error: CMake was unable to find a build program corresponding to "MinGW Makefiles".  CMAKE_MAKE_PROGRAM is not set.  You probably need to select a different build tool.
CMake Error: CMAKE_C_COMPILER not set, after EnableLanguage
CMake Error: CMAKE_CXX_COMPILER not set, after EnableLanguage
-- **Configuring incomplete, errors occurred!**
[done]
Compiling Python wrapper...                                     C:\......\env_test\lib\site-packages\setuptools\__init__.py:80: _**DeprecatedInstaller: setuptools.installer and fetch_build_eggs are deprecated.**
!!

        ********************************************************************************
        Requirements should be satisfied by a PEP 517 installer.
        If you are using pip, you can try `pip install --use-pep517`.
        ********************************************************************************

!!
  dist.fetch_build_eggs(dist.setup_requires)
emosqpmodule.c
auxil.c
error.c
kkt.c
lin_alg.c
osqp.c
proj.c
qdldl.c
qdldl_interface.c
scaling.c
util.c
workspace.c
   Creating library build\temp.win-amd64-cpython-39\Release\emosqp.cp39-win_amd64.lib and object build\temp.win-amd64-cpython-39\Release\emosqp.cp39-win_amd64.exp
Generating code
Finished generating code
[done]
Copying code-generated Python solver to current directory...    [done]
CVXPYgen finished generating code.
Compiling python wrapper with CVXPYgen ...
C:\....\env_test\lib\site-packages\setuptools\__init__.py:80: _DeprecatedInstaller: setuptools.installer and fetch_build_eggs are deprecated.
!!

        ********************************************************************************
        Requirements should be satisfied by a PEP 517 installer.
        If you are using pip, you can try `pip install --use-pep517`.
        ********************************************************************************

!!
  dist.fetch_build_eggs(dist.setup_requires)
-- The C compiler identification is MSVC 19.41.34120.0
-- The CXX compiler identification is MSVC 19.41.34120.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: C:/Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/MSVC/14.41.34120/bin/Hostx64/x64/cl.exe - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: C:/Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/MSVC/14.41.34120/bin/Hostx64/x64/cl.exe - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- We are on a Windows system
-- Embedded is 2
-- Passing EMBEDDED flag to compiler
-- Disabling printing for embedded
-- Printing is OFF
-- Disabling profiling for embedded
-- Profiling is OFF
-- Floats are OFF
-- Long integers (64bit) are ON
-- Configuring done (14.0s)
-- Generating done (0.3s)
-- Build files have been written to: D:/....../cvxpygen_test/c/build
MSBuild version 17.11.2+c078802d4 for .NET Framework

  1>Checking Build System
  Building Custom Rule D:/....../cvxpygen_test/c/CMakeLists.txt
  cpg_workspace.c
  cpg_solve.c
  auxil.c
  error.c
  lin_alg.c
  osqp.c
  proj.c
  scaling.c
  util.c
  kkt.c
  workspace.c
  qdldl.c
  qdldl_interface.c
  Generating Code...
  cpg.vcxproj -> D:\.......\cvxpygen_test\c\build\out\Release\cpg.lib
cpg_module.cpp
cpp\src\cpg_module.cpp(47): warning C4244: '=': conversion from 'cpg_int' to 'int', possible loss of data
   Creating library build\temp.win-amd64-cpython-39\Release\cpp\src\cpg_module.cp39-win_amd64.lib and object build\temp.win-amd64-cpython-39\Release\cpp\src\cpg_module.cp39-win_amd64.exp
Generating code
Finished generating code
CVXPYgen finished compiling python wrapper.

The readme.md file shows the following file tree

cvxpygen_test
    c
      solver_code
        ...
      include
        cpg_workspace.h
        cpg_solve.h
      src
        cpg_workspace.c
        cpg_solve.c
        cpg_example.c
      build
      CMakeLists.txt
    cpp
      include
        cpg_module.hpp
      src
        cpg_module.cpp
    setup.py
    cpg_solver.py
    problem.pickle
    LICENSE
    README.html

A folder named 'C' is created with multiple subfolders and files. Can you please advice what is wrong with the highlighted error messages and warnings. ?

Regards, Karthik

maxschaller commented 2 weeks ago

The file tree looks good. To prevent the CMake error, please follow the installation instructions for Windows.

karthyyy commented 2 weeks ago

Hi @maxschaller ,

Thanks for the prompt reply. I am running the same on offline PC (not connected to internet). I had installed Visual Studio with the same workload using an offline method. The errors may be surfaced due to that. So can I ignore the errors ? How to confirm the files generated are fine ?

Regards Karthik

maxschaller commented 2 weeks ago

An easy way to check if code was generated successfully is to compile and run the example executable.

karthyyy commented 2 weeks ago

Hi @maxschaller ,

Its working. :).

One more query.

I am trying to integrate the cvxpygen generated code to my project for an embedded microcontroller using an Eclipse based IDE. I generated the code for a simple example using "cpg.generate_code(problem, code_dir='cvxpygen_test', solver='OSQP')" command in my python code. The folders (include, solver_code & src) from 'cvxpygen_test/c' are copied to my embedded project directory. I added the cross compilation include paths through the IDE GUI and tried to build the code. The compilation threw following errors and warnings. Also I haven't used the CMakeLists file.

Error Message

Building file: ../src/cvxpygen/solver_code/src/emosqpmodule.c
Invoking: GNU RISC-V Cross C Compiler
riscv64-unknown-elf-gcc -march=rv64gc -mabi=lp64d -mcmodel=medany -msmall-data-limit=8 -mstrict-align -mno-save-restore -O0 -fmessage-length=0 -fsigned-char -ffunction-sections -fdata-sections  -g3 -I"D:\....\src\application" -I"D:\...\src\cvxpygen\include" -I"D:\...\src\cvxpygen\solver_code\include" -I"D:\...\src\platform" -I"D:\....\src\boards\icicle-kit-es" -I"D:\.....\src\platform\platform_config_reference" -std=gnu11 -Wstrict-prototypes -Wbad-function-cast -Wa,-adhlns="src/cvxpygen/solver_code/src/emosqpmodule.o.lst" --specs=nano.specs -MMD -MP -MF"src/cvxpygen/solver_code/src/emosqpmodule.d" -MT"src/cvxpygen/solver_code/src/emosqpmodule.o" -c -o "src/cvxpygen/solver_code/src/emosqpmodule.o" "../src/cvxpygen/solver_code/src/emosqpmodule.c"
../src/cvxpygen/solver_code/src/emosqpmodule.c:4:10: fatal error: Python.h: No such file or directory
 #include "Python.h"                // Python API
          ^~~~~~~~~~
compilation terminated.
make[1]: *** [src/cvxpygen/solver_code/src/subdir.mk:23: src/cvxpygen/solver_code/src/emosqpmodule.o] Error 1
make: *** [makefile:70: all] Error 2
"make all" terminated with exit code 2. Build might be incomplete.

Build Failed. 3 errors, 25 warnings. (took 38s.20ms)

Warnings

......

In file included from ../src/cvxpygen/src/cpg_example.c:9:
D:\.....\src\cvxpygen\include/cpg_solve.h:14:1: warning: function declaration isn't a prototype [-Wstrict-prototypes]
 extern void cpg_canonicalize_A();
 ^~~~~~
D:\....\src\cvxpygen\include/cpg_solve.h:15:1: warning: function declaration isn't a prototype [-Wstrict-prototypes]
 extern void cpg_canonicalize_l();

.......

D:\.....\src\cvxpygen\include/cpg_solve.h:31:1: warning: function declaration isn't a prototype [-Wstrict-prototypes]
 extern void cpg_set_solver_default_settings();
 ^~~~~~
Finished building: ../src/cvxpygen/src/cpg_example.c

Whether warnings can be neglected and how to solve the error messages ? I couldn't find python.h file anywhere.

Regards Karthik

maxschaller commented 2 weeks ago

You should use the CMakelists file. Citing my above response:

The generated CMakeLists.txt exposes cpg_head and cpg_src to the parent scope, such that the generated solver can be included in any C/C++ project as a subdirectory. We expect that hardware-specific customizations are done in the main directory's CMakeLists.txt (written by the user).

karthyyy commented 2 weeks ago

@maxschaller,

Sure. Let me try to do it and get back.

Regards Karthik