Running multiprocess applications from MATLAB

最后都变了- 提交于 2020-01-11 09:09:31

问题


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:


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.




回答2:


Run the command outside of Matlab and see how many cores its using. There should be no difference running it from within Matlab because its just a call down to the operating system. IE. equivalent from running on command line.

EDIT

Ok odd, what do you get when you call feature('NumCores') ? What version of Matlab are you using?

Does it help to enable this?




回答3:


you have to execute in MATLAB command-line:

setenv OMP_NUM_THREADS 4

if you want to use 4 threads.



来源:https://stackoverflow.com/questions/2626230/running-multiprocess-applications-from-matlab

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!