Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
435 views
in Technique[技术] by (71.8m points)

c++ - Set processor affinity for MATLAB engine (Windows 7)

I'm developing an application in c++. One of the components of the application uses Matlab (via the Matlab engine) for data processing. At the same time, a data acquisition system is streaming data to disk. Occasionally, during periods of intensive Matlab processing, the acquisition system crashes. By setting the processor affinity of Matlab to a subset of available processors, this problem is resolved. However, as the application is launched a few times daily, and on multiple machines, manually setting the affinity each time is inconvenient. The trick of setting processor affinity via the command-line of a shortcut doesn't work, since the engine is launched from within my application, not via a shortcut. I've been searching for a way to programatically set the affinity, but with limited success.

I've considered the following options (ranked in order of preference):

  1. Specify processor affinity for the matlab engine from within the application, when the engine is launched.
  2. Specify a default processor affinity for the matlab engine, separately from the full Matlab application itself.
  3. As a last resort, set a default affinity for Matlab (both engine and non-engine uses). This is the least desirable, since Matlab is used for other purposes on the deployment machines, and it would be preferable to not limit it for other usages.

Is it possible to set the processor affinity from within my application, and if so, how? If not, what is the right way to tackle this problem? Any advice on these options, or other suggestions/solutions, will be welcome.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Sounds like you're on Windows. You can call .NET directly from Matlab to manipulate the processor affinity mask, and avoid having to build a MEX file. The System.Diagnostics.Process class has controls for processor affinity, as described in this solution. Here's a Matlab function that uses it. Run it in the Matlab engine first thing after launching it.

function twiddle_processor_affinity()
proc = System.Diagnostics.Process.GetCurrentProcess();
aff = proc.ProcessorAffinity.ToInt32;  % get current affinity mask
fprintf('Current affinity mask: %s
', dec2bin(aff, 8));
proc.ProcessorAffinity = System.IntPtr(int32(2)); % set affinity mask
fprintf('Adjusted affinity to: %s
', dec2bin(proc.ProcessorAffinity.ToInt32, 8));

Since Matlab exposes the .NET standard library objects on Windows, you can sometimes search for questions like this under C# or .NET and port the answer directly over to Matlab.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...