Many questions about using a compiled Matlab Assembly from within a .NET application

조회 수: 1 (최근 30일)
Hi All - I have a bunch of questions about using a compiled MatLab assembly inside of my .NET WinForms application. Sorryit is a little long, , but I wanted a comprehensive summary I could share with people here or on other sites who might have some expertise to lend.
It's a little complicated, but here is the situation. I have a single MATLAB assembly (MatlabSoftware) I get from Jon (another developer in out group who owns the MatLAb code). It contains 2 classes (ApexSearch & TreatmentPlan). Each class contains several methods
  • Both contain an InitializeRuntime() method - A trivial method that is intened to be called during my application startup to initialize the MATLAB runtime, such that subsequent calls to other methods don't take so long.
  • Both contain a method for retrieving the version numbers of the associated files for the class as a JSON string ( ApexSearch_Version(), TreatmentPlanVersion())
  • The ApexSearch class contains a method for performing apex finding given the set of images collected by the surface search application (ApexFinding())]
  • The TreatmentPlan Class contains methods for calculating the treatment plan given the prescription, the calibration data and the writing position (FOCLTreatmentPlan()
The MATLAB compiler builds the DLL in such a way that Jon delivers a DLL file (relatively small) that contains the actual methods (MatLabSoftare.DLL) and a much larger (220 Mb+) file that contains all of the associated common functionality (Matlabsoftware.ctf)
We have experimented around with various different ways of packaging this stuff (different DLL for each class, with/without the associated .ctf file, etc) Not sure we really understand what the consequences of any particular deployment choice is - but this is what we have settled upon.
My application essentially performs as follows
  • During startup, it calls the InitializeRuntime method of both classes
  • If the user requests (via the 'About' menu item), an information form is displayed which include the revision information of the referenced MatLab assemblies. This form calls both classes Revision methods during its startup. I have experimented with having these forms make the calls in a separate thread to prevent UI lockup while waiting for the methods to complete (they take a long time if run time not already initialized).
  • At the end of the surface search procedure, the ApexFinding method is called. This is being performed on a separate thread becuse the search take a long time and the UI must be available for other stuff. I have experimented with marshaling this call back onto the main UI thread.
  • When requested by the user, the FOCLTreatmentPlan() method is called. There are certain (not impossible) circumstances in which the data passed to this method will result in it never converging upon a solution - so that it will essentially run forever. My application needs to detect this and take an appropriate action. So the method has several threads associated with it. But it is running in a separate thread that my app can monitor to determine if it is taking too long to complete. (More on this later)
Here is some of the odd behavior I have been seeing.
  • If I make no other calls first, the ApexFind and the TreatmentPlan methods both seem to function as expected, although they are very slow (2+ minutes) the first time they are called.
  • If I call either the InitRuntime or the revision Methods first, then the Apex finding methods fails in one of a variety of ways
  • With an error that reads MWMCR::EvaluateFunction error - ApexCOM_COMPLETE_CTF is not a reference to an absolute path (that's not the exact wording but something very close to that).
  • This is particularity odd because the reference to ApexCOM_Complete is a part of a previous generation of the DLL and there is no such name involved in the current code. More on this later.
  • This error is reported immediately when attempting make the call - the called method never actually gets executed - its just a return from whatever mechanism is used bu .NET and MATLAB to get the method loaded and started.
  • Alternativly, the system appears to begin executing the method but then the entire application just crashes and shuts down (without throwing any excption) .
More on the MWMCR::EvaluateFunction Error
  • I've noticed that after I make a call into the MatlabSoftwareDLL, I get a large set of directories created in my application's binary directory (MatLabSoftware_mcr). This directory tree contains approx 380Mg various files (mostly binary files with a .m extension that I can't read).
  • I found that if I deleted this directory, and then rebuilt my application, that the EvaluateFunction error appears to have disappeared. Now all I get is the mysterious crash and burn error.
More on the infinite surface search
  • It is completely possible that the surface search will result in a data set that the apexFinding method cannot resolve. Jon tells me that MATLAB does not contain a built-in mechanism for detecting or preventing this.
  • I can currently run the method on a thread, and I can detect if that thread does not complete in a reasonable time.
  • But then what do I do?
  • According to Jon, MATLAB does not contain a mechanism for interrupting and canceling an on-going method.
  • I can just kill the thread, but that is a very bad practice, as I have no idea what resources MATLAB is using and what state they are left in. System would be likely to become unstable and start doing weird stuff relatively quickly.
  • Currently, I'm just leaving the thread running, and informing the user about the issue and telling them they need to reboot when they can. Depending on what they choose to do before rebooting, this might also cause some pretty weird behavior, but I judged it to be safer than attempting to kill the thread.
All of which leads me to a list of questions that I think we need answers to before we marry ourselves to interfacing MATLAB to our other applications.
  • What is the best way for us to package the matlab methods we want to integrate? ctf/no ctf, different classes, etc?
  • Is it possible to reduce the start up time?
  • Is it possible to separate the startup time from the method execution time so that the startup can be performed at a time when user might not notice or care?
  • Are the MATLAb methods thread-safe? What are the limits/restrictions of calling MATLAB from different threads at different times?
  • Is there a safe way to kill a long-running MATLAB method?
  • What is going on with the *_mcr directories being built in the application binary folder? When/why should they be cleaned up? Is there a way to put them someplace else? Where is the right place to keep them?
Sorry this is so long. Any answers you can provide would be greatly appreciated

답변 (0개)

카테고리

Help CenterFile Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by