【问题标题】:Python Multiprocessing of parallel cases并行案例的Python多处理
【发布时间】:2018-01-03 03:18:37
【问题描述】:

我有一组模拟,每个都使用 MPI 并行运行(它们是 CFD 模拟)。但是,我想在 Python 中创建一个任务池并并行运行。我使用的多处理库如下:

import itertools
import multiprocessing

def Run_Cases(params):
    run_each_mpi_simulation

a = range(1,len(U)); b = range(len(R))
paramlist = list(itertools.product(a,b))
pool = multiprocessing.Pool()
pool.map(Run_Case,paramlist)

所以基本上,代码创建任务池(模拟实例)并将它们分配给每个处理器运行。但是,它没有考虑到每个模拟都需要 2 个处理器,因为每种情况都是并行 (MPI) 模拟。这导致模拟中的性能显着下降。

在此,我想知道是否有可能以某种方式定义分配给 Python 多处理包中每个任务的处理器数量?

非常感谢任何 cmets。

亲切的问候 阿什坎

编辑/更新:

非常感谢@AntiMatterDynamite 的回答。

我尝试了您的方法,处理器上的性能和工作负载分布得到了很大改善,但似乎存在两个问题:

1) 尽管一切仍在继续,但我收到以下消息

Exception in thread Thread-2:
Traceback (most recent call last):
  File "/usr/lib/python2.7/threading.py", line 801, in __bootstrap_inner
    self.run()
  File "/usr/lib/python2.7/threading.py", line 1073, in run
    self.function(*self.args, **self.kwargs)
  File "/usr/local/lib/python2.7/dist-packages/PyFoam/Execution/FoamThread.py", line 86, in getLinuxMem
    me=psutil.Process(thrd.threadPid)
  File "/usr/local/lib/python2.7/dist-packages/psutil/__init__.py", line 341, in __init__
    self._init(pid)
  File "/usr/local/lib/python2.7/dist-packages/psutil/__init__.py", line 381, in _init
    raise NoSuchProcess(pid, None, msg)
NoSuchProcess: psutil.NoSuchProcess no process found with pid 8880

非常感谢您的 cmets。

再次感谢 阿什坎

编辑/更新:

我认为该消息是因为进程数少于处理器列表。因为我有两个案例/模拟,每个案例都使用 2 个处理器,所以当我使用超线程时,我有 8 个处理器,所以得到了消息。它是使用 4 个处理器或具有更大的模拟池来解决的。

【问题讨论】:

  • 我无法完全理解这个问题。那么什么是“假设 2 个处理器,因为每种情况都是并行 (MPI) 模拟”?
  • 确保您在线程之间正确分配工作:您不希望... 10 个线程(可以说是)在每个线程中运行整个模拟。
  • 很抱歉,如果描述不清楚。基本上,我的意思是我的每个 python 任务都是一个并行模拟,将使用 2 个处理器。因此,当任务分配给处理器时,我需要每个任务有 2 个处理器可用,而不是 1 个。想想它就像 python 代码试图同时运行多个模拟,但每个模拟本身都是一个并行模拟,需要2个处理器。我真的希望我的意思很清楚。
  • @BorrajaX 我认为这正是我应该做的,但我怎样才能正确地将任务拆分到线程?例如,我需要将每个任务拆分为 2 个线程。
  • 这取决于任务和模拟的运行方式。积分清单?也许您可以将点的前半部分传递给一个,而将第二部分传递给另一个? (但要注意相互依赖!!)

标签: python python-multiprocessing


【解决方案1】:

multiprocessing.Pool 接受要创建的进程数作为其第一个参数。 您可以使用multiprocessing.cpu_count() 获取逻辑 cpu 核心的数量,然后在池中创建一半的进程(因此每个得到 2 个)

 multiprocessing.Pool(multiprocessing.cpu_count()/2)

这假设您的 cpu 计数除以 2,这对于几乎所有 cpu 来说都是正确的......

请注意,此解决方案不考虑 SMT(或超线程),因为 multiprocessing.cpu_count() 计算 逻辑 内核,因此它可能会报告双倍的物理内核。对于大多数 CPU 密集型任务而言,SMT 可以提高性能,您可以运行两倍以上的任务,但速度会超过一半,如果您确实有 SMT,您需要确定它是否适合您的模拟

最后,您还可以设置每个进程的亲和性,使其只能在 2 个内核上运行 但是没有直接的标准方法可以做到这一点,因为多处理不会公开它打开的进程的 PID。这是一些粗略的完整代码,可以为每个进程设置亲和力:

import multiprocessing
import psutil
import itertools

cores_per_process = 2
cpu_count = multiprocessing.cpu_count()

manager = multiprocessing.Manager()
pid_list = manager.list()  # trick to find pid of all the processes in the pool

cores_list = range(cpu_count)
it = [iter(cores_list)] * cores_per_process  # this is a python trick to group items from the same list together into tuples
affinity_pool = manager.list(zip(*it))  # list of affinity

def Run_Case(params):
    self_proc = psutil.Process()  # get your own process
    if self_proc.pid not in pid_list:
        pid_list.append(self_proc.pid)  # found new pid in pool
        self_proc.cpu_affinity(affinity_pool.pop())  # set affinity from the affinity list but also remove it so other processes can't use the same affinity
    #run simulation

a = range(1, len(U))
b = range(len(R))
paramlist = list(itertools.product(a, b))
pool = multiprocessing.Pool(cpu_count/cores_per_process)  # decide on how many processes you want
pool.map(Run_Case, paramlist)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-28
    • 1970-01-01
    • 2012-01-23
    • 1970-01-01
    相关资源
    最近更新 更多