views:

235

answers:

3

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?

+1  A: 
Pyrolistical
It works. I opened a new command window and keyed in the exact command, and it uses both cores.
Jacob
Thanks for the help! I think that's for MATLAB's own computation. It's set to `Automatic` and `2` at the moment. `feature('NumCores') ` returns `2` as well
Jacob
+4  A: 

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.

Andrew Janke
Dude .. +100 if it were possible. Thanks!
Jacob
For those who're interested, I wrote a batch file with `set OMP_NUM_THREADS=%NUMBER_OF_PROCESSORS%` before executing any commands :)
Jacob
D'oh! I should have thought of this myself, since I recently had a problem with environment variables in the terminal "opened" by Matlab for system commands. +1!
Jonas
A: 

you have to execute in MATLAB command-line:

setenv OMP_NUM_THREADS 4

if you want to use 4 threads.

S.Fiorentino