If I try to parallelize a for loop with dask, it ends up executing slower than the regular version. Basically, I just follow the introductory example from the dask tutorial, but for some reason it's failing on my end. What am I doing wrong?
In [1]: import numpy as np
...: from dask import delayed, compute
...: import dask.multiprocessing
In [2]: a10e4 = np.random.rand(10000, 11).astype(np.float16)
...: b10e4 = np.random.rand(10000, 11).astype(np.float16)
In [3]: def subtract(a, b):
...: return a - b
In [4]: %%timeit
...: results = [subtract(a10e4, b10e4[index]) for index in range(len(b10e4))]
1 loop, best of 3: 10.6 s per loop
In [5]: %%timeit
...: values = [delayed(subtract)(a10e4, b10e4[index]) for index in range(len(b10e4)) ]
...: resultsDask = compute(*values, get=dask.multiprocessing.get)
1 loop, best of 3: 14.4 s per loop
In your example, dask is slower than python multiprocessing, because you don't specify the scheduler, so dask uses the multithreading backend, which is the default. As mdurant has pointed out, your code does not release the GIL, therefore multithreading cannot execute the task graph in parallel.
The Dask delayed function decorates your functions so that they operate lazily. Rather than executing your function immediately, it will defer execution, placing the function and its arguments into a task graph. delayed ([obj, name, pure, nout, traverse]) Wraps a function or object to produce a Delayed .
dask. bag uses the multiprocessing scheduler by default.
Two issues:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With