Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running multiprocess applications from MATLAB

I've written a multitprocess application in VC++ and tried to execute it with command line arguments with the system command from MATLAB. It runs, but only on one core --- any suggestions?

Update:In fact, it doesn't even see the second core. I used OpenMP and used omp_get_max_threads() and omp_get_thread_num() to check and omp_get_max_threads() seems to be 1 when I execute the application from MATLAB but it's 2 (as is expected) if I run it from the command window.

Question:My task manager reports that CPU usage is close to 100% --- could this mean that the aforementioned API is malfunctioning it's still running as a multiprocess application?

Confirmation:

I used Process Explorer to check if there were any differences in the number of threads.

When I call the application from the command window, 1 thread goes to cmd.exe and 2 go to my application.

When I call it from MATLAB, 26 threads are for MATLAB.exe, 1 for cmd.exe and 1 for my application.

Any ideas?

like image 748
Jacob Avatar asked Apr 13 '10 00:04

Jacob


1 Answers

The question is how Matlab is affecting your app's behavior, since it's a separate process. I suspect Matlab is modifying environment variables in a manner that affects OMP, maybe because it uses OMP internally, and the process you are spawning from Matlab is inheriting this modified environment.

Do a "set > plain.txt" from the command window where you're launching you app plain, and "system('set > from_matlab.txt')" from within Matlab, and diff the outputs. This will show you the differences in environment variables that Matlab is introducing. When I do this, this appears in the environment inherited from Matlab, but not in the plain command window's environment.

OMP_NUM_THREADS=1 

That looks like an OpenMP setting related to the function calls in your question. I'll bet your spawned app is seeing that and respecting it.

I don't know why Matlab is setting it. But as a workaround, when you launch the app from Matlab, instead of calling it directly, call a wrapper .bat file that clears the OMP_NUM_THREADS environment variable, or sets it to a higher number.

like image 105
Andrew Janke Avatar answered Oct 06 '22 23:10

Andrew Janke