views:

275

answers:

3

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

ie, if matlab_func1() gets called from thread1 then matlab_func2() gets called from thread2, matlab_func2() must wait for matlab_func1() to finish executing.

Is there a way to call different matlab functions simultaneously? 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);
  }
+3  A: 

I would suggest that when you call your functions, MATLAB (or MATLAB's real time workshop that gets embedded in to a DLL) is spawned and the function is run. The MATLAB interface between C# and C likely has a few global external variables, and as a result MATLAB probably can't be spawned twice within the same process. Also, MATLAB itself is in many ways single threaded. They are working on moving towards more use of multi-core.

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

What I might suggest as a test is to put "DoKalmanFilter" and "DoImageProcessing" in to seperate assemblies or applications, and then make an application that spawns your two new seperate applications. This way you get two processes and MATLAB will run once inside each process giving you the ability to run your functions simultaneously.

If that works, you can then start thinking of ways around this. You dont want to have to be spawning applications and closing them down all the time. You might need to use some inter-process communication between your processes to pass data around, it might get ugly :-)

Fuzz
A: 

Cloud computing all the way bro. You just point that sucker into the cloud and let some provider like amazon deal with it.

Sure there's overhead on the marshaling of data to and from the cloud but that will never compete with the time the actual work in matlab takes for each transaction.

This is a really low cost solution that scales economically better (and I say this assuming you're not parked on a world-class server farm) than you can do in-house.

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

The cloud is the truth.

GabeH
I downvoted you, because, while arguably useful, your answer is not appropriate here.
Noon Silk
+1  A: 

According to the Mathworks documentation,

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 the least a second process, rather than a second thread. However, you can leave the code in one assembly - calling the same assembly from a second process gives it its own resources.

An alternative would be to use MATLAB's Parallel Processing Toolbox to split each function call over several cores, making each run faster, and calling them sequentially. However, that depends on whether your MATLAB functions lend themselves to parallelization.

mtrw