如何将任务分配给特定的逻辑核心?

如何将任务分配给特定的逻辑核心?

从命令终端调用计算密集型程序。通过在一个或多个批处理文件中多次调用该程序,需要进行一组计算,因此每个调用都可以并行运行。假设基本终端语法是:

模拟.exe输入.txt

假设 Windows 10 PC 有一个i7-3770 CPU具有 4 个物理核心:每个物理核心提供两个逻辑核心,总共 8 个逻辑核心。生成 6 个批处理文件:每个逻辑核心一个批处理文件。目标是确保 6 个批处理文件在 3 个物理核心上运行,第 4 个核心仅供 Windows 使用。例如,go_CPU1.bat执行:

 simulate.exe input1.txt
 simulate.exe input2.txt
 simulate.exe input3.txt
 ...
 simulate.exe input100.txt

是否有命令行语法可以将模拟分配给特定的枚举逻辑核心?

答案1

您可以使用startaffinity掩码相结合的命令来控制想要将进程隔离到哪些核心。

START ["标题"] [/D 路径] [/I] [/MIN] [/MAX] [/SEPARATE | /SHARED] [/LOW | /NORMAL | /HIGH | /REALTIME | /ABOVENORMAL | /BELOWNORMAL] [/NODE ] [/AFFINITY ] [/WAIT] [/B​​] [命令/程序] [参数]

AFFINITY    Specifies the processor affinity mask as a hexadecimal number.
            The process is restricted to running on these processors.
            The affinity mask is interpreted differently when /AFFINITY and
            /NODE are combined.  Specify the affinity mask as if the NUMA
            node's processor mask is right shifted to begin at bit zero.
            The process is restricted to running on those processors in
            common between the specified affinity mask and the NUMA node.
            If no processors are in common, the process is restricted to
            running on the specified NUMA node.

所有掩码均以十六进制表示。 您可以使用 Windows 计算器的程序员模式来计算。下面我将向您展示如何操作。

例如,十六进制值 3A 转换为00111010
基于 0 的 proc #1 实际上是 #0
另请注意,二进制读取方向相反

这些是流程的布局
二进制:00111010 <-HEX 3A
实际:76543210 <-按相反顺序处理

3A 表示不要使用 0,2,6,7 - 使用 1,3,4,5

“start /AFFINITY 3A Notepad.exe”生成以下内容:

启动/AFFINITY 3A Notepad.exe

真是太痛苦了。。对吧?。。
别担心!方便的 Windows 计算器可以帮助我们。

  1. 打开calc.exe并将其更改为程序员模式。
  2. 单击第二个带点的“位切换键盘”(如屏幕截图所示)。
  3. 单击左侧的 BIN 东西。
  4. 输入您想要使用的程序的二进制文件。

为了让您观看时更愉快,我附上了带有提示的屏幕截图。

Windows Calc.exe 显示程序员模式

  @echo off
  start /AFFINITY 3A notepad.exe
  REM Why on earth would notepad need so many procs?
  REM Who knows.. ?

相关内容