Skip to content

Commit

Permalink
Merge pull request #50 from osqp/matlab-interface
Browse files Browse the repository at this point in the history
  • Loading branch information
imciner2 authored Nov 17, 2023
2 parents edbafcb + 3896fa0 commit fd75800
Show file tree
Hide file tree
Showing 16 changed files with 1,108 additions and 1,493 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# -------------------------------------------------------------------
# Out folder
out/
c_sources/build/

# Prerequisites
*.d
Expand Down
2 changes: 1 addition & 1 deletion .gitmodules
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
68 changes: 68 additions & 0 deletions c_sources/CMakeLists.txt
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 )
25 changes: 25 additions & 0 deletions c_sources/interrupt_matlab.c
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();
}
22 changes: 22 additions & 0 deletions c_sources/memory_matlab.h
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
Loading

0 comments on commit fd75800

Please sign in to comment.