id,node_id,number,title,user,state,locked,assignee,milestone,comments,created_at,updated_at,closed_at,author_association,active_lock_reason,draft,pull_request,body,reactions,performed_via_github_app,state_reason,repo,type 252358450,MDExOlB1bGxSZXF1ZXN0MTM3Mjc0Nzgx,1517,Automatic parallelization for dask arrays in apply_ufunc,1217238,closed,0,,,22,2017-08-23T17:27:36Z,2017-10-09T23:28:52Z,2017-10-09T23:26:06Z,MEMBER,,0,pydata/xarray/pulls/1517,"This lets you parallelize a function designed for numpy inputs just by adding a few keyword arguments to `apply_ufunc`. Example usage, to calculate rank correlation between two variables (this will probably turn into an example for the docs): ```python import numpy as np import xarray as xr import bottleneck def covariance_gufunc(x, y): return ((x - x.mean(axis=-1, keepdims=True)) * (y - y.mean(axis=-1, keepdims=True))).mean(axis=-1) def correlation_gufunc(x, y): return covariance_gufunc(x, y) / (x.std(axis=-1) * y.std(axis=-1)) def spearman_correlation_gufunc(x, y): x_ranks = bottleneck.rankdata(x, axis=-1) y_ranks = bottleneck.rankdata(y, axis=-1) return correlation_gufunc(x_ranks, y_ranks) def spearman_correlation(x, y, dim): return xr.core.compuation.apply_ufunc( spearman_correlation_gufunc, x, y, input_core_dims=[[dim], [dim]], dask='parallelized', output_dtypes=[float]) ``` We get a nice 5x speedup for this compute bound task: ``` In [56]: rs = np.random.RandomState(0) In [57]: array1 = xr.DataArray(rs.randn(1000, 100000), dims=['place', 'time']) # 800MB In [58]: array2 = array1 + 0.5 * rs.randn(1000, 100000) # using one core, on numpy arrays In [61]: %time _ = spearman_correlation(array1, array2, 'time') CPU times: user 21.6 s, sys: 2.84 s, total: 24.5 s Wall time: 24.9 s # using all my laptop's cores, with dask In [63]: r = spearman_correlation(array1.chunk({'place': 10}), array2.chunk({'place': 10}), 'time') In [64]: %time _ = r.compute() CPU times: user 30.9 s, sys: 1.74 s, total: 32.6 s Wall time: 4.59 s ``` Still needs examples in the documentation. The next step is finally expose `apply_ufunc` as public API :). - [x] Closes #585 - [x] Tests added / passed - [x] Passes ``git diff upstream/master | flake8 --diff`` - [x] Fully documented, including `whats-new.rst` for all changes and `api.rst` for new API cc @mrocklin ","{""url"": ""https://api.github.com/repos/pydata/xarray/issues/1517/reactions"", ""total_count"": 2, ""+1"": 2, ""-1"": 0, ""laugh"": 0, ""hooray"": 0, ""confused"": 0, ""heart"": 0, ""rocket"": 0, ""eyes"": 0}",,,13221727,pull