Skip to content

bug: dask_worker runs forever using multiple threads per process #5132

Closed

Description

@llodds

I have been learning dask recently and want to use dask and numba to parallelize python program like how MPI and OpenMP parallelize C++ program.

Here is my dummy dask program:

from numba import njit
import numpy as np

mean = np.random.uniform(-1, 1, size=1000000)
width = np.random.uniform(0.1, 0.3, size=1000000)

@njit(parallel=True)
def gaussians(x, mean, width):
    n = mean.shape[0]
    result = np.exp( -0.5 * ((x - mean) / width)**2 ) / width
    return result/n

futures = []
for i in range(100):
    futures.append(client.submit(gaussians, i, mean, width))
y = 0
for future in as_completed(futures):
    y += future.result()

I use a SGECluster to submit dask jobs. If I specifiy processes=6, cores=12, i.e., forking 6 processes for a worker and 2 threads per process:

distributed.cli.dask_worker tcp://10.74.12.88:39274 --nthreads 2 --nprocs 6 --memory-limit 34.00GB --name dask-worker--${JOB_ID}-- --death-timeout 60

then the above program runs forever. Below is the final frozen task stream image from the dashboard:

Screen Shot 2019-07-22 at 4 16 49 PM

However, if I specifiy processes=6, cores=6, i.e., using 1 thread per process:

distributed.cli.dask_worker tcp://10.74.12.88:39274 --nthreads 1 --nprocs 6 --memory-limit 34.00GB --name dask-worker--${JOB_ID}-- --death-timeout 60

then the above program finishes in seconds. Weird thing is: in this case, dashboard shows that CPU usage is above 100%, i.e., dask-worker are threaded.

Does anyone know why it happens, or whether this is a potential bug? Thanks in advance.

Activity

  1. changed the title [-]bug: program runs forever using multiple threads per dask_worker[/-] [+]bug: dask_worker runs forever using multiple threads per process[/+] on Jul 22, 2019
  2. jcrist commented on Jul 23, 2019

    @jcrist
    Member

    You're using njit with parallel=True, which results in numba using threads behind the scenes. In this case, you're parallelizing on multiple levels (multiple dask workers, each with multiple threads, running numba functions that also use multiple threads). This can oversubscribe your cores, leading to contention and slowdowns, and also uses more CPUs than you intended. I recommend either using single threaded numba or single-threaded dask, and not mixing them. To set the number of threads in numba see http://numba.pydata.org/numba-doc/latest/reference/envvars.html?highlight=numba_default_num_threads#envvar-NUMBA_NUM_THREADS, or just remove the parallel=True in njit.

  3. llodds commented on Jul 24, 2019

    @llodds
    Author

    Thanks for the explanation. I was assuming Numba would pick "nthreads" of the belonging Dask worker and fork exactly "nthreads" threads. It sounds like Numba and Dask use different threading mechanism/layer, and even if a Dask worker uses only one thread, Numba can still fork multiple threads under that Dask thread. Please let me know if I miss any key concept here.

  4. jcrist commented on Jul 24, 2019

    @jcrist
    Member

    It sounds like Numba and Dask use different threading mechanism/layer, and even if a Dask worker uses only one thread, Numba can still fork multiple threads under that Dask thread.

    Yes, that is correct. The standard threading API libraries use doesn't make it easy to share threadpools between different projects (e.g. dask, numba, openblas, etc...). Often if you have parallelism at one level you want to limit parallelism at other levels to prevent contention.

    I believe this question has been answered, closing. Feel free to reopen if you continue to have issues.

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