How to do wait calllib to give answer or catch exception?

Good day,
I want to fit some parameters (around 50) for a model implemented in a DLL and am using lsqcurvefit. Unfortunately I have noticed Matlab halts sometimes within that optimisation and the only way to resume it is killing Matlab and restart Matlab. Matlab tells me then to have encountered an internal problem. Sometimes it happens on first parameter set, sometimes on 40th. The author of the DLL told me, the model is not very stable and it can happen, that the answer is not an array of double but an array which contains INF entries. I do not know, but I guess this makes Matlab hold.
First idea was to write down every parameter set and get to know the bounds for parameters. But this is not good approach because combination of some parameters may cause this behaviour. Excluding both parameters from parameter space would exclude some parameter combinations which can actually work. Creating such rules of possible combinations would need way too much effort.
Now I am searching, how to wrap a time condition around the calllib command. If time is over and there is no understandable result, the result could be set to zero or excluded. I tried to make a function_handle for that calllib command and use it with wait(fun,'finished',1). But "wait" does not work with function_handles. How can I put the command into a job to use it with wait? Or is there a way to remove the DLL job if failed and prevent Matlab from halting? I do not know, whether such approach could work with a DLL but basically I am thinking of something simple like:
try('time',1)
[answer1, answer2 , answer3] = calllib(DLL,'function',length,array1, array2 ,array3);
catch
[answer1, answer2 , answer3] = [zeros(1,length), zeros(1,length) , zeros(1,length)];
end
Thanks for you help!
Tom

 Accepted Answer

No, you can't do something like that in matlab and even in languages were you have access to the OS API it wouldn't be trivial to code without risking to crash the application.
Dlls are highly privileged, when you load a dll (with loadlibrary in matlab) they become an integral part of your application (so a part of matlab executable in your case). That makes them very useful but also means that if they go wrong they take the application down with them.
Ultimately the problem is with your dll. I'm afraid you'll have to go back to the author and ask them to improve their code. A dll that is not stable will crash whichever program is calling it. The author of that dll could implement the timer you suggest in the dll to stop the processing. Depending on the language they've used to write the dll, they will have to ensure that when they interrupt the processing, the dll is in a stable state. For some languages, this would be taken care of for them.

2 Comments

Thank for your answer. I understand now a little bit better how Matlab and DLL are connected. But weird is, that the author of the DLL is testing it with Python and there he has never experienced a crash. Only on bad input parameter he gets unrealistic numbers on answer array.
So there must anyway be a difference how Matlab and Python are handling a DLL.
While troubleshooting a similar issue, an option we did not have to pursue (solved the issue by using 2019a instead of MATLAB 2024a) was using a parallel process to run the DLL. If the DLL took a certain amount of time (in our case, 1-2 seconds was enough to know there was a problem), then the parallel process could be killed and move on. At least that was the principle. Maybe someone out there could make that a reality for their issue/workaround.
Another part of the problem was that initially my inputs exceded what the DLL could handle. So the DLL could perhaps have been coded better, but that part at least was my error.
See also

Sign in to comment.

More Answers (2)

I got following answer from Mathworks support which seems useful but a lot of effort to me in second case:
There is no concept which would allow you to abort/stop a function call when it takes too long/hangs.
For a job you would need the Parallel Computing Toolbox which you do not seem to have available. But with Parallel Computing Toolbox you could start a parallel pool with one worker, load the DLL on that worker and then call calllib on the worker. If the worker takes too long to finish you can kill it and start again. I would suggest using parfeval
and fetchNext with the timout set
For the toolboxes you have installed, the only other way is not to use loadlibrary and callllib. Instead write a MEX file
to call your DLL from C/C++ directly and in there make use of threads. If the threads takes too long to finish kill it.
I have received a version of that DLL which is giving back arrays which do not contain any INF entries but zeros instead and an integer value indicating an error in case.
But this has not helped me. As I experience it, it is a problem about how Matlab is handling the DLL. I have put the DLL calling into a loop and call it every time with same routine and same parameters (!) and mostly the routine is beeing performed. But sometimes Matlab halts and crashes. This does not happen under python.
Maybe there is some difference how Matlab is implementing the DLL into the environment, which is weak to some memory errors?

Categories

Asked:

on 3 Apr 2019

Edited:

on 27 Nov 2024

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!