Skip to content

multiprocessing fail for large dask array #2602

Closed

Description

@amjhou

In the following code, use get=dask.threaded.get works. But get=dask.multiprocessing.get doesn't work. It will hang silently. I have 12 cpus on my machine. If I reduce the size of dask array, it then can work.

import scipy.ndimage.filters as sc
import numpy as np
import dask.array as da
import dask.multiprocessing

def filter(input):
    return sc.gaussian_filter(input,sigma=0.5)

daskdata = da.from_array(np.random.rand(1000,1000,1000),chunks=(300,350,1000))
decomposed_data = da.ghost.ghost_internal(daskdata, {0:2,1:2})
out = decomposed_data.map_blocks(filter)

out.compute() # This works. 

out.compute(get=dask.multiprocessing.get) # This doesn't work. 

The version of dask is 0.15.1. And I use python 3.5.

Another thing might be useful is that if I generate the dask array with daskdata = da.random.normal(0, 1, size=(1000,1000,1000),chunks=(300,350,1000)). It then can work for multiprocessing. It just takes much longer than the default get. (abou 17 times slower.) Don't know why.

Activity

  1. amjhou commented on Aug 12, 2017

    @amjhou
    Author

    Thanks for your reply. I understand the difference between schedulers. Using thread will be faster than process in this case. But I am surprised that using multiprocessing.get will completely hang it. I wait for a couple of hours. Nothing comes out. Is there any way to prevent this? Maybe generate an err message?

  2. mrocklin commented on Aug 12, 2017

    @mrocklin
    Member
  3. amjhou commented on Aug 12, 2017

    @amjhou
    Author

    The client does work. However, if you look at the profile, only one cpu is working although I have 12 available.

    Cluster
    
        Workers: 12
        Cores: 12
        Memory: 81.17 GB
    

    And it still takes much longer than threaded.get.

  4. amjhou commented on Aug 12, 2017

    @amjhou
    Author

    If I interrupt the run with multiprocess.get, I get the following err message. It seems to indicate where it hangs. It puzzles me since it will work properly if I reduce the size of the array.

    Process ForkPoolWorker-20:
    Process ForkPoolWorker-24:
    Process ForkPoolWorker-22:
    Process ForkPoolWorker-15:
    Process ForkPoolWorker-21:
    Process ForkPoolWorker-23:
    Process ForkPoolWorker-18:
    Process ForkPoolWorker-17:
    Process ForkPoolWorker-19:
    Process ForkPoolWorker-16:
    Process ForkPoolWorker-13:
    Process ForkPoolWorker-14:
    Traceback (most recent call last):
    Traceback (most recent call last):
    Traceback (most recent call last):
    Traceback (most recent call last):
    Traceback (most recent call last):
    Traceback (most recent call last):
      File "/home/App/anaconda3/lib/python3.5/multiprocessing/process.py", line 254, in _bootstrap
        self.run()
      File "/home/App/anaconda3/lib/python3.5/multiprocessing/process.py", line 254, in _bootstrap
        self.run()
    Traceback (most recent call last):
    Traceback (most recent call last):
    Traceback (most recent call last):
      File "/home/App/anaconda3/lib/python3.5/multiprocessing/process.py", line 254, in _bootstrap
        self.run()
    Traceback (most recent call last):
      File "/home/App/anaconda3/lib/python3.5/multiprocessing/process.py", line 254, in _bootstrap
        self.run()
      File "/home/App/anaconda3/lib/python3.5/multiprocessing/process.py", line 93, in run
        self._target(*self._args, **self._kwargs)
      File "/home/App/anaconda3/lib/python3.5/multiprocessing/process.py", line 93, in run
        self._target(*self._args, **self._kwargs)
      File "/home/App/anaconda3/lib/python3.5/multiprocessing/process.py", line 254, in _bootstrap
        self.run()
      File "/home/App/anaconda3/lib/python3.5/multiprocessing/process.py", line 254, in _bootstrap
        self.run()
      File "/home/App/anaconda3/lib/python3.5/multiprocessing/process.py", line 254, in _bootstrap
        self.run()
      File "/home/App/anaconda3/lib/python3.5/multiprocessing/process.py", line 254, in _bootstrap
        self.run()
      File "/home/App/anaconda3/lib/python3.5/multiprocessing/process.py", line 254, in _bootstrap
        self.run()
      File "/home/App/anaconda3/lib/python3.5/multiprocessing/process.py", line 93, in run
        self._target(*self._args, **self._kwargs)
    Traceback (most recent call last):
      File "/home/App/anaconda3/lib/python3.5/multiprocessing/process.py", line 254, in _bootstrap
        self.run()
      File "/home/App/anaconda3/lib/python3.5/multiprocessing/process.py", line 93, in run
        self._target(*self._args, **self._kwargs)
      File "/home/App/anaconda3/lib/python3.5/multiprocessing/process.py", line 93, in run
        self._target(*self._args, **self._kwargs)
      File "/home/App/anaconda3/lib/python3.5/multiprocessing/pool.py", line 108, in worker
        task = get()
      File "/home/App/anaconda3/lib/python3.5/multiprocessing/pool.py", line 108, in worker
        task = get()
      File "/home/App/anaconda3/lib/python3.5/multiprocessing/process.py", line 93, in run
        self._target(*self._args, **self._kwargs)
      File "/home/App/anaconda3/lib/python3.5/multiprocessing/process.py", line 93, in run
        self._target(*self._args, **self._kwargs)
      File "/home/App/anaconda3/lib/python3.5/multiprocessing/process.py", line 93, in run
        self._target(*self._args, **self._kwargs)
      File "/home/App/anaconda3/lib/python3.5/multiprocessing/process.py", line 93, in run
        self._target(*self._args, **self._kwargs)
      File "/home/App/anaconda3/lib/python3.5/multiprocessing/pool.py", line 108, in worker
        task = get()
      File "/home/App/anaconda3/lib/python3.5/multiprocessing/pool.py", line 108, in worker
        task = get()
      File "/home/App/anaconda3/lib/python3.5/multiprocessing/process.py", line 254, in _bootstrap
        self.run()
      File "/home/App/anaconda3/lib/python3.5/multiprocessing/process.py", line 93, in run
        self._target(*self._args, **self._kwargs)
      File "/home/App/anaconda3/lib/python3.5/multiprocessing/queues.py", line 342, in get
        with self._rlock:
      File "/home/App/anaconda3/lib/python3.5/multiprocessing/pool.py", line 108, in worker
        task = get()
      File "/home/App/anaconda3/lib/python3.5/multiprocessing/pool.py", line 108, in worker
        task = get()
      File "/home/App/anaconda3/lib/python3.5/multiprocessing/process.py", line 93, in run
        self._target(*self._args, **self._kwargs)
      File "/home/App/anaconda3/lib/python3.5/multiprocessing/pool.py", line 108, in worker
        task = get()
      File "/home/App/anaconda3/lib/python3.5/multiprocessing/queues.py", line 342, in get
        with self._rlock:
      File "/home/App/anaconda3/lib/python3.5/multiprocessing/pool.py", line 108, in worker
        task = get()
      File "/home/App/anaconda3/lib/python3.5/multiprocessing/queues.py", line 342, in get
        with self._rlock:
      File "/home/App/anaconda3/lib/python3.5/multiprocessing/pool.py", line 108, in worker
        task = get()
      File "/home/App/anaconda3/lib/python3.5/multiprocessing/queues.py", line 342, in get
        with self._rlock:
      File "/home/App/anaconda3/lib/python3.5/multiprocessing/pool.py", line 108, in worker
        task = get()
      File "/home/App/anaconda3/lib/python3.5/multiprocessing/synchronize.py", line 96, in __enter__
        return self._semlock.__enter__()
      File "/home/App/anaconda3/lib/python3.5/multiprocessing/queues.py", line 342, in get
        with self._rlock:
      File "/home/App/anaconda3/lib/python3.5/multiprocessing/queues.py", line 342, in get
        with self._rlock:
      File "/home/App/anaconda3/lib/python3.5/multiprocessing/synchronize.py", line 96, in __enter__
        return self._semlock.__enter__()
      File "/home/App/anaconda3/lib/python3.5/multiprocessing/synchronize.py", line 96, in __enter__
        return self._semlock.__enter__()
      File "/home/App/anaconda3/lib/python3.5/multiprocessing/synchronize.py", line 96, in __enter__
        return self._semlock.__enter__()
      File "/home/App/anaconda3/lib/python3.5/multiprocessing/queues.py", line 342, in get
        with self._rlock:
    KeyboardInterrupt
      File "/home/App/anaconda3/lib/python3.5/multiprocessing/synchronize.py", line 96, in __enter__
        return self._semlock.__enter__()
      File "/home/App/anaconda3/lib/python3.5/multiprocessing/synchronize.py", line 96, in __enter__
        return self._semlock.__enter__()
      File "/home/App/anaconda3/lib/python3.5/multiprocessing/queues.py", line 342, in get
        with self._rlock:
      File "/home/App/anaconda3/lib/python3.5/multiprocessing/queues.py", line 342, in get
        with self._rlock:
      File "/home/App/anaconda3/lib/python3.5/multiprocessing/queues.py", line 342, in get
        with self._rlock:
      File "/home/App/anaconda3/lib/python3.5/multiprocessing/synchronize.py", line 96, in __enter__
        return self._semlock.__enter__()
    KeyboardInterrupt
    KeyboardInterrupt
    KeyboardInterrupt
    KeyboardInterrupt
      File "/home/App/anaconda3/lib/python3.5/multiprocessing/synchronize.py", line 96, in __enter__
        return self._semlock.__enter__()
    
      File "/home/App/anaconda3/lib/python3.5/multiprocessing/synchronize.py", line 96, in __enter__
        return self._semlock.__enter__()
    KeyboardInterrupt
    KeyboardInterrupt
    KeyboardInterrupt
      File "/home/App/anaconda3/lib/python3.5/multiprocessing/synchronize.py", line 96, in __enter__
        return self._semlock.__enter__()
    KeyboardInterrupt
      File "/home/App/anaconda3/lib/python3.5/multiprocessing/pool.py", line 108, in worker
        task = get()
      File "/home/App/anaconda3/lib/python3.5/multiprocessing/queues.py", line 342, in get
        with self._rlock:
    Traceback (most recent call last):
    KeyboardInterrupt
      File "/home/App/anaconda3/lib/python3.5/multiprocessing/process.py", line 254, in _bootstrap
        self.run()
      File "/home/App/anaconda3/lib/python3.5/multiprocessing/synchronize.py", line 96, in __enter__
        return self._semlock.__enter__()
    KeyboardInterrupt
      File "/home/App/anaconda3/lib/python3.5/multiprocessing/process.py", line 93, in run
        self._target(*self._args, **self._kwargs)
      File "/home/App/anaconda3/lib/python3.5/multiprocessing/pool.py", line 108, in worker
        task = get()
      File "/home/App/anaconda3/lib/python3.5/multiprocessing/queues.py", line 343, in get
        res = self._reader.recv_bytes()
      File "/home/App/anaconda3/lib/python3.5/multiprocessing/connection.py", line 216, in recv_bytes
        buf = self._recv_bytes(maxlength)
      File "/home/App/anaconda3/lib/python3.5/multiprocessing/connection.py", line 407, in _recv_bytes
        buf = self._recv(4)
      File "/home/App/anaconda3/lib/python3.5/multiprocessing/connection.py", line 379, in _recv
        chunk = read(handle, remaining)
    KeyboardInterrupt
    
    ---------------------------------------------------------------------------
    KeyboardInterrupt                         Traceback (most recent call last)
    <ipython-input-7-67c3034909c9> in <module>()
    ----> 1 get_ipython().magic('timeit -n1 -r1 out.compute(get=dask.multiprocessing.get,lock=False)')
    
    /home/App/anaconda3/lib/python3.5/site-packages/IPython/core/interactiveshell.py in magic(self, arg_s)
       2144         magic_name, _, magic_arg_s = arg_s.partition(' ')
       2145         magic_name = magic_name.lstrip(prefilter.ESC_MAGIC)
    -> 2146         return self.run_line_magic(magic_name, magic_arg_s)
       2147 
       2148     #-------------------------------------------------------------------------
    
    /home/App/anaconda3/lib/python3.5/site-packages/IPython/core/interactiveshell.py in run_line_magic(self, magic_name, line)
       2065                 kwargs['local_ns'] = sys._getframe(stack_depth).f_locals
       2066             with self.builtin_trap:
    -> 2067                 result = fn(*args,**kwargs)
       2068             return result
       2069 
    
    <decorator-gen-61> in timeit(self, line, cell)
    
    /home/App/anaconda3/lib/python3.5/site-packages/IPython/core/magic.py in <lambda>(f, *a, **k)
        185     # but it's overkill for just that one bit of state.
        186     def magic_deco(arg):
    --> 187         call = lambda f, *a, **k: f(*a, **k)
        188 
        189         if callable(arg):
    
    /home/App/anaconda3/lib/python3.5/site-packages/IPython/core/magics/execution.py in timeit(self, line, cell)
       1082                     break
       1083 
    -> 1084         all_runs = timer.repeat(repeat, number)
       1085         best = min(all_runs) / number
       1086         worst = max(all_runs) / number
    
    /home/App/anaconda3/lib/python3.5/timeit.py in repeat(self, repeat, number)
        204         r = []
        205         for i in range(repeat):
    --> 206             t = self.timeit(number)
        207             r.append(t)
        208         return r
    
    /home/App/anaconda3/lib/python3.5/site-packages/IPython/core/magics/execution.py in timeit(self, number)
        158         gc.disable()
        159         try:
    --> 160             timing = self.inner(it, self.timer)
        161         finally:
        162             if gcold:
    
    <magic-timeit> in inner(_it, _timer)
    
    /home/App/anaconda3/lib/python3.5/site-packages/dask/base.py in compute(self, **kwargs)
         96             Extra keywords to forward to the scheduler ``get`` function.
         97         """
    ---> 98         (result,) = compute(self, traverse=False, **kwargs)
         99         return result
        100 
    
    /home/App/anaconda3/lib/python3.5/site-packages/dask/base.py in compute(*args, **kwargs)
        203     dsk = collections_to_dsk(variables, optimize_graph, **kwargs)
        204     keys = [var._keys() for var in variables]
    --> 205     results = get(dsk, keys, **kwargs)
        206 
        207     results_iter = iter(results)
    
    /home/App/anaconda3/lib/python3.5/site-packages/dask/multiprocessing.py in get(dsk, keys, num_workers, func_loads, func_dumps, optimize_graph, **kwargs)
        175                            get_id=_process_get_id, dumps=dumps, loads=loads,
        176                            pack_exception=pack_exception,
    --> 177                            raise_exception=reraise, **kwargs)
        178     finally:
        179         if cleanup:
    
    /home/App/anaconda3/lib/python3.5/site-packages/dask/local.py in get_async(apply_async, num_workers, dsk, result, cache, get_id, rerun_exceptions_locally, pack_exception, raise_exception, callbacks, dumps, loads, **kwargs)
        510             # Main loop, wait on tasks to finish, insert new ones
        511             while state['waiting'] or state['ready'] or state['running']:
    --> 512                 key, res_info, failed = queue_get(queue)
        513                 if failed:
        514                     exc, tb = loads(res_info)
    
    /home/App/anaconda3/lib/python3.5/site-packages/dask/local.py in queue_get(q)
        149 else:
        150     def queue_get(q):
    --> 151         return q.get()
        152 
        153 
    
    /home/App/anaconda3/lib/python3.5/queue.py in get(self, block, timeout)
        162             elif timeout is None:
        163                 while not self._qsize():
    --> 164                     self.not_empty.wait()
        165             elif timeout < 0:
        166                 raise ValueError("'timeout' must be a non-negative number")
    
    /home/App/anaconda3/lib/python3.5/threading.py in wait(self, timeout)
        291         try:    # restore state no matter what (e.g., KeyboardInterrupt)
        292             if timeout is None:
    --> 293                 waiter.acquire()
        294                 gotit = True
        295             else:
    
    KeyboardInterrupt: 
    
    
    
  5. mrocklin commented on Aug 12, 2017

    @mrocklin
    Member

    I don't know why multiprocessing is failing for you. I'm not surprised that it's slower or that the distributed scheduler is choosing to use only one core. Moving data between processes can be expensive.

  6. mrocklin commented on Aug 12, 2017

    @mrocklin
    Member

    Also you start out with all of your data on a single block. You might consider using the dask random functions rather than from_array. Or you might consider persisting the array and then rebalancing explicitly with client.rebalance.

  7. amjhou commented on Aug 12, 2017

    @amjhou
    Author

    Thanks. It seems that the size of the data is too big for multiprocessing to move between processes. Not sure whether it relates to the explanation here (https://sopython.com/canon/82/programs-using-multiprocessing-hang-deadlock-and-never-complete/). I tried the following.

    out2 = out.persist()
    client.rebalance(out2)
    

    It still seems to only use one worker. Any idea? I suppose rebalance will trigger the computation.

  8. amjhou commented on Aug 12, 2017

    @amjhou
    Author

    I guess the solution you mentioned won't be able to spread chunks of one dataset to different workers. Maybe I should divide the dataset into many pieces first?

  9. TomAugspurger commented on Aug 14, 2017

    @TomAugspurger
    Member

    @jierice I'd recommend reading through http://dask.pydata.org/en/latest/scheduler-choice.html an d http://distributed.readthedocs.io/en/latest/efficiency.html. And just to make sure it's entirely clear, the line

    daskdata = da.from_array(np.random.rand(1000,1000,1000),chunks=(300,350,1000))

    Is expensive. It first creates a 1000 x 1000 x 1000 NumPy array in memory. If you try to use either multiprocessing scheduler or the distributed scheduler, each processes needs it's own copy of the NumPy array, blowing up your memory. It'd be better to use the da.random methods, to avoid creating the large NumPy array.

    daskdata = da.random.uniform(size=(1000,1000,1000), chunks=(300,350,1000))

    This alone may fix your issue.

  10. amjhou commented on Aug 14, 2017

    @amjhou
    Author

    @TomAugspurger Thanks. The da.random does solve the problem. But this is a minimal example I generate. In my real case, I have a pretty big array in memory. I am trying to use multiprocessing for that. As you explained, this might not be a good use case for multiprocessing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions