Skip to content

Commit

Permalink
Add --threads / -j to control number of compute threads (#5021)
Browse files Browse the repository at this point in the history
### What
* Closes #4931

You can now control the number of threads in the rayon thread pool using
`-j` or `--threads`. The default is `-2`, meaning two less threads than
the number of cores. This is to leave some breathing room for other
threads the viewer spawns, as well as the rest of the users system.

### Checklist
* [x] I have read and agree to [Contributor
Guide](https://github.com/rerun-io/rerun/blob/main/CONTRIBUTING.md) and
the [Code of
Conduct](https://github.com/rerun-io/rerun/blob/main/CODE_OF_CONDUCT.md)
* [x] I've included a screenshot or gif (if applicable)
* [x] I have tested the web demo (if applicable):
* Using newly built examples:
[app.rerun.io](https://app.rerun.io/pr/5021/index.html)
* Using examples from latest `main` build:
[app.rerun.io](https://app.rerun.io/pr/5021/index.html?manifest_url=https://app.rerun.io/version/main/examples_manifest.json)
* Using full set of examples from `nightly` build:
[app.rerun.io](https://app.rerun.io/pr/5021/index.html?manifest_url=https://app.rerun.io/version/nightly/examples_manifest.json)
* [x] The PR title and labels are set such as to maximize their
usefulness for the next release's CHANGELOG

- [PR Build Summary](https://build.rerun.io/pr/5021)
- [Docs
preview](https://rerun.io/preview/e2052e86d1395fc070e84bd8357ce56081959d87/docs)
<!--DOCS-PREVIEW-->
- [Examples
preview](https://rerun.io/preview/e2052e86d1395fc070e84bd8357ce56081959d87/examples)
<!--EXAMPLES-PREVIEW-->
- [Recent benchmark results](https://build.rerun.io/graphs/crates.html)
- [Wasm size tracking](https://build.rerun.io/graphs/sizes.html)
  • Loading branch information
emilk committed Feb 5, 2024
1 parent c3da540 commit 13f1852
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 6 deletions.
6 changes: 0 additions & 6 deletions crates/rerun-cli/src/bin/rerun.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,6 @@ static GLOBAL: AccountingAllocator<mimalloc::MiMalloc> =
async fn main() -> std::process::ExitCode {
re_log::setup_logging();

// Name the rayon threads for the benefit of debuggers and profilers:
rayon::ThreadPoolBuilder::new()
.thread_name(|i| format!("rayon-{i}"))
.build_global()
.unwrap();

let build_info = re_build_info::build_info!();

let result = rerun::run(build_info, rerun::CallSource::Cli, std::env::args()).await;
Expand Down
43 changes: 43 additions & 0 deletions crates/rerun/src/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,19 @@ When persisted, the state will be stored at the following locations:
#[clap(long)]
skip_welcome_screen: bool,

/// The number of compute threads to use.
///
/// If zero, the same number of threads as the number of cores will be used.
/// If negative, will use that much fewer threads than cores.
///
/// Rerun will still use some additional threads for I/O.
#[clap(
long,
short = 'j',
default_value = "-2", // save some CPU for the main thread and the rest of the users system
)]
threads: i32,

#[clap(long_help = r"Any combination of:
- A WebSocket url to a Rerun Server
- An HTTP(S) URL to an .rrd file to load
Expand Down Expand Up @@ -329,6 +342,8 @@ where
use clap::Parser as _;
let mut args = Args::parse_from(args);

initialize_thread_pool(args.threads);

if args.web_viewer {
args.serve = true;
}
Expand Down Expand Up @@ -386,6 +401,34 @@ where
}
}

fn initialize_thread_pool(threads_args: i32) {
// Name the rayon threads for the benefit of debuggers and profilers:
let mut builder = rayon::ThreadPoolBuilder::new().thread_name(|i| format!("rayon-{i}"));

if threads_args < 0 {
match std::thread::available_parallelism() {
Ok(cores) => {
let threads = cores.get().saturating_sub((-threads_args) as _).max(1);
re_log::debug!("Detected {cores} cores. Using {threads} compute threads.");
builder = builder.num_threads(threads);
}
Err(err) => {
re_log::warn!("Failed to query system of the number of cores: {err}.");
// Let rayon decide for itself how many threads to use.
// It's default is to use as many threads as we have cores,
// (if rayon manages to figure out how many cores we have).
}
}
} else {
// 0 means "use all cores", and rayon understands that
builder = builder.num_threads(threads_args as usize);
}

if let Err(err) = builder.build_global() {
re_log::warn!("Failed to initialize rayon thread pool: {err}");
}
}

/// Checks whether two .rrd files are _similar_, i.e. not equal on a byte-level but
/// functionally equivalent.
///
Expand Down

0 comments on commit 13f1852

Please sign in to comment.