@@ -30,29 +30,36 @@ namespace tatami_python {
3030
3131/* *
3232 * Replacement for `tatami::parallelize()` that applies a function to a set of tasks in parallel, usually for iterating over a dimension of a `Matrix`.
33- * This releases the Python GIL so that it can be re-acquired by `UnknownMatrix` extractors in each individual thread .
33+ * This releases the Python GIL so that it can be re-acquired by `UnknownMatrix` extractors in each individual worker .
3434 *
3535 * @tparam Function_ Function to be applied to a contiguous range of tasks.
3636 * This should accept three arguments:
37- * - `thread `, the thread number executing this task range.
38- * This will be passed as an `int`.
37+ * - `worker `, the worker ID executing this task range.
38+ * This will be passed as an `int` in `[0, workers)` .
3939 * - `task_start`, the start index of the task range.
40- * This will be passed as an `Index_`.
40+ * This will be passed as an `Index_` in `[0, tasks)` .
4141 * - `task_length`, the number of tasks in the task range.
42- * This will be passed as an `Index_`.
42+ * This will be passed as an `Index_` in `(0, tasks)`, i.e., it is always positive .
4343 * @tparam Index_ Integer type for the number of tasks.
4444 *
4545 * @param fun Function that executes a contiguous range of tasks.
46+ * This will be called no more than once in each worker with a different non-overlapping range, where the union of all ranges will cover `[0, tasks)`.
4647 * @param tasks Number of tasks.
47- * @param threads Number of threads.
48+ * This should be non-negative.
49+ * @param workers Number of workers.
50+ * This should be positive.
51+ *
52+ * @return The number of workers (`K`) that were actually used.
53+ * `K` is guaranteed to be no greater than `workers` (or 1, if `workers` is not positive).
54+ * `fun()` will have been called once for each of the worker IDs `[0, ..., K - 1]`.
4855 */
4956template <class Function_ , class Index_ >
50- void parallelize (const Function_ fun, const Index_ tasks, int threads ) {
57+ int parallelize (const Function_ fun, const Index_ tasks, int workers ) {
5158 std::optional<pybind11::gil_scoped_release> ungil;
5259 if (PyGILState_Check ()) {
5360 ungil.emplace ();
5461 }
55- subpar::parallelize_range (threads , tasks, std::move (fun));
62+ return subpar::parallelize_range (workers , tasks, std::move (fun));
5663}
5764
5865/* *
0 commit comments