1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
use crate::{iter::*, util::*};
use rayon::prelude::*;
use std::marker::PhantomData;
#[derive(Clone)]
pub struct Settings<T: Coord> {
#[cfg(feature = "barnes_hut")]
pub barnes_hut: Option<T>,
#[cfg(feature = "parallel")]
pub chunk_size: Option<usize>,
pub dimensions: usize,
pub dissuade_hubs: bool,
pub ka: T,
pub kg: T,
pub kr: T,
pub lin_log: bool,
pub prevent_overlapping: Option<(T, T)>,
pub strong_gravity: bool,
}
impl<T: Coord> Default for Settings<T> {
fn default() -> Self {
Self {
#[cfg(feature = "barnes_hut")]
barnes_hut: None,
#[cfg(feature = "parallel")]
chunk_size: Some(256),
dimensions: 2,
dissuade_hubs: false,
ka: T::one(),
kg: T::one(),
kr: T::one(),
lin_log: false,
prevent_overlapping: None,
strong_gravity: false,
}
}
}
pub struct Layout<T: Coord> {
pub edges: Vec<Edge>,
pub masses: Vec<T>,
pub points: PointList<T>,
pub(crate) settings: Settings<T>,
pub speeds: PointList<T>,
pub old_speeds: PointList<T>,
pub(crate) fn_attraction: fn(&mut Self),
pub(crate) fn_gravity: fn(&mut Self),
pub(crate) fn_repulsion: fn(&mut Self),
}
impl<T: Coord> Layout<T> {
pub fn iter_nodes(&mut self) -> NodeIter<T> {
NodeIter {
ind: 0,
layout: SendPtr(self.into()),
offset: 0,
_phantom: PhantomData::default(),
}
}
}
impl<T: Coord + Send> Layout<T> {
pub fn iter_par_nodes(
&mut self,
chunk_size: usize,
) -> impl Iterator<Item = impl ParallelIterator<Item = NodeParIter<T>>> {
let ptr = SendPtr(self.into());
let dimensions = self.settings.dimensions;
let chunk_size_d = chunk_size * dimensions;
let n = self.masses.len() * dimensions;
(0..self.masses.len()).step_by(chunk_size).map(move |y0| {
let y0_d = y0 * dimensions;
(0..self.masses.len() - y0)
.into_par_iter()
.step_by(chunk_size)
.map(move |x0| {
let x0_d = x0 * dimensions;
NodeParIter {
end: (x0_d + chunk_size_d).min(n),
ind: x0,
layout: ptr,
n2_start: x0_d + y0_d,
n2_start_ind: x0 + y0,
n2_end: (x0_d + y0_d + chunk_size_d).min(n),
offset: x0_d,
_phantom: PhantomData::default(),
}
})
})
}
}
#[cfg(test)]
mod test {
use super::*;
use itertools::iproduct;
use std::{
collections::BTreeSet,
sync::{Arc, RwLock},
};
#[test]
fn test_iter_nodes() {
for n_nodes in 1usize..16 {
let mut layout =
Layout::<f32>::from_graph(vec![], Nodes::Degree(n_nodes), Settings::default());
let mut hits = iproduct!(0..n_nodes, 0..n_nodes)
.filter(|(n1, n2)| n1 < n2)
.collect::<BTreeSet<(usize, usize)>>();
let points = layout.points.clone();
for n1 in layout.iter_nodes() {
for n2 in n1.n2_iter {
assert!(hits.remove(&(n1.ind, n2.ind)));
assert_eq!(n1.pos, points.get(n1.ind));
assert_eq!(n2.pos, points.get(n2.ind));
}
}
assert!(hits.is_empty());
}
}
#[test]
fn test_iter_par_nodes() {
for n_nodes in 1usize..16 {
let mut layout = Layout::<f32>::from_graph(
vec![],
Nodes::Mass((1..n_nodes + 1).map(|i| i as f32).collect()),
Settings::default(),
);
layout
.speeds
.iter_mut()
.enumerate()
.for_each(|(i, speed)| speed.iter_mut().for_each(|speed| *speed = i as f32));
let hits = Arc::new(RwLock::new(
iproduct!(0..n_nodes, 0..n_nodes)
.filter(|(n1, n2)| n1 < n2)
.collect::<BTreeSet<(usize, usize)>>(),
));
let points = layout.points.clone();
let speeds = layout.speeds.clone();
for chunk_iter in layout.iter_par_nodes(4) {
chunk_iter.for_each(|n1_iter| {
for n1 in n1_iter {
for n2 in n1.n2_iter {
let mut hits = hits.write().unwrap();
assert!(hits.remove(&(n1.ind, n2.ind)));
assert_eq!(n1.pos, points.get(n1.ind));
assert_eq!(n2.pos, points.get(n2.ind));
assert_eq!(n1.speed, speeds.get(n1.ind));
assert_eq!(n2.speed, speeds.get(n2.ind));
assert_eq!(*n1.mass, n1.ind as f32 + 1.);
assert_eq!(*n2.mass, n2.ind as f32 + 1.);
}
}
});
}
assert!(hits.read().unwrap().is_empty());
}
}
}