Skip to content

Commit 9aec259

Browse files
committed
added first pass of dask OOD tutorial
1 parent 815de9e commit 9aec259

6 files changed

Lines changed: 116 additions & 1 deletion

File tree

docs/hpc/09_ood/13_TeachOpenCADD.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ You can select the number cores, amount of memory, gpu type (if any), optional S
2121
If you select to use `/home` as your root directory be careful not to go over your quota. You can find your current usage with the `myquota` command. Please see our [Storage documentation](../03_storage/01_intro_and_data_management.mdx) for details about your storage options.
2222
:::
2323

24-
## Spark Standalone Cluster with Jupyter Notebook running in OOD
24+
## TeachOpenCADD with Jupyter Notebook running in OOD
2525

2626
After you hit the `Launch` button you'll have to wait for the scheduler to find node(s)for you to run on:
2727
![OOD TeachOpenCADD in queue](./static/ood_teachopencadd_in_queue.png)
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
# Dask in Jupyter Notebook in OOD
2+
3+
[Dask](https://docs.dask.org/en/stable/) is a Python library for parallel and distributed computing.
4+
5+
## Getting Started
6+
You can run Dask in a Jupyter Notebook in OOD by going to the URL [ood.hpc.nyu.edu](http://ood.hpc.nyu.edu) in your browser and selecting `DS-GA.1004 - Jupyter Dask` from the `Interactive Apps` pull-down menu at the top of the page. As you can see below, once you've used it and other interactive apps they'll show up on your home screen under the `Recently Used Apps` header.
7+
8+
:::note
9+
Be aware that when you start from `Recently Used Apps` it will start with the same configuration that you used previously. If you'd like to configure your Dask session differently, you'll need to select it from the menu.
10+
:::
11+
12+
![OOD Interactive Apps menu](./static/ood_interactive_apps_dask.png)
13+
14+
## Configuration
15+
16+
You can select the Dask version, number of cores, amount of memory, root directory, number of hours, and optional Slurm options.
17+
18+
![OOD Dask Configuration](./static/ood_dask_config.png)
19+
20+
:::warning
21+
If you select to use `/home` as your root directory be careful not to go over your quota. You can find your current usage with the `myquota` command. Please see our [Storage documentation](../03_storage/01_intro_and_data_management.mdx) for details about your storage options.
22+
:::
23+
24+
## Dask with Jupyter Notebook running in OOD
25+
26+
After you hit the `Launch` button you'll have to wait for the scheduler to find node(s)for you to run on:
27+
![OOD Dask in queue](./static/ood_dask_in_queue.png)
28+
29+
Then you'll have a short wait for Spark itself to start up.<br />
30+
Once that happens you'll get one last page that will give you links to:
31+
- open a terminal window on the compute node your Spark session is running on
32+
- go to the directory associated with your Session ID that stores output, config and other related files for your session
33+
34+
![Pre-launch Dask OOD](./static/ood_dask_prelaunch.png)
35+
36+
Please click the `Connect to Jupyter` button and a Jupyter window will open.
37+
38+
## Dask Example
39+
40+
Start a new Jupyter notebook with 4 cores, 16GB memory, and set your root directory to `/scratch`. Enter the following code in the first cell and execute it by pressing the `Shift` and `Enter` keys at the same time.
41+
```python
42+
import os
43+
import pandas as pd
44+
import numpy as np
45+
import time
46+
47+
# Create a directory for the large files
48+
output_dir = "tmp/large_data_files"
49+
os.makedirs(output_dir, exist_ok=True)
50+
51+
num_files = 5 # Number of files to create
52+
rows_per_file = 10_000_000 # 10 million rows per file
53+
for i in range(num_files):
54+
data = {
55+
'col1': np.random.randint(0, 100, size=rows_per_file),
56+
'value': np.random.rand(rows_per_file) * 100
57+
}
58+
df = pd.DataFrame(data)
59+
df.to_csv(os.path.join(output_dir, f'data_{i}.csv'), index=False)
60+
print(f"{num_files} large CSV files created in '{output_dir}'.")
61+
62+
import dask.dataframe as dd
63+
from dask.distributed import Client
64+
import time
65+
import os
66+
67+
# Start a Dask client for distributed processing (optional but recommended)
68+
# This allows you to monitor the computation with the Dask dashboard
69+
client = Client(n_workers=4, threads_per_worker=2, memory_limit='16GB') # Adjust these as per your system resources
70+
print(client)
71+
72+
# Load multiple CSV files into a Dask DataFrame
73+
# Dask will automatically partition and parallelize the reading of these files
74+
output_dir = '/scratch/rjy1/tmp/large_data_files'
75+
dask_df = dd.read_csv(os.path.join(output_dir, 'data_*.csv'))
76+
77+
# Perform a calculation (e.g., calculate the mean of the 'value' column)
78+
# This operation will be parallelized across the available workers
79+
result_dask = dask_df['value'].mean()
80+
81+
# Trigger the computation and measure the time
82+
start_time = time.time()
83+
computed_result_dask = result_dask.compute()
84+
end_time = time.time()
85+
86+
print(f"Dask took {end_time - start_time} seconds to compute the mean across {num_files} files.")
87+
print(f"Result (Dask): {computed_result_dask}")
88+
89+
import pandas as pd
90+
import time
91+
import os
92+
93+
# Perform the same calculation sequentially with Pandas
94+
start_time_pandas = time.time()
95+
total_mean = 0
96+
total_count = 0
97+
for i in range(num_files):
98+
df = pd.read_csv(os.path.join(output_dir, f'data_{i}.csv'))
99+
total_mean += df['value'].sum()
100+
total_count += len(df)
101+
computed_result_pandas = total_mean / total_count
102+
end_time_pandas = time.time()
103+
104+
print(f"Pandas took {end_time_pandas - start_time_pandas} seconds to compute the mean across {num_files} files.")
105+
print(f"Result (Pandas): {computed_result_pandas}")
106+
```
107+
You should get output like:
108+
```
109+
5 large CSV files created in 'tmp/large_data_files'.
110+
<Client: 'tcp://127.0.0.1:45511' processes=4 threads=8, memory=59.60 GiB>
111+
Dask took 3.448112726211548 seconds to compute the mean across 5 files.
112+
Result (Dask): 50.010815178612596
113+
Pandas took 9.641847610473633 seconds to compute the mean across 5 files.
114+
Result (Pandas): 50.01081517861258
115+
```
290 KB
Loading
132 KB
Loading
132 KB
Loading
204 KB
Loading

0 commit comments

Comments
 (0)