numpy - (I)Python 中大数据 block 的并行性

标签 numpy multiprocessing ipython

我已经在线程和进程上辛苦工作了一段时间,试图加快 IPython 中的并行工作速度。我不确定我调用的函数有多少细节是有用的,所以这里有一个 bash,但询问您是否需要更多。

我的函数的调用签名看起来像

def intersplit_array(ob,er,nl,m,mi,t,ti,dmax,n0=6,steps=50):

基本上,ob , ernl是观测值的参数,m , mi , t , tidmax是表示将与观察结果进行比较的模型的参数。 ( n0steps 是该函数的固定数值参数。)该函数循环遍历 m 中的所有模型。并且,使用 mi 中的相关信息, t , tidmax ,计算该模型匹配的概率。请注意m相当大:它是大约 700 000 个 22x3 NumPy 数组的列表。 midmax大小相似。如果相关的话,我的普通 IPython 实例在 top 中使用了大约 25% 的系统内存。 :我的 16GB RAM 中的 4GB。

我尝试通过两种方式并行化它。首先,我尝试使用parallel_map at the SciPy Cookbook 给出的函数。我打了电话

P = parallel_map(lambda i: intersplit_array(ob,er,nl,m[i+1],mi[i:i+2],t[i+1],ti[i:i+2],dmax[i+1],range(1,len(m)-1))

它运行并提供正确的答案。没有 parallel_部分,这只是将函数逐一应用于每个元素的结果。但这比使用单核慢。我猜这与全局解释器锁有关?

其次,我尝试使用Pool来自multiprocessing 。我初始化了一个池

p = multiprocessing.Pool(6)

然后尝试调用我的函数

P = p.map(lambda i: intersplit_array(ob,er,nl,m[i+1],mi[i:i+2],t[i+1],ti[i:i+2],dmax[i+1],range(1,len(m)-1))

首先,我收到一个错误。

Exception in thread Thread-3:
Traceback (most recent call last):
  File "/usr/lib64/python2.7/threading.py", line 551, in __bootstrap_inner
    self.run()
  File "/usr/lib64/python2.7/threading.py", line 504, in run
    self.__target(*self.__args, **self.__kwargs)
  File "/usr/lib64/python2.7/multiprocessing/pool.py", line 319, in _handle_tasks
    put(task)
PicklingError: Can't pickle <type 'function'>: attribute lookup __builtin__.function failed

查看top ,然后我看到所有额外的 ipython进程,每个进程显然占用了 25% 的 RAM(这不可能,因为我还有 4GB 可用空间)并且使用 0% 的 CPU。我认为它没有做任何事情。我也无法使用 IPython。我尝试了 Ctrl-C 一段时间,但在超过第 300 个池工作人员后就放弃了。

最佳答案

它是否可以交互工作?

multiprocessing 由于其拆分进程的方式而不能很好地交互运行。这也是为什么你很难杀死它,因为它产生了如此多的进程。您必须跟踪主进程才能取消它。

来自the documentation :

Note
Functionality within this package requires that the __main__ module be importable by the children. This is covered in Programming guidelines however it is worth pointing out here. This means that some examples, such as the multiprocessing.Pool examples will not work in the interactive interpreter.
...
If you try this it will actually output full tracebacks interleaved in a semi-random fashion, and then you may have to stop the master process somehow.

最好的解决方案可能是从命令行将其作为脚本运行。或者,IPython has its own system for parallel computing ,但我从来没有用过它。

关于numpy - (I)Python 中大数据 block 的并行性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16540675/

相关文章:

python - numpy中deg2rad和弧度之间的区别?

python - 将 numpy (n,) 向量 reshape 为 (n,1) 向量

python - 检查日期是否属于 pandas 数据框

python - Multiprocessing Pool.apply 执行 n-1 次

python - 多处理日志记录 : Lock vs Queue

python - 如何计算跨列列表中元素的平均值?

python - 创建和填充巨大的 numpy 二维数组的最快方法?

python - 从 python 脚本执行 IPython 笔记本单元

ipython - "import matlab.engine"适用于 Linux 命令行,但不适用于 Spyder

python - 使用 IPython 测量(最大)内存使用情况——类似于 timeit 但 memit