Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support CPU core ranges in farmer #2640

Merged
merged 1 commit into from
Mar 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 22 additions & 7 deletions crates/subspace-farmer/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,9 @@ use futures::channel::oneshot::Canceled;
use futures::future::Either;
use rayon::{ThreadBuilder, ThreadPool, ThreadPoolBuildError, ThreadPoolBuilder};
use std::future::Future;
use std::num::{NonZeroUsize, ParseIntError};
use std::num::NonZeroUsize;
use std::ops::Deref;
use std::pin::{pin, Pin};
use std::str::FromStr;
use std::task::{Context, Poll};
use std::{io, thread};
use thread_priority::{set_current_thread_priority, ThreadPriority};
Expand Down Expand Up @@ -273,16 +272,32 @@ pub fn all_cpu_cores() -> Vec<CpuCoreSet> {

/// Parse space-separated set of groups of CPU cores (individual cores are coma-separated) into
/// vector of CPU core sets that can be used for creation of plotting/replotting thread pools.
pub fn parse_cpu_cores_sets(s: &str) -> Result<Vec<CpuCoreSet>, ParseIntError> {
pub fn parse_cpu_cores_sets(
s: &str,
) -> Result<Vec<CpuCoreSet>, Box<dyn std::error::Error + Send + Sync>> {
#[cfg(feature = "numa")]
let topology = hwlocality::Topology::new().map(std::sync::Arc::new).ok();

s.split(' ')
.map(|s| {
let cores = s
.split(',')
.map(usize::from_str)
.collect::<Result<Vec<usize>, _>>()?;
let mut cores = Vec::new();
for s in s.split(',') {
let mut parts = s.split('-');
let range_start = parts
.next()
.ok_or(
"Bad string format, must be comma separated list of CPU cores or ranges",
)?
.parse()?;

if let Some(range_end) = parts.next() {
let range_end = range_end.parse()?;

cores.extend(range_start..=range_end);
} else {
cores.push(range_start);
}
}

Ok(CpuCoreSet {
cores,
Expand Down
34 changes: 33 additions & 1 deletion crates/subspace-farmer/src/utils/tests.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::utils::run_future_in_dedicated_thread;
use crate::utils::{parse_cpu_cores_sets, run_future_in_dedicated_thread};
use std::future;
use tokio::sync::oneshot;

Expand Down Expand Up @@ -46,3 +46,35 @@ fn run_future_in_dedicated_thread_tokio_on_drop() {
));
});
}

#[test]
fn test_parse_cpu_cores_sets() {
{
let cores = parse_cpu_cores_sets("0").unwrap();
assert_eq!(cores.len(), 1);
assert_eq!(cores[0].cores, vec![0]);
}
{
let cores = parse_cpu_cores_sets("0,1,2").unwrap();
assert_eq!(cores.len(), 1);
assert_eq!(cores[0].cores, vec![0, 1, 2]);
}
{
let cores = parse_cpu_cores_sets("0,1,2 4,5,6").unwrap();
assert_eq!(cores.len(), 2);
assert_eq!(cores[0].cores, vec![0, 1, 2]);
assert_eq!(cores[1].cores, vec![4, 5, 6]);
}
{
let cores = parse_cpu_cores_sets("0-2 4-6,7").unwrap();
assert_eq!(cores.len(), 2);
assert_eq!(cores[0].cores, vec![0, 1, 2]);
assert_eq!(cores[1].cores, vec![4, 5, 6, 7]);
}

assert!(parse_cpu_cores_sets("").is_err());
assert!(parse_cpu_cores_sets("a").is_err());
assert!(parse_cpu_cores_sets("0,").is_err());
assert!(parse_cpu_cores_sets("0,a").is_err());
assert!(parse_cpu_cores_sets("0 a").is_err());
}
Loading