use crate::error::KError;
use std::fmt;
use std::str::FromStr;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum DistCoarseStrategy {
None,
RootGather,
LocalPrototype,
SuperLuDist,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum DistCoarsePolicy {
RootGather,
Replicated,
External,
}
impl Default for DistCoarsePolicy {
fn default() -> Self {
Self::RootGather
}
}
impl FromStr for DistCoarsePolicy {
type Err = KError;
fn from_str(value: &str) -> Result<Self, Self::Err> {
match value.to_lowercase().as_str() {
"root" | "root_gather" | "gather" => Ok(Self::RootGather),
"replicated" | "replicate" | "all" => Ok(Self::Replicated),
"external" | "backend" | "superlu_dist" => Ok(Self::External),
other => Err(KError::InvalidInput(format!(
"invalid dist coarse policy: {other}"
))),
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum DistCoarseRepartition {
Keep,
Uniform,
Root,
}
impl Default for DistCoarseRepartition {
fn default() -> Self {
Self::Keep
}
}
impl FromStr for DistCoarseRepartition {
type Err = KError;
fn from_str(value: &str) -> Result<Self, Self::Err> {
match value.to_lowercase().as_str() {
"keep" | "fine" | "inherit" => Ok(Self::Keep),
"uniform" | "block" | "contiguous" => Ok(Self::Uniform),
"hybrid" | "local" => Ok(Self::Uniform),
"root" | "root_owned" => Ok(Self::Root),
other => Err(KError::InvalidInput(format!(
"invalid dist coarse repartition policy: {other}"
))),
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum DistCoarseSolverRoute {
Auto,
Root,
Local,
SuperLuDist,
}
impl Default for DistCoarseSolverRoute {
fn default() -> Self {
Self::Auto
}
}
impl FromStr for DistCoarseSolverRoute {
type Err = KError;
fn from_str(value: &str) -> Result<Self, Self::Err> {
match value.to_lowercase().as_str() {
"auto" => Ok(Self::Auto),
"root" | "root_gather" | "gather" => Ok(Self::Root),
"local" | "local_prototype" | "hybrid" => Ok(Self::Local),
"superlu_dist" | "superludist" => Ok(Self::SuperLuDist),
other => Err(KError::InvalidInput(format!(
"invalid dist coarse solver route: {other}"
))),
}
}
}
impl DistCoarseStrategy {
pub fn is_rank_local(self) -> bool {
matches!(
self,
DistCoarseStrategy::None | DistCoarseStrategy::LocalPrototype
)
}
pub fn is_collective(self) -> bool {
matches!(
self,
DistCoarseStrategy::RootGather | DistCoarseStrategy::SuperLuDist
)
}
}
impl Default for DistCoarseStrategy {
fn default() -> Self {
DistCoarseStrategy::RootGather
}
}
impl fmt::Display for DistCoarseStrategy {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let label = match self {
DistCoarseStrategy::None => "none",
DistCoarseStrategy::RootGather => "root_gather",
DistCoarseStrategy::LocalPrototype => "local_prototype",
DistCoarseStrategy::SuperLuDist => "superlu_dist",
};
write!(f, "{label}")
}
}
impl FromStr for DistCoarseStrategy {
type Err = KError;
fn from_str(value: &str) -> Result<Self, Self::Err> {
match value.to_lowercase().as_str() {
"none" | "off" => Ok(DistCoarseStrategy::None),
"root" | "root_gather" | "gather" => Ok(DistCoarseStrategy::RootGather),
"local" | "local_prototype" | "prototype" | "hybrid" => {
Ok(DistCoarseStrategy::LocalPrototype)
}
"superlu_dist" | "superludist" => Ok(DistCoarseStrategy::SuperLuDist),
other => Err(KError::InvalidInput(format!(
"invalid dist coarse strategy: {other}"
))),
}
}
}