在共享内存中使用numpy数组进行多处理

Ian*_*ore 95 python shared numpy multiprocessing

我想在共享内存中使用numpy数组与多处理模块一起使用.困难是使用它像一个numpy数组,而不仅仅是一个ctypes数组.

from multiprocessing import Process, Array
import scipy

def f(a):
    a[0] = -a[0]

if __name__ == '__main__':
    # Create the array
    N = int(10)
    unshared_arr = scipy.rand(N)
    arr = Array('d', unshared_arr)
    print "Originally, the first two elements of arr = %s"%(arr[:2])

    # Create, start, and finish the child processes
    p = Process(target=f, args=(arr,))
    p.start()
    p.join()

    # Printing out the changed values
    print "Now, the first two elements of arr = %s"%arr[:2]
Run Code Online (Sandbox Code Playgroud)

这会产生如下输出:

Originally, the first two elements of arr = [0.3518653236697369, 0.517794725524976]
Now, the first two elements of arr = [-0.3518653236697369, 0.517794725524976]
Run Code Online (Sandbox Code Playgroud)

可以以ctypes方式访问数组,例如arr[i]有意义.但是,它不是一个numpy数组,我不能执行诸如-1*arr或之类的操作arr.sum().我想解决方案是将ctypes数组转换为numpy数组.然而(除了无法完成这项工作),我不相信它会被分享.

对于必须存在的常见问题,似乎会​​有一个标准的解决方案.

jfs*_*jfs 72

添加到@ unutbu(不再可用)和@Henry Gomersall的答案.您可以shared_arr.get_lock()在需要时使用同步访问:

shared_arr = mp.Array(ctypes.c_double, N)
# ...
def f(i): # could be anything numpy accepts as an index such another numpy array
    with shared_arr.get_lock(): # synchronize access
        arr = np.frombuffer(shared_arr.get_obj()) # no data copying
        arr[i] = -arr[i]
Run Code Online (Sandbox Code Playgroud)

import ctypes
import logging
import multiprocessing as mp

from contextlib import closing

import numpy as np

info = mp.get_logger().info

def main():
    logger = mp.log_to_stderr()
    logger.setLevel(logging.INFO)

    # create shared array
    N, M = 100, 11
    shared_arr = mp.Array(ctypes.c_double, N)
    arr = tonumpyarray(shared_arr)

    # fill with random values
    arr[:] = np.random.uniform(size=N)
    arr_orig = arr.copy()

    # write to arr from different processes
    with closing(mp.Pool(initializer=init, initargs=(shared_arr,))) as p:
        # many processes access the same slice
        stop_f = N // 10
        p.map_async(f, [slice(stop_f)]*M)

        # many processes access different slices of the same array
        assert M % 2 # odd
        step = N // 10
        p.map_async(g, [slice(i, i + step) for i in range(stop_f, N, step)])
    p.join()
    assert np.allclose(((-1)**M)*tonumpyarray(shared_arr), arr_orig)

def init(shared_arr_):
    global shared_arr
    shared_arr = shared_arr_ # must be inherited, not passed as an argument

def tonumpyarray(mp_arr):
    return np.frombuffer(mp_arr.get_obj())

def f(i):
    """synchronized."""
    with shared_arr.get_lock(): # synchronize access
        g(i)

def g(i):
    """no synchronization."""
    info("start %s" % (i,))
    arr = tonumpyarray(shared_arr)
    arr[i] = -1 * arr[i]
    info("end   %s" % (i,))

if __name__ == '__main__':
    mp.freeze_support()
    main()
Run Code Online (Sandbox Code Playgroud)

如果您不需要同步访问或创建自己的锁,则mp.Array()不需要.你可以mp.sharedctypes.RawArray在这种情况下使用.

  • 美丽的答案!如果我想要多个共享数组,每个数组都可以单独锁定,但在运行时确定数组的数量,这是您在这里所做的事情的直接扩展吗? (2认同)
  • @Andrew:应该在产生子进程之前创建共享数组。 (2认同)

Hen*_*all 18

Array对象具有get_obj()与之关联的方法,该方法返回呈现缓冲区接口的ctypes数组.我认为以下应该有效......

from multiprocessing import Process, Array
import scipy
import numpy

def f(a):
    a[0] = -a[0]

if __name__ == '__main__':
    # Create the array
    N = int(10)
    unshared_arr = scipy.rand(N)
    a = Array('d', unshared_arr)
    print "Originally, the first two elements of arr = %s"%(a[:2])

    # Create, start, and finish the child process
    p = Process(target=f, args=(a,))
    p.start()
    p.join()

    # Print out the changed values
    print "Now, the first two elements of arr = %s"%a[:2]

    b = numpy.frombuffer(a.get_obj())

    b[0] = 10.0
    print a[0]
Run Code Online (Sandbox Code Playgroud)

运行时,这会打印出现在a为10.0 的第一个元素,显示a并且b只是同一个内存中的两个视图.

为了确保它仍然是多处理器安全的,我相信你将不得不使用对象上存在的acquirerelease方法,以及它内置的锁以确保它的所有安全访问(尽管我不是多处理器模块).Arraya


Eel*_*aak 14

虽然已经给出的答案很好,但只要满足两个条件,就可以更容易地解决这个问题:

  1. 您使用的是符合POSIX标准的操作系统(例如Linux,Mac OSX); 和
  2. 您的子进程需要对共享阵列的只读访问权限.

在这种情况下,您不需要明确地使变量共享,因为子进程将使用fork创建.分叉子项自动共享父项的内存空间.在Python多处理的上下文中,这意味着它共享所有模块级变量; 请注意,这不适用于您明确传递给子进程或您在一个multiprocessing.Pool左右调用的函数的参数.

一个简单的例子:

import multiprocessing
import numpy as np

# will hold the (implicitly mem-shared) data
data_array = None

# child worker function
def job_handler(num):
    # built-in id() returns unique memory ID of a variable
    return id(data_array), np.sum(data_array)

def launch_jobs(data, num_jobs=5, num_worker=4):
    global data_array
    data_array = data

    pool = multiprocessing.Pool(num_worker)
    return pool.map(job_handler, range(num_jobs))

# create some random data and execute the child jobs
mem_ids, sumvals = zip(*launch_jobs(np.random.rand(10)))

# this will print 'True' on POSIX OS, since the data was shared
print(np.all(np.asarray(mem_ids) == id(data_array)))
Run Code Online (Sandbox Code Playgroud)

  • @EelkeSpaak:你的陈述 - "分叉的孩子自动分享父母的记忆空间" - 是不正确的.如果我有一个想要监视父进程状态的子进程,那么以严格只读的方式,分叉将不会让我在那里:子进程只能在分叉时看到父状态的快照.事实上,当我发现这个限制时,这正是我试图做的(跟着你的回答).因此,你的答案后记.简而言之:父状态不是"共享",而只是复制到孩子身上.这并不是通常意义上的"分享". (4认同)
  • 我是否错误地认为这是一种写时复制的情况,至少在 posix 系统上是这样?也就是说,在fork之后,我认为内存是共享的,直到写入新数据,此时会创建一个副本。所以是的,数据确实没有完全“共享”,但它可以提供潜在的巨大性能提升。如果您的进程是只读的,那么将没有复制开销!我是否正确理解了这一点? (4认同)
  • +1非常有价值的信息.你能解释为什么它只是共享的模块级变量吗?为什么本地变量不属于父级的内存空间?例如,如果我有一个带有局部变量V的函数F和一个参考V的F里面的函数G,为什么不能工作呢? (3认同)
  • 警告:这个答案有点欺骗性.子进程在fork时收到父进程状态的副本,包括全局变量.各州都没有同步,并且会与那个时刻背道而驰.此技术在某些情况下可能很有用(例如:分离每个处理父进程快照然后终止的临时子进程),但在其他情况下无用(例如:长时间运行的子进程必须共享和与父进程同步数据). (3认同)
  • @senderle 是的,这正是我的意思!因此,我在关于只读访问的答案中提出了第 (2) 点。 (2认同)

小智 10

我写了一个小的python模块,它使用POSIX共享内存在python解释器之间共享numpy数组.也许你会发现它很方便.

https://pypi.python.org/pypi/SharedArray

以下是它的工作原理:

import numpy as np
import SharedArray as sa

# Create an array in shared memory
a = sa.create("test1", 10)

# Attach it as a different array. This can be done from another
# python interpreter as long as it runs on the same computer.
b = sa.attach("test1")

# See how they are actually sharing the same memory block
a[0] = 42
print(b[0])

# Destroying a does not affect b.
del a
print(b[0])

# See how "test1" is still present in shared memory even though we
# destroyed the array a.
sa.list()

# Now destroy the array "test1" from memory.
sa.delete("test1")

# The array b is not affected, but once you destroy it then the
# data are lost.
print(b[0])
Run Code Online (Sandbox Code Playgroud)


Jas*_*sha 9

使用 Python3.8+,您可以使用multiprocessing.shared_memory标准库模块创建由共享内存支持的 numpy 数组。该共享内存可以被多个进程访问。这是一个例子:

# np_sharing.py
from multiprocessing import Process
from multiprocessing.managers import SharedMemoryManager
from multiprocessing.shared_memory import SharedMemory
from typing import Tuple

import numpy as np


def create_np_array_from_shared_mem(
    shared_mem: SharedMemory, shared_data_dtype: np.dtype, shared_data_shape: Tuple[int, ...]
) -> np.ndarray:
    arr = np.frombuffer(shared_mem.buf, dtype=shared_data_dtype)
    arr = arr.reshape(shared_data_shape)
    return arr


def child_process(
    shared_mem: SharedMemory, shared_data_dtype: np.dtype, shared_data_shape: Tuple[int, ...]
):
    """Logic to be executed by the child process"""
    arr = create_np_array_from_shared_mem(shared_mem, shared_data_dtype, shared_data_shape)
    arr[0, 0] = -arr[0, 0]  # modify the array backed by shared memory


def main():
    """Logic to be executed by the parent process"""

    # Data to be shared:
    data_to_share = np.random.rand(10, 10)

    SHARED_DATA_DTYPE = data_to_share.dtype
    SHARED_DATA_SHAPE = data_to_share.shape
    SHARED_DATA_NBYTES = data_to_share.nbytes

    with SharedMemoryManager() as smm:
        shared_mem = smm.SharedMemory(size=SHARED_DATA_NBYTES)

        arr = create_np_array_from_shared_mem(shared_mem, SHARED_DATA_DTYPE, SHARED_DATA_SHAPE)
        arr[:] = data_to_share  # load the data into shared memory

        print(f"The [0,0] element of arr is {arr[0,0]}")  # before

        # Run child process:
        p = Process(target=child_process, args=(shared_mem, SHARED_DATA_DTYPE, SHARED_DATA_SHAPE))
        p.start()
        p.join()

        print(f"The [0,0] element of arr is {arr[0,0]}")  # after

        del arr  # delete np array so the shared memory can be deallocated


if __name__ == "__main__":
    main()
Run Code Online (Sandbox Code Playgroud)

运行脚本:

$ python3.10 np_sharing.py
The [0,0] element of arr is 0.262091705529628
The [0,0] element of arr is -0.262091705529628
Run Code Online (Sandbox Code Playgroud)

由于不同进程中的数组共享相同的底层内存缓冲区,因此适用竞争条件的标准警告。


Vel*_*ker 8

您可以使用该sharedmem模块:https://bitbucket.org/cleemesser/numpy-sharedmem

这是你的原始代码,这次使用的行为类似于NumPy数组的共享内存(注意调用NumPy sum()函数的附加最后一个语句):

from multiprocessing import Process
import sharedmem
import scipy

def f(a):
    a[0] = -a[0]

if __name__ == '__main__':
    # Create the array
    N = int(10)
    unshared_arr = scipy.rand(N)
    arr = sharedmem.empty(N)
    arr[:] = unshared_arr.copy()
    print "Originally, the first two elements of arr = %s"%(arr[:2])

    # Create, start, and finish the child process
    p = Process(target=f, args=(arr,))
    p.start()
    p.join()

    # Print out the changed values
    print "Now, the first two elements of arr = %s"%arr[:2]

    # Perform some NumPy operation
    print arr.sum()
Run Code Online (Sandbox Code Playgroud)

  • 注意:这不再被开发,并且似乎不适用于 Linux https://github.com/sturlamolden/sharedmem-numpy/issues/4 (2认同)

归档时间:

查看次数:

57194 次

最近记录:

7 年,9 月 前