Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move Matlab-specific interface code from main library to Matlab interface repo #47

Open
imciner2 opened this issue Apr 7, 2022 · 3 comments

Comments

@imciner2
Copy link
Member

imciner2 commented Apr 7, 2022

The merging of osqp/osqp#396 in the main OSQP library repo will allow us to move the Matlab-specific printing functions to the Matlab repo, removing the need to include the Matlab headers and code in the main library.

This is also possible to do with the memory allocation functions currently, and hopefully the timing/interrupt functions will move to this as well, so we can then move all Matlab-interface specific code to the interface and remove it from the main library repo - allowing us to then remove the Matlab CMake parts from the main library and only have it here in the interface.

@imciner2
Copy link
Member Author

imciner2 commented Aug 4, 2022

The MATLAB-specific code for the printing functions is

/* informational print function */
# define c_print mexPrintf

This should be placed into a header file, and then the OSQP_CUSTOM_PRINTING CMake option should be set to the header file containing that code.

@imciner2
Copy link
Member Author

imciner2 commented Aug 4, 2022

To split out the interrupt routine for MATLAB from the main code, using a WIP branch, the interface will need to contain a C file with

/*
 * Implements interrupt handling using ctrl-c for MATLAB mex files.
 */

#include "interrupt.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();
}

Then set OSQP_CUSTOM_INTERRUPT=ON and link the OSQP static library against that file when building the mex file.

@imciner2
Copy link
Member Author

imciner2 commented Aug 4, 2022

The MATLAB-specific memory management code is:

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

Which should be included into a header file and then the OSQP_CUSTOM_MEMORY CMake variable should be set with the name of that header file.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants