Issue
I am trying to understand how dask.foldby works. Consider the following code.
tasks = [{"task_group": i // 10, "numbers": list(range(i, i + 10))} for i in range(1, 1000, 10)]
tb = db.from_sequence(tasks)
I create a dask bag with 100 items.
def aggregate_task(task):
return np.array([sum(task['numbers'])] * 10000) # A relatively big result
def add(x, y):
x = aggregate_task(x) if isinstance(x, dict) else x
y = aggregate_task(y) if isinstance(y, dict) else y
return x + y
res = tb.foldby(lambda task: task['task_group'] % 5, add, split_every=25)
I then fold the items by a certain grouping function, to calculate 5 sums.
res.compute()
[(0, array([96100, 96100, 96100, ..., 96100, 96100, 96100])),
(1, array([98100, 98100, 98100, ..., 98100, 98100, 98100])),
(2, array([100100, 100100, 100100, ..., 100100, 100100, 100100])),
(3, array([102100, 102100, 102100, ..., 102100, 102100, 102100])),
(4, array([104100, 104100, 104100, ..., 104100, 104100, 104100]))]
When I look at the res task graph, I see this:

It seems like the result of folding is stored in a single worker / single partition (I am not too sure that 1 partition = 1 worker). I can confirm that res has only one partition.
>> res.npartitions
1
My question is: Is it possible to ask dask to hold the result of each foldby group in a separate partition (and hence separate workers)? The reason I ask is that, each of the resultant array is massive and I would like to keep them at different workers for memory reason. It will also make it possible to write those results to different files in disk in parallel.
Solution
With the current API, I don't think this is possible to do in one go. One hack/work-around is to create filtered bags:
bag1 = tb.filter(lambda task: task['task_group'].isin([1,2,3]))
bag2 = tb.filter(lambda task: task['task_group'].isin([4,5]))
The code then proceeds to apply the .foldby on the filtered bags.
Answered By - SultanOrazbayev Answer Checked By - Candace Johnson (sql.easybugfix Volunteer)
Post a Comment
Post a Comment