Generic function for cumulative reduction Parameters ---------- func: callable Cumulative function like np.cumsum or np.cumprod binop: callable Associated binary operator like ``np.cumsum->add`` or ``np.cumprod->mul`` ident: Number Associated identity lik
(
func,
binop,
ident,
x,
axis=None,
dtype=None,
out=None,
method="sequential",
preop=None,
)
| 1155 | |
| 1156 | |
| 1157 | def cumreduction( |
| 1158 | func, |
| 1159 | binop, |
| 1160 | ident, |
| 1161 | x, |
| 1162 | axis=None, |
| 1163 | dtype=None, |
| 1164 | out=None, |
| 1165 | method="sequential", |
| 1166 | preop=None, |
| 1167 | ): |
| 1168 | """Generic function for cumulative reduction |
| 1169 | |
| 1170 | Parameters |
| 1171 | ---------- |
| 1172 | func: callable |
| 1173 | Cumulative function like np.cumsum or np.cumprod |
| 1174 | binop: callable |
| 1175 | Associated binary operator like ``np.cumsum->add`` or ``np.cumprod->mul`` |
| 1176 | ident: Number |
| 1177 | Associated identity like ``np.cumsum->0`` or ``np.cumprod->1`` |
| 1178 | x: dask Array |
| 1179 | axis: int |
| 1180 | dtype: dtype |
| 1181 | method : {'sequential', 'blelloch'}, optional |
| 1182 | Choose which method to use to perform the cumsum. Default is 'sequential'. |
| 1183 | |
| 1184 | * 'sequential' performs the scan of each prior block before the current block. |
| 1185 | * 'blelloch' is a work-efficient parallel scan. It exposes parallelism by first |
| 1186 | calling ``preop`` on each block and combines the values via a binary tree. |
| 1187 | This method may be faster or more memory efficient depending on workload, |
| 1188 | scheduler, and hardware. More benchmarking is necessary. |
| 1189 | preop: callable, optional |
| 1190 | Function used by 'blelloch' method, |
| 1191 | like ``np.cumsum->np.sum`` or ``np.cumprod->np.prod`` |
| 1192 | |
| 1193 | Returns |
| 1194 | ------- |
| 1195 | dask array |
| 1196 | |
| 1197 | See also |
| 1198 | -------- |
| 1199 | cumsum |
| 1200 | cumprod |
| 1201 | """ |
| 1202 | if method == "blelloch": |
| 1203 | if preop is None: |
| 1204 | raise TypeError( |
| 1205 | 'cumreduction with "blelloch" method required `preop=` argument' |
| 1206 | ) |
| 1207 | return prefixscan_blelloch(func, preop, binop, x, axis, dtype, out=out) |
| 1208 | elif method != "sequential": |
| 1209 | raise ValueError( |
| 1210 | f'Invalid method for cumreduction. Expected "sequential" or "blelloch". Got: {method!r}' |
| 1211 | ) |
| 1212 | |
| 1213 | if axis is None: |
| 1214 | if x.ndim > 1: |
searching dependent graphs…