use std::ffi::{c_int, c_void};
use std::collections::HashMap;
pub trait MPIDatatype {
fn mpi_datatype() -> c_int;
}
impl MPIDatatype for f32 {
fn mpi_datatype() -> c_int { 0 } }
impl MPIDatatype for f64 {
fn mpi_datatype() -> c_int { 1 } }
impl MPIDatatype for i32 {
fn mpi_datatype() -> c_int { 2 } }
impl MPIDatatype for i64 {
fn mpi_datatype() -> c_int { 3 } }
impl MPIDatatype for u32 {
fn mpi_datatype() -> c_int { 4 } }
impl MPIDatatype for u64 {
fn mpi_datatype() -> c_int { 5 } }
impl MPIDatatype for i8 {
fn mpi_datatype() -> c_int { 6 } }
impl MPIDatatype for u8 {
fn mpi_datatype() -> c_int { 7 } }
#[derive(Debug)]
pub struct MPIDatatype {
type_handle: *mut c_void,
elementsize: usize,
is_committed: bool,
}
#[derive(Debug, Clone, Copy)]
pub enum MPIReduceOp {
Sum,
Product,
Max,
Min,
LogicalAnd,
LogicalOr,
BitwiseAnd,
BitwiseOr,
BitwiseXor,
Custom(u32),
}
impl MPIReduceOp {
pub fn to_mpi_op(self) -> c_int {
match self {
MPIReduceOp::Sum => 0,
MPIReduceOp::Product => 1,
MPIReduceOp::Max => 2,
MPIReduceOp::Min => 3,
MPIReduceOp::LogicalAnd => 4,
MPIReduceOp::LogicalOr => 5,
MPIReduceOp::BitwiseAnd => 6,
MPIReduceOp::BitwiseOr => 7,
MPIReduceOp::BitwiseXor => 8,
MPIReduceOp::Custom(op) => op as c_int,
}
}
}
#[derive(Debug)]
pub struct DatatypeManager {
derived_types: HashMap<String, MPIDatatype>,
}
impl DatatypeManager {
pub fn new() -> Self {
Self {
derived_types: HashMap::new(),
}
}
pub fn register_datatype(&mut self, name: String, datatype: MPIDatatype) {
self.derived_types.insert(name, datatype);
}
pub fn get_datatype(&self, name: &str) -> Option<&MPIDatatype> {
self.derived_types.get(name)
}
pub fn has_datatype(&self, name: &str) -> bool {
self.derived_types.contains_key(name)
}
}
impl Default for DatatypeManager {
fn default() -> Self {
Self::new()
}
}
unsafe impl Send for MPIDatatype {}
unsafe impl Sync for MPIDatatype {}
impl MPIDatatype {
pub fn new(type_handle: *mut c_void, elementsize: usize) -> Self {
Self {
type_handle,
elementsize,
is_committed: false,
}
}
pub fn elementsize(&self) -> usize {
self.elementsize
}
pub fn is_committed(&self) -> bool {
self.is_committed
}
pub fn commit(&mut self) {
self.is_committed = true;
}
pub fn type_handle(&self) -> *mut c_void {
self.type_handle
}
}