Running DataFrame Partition Simulations in Parallel using dask.delayed()


I have a newbie query regarding running simulations in parallel using Dask.

I want to…

  • Read multiple JSON files into a Dask DataFrame

  • For each dataframe partition, run multiple simulations using dask.delayed()

  • Return the results as lists of lists for further processing.

My Jupyter Lab dummy code below seems to work well. dask.compute(combined_simualtions) produces the correct results.

My query relates to the output of dask.visualize(), below. What are the finalize cells in visualisation? And why are there 2 square output blocks before the finalize? They make me wonder whether I’m parallelising my code correctly. Also, I’m expecting to see a cell for the partition_simulations() function, but there isn’t one in the visualisation.

Any suggestions/guidance would be much appreciated.

Kind regards

import dask
import dask.dataframe as dd
ddf = dd.read_json('*.json')
def simulation(df, i):
    values = []
    for (value,) in df.itertuples(index=None, name=None): # Simulation is sequential in nature.
        values.append(value * i)
    return values
def partition_simulations(df, n):
    sims = []
    for i in range(n):
        sims.append(dask.delayed(simulation)(df, i))
    return sims
# Conduct N simulations for each partition in the dataframe.

N = 5

combined_simulations = [partition_simulations(df, N) for df in ddf.partitions]

Hi @JohnDuffy, welcome to Dask community!

I’ve got to admit that I don’t know. Probably some cause of your iteration into the DataFrame partitions directly. I think it would be better to use DataFrame.to_delayed.

The first square is when you end up building the Dataframe, with two chunks. Second square is because you take partitions one by one next.

This function is is just building a task graph, it is not used by any Dask Collection, so Dask doesn’t know about it. Dask just knows that it has to run simulation function.

Thank you Guillaume

Your comments make sense. I will experiment with DataFrame.to_delayed, as you suggest.

Kind regards


1 Like