Once again I would like to welcome guest blogger Pavel Holoborodko, the developer of the Advanpix Multiprecision Computing Toolbox. Pavel has already posted here as a guest blogger about undocumented Matlab MEX functions. Today he will discuss another little-known aspect of advanced MEX programming with Matlab, a repost of an article that was originally posted on his own blog. Happy holidays everybody!
Matlab allows flexible adjustment of visibility of warning messages. Some, or even all, messages can be disabled from showing on the screen by warning command.
The little known fact is that status of some warnings may be used to change the execution path in algorithms. For example, if warning 'Matlab:nearlySingularMatrix'
is disabled, then the linear system solver (mldivide operator) might skip estimation of reciprocal condition number which is used exactly for the purpose of detection of nearly singular matrices. If the trick is used, it allows 20%-50% boost in solver performance, since rcond
estimation is a time consuming process.
Therefore it is important to be able to retrieve status of warnings in Matlab. Especially in MEX libraries targeted for improved performance. Unfortunately Matlab provides no simple way to check status of warning message from MEX module.
Today’s article outlines two workarounds for the issue:
- Using
mexCallMATLABWithTrap
(documented) - Using
utGetWarningStatus
(undocumented)
Using mexCallMATLABWithTrap (documented)
The first idea is to use documented mexCallMATLABWithTrap
function to execute warning(‘query’,…) command using Matlab’s interpreter and then parse the returned result:
bool mxIsWarningEnabled(const char* warningId) { bool enabled = true; if (NULL != warningId) { mxArray *mxCommandResponse = NULL, *mxException = NULL; mxArray *args[2]; /* warning('query', warningId); */ args[0] = mxCreateString("query"); args[1] = mxCreateString(warningId); mxException = mexCallMATLABWithTrap(1,&mxCommandResponse,2,args,"warning"); if (NULL == mxException && NULL != mxCommandResponse) { if (mxIsStruct(mxCommandResponse)) { const mxArray* state_field = mxGetField(mxCommandResponse, 0, "state"); if (mxIsChar(state_field)) { char state_value[8] = {0}; enabled = (0 == mxGetString(state_field, state_value, 8)) && (0 == strcmp(state_value,"on")); } } mxDestroyArray(mxCommandResponse); } else { /* 'warning' returned with error */ mxDestroyArray(mxException); } mxDestroyArray(args[0]); mxDestroyArray(args[1]); } return enabled; } |
This approach is slow, but works fine in most standard situations. See the bottom of this post for a usage example.
However, this approach has an important drawback – we should be careful with recursive calls to the Matlab interpreter (Matlab -> MEX -> Matlab
) and with handling Matlab errors in MEX. It is safe only if we use identical standard libraries and compiler to build both MEX and Matlab.
In other cases, for example when MEX is targeted to work with different versions of Matlab, or was built with a different standard library and compiler, etc. – cross boundary handling of errors (which are just C++ exceptions) might lead to unpredictable results, most likely segfaults.
Using utGetWarningStatus (undocumented)
To avoid all the overhead of calling Matlab interpreter and unsafe error handling, we can use some undocumented internal Matlab functions:
/* Link with libut library to pick-up undocumented functions: */ extern "C" void* utGetWarningManagerContext(void); extern "C" bool utIsValidMessageIdentifier(const char *warningId); extern "C" bool utGetWarningStatus(void* context, const char *warningId); /* Returns true if warning with warningId enabled Matlab versions supported/tested: R2008b - R2016b */ bool mxIsWarningEnabled(const char *warningId) { bool enabled = true; if (NULL != warningId && utIsValidMessageIdentifier(warningId)) { void* context = utGetWarningManagerContext(); enabled = (NULL != context) && utGetWarningStatus(context, warningId); } return enabled; } |
Now the code is clean, fast and safe – we bypass the interpreter and work directly with Matlab kernel. All the undocumented functions involved are present in Matlab for at least 10 years and do simple logical checks under the hood.
The standard function mexWarnMsgIdAndTxt
uses similar code to check if it should display the warning or just suppress it, and that code remains unchanged since R2008b. This is a good indication of code stability and makes us believe that it will not be changed in future versions of Matlab.
For both workarounds, usage is simple:
if (mxIsWarningEnabled("Matlab:nearlySingularMatrix")) { /* compute rcond */ } else { /* do something else */ } |