Repository navigation
Description
There are currently a few ways to construct highly dynamic workloads, where the graph can change during computation. This includes operations like get_client(), calling dask.compute within a task, using futures and as_completed, and so on.
Sometimes these workloads can grow complex and difficult to reason about (see #1424). Are there better programming interfaces to present to users that still cover the same options, but perhaps guide users to correct behavior.
Asynchronous projects like Tornado/Asyncio/Curio/Trio seem to prefer a coroutine-style approach. Is this a possible option for a distributed runtime like Dask? If so what would it look like?
Here are a couple of toy problems that come up frequently and naive thoughts on how they might look as coroutines
Fibonacci
@dask.coroutine
def fib(i):
if i < 2:
return i
else:
a, b = yield [fib(i - 1), fib(i - 2)]
return a + bEvaluating on a remote list of unknown size
@dask.coroutine
def generate_data():
return list(range(random.randint(0, 10))) # a list of data of unknown length
@dask.coroutine
def inc(x):
return x + 1
@dask.coroutine
def my_len(L):
return len(L)
@dask.coroutine
def my_sum(L):
return sum(L)
@dask.coroutine
def process_all():
L = generate_data()
n = yield my_len(L)
processed = [inc(L[i]) for i in range(n)]
total = yield sum(processed)
return totalThere are problems with both examples. They also don't represent the full space of complexity that existing solutions can cover. Broad thoughts on this topic are welcome.
Activity
asford commentedon Dec 29, 2017
edit On rereading, it seems like this idea belongs in #1424. Happy to move it there if you all would prefer.
edit Clarify use of
worker_clientrather thanget_client.TLDR
Introducing promises into the dask scheduler, akin to c++'s promise/future interface, could provide the ability to "rewrite" the task graph below a promise and would be an extremely useful feature when scaling with dynamic tasks.
I've run into a potentially related issue while implementing a CPU-bound distributed scatter-gather style operation in dask using the
get_clientworker_clientfeature. I hope the write up below is useful, though it isn't directly related to the coroutine feature you've proposed.The workload I have is relatively similar to the "list of unknown size problem" you've described above, in that a single "logical task" involves:
len)inc)sum)For clarity, this is roughly implemented as:
In the current dask implementatation, the task graph is composed entirely of
futures and the
subprocesstask thread (or coroutine) serves as a long-lived representationof the "expanded" future in the task graph. The root
subprocesstask is marked asin-progress while a new computational graph of
subtaskis assembled, submittedto the scheduler, and then gathered from the distributed system.
I initialy implemented this via
get_clientworker_clientbut found that this caused majorscaling issues when processing large number of "logical tasks", as each logical
task requires a long-lived client connection to the core scheduler
for the duration of the
get_clientworker_clientcontext. This quickly overloads the schedulerwhen there are a large number of potentially-concurrent logical tasks to execute.
Introducing the concept of a promise in the dask graph could be used to more
effectively describe this distributed computation. In this context, a promise
would be a node within the task graph on which tasks may depend, but who's
dependencies may vary. The execution of tasks would then involve temporary creation
a worker client, dynamic initialization of the subgraph, and then an update of the
promise's dependencies to point at the new subgraph. The promise reference would
then function similarly to a persistent client reference, triggering execution of
the subgraph and fulfillment of the promise when it's current dependencies finish.
The API might look like:
Execution of "logical_task" would result in several graph states:
mrocklin commentedon Dec 29, 2017
Can you expand on how the schedule became overwhelmed? Are you familiar with secede/rejoin? http://dask.pydata.org/en/latest/futures.html#submit-tasks-from-tasks
asford commentedon Dec 29, 2017
Yes. Sorry to muddle the example but I used the
worker_clientcontext managerto handle the long-running tasks. I've updated my previous comment to reflect this.
I'll be first to admit it was a
terriblenaive setup, but I developed thisissue while executing a group of 1-2 thousand of these "logical tasks" on a
cluster of ~256 single-process workers. When running via
worker_clientthecluster ground to a halt as long-running tasks accumulated, and I was eventually
unable to establish connections to the scheduler. My assumption, from looking
through the logs, was that the scheduler node was being overwhelming by the
number of client connections, but I didn't directly verify that fact and may
be incorrect.
I've resolved this issue in the short term by:
can be fully defined during setup. However, this is non-ideal in long-tail cases
where the initial search results in a very large number of subtasks.
execution, which dramatically reduces the number worker connections.
Unfortunately solution (1) isn't particularly workable in the long term, as the
end implementation for this task will likely require a branched search strategy
in which sub tasks may require recursive expansion to an arbitrary depth.
mrocklin commentedon Dec 29, 2017
In theory the number of long-running connections shouldn't have any impact on the number of actual connections to the scheduler. They're all sharing the same single connection. There is a limit on the worker side, each long-running task occupies a thread, but you should be ok with up to thousands of these per worker.
asford commentedon Dec 29, 2017
That makes sense. I suspect I may have then been seeing a combination of effects due to the pathological use of hundreds of workers along with worker clients. Would opening the first client on the worker result in establishing an additional connection to the scheduler, in comparison to a worker with no clients?
mrocklin commentedon Dec 29, 2017
Yes, each additional client would open up a couple of new connections. This might push you over the 1024 open file limit that is default on most linux distros. You might want to watch the
system/page in the dashboard to watch the number of open file descriptors to verify. You could also up your open file limit (there are good instructions online for this)asford commentedon Dec 29, 2017
Yes, as I vaguely alluded to above, the primary solution to this problem was
reworking how our compiled extension modules handled locking to enable
multithreaded workers. This, along with the
ulimitmodifications you recommend,resolved most of these issues.
On the original topic, coroutines, I'd be very much +1. This would be an
elegant way to reduce the effective complexity of the current succession-based
long-running task system, and it closely matches existing python async patterns.
I would imagine a system in which dask coroutines are executed on the worker
thread pool, but can yield
compute-able objects to an existing asyncClient.
If I could, slighly, update your example to illustrate this point:
One concern with worker-based coroutines and long-running tasks is that they
introduce implicit dependencies between tasks that are only captured in the
state of the coroutine. For example, the
process_alltask result depends onon the
generate_data->count->inc->sumtask graph, but the scheduler isunaware of this dependency, making the worker a single point of failure for
this task execution. If the worker executing a given coroutine is lost
then the subgraph is invalidated and must be recomputed.
adamklein commentedon Dec 29, 2017
@mrocklin Thanks for posting your thoughts on the coroutine-style approach. It definitely makes sense from an ease-of-use perspective.
Regarding implementation possibilities ... would the
@dask.coroutineannotation automatically secede the worker thread, as in using aworker_clientcontext manager? Or, would it perhaps be able to follow a true coroutine approach without additional threads? E.g., one problem I've run into is needing to be careful with tasks that are not thread-safe: I wouldn't want a single-threaded worker pool to execute anything else while the@dask.coroutineis executing until actually hitting theyieldstatement.Too bad a partially-evaluated coroutine is impossible to serialize and resume elsewhere. If we were using, e.g., stackless python, then you could even do the following: given a task A that has launched tasks B and C, you could capture the dependency of the continuation of A on B & C and reschedule it so that it respects data locality. (Too bad we live in the real world ...) Failing that, it would seem quite useful to follow a local-first scheduling policy whereby B&C are scheduled on the same worker as A because we know A will consume their results. Then of course the scheduler might relocate B&C. However, perhaps then there may even be a way to inform the scheduler not to move a task (if, e.g., you know it's going to generate large results). This is motivated by some of the same workflows that Ray seems geared toward (https://github.com/ray-project/ray). Not having played with it yet, it's not clear to me how they achieve their yield/resume semantics with tasks generating and awaiting other tasks.
mrocklin commentedon Dec 29, 2017
I think that that's the kind of question that we would hammer out here. Thanks for bringing it up.
There seem to be a few options here (some of which you mention)
We secede when we yield and rejoin when we come back, leaving an active thread waiting out of the main thread pool.
My guess is that 1 or 3 is more likely, with the decision being made by how much people care about having extra threads hanging around.
Yeah, my guess is that this would be tricky to do in general. We could ask some of the cloudpickle maintainers. They might have thoughts on how to serialize ongoing generators. cc @llllllllll
This would be a bigger thing to accomplish, but is generally in scope. There is nothing stopping us from making workers more clever and doing their own local task scheduling work, kicking tasks up to the global scheduler in times of need. This would require time and active feedback to get right, but certainly seems possible.
llllllllll commentedon Jan 2, 2018
I have an idea for CPython, but I don't know if you are going to like it. I can push a prototype later tonight.
llllllllll commentedon Jan 3, 2018
Here is my "prototype": https://github.com/llllllllll/cloudpickle-generators