Skip to content

Commit

Permalink
Officially mention that parallelize uses <thread>.
Browse files Browse the repository at this point in the history
This allows people to reliably use <mutex> to serialize code within this
section. Also link to manticore in the docstrings.
  • Loading branch information
LTLA committed Aug 27, 2024
1 parent 0dfc05d commit 0e5b24b
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 18 deletions.
5 changes: 5 additions & 0 deletions .github/workflows/doxygenate.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ jobs:
with:
args: -O docs/tatami.tag https://tatami-inc.github.io/tatami/tatami.tag

- name: Get the manticore tagfile
uses: wei/wget@v1
with:
args: -O docs/manticore.tag https://tatami-inc.github.io/manticore/manticore.tag

- name: Doxygen Action
uses: mattnotmitt/doxygen-action@v1.1.0
with:
Expand Down
3 changes: 2 additions & 1 deletion docs/Doxyfile
Original file line number Diff line number Diff line change
Expand Up @@ -2328,7 +2328,8 @@ SKIP_FUNCTION_MACROS = YES
# the path). If a tag file is not located in the directory in which doxygen is
# run, you must also specify the path to the tagfile here.

TAGFILES = tatami.tag=https://tatami-inc.github.io/tatami
TAGFILES = tatami.tag=https://tatami-inc.github.io/tatami \
manticore.tag=https://tatami-inc.github.io/manticore

# When a file name is specified after GENERATE_TAGFILE, doxygen will create a
# tag file that is based on the input files it reads. See section "Linking to
Expand Down
34 changes: 17 additions & 17 deletions include/tatami_r/parallelize.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,40 +40,40 @@ inline manticore::Executor& executor() {

/**
* @tparam Function_ Function to be executed.
* @tparam Index_ Integer type for the job indices.
* @tparam Index_ Integer type for the task indices.
*
* @param fun Function to run in each thread.
* This is a lambda that should accept three arguments:
* - Integer containing the thread ID.
* - Integer specifying the index of the first job to be executed in a thread.
* - Integer specifying the number of jobs to be executed in a thread.
* @param njobs Number of jobs to be executed.
* - Integer specifying the index of the first task to be executed in a thread.
* - Integer specifying the number of tasks to be executed in a thread.
* @param ntasks Number of tasks to be executed.
* @param nthreads Number of threads to parallelize over.
*
* This function is a drop-in replacement for `tatami::parallelize()`.
* The series of integers from 0 to `njobs - 1` is split into `nthreads` contiguous ranges.
* Each range is used as input to `fun` within the corresponding thread.
* It is assumed that the execution of any given job is independent of the next.
* The series of integers from `[0, ntasks)` is split into `nthreads` contiguous ranges.
* Each range is used as input to a call to `fun` within a thread created by the standard `<thread>` library.
* Serialization can be achieved via `<mutex>` in most cases, or `manticore::Executor::run()` if the task must be performed on the main thread (see `executor()`).
*
* This function is only available if `TATAMI_R_PARALLELIZE_UNKNOWN` is defined.
*/
template<class Function_, class Index_>
void parallelize(Function_ fun, Index_ njobs, int nthreads) {
if (njobs == 0) {
void parallelize(Function_ fun, Index_ ntasks, int nthreads) {
if (ntasks == 0) {
return;
}

if (nthreads <= 1 || njobs == 1) {
fun(0, 0, njobs);
if (nthreads <= 1 || ntasks == 1) {
fun(0, 0, ntasks);
return;
}

Index_ jobs_per_worker = njobs / nthreads;
int remainder = njobs % nthreads;
if (jobs_per_worker == 0) {
jobs_per_worker = 1;
Index_ tasks_per_worker = ntasks / nthreads;
int remainder = ntasks % nthreads;
if (tasks_per_worker == 0) {
tasks_per_worker = 1;
remainder = 0;
nthreads = njobs;
nthreads = ntasks;
}

auto& mexec = executor();
Expand All @@ -85,7 +85,7 @@ void parallelize(Function_ fun, Index_ njobs, int nthreads) {

Index_ start = 0;
for (int w = 0; w < nthreads; ++w) {
Index_ length = jobs_per_worker + (w < remainder);
Index_ length = tasks_per_worker + (w < remainder);

runners.emplace_back([&](int id, Index_ s, Index_ l) {
try {
Expand Down

0 comments on commit 0e5b24b

Please sign in to comment.