Call two Matlab functions at the same time from .net

I am writing a C # application and I would like to make calls for different matlab functions at the same time (from different threads). Each Matlab function is located in its own compiled .net library. It seems that I can only name one Matlab function at a time.

those. if matlab_func1 () is called from thread1, then matlab_func2 () is called from thread2, matlab_func2 () must wait for matlab_func1 () to complete the execution.

Is there a way to call different Matlab functions at the same time? Thanks.

MWArray[] DoKalmanFilter(double vel_x, double vel_y, double vel_z, double cal_x, double cal_y, double cal_z, bool doCal) { ...set up parameters ret = KalmanFilter.kalman_actual(6, velx, vely, velz, cal_x, cal_y, cal_z, return ret; } private void DoImageProcessing() { ..set up parameters MWArray[] ret = _imgProcessor.DoImageProcessing(2, rgbMarkerColor, hsvThreshold, angleDiffThreshold); } 
+4
source share
3 answers

I would suggest that when you call your functions, MATLAB (or the MATLAB real-time seminar that is embedded in the DLL) is generated and the function starts. The MATLAB interface between C # and C probably has several global external variables, and as a result, MATLAB probably cannot be spawned twice within the same process. In addition, MATLAB itself is a single thread. They are working to improve the use of multi-core processors.

I already used the Java / Matlab interface and read the following on this site : Matlab is single-threaded. This means that if you try eval or feval from a Java function that was called directly from the Matlab function, it will wait for the first matlab function to complete.

What I can offer as a test is to put "DoKalmanFilter" and "DoImageProcessing" to separate assemblies or applications, and then create an application that spawns two new separate applications. Thus, you will get two processes, and MATLAB will be launched once inside each process, which will give you the opportunity to simultaneously execute your functions.

If that works, you can start thinking about it. You do not want applications to spawn and close them all the time. You may need to use some interprocess communication between your processes to transfer data, this can become ugly :-)

+7
source

In accordance with the Documentation The Mathworks ,

 A single application only has access to a single MCR computational engine. 

Therefore, as Fuzz said, you need to create a second application, or at least a second process, not a second thread. However, you can leave the code in one assembly - calling the same assembly from the second process gives it its own resources.

An alternative would be to use the MATLAB Parallel Processing Toolbox to split each function call into several cores, which makes each run faster and calls them sequentially. However, it depends on whether your MATLAB functions can be parallelized.

+2
source

Cloud computing all the way. You simply indicate that the suckers are in the cloud and let some provider, like amazon, handle it.

Sure there is the overhead of marshaling data to and from the cloud, but it will never compete with the actual time in Matlab for each transaction.

This is an inexpensive solution that scales economically better (and I say that I assume that you are not parked on a world-class server farm) than you can do in your home.

Here's something that turns me on: http://www.mathworks.com/programs/techkits/ec2_paper.html

The cloud is true.

-2
source

Source: https://habr.com/ru/post/1306632/


All Articles