为 MATLAB 引擎设置处理器关联 (Windows 7) [英] Set processor affinity for MATLAB engine (Windows 7)

查看:34
本文介绍了为 MATLAB 引擎设置处理器关联 (Windows 7)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用 C++ 开发一个应用程序.该应用程序的组件之一使用 Matlab(通过 Matlab 引擎)进行数据处理.同时,数据采集系统正在将数据流式传输到磁盘.有时,在密集的 Matlab 处理期间,采集系统会崩溃.通过将 Matlab 的处理器亲和性设置为可用处理器的子集,这个问题得到了解决.然而,由于应用程序每天启动几次,并且在多台机器上,每次手动设置亲和度并不方便.通过快捷方式的命令行设置处理器关联的技巧不起作用,因为引擎是从我的应用程序中启动的,而不是通过快捷方式启动的.我一直在寻找一种以编程方式设置亲和力的方法,但收效甚微.

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. 在引擎启动时,在应用程序中为 matlab 引擎指定处理器亲和性.
  2. 为 matlab 引擎指定一个默认的处理器关联,与完整的 Matlab 应用程序本身分开.
  3. 作为最后的手段,为 Matlab 设置默认关联(引擎和非引擎使用).这是最不可取的,因为 Matlab 在部署机器上用于其他目的,最好不要将其限制为其他用途.

是否可以在我的应用程序中设置处理器关联,如果可以,如何设置?如果没有,解决这个问题的正确方法是什么?欢迎对这些选项或其他建议/解决方案提出任何建议.

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.

推荐答案

听起来你在 Windows 上.您可以直接从 Matlab 调用 .NET 来操作处理器关联掩码,而不必构建 MEX 文件.System.Diagnostics.Process 类具有对处理器关联性的控制,如 中所述这个解决方案.这是一个使用它的 Matlab 函数.启动后首先在 Matlab 引擎中运行它.

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));

由于 Matlab 在 Windows 上公开了 .NET 标准库对象,您有时可以在 C# 或 .NET 下搜索此类问题并将答案直接移植到 Matlab.

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.

这篇关于为 MATLAB 引擎设置处理器关联 (Windows 7)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆