Making my NumPy array shared across processes

前端 未结 2 1544
[愿得一人]
[愿得一人] 2020-12-31 10:39

I have read quite a few of the questions on SO about sharing arrays and it seems simple enough for simple arrays but I am stuck trying to get it working for the array I have

2条回答
  •  情书的邮戳
    2020-12-31 11:22

    Note that you can start out with an array of complex dtype:

    In [4]: data = np.zeros(250,dtype='float32, (250000,2)float32')
    

    and view it as an array of homogenous dtype:

    In [5]: data2 = data.view('float32')
    

    and later, convert it back to complex dtype:

    In [7]: data3 = data2.view('float32, (250000,2)float32')
    

    Changing the dtype is a very quick operation; it does not affect the underlying data, only the way NumPy interprets it. So changing the dtype is virtually costless.

    So what you've read about arrays with simple (homogenous) dtypes can be readily applied to your complex dtype with the trick above.


    The code below borrows many ideas from J.F. Sebastian's answer, here.

    import numpy as np
    import multiprocessing as mp
    import contextlib
    import ctypes
    import struct
    import base64
    
    
    def decode(arg):
        chunk, counter = arg
        print len(chunk), counter
        for x in chunk:
            peak_counter = 0
            data_buff = base64.b64decode(x)
            buff_size = len(data_buff) / 4
            unpack_format = ">%dL" % buff_size
            index = 0
            for y in struct.unpack(unpack_format, data_buff):
                buff1 = struct.pack("I", y)
                buff2 = struct.unpack("f", buff1)[0]
                with shared_arr.get_lock():
                    data = tonumpyarray(shared_arr).view(
                        [('f0', '

    If you can guarantee that the various processes which execute the assignments

    if (index % 2 == 0):
        data[counter][1][peak_counter][0] = float(buff2)
    else:
        data[counter][1][peak_counter][1] = float(buff2)
    

    never compete to alter the data in the same locations, then I believe you can actually forgo using the lock

    with shared_arr.get_lock():
    

    but I don't grok your code well enough to know for sure, so to be on the safe side, I included the lock.

提交回复
热议问题