Skip to content

Commit

Permalink
Add --threads / -j to control number of compute threads
Browse files Browse the repository at this point in the history
  • Loading branch information
emilk committed Feb 2, 2024
1 parent a5a1d7d commit 8cd04d1
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 6 deletions.
1 change: 1 addition & 0 deletions Cranky.toml
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ warn = [
]

allow = [
"clippy::comparison_chain", # annoying
"clippy::manual_range_contains", # this one is just worse imho

# TODO(emilk): enable more of these lints:
Expand Down
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
40 changes: 40 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_thead_pool(args.threads);

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

fn initialize_thead_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}");
}
}
} 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 8cd04d1

Please sign in to comment.