Skip to content

Commit

Permalink
Remove extraneous static string on mex call to static methods
Browse files Browse the repository at this point in the history
  • Loading branch information
imciner2 committed Nov 23, 2023
1 parent 424bccd commit 5cf4a61
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 95 deletions.
6 changes: 3 additions & 3 deletions @osqp/osqp.m
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
%%
function out = default_settings()
% DEFAULT_SETTINGS get the default solver settings structure
out = osqp_mex('default_settings', 'static');
out = osqp_mex('default_settings');

% Convert linsys solver to string
out.linsys_solver = linsys_solver_to_string(out.linsys_solver);
Expand All @@ -44,13 +44,13 @@
function out = constant(constant_name)
% CONSTANT Return solver constant
% C = CONSTANT(CONSTANT_NAME) return constant called CONSTANT_NAME
out = osqp_mex('constant', 'static', constant_name);
out = osqp_mex('constant', constant_name);
end

%%
function out = version()
% Return OSQP version
out = osqp_mex('version', 'static');
out = osqp_mex('version');
end
end

Expand Down
189 changes: 97 additions & 92 deletions c_sources/osqp_mex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,21 @@ static void setToNaN(double* arr_out, OSQPInt len){
// Main mex function
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
// OSQP solver wrapper
OsqpData* osqpData;

// Exitflag
OSQPInt exitflag = 0;
// Static string for static methods
char stat_string[64];

// Get the command string
char cmd[64];
if (nrhs < 1 || mxGetString(prhs[0], cmd, sizeof(cmd)))

if (nrhs < 1 || mxGetString(prhs[0], cmd, sizeof(cmd)))
mexErrMsgTxt("First input should be a command string less than 64 characters long.");
// new object

/*
* First check to see if a new object was requested
*/
if (!strcmp("new", cmd)) {
// Check parameters
if (nlhs != 1){
Expand All @@ -59,18 +63,97 @@ void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
return;
}

// Check for a second input, which should be the class instance handle or string 'static'
if (nrhs < 2)
mexErrMsgTxt("Second input should be a class instance handle or the string 'static'.");

if(mxGetString(prhs[1], stat_string, sizeof(stat_string))){
// If we are dealing with non-static methods, get the class instance pointer from the second input
osqpData = convertMat2Ptr<OsqpData>(prhs[1]);
} else {
if (strcmp("static", stat_string)){
mexErrMsgTxt("Second argument for static functions is string 'static'");
/*
* Next check to see if any of the static methods were called
*/
// Report the version
if (!strcmp("version", cmd)) {
plhs[0] = mxCreateString(osqp_version());
return;
}

// Report the default settings
if (!strcmp("default_settings", cmd)) {
// Warn if other commands were ignored
if (nrhs > 2)
mexWarnMsgTxt("Default settings: unexpected number of arguments.");

// Create a Settings structure in default form and report the results
// Useful for external solver packages (e.g. Yalmip) that want to
// know which solver settings are supported
OSQPSettingsWrapper settings;
plhs[0] = settings.GetMxStruct();
return;
}

// Return solver constants
if (!strcmp("constant", cmd)) {
static std::map<std::string, OSQPFloat> floatConstants{
// Numerical constants
{"OSQP_INFTY", OSQP_INFTY}
};

static std::map<std::string, OSQPInt> intConstants{
// Return codes
{"OSQP_SOLVED", OSQP_SOLVED},
{"OSQP_SOLVED_INACCURATE", OSQP_SOLVED_INACCURATE},
{"OSQP_UNSOLVED", OSQP_UNSOLVED},
{"OSQP_PRIMAL_INFEASIBLE", OSQP_PRIMAL_INFEASIBLE},
{"OSQP_PRIMAL_INFEASIBLE_INACCURATE", OSQP_PRIMAL_INFEASIBLE_INACCURATE},
{"OSQP_DUAL_INFEASIBLE", OSQP_DUAL_INFEASIBLE},
{"OSQP_DUAL_INFEASIBLE_INACCURATE", OSQP_DUAL_INFEASIBLE_INACCURATE},
{"OSQP_MAX_ITER_REACHED", OSQP_MAX_ITER_REACHED},
{"OSQP_NON_CVX", OSQP_NON_CVX},
{"OSQP_TIME_LIMIT_REACHED", OSQP_TIME_LIMIT_REACHED},

// Linear system solvers
{"QDLDL_SOLVER", QDLDL_SOLVER},
{"OSQP_UNKNOWN_SOLVER", OSQP_UNKNOWN_SOLVER},
{"OSQP_DIRECT_SOLVER", OSQP_DIRECT_SOLVER},
{"OSQP_INDIRECT_SOLVER", OSQP_INDIRECT_SOLVER}
};

char constant[64];
int constantLength = mxGetN(prhs[1]) + 1;
mxGetString(prhs[1], constant, constantLength);

auto ci = intConstants.find(constant);

if(ci != intConstants.end()) {
plhs[0] = mxCreateDoubleScalar(ci->second);
return;
}

auto cf = floatConstants.find(constant);

if(cf != floatConstants.end()) {
plhs[0] = mxCreateDoubleScalar(cf->second);
return;
}

// NaN is special because we need the Matlab version
if (!strcmp("OSQP_NAN", constant)){
plhs[0] = mxCreateDoubleScalar(mxGetNaN());
return;
}

mexErrMsgTxt("Constant not recognized.");

return;
}

/*
* Finally, check to see if this is a function operating on a solver instance
*/

// Check for a second input, which should be the class instance handle
if (nrhs < 2)
mexErrMsgTxt("Second input should be a class instance handle.");


// Get the class instance pointer from the second input
osqpData = convertMat2Ptr<OsqpData>(prhs[1]);

// delete the object and its data
if (!strcmp("delete", cmd)) {

Expand Down Expand Up @@ -126,21 +209,6 @@ void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
return;
}

// report the default settings
if (!strcmp("default_settings", cmd)) {
// Warn if other commands were ignored
if (nrhs > 2)
mexWarnMsgTxt("Default settings: unexpected number of arguments.");


// Create a Settings structure in default form and report the results
// Useful for external solver packages (e.g. Yalmip) that want to
// know which solver settings are supported
OSQPSettingsWrapper settings;
plhs[0] = settings.GetMxStruct();
return;
}

// setup
if (!strcmp("setup", cmd)) {
//throw an error if this is called more than once
Expand Down Expand Up @@ -231,14 +299,6 @@ void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
return;
}

// report the version
if (!strcmp("version", cmd)) {

plhs[0] = mxCreateString(osqp_version());

return;
}

// update linear cost and bounds
if (!strcmp("update", cmd)) {

Expand Down Expand Up @@ -451,61 +511,6 @@ void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
return;
}

if (!strcmp("constant", cmd)) { // Return solver constants
static std::map<std::string, OSQPFloat> floatConstants{
// Numerical constants
{"OSQP_INFTY", OSQP_INFTY}
};

static std::map<std::string, OSQPInt> intConstants{
// Return codes
{"OSQP_SOLVED", OSQP_SOLVED},
{"OSQP_SOLVED_INACCURATE", OSQP_SOLVED_INACCURATE},
{"OSQP_UNSOLVED", OSQP_UNSOLVED},
{"OSQP_PRIMAL_INFEASIBLE", OSQP_PRIMAL_INFEASIBLE},
{"OSQP_PRIMAL_INFEASIBLE_INACCURATE", OSQP_PRIMAL_INFEASIBLE_INACCURATE},
{"OSQP_DUAL_INFEASIBLE", OSQP_DUAL_INFEASIBLE},
{"OSQP_DUAL_INFEASIBLE_INACCURATE", OSQP_DUAL_INFEASIBLE_INACCURATE},
{"OSQP_MAX_ITER_REACHED", OSQP_MAX_ITER_REACHED},
{"OSQP_NON_CVX", OSQP_NON_CVX},
{"OSQP_TIME_LIMIT_REACHED", OSQP_TIME_LIMIT_REACHED},

// Linear system solvers
{"QDLDL_SOLVER", QDLDL_SOLVER},
{"OSQP_UNKNOWN_SOLVER", OSQP_UNKNOWN_SOLVER},
{"OSQP_DIRECT_SOLVER", OSQP_DIRECT_SOLVER},
{"OSQP_INDIRECT_SOLVER", OSQP_INDIRECT_SOLVER}
};

char constant[64];
int constantLength = mxGetN(prhs[2]) + 1;
mxGetString(prhs[2], constant, constantLength);

auto ci = intConstants.find(constant);

if(ci != intConstants.end()) {
plhs[0] = mxCreateDoubleScalar(ci->second);
return;
}

auto cf = floatConstants.find(constant);

if(cf != floatConstants.end()) {
plhs[0] = mxCreateDoubleScalar(cf->second);
return;
}

// NaN is special because we need the Matlab version
if (!strcmp("OSQP_NAN", constant)){
plhs[0] = mxCreateDoubleScalar(mxGetNaN());
return;
}

mexErrMsgTxt("Constant not recognized.");

return;
}

// Got here, so command not recognized
mexErrMsgTxt("Command not recognized.");
}

0 comments on commit 5cf4a61

Please sign in to comment.