Set processor affinity for MATLAB engine (Windows 7)

淺唱寂寞╮ 提交于 2019-11-29 06:58:39
Andrew Janke

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.

I haven't tried this solution but it seems like it should work. Create a simple mex function that does the following:

  1. Call GetCurrentProcess to retrieve a handle to the MATLAB process
  2. Set the appropriate affinity mask for this process using SetProcessAffinityMask

Now, when your application launches, just call this mex function as you would a regular MATLAB function (the mex function must be visible on the MATLAB path) and it should set processor affinity as you desire. You could even pass the affinity mask as an input to the function to make it more versatile.

Amro

Below is an implementation of the MEX function that @Praetorian described (shows how to use the SetProcessAffinityMask function):

set_affinity.c

#include "mex.h"
#include <windows.h>

void mexFunction(int nlhs, mxArray* plhs[], int nrhs, const mxArray* prhs[])
{
    HANDLE hProc;
    DWORD_PTR dwAffinityMask;
    unsigned int numCores;

    // check arguments
    if (nlhs > 0 || nrhs != 1) {
        mexErrMsgIdAndTxt("mex:error", "Wrong number of arguments.");
    }
    if (!mxIsDouble(prhs[0]) || mxGetNumberOfElements(prhs[0])!=1) {
        mexErrMsgIdAndTxt("mex:error", "Expecting a scalar number.");
    }

    // number of logical processors
    numCores = (unsigned int) mxGetScalar(prhs[0]);

    // set affinity of current process to use all cores
    hProc = GetCurrentProcess();
    dwAffinityMask = (1 << numCores) - 1;
    if (!SetProcessAffinityMask(hProc, dwAffinityMask)) {
        mexErrMsgIdAndTxt("mex:error", "WinAPI error code: %lu", GetLastError());
    }
}

Example:

On my quad-core hyper-threaded machine, I would invoke the MEX-function as following to allow MATLAB to execute on all 8 logical processors:

>> getenv('NUMBER_OF_PROCESSORS')
ans =
8

>> mex -largeArrayDims set_affinity.c
>> set_affinity(8)

To use only half the number of processors:

>> set_affinity(4)

Note the following remark in the MSDN doc page:

Process affinity is inherited by any child process or newly instantiated local process.

Do not call SetProcessAffinityMask in a DLL that may be called by processes other than your own.

So messing with the affinity will affect all computations initiated by MATLAB and its dependent libraries. Here is a post by Raymond Chen on the topic.

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