-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #50 from osqp/matlab-interface
- Loading branch information
Showing
16 changed files
with
1,108 additions
and
1,493 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
[submodule "osqp_sources"] | ||
path = osqp_sources | ||
path = c_sources/osqp_sources | ||
url = https://github.com/osqp/osqp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
cmake_minimum_required( VERSION 3.18 ) | ||
|
||
project( osqp-matlab ) | ||
|
||
|
||
# Find the MATLAB installation directory and various settings for it | ||
find_package( Matlab REQUIRED COMPONENTS MEX_COMPILER) | ||
|
||
message( STATUS "Matlab root is " ${Matlab_ROOT_DIR} ) | ||
|
||
include_directories( ${Matlab_INCLUDE_DIRS} ) | ||
|
||
if( CMAKE_COMPILER_IS_GNUCXX ) | ||
# Add debug symbols and optimizations to the build | ||
set( CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -g -O2" ) | ||
set( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -O2" ) | ||
endif() | ||
|
||
# Insist on the pre-2018 complex data API so that mxGetPr will work correctly | ||
add_compile_definitions( MATLAB_MEXSRC_RELEASE=R2017b ) | ||
message( STATUS "Using Matlab pre-2018a API for mxGetPr compatibility" ) | ||
|
||
# Some parts of the main libraries need to know we are building for MATLAB | ||
# So pass a definition to let them know | ||
add_compile_definitions( MATLAB ) | ||
|
||
# Configure the settings of the OSQP library | ||
set( OSQP_ALGEBRA_BACKEND "builtin" ) | ||
set( OSQP_USE_FLOAT OFF ) | ||
set( OSQP_CODEGEN ON ) | ||
set( OSQP_ENABLE_DERIVATIVES ON ) | ||
set( OSQP_ENABLE_PRINTING ON ) | ||
set( OSQP_ENABLE_PROFILING ON ) | ||
set( OSQP_ENABLE_INTERRUPT ON ) | ||
|
||
# Configure the build outputs for the OSQP library | ||
set( OSQP_BUILD_STATIC_LIB ON ) | ||
set( OSQP_BUILD_SHARED_LIB OFF ) | ||
set( OSQP_BUILD_UNITTESTS OFF ) | ||
set( OSQP_BUILD_DEMO_EXE OFF ) | ||
|
||
# Add the custom MATLAB memory handling | ||
set( OSQP_CUSTOM_MEMORY "memory_matlab.h" ) | ||
include_directories( ${CMAKE_CURRENT_SOURCE_DIR} ) | ||
|
||
# Actually pull in the OSQP targets | ||
add_subdirectory( osqp_sources ) | ||
|
||
# Add OSQP include directories that are needed | ||
include_directories( ${CMAKE_CURRENT_SOURCE_DIR}/osqp_sources/include/public | ||
${CMAKE_CURRENT_SOURCE_DIR}/osqp_sources/include/private ) | ||
|
||
# Interrupt support requires the libut library | ||
get_filename_component( Matlab_LIB_DIR ${Matlab_MEX_LIBRARY} DIRECTORY ) | ||
|
||
if( WIN32 ) | ||
set( UT_LIBRARY "${Matlab_LIB_DIR}/libut.lib" ) | ||
else() | ||
set( UT_LIBRARY "-L${Matlab_LIB_DIR} -lut" ) | ||
endif() | ||
|
||
matlab_add_mex( NAME osqp_mex | ||
SRC ${CMAKE_CURRENT_SOURCE_DIR}/osqp_mex.cpp | ||
${CMAKE_CURRENT_SOURCE_DIR}/interrupt_matlab.c | ||
LINK_TO osqpstatic | ||
${UT_LIBRARY} | ||
# Force compilation in the traditional C API (equivalent to the -R2017b flag) | ||
R2017b ) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/* | ||
* Implements interrupt handling using ctrl-c for MATLAB mex files. | ||
*/ | ||
|
||
#include "interrupt.h" | ||
|
||
#include <stdbool.h> | ||
|
||
/* No header file available here; define the prototypes ourselves */ | ||
bool utIsInterruptPending(void); | ||
bool utSetInterruptEnabled(bool); | ||
|
||
static int istate; | ||
|
||
void osqp_start_interrupt_listener(void) { | ||
istate = utSetInterruptEnabled(1); | ||
} | ||
|
||
void osqp_end_interrupt_listener(void) { | ||
utSetInterruptEnabled(istate); | ||
} | ||
|
||
int osqp_is_interrupted(void) { | ||
return utIsInterruptPending(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/* Memory managment for MATLAB */ | ||
#include "mex.h" | ||
|
||
static void* c_calloc(size_t num, size_t size) { | ||
void *m = mxCalloc(num, size); | ||
mexMakeMemoryPersistent(m); | ||
return m; | ||
} | ||
|
||
static void* c_malloc(size_t size) { | ||
void *m = mxMalloc(size); | ||
mexMakeMemoryPersistent(m); | ||
return m; | ||
} | ||
|
||
static void* c_realloc(void *ptr, size_t size) { | ||
void *m = mxRealloc(ptr, size); | ||
mexMakeMemoryPersistent(m); | ||
return m; | ||
} | ||
|
||
#define c_free mxFree |
Oops, something went wrong.