violin 0.3.0

decentralized network coordinate system using the vivaldi algorithm
Documentation
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
//! # `violin`
//!
//! ![Rust Version][rustc-image]
//! [![crates.io][crate-image]][crate-link]
//! [![Documentation][docs-image]][docs-link]
//! [![Dependency Status][deps-image]][deps-link]
//!
//! A Rust `no_std` no `alloc` implementation of the [Vivaldi algorithm][1](PDF)
//! for a network coordinate system.
//!
//! A network coordinate system allows nodes to accurately estimate network
//! latencies by merely exchanging coordinates.
//!
//!
//! <!-- vim-markdown-toc GFM -->
//!
//! * [Violin - The Pitch](#violin---the-pitch)
//! * [Violin - The Anit-Pitch](#violin---the-anit-pitch)
//! * [Compile from Source](#compile-from-source)
//! * [Usage](#usage)
//! * [Benchmarks](#benchmarks)
//!     * [Notes on `no_std` Performance](#notes-on-no_std-performance)
//! * [License](#license)
//!     * [Contribution](#contribution)
//! * [Related Papers and Research](#related-papers-and-research)
//!
//! <!-- vim-markdown-toc -->
//!
//! ## Violin - The Pitch
//!
//! Violin is an implementation of Vivaldi network coordinates that works in
//! `no_std` and no `alloc` environments. Each coordinate is small consisting of
//! a dimensional vector made up of an array of `f64`s. The arrays use const
//! generics, so they can be as small as a single f64 or large as one needs.
//! Although above a certain dimension there are diminishing returns.
//!
//! Nodes can measure real latencies between an origin node, or each-other to
//! adjust their coordinates in space.
//!
//! The real power comes from being able to calculate distance between a remote
//! coordinate without ever having done a real latency check. For example node
//! `A` measures against node `Origin`, node `B` does the same. Then `A` can be
//! given the coordinates to `B` and accurately estimate the latency without
//! ever having measured `B` directly.
//!
//! ## Violin - The Anit-Pitch
//!
//! Vivaldi isn't a magic bullet and still requires measuring real latencies to
//! adjust the coordinates. In a naive implementation, conducting a latency
//! check prior to a coordinate calculation is not much better than just using
//! the latency check directly as the answer. However, this is not how it's
//! supposed to be used.
//!
//! Transferring a Violin coordinate in practice can be comparable data to a
//! small set of ICMP messages. For example an 8-Dimension coordinate (plus
//! three additional `f64`s of metadata) is 88 bytes. However, unlike ICMP
//! messages, the Violin coordinates are a single transmission and only need to
//! be re-transmitted on significant change. Work could even be done to only
//! transmit deltas as well.
//!
//! ## Compile from Source
//!
//! Ensure you have a [Rust toolchain installed][rustup].
//!
//! ```notrust
//! $ git clone https://github.com/kbknapp/violin
//! $ cd violin
//! $ RUSTFLAGS='-Ctarget-cpu=native' cargo build --release
//! ```
//!
//! **NOTE:** The `RUSTFLAGS` can be omitted. However, if on a recent CPU that
//! supports SIMD instructions, and the code will be run on the same CPU it's
//! compiled for, including this flag can improve performance.
//!
//! ## Usage
//!
//! See the `examples/` directory in this repository for complete details,
//! although at quick glance creating three coordinates (`origin`, `a` and `b`)
//! and updating `a` and `b`'s coordinate from experienced real latency would
//! look like this:
//!
//! ```notrust
//! use std::time::Duration;
//! use violin::{heapless::VecD, Coord, Node};
//!
//! // Create two nodes and an "origin" coordinate, all using a 4-Dimensional
//! // coordinate. `VecD` is a dimensional vector.
//! let origin = Coord::<VecD<4>>::rand();
//! let mut a = Node::<VecD<4>>::rand();
//! let mut b = Node::<VecD<4>>::rand();
//!
//! // **conduct some latency measurement from a to origin**
//! // let's assume we observed a value of `0.2` seconds...
//! //
//! // **conduct some latency measurement from b to origin**
//! // let's assume we observed a value of `0.03` seconds...
//!
//! a.update(Duration::from_secs_f64(0.2), &origin);
//! b.update(Duration::from_secs_f64(0.03), &origin);
//!
//! // Estimate from a to b even though we never measured them directly
//! println!(
//!     "a's estimate to b: {:.2}ms",
//!     a.distance_to(&b.coordinate()).as_millis()
//! );
//! ```
//!
//! ## Benchmarks
//!
//! A set of benchmarks are included using 8D, 4D, and 2D coordinates both using
//! `heap::VecD` (requires the `alloc` feature) and `heapless::VecD`.
//!
//! The benchmarks measure both the higher level `Node` as well as a lower level
//! `Coord` abstractions.
//!
//! To measure we create 10,000 coordinates and the coordinates are
//! update for each coordinate 100 times, totaling 1,000,000 updates.
//!
//! On my 8 core AMD Ryzen 7 5850U laptop with 16GB RAM the benchmarks look as
//! follows:
//!
//! | Abstraction | Memory   | Dimensions | Time |
//! | :-: | :-:      | :-:        | :-:  |
//! | `Node` | heap     | 8          | 66.537 ms |
//! | `Coord` | heap     | 8          | 55.402 ms |
//! | `Node` | heapless | 8          | 24.997 ms |
//! | `Coord` | heapless | 8          | 16.552 ms |
//! | `Node` | heap     | 4          | 49.501 ms |
//! | `Coord` | heap     | 4          | 39.163 ms |
//! | `Node` | heapless | 4          | 16.795 ms |
//! | `Coord` | heapless | 4          | 11.780 ms |
//! | `Node` | heap     | 2          | 54.363 ms |
//! | `Coord` | heap     | 2          | 46.001 ms |
//! | `Node` | heapless | 2          | 13.181 ms |
//! | `Coord` | heapless | 2          | 10.916 ms |
//!
//! To run the benchmarks yourself use `RUSTFLAGS='-Ctarget-cpu=native' cargo
//! bench`.
//!
//! ### Notes on `no_std` Performance
//!
//! The `no_std` version is _much_ slower because it cannot use platform
//! intrinsics for square roots, floating point rounding, etc. Instead these
//! functions had to be hand written.
//!
//! Additionally, the `no_std` square root functions round up to 8 decimals of
//! precision.
//!
//! One should realistically only use the `no_std` version when there is a good
//! reason to do so, such as an embedded device that absolutely does not support
//! `std`.
//!
//! A single Vivaldi calculation only requires one square root calculation per
//! distance estimate. So pragmatically, it should be rare where such a device
//! is _also_ needing to calculate thousands of square root operations per
//! second.
//!
//! ## License
//!
//! This crate is licensed under either of
//!
//!  * [Apache License, Version 2.0](http://www.apache.org/licenses/LICENSE-2.0)
//!  * [MIT license](http://opensource.org/licenses/MIT)
//!
//! at your option.
//!
//! ### Contribution
//!
//! Unless you explicitly Node otherwise, any contribution intentionally
//! submitted for inclusion in the work by you, as defined in the Apache-2.0
//! license, shall be dual licensed as above, without any additional terms or
//! conditions.
//!
//! ## Related Papers and Research
//!
//! - [Vivaldi - A Decentralized Network Coordinate System][1](PDF)
//! - [Network Coordinates in the Wild][2](PDF)
//! - [Towards Network Triangle Inequality Violation Aware Distributed
//!   Systems][3](PDF)
//! - [On Suitability of Euclidean Embedding for Host-based Network Coordinate
//!   Systems][4](PDF)
//! - [Practical, Distributed Network Coordinates][5](PDF)
//! - [Armon Dadgar on Vivaldi: Decentralized Network Coordinate
//!   System][6](Video)
//!
//! [//]: # (badges)
//!
//! [rustc-image]: https://img.shields.io/badge/rustc-1.59+-blue.svg
//! [crate-image]: https://img.shields.io/crates/v/violin.svg
//! [crate-link]: https://crates.io/crates/violin
//! [docs-image]: https://docs.rs/violin/badge.svg
//! [docs-link]: https://docs.rs/violin
//! [deps-image]: https://deps.rs/repo/github/kbknapp/violin/status.svg
//! [deps-link]: https://deps.rs/repo/github/kbknapp/violin
//!
//! [//]: # (links)
//!
//! [rustup]: https://rustup.rs
//! [1]: https://pdos.csail.mit.edu/papers/vivaldi:sigcomm/paper.pdf
//! [2]: https://www.usenix.org/legacy/event/nsdi07/tech/full_papers/ledlie/ledlie.pdf
//! [3]: https://www.cs.rice.edu/~eugeneng/papers/IMC07.pdf
//! [4]: https://www-users.cse.umn.edu/~zhang089/Papers/Lee-Suitability-tonfinal.pdf
//! [5]: http://www.news.cs.nyu.edu/~jinyang/pub/hotnets03.pdf
//! [6]: https://youtu.be/AszPoJjWK9Q?t=1690
#![deny(
    missing_docs,
    missing_debug_implementations,
    missing_copy_implementations,
    trivial_casts,
    unused_allocation,
    trivial_numeric_casts
)]
#![forbid(unsafe_code)]
#![cfg_attr(not(feature = "std"), no_std)]
#![cfg_attr(docsrs, feature(doc_cfg))]

#[cfg(any(feature = "std", test))]
#[macro_use]
extern crate std;

// When we're building for a no-std target, we pull in `core`, but alias
// it as `std` so the `use` statements are the same between `std` and `core`.
#[cfg(all(not(feature = "std"), not(test)))]
#[macro_use]
extern crate core as std;

#[cfg(feature = "alloc")]
extern crate alloc;

use crate::std::{
    default::Default,
    ops::{Add, AddAssign, Div, Mul},
};

#[macro_use]
mod macros;
mod coord;
pub mod error;
#[cfg(feature = "alloc")]
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
pub mod heap;
pub mod heapless;
mod node;

pub use coord::Coord;
#[cfg(feature = "alloc")]
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
pub use heap::VecD;
pub use node::{Config, Node};

/// Determines at what threshold two coordinates overlap
const OVERLAP_THRESHOLD: f64 = 1.0e-6;
const DEFAULT_HEIGHT_MIN: f64 = 0.0;

/// The abstraction over coordinate vectors
pub trait Vector:
    Default
    + Add<Self, Output = Self>
    + Mul<f64, Output = Self>
    + AddAssign<Self>
    + Div<f64, Output = Self>
    + AsRef<[f64]>
    + AsMut<[f64]>
where
    Self: Sized,
{
    /// The length of the vector
    const LEN: usize;

    /// Returns a unit vector (`â`) from `other` pointing at `self` along
    /// with the magnitude of the difference beand tween both vectors
    fn unit_vector_from(&self, other: &Self) -> (f64, Self) {
        let diff = self.difference(other);
        let mag = diff.magnitude();
        // If the coordinates overlap return a unit vector in the first dimension
        if mag < OVERLAP_THRESHOLD {
            let mut ret = Self::default();
            ret.as_mut()[0] = 1.0;
            return (0.0, ret);
        }
        (mag, diff * (1. / mag))
    }

    /// Returns distance between `self` and `other`
    #[cfg_attr(feature = "std", doc = "```rust")]
    #[cfg_attr(not(feature = "std"), doc = "```no_run")]
    /// use violin::{heapless::VecD, Vector};
    ///
    /// let a = VecD::from([1., 0., 5.]);
    /// let b = VecD::from([0., 2., 4.]);
    ///
    /// assert_eq!(a.distance(&b), 2.449489742783178);
    /// ```
    fn distance(&self, other: &Self) -> f64 { self.difference(other).magnitude() }

    /// Returns the difference between two vectors
    ///
    /// ```rust
    /// use violin::{heapless::VecD, Vector};
    ///
    /// let a = VecD::from([1.0, -3.0, 3.0]);
    /// let b = VecD::from([-4.0, 5.0, 6.0]);
    ///
    /// assert_eq!(a.difference(&b), VecD::from([5.0, -8.0, -3.0]));
    /// assert_eq!(a.difference(&VecD::default()), a);
    fn difference(&self, other: &Self) -> Self;

    /// Returns the magnitude of the vector `v` (`|v|`) represented by `self`
    ///
    /// ```rust
    /// use violin::{heapless::VecD, Vector};
    ///
    /// let a = VecD::from([1.0, -2.0, 3.0]);
    /// let b = VecD::from([-2., 4., -4.]);
    /// assert_eq!(a.magnitude(), 3.7416573867739413);
    /// assert_eq!(b.magnitude(), 6.0f64);
    /// ```
    #[cfg(feature = "std")]
    fn magnitude(&self) -> f64 { self.magnitude2().sqrt() }

    /// Returns the magnitude of the vector `v` (`|v|`) represented by `self`
    #[cfg_attr(feature = "std", doc = "```rust")]
    #[cfg_attr(not(feature = "std"), doc = "```no_run")]
    /// use violin::{heapless::VecD, Vector};
    ///
    /// let a = VecD::from([1.0, -2.0, 3.0]);
    /// let b = VecD::from([-2., 4., -4.]);
    /// assert_eq!(a.magnitude(), 3.7416573867739413);
    /// assert_eq!(b.magnitude(), 6.0f64);
    /// ```
    #[cfg(not(feature = "std"))]
    fn magnitude(&self) -> f64 { _sqrt(self.magnitude2()) }

    /// Returns the magnitude of the vector `v` (`|v|`) represented by `self`
    /// **without** performing the expensive square root operation
    /// ```rust
    /// use violin::{heapless::VecD, Vector};
    ///
    /// let a = VecD::from([1.0, -2.0, 3.0]);
    /// let b = VecD::from([-2., 4., -4.]);
    /// let c = VecD::from([0., 0., 0.]);
    /// assert_eq!(a.magnitude2(), 14.0);
    /// assert_eq!(b.magnitude2(), 36.0);
    /// assert_eq!(c.magnitude2(), 0.0);
    /// ```
    fn magnitude2(&self) -> f64;
}

#[cfg(not(feature = "std"))]
const PRECISION_INC: f64 = 1.0e-8;
#[cfg(not(feature = "std"))]
const PRECISION_POW: f64 = 1.0e+8;

#[cfg(not(feature = "std"))]
fn _sqrt(n: f64) -> f64 {
    if n == 0.0 {
        return 0.0;
    }
    let mut ans;
    let mut last;
    let mut mid = n / 2.0;
    let mut top = n;
    let r_n = _round(n);

    loop {
        last = mid;
        let sq = _round(mid * mid);
        if sq == r_n {
            ans = mid;
            break;
        }
        if sq > r_n {
            mid = last / 2.0;
            top = last;
        } else {
            mid += (top - mid) / 2.0;
        }
        if mid == last {
            mid += 1.0;
        }
    }

    ans = f64::max(ans - 1.0, 0.0);
    mid = 0.5;
    loop {
        last = mid;
        let sq = (mid + ans) * (mid + ans);
        let r_sq = _round(sq);
        if r_sq == r_n {
            ans += mid;
            break;
        }
        if sq > n {
            mid = last / 2.0;
            top = last;
        } else {
            mid += (top - mid) / 2.0;
        }

        if mid == last {
            mid += PRECISION_INC;
        }
    }

    _round(ans)
}

#[inline(always)]
#[cfg(not(feature = "std"))]
fn _round(n: f64) -> f64 { _ceil((n * PRECISION_POW) - 0.49999999) / PRECISION_POW }

#[inline(always)]
#[cfg(not(feature = "std"))]
fn _ceil(n: f64) -> f64 {
    let n_int = n as u64;
    if n > n_int as f64 {
        (n_int + 1) as f64
    } else {
        n
    }
}

#[cfg(test)]
mod tests {
    #[cfg(not(feature = "std"))]
    use super::*;

    #[cfg_attr(not(feature = "std"), test)]
    #[cfg(not(feature = "std"))]
    fn test_sqrt() {
        assert_eq!(_sqrt(36.0), 6.0);
        assert_eq!(_sqrt(27.2934), 5.22430857);
        assert_eq!(_sqrt(8.408207478410603), 2.89969093);
        assert_eq!(_sqrt(158.57), 12.59245806);
        assert_eq!(_sqrt(0.0), 0.0);
        assert_eq!(_sqrt(0.0), 0.0);
        assert_eq!(_sqrt(0.009983505350056349), 0.0999175);
    }
}