futureverse / futureverse/parallelly

detectThreadsPerCore(): infer how many threads per cores the machine's CPU provides

Open
#47 0 comments 0 reactions 0 assignees View on GitHub
enhancement feature/resources help wanted
Dominant language
R
Stars
140
Forks
9
PR merge metrics
No merged PRs in 30d

Description

# Wish

For example, for a machine with:

```sh
$ lscpu | grep -iF "core"
Thread(s) per core: 2
Core(s) per socket: 4
Model name: Intel(R) Core(TM) i7-8650U CPU @ 1.90GHz
```

we should get:

```r
> detectThreadsPerCore()
[1] 2
```

# Action

* [ ] Figure out how to do this reliably on all operating systems and Unix distributions.
* [ ] If it cannot be inferred, return `default`, which could default to either `1L` ("always works") or `NA_integer_` ("unknown"). Alternatively, there could be a `mustWork` argument or similar.

# Background

This will be useful when limiting multi-_threading_ in nested parallelization. For example, in **future** we try to force single-_processing_ by setting various environment variables and R options in parallel workers, e.g. `MC_CORES=1`. Currently, we do not limit the number of parallel threads.

The simplest approach would be to force a single thread, but it would be more efficient on modern systems if we'd limit it to `detectThreadsPerCore()` threads, e.g.

```r
RhpcBLASctl::blas_set_num_threads(threads = detectThreadsPerCore())
RcppParallel::setThreadOptions(numThreads = detectThreadsPerCore())
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.