Multithreading with mex functions
조회 수: 7 (최근 30일)
이전 댓글 표시
The documentation of MATLAB suggests that it is OK to multithread with mex functions as long as as the mex API is not used. Multithreading and mex
The link above shows an example of a simple counter. We deploy a software tool developed in MATLAB. Much of the computationally intensive code is written in separately compiled FORTRAN or C modules. The MATLAB GUI just kicks off the executable and returns control to the user. When the executable is finished it writes the output to an ASCII text file. Is it possible to write a mex file that is kicked off when the executable is kicked off? That mex file would just be a while loop checking for the existence of the output file. Once the output file exists, the mex file would interrupt or notify that the results are ready. This would be better then our current implementation where the user clicks to load the results; if the data is not ready it just responds with an error saying to wait a little longer. Does anybody know if this is possible? The link aboves says that the mex API may not be used which includes simple things like mexPrintf. That makes me think that this may not be possible.
Any suggestions?
댓글 수: 0
채택된 답변
Geoff
2012년 3월 15일
I'm not sure if I've interpreted your question correctly, but I think your main problem is working out how to get data back to MatLab asynchronously.
If that is correct, you can start a MatLab timer before firing off your processing app. The timer can poll once per second to check for the existence of the output, then kill itself and fire off some MatLab callback function once the file shows up... You can also do timeouts etc.
-g-
추가 답변 (3개)
James Tursa
2012년 3월 15일
As I understand it, the executable you plan on running is a completely separate process (is this true?), and as such there are no restrictions on running that separate process while the mex routine is running. In fact, it could be the mex routine itself that kicks off this other executable. The restriction is that within the mex routine itself you cannot call certain API functions (specifically the ones that change memory) within a multi-threaded section of code. You are not doing that.
One piece of advice ... I would have your executable code create two files ... on that is the output data file itself, and another that is a "done" file. Have your executable finish writing to and close the data file, and then have it create the "done" file. Your mex routine then checks for the existence of the "done" file ... at that point you know that the executable has closed the data file and it is avaialable for opening and reading on the MATLAB side.
BUT ... having said all this I have to ask why you need a mex routine for this at all? Why can't you have m-code checking for the existence of this output file?
Geoff
2012년 3월 19일
Hey Paul, glad to know that solved your problem. You're still doing it the hard way though! =)
Don't search through a directory listing every poll if you already know the name of the file:
if exist('done.txt', 'file')
display('Output is ready');
delete(handles.t);
end
Also, why not just look for the output file instead of a separate 'done' file. Normally, your processing app would write to (for example) 'output.csv.tmp' and then rename to 'output.csv' when complete. That way you picking up partial results.
-g-
참고 항목
카테고리
Help Center 및 File Exchange에서 MATLAB Compiler에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!