<bdo id='VE0kN'></bdo><ul id='VE0kN'></ul>
  • <small id='VE0kN'></small><noframes id='VE0kN'>

        <tfoot id='VE0kN'></tfoot>

      1. <legend id='VE0kN'><style id='VE0kN'><dir id='VE0kN'><q id='VE0kN'></q></dir></style></legend>
        <i id='VE0kN'><tr id='VE0kN'><dt id='VE0kN'><q id='VE0kN'><span id='VE0kN'><b id='VE0kN'><form id='VE0kN'><ins id='VE0kN'></ins><ul id='VE0kN'></ul><sub id='VE0kN'></sub></form><legend id='VE0kN'></legend><bdo id='VE0kN'><pre id='VE0kN'><center id='VE0kN'></center></pre></bdo></b><th id='VE0kN'></th></span></q></dt></tr></i><div id='VE0kN'><tfoot id='VE0kN'></tfoot><dl id='VE0kN'><fieldset id='VE0kN'></fieldset></dl></div>

        numpy 与多处理和 mmap

        时间:2023-05-27
      2. <i id='X5kwD'><tr id='X5kwD'><dt id='X5kwD'><q id='X5kwD'><span id='X5kwD'><b id='X5kwD'><form id='X5kwD'><ins id='X5kwD'></ins><ul id='X5kwD'></ul><sub id='X5kwD'></sub></form><legend id='X5kwD'></legend><bdo id='X5kwD'><pre id='X5kwD'><center id='X5kwD'></center></pre></bdo></b><th id='X5kwD'></th></span></q></dt></tr></i><div id='X5kwD'><tfoot id='X5kwD'></tfoot><dl id='X5kwD'><fieldset id='X5kwD'></fieldset></dl></div>

      3. <legend id='X5kwD'><style id='X5kwD'><dir id='X5kwD'><q id='X5kwD'></q></dir></style></legend>

            1. <tfoot id='X5kwD'></tfoot>
                <bdo id='X5kwD'></bdo><ul id='X5kwD'></ul>
              • <small id='X5kwD'></small><noframes id='X5kwD'>

                    <tbody id='X5kwD'></tbody>
                • 本文介绍了numpy 与多处理和 mmap的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  我正在使用 Python 的 multiprocessing 模块来并行处理大型 numpy 数组.数组在主进程中使用 numpy.load(mmap_mode='r') 进行内存映射.之后,multiprocessing.Pool() 分叉进程(我猜).

                  I am using Python's multiprocessing module to process large numpy arrays in parallel. The arrays are memory-mapped using numpy.load(mmap_mode='r') in the master process. After that, multiprocessing.Pool() forks the process (I presume).

                  一切似乎都运行良好,除了我得到如下行:

                  Everything seems to work fine, except I am getting lines like:

                  AttributeError("'NoneType' object has no attribute 'tell'",)
                    in `<bound method memmap.__del__ of
                         memmap([ 0.57735026,  0.57735026,  0.57735026,  0.        ,  0.        ,        0.        ,  0.        ,  0.        ,  0.        ,  0.        ,        0.        ,  0.        ], dtype=float32)>`
                       ignored
                  

                  在单元测试日志中.尽管如此,测试还是通过了.

                  in the unittest logs. The tests pass fine, nevertheless.

                  知道那里发生了什么吗?

                  Any idea what's going on there?

                  使用 Python 2.7.2、OS X、NumPy 1.6.1.

                  Using Python 2.7.2, OS X, NumPy 1.6.1.

                  更新:

                  经过一些调试,我找到了一个代码路径的原因,该路径使用这个内存映射的 numpy 数组的(一小部分)作为 Pool.imap 调用的输入.

                  After some debugging, I hunted down the cause to a code path that was using a (small slice of) this memory-mapped numpy array as input to a Pool.imap call.

                  显然,问题"在于 multiprocessing.Pool.imap 将其输入传递给新进程的方式:它使用 pickle.这不适用于 mmaped numpy 数组,并且内部的某些东西会导致错误.

                  Apparently the "issue" is with the way multiprocessing.Pool.imap passes its input to the new processes: it uses pickle. This doesn't work with mmaped numpy arrays, and something inside breaks which leads to the error.

                  我发现 这个回复 Robert Kern 似乎解决了同样的问题.他建议为 imap 输入来自内存映射数组时创建一个特殊的代码路径:在生成的进程中手动内存映射相同的数组.

                  I found this reply by Robert Kern which seems to address the same issue. He suggests creating a special code path for when the imap input comes from a memory-mapped array: memory-mapping the same array manually in the spawned process.

                  这将是如此复杂和丑陋,以至于我宁愿忍受错误和额外的内存副本.有没有其他方法可以更轻松地修改现有代码?

                  This would be so complicated and ugly that I'd rather live with the error and the extra memory copies. Is there any other way that would be lighter on modifying existing code?

                  推荐答案

                  我通常的方法(如果你可以忍受额外的内存副本)是在一个进程中完成所有 IO,然后将它们发送到工作线程池中.要将内存映射数组的切片加载到内存中,只需执行 x = np.array(data[yourslice]) (data[yourslice].copy() 实际上并不这样做会导致一些混乱.).

                  My usual approach (if you can live with extra memory copies) is to do all IO in one process and then send things out to a pool of worker threads. To load a slice of a memmapped array into memory just do x = np.array(data[yourslice]) (data[yourslice].copy() doesn't actually do this, which can lead to some confusion.).

                  首先,让我们生成一些测试数据:

                  First off, let's generate some test data:

                  import numpy as np
                  np.random.random(10000).tofile('data.dat')
                  

                  您可以通过以下方式重现您的错误:

                  You can reproduce your errors with something like this:

                  import numpy as np
                  import multiprocessing
                  
                  def main():
                      data = np.memmap('data.dat', dtype=np.float, mode='r')
                      pool = multiprocessing.Pool()
                      results = pool.imap(calculation, chunks(data))
                      results = np.fromiter(results, dtype=np.float)
                  
                  def chunks(data, chunksize=100):
                      """Overly-simple chunker..."""
                      intervals = range(0, data.size, chunksize) + [None]
                      for start, stop in zip(intervals[:-1], intervals[1:]):
                          yield data[start:stop]
                  
                  def calculation(chunk):
                      """Dummy calculation."""
                      return chunk.mean() - chunk.std()
                  
                  if __name__ == '__main__':
                      main()
                  

                  如果你只是切换到产生 np.array(data[start:stop]) 代替,你会解决问题:

                  And if you just switch to yielding np.array(data[start:stop]) instead, you'll fix the problem:

                  import numpy as np
                  import multiprocessing
                  
                  def main():
                      data = np.memmap('data.dat', dtype=np.float, mode='r')
                      pool = multiprocessing.Pool()
                      results = pool.imap(calculation, chunks(data))
                      results = np.fromiter(results, dtype=np.float)
                  
                  def chunks(data, chunksize=100):
                      """Overly-simple chunker..."""
                      intervals = range(0, data.size, chunksize) + [None]
                      for start, stop in zip(intervals[:-1], intervals[1:]):
                          yield np.array(data[start:stop])
                  
                  def calculation(chunk):
                      """Dummy calculation."""
                      return chunk.mean() - chunk.std()
                  
                  if __name__ == '__main__':
                      main()
                  

                  当然,这确实会为每个块创建一个额外的内存副本.

                  Of course, this does make an extra in-memory copy of each chunk.

                  从长远来看,您可能会发现从内存映射文件切换到 HDF 之类的文件会更容易.如果您的数据是多维的,则尤其如此.(我推荐 h5py,但如果您的数据是类似表格的",pyTables 会很好.)

                  In the long run, you'll probably find that it's easier to switch away from memmapped files and move to something like HDF. This especially true if your data is multidimensional. (I'd reccomend h5py, but pyTables is nice if your data is "table-like".)

                  祝你好运,无论如何!

                  这篇关于numpy 与多处理和 mmap的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:如何通过 python 多处理利用所有内核 下一篇:如何使用初始化程序来设置我的多进程池?

                  相关文章

                  最新文章

                  <i id='3la2n'><tr id='3la2n'><dt id='3la2n'><q id='3la2n'><span id='3la2n'><b id='3la2n'><form id='3la2n'><ins id='3la2n'></ins><ul id='3la2n'></ul><sub id='3la2n'></sub></form><legend id='3la2n'></legend><bdo id='3la2n'><pre id='3la2n'><center id='3la2n'></center></pre></bdo></b><th id='3la2n'></th></span></q></dt></tr></i><div id='3la2n'><tfoot id='3la2n'></tfoot><dl id='3la2n'><fieldset id='3la2n'></fieldset></dl></div>
                  <legend id='3la2n'><style id='3la2n'><dir id='3la2n'><q id='3la2n'></q></dir></style></legend>
                    • <bdo id='3la2n'></bdo><ul id='3la2n'></ul>

                    <small id='3la2n'></small><noframes id='3la2n'>

                    1. <tfoot id='3la2n'></tfoot>