-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
192 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
//! Map betwen two index layouts | ||
use bempp_distributed_tools::{ | ||
index_layout::{IndexLayout, IndexLayoutFromLocalCounts}, | ||
EquiDistributedIndexLayout, | ||
}; | ||
use itertools::{izip, Itertools}; | ||
use mpi::traits::Communicator; | ||
|
||
fn main() { | ||
let universe = mpi::initialize().unwrap(); | ||
let world = universe.world(); | ||
|
||
// Create an index layout with 10 indices on each rank. | ||
|
||
let layout1 = EquiDistributedIndexLayout::new(30, 1, &world); | ||
|
||
// Create a second layout with 5 indices on rank 0, 17 on rank 1 and 8 on rank 2. | ||
|
||
let counts = match world.rank() { | ||
0 => 5, | ||
1 => 17, | ||
2 => 8, | ||
_ => panic!("This example only works with three processes."), | ||
}; | ||
|
||
let layout2 = IndexLayoutFromLocalCounts::new(counts, &world); | ||
|
||
// Now we can map between the two layouts. | ||
|
||
let data = if world.rank() == 0 { | ||
(0..10).collect_vec() | ||
} else if world.rank() == 1 { | ||
(10..20).collect_vec() | ||
} else { | ||
(20..30).collect_vec() | ||
}; | ||
|
||
let mapped_data = layout1.remap(&layout2, &data); | ||
|
||
if world.rank() == 0 { | ||
assert_eq!(mapped_data.len(), 5); | ||
for (expected, &actual) in izip!(0..5, mapped_data.iter()) { | ||
assert_eq!(expected, actual); | ||
} | ||
} else if world.rank() == 1 { | ||
assert_eq!(mapped_data.len(), 17); | ||
for (expected, &actual) in izip!(5..22, mapped_data.iter()) { | ||
assert_eq!(expected, actual); | ||
} | ||
} else if world.rank() == 2 { | ||
assert_eq!(mapped_data.len(), 8); | ||
for (expected, &actual) in izip!(22..30, mapped_data.iter()) { | ||
assert_eq!(expected, actual); | ||
} | ||
} | ||
|
||
let remapped_data = layout2.remap(&layout1, &mapped_data); | ||
|
||
assert_eq!(data, remapped_data); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
//! Default distributed index layout | ||
use crate::index_layout::IndexLayout; | ||
use mpi::traits::{Communicator, CommunicatorCollectives}; | ||
|
||
/// Specify an index layout from local variable counts | ||
pub struct IndexLayoutFromLocalCounts<'a, C: Communicator> { | ||
counts: Vec<usize>, | ||
comm: &'a C, | ||
} | ||
|
||
impl<'a, C: Communicator + CommunicatorCollectives> IndexLayoutFromLocalCounts<'a, C> { | ||
/// Crate new | ||
pub fn new(local_count: usize, comm: &'a C) -> Self { | ||
let size = comm.size() as usize; | ||
let mut counts = vec![0; size + 1]; | ||
comm.all_gather_into(&local_count, &mut counts[1..]); | ||
for i in 1..=size { | ||
counts[i] += counts[i - 1]; | ||
} | ||
Self { counts, comm } | ||
} | ||
} | ||
|
||
impl<C: Communicator> IndexLayout for IndexLayoutFromLocalCounts<'_, C> { | ||
type Comm = C; | ||
|
||
fn counts(&self) -> &[usize] { | ||
&self.counts | ||
} | ||
|
||
fn comm(&self) -> &Self::Comm { | ||
&self.comm | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters