-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathkmeans.rs
164 lines (142 loc) · 4.27 KB
/
kmeans.rs
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
use std::cmp::Ordering;
use std::fs::File;
use std::hash::{Hash, Hasher};
use std::ops::{AddAssign, Div};
use std::time::Instant;
use serde::{Deserialize, Serialize};
use renoir::prelude::*;
#[global_allocator]
static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc;
#[derive(Serialize, Deserialize, Copy, Clone, Debug)]
struct Point {
x: f64,
y: f64,
}
impl Point {
fn distance_to(&self, other: &Point) -> f64 {
((self.x - other.x).powi(2) + (self.y - other.y).powi(2)).sqrt()
}
}
impl AddAssign for Point {
fn add_assign(&mut self, other: Self) {
self.x += other.x;
self.y += other.y;
}
}
impl PartialEq for Point {
fn eq(&self, other: &Self) -> bool {
let precision = 0.1;
(self.x - other.x).abs() < precision && (self.y - other.y).abs() < precision
}
}
impl Eq for Point {}
impl PartialOrd for Point {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl Ord for Point {
fn cmp(&self, other: &Self) -> Ordering {
self.x.total_cmp(&other.x).then(other.y.total_cmp(&self.y))
}
}
impl Hash for Point {
fn hash<H: Hasher>(&self, state: &mut H) {
self.x.to_le_bytes().hash(state);
self.y.to_le_bytes().hash(state);
}
}
impl Div<f64> for Point {
type Output = Self;
fn div(self, rhs: f64) -> Self::Output {
Self {
x: self.x / rhs,
y: self.y / rhs,
}
}
}
fn read_centroids(filename: &str, n: usize) -> Vec<Point> {
let file = File::open(filename).unwrap();
csv::ReaderBuilder::new()
.has_headers(false)
.from_reader(file)
.into_deserialize::<Point>()
.map(Result::unwrap)
.take(n)
.collect()
}
fn select_nearest(point: Point, old_centroids: &[Point]) -> Point {
*old_centroids
.iter()
.map(|c| (c, point.distance_to(c)))
.min_by(|a, b| a.1.partial_cmp(&b.1).unwrap())
.unwrap()
.0
}
#[derive(Clone, Serialize, Deserialize, Default)]
struct State {
iter_count: i64,
old_centroids: Vec<Point>,
centroids: Vec<Point>,
changed: bool,
}
impl State {
fn new(centroids: Vec<Point>) -> State {
State {
centroids,
..Default::default()
}
}
}
fn main() {
let (config, args) = RuntimeConfig::from_args();
if args.len() != 4 {
panic!("Pass the number of centroid, the number of iterations and the dataset path as arguments");
}
let num_centroids: usize = args[1].parse().expect("Invalid number of centroids");
let num_iters: usize = args[2].parse().expect("Invalid number of iterations");
let path = &args[3];
config.spawn_remote_workers();
let env = StreamContext::new(config);
let centroids = read_centroids(path, num_centroids);
assert_eq!(centroids.len(), num_centroids);
let initial_state = State::new(centroids);
let source = CsvSource::<Point>::new(path).has_headers(false);
let res = env
.stream(source)
.replay(
num_iters,
initial_state,
|s, state| {
s.map(move |point| (point, select_nearest(point, &state.get().centroids), 1))
.group_by_avg(|(_p, c, _n)| *c, |(p, _c, _n)| *p)
.drop_key()
},
|update: &mut Vec<Point>, p| update.push(p),
move |state, mut update| {
if state.changed {
state.changed = true;
state.old_centroids.clear();
state.old_centroids.append(&mut state.centroids);
}
state.centroids.append(&mut update);
},
|state| {
state.changed = false;
state.iter_count += 1;
state.centroids.sort_unstable();
state.old_centroids.sort_unstable();
state.centroids != state.old_centroids
},
)
.collect_vec();
let start = Instant::now();
env.execute_blocking();
let elapsed = start.elapsed();
if let Some(res) = res.get() {
let state = &res[0];
eprintln!("Iterations: {}", state.iter_count);
eprintln!("Output: {:?}", state.centroids.len());
}
eprintln!("Elapsed: {elapsed:?}");
}