Using dask.distributed as Parallel Pool Example
Using dask.distributed
as Parallel Pool Example#
This example shows how you can use any parallel pool-executor like object to run combos - specifally the dask.distributed.Client.
[1]:
import xyzpy as xyz
import numpy as np
from dask.distributed import Client
First we instantiate the client:
[2]:
address = None # put in an actual address like '127.0.0.1:8786' if you have a scheduler running
c = Client(address=address)
c
[2]:
Client
|
Cluster
|
The Client
instance can act like a normal parallel pool since it has a submit method and returns futures:
[3]:
c.submit
[3]:
<bound method Client.submit of <Client: 'tcp://127.0.0.1:43127' processes=4 threads=8, memory=16.49 GB>>
Let’s define our test function and combos
:
[4]:
def rad(x, y, n):
r = (x**n + y**n)**(1 / n)
if r == 0.0:
return 1
return np.sin(r) / r
combos = {
'x': np.linspace(-15, 15, 51),
'y': np.linspace(-15, 15, 51),
'n': [2, 4, 6],
}
r = xyz.Runner(rad, var_names='sinc2d')
Now we can run (or harvest
, or grow_missing
) our combos, supplying the Client
instance to the executor=
keyword:
[5]:
r.run_combos(combos, executor=c)
100%|##########| 7803/7803 [00:11<00:00, 686.28it/s]
[5]:
<xarray.Dataset> Dimensions: (n: 3, x: 51, y: 51) Coordinates: * x (x) float64 -15.0 -14.4 -13.8 -13.2 -12.6 ... 13.2 13.8 14.4 15.0 * y (y) float64 -15.0 -14.4 -13.8 -13.2 -12.6 ... 13.2 13.8 14.4 15.0 * n (n) int64 2 4 6 Data variables: sinc2d (x, y, n) float64 0.03308 -0.04752 -0.05369 ... -0.04752 -0.05369
xarray.Dataset
- n: 3
- x: 51
- y: 51
- x(x)float64-15.0 -14.4 -13.8 ... 14.4 15.0
array([-15. , -14.4, -13.8, -13.2, -12.6, -12. , -11.4, -10.8, -10.2, -9.6, -9. , -8.4, -7.8, -7.2, -6.6, -6. , -5.4, -4.8, -4.2, -3.6, -3. , -2.4, -1.8, -1.2, -0.6, 0. , 0.6, 1.2, 1.8, 2.4, 3. , 3.6, 4.2, 4.8, 5.4, 6. , 6.6, 7.2, 7.8, 8.4, 9. , 9.6, 10.2, 10.8, 11.4, 12. , 12.6, 13.2, 13.8, 14.4, 15. ])
- y(y)float64-15.0 -14.4 -13.8 ... 14.4 15.0
array([-15. , -14.4, -13.8, -13.2, -12.6, -12. , -11.4, -10.8, -10.2, -9.6, -9. , -8.4, -7.8, -7.2, -6.6, -6. , -5.4, -4.8, -4.2, -3.6, -3. , -2.4, -1.8, -1.2, -0.6, 0. , 0.6, 1.2, 1.8, 2.4, 3. , 3.6, 4.2, 4.8, 5.4, 6. , 6.6, 7.2, 7.8, 8.4, 9. , 9.6, 10.2, 10.8, 11.4, 12. , 12.6, 13.2, 13.8, 14.4, 15. ])
- n(n)int642 4 6
array([2, 4, 6])
- sinc2d(x, y, n)float640.03308 -0.04752 ... -0.05369
array([[[ 0.03308398, -0.04751635, -0.05368978], [ 0.04478704, -0.05587021, -0.0438239 ], [ 0.04902664, -0.05789407, -0.03087697], ..., [ 0.04902664, -0.05789407, -0.03087697], [ 0.04478704, -0.05587021, -0.0438239 ], [ 0.03308398, -0.04751635, -0.05368978]], [[ 0.04478704, -0.05587021, -0.0438239 ], [ 0.04902855, -0.05770291, -0.02721588], [ 0.0445776 , -0.05231428, -0.00859576], ..., [ 0.0445776 , -0.05231428, -0.00859576], [ 0.04902855, -0.05770291, -0.02721588], [ 0.04478704, -0.05587021, -0.0438239 ]], [[ 0.04902664, -0.05789407, -0.03087697], [ 0.0445776 , -0.05231428, -0.00859576], [ 0.031682 , -0.03939915, 0.01396159], ..., ... ..., [ 0.031682 , -0.03939915, 0.01396159], [ 0.0445776 , -0.05231428, -0.00859576], [ 0.04902664, -0.05789407, -0.03087697]], [[ 0.04478704, -0.05587021, -0.0438239 ], [ 0.04902855, -0.05770291, -0.02721588], [ 0.0445776 , -0.05231428, -0.00859576], ..., [ 0.0445776 , -0.05231428, -0.00859576], [ 0.04902855, -0.05770291, -0.02721588], [ 0.04478704, -0.05587021, -0.0438239 ]], [[ 0.03308398, -0.04751635, -0.05368978], [ 0.04478704, -0.05587021, -0.0438239 ], [ 0.04902664, -0.05789407, -0.03087697], ..., [ 0.04902664, -0.05789407, -0.03087697], [ 0.04478704, -0.05587021, -0.0438239 ], [ 0.03308398, -0.04751635, -0.05368978]]])
That should take enough time to check out client status including the tasks being processed on for example: http://127.0.0.1:8787/tasks.
Finally, visualize the output:
[6]:
r.last_ds.xyz.iheatmap('x', 'y', 'sinc2d', col='n')