Skip to content

Commit 450ec3f

Browse files
committed
refine reduce_sum function reference
1 parent 3b855e8 commit 450ec3f

File tree

1 file changed

+27
-26
lines changed

1 file changed

+27
-26
lines changed

src/functions-reference/higher-order_functions.Rmd

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -385,47 +385,48 @@ The gradients of the integral are computed in accordance with the Leibniz integr
385385

386386
## Reduce-Sum Function {#functions-reduce}
387387

388-
Stan provides a higher-order `reduce_sum` function for parallelizing operations that can be represented as a sum of a function, `g: U -> real`, evaluated at each element of a list of type `U[]`, `{ x1, x2, ... }`. That is:
388+
Stan provides a higher-order `reduce_sum` function which maps
389+
evaluation of a function `g: U -> real` to a list of type `U[]`, `{
390+
x1, x2, ... }`, and performs as reduction operation a sum over the
391+
results. That is:
389392

390393
```g(x1) + g(x2) + ...```
391394

392-
`reduce_sum` doesn't work on `g` itself, but takes a partial sum function, `f: U[] -> real`, where:
395+
`reduce_sum` doesn't work with the element-wise evaluated
396+
function `g` itself, but instead works through evaluating partial
397+
sums, `f: U[] -> real`, where:
393398

394-
```f({ x1 }) = g(x1)```
395-
```f({ x1, x2 }) = g(x1) + g(x2)```
396-
```f({ x1, x2, ... }) = g(x1) + g(x2) + ...```
399+
```
400+
f({ x1 }) = g(x1)
401+
f({ x1, x2 }) = g(x1) + g(x2)
402+
f({ x1, x2, ... }) = g(x1) + g(x2) + ...
403+
```
397404

398-
### The Reduce-sum Function
405+
### Specifying the Reduce-sum Function
399406

400-
The `reduce_sum` function takes a partial sum function, an array argument x
401-
(with one for each term in the sum), a recommended grainsize, and a set of shared arguments and
402-
parallelizes the resultant sum.
407+
The `reduce_sum` function takes a partial sum function `f`, an array argument `x`
408+
(with one array element for each term in the sum), a recommended
409+
`grainsize`, and a set of shared arguments. This representation allows
410+
to parallelize the resultant sum.
403411

404412
<!-- real; reduce_sum; (F f, T[] x, int grainsize, T1 s1, T2 s2, ...); -->
405413
\index{{\tt \bfseries reduce\_sum }!{\tt (F f, T[] x, int grainsize, T1 s1, T2 s2, ...): real}|hyperpage}
406414

407415
`real` **`reduce_sum`**`(F f, T[] x, int grainsize, T1 s1, T2 s2, ...)`<br>\newline
408416

409-
Return the equivalent of `f(1, size(x), x, s1, s2, ...)`, but compute
410-
the result in parallel by breaking the array `x` into pieces and computing each piece
411-
in a different thread. `s1, s2, ...` are shared between all terms in the sum.
412-
413-
* *`f`*: function literal referring to a function specifying the partial sum operation with signature `(int, int, T[], T1, T2, ...):real`
414-
The arguments represent
415-
+ (1) the index of the first term of the partial sum,
416-
+ (2) the index of the last term of the partial sum,
417-
+ (3) the subset `x` this reduce is responsible for computing,
418-
+ (4) first shared argument,
419-
+ (5) second shared argument,
420-
+ ... the rest of the shared arguments.
417+
Returns the equivalent of `f(1, size(x), x, s1, s2, ...)`, but computes
418+
the result in parallel by breaking the array `x` into independent
419+
partial sums. `s1, s2, ...` are shared between all terms in the sum.
421420

421+
* *`f`*: function literal referring to a function specifying the
422+
partial sum operation. Refer to the [partial sum function](#functions-partial-sum).
422423
* *`x`*: array of `T`, one for each term of the reduction, `T` can be any type,
423-
* *`grainsize`*: recommented number of terms in each reduce call, set to one to estimate automatically, type `int`,
424+
* *`grainsize`*: recommended number of terms in each reduce call, set to one to estimate automatically, type `int`,
424425
* *`s1`*: first (optional) shared argument, type `T1`, where `T1` can be any type
425426
* *`s2`*: second (optional) shared argument, type `T2`, where `T2` can be any type,
426427
* *`...`*: remainder of shared arguments, each of which can be any type.
427428

428-
### The Partial-sum Function
429+
### The Partial-sum Function {#functions-partial-sum}
429430

430431
The partial sum function must have the following signature where the types `T`, and the
431432
types of all the shared arguments (`T1`, `T2`, ...) match those of the original
@@ -435,14 +436,14 @@ types of all the shared arguments (`T1`, `T2`, ...) match those of the original
435436
(int start, int end, T[] x_subset, T1 s1, T2 s2, ...):real
436437
```
437438

438-
The reduce function returns the sum of the `start` to `end` terms (inclusive) of the overall
439-
calculations. The arguments to the reduce function are:
439+
The partial sum function returns the sum of the `start` to `end` terms (inclusive) of the overall
440+
calculations. The arguments to the partial sum function are:
440441

441442
* *`start`*, the index of the first term of the partial sum, type `int`
442443

443444
* *`end`*, the index of the last term of the partial sum (inclusive), type `int`
444445

445-
* *`x_subset`*, the subset of `x` this partial sum is responsible for computing, type `T[]`, where `T` matches the type of `x` in `reduce_sum`
446+
* *`x_subset`*, the subset of `x` a given partial sum is responsible for computing, type `T[]`, where `T` matches the type of `x` in `reduce_sum`
446447

447448
* *`s1`*, first shared argument, type `T1`, matching type of `s1` in `reduce_sum`
448449

0 commit comments

Comments
 (0)