From 5cf4a612a5b8a681c88fd25048003dae3d312f29 Mon Sep 17 00:00:00 2001 From: Ian McInerney Date: Thu, 23 Nov 2023 17:29:34 +0000 Subject: [PATCH] Remove extraneous static string on mex call to static methods --- @osqp/osqp.m | 6 +- c_sources/osqp_mex.cpp | 189 +++++++++++++++++++++-------------------- 2 files changed, 100 insertions(+), 95 deletions(-) diff --git a/@osqp/osqp.m b/@osqp/osqp.m index d26fffc..cc58bcd 100644 --- a/@osqp/osqp.m +++ b/@osqp/osqp.m @@ -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); @@ -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 diff --git a/c_sources/osqp_mex.cpp b/c_sources/osqp_mex.cpp index 12b0e9e..5fc90d3 100755 --- a/c_sources/osqp_mex.cpp +++ b/c_sources/osqp_mex.cpp @@ -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){ @@ -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(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 floatConstants{ + // Numerical constants + {"OSQP_INFTY", OSQP_INFTY} + }; + + static std::map 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(prhs[1]); + // delete the object and its data if (!strcmp("delete", cmd)) { @@ -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 @@ -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)) { @@ -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 floatConstants{ - // Numerical constants - {"OSQP_INFTY", OSQP_INFTY} - }; - - static std::map 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."); }