Skip to content

Commit af9a2eb

Browse files
committed
refactor: simplify config parsing
Signed-off-by: Richard Zak <[email protected]>
1 parent ca66f89 commit af9a2eb

File tree

4 files changed

+40
-62
lines changed

4 files changed

+40
-62
lines changed

src/env_var.rs

Lines changed: 32 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
4545
use serde::Deserialize;
4646
use std::sync::OnceLock;
47+
use crate::ExecutorType;
4748

4849
fn default_deadlock_warning_timeout() -> f64 {
4950
600.0
@@ -57,20 +58,6 @@ fn default_dissemination_factor() -> usize {
5758
2
5859
}
5960

60-
fn default_backend() -> String {
61-
#[cfg(feature = "rofi")]
62-
return "rofi".to_owned();
63-
#[cfg(not(feature = "rofi"))]
64-
return "local".to_owned();
65-
}
66-
67-
fn default_executor() -> String {
68-
#[cfg(feature = "tokio-executor")]
69-
return "tokio".to_owned();
70-
#[cfg(not(feature = "tokio-executor"))]
71-
return "lamellar".to_owned();
72-
}
73-
7461
fn default_batcher() -> String {
7562
"simple".to_owned()
7663
}
@@ -109,15 +96,14 @@ fn default_alloc() -> Alloc {
10996
}
11097

11198
#[doc(hidden)]
112-
#[derive(Deserialize, Debug, PartialEq)]
99+
#[derive(Deserialize, Debug, Default, PartialEq)]
113100
#[serde(rename_all = "lowercase")]
114101
pub enum IndexType {
115102
Static,
103+
104+
#[default]
116105
Dynamic,
117106
}
118-
fn default_array_dynamic_index() -> IndexType {
119-
IndexType::Dynamic
120-
}
121107

122108
fn default_cmd_buf_len() -> usize {
123109
50000
@@ -156,6 +142,32 @@ where
156142
}
157143
}
158144

145+
#[doc(hidden)]
146+
#[derive(Deserialize, Debug, Default)]
147+
pub enum Executor {
148+
#[default]
149+
Lamellar,
150+
Tokio,
151+
Async_std
152+
}
153+
154+
/// The lamellae backend to use
155+
#[doc(hidden)]
156+
#[derive(Deserialize, Debug, Default)]
157+
pub enum Backend {
158+
/// multi pe distributed execution, default if rofi feature is turned on
159+
#[cfg(feature = "rofi")]
160+
#[cfg_attr(feature = "rofi", default)]
161+
Rofi,
162+
163+
/// Single pe execution, default if rofi feature is turned off
164+
#[cfg_attr(not(feature = "rofi"), default)]
165+
Shmem,
166+
167+
/// Multi pe single node execution
168+
Local
169+
}
170+
159171
#[doc(hidden)]
160172
#[derive(Deserialize, Debug)]
161173
pub struct Config {
@@ -187,12 +199,10 @@ pub struct Config {
187199
/// rofi -- multi pe distributed execution, default if rofi feature is turned on
188200
/// local -- single pe execution, default if rofi feature is turned off
189201
/// shmem -- multi pe single node execution
190-
#[serde(default = "default_backend")]
191-
pub backend: String, //rofi,shmem,local
202+
pub backend: Backend,
192203

193204
/// The executor (thread scheduler) to use, default: 'lamellar' unless the tokio feature is turned on
194-
#[serde(default = "default_executor")]
195-
pub executor: String, //lamellar,tokio,async_std
205+
pub executor: ExecutorType,
196206

197207
/// The batcher to use, default: 'simple'
198208
#[serde(default = "default_batcher")]
@@ -205,7 +215,6 @@ pub struct Config {
205215
pub heap_mode: HeapMode,
206216
#[serde(default = "default_alloc")]
207217
pub alloc: Alloc,
208-
#[serde(default = "default_array_dynamic_index")]
209218
pub index_size: IndexType,
210219

211220
//used internally by the command queues

src/lamellae.rs

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,15 @@ lazy_static! {
3636

3737
/// The list of available lamellae backends, used to specify how data is transfered between PEs
3838
#[derive(
39-
serde::Serialize, serde::Deserialize, Debug, PartialEq, Eq, Ord, PartialOrd, Hash, Clone, Copy,
39+
serde::Serialize, serde::Deserialize, Debug, Default, PartialEq, Eq, Ord, PartialOrd, Hash, Clone, Copy,
4040
)]
4141
pub enum Backend {
4242
#[cfg(feature = "rofi")]
43+
#[cfg_attr(feature = "rofi", default)]
4344
/// The Rofi (Rust-OFI) backend -- intended for multi process and distributed environments
4445
Rofi,
4546
/// The Local backend -- intended for single process environments
47+
#[cfg_attr(not(feature = "rofi"), default)]
4648
Local,
4749
/// The Shmem backend -- intended for multi process environments single node environments
4850
Shmem,
@@ -55,24 +57,6 @@ pub(crate) enum AllocationType {
5557
Sub(Vec<usize>),
5658
}
5759

58-
impl Default for Backend {
59-
fn default() -> Self {
60-
match config().backend.as_str() {
61-
"rofi" => {
62-
#[cfg(feature = "rofi")]
63-
return Backend::Rofi;
64-
#[cfg(not(feature = "rofi"))]
65-
panic!("unable to set rofi backend, recompile with 'enable-rofi' feature")
66-
}
67-
"shmem" => {
68-
return Backend::Shmem;
69-
}
70-
_ => {
71-
return Backend::Local;
72-
}
73-
}
74-
}
75-
}
7660
// fn default_backend() -> Backend {
7761
// match std::env::var("LAMELLAE_BACKEND") {
7862
// Ok(p) => match p.as_str() {

src/lamellar_world.rs

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -457,27 +457,11 @@ impl LamellarWorldBuilder {
457457
pub fn new() -> LamellarWorldBuilder {
458458
// simple_logger::init().unwrap();
459459
// trace!("New world builder");
460-
let executor = match config().executor.as_str(){
461-
"tokio" => {
462-
#[cfg(not(feature = "tokio-executor"))]
463-
{
464-
panic!("[LAMELLAR WARNING]: tokio-executor selected but it is not enabled, either recompile lamellar with --features tokio-executor, or set LAMELLAR_EXECUTOR to one of 'lamellar' or 'async_std'");
465-
}
466-
#[cfg(feature = "tokio-executor")]
467-
ExecutorType::Tokio
468-
}
469-
"async_std" => ExecutorType::AsyncStd,
470-
"lamellar" => ExecutorType::LamellarWorkStealing,
471-
"lamellar2" => ExecutorType::LamellarWorkStealing2,
472-
"lamellar3" => ExecutorType::LamellarWorkStealing3,
473-
_ => panic!("[LAMELLAR WARNING]: unexpected executor type, please set LAMELLAR_EXECUTOR to one of the following 'lamellar', 'async_std', or (if tokio-executor feature is enabled, 'tokio'.")
474-
};
475-
476460
let num_threads = config().threads;
477461
LamellarWorldBuilder {
478462
primary_lamellae: Default::default(),
479463
// secondary_lamellae: HashSet::new(),
480-
executor: executor,
464+
executor: config().executor,
481465
num_threads: num_threads,
482466
}
483467
}

src/scheduler.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ use async_std_executor::AsyncStdRt;
3131
pub(crate) mod tokio_executor;
3232
#[cfg(feature = "tokio-executor")]
3333
use tokio_executor::TokioRt;
34-
34+
use crate::Deserialize;
3535
// ACTIVE ENUM
3636
// since atomic enums would be another dependecy
3737

@@ -78,18 +78,19 @@ pub(crate) struct ReqId {
7878
/// Default is a work stealing executor
7979
/// If the "tokio-executor" feature is enabled,the tokio executor can also be used
8080
/// allowing seemless integration with tokio based applications
81-
#[derive(Debug)]
81+
#[derive(Copy, Clone, Debug, Default, Deserialize, PartialEq, Eq)]
8282
pub enum ExecutorType {
8383
/// The default work stealing executor
84+
#[default]
8485
LamellarWorkStealing,
8586
/// Experimental numa-aware(ish) work stealing executor
8687
LamellarWorkStealing2,
8788
/// Experimental numa-aware(ish) work stealing executor
8889
LamellarWorkStealing3,
8990
/// executor provided by the AsyncStd crate
9091
AsyncStd,
91-
#[cfg(feature = "tokio-executor")]
9292
/// The tokio executor
93+
#[cfg(feature = "tokio-executor")]
9394
Tokio,
9495
// Dyn(impl LamellarExecutor),
9596
}

0 commit comments

Comments
 (0)