ndgrid/grid/
parallel_grid.rs

1//! Parallel grid
2#[cfg(feature = "serde")]
3use crate::traits::{ConvertToSerializable, RONImportParallel};
4use crate::{
5    grid::local_grid::LocalGrid,
6    traits::{Grid, ParallelGrid},
7    types::{Ownership, Scalar},
8};
9use mpi::traits::Communicator;
10use rlst::distributed_tools::IndexLayout;
11#[cfg(feature = "serde")]
12use std::hash::Hash;
13use std::{collections::HashMap, fmt::Debug};
14
15/// Parallel grid
16#[derive(Debug)]
17pub struct ParallelGridImpl<'a, C: Communicator, G: Grid + Sync> {
18    comm: &'a C,
19    local_grid: LocalGrid<G>,
20    cell_layout: std::rc::Rc<IndexLayout<'a, C>>,
21}
22
23impl<'a, C: Communicator, G: Grid + Sync> ParallelGridImpl<'a, C, G> {
24    /// Create new
25    pub fn new(
26        comm: &'a C,
27        serial_grid: G,
28        ownership: HashMap<G::EntityDescriptor, Vec<Ownership>>,
29        global_indices: HashMap<G::EntityDescriptor, Vec<usize>>,
30    ) -> Self {
31        let local_grid = LocalGrid::new(serial_grid, ownership, global_indices);
32        let owned_cell_count = local_grid.owned_cell_count();
33        Self {
34            comm,
35            local_grid,
36            cell_layout: std::rc::Rc::new(IndexLayout::from_local_counts(owned_cell_count, comm)),
37        }
38    }
39}
40
41#[cfg(feature = "serde")]
42impl<
43    'a,
44    EntityDescriptor: Debug + PartialEq + Eq + Clone + Copy + Hash + serde::Serialize,
45    C: Communicator + 'a,
46    G: Grid<EntityDescriptor = EntityDescriptor> + Sync + ConvertToSerializable,
47> RONImportParallel<'a, C> for ParallelGridImpl<'a, C, G>
48where
49    for<'de2> <G as ConvertToSerializable>::SerializableType: serde::Deserialize<'de2>,
50    for<'de2> EntityDescriptor: serde::Deserialize<'de2>,
51    Self: 'a,
52{
53    fn create_from_ron_info(comm: &'a C, local_grid: LocalGrid<G>) -> Self {
54        let owned_cell_count = local_grid.owned_cell_count();
55        Self {
56            comm,
57            local_grid,
58            cell_layout: std::rc::Rc::new(IndexLayout::from_local_counts(owned_cell_count, comm)),
59        }
60    }
61}
62
63impl<T: Scalar, C: Communicator, G: Grid<T = T> + Sync> ParallelGrid
64    for ParallelGridImpl<'_, C, G>
65{
66    type LocalGrid = LocalGrid<G>;
67
68    type C = C;
69
70    type T = T;
71    fn comm(&self) -> &C {
72        self.comm
73    }
74    fn local_grid(&self) -> &Self::LocalGrid {
75        &self.local_grid
76    }
77
78    fn cell_layout(&self) -> std::rc::Rc<IndexLayout<'_, C>> {
79        self.cell_layout.clone()
80    }
81}