Set processor affinity for MATLAB engine (Windows 7)

前端 未结 3 962
萌比男神i
萌比男神i 2020-12-18 01:05

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

3条回答
  •  北荒
    北荒 (楼主)
    2020-12-18 01:49

    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\n', dec2bin(aff, 8));
    proc.ProcessorAffinity = System.IntPtr(int32(2)); % set affinity mask
    fprintf('Adjusted affinity to: %s\n', 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.

提交回复
热议问题