counting-networks 0.1.3

Lock-free data structures for concurrent counting
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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
use std::sync::atomic::{self, AtomicUsize};
use std::ptr::NonNull;
use std::heap::{Alloc, Heap, Layout};
use std::collections::HashSet;
use std::thread;
use std::cmp;
use std::ops::Range;
use std::collections::VecDeque;
use std::fmt;
use std::hash::{Hash, Hasher};

use util::{binomial_coefficient, hash_single, log2_floor};

/// A type of counting network
///
/// See [the module level documentation](index.html) for general information about counting networks.
/// 
/// A bitonic network is constructed recurisvely. A rough pseudocode implementatio would look like
/// ```text
/// fn bitonic(width):
///     upper_wires = bitonic(width / 2)
///     lower_wires = bitonic(width / 2)
///
///     output = merge(upper_wires, lower_wires)
///     return output
/// ```
///
/// The construction of a ``Bitonic[8]`` looks like:
/// ```text
///      ┌────────────────┐          ┌──────────────┐
/// ─────┤                ├──────────┤              ├──────────
/// ─────┤   Bitonic[4]   ├──────────┤              ├──────────
/// ─────┤                ├──────────┤              ├──────────
/// ─────┤                ├──────────┤              ├──────────
///      └────────────────┘          │   Merge[8]   │
///      ┌────────────────┐          │              │
/// ─────┤                ├──────────┤              ├──────────
/// ─────┤   Bitonic[4]   ├──────────┤              ├──────────
/// ─────┤                ├──────────┤              ├──────────
/// ─────┤                ├──────────┤              ├──────────
///      └────────────────┘          └──────────────┘
/// ```
///
/// The base case for ``Bitonic[w]`` is ``Bitonic[1]`` which is a no op, the single wire is 
/// unchanced. The real work of the recursive construction occurs in the ``Merge[w]`` element.
/// The base case of the ``Merge[w]`` network is ``Merge[2]`` which consists of a single balancer.
/// ``Merge[8]`` can be visualized as:
///
/// ```text
///                           ┌────────────────┐
/// x0 ───────────────────────┤                ├─────────────┲┱── y0
/// x1 ─────┐ ┌───────────────┤    Merge[4]    ├────┐  ┌─────┺┹── y1
/// x2 ─────┼─┘ ┌─────────────┤                ├───┐└──┼─────┲┱── y2
/// x3 ───┐ │   │ ┌───────────┤                ├─┐ │ ┌─┼─────┺┹── y3
///       │ └───┼─┼───────┐   └────────────────┘ └─┼─┼─┼─┐
///       └─────┼─┼─────┐ │   ┌────────────────┐   └─┼─┼─┼─┐
/// x4 ─────────┼─┼───┐ │ └───┤                ├─────┼─┘ │ └─┲┱── y4
/// x5 ─────────┘ │   │ └─────┤    Merge[4]    ├─────┘┌──┼───┺┹── y5
/// x6 ───────────┼─┐ └───────┤                ├──────┘  └───┲┱── y6
/// x7 ───────────┘ └─────────┤                ├─────────────┺┹── y7
///                           └────────────────┘
/// ┏┓
/// ┗┛ are balancers, xi is the ith wire, yi is the ith output
/// ```
///
/// For the input wires, the even numbered wires (0, 2) of the top inputs to the top 2 slots
/// of the top ``Merge[4]`` network, while the odd numbered wires of (1, 3) of the top 
/// inputs go to top of the 2 slots of the bottom ``Merge[4]`` network. This is flipped for the 
/// bottom 4 inputs, where the odd numbered inputs (5, 7) go to the upper ``Merge[4]`` network,
/// while the evens go to the bottom ``Merge[4]`` network.
pub struct BitonicNetwork<L> {
    // Width of the network
    width: usize,
    // Outputs of the network
    outputs: Vec<NonNull<L>>,
    // Pointers to balancer's memory locations
    balancers: Vec<NonNull<InternalBalancer<L>>>,
}

enum Balancer<L> {
    Internal(NonNull<InternalBalancer<L>>),
    Leaf(NonNull<L>),
}

// Align struct to cache size (Intel)
// This prevents false sharing of the balancer between multiple cores.
#[repr(align(64))]
struct InternalBalancer<L> {
    value: AtomicUsize,
    outputs: [Balancer<L>; 2],
}

impl<L> InternalBalancer<L> {
    fn new() -> Self {
        InternalBalancer {
            value: AtomicUsize::new(0),
            outputs: [
                Balancer::Internal(NonNull::dangling()),
                Balancer::Internal(NonNull::dangling()),
            ],
        }
    }

    fn next<'a>(&'a self) -> &'a Balancer<L> {
        let next_index = self.toggle_up();
        unsafe { self.outputs.get_unchecked(next_index) }
    }

    fn toggle_up(&self) -> usize {
        let old_value = self.value.fetch_add(1, atomic::Ordering::SeqCst);
        old_value % 2
    }
}

impl<L> Balancer<L> {
    fn is_leaf(&self) -> bool {
        match self {
            &Balancer::Internal(_) => false,
            &Balancer::Leaf(_) => true,
        }
    }

    fn leaf_ref(&self) -> &NonNull<L> {
        match self {
            &Balancer::Internal(_) => {
                panic!("called `Balancer::unwrap_leaf()` on a `Internal` value")
            }
            &Balancer::Leaf(ref value) => value,
        }
    }
}

impl<L> BitonicNetwork<L> {
    /// Construct a new network with given width (which must be a power of 2) and outputs.
    ///
    /// Outputs must be ordered corresponding to how they should appear in the network.
    ///
    /// For example in a 4-width network:
    /// ```text
    /// xi = ith input
    /// yi = ith output
    ///
    /// x1 ─────╥────╥─────╥─── y1
    /// x2 ─────╨────║──╥──╨─── y2
    /// x3 ─────╥────║──╨──╥─── y3
    /// x4 ─────╨────╨─────╨─── y4
    /// ```
    /// The outputs passed through should appear [y1, y2, y3, y4]
    ///
    /// # Examples
    ///
    /// ```
    /// use counting_networks::networks::BitonicNetwork;
    ///
    /// let outputs = vec![1, 2, 3, 4];
    ///
    /// let network = BitonicNetwork::new(outputs);
    ///
    /// assert_eq!(network.width(), 4);
    /// assert_eq!(network.outputs(), vec![&1, &2, &3, &4]);
    /// ```
    pub fn new(outputs: Vec<L>) -> Self {
        assert!(outputs.len().is_power_of_two());

        let width = outputs.len();
        let allocated_outputs = outputs
            .into_iter()
            .map(|output: L| {
                let output_location = Heap.alloc_one::<L>().unwrap();
                unsafe {
                    output_location.as_ptr().write(output);
                }
                output_location
            })
            .collect::<Vec<_>>();

        let num_layers = num_layers(width);
        let layer_width = width / 2;

        let mut wires: Vec<Wire<L>> = construct_bitonic(width, 0);
        debug_assert_eq!(wires.len(), allocated_outputs.len());
        debug_assert_eq!(
            num_layers * layer_width * 2,
            wires.iter().map(|w| w.num_balancers()).sum::<usize>()
        );

        // For each wire, attach the output. This assumes that the outputs are ordered
        // corresponding to the way they should be arranged in the network, e.g.
        // [output for wire 0, output for wire 2, output for wire 3, ...]
        for (wire, output) in wires.iter().zip(allocated_outputs.iter()) {
            let (last_balancer, up) = wire.last();
            unsafe {
                (*last_balancer.as_ptr()).outputs[up as usize] = Balancer::Leaf(output.clone());
            }
        }

        let mut network = BitonicNetwork {
            width,
            outputs: allocated_outputs,
            balancers: Vec::with_capacity(num_layers * layer_width),
        };

        // For each layer in network, fill it with balancers. This method will allow easy
        // access to inputs in the traverse call.
        for _ in 0..num_layers {
            let mut unique_balancers: HashSet<*mut InternalBalancer<L>> = HashSet::new();
            let mut layer: Vec<(usize, NonNull<InternalBalancer<L>>)> = Vec::new();

            for wire in wires.iter_mut() {
                if let Some((balancer, _)) = wire.pop_front() {
                    if !unique_balancers.contains(&balancer.as_ptr()) {
                        unique_balancers.insert(balancer.as_ptr());

                        layer.push((wire.value, balancer));
                    }
                }
            }

            layer.sort_by_key(|&(idx, _)| idx);

            network
                .balancers
                .extend(layer.into_iter().map(|(_, ptr)| ptr));
        }

        network
    }

    /// Returns the width of the network.
    ///
    /// # Examples
    ///
    /// ```
    /// use counting_networks::networks::BitonicNetwork;
    ///
    /// let network = BitonicNetwork::new(vec![1, 2, 3, 4]);
    ///
    /// assert_eq!(network.width(), 4);
    /// ```
    pub fn width(&self) -> usize {
        self.width
    }

    /// Traverse the network and obtain a reference to an output element.
    ///
    /// # Examples
    ///
    /// ```
    /// use counting_networks::networks::BitonicNetwork;
    ///
    /// let network = BitonicNetwork::new(vec![1, 2, 3, 4]);
    ///
    /// assert_eq!(network.traverse(), &1);
    /// assert_eq!(network.traverse(), &2);
    /// assert_eq!(network.traverse(), &3);
    /// assert_eq!(network.traverse(), &4);
    /// ```
    pub fn traverse(&self) -> &L {
        let input_slot = hash_single(thread::current().id()) % (self.width as u64);

        let mut current: &Balancer<L> = unsafe {
            self.balancers[get_layer_range(0, self.width / 2)][input_slot as usize / 2]
                .as_ref()
                .next()
        };

        while let &Balancer::Internal(ref balancer) = current {
            current = unsafe { balancer.as_ref().next() };
        }

        assert!(current.is_leaf());
        unsafe { current.leaf_ref().as_ref() }
    }

    /// Get references to all the outputs of the network.
    ///
    /// # Examples
    ///
    /// ```
    /// use counting_networks::networks::BitonicNetwork;
    ///
    /// let network = BitonicNetwork::new(vec![1, 2, 3, 4]);
    ///
    /// assert_eq!(network.outputs(), vec![&1, &2, &3, &4]);
    /// ```
    pub fn outputs(&self) -> Vec<&L> {
        self.outputs.iter().map(|v| unsafe { v.as_ref() }).collect()
    }
}

impl<L: PartialEq> PartialEq for BitonicNetwork<L> {
    fn eq(&self, other: &Self) -> bool {
        let output_refs = self.outputs.iter().map(|v| unsafe { v.as_ref() });
        let other_outputs = other.outputs.iter().map(|v| unsafe { v.as_ref() });

        output_refs.eq(other_outputs)
    }
}

impl<L: Eq> Eq for BitonicNetwork<L> {}

impl<L: Hash> Hash for BitonicNetwork<L> {
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.width.hash(state);
        self.outputs
            .iter()
            .map(|v| unsafe { v.as_ref() })
            .for_each(|output| {
                output.hash(state);
            });
    }
}

impl<L: Clone> Clone for BitonicNetwork<L> {
    fn clone(&self) -> Self {
        let outputs: Vec<L> = self.outputs
            .iter()
            .map(|v| unsafe { v.as_ref() })
            .cloned()
            .collect();

        BitonicNetwork::new(outputs)
    }
}

impl<L: fmt::Debug> fmt::Debug for BitonicNetwork<L> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.debug_struct("BitonicNetwork")
            .field("width", &self.width)
            .field(
                "outputs",
                &self.outputs
                    .iter()
                    .map(|v| unsafe { v.as_ref() })
                    .collect::<Vec<_>>(),
            )
            .finish()
    }
}

unsafe impl<L> Send for BitonicNetwork<L> {}
unsafe impl<L> Sync for BitonicNetwork<L> {}

impl<L> Drop for BitonicNetwork<L> {
    fn drop(&mut self) {
        // Drop each internal balancer, leaving NonNull pointers to output
        // Then dealloc balancer memory
        let balancer_layout = Layout::new::<InternalBalancer<L>>();
        for balancer_ptr in self.balancers.iter_mut() {
            unsafe {
                let raw_ptr = balancer_ptr.as_ptr();
                raw_ptr.drop_in_place();
                Heap.dealloc(raw_ptr as *mut u8, balancer_layout.clone());
            }
        }

        // For each output allocation, drop output and dealloc
        let output_layout = Layout::new::<L>();
        for output_ptr in self.outputs.iter_mut() {
            unsafe {
                let raw_ptr = output_ptr.as_ptr();
                raw_ptr.drop_in_place();
                Heap.dealloc(raw_ptr as *mut u8, output_layout.clone());
            }
        }
    }
}

impl<L> From<Vec<L>> for BitonicNetwork<L> {
    fn from(src: Vec<L>) -> Self {
        BitonicNetwork::new(src)
    }
}

fn num_layers(width: usize) -> usize {
    binomial_coefficient((log2_floor(width as u64) + 1) as u64, 2) as usize
}

fn get_layer_range(layer_idx: usize, layer_width: usize) -> Range<usize> {
    let start = layer_width * layer_idx;
    let end = layer_width * (layer_idx + 1);

    start..end
}

struct Wire<L> {
    balancer_history: VecDeque<(NonNull<InternalBalancer<L>>, bool)>,
    value: usize,
}

impl<L> PartialEq for Wire<L> {
    fn eq(&self, other: &Self) -> bool {
        self.value == other.value
    }
}

impl<L> PartialOrd for Wire<L> {
    fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> {
        self.value.partial_cmp(&other.value)
    }
}

impl<L> Eq for Wire<L> {}

impl<L> Ord for Wire<L> {
    fn cmp(&self, other: &Self) -> cmp::Ordering {
        self.value.cmp(&other.value)
    }
}

impl<L> Wire<L> {
    fn num_balancers(&self) -> usize {
        self.balancer_history.len()
    }

    fn last(&self) -> (NonNull<InternalBalancer<L>>, bool) {
        self.balancer_history[self.balancer_history.len() - 1]
    }

    fn add(&mut self, balancer: NonNull<InternalBalancer<L>>, up: bool) {
        self.balancer_history.push_back((balancer, up));
    }

    fn pop_front(&mut self) -> Option<(NonNull<InternalBalancer<L>>, bool)> {
        self.balancer_history.pop_front()
    }
}

fn split_even_odd<L>(wires: Vec<Wire<L>>) -> (Vec<Wire<L>>, Vec<Wire<L>>) {
    let (even_wires, odd_wires): (Vec<(usize, Wire<L>)>, Vec<(usize, Wire<L>)>) = wires
        .into_iter()
        .enumerate()
        .partition(|&(idx, _)| idx % 2 == 0);

    let even = even_wires
        .into_iter()
        .map(|(_, value)| value)
        .collect::<Vec<_>>();

    let odd = odd_wires
        .into_iter()
        .map(|(_, value)| value)
        .collect::<Vec<_>>();

    (even, odd)
}

fn merge_wires<L>(upper: Vec<Wire<L>>, lower: Vec<Wire<L>>) -> Vec<Wire<L>> {
    let pairs = upper.into_iter().zip(lower.into_iter());

    let mut wires = Vec::new();
    for (mut upper_wire, mut lower_wire) in pairs {
        debug_assert_eq!(upper_wire.num_balancers(), lower_wire.num_balancers());

        let new_balancer = InternalBalancer::new();
        let new_balancer_alloc = Heap.alloc_one::<InternalBalancer<L>>().unwrap();
        unsafe {
            new_balancer_alloc.as_ptr().write(new_balancer);

            if upper_wire.num_balancers() > 0 {
                let (last, up) = upper_wire.last();
                let mut temp = last.as_ptr().read();
                temp.outputs[up as usize] = Balancer::Internal(new_balancer_alloc);
                last.as_ptr().write(temp);
            }

            if lower_wire.num_balancers() > 0 {
                let (mut last, up) = lower_wire.last();
                let mut temp = last.as_ptr().read();
                temp.outputs[up as usize] = Balancer::Internal(new_balancer_alloc);
                last.as_ptr().write(temp);
            }
        }

        upper_wire.add(new_balancer_alloc, false);
        lower_wire.add(new_balancer_alloc, true);

        wires.push(upper_wire);
        wires.push(lower_wire);
    }

    wires
}

fn construct_bitonic<L>(width: usize, wire_index: usize) -> Vec<Wire<L>> {
    if width == 1 {
        vec![
            Wire {
                balancer_history: VecDeque::new(),
                value: wire_index,
            },
        ]
    } else {
        let upper_result = construct_bitonic(width / 2, wire_index);
        let lower_result = construct_bitonic(width / 2, wire_index + width / 2);

        merge_networks(upper_result, lower_result)
    }
}

fn merge_networks<L>(upper: Vec<Wire<L>>, lower: Vec<Wire<L>>) -> Vec<Wire<L>> {
    if upper.len() + lower.len() == 2 {
        merge_wires(upper, lower)
    } else {
        let (upper_even, upper_odd) = split_even_odd(upper);
        let (lower_even, lower_odd) = split_even_odd(lower);

        let upper_result = merge_networks(upper_even, lower_odd);
        let lower_result = merge_networks(upper_odd, lower_even);

        merge_wires(upper_result, lower_result)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn is_send() {
        fn send_only<T: Send>(_: T) {}

        send_only(BitonicNetwork::new(vec![1; 4]));
    }

    #[test]
    fn is_sync() {
        fn sync_only<T: Sync>(_: T) {}

        sync_only(BitonicNetwork::new(vec![1; 4]));
    }

    #[test]
    fn initialize_network() {
        const WIDTH: usize = 16;

        let network = BitonicNetwork::new(vec![1; WIDTH]);

        assert_eq!(network.width(), WIDTH);
    }

    #[test]
    #[should_panic]
    fn initialize_network_bad_width() {
        let _ = BitonicNetwork::new(vec![1, 2, 3]);
    }

    #[test]
    fn traverse_network() {
        const WIDTH: usize = 16;
        let outputs = (1..(WIDTH + 1)).collect::<Vec<_>>();
        let network = BitonicNetwork::new(outputs);

        for output in 1..(WIDTH + 1) {
            assert_eq!(network.traverse(), &output);
        }
    }
}