Question

I am facing a huge problem. I built a complex C application with embedded Matlab functions that I call using the Matlab engine (engOpen() and such ...). The following happens:

  • I spawn multiple instances of this application on a machine, one for each core
  • However! ... The application then slows down to a halt. In fact, on my 16-core machine, the application slows down approximately by factor 16.
  • Now I realized this is because there is only a sngle matlab engine started per machine and all my 16 instances share the same copy of matlab!
  • I tried to replicate this with the matlab GUI and its the same problem. I run a program in the GUI that takes 14 seconds, and THEN I run it in two GUIs at the same time and it takes 28 seconds

This is a huge problem for me, because I will miss my deadline if I have to reprogram my entire c application without matlab. I know that matlab has commands for parallel programming, but my matlab calls are embedded in the C application and I want to run multiple instances of the C application. Again, I cannot refactor my entire c application because I will miss the deadline.

Can anyone please let me know if there is a solution for this (e.g. really start multiple matlab processes on the same machine). I am willing to pay for extra licenses. I currently have fully lincensed matlab installed on all machines.

Thank you so so much!

EDIT

Thank you Ben Voigt for your help. I found that a single instance of Matlab is already using multiple cores. In fact, running one instance shows me full utilization of 4 cores. If I run two copies of Matlab, I get full utilization of 8 cores. Hence it is actually running in parallel. However, even though 2 instances seem to take up double the processing power, I still get 2* slowdown. Hence, 2 instances seem to get twice the result with 4* the compute power total. Why could that be?

Was it helpful?

Solution

Your slowdown is not caused by stuffing all N instances into a single MatLab instance on a single core, but by the fact that there are no longer 16 cores at the disposal of each instance. Many MATLAB vector operations use parallel computation even without explicit parallel constructs, so more than one core per instance is needed for optimal efficiency.

OTHER TIPS

MATLAB libraries are not thread-safe. If you create multithreaded applications, make sure only one thread accesses the engine application.

I think the matlab engine is the wrong technique. For windows platforms, you can try using the com automation server, which has the .Single option which starts one matlab instance for each com client you open.

Alternatives are:

  • Generate C++ code for the functions.
  • Create a .NET library. (NE Builder)
  • Run matlab via command line.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top