| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
from random import randint |
| 4 |
from time import time |
| 5 |
|
| 6 |
import matplotlib.pyplot as plt |
| 7 |
|
| 8 |
import dask |
| 9 |
from dask import local, multiprocessing, threaded |
| 10 |
|
| 11 |
|
| 12 |
def noop(x): |
| 13 |
pass |
| 14 |
|
| 15 |
|
| 16 |
nrepetitions = 1 |
| 17 |
|
| 18 |
|
| 19 |
def trivial(width, height): |
| 20 |
"""Embarrassingly parallel dask""" |
| 21 |
d = {("x", 0, i): i for i in range(width)} |
| 22 |
for j in range(1, height): |
| 23 |
d.update({("x", j, i): (noop, ("x", j - 1, i)) for i in range(width)}) |
| 24 |
return d, [("x", height - 1, i) for i in range(width)] |
| 25 |
|
| 26 |
|
| 27 |
def crosstalk(width, height, connections): |
| 28 |
"""Natural looking dask with some inter-connections""" |
| 29 |
d = {("x", 0, i): i for i in range(width)} |
| 30 |
for j in range(1, height): |
| 31 |
d.update( |
| 32 |
{ |
| 33 |
("x", j, i): ( |
| 34 |
noop, |
| 35 |
[("x", j - 1, randint(0, width)) for _ in range(connections)], |
| 36 |
) |
| 37 |
for i in range(width) |
| 38 |
} |
| 39 |
) |
| 40 |
return d, [("x", height - 1, i) for i in range(width)] |
| 41 |
|
| 42 |
|
| 43 |
def dense(width, height): |
| 44 |
"""Full barriers between each step""" |
| 45 |
d = {("x", 0, i): i for i in range(width)} |
| 46 |
for j in range(1, height): |
| 47 |
d.update( |
| 48 |
{ |
| 49 |
("x", j, i): (noop, [("x", j - 1, k) for k in range(width)]) |
| 50 |
for i in range(width) |
| 51 |
} |
| 52 |
) |
| 53 |
return d, [("x", height - 1, i) for i in range(width)] |
| 54 |
|
| 55 |
|
| 56 |
import numpy as np |
| 57 |
|
| 58 |
x = np.logspace(0, 4, 10) |
| 59 |
trivial_results = dict() |
| 60 |
for get in (dask.get, threaded.get, local.get_sync, multiprocessing.get): |
| 61 |
y = list() |
| 62 |
for n in x: |
| 63 |
dsk, keys = trivial(int(n), 5) |
| 64 |
start = time() |
| 65 |
get(dsk, keys) |
| 66 |
end = time() |
| 67 |
y.append(end - start) |
| 68 |
trivial_results[get] = np.array(y) |
| 69 |
|
| 70 |
|
| 71 |
######## |
| 72 |
# Plot # |
| 73 |
######## |
| 74 |
|
| 75 |
f, (left, right) = plt.subplots( |
| 76 |
nrows=1, ncols=2, sharex=True, figsize=(12, 5), squeeze=True |
| 77 |
) |
| 78 |
|
| 79 |
for get in trivial_results: |
| 80 |
left.loglog(x * 5, trivial_results[get], label=get.__module__) |
| 81 |
right.loglog(x * 5, trivial_results[get] / x, label=get.__module__) |
| 82 |
|
| 83 |
left.set_title("Cost for Entire graph") |
| 84 |
right.set_title("Cost per task") |
| 85 |
left.set_ylabel("Duration (s)") |
| 86 |
right.set_ylabel("Duration (s)") |
| 87 |
left.set_xlabel("Number of tasks") |
| 88 |
right.set_xlabel("Number of tasks") |
| 89 |
|
| 90 |
plt.legend() |
| 91 |
plt.savefig("images/scaling-nodes.png") |
| 92 |
|
| 93 |
##################### |
| 94 |
# Crosstalk example # |
| 95 |
##################### |
| 96 |
|
| 97 |
x = np.linspace(1, 100, 10) |
| 98 |
crosstalk_results = dict() |
| 99 |
for get in [threaded.get, local.get_sync]: # type: ignore[assignment] |
| 100 |
y = list() |
| 101 |
for n in x: |
| 102 |
dsk, keys = crosstalk(1000, 5, int(n)) |
| 103 |
start = time() |
| 104 |
get(dsk, keys) |
| 105 |
end = time() |
| 106 |
y.append(end - start) |
| 107 |
crosstalk_results[get] = np.array(y) |
| 108 |
|
| 109 |
######## |
| 110 |
# Plot # |
| 111 |
######## |
| 112 |
|
| 113 |
f, (left, right) = plt.subplots( |
| 114 |
nrows=1, ncols=2, sharex=True, figsize=(12, 5), squeeze=True |
| 115 |
) |
| 116 |
|
| 117 |
for get in crosstalk_results: |
| 118 |
left.plot(x, crosstalk_results[get], label=get.__module__) |
| 119 |
right.semilogy(x, crosstalk_results[get] / 5000.0 / x, label=get.__module__) |
| 120 |
|
| 121 |
left.set_title("Cost for Entire graph") |
| 122 |
right.set_title("Cost per edge") |
| 123 |
left.set_ylabel("Duration (s)") |
| 124 |
right.set_ylabel("Duration (s)") |
| 125 |
left.set_xlabel("Number of edges per task") |
| 126 |
right.set_xlabel("Number of edges per task") |
| 127 |
plt.legend() |
| 128 |
plt.savefig("images/scaling-edges.png") |