【问题标题】:use GNU parallel to parallelize a multi-threaded command使用 GNU parallel 并行化多线程命令
【发布时间】:2017-01-26 09:36:32
【问题描述】:

我刚刚写了一个涉及多线程的python脚本,比如:

    python myScript.py -cpu_n 5 -i input_file

为了为我的数百个输入文件运行命令,我正在为每个文件生成一个命令列表 (commands.list):

    python myScript.py -cpu_n 5 -i input_file1
    python myScript.py -cpu_n 5 -i input_file2
    python myScript.py -cpu_n 5 -i input_file3
    ...

我正在尝试使用命令“parallel”和三台不同机器的 10 个 CPU 来安排它们:

   parallel -S 10/$server1 -S 10/$server2 -S 10/$server3 < commands.list

我的问题是:并行命令在每台服务器上使用的最大 CPU 数是多少?是 5*10=50 还是只有 10 个 CPU?

【问题讨论】:

  • 没有代码很难说。
  • 首先,要注意区分CPUscores,单个CPU可能有8个或更多核。其次,您需要小心 jobscores - 您可以创建一个可能会四处游荡并在不同时间在不同核心上执行的作业 - 想想 " CPU/核心亲和性”。理论上,一项作业可以在其生命周期的某个阶段在 10 个 CPU 中的每一个上运行。
  • 如果 commands.list 如此简单,您可能只需让 GNU Parallel 生成它:parallel python myScript.py -cpu_n 5 -i ::: input_file*;如果命令非常复杂,请使用 bash 函数:gnu.org/software/parallel/…

标签: python multithreading gnu-parallel


【解决方案1】:

来自man parallel

   --jobs N
   -j N
   --max-procs N
   -P N     Number of jobslots on each machine. Run up to N
            jobs in parallel.  0 means as many as possible.
            Default is 100% which will run one job per CPU
            core on each machine.


   -S
   [@hostgroups/][ncpu/]sshlogin[,[@hostgroups/][ncpu/]sshlogin[,...]]
   :
            GNU parallel will determine the number of CPU
            cores on the remote computers and run the number
            of jobs as specified by -j.  If the number ncpu
            is given GNU parallel will use this number for
            number of CPU cores on the host. Normally ncpu
            will not be needed.

因此,您的命令将在每台服务器上并行运行多达 10 个作业。

您的每个命令是否将使用 5 个 CPU 内核尚不清楚。如果您的每个命令使用 5 个核心,则每台服务器将使用 50 个核心,在这种情况下,我建议您不要使用 ncpu/server 语法,而是使用:

parallel -j 20% -S $server1,$server2,$server3 < commands.list

这样您可以混合具有不同内核数量的服务器,GNU Parallel 将并行启动其中的 1/5。

【讨论】:

  • 我同意应该包括百分比。有趣的是,我只是在较小的规模上进行了尝试:一个使用 2 cpu 的 python 脚本和一个分别在三台服务器上使用 2 cpu 的 prallel 命令。结果是每台服务器只提供2个cpus而不提供2*2个cpus。