Skip to main content

webgraph_cli/bench/
bf_visit.rs

1/*
2 * SPDX-FileCopyrightText: 2023 Inria
3 * SPDX-FileCopyrightText: 2023 Sebastiano Vigna
4 * SPDX-FileCopyrightText: 2023 Tommaso Fontana
5 *
6 * SPDX-License-Identifier: Apache-2.0 OR MIT
7 */
8
9use anyhow::Result;
10use clap::Parser;
11use dsi_progress_logger::prelude::*;
12use std::collections::VecDeque;
13use std::path::PathBuf;
14use sux::prelude::BitVec;
15use sux::traits::BitVecOpsMut;
16use webgraph::prelude::*;
17
18#[derive(Parser, Debug)]
19#[command(name = "bf-visit", about = "Benchmarks a breadth-first visit.", long_about = None, next_line_help = true)]
20pub struct CliArgs {
21    /// The basename of the graph.​
22    pub basename: PathBuf,
23    /// Static dispatch (default parameters for the BV format).​
24    #[arg(short = 'S', long = "static")]
25    pub _static: bool,
26    /// Number of repeats (usually to warm up the cache or memory mapping).​
27    #[arg(short = 'R', long, default_value_t = 1)]
28    pub repeats: usize,
29
30    #[clap(long, default_value = "false")]
31    /// Use memory mapping instead of loading the graph into memory.​
32    pub mmap: bool,
33}
34
35pub fn main(args: CliArgs) -> Result<()> {
36    // Loads the graph with the given endianness and load mode, and visits it.
37    macro_rules! load_and_visit {
38        ($E:ty, $mode:ty) => {{
39            let config = BvGraph::with_basename(&args.basename)
40                .mode::<$mode>()
41                .flags(MemoryFlags::TRANSPARENT_HUGE_PAGES | MemoryFlags::RANDOM_ACCESS)
42                .endianness::<$E>();
43            if args._static {
44                visit(config.dispatch::<Static>().load()?)
45            } else {
46                visit(config.load()?)
47            }
48        }};
49    }
50
51    for _ in 0..args.repeats {
52        dispatch_endianness!(&args.basename, |E| if args.mmap {
53            load_and_visit!(E, Mmap)
54        } else {
55            load_and_visit!(E, LoadMmap)
56        })?;
57    }
58    Ok(())
59}
60
61fn visit(graph: impl RandomAccessGraph) -> Result<()> {
62    let num_nodes = graph.num_nodes();
63    let mut seen: BitVec = BitVec::new(num_nodes);
64    let mut queue = VecDeque::new();
65
66    let mut pl = progress_logger![
67        display_memory = true,
68        item_name = "node",
69        local_speed = true,
70        expected_updates = Some(num_nodes),
71    ];
72    pl.start("Visiting graph...");
73
74    for start in 0..num_nodes {
75        if seen[start] {
76            continue;
77        }
78        queue.push_back(start as _);
79        seen.set(start, true);
80
81        while !queue.is_empty() {
82            pl.light_update();
83            let current_node = queue.pop_front().unwrap();
84            for succ in graph.successors(current_node) {
85                if !seen[succ] {
86                    queue.push_back(succ);
87                    seen.set(succ as _, true);
88                }
89            }
90        }
91    }
92
93    pl.done();
94
95    Ok(())
96}